ajout d'un installeur en ligne
[mtweb] / mw / mw_app.php
1 <?php
2
3   class mw_app{
4
5     var $env;
6     var $path_file;
7     var $pathes;
8     var $config_file;
9     var $config;
10     var $bdd;
11     var $INITED;
12     var $logs;
13     var $DO_SETUP;
14
15     function mw_app($path_file){
16
17       // executer le setup si le fichier de config est absent
18       $this->DO_SETUP = true;
19
20       $this->path_file = $path_file;
21       $this->pathes = array();
22       $this->config_file = "";
23       $this->config = array();
24       $this->bdd = array();
25       $this->INITED = false;
26       $this->logs = array();
27     }
28
29     function set_env(&$env){
30       $this->env =& $env;
31     }
32
33     function env(){
34       return $this->env;
35     }
36
37     function init(){
38       $this->INITED =
39           $this->init_pathes()
40       &&  $this->init_config()
41       &&  $this->init_env();
42       if(!$this->INITED) return false;
43       if($this->config_file) return true;
44       $this->setup();
45       return false;
46     }
47
48     function init_pathes(){
49       if(($n = strpos($_SERVER["REQUEST_URI"], "?")) !== false){
50         $_SERVER["REQUEST_URI"] = substr($_SERVER["REQUEST_URI"], 0, $n);
51       }
52       $web_path = explode("/", preg_replace('#/+#','/',$_SERVER["REQUEST_URI"]));
53       $this->pathes["web"] = "";
54       for($i = 0; $i < count($web_path) - 1; $i++) $this->pathes["web"] .= $web_path[$i]."/";
55       if(
56             !$this->path_file
57         ||  !file_exists($this->path_file)
58       ){
59         $this->log("path_file introuvable: ".$this->path_file);
60         return false;
61       }
62       require_once $this->path_file;
63       if(
64             !isset($PATHES)
65         ||  !is_array($PATHES)
66       ){
67         $this->log("path_file: ".$this->path_file);
68         $this->log("variable PATHES non defini");
69         return false;
70       }
71       foreach($PATHES as $path_name => $path_value){
72         $this->pathes[$path_name] = $path_value;
73         $this->pathes[$path_name] .= $path_value && substr($path_value, -1) != "/" ? "/" : "";
74       }
75       if(
76             !isset($this->pathes["mw_dir"])
77         ||  !isset($this->pathes["mw_path"])
78         ||  !isset($this->pathes["content"])
79       ){
80         $this->log("variable PATHES incomplete");
81         return false;
82       }
83       if(
84             !file_exists($this->pathes["content"])
85         ||  !is_dir($this->pathes["content"])
86       ){
87         $this->log("dossier content introuvable: ".$this->pathes["content"]);
88         return false;
89       }
90       if(!is_writable($this->pathes["content"])){
91         $this->log("Php ne peut pas ecrire dans le dossier content: ".$this->pathes["content"]);
92         return false;
93       }
94       return true;
95     }
96
97     function init_config(){
98       if(file_exists($config_file = $this->pathes["content"]."config/config.php")){
99         $this->config_file = $config_file;
100         require_once $this->config_file;
101         if(
102               isset($CONFIG)
103           &&  is_array($CONFIG)
104         ){
105           foreach($CONFIG as $config_name => $config_value){
106             $this->config[$config_name] = $config_value;
107           }
108         }
109         if(
110               isset($bdd)
111           &&  is_array($bdd)
112         ){
113           foreach($bdd as $bdd_key => $bdd_value){
114             $this->bdd[$bdd_key] = $bdd_value;
115           }
116         }
117       }
118       else $this->log("pas de fichier de config: ".$config_file);
119       return true;
120     }
121
122     function init_env(){
123       $env = false;
124       $this->set_env($env);
125       if(
126             ($sxml_class_file = (file_exists($this->pathes["mw_dir"]."libs/sxml.php") ? $this->pathes["mw_dir"]."libs/sxml.php" : ""))
127         &&  ($empty_class_file = (file_exists($this->pathes["mw_dir"]."libs/empty_class.php") ? $this->pathes["mw_dir"]."libs/empty_class.php" : ""))
128         &&  ($env_class_file = (file_exists($this->pathes["mw_dir"]."env/mw_env.php") ? $this->pathes["mw_dir"]."env/mw_env.php" : ""))
129       ){
130         if(!class_exists("sxml")) require_once $sxml_class_file;
131         if(!class_exists("empty_class")) require_once $empty_class_file;
132         if(!class_exists("mw_env")) require_once $env_class_file;
133         if(
134               class_exists("sxml")
135           &&  class_exists("empty_class")
136           &&  class_exists("mw_env")
137         ){
138           $env = new mw_env(true);
139           $this->set_env($env);
140           $env->load_modules($this->pathes["mw_dir"], "env/modules/");
141           $env->set_config_file($this->config_file);
142           $env->set_PATHES($this->pathes);
143           $env->init_plugins();
144           $env->load_config($this->bdd, $this->config);
145           $env->init();
146         }
147       }
148       return $this->env();
149     }
150
151     function run($etat = "", $params = array(), $valid_role = true){
152       if($this->INITED){
153         $env = $this->env();
154         $etat = ($etat === false ? false : ($etat ? $etat : (isset($_GET[$env->param("e")]) ? $_GET[$env->param("e")] : "")));
155         if($etat !== false) $env->run($etat, $params, $valid_role);
156         return true;
157       }
158       $this->log("run sans init. l'application ne s'est pas executee");
159       return false;
160     }
161   
162     function display(){
163       $env = $this->env();
164       if($env->etat_is_valid()){
165         $template = $env->get_template();
166         $layout = $env->init_layout();
167         $template->render_layout($layout);
168       }
169     }
170
171     function log($content){
172       $this->logs[] = array(
173         "time" => date("Y-m-d H:i:s"),
174         "value" => $content
175       );
176     }
177
178     function setup(){
179       if($this->DO_SETUP){
180         $env = $this->env();
181         $etat = isset($_GET[$env->param("e")]) ? $_GET[$env->param("e")] : "install";
182         $env->run($etat, array(), false);
183         $this->display();
184         exit;
185       }
186     }
187
188     function show_logs(){
189       foreach($this->logs as $log_time => $log_value){
190         $log_value = is_bool($log_value) ? "bool(".($log_value ? "true" : "false").")" : $log_value;
191         echo PHP_SAPI == "cli" ?
192           "\n".print_r($log_value, true)."\n"
193         : "<pre>".htmlentities(print_r($log_value, true), ENT_QUOTES, "UTF-8")."</pre>";
194       }
195     }
196
197   }
198
199 ?>