ajout d'un installeur en ligne
[mtweb] / mw / mw_app.php
index 044d711..b198197 100644 (file)
@@ -3,6 +3,28 @@
   class mw_app{
 
     var $env;
+    var $path_file;
+    var $pathes;
+    var $config_file;
+    var $config;
+    var $bdd;
+    var $INITED;
+    var $logs;
+    var $DO_SETUP;
+
+    function mw_app($path_file){
+
+      // executer le setup si le fichier de config est absent
+      $this->DO_SETUP = true;
+
+      $this->path_file = $path_file;
+      $this->pathes = array();
+      $this->config_file = "";
+      $this->config = array();
+      $this->bdd = array();
+      $this->INITED = false;
+      $this->logs = array();
+    }
 
     function set_env(&$env){
       $this->env =& $env;
       return $this->env;
     }
 
-    function init($config_file){
-      require $config_file;
-      $PATHES["mw_dir"] .= $PATHES["mw_dir"] && substr($PATHES["mw_dir"], -1) != "/" ? "/" : "";
-      $PATHES["mw_path"] .= $PATHES["mw_path"] && substr($PATHES["mw_path"], -1) != "/" ? "/" : "";
-      if(!isset($CONFIG)) $CONFIG = array();
+    function init(){
+      $this->INITED =
+          $this->init_pathes()
+      &&  $this->init_config()
+      &&  $this->init_env();
+      if(!$this->INITED) return false;
+      if($this->config_file) return true;
+      $this->setup();
+      return false;
+    }
+
+    function init_pathes(){
+      if(($n = strpos($_SERVER["REQUEST_URI"], "?")) !== false){
+        $_SERVER["REQUEST_URI"] = substr($_SERVER["REQUEST_URI"], 0, $n);
+      }
+      $web_path = explode("/", preg_replace('#/+#','/',$_SERVER["REQUEST_URI"]));
+      $this->pathes["web"] = "";
+      for($i = 0; $i < count($web_path) - 1; $i++) $this->pathes["web"] .= $web_path[$i]."/";
+      if(
+            !$this->path_file
+        ||  !file_exists($this->path_file)
+      ){
+        $this->log("path_file introuvable: ".$this->path_file);
+        return false;
+      }
+      require_once $this->path_file;
+      if(
+            !isset($PATHES)
+        ||  !is_array($PATHES)
+      ){
+        $this->log("path_file: ".$this->path_file);
+        $this->log("variable PATHES non defini");
+        return false;
+      }
+      foreach($PATHES as $path_name => $path_value){
+        $this->pathes[$path_name] = $path_value;
+        $this->pathes[$path_name] .= $path_value && substr($path_value, -1) != "/" ? "/" : "";
+      }
+      if(
+            !isset($this->pathes["mw_dir"])
+        ||  !isset($this->pathes["mw_path"])
+        ||  !isset($this->pathes["content"])
+      ){
+        $this->log("variable PATHES incomplete");
+        return false;
+      }
+      if(
+            !file_exists($this->pathes["content"])
+        ||  !is_dir($this->pathes["content"])
+      ){
+        $this->log("dossier content introuvable: ".$this->pathes["content"]);
+        return false;
+      }
+      if(!is_writable($this->pathes["content"])){
+        $this->log("Php ne peut pas ecrire dans le dossier content: ".$this->pathes["content"]);
+        return false;
+      }
+      return true;
+    }
+
+    function init_config(){
+      if(file_exists($config_file = $this->pathes["content"]."config/config.php")){
+        $this->config_file = $config_file;
+        require_once $this->config_file;
+        if(
+              isset($CONFIG)
+          &&  is_array($CONFIG)
+        ){
+          foreach($CONFIG as $config_name => $config_value){
+            $this->config[$config_name] = $config_value;
+          }
+        }
+        if(
+              isset($bdd)
+          &&  is_array($bdd)
+        ){
+          foreach($bdd as $bdd_key => $bdd_value){
+            $this->bdd[$bdd_key] = $bdd_value;
+          }
+        }
+      }
+      else $this->log("pas de fichier de config: ".$config_file);
+      return true;
+    }
+
+    function init_env(){
       $env = false;
       $this->set_env($env);
       if(
-            ($sxml_class_file = (file_exists($PATHES["mw_dir"]."libs/sxml.php") ? $PATHES["mw_dir"]."libs/sxml.php" : ""))
-        &&  ($empty_class_file = (file_exists($PATHES["mw_dir"]."libs/empty_class.php") ? $PATHES["mw_dir"]."libs/empty_class.php" : ""))
-        &&  ($env_class_file = (file_exists($PATHES["mw_dir"]."env/mw_env.php") ? $PATHES["mw_dir"]."env/mw_env.php" : ""))
+            ($sxml_class_file = (file_exists($this->pathes["mw_dir"]."libs/sxml.php") ? $this->pathes["mw_dir"]."libs/sxml.php" : ""))
+        &&  ($empty_class_file = (file_exists($this->pathes["mw_dir"]."libs/empty_class.php") ? $this->pathes["mw_dir"]."libs/empty_class.php" : ""))
+        &&  ($env_class_file = (file_exists($this->pathes["mw_dir"]."env/mw_env.php") ? $this->pathes["mw_dir"]."env/mw_env.php" : ""))
       ){
         if(!class_exists("sxml")) require_once $sxml_class_file;
         if(!class_exists("empty_class")) require_once $empty_class_file;
         ){
           $env = new mw_env(true);
           $this->set_env($env);
-          $env->load_modules($PATHES["mw_dir"], "env/modules/");
-          $env->set_config_file($config_file);
-          $env->set_PATHES($PATHES);
+          $env->load_modules($this->pathes["mw_dir"], "env/modules/");
+          $env->set_config_file($this->config_file);
+          $env->set_PATHES($this->pathes);
           $env->init_plugins();
-          $env->load_config($bdd, $CONFIG);
+          $env->load_config($this->bdd, $this->config);
           $env->init();
         }
       }
     }
 
     function run($etat = "", $params = array(), $valid_role = true){
-      $env = $this->env();
-      $etat = ($etat === false ? false : ($etat ? $etat : (isset($_GET[$env->param("e")]) ? $_GET[$env->param("e")] : "")));
-      if($etat !== false) $env->run($etat, $params, $valid_role);
+      if($this->INITED){
+        $env = $this->env();
+        $etat = ($etat === false ? false : ($etat ? $etat : (isset($_GET[$env->param("e")]) ? $_GET[$env->param("e")] : "")));
+        if($etat !== false) $env->run($etat, $params, $valid_role);
+        return true;
+      }
+      $this->log("run sans init. l'application ne s'est pas executee");
+      return false;
     }
   
     function display(){
       }
     }
 
+    function log($content){
+      $this->logs[] = array(
+        "time" => date("Y-m-d H:i:s"),
+        "value" => $content
+      );
+    }
+
+    function setup(){
+      if($this->DO_SETUP){
+        $env = $this->env();
+        $etat = isset($_GET[$env->param("e")]) ? $_GET[$env->param("e")] : "install";
+        $env->run($etat, array(), false);
+        $this->display();
+        exit;
+      }
+    }
+
+    function show_logs(){
+      foreach($this->logs as $log_time => $log_value){
+        $log_value = is_bool($log_value) ? "bool(".($log_value ? "true" : "false").")" : $log_value;
+        echo PHP_SAPI == "cli" ?
+          "\n".print_r($log_value, true)."\n"
+        : "<pre>".htmlentities(print_r($log_value, true), ENT_QUOTES, "UTF-8")."</pre>";
+      }
+    }
+
   }
 
 ?>
\ No newline at end of file