module cli pour appels en ligne de commande mtweb.0.11.0
authordj3c1t <dj3c1t@free.fr>
Sat, 26 Oct 2013 13:15:43 +0000 (15:15 +0200)
committerdj3c1t <dj3c1t@free.fr>
Sat, 26 Oct 2013 13:15:43 +0000 (15:15 +0200)
cli.php [new file with mode: 0644]
index.php
mw/app/data/modules/share/mw_data_users_sessions.php
mw/app/out/default/views/tinymce.init.js.php
mw/env/modules/mw_env_cli.php [new file with mode: 0644]
mw/mw_app.php

diff --git a/cli.php b/cli.php
new file mode 100644 (file)
index 0000000..9f614e4
--- /dev/null
+++ b/cli.php
@@ -0,0 +1,44 @@
+<?php
+
+  if(!isset($argv) || !$argv){
+    echo "variable argv non disponible\n";
+    return;
+  }
+
+  $path_file = "pathes.php";
+  $FIRST = false;
+  foreach($argv as $cmd_arg){
+    if(!$FIRST){
+      $FIRST = true;
+      continue;
+    }
+    if(strlen($cmd_arg)){
+      if(($k = strpos($cmd_arg, "=")) && (substr($cmd_arg, 0, $k) == "path_file")){
+        $path_file = substr($cmd_arg, $k + 1);
+      }
+    }
+  }  
+  if(!$path_file || !file_exists($path_file)){
+    echo "fichier des chemins introuvable\n";
+    return;
+  }
+
+  require $path_file;
+  $PATHES["mw_dir"] .= $PATHES["mw_dir"] && substr($PATHES["mw_dir"], -1) != "/" ? "/" : "";
+  require $PATHES["mw_dir"]."mw_app.php";
+
+  $app = new mw_app($path_file);
+
+  if(($res = $app->init()) !== true){
+    echo $res."\n";
+    return;
+  }
+
+  $env = $app->env();
+
+  if(($res = $env->run_cli($argv)) !== true){
+    echo $res."\n";
+    return;
+  }
+
+  debug($env->get_out());
index f43715b..ac39687 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1,11 +1,12 @@
 <?php
 
   require "mw/mw_app.php";
-  $app = new mw_app("pathes.php");
+  $app = new mw_app("pathes.php", true);
 
-  if(!$app->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();
index a82c9a0..9a787a0 100644 (file)
@@ -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(){
         "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(){
index 737c468..1922f09 100644 (file)
       file_browser_callback : "tinyBrowser",
 
       invalid_elements : "script",
+
+relative_urls : false,
+remove_script_host : false,
+//convert_urls : true,
 \r
                // Style formats\r
                style_formats : [\r
diff --git a/mw/env/modules/mw_env_cli.php b/mw/env/modules/mw_env_cli.php
new file mode 100644 (file)
index 0000000..33dd0fb
--- /dev/null
@@ -0,0 +1,132 @@
+<?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
index 1c18b21..9af31bc 100644 (file)
@@ -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();
       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(){
         $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){
     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;
     }
   
       $this->error = $content;
     }
 
-    function show_error(){
-      echo $this->error;
+    function get_error(){
+      return isset($this->error) && $this->error ? $this->error : false;
     }
 
   }