public attr / function, constructeurs __construct
[mtweb] / mw / env / modules / mw_env_run.php
1 <?php
2
3   class mw_env_run extends mw_env{
4
5     public $etat;
6     public $controllers;
7
8     public function user(){
9       if(!($data = $this->data())) return array();
10       return $data->get_session_user();
11     }
12
13     public function set_etat($etat, $valid_role = true){
14       if(($this->etat = $this->valid_etat($etat)) !== false){
15         if(!$valid_role || $this->action_allowed($this->etat, false)){
16           return $this->etat;
17         }
18         else{
19           $etat = $this->etat;
20           $this->erreur("Vous n'avez pas le role requis pour effectuer cette action");
21           $this->call_observers("action_permission_denied", array("etat" => $etat));
22         }
23       }
24       else $this->erreur("etat invalide");
25       return false;
26     }
27
28     public function valid_etat($etat){
29       $_etat = array(
30         "mod" => "",
31         "controller" => "",
32         "action" => ""
33       );
34       if(is_array($etat)){
35         $_etat["mod"] = isset($etat["mod"]) ? $etat["mod"] : "";
36         $_etat["controller"] = isset($etat["controller"]) ? $etat["controller"] : "";
37         $_etat["action"] = isset($etat["action"]) ? $etat["action"] : "";
38       }
39       else{
40         $etat = explode("/", $etat);
41         foreach($etat as $etat_item){
42           if($etat_item){
43             if(!$_etat["mod"]) $_etat["mod"] = $etat_item;
44             else{
45               if(!$_etat["controller"]) $_etat["controller"] = $etat_item;
46               else{
47                 if(!$_etat["action"]) $_etat["action"] = $etat_item;
48                 break;
49               }
50             }
51           }
52         }
53       }
54       if(!$_etat["mod"]){
55         $_etat["mod"] = "index";
56         $_etat["controller"] = "index";
57         $_etat["action"] = "index";
58       }
59       else{
60         if(!$_etat["controller"]){
61           $_etat["controller"] = "index";
62           $_etat["action"] = "index";
63         }
64         else{
65           if(!$_etat["action"]) $_etat["action"] = "index";
66         }
67       }
68       if(
69            is_array($_etat)
70         && count($_etat) == 3
71         && isset($_etat["mod"]) && preg_match("/^[a-zA-Z0-9_]+$/", $_etat["mod"])
72         && isset($_etat["controller"]) && preg_match("/^[a-zA-Z0-9_]+$/", $_etat["controller"])
73         && isset($_etat["action"]) && preg_match("/^[a-zA-Z0-9_]+$/", $_etat["action"])
74       ){
75         return $_etat;
76       }
77       return false;
78     }
79
80     public function etat_is_valid(){
81       return $this->valid_etat($this->etat);
82     }
83
84     public function action_allowed($etat, $CHECK_FORMAT = true){
85       if(!$this->bdd("sgbd")) return false;
86       $data = $this->data();
87       if($CHECK_FORMAT) $etat = $this->valid_etat($etat);
88       if($etat === false){
89         $this->erreur("etat invalide");
90         return false;
91       }
92       if(($user_roles = $data->get_user_roles()) === false){
93         $this->erreur("Impossible de lire les roles de l'utilisateur courant");
94         return false;
95       }
96       if(!$user_roles){
97         $this->erreur("L'utilisateur courant n'a aucun role");
98         return false;
99       }
100       if(
101         (
102           $action_roles = $data->get_action_roles(
103             $etat["mod"],
104             $etat["controller"],
105             $etat["action"]
106           )
107         ) === false
108       ){
109         $this->erreur("Impossible de lire les roles des actions en base");
110         return false;
111       }
112       foreach($user_roles as $id_role){
113         $OK = $this->config("default_allow");
114         $action = $etat["mod"];
115         if(isset($action_roles[$action])){
116           $OK =
117                (isset($action_roles[$action][0]) && $action_roles[$action][0])
118             || (isset($action_roles[$action][$id_role]) && $action_roles[$action][$id_role]);
119         }
120         $action = $etat["mod"]."/".$etat["controller"];
121         if(isset($action_roles[$action])){
122           $OK =
123                (isset($action_roles[$action][0]) && $action_roles[$action][0])
124             || (isset($action_roles[$action][$id_role]) && $action_roles[$action][$id_role]);
125         }
126         $action = $etat["mod"]."/".$etat["controller"]."/".$etat["action"];
127         if(isset($action_roles[$action])){
128           $OK =
129                (isset($action_roles[$action][0]) && $action_roles[$action][0])
130             || (isset($action_roles[$action][$id_role]) && $action_roles[$action][$id_role]);
131         }
132         if($OK) break;
133       }
134       return $OK;
135     }
136
137     public function run($etat, $params = array(), $valid_role = true){
138       if($this->set_etat($etat, $valid_role)){
139         if($controller = $this->get_controller($this->etat("mod")."/".$this->etat("controller"))){
140           $action_method = $this->etat("action");
141           if(method_exists($controller, $action_method)){
142             foreach($params as $params_method => $values){
143               foreach($values as $key => $value){
144                 switch(strtolower($params_method)){
145                   case "get":
146                     $_GET[$this->param($key)] = $value;
147                     break;
148                   case "post":
149                     $_POST[$key] = $value;
150                     break;
151                 }
152               }
153             }
154             if(($controller_validate = $controller->validate()) === true){
155               if(($controller_prepare_inputs = $controller->prepare_inputs()) === true){
156                 $etat_before = $this->etat;
157                 $this->call_observers("before_action");
158                 $controller->$action_method($this);
159                 $etat_after = $this->etat;
160                 $this->etat = $etat_before;
161                 $this->call_observers("after_action");
162                 $this->etat = $etat_after;
163               }
164               else $this->erreur($controller_prepare_inputs);
165             }
166             else $this->erreur($controller_validate);
167           }
168           else $this->erreur("Impossible de trouver l'action ".$this->etat("action"));
169         }
170         else $this->erreur("Impossible d'instancier le controleur ".$this->etat("controller"));
171       }
172     }
173
174     public function is_running($etat){
175       $_etat = array();
176       if(is_array($etat)){
177         if(isset($etat["mod"])){
178           $_etat["mod"] = $etat["mod"];
179           if(isset($etat["controller"])){
180             $_etat["controller"] = $etat["controller"];
181             if(isset($etat["action"])) $_etat["action"] = $etat["action"];
182           }
183         }
184       }
185       else{
186         $etat = explode("/", $etat);
187         foreach($etat as $etat_item){
188           if($etat_item){
189             if(!isset($_etat["mod"])) $_etat["mod"] = $etat_item;
190             else{
191               if(!isset($_etat["controller"])) $_etat["controller"] = $etat_item;
192               else{
193                 if(!isset($_etat["action"])) $_etat["action"] = $etat_item;
194                 break;
195               }
196             }
197           }
198         }
199       }
200       $IS_RUNNING = true;
201       if($IS_RUNNING && isset($_etat["mod"])) $IS_RUNNING = ($_etat["mod"] == $this->etat("mod"));
202       if($IS_RUNNING && isset($_etat["controller"])) $IS_RUNNING = ($_etat["controller"] == $this->etat("controller"));
203       if($IS_RUNNING && isset($_etat["action"])) $IS_RUNNING = ($_etat["action"] == $this->etat("action"));
204       return $IS_RUNNING;
205     }
206
207     public function etat($name = null){
208       if(!isset($name)) return $this->etat;
209       return $this->etat[$name];
210     }
211
212     public function get_controller($controller_path){
213       if($etat = $this->valid_etat($controller_path)){
214         if(!isset($this->controllers)) $this->controllers = array();
215         if(!isset($this->controllers[$etat["mod"]])) $this->controllers[$etat["mod"]] = array();
216         if(!isset($this->controllers[$etat["mod"]][$etat["controller"]])){
217           $controller_class = "mw_".$etat["mod"]."_".$etat["controller"];
218           if(!class_exists($controller_class)){
219             $controller_file = "controllers/".$etat["mod"]."/".$etat["controller"].".php";
220             if($this->app_file_exists($controller_file, "DESC")){
221               require_once $this->app_file($controller_file, "DESC");
222             }
223             if(!class_exists($controller_class)) return false;
224           }
225           $this->controllers[$etat["mod"]][$etat["controller"]] = new $controller_class();
226           $this->controllers[$etat["mod"]][$etat["controller"]]->set_env($this);
227         }
228         return $this->controllers[$etat["mod"]][$etat["controller"]];
229       }
230       return false;
231     }
232
233   }
234
235   // -------------------------------------------------------------------------------------------
236   //                                                                         class mw_controller
237   //
238
239   abstract class mw_controller{
240
241     public $env;
242
243     public function set_env(&$env){
244       $this->env = &$env;
245     }
246
247     public function env(){
248       return $this->env;
249     }
250
251     public function validate(){
252       return true;
253     }
254
255     public function prepare_inputs(){
256       return true;
257     }
258
259   }