nouveau module observers dans env
[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_role = true){
13       if(($this->etat = $this->valid_etat($etat)) !== false){
14         if(!$valid_role || $this->action_allowed($this->etat, false)){
15           return $this->etat;
16         }
17         else $this->erreur("Vous n'avez pas le role 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 action_allowed($etat, $CHECK_FORMAT = true){
79       $data = $this->data();
80       if($CHECK_FORMAT) $etat = $this->valid_etat($etat);
81       if($etat === false){
82         $this->erreur("etat invalide");
83         return false;
84       }
85       if(($user_roles = $data->get_user_roles()) === false){
86         $this->erreur("Impossible de lire les roles de l'utilisateur courant");
87         return false;
88       }
89       if(!$user_roles){
90         $this->erreur("L'utilisateur courant n'a aucun role");
91         return false;
92       }
93       if(
94         (
95           $action_roles = $data->get_action_roles(
96             $etat["mod"],
97             $etat["controller"],
98             $etat["action"]
99           )
100         ) === false
101       ){
102         $this->erreur("Impossible de lire les roles des actions en base");
103         return false;
104       }
105       foreach($user_roles as $id_role){
106         $OK = $this->config("default_allow");
107         $action = $etat["mod"];
108         if(isset($action_roles[$action])){
109           $OK =
110                (isset($action_roles[$action][0]) && $action_roles[$action][0])
111             || (isset($action_roles[$action][$id_role]) && $action_roles[$action][$id_role]);
112         }
113         if(!$OK){
114           $action = $etat["mod"]."/".$etat["controller"];
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         }
121         if(!$OK){
122           $action = $etat["mod"]."/".$etat["controller"]."/".$etat["action"];
123           if(isset($action_roles[$action])){
124             $OK =
125                  (isset($action_roles[$action][0]) && $action_roles[$action][0])
126               || (isset($action_roles[$action][$id_role]) && $action_roles[$action][$id_role]);
127           }
128         }
129         if($OK) break;
130       }
131       return $OK;
132     }
133
134     function run($etat, $valid_role = true, $params = array(), $method = "GET"){
135       if($this->set_etat($etat, $valid_role)){
136         $controller_file = "mods/".$this->etat("mod")."/".$this->etat("controller").".php";
137         if($this->app_file_exists($controller_file = "mods/".$this->etat("mod")."/".$this->etat("controller").".php", "DESC")){
138           if(!class_exists("mw_mod")) require $this->app_file("mods/mw_mod.php");
139           if(!class_exists($controller_class = "mw_".$this->etat("mod")."_".$this->etat("controller"))){
140             require $this->app_file($controller_file, "DESC");
141           }
142           if(class_exists($controller_class)){
143             $controller = new $controller_class();
144             $action_method = $this->etat("action");
145             if(method_exists($controller, $action_method)){
146               foreach($params as $key => $value){
147                 switch(strtolower($method)){
148                   case "get": $_GET[$this->param($key)] = $value; break;
149                   case "post": $_POST[$key] = $value; break;
150                   default: break;
151                 }
152               }
153               if(($controller_validate = $controller->validate($this)) === true){
154                 if(($controller_prepare_inputs = $controller->prepare_inputs($this)) === true){
155                   $etat_before = $this->etat;
156                   $this->call_observers("before_action");
157                   $controller->$action_method($this);
158                   $etat_after = $this->etat;
159                   $this->etat = $etat_before;
160                   $this->call_observers("after_action");
161                   $this->etat = $etat_after;
162                 }
163                 else $this->erreur($controller_prepare_inputs);
164               }
165               else $this->erreur($controller_validate);
166             }
167             else $this->erreur("Impossible de trouver l'action ".$this->etat("action"));
168           }
169           else $this->erreur("Impossible d'instancier le controleur ".$this->etat("controller"));
170         }
171         else $this->erreur("Impossible de trouver le controleur ".$this->etat("controller")." pour le module ".$this->etat("mod"));
172       }
173       else $this->erreur("Impossible d'effectuer cette action");
174     }
175
176     function is_running($etat){
177       if($etat = $this->valid_etat($etat)){
178         return
179             ($etat["mod"] == $this->etat("mod"))
180         &&  ($etat["controller"] == $this->etat("controller"))
181         &&  ($etat["action"] == $this->etat("action"));
182       }
183       return false;
184     }
185
186     function etat($name = null){
187       if(!isset($name)) return $this->etat;
188       return $this->etat[$name];
189     }
190
191     function check_stop(){
192       return $this->etat("mod") == "reponses";
193     }
194
195     function get_mod($mod_name){
196       if($etat = $this->valid_etat($mod_name)){
197         if($this->app_file_exists($controller_file = "mods/".$etat["mod"]."/".$etat["controller"].".php")){
198           if(!class_exists("mw_mod")) require $this->app_file("mods/mw_mod.php");
199           if(!class_exists($controller_class = "mw_".$etat["mod"]."_".$etat["controller"])){
200             require $this->app_file($controller_file);
201           }
202           if(class_exists($controller_class)){
203             return new $controller_class();
204           }
205         }
206       }
207       return false;
208     }
209
210   }
211
212 ?>