3 class mw_env_cli extends mw_env{
12 function run_cli($argv){
13 if(PHP_SAPI != "cli"){
14 $this->INITED = false;
15 return "php cli uniquement";
17 if(!isset($argv) || !$argv){
18 $this->INITED = false;
19 return "variable argv non disponible";
25 $this->params = array();
28 (($res = $this->parse_cli_arguments()) !== true)
29 || (($res = $this->init_cli_user()) !== true)
33 $this->run($this->etat, array("get" => $this->params));
38 return isset($this->INITED) && $this->INITED;
41 function parse_cli_arguments(){
42 if(!$this->inited()) return "cli not inited";
45 foreach($this->argv as $cmd_arg){
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);
66 if(($k = strpos($cmd_arg, "=")) === false){
67 foreach($this->get_PARAMS() as $_key => $_value) if(strcmp($cmd_arg, $_value) == 0){
73 $_cmd_arg = substr($cmd_arg, 0, $k);
74 foreach($this->get_PARAMS() as $_key => $_value) if(strcmp($_cmd_arg, $_value) == 0){
76 $param_value = substr($cmd_arg, $k + 1);
81 $this->params[$param_name] = $param_value;
85 $this->etat = $this->valid_etat($this->etat);
86 if($this->etat === false) return "etat invalide";
90 function init_cli_user(){
91 if(!$this->inited()) return "cli not inited";
92 $data = $this->data();
95 $this->password = $this->cmd_prompt("password: ", true);
98 if(!($user = $data->user($this->login)) || (md5($this->password) != $user["password"])){
99 return "indentification incorrecte";
101 $data->set_session_user($user);
103 if(!$this->action_allowed($this->etat, false)){
104 $this->login = $this->cmd_prompt("login: ");
105 $this->password = $this->cmd_prompt("password: ", true);
107 if(!($user = $data->user($this->login)) || (md5($this->password) != $user["password"])){
108 return "indentification incorrecte";
110 $data->set_session_user($user);
111 if(!$this->action_allowed($this->etat, false)){
112 return "permission refusee pour cette action";
118 function cmd_prompt($prompt, $SILENT = false){
119 if(preg_match('/^win/i', PHP_OS)){
120 $vbscript = sys_get_temp_dir().'prompt_password.vbs';
123 'wscript.echo(InputBox("'
125 .'", "", "'.$prompt.'"))'
127 $command = "cscript //nologo " . escapeshellarg($vbscript);
128 $password = rtrim(shell_exec($command));
133 $command = "/usr/bin/env bash -c 'echo OK'";
134 if(rtrim(shell_exec($command)) !== 'OK') return false;
136 "/usr/bin/env bash -c 'read".($SILENT ? " -s" : "")." -p \""
138 ."\" mypassword && echo \$mypassword'";
139 return rtrim(shell_exec($command));