From b9147768f6fbd8ad385ce1046a8a50acf27fbb99 Mon Sep 17 00:00:00 2001 From: dj3c1t Date: Sat, 26 Oct 2013 15:15:43 +0200 Subject: [PATCH] module cli pour appels en ligne de commande --- cli.php | 44 +++++++ index.php | 9 +- .../data/modules/share/mw_data_users_sessions.php | 14 ++- mw/app/out/default/views/tinymce.init.js.php | 4 + mw/env/modules/mw_env_cli.php | 132 +++++++++++++++++++++ mw/mw_app.php | 31 +++-- 6 files changed, 209 insertions(+), 25 deletions(-) create mode 100644 cli.php create mode 100644 mw/env/modules/mw_env_cli.php diff --git a/cli.php b/cli.php new file mode 100644 index 0000000..9f614e4 --- /dev/null +++ b/cli.php @@ -0,0 +1,44 @@ +init()) !== true){ + echo $res."\n"; + return; + } + + $env = $app->env(); + + if(($res = $env->run_cli($argv)) !== true){ + echo $res."\n"; + return; + } + + debug($env->get_out()); diff --git a/index.php b/index.php index f43715b..ac39687 100644 --- a/index.php +++ b/index.php @@ -1,11 +1,12 @@ init()){ - $app->show_error(); + if(($res = $app->init()) !== true){ + echo $res; return; } - if($app->run()) $app->display(); + $app->run(isset($_GET[$app->param("e")]) ? $_GET[$app->param("e")] : ""); + $app->display(); diff --git a/mw/app/data/modules/share/mw_data_users_sessions.php b/mw/app/data/modules/share/mw_data_users_sessions.php index a82c9a0..9a787a0 100644 --- a/mw/app/data/modules/share/mw_data_users_sessions.php +++ b/mw/app/data/modules/share/mw_data_users_sessions.php @@ -78,8 +78,8 @@ $_SESSION[$this->app_session_key()]["pass"] = md5($user["password"].$_SESSION[$this->app_session_key()]["id"]); $env = $this->env(); return - setcookie($this->app_session_key()."_user", $user["login"], time() + (60 * 60 * 24 * 7), "/") - && setcookie($this->app_session_key()."_pass", $user["password"], time() + (60 * 60 * 24 * 7), "/"); + @setcookie($this->app_session_key()."_user", $user["login"], time() + (60 * 60 * 24 * 7), "/") + && @setcookie($this->app_session_key()."_pass", $user["password"], time() + (60 * 60 * 24 * 7), "/"); } function clear_session(){ @@ -88,10 +88,14 @@ "ip" => $_SERVER["REMOTE_ADDR"], "id" => md5(rand()) ); - $env = $this->env(); return - setcookie($this->app_session_key()."_user", "", 0, "/") - && setcookie($this->app_session_key()."_pass", "", 0, "/"); + @setcookie($this->app_session_key()."_user", "", 0, "/") + && @setcookie($this->app_session_key()."_pass", "", 0, "/"); + } + + function set_session_user($user){ + $this->set_session($user); + $this->user =& $user; } function get_session_user(){ diff --git a/mw/app/out/default/views/tinymce.init.js.php b/mw/app/out/default/views/tinymce.init.js.php index 737c468..1922f09 100644 --- a/mw/app/out/default/views/tinymce.init.js.php +++ b/mw/app/out/default/views/tinymce.init.js.php @@ -29,6 +29,10 @@ file_browser_callback : "tinyBrowser", invalid_elements : "script", + +relative_urls : false, +remove_script_host : false, +//convert_urls : true, // Style formats style_formats : [ diff --git a/mw/env/modules/mw_env_cli.php b/mw/env/modules/mw_env_cli.php new file mode 100644 index 0000000..33dd0fb --- /dev/null +++ b/mw/env/modules/mw_env_cli.php @@ -0,0 +1,132 @@ +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 diff --git a/mw/mw_app.php b/mw/mw_app.php index 1c18b21..9af31bc 100644 --- a/mw/mw_app.php +++ b/mw/mw_app.php @@ -11,7 +11,7 @@ var $error; var $DO_SETUP; - function mw_app($path_file, $DO_SETUP = true){ + function mw_app($path_file, $DO_SETUP = false){ $this->DO_SETUP = $DO_SETUP; $this->path_file = $path_file; $this->pathes = array(); @@ -29,13 +29,18 @@ return $this->env; } + function param($name){ + if(!isset($this->env)) return false; + return $this->env->param($name); + } + function init(){ - if(!$this->init_pathes()) return false; - if(!$this->init_config()) return false; - if(!$this->init_env()) return false; + if(!$this->init_pathes()) return $this->get_error(); + if(!$this->init_config()) return $this->get_error(); + if(!$this->init_env()) return $this->get_error(); if($this->config_file) return true; if($this->DO_SETUP) $this->setup(); - return false; + return $this->get_error(); } function init_pathes(){ @@ -52,12 +57,12 @@ $this->error("le fichier des chemins est introuvable"); return false; } - require_once $this->path_file; + require $this->path_file; if( !isset($PATHES) || !is_array($PATHES) ){ - $this->erreur("variable PATHES non defini"); + $this->error("variable PATHES non defini"); return false; } foreach($PATHES as $path_name => $path_value){ @@ -150,13 +155,7 @@ function run($etat = "", $params = array(), $valid_role = true){ $env = $this->env(); if(!is_callable(array($env, "run"))) return false; - $env->run( - $etat ? - $etat - : (isset($_GET[$env->param("e")]) ? $_GET[$env->param("e")] : ""), - $params, - $valid_role - ); + $env->run($etat); return true; } @@ -181,8 +180,8 @@ $this->error = $content; } - function show_error(){ - echo $this->error; + function get_error(){ + return isset($this->error) && $this->error ? $this->error : false; } } -- 2.1.4