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);
64 if(($k = strpos($cmd_arg, "=")) === false){
65 $this->params[$cmd_arg] = "";
68 $this->params[substr($cmd_arg, 0, $k)] = substr($cmd_arg, $k + 1);
72 $this->etat = $this->valid_etat($this->etat);
73 if($this->etat === false) return "etat invalide";
77 function init_cli_user(){
78 if(!$this->inited()) return "cli not inited";
79 $data = $this->data();
82 $this->password = $this->cmd_prompt("password: ", true);
85 if(!($user = $data->user($this->login)) || (md5($this->password) != $user["password"])){
86 return "indentification incorrecte";
88 $data->set_session_user($user);
90 if(!$this->action_allowed($this->etat, false)){
91 $this->login = $this->cmd_prompt("login: ");
92 $this->password = $this->cmd_prompt("password: ", true);
94 if(!($user = $data->user($this->login)) || (md5($this->password) != $user["password"])){
95 return "indentification incorrecte";
97 $data->set_session_user($user);
98 if(!$this->action_allowed($this->etat, false)){
99 return "permission refusee pour cette action";
105 function cmd_prompt($prompt, $SILENT = false){
106 if(preg_match('/^win/i', PHP_OS)){
107 $vbscript = sys_get_temp_dir().'prompt_password.vbs';
110 'wscript.echo(InputBox("'
112 .'", "", "'.$prompt.'"))'
114 $command = "cscript //nologo " . escapeshellarg($vbscript);
115 $password = rtrim(shell_exec($command));
120 $command = "/usr/bin/env bash -c 'echo OK'";
121 if(rtrim(shell_exec($command)) !== 'OK') return false;
123 "/usr/bin/env bash -c 'read".($SILENT ? " -s" : "")." -p \""
125 ."\" mypassword && echo \$mypassword'";
126 return rtrim(shell_exec($command));