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