module cli pour appels en ligne de commande
[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 $error;
12     var $DO_SETUP;
13
14     function mw_app($path_file, $DO_SETUP = false){
15       $this->DO_SETUP = $DO_SETUP;
16       $this->path_file = $path_file;
17       $this->pathes = array();
18       $this->config_file = "";
19       $this->config = array();
20       $this->bdd = array();
21       $this->error = false;
22     }
23
24     function set_env(&$env){
25       $this->env =& $env;
26     }
27
28     function env(){
29       return $this->env;
30     }
31
32     function param($name){
33       if(!isset($this->env)) return false;
34       return $this->env->param($name);
35     }
36
37     function init(){
38       if(!$this->init_pathes()) return $this->get_error();
39       if(!$this->init_config()) return $this->get_error();
40       if(!$this->init_env()) return $this->get_error();
41       if($this->config_file) return true;
42       if($this->DO_SETUP) $this->setup();
43       return $this->get_error();
44     }
45
46     function init_pathes(){
47       if(($n = strpos($_SERVER["REQUEST_URI"], "?")) !== false){
48         $_SERVER["REQUEST_URI"] = substr($_SERVER["REQUEST_URI"], 0, $n);
49       }
50       $web_path = explode("/", preg_replace('#/+#','/',$_SERVER["REQUEST_URI"]));
51       $this->pathes["web"] = "";
52       for($i = 0; $i < count($web_path) - 1; $i++) $this->pathes["web"] .= $web_path[$i]."/";
53       if(
54             !$this->path_file
55         ||  !file_exists($this->path_file)
56       ){
57         $this->error("le fichier des chemins est introuvable");
58         return false;
59       }
60       require $this->path_file;
61       if(
62             !isset($PATHES)
63         ||  !is_array($PATHES)
64       ){
65         $this->error("variable PATHES non defini");
66         return false;
67       }
68       foreach($PATHES as $path_name => $path_value){
69         $this->pathes[$path_name] = $path_value;
70         $this->pathes[$path_name] .= $path_value && substr($path_value, -1) != "/" ? "/" : "";
71       }
72       if(
73             !isset($this->pathes["mw_dir"])
74         ||  !isset($this->pathes["mw_path"])
75         ||  !isset($this->pathes["content"])
76       ){
77         $this->error("variable PATHES incomplete");
78         return false;
79       }
80       if(
81             !file_exists($this->pathes["content"])
82         ||  !is_dir($this->pathes["content"])
83       ){
84         $this->error("dossier content introuvable");
85         return false;
86       }
87       if(!is_writable($this->pathes["content"])){
88         $this->error("Php ne peut pas ecrire dans le dossier content");
89         return false;
90       }
91       return true;
92     }
93
94     function init_config(){
95       if(file_exists($config_file = $this->pathes["content"]."config/config.php")){
96         $this->config_file = $config_file;
97         require_once $this->config_file;
98         if(
99               isset($CONFIG)
100           &&  is_array($CONFIG)
101         ){
102           foreach($CONFIG as $config_name => $config_value){
103             $this->config[$config_name] = $config_value;
104           }
105         }
106         if(
107               isset($bdd)
108           &&  is_array($bdd)
109         ){
110           foreach($bdd as $bdd_key => $bdd_value){
111             $this->bdd[$bdd_key] = $bdd_value;
112           }
113         }
114       }
115       else{
116         if(!$this->DO_SETUP){
117           $this->error("fichier config.php manquant");
118           return false;
119         }
120       }
121       return true;
122     }
123
124     function init_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         $this->error("des fichiers sont introuvables. impossible d'initialiser l'environnement");
131         return false;
132       }
133       if(!class_exists("sxml")) require_once $sxml_class_file;
134       if(!class_exists("empty_class")) require_once $empty_class_file;
135       if(!class_exists("mw_env")) require_once $env_class_file;
136       if(
137             !class_exists("sxml")
138         ||  !class_exists("empty_class")
139         ||  !class_exists("mw_env")
140       ){
141         $this->error("des classes sont introuvables. impossible d'initialiser l'environnement");
142         return false;
143       }
144       $env = new mw_env(true);
145       $this->set_env($env);
146       $env->load_modules($this->pathes["mw_dir"], "env/modules/");
147       $env->set_config_file($this->config_file);
148       $env->set_PATHES($this->pathes);
149       $env->init_plugins();
150       $env->load_config($this->bdd, $this->config);
151       $env->init();
152       return true;
153     }
154
155     function run($etat = "", $params = array(), $valid_role = true){
156       $env = $this->env();
157       if(!is_callable(array($env, "run"))) return false;
158       $env->run($etat);
159       return true;
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 setup(){
172       $env = $this->env();
173       $etat = isset($_GET[$env->param("e")]) ? $_GET[$env->param("e")] : "install";
174       $env->run($etat, array(), false);
175       $this->display();
176       exit;
177     }
178
179     function error($content){
180       $this->error = $content;
181     }
182
183     function get_error(){
184       return isset($this->error) && $this->error ? $this->error : false;
185     }
186
187   }