module cli pour appels en ligne de commande
[mtweb] / mw / env / modules / mw_env_cli.php
1 <?php
2
3   class mw_env_cli extends mw_env{
4
5     var $argv;
6     var $etat;
7     var $login;
8     var $password;
9     var $params;
10     var $INITED;
11
12     function run_cli($argv){
13       if(PHP_SAPI != "cli"){
14         $this->INITED = false;
15         return "php cli uniquement";
16       }
17       if(!isset($argv) || !$argv){
18         $this->INITED = false;
19         return "variable argv non disponible";
20       }
21       $this->argv = $argv;
22       $this->etat = "";
23       $this->login = "";
24       $this->password = "";
25       $this->params = array();
26       $this->INITED = true;
27       if(
28             (($res = $this->parse_cli_arguments()) !== true)
29         ||  (($res = $this->init_cli_user()) !== true)
30       ){
31         return $res;
32       }
33       $this->run($this->etat, array("get" => $this->params));
34       return true;
35     }
36
37     function inited(){
38       return isset($this->INITED) && $this->INITED;
39     }
40
41     function parse_cli_arguments(){
42       if(!$this->inited()) return "cli not inited";
43       $FIRST = false;
44       $SECOND = false;
45       foreach($this->argv as $cmd_arg){
46         if(!$FIRST){
47           $FIRST = true;
48           continue;
49         }
50         elseif(!$SECOND){
51           $SECOND = true;
52           if(($k = strpos($cmd_arg, "=")) === false){
53             $this->etat = $cmd_arg;
54             if(($k = strpos($this->etat, "@")) !== false){
55               $this->login = explode(":", substr($this->etat, 0, $k));
56               if(isset($this->login[1])) $this->password = $this->login[1];
57               $this->login = $this->login[0];
58               $this->etat = substr($this->etat, $k + 1);
59             }
60             continue;
61           }
62         }
63         if(strlen($cmd_arg)){
64           if(($k = strpos($cmd_arg, "=")) === false){
65             $this->params[$cmd_arg] = "";
66           }
67           elseif($k != 0){
68             $this->params[substr($cmd_arg, 0, $k)] = substr($cmd_arg, $k + 1);
69           }
70         }
71       }
72       $this->etat = $this->valid_etat($this->etat);
73       if($this->etat === false) return "etat invalide";
74       return true;
75     }
76
77     function init_cli_user(){
78       if(!$this->inited()) return "cli not inited";
79       $data = $this->data();
80       if($this->login){
81         if(!$this->password){
82           $this->password = $this->cmd_prompt("password: ", true);
83           echo "\n";
84         }
85         if(!($user = $data->user($this->login)) || (md5($this->password) != $user["password"])){
86           return "indentification incorrecte";
87         }
88         $data->set_session_user($user);
89       }
90       if(!$this->action_allowed($this->etat, false)){
91         $this->login = $this->cmd_prompt("login: ");
92         $this->password = $this->cmd_prompt("password: ", true);
93         echo "\n";
94         if(!($user = $data->user($this->login)) || (md5($this->password) != $user["password"])){
95           return "indentification incorrecte";
96         }
97         $data->set_session_user($user);
98         if(!$this->action_allowed($this->etat, false)){
99           return "permission refusee pour cette action";
100         }
101       }
102       return true;
103     }
104
105     function cmd_prompt($prompt, $SILENT = false){
106       if(preg_match('/^win/i', PHP_OS)){
107         $vbscript = sys_get_temp_dir().'prompt_password.vbs';
108         file_put_contents(
109           $vbscript,
110           'wscript.echo(InputBox("'
111           .addslashes($prompt)
112           .'", "", "'.$prompt.'"))'
113         );
114         $command = "cscript //nologo " . escapeshellarg($vbscript);
115         $password = rtrim(shell_exec($command));
116         unlink($vbscript);
117         return $password;
118       }
119       else{
120         $command = "/usr/bin/env bash -c 'echo OK'";
121         if(rtrim(shell_exec($command)) !== 'OK') return false;
122         $command =
123          "/usr/bin/env bash -c 'read".($SILENT ? " -s" : "")." -p \""
124         .addslashes($prompt)
125         ."\" mypassword && echo \$mypassword'";
126         return rtrim(shell_exec($command));
127       }
128     }
129
130   }
131
132 ?>