+<?php
+
+ class mw_env_cli extends mw_env{
+
+ var $argv;
+ var $etat;
+ var $login;
+ var $password;
+ var $params;
+ var $INITED;
+
+ function run_cli($argv){
+ if(PHP_SAPI != "cli"){
+ $this->INITED = false;
+ return "php cli uniquement";
+ }
+ if(!isset($argv) || !$argv){
+ $this->INITED = false;
+ return "variable argv non disponible";
+ }
+ $this->argv = $argv;
+ $this->etat = "";
+ $this->login = "";
+ $this->password = "";
+ $this->params = array();
+ $this->INITED = true;
+ if(
+ (($res = $this->parse_cli_arguments()) !== true)
+ || (($res = $this->init_cli_user()) !== true)
+ ){
+ return $res;
+ }
+ $this->run($this->etat, array("get" => $this->params));
+ return true;
+ }
+
+ function inited(){
+ return isset($this->INITED) && $this->INITED;
+ }
+
+ function parse_cli_arguments(){
+ if(!$this->inited()) return "cli not inited";
+ $FIRST = false;
+ $SECOND = false;
+ foreach($this->argv as $cmd_arg){
+ if(!$FIRST){
+ $FIRST = true;
+ continue;
+ }
+ elseif(!$SECOND){
+ $SECOND = true;
+ if(($k = strpos($cmd_arg, "=")) === false){
+ $this->etat = $cmd_arg;
+ if(($k = strpos($this->etat, "@")) !== false){
+ $this->login = explode(":", substr($this->etat, 0, $k));
+ if(isset($this->login[1])) $this->password = $this->login[1];
+ $this->login = $this->login[0];
+ $this->etat = substr($this->etat, $k + 1);
+ }
+ continue;
+ }
+ }
+ if(strlen($cmd_arg)){
+ if(($k = strpos($cmd_arg, "=")) === false){
+ $this->params[$cmd_arg] = "";
+ }
+ elseif($k != 0){
+ $this->params[substr($cmd_arg, 0, $k)] = substr($cmd_arg, $k + 1);
+ }
+ }
+ }
+ $this->etat = $this->valid_etat($this->etat);
+ if($this->etat === false) return "etat invalide";
+ return true;
+ }
+
+ function init_cli_user(){
+ if(!$this->inited()) return "cli not inited";
+ $data = $this->data();
+ if($this->login){
+ if(!$this->password){
+ $this->password = $this->cmd_prompt("password: ", true);
+ echo "\n";
+ }
+ if(!($user = $data->user($this->login)) || (md5($this->password) != $user["password"])){
+ return "indentification incorrecte";
+ }
+ $data->set_session_user($user);
+ }
+ if(!$this->action_allowed($this->etat, false)){
+ $this->login = $this->cmd_prompt("login: ");
+ $this->password = $this->cmd_prompt("password: ", true);
+ echo "\n";
+ if(!($user = $data->user($this->login)) || (md5($this->password) != $user["password"])){
+ return "indentification incorrecte";
+ }
+ $data->set_session_user($user);
+ if(!$this->action_allowed($this->etat, false)){
+ return "permission refusee pour cette action";
+ }
+ }
+ return true;
+ }
+
+ function cmd_prompt($prompt, $SILENT = false){
+ if(preg_match('/^win/i', PHP_OS)){
+ $vbscript = sys_get_temp_dir().'prompt_password.vbs';
+ file_put_contents(
+ $vbscript,
+ 'wscript.echo(InputBox("'
+ .addslashes($prompt)
+ .'", "", "'.$prompt.'"))'
+ );
+ $command = "cscript //nologo " . escapeshellarg($vbscript);
+ $password = rtrim(shell_exec($command));
+ unlink($vbscript);
+ return $password;
+ }
+ else{
+ $command = "/usr/bin/env bash -c 'echo OK'";
+ if(rtrim(shell_exec($command)) !== 'OK') return false;
+ $command =
+ "/usr/bin/env bash -c 'read".($SILENT ? " -s" : "")." -p \""
+ .addslashes($prompt)
+ ."\" mypassword && echo \$mypassword'";
+ return rtrim(shell_exec($command));
+ }
+ }
+
+ }
+
+?>
\ No newline at end of file