correction bug table_exists
[mtweb] / mw / env / modules / mw_env_run.php
1 <?php
2
3   class mw_env_run extends mw_env{
4
5     var $etat;
6
7     function user(){
8       $data = $this->data();
9       return $data->get_session_user();
10     }
11
12     function set_etat($etat, $valid_status = true){
13       if(($this->etat = $this->valid_etat($etat)) !== false){
14         if(!$valid_status || $this->status_ok($this->etat, false)){
15           return $this->etat;
16         }
17         else $this->erreur("Vous n'avez pas le statut requis pour effectuer cette action");
18       }
19       else $this->erreur("etat invalide");
20       return false;
21     }
22
23     function valid_etat($etat){
24       $_etat = array();
25       $_etat["mod"] = "";
26       $_etat["controller"] = "";
27       $_etat["action"] = "";
28       if(is_array($etat)){
29         $_etat["mod"] = isset($etat["mod"]) ? $etat["mod"] : "";
30         $_etat["controller"] = isset($etat["controller"]) ? $etat["controller"] : "";
31         $_etat["action"] = isset($etat["action"]) ? $etat["action"] : "";
32       }
33       else{
34         $etat = explode("/", $etat);
35         foreach($etat as $etat_item){
36           if($etat_item){
37             if(!$_etat["mod"]) $_etat["mod"] = $etat_item;
38             else{
39               if(!$_etat["controller"]) $_etat["controller"] = $etat_item;
40               else{
41                 if(!$_etat["action"]) $_etat["action"] = $etat_item;
42                 break;
43               }
44             }
45           }
46         }
47       }
48       if(!$_etat["mod"]){
49         $_etat["mod"] = "index";
50         $_etat["controller"] = "index";
51         $_etat["action"] = "index";
52       }
53       else{
54         if(!$_etat["controller"]){
55           $_etat["controller"] = "index";
56           $_etat["action"] = "index";
57         }
58         else{
59           if(!$_etat["action"]) $_etat["action"] = "index";
60         }
61       }
62       if(
63            is_array($_etat)
64         && count($_etat) == 3
65         && isset($_etat["mod"]) && preg_match("/^[a-zA-Z0-9_]+$/", $_etat["mod"])
66         && isset($_etat["controller"]) && preg_match("/^[a-zA-Z0-9_]+$/", $_etat["controller"])
67         && isset($_etat["action"]) && preg_match("/^[a-zA-Z0-9_]+$/", $_etat["action"])
68       ){
69         return $_etat;
70       }
71       return false;
72     }
73
74     function etat_is_valid(){
75       return $this->valid_etat($this->etat);
76     }
77
78     function status_ok($etat, $CHECK_FORMAT = true){
79       $OK = $this->config("default_allow");
80       $data = $this->data();
81       if($CHECK_FORMAT) $etat = $this->valid_etat($etat);
82       if($etat !== false){
83         if(($user_status = $data->get_user_status()) !== false){
84           if(
85             (
86               $action_status = $data->get_action_status(
87                 $etat["mod"],
88                 $etat["controller"],
89                 $etat["action"]
90               )
91             ) !== false
92           ){
93             $action = $etat["mod"]."/".$etat["controller"]."/".$etat["action"];
94             if(isset($action_status[$action])){
95               $OK =
96                    (isset($action_status[$action][0]) && $action_status[$action][0])
97                 || (isset($action_status[$action][$user_status]) && $action_status[$action][$user_status]);
98             }
99             else{
100               $action = $etat["mod"]."/".$etat["controller"];
101               if(isset($action_status[$action])){
102                 $OK =
103                      (isset($action_status[$action][0]) && $action_status[$action][0])
104                   || (isset($action_status[$action][$user_status]) && $action_status[$action][$user_status]);
105               }
106               else{
107                 $action = $etat["mod"];
108                 if(isset($action_status[$action])){
109                   $OK =
110                        (isset($action_status[$action][0]) && $action_status[$action][0])
111                     || (isset($action_status[$action][$user_status]) && $action_status[$action][$user_status]);
112                 }
113               }
114             }
115           }
116           else $this->erreur("Impossible de lire les status des actions en base");
117         }
118         else $this->erreur("Impossible de lire le statut de l'utilisateur courant");
119       }
120       else $this->erreur("etat invalide");
121       return $OK;
122     }
123
124     function run($etat, $valid_status = true, $params = array(), $method = "GET"){
125       if($this->set_etat($etat, $valid_status)){
126         $controller_file = "mods/".$this->etat("mod")."/".$this->etat("controller").".php";
127         if($this->app_file_exists($controller_file = "mods/".$this->etat("mod")."/".$this->etat("controller").".php", "DESC")){
128           if(!class_exists("mw_mod")) require $this->app_file("mods/mw_mod.php");
129           if(!class_exists($controller_class = "mw_".$this->etat("mod")."_".$this->etat("controller"))){
130             require $this->app_file($controller_file, "DESC");
131           }
132           if(class_exists($controller_class)){
133             $controller = new $controller_class();
134             $action_method = $this->etat("action");
135             if(method_exists($controller, $action_method)){
136               foreach($params as $key => $value){
137                 switch(strtolower($method)){
138                   case "get": $_GET[$this->param($key)] = $value; break;
139                   case "post": $_POST[$key] = $value; break;
140                   default: break;
141                 }
142               }
143               if(($controller_validate = $controller->validate($this)) === true){
144                 if(($controller_prepare_inputs = $controller->prepare_inputs($this)) === true){
145                   $controller->$action_method($this);
146                 }
147                 else $this->erreur($controller_prepare_inputs);
148               }
149               else $this->erreur($controller_validate);
150             }
151             else $this->erreur("Impossible de trouver l'action ".$this->etat("action"));
152           }
153           else $this->erreur("Impossible d'instancier le controleur ".$this->etat("controller"));
154         }
155         else $this->erreur("Impossible de trouver le controleur ".$this->etat("controller")." pour le module ".$this->etat("mod"));
156       }
157       else $this->erreur("Impossible d'effectuer cette action");
158     }
159
160     function etat($name){
161       return $this->etat[$name];
162     }
163
164     function check_stop(){
165       return $this->etat("mod") == "reponses";
166     }
167
168     function get_mod($mod_name){
169       if($etat = $this->valid_etat($mod_name)){
170         if($this->app_file_exists($controller_file = "mods/".$etat["mod"]."/".$etat["controller"].".php")){
171           if(!class_exists("mw_mod")) require $this->app_file("mods/mw_mod.php");
172           if(!class_exists($controller_class = "mw_".$etat["mod"]."_".$etat["controller"])){
173             require $this->app_file($controller_file);
174           }
175           if(class_exists($controller_class)){
176             return new $controller_class();
177           }
178         }
179       }
180       return false;
181     }
182
183   }
184
185 ?>