roles multiples possible par user, administrables
[mtweb] / mw / app / data / modules / xml / mw_data_users.php
1 <?php
2
3   class mw_data_users extends mw_data{
4
5     var $users;
6     var $_user;
7     var $user_status;
8     var $action_status;
9
10     # ----------------------------------------------------------------------------------------
11     #                                                                                    users
12     #
13
14     function users($start = 0, $alpha = null, $status = null){
15       $sgbd = $this->sgbd();
16       $env = $this->env();
17       $users = array("list" => array(), "total" => 0);
18       $res = array();
19       if($rst = $sgbd->open_data("users")){
20         while($v_rst = $sgbd->fetch_data($rst)){
21           if(isset($v_rst)){
22             if(!isset($alpha) || (isset($v_rst["login"]) && strtolower(substr($v_rst["login"], 0, 1)) == strtolower($alpha))){
23               if(!isset($status) || (isset($v_rst["status"]) && $v_rst["status"] == $status)){
24                 $res[$v_rst["id"]] = $v_rst;
25                 $users["total"]++;
26               }
27             }
28           }
29           else{
30             $res = false;
31             break;
32           }
33         }
34         $sgbd->close_data($rst);
35         if($res !== false){
36           $n = 0;
37           foreach($res as $id_user => $user){
38             $n++;
39             if(!$env->config("max_list") || ($n > $start && $n <= ($start + $env->config("max_list")))){
40               $users["list"][$user["id"]] = $user;
41               if(!isset($this->users)) $this->users = array();
42               $this->users[$user["id"]] = $user;
43             }
44           }
45           foreach($users["list"] as $id_user => $user){
46             if(($status = $this->list_user_status($id_user)) !== false){
47               $users["list"][$id_user]["status"] = $status;
48             }
49             else{
50               $users = false;
51               break;
52             }
53           }
54         }
55         else $users = false;
56       }
57       else $users = false;
58       return $users;
59     }
60
61     function list_user_status($id_user){
62       $sgbd = $this->sgbd();
63       $status = array();
64       if($rst = $sgbd->open_data("users_roles")){
65         while($v_rst = $sgbd->fetch_data($rst)){
66           if(isset($v_rst)){
67             if(isset($v_rst["id_role"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $id_user){
68               $status[] = $v_rst["id_role"];
69             }
70           }
71           else{
72             $status = false;
73             break;
74           }
75         }
76         $sgbd->close_data($rst);
77       }
78       else $status = false;
79       return $status;
80     }
81
82     function user_by_id($id){
83       if(!isset($this->users)) $this->users = array();
84       if(isset($this->users[$id])) return $this->users[$id];
85       $sgbd = $this->sgbd();
86       if(($user = $sgbd->get_data("users", $id)) !== false){
87         $this->users[$id] = $user;
88         if(($status = $this->list_user_status($user["id"])) !== false) $user["status"] = $status;
89         else $user = false;
90       }
91       return $user;
92     }
93
94     function user($login){
95       $sgbd = $this->sgbd();
96       $user = array();
97       if($rst = $sgbd->open_data("users")){
98         while($v_rst = $sgbd->fetch_data($rst)){
99           if(isset($v_rst)){
100             if(isset($v_rst["login"]) && $v_rst["login"] == $login){
101               $user = $v_rst;
102               break;
103             }
104           }
105           else $user = false;
106         }
107         $sgbd->close_data($rst);
108         if($user){
109           if(($status = $this->list_user_status($user["id"])) !== false) $user["status"] = $status;
110           else $user = false;
111         }
112       }
113       else $user = false;
114       if($user !== false){
115         if(!isset($this->users)) $this->users = array();
116         if($user) $this->users[$user["id"]] = $user;
117       }
118       return $user;
119     }
120
121     function user_exists($login){
122       $sgbd = $this->sgbd();
123       $EXISTS = 0;
124       if($rst = $sgbd->open_data("users")){
125         while($v_rst = $sgbd->fetch_data($rst)){
126           if(isset($v_rst)){
127             if(isset($v_rst["login"]) && $v_rst["login"] == $login){
128               $EXISTS++;
129             }
130           }
131           else{
132             $EXISTS = false;
133             break;
134           }
135         }
136         $sgbd->close_data($rst);
137       }
138       else $EXISTS = false;
139       return $EXISTS;
140     }
141
142     function add_user($login, $password, $email, $status){
143       $sgbd = $this->sgbd();
144       if(
145         (
146           $id_user = $sgbd->add_data(
147             "users",
148             array(
149               "login" => $login,
150               "password" => $password,
151               "email" => $email
152             )
153           )
154         ) === false
155       ) return false;
156       $OK = true;
157       foreach($status as $id_role){
158         $OK = $sgbd->add_data(
159           "users_roles",
160           array(
161             "id_user" => $id_user,
162             "id_role" => $id_role
163           )
164         );
165         if(!$OK) break;
166       }
167       if(!$OK) return false;
168       return $id_user;
169     }
170
171     function set_user($id, $login, $password, $email, $status){
172       $sgbd = $this->sgbd();
173       if(
174         !$sgbd->set_data(
175           "users",
176           $id,
177           array(
178             "login" => $login,
179             "password" => $password,
180             "email" => $email
181           )
182         )
183       ) return false;
184       if($rst = $sgbd->open_data("users_roles")){
185         $OK = true;
186         while($v_rst = $sgbd->fetch_data($rst)){
187           if(isset($v_rst)){
188             if(isset($v_rst["id"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $id){
189               if(!$sgbd->del_data("users_roles", $v_rst["id"])){
190                 $OK = false;
191                 break;
192               }
193             }
194           }
195           else $OK = false;
196         }
197         $sgbd->close_data($rst);
198         if(!$OK) return false;
199       }
200       else return false;
201       foreach($status as $id_role){
202         $OK = $sgbd->add_data(
203           "users_roles",
204           array(
205             "id_user" => $id,
206             "id_role" => $id_role
207           )
208         );
209         if(!$OK) break;
210       }
211       if(!$OK) return false;
212       return true;
213     }
214
215     function del_user($login){
216       if(($user = $this->user($login)) !== false){
217         $sgbd = $this->sgbd();
218         if(!$sgbd->del_data("users", $user["id"])) return false;
219         if($rst = $sgbd->open_data("users_roles")){
220           $OK = true;
221           while($v_rst = $sgbd->fetch_data($rst)){
222             if(isset($v_rst)){
223               if(isset($v_rst["id"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $user["id"]){
224                 if(!$sgbd->del_data("users_roles", $v_rst["id"])){
225                   $OK = false;
226                   break;
227                 }
228               }
229             }
230             else $OK = false;
231           }
232           $sgbd->close_data($rst);
233           return $OK;
234         }
235       }
236       return false;
237     }
238
239     # ----------------------------------------------------------------------------------------
240     #                                                                                   status
241     #
242
243     function init_user_status($status = array()){
244       $sgbd = $this->sgbd();
245       $this->user_status = array();
246       if($rst = $sgbd->open_data("roles")){
247         while($v_rst = $sgbd->fetch_data($rst)){
248           if(isset($v_rst)){
249             $this->user_status[$v_rst["id"]] = $v_rst;
250           }
251           else{
252             $this->user_status = false;
253             break;
254           }
255         }
256         $sgbd->close_data($rst);
257       }
258       else $this->user_status = false;
259       return $this->user_status;
260     }
261
262     function add_role($nom, $intitule){
263       $sgbd = $this->sgbd();
264       $id_role = $sgbd->add_data(
265         "roles",
266         array(
267           "nom" => $nom,
268           "intitule" => $intitule
269         )
270       );
271       if(!isset($id_role)) return false;
272       return $id_role;
273     }
274
275     function get_role($id){
276       if($id === "0") return array(
277         "id" => 0,
278         "nom" => "",
279         "intitule" => ""
280       );
281       $sgbd = $this->sgbd();
282       $role = $sgbd->get_data("roles", $id);
283       if(!isset($role)) return false;
284       return $role ? $role : array();
285     }
286
287     function set_role($id, $nom, $intitule){
288       $sgbd = $this->sgbd();
289       if(
290         !$sgbd->set_data(
291           "roles",
292           $id,
293           array(
294             "nom" => $nom,
295             "intitule" => $intitule
296           )
297         )
298       ) return false;
299       return true;
300     }
301
302     function clear_role_actions($id_role){
303       $sgbd = $this->sgbd();
304       if($rst = $sgbd->open_data("action_status")){
305         $OK = true;
306         while($v_rst = $sgbd->fetch_data($rst)){
307           if(isset($v_rst)){
308             if(isset($v_rst["id"]) && isset($v_rst["id_status"]) && $v_rst["id_status"] == $id_role){
309               if(!$sgbd->del_data("action_status", $v_rst["id"])){
310                 $OK = false;
311                 break;
312               }
313             }
314           }
315           else $OK = false;
316         }
317         $sgbd->close_data($rst);
318         return $OK;
319       }
320       return false;
321     }
322
323     function clear_role_users($id_role){
324       $sgbd = $this->sgbd();
325       if($rst = $sgbd->open_data("users_roles")){
326         $OK = true;
327         while($v_rst = $sgbd->fetch_data($rst)){
328           if(isset($v_rst)){
329             if(isset($v_rst["id"]) && isset($v_rst["id_role"]) && $v_rst["id_role"] == $id_role){
330               if(!$sgbd->del_data("users_roles", $v_rst["id"])){
331                 $OK = false;
332                 break;
333               }
334             }
335           }
336           else $OK = false;
337         }
338         $sgbd->close_data($rst);
339         return $OK;
340       }
341       return false;
342     }
343
344     function add_role_action($id_role, $action){
345       $sgbd = $this->sgbd();
346       $id_action_status = $sgbd->add_data(
347         "action_status",
348         array(
349           "action" => $action,
350           "id_status" => $id_role
351         )
352       );
353       if(!isset($id_action_status)) return false;
354       return $id_action_status;
355     }
356
357     function del_role($id_role){
358       $sgbd = $this->sgbd();
359       return $sgbd->del_data("roles", $id_role) ? true : false;
360     }
361
362     function status(){
363       if(!isset($this->user_status)) return false;
364       return $this->user_status;
365     }
366
367     function get_user_status(){
368       $user_status = array();
369       $user = $this->get_session_user();
370       if($user && isset($user["id"])){
371         $sgbd = $this->sgbd();
372         if($rst = $sgbd->open_data("users_roles")){
373           while($v_rst = $sgbd->fetch_data($rst)){
374             if(isset($v_rst)){
375               if(isset($v_rst["id_role"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $user["id"]){
376                 $user_status[] = $v_rst["id_role"];
377               }
378             }
379             else{
380               $user_status = false;
381               break;
382             }
383           }
384           $sgbd->close_data($rst);
385         }
386         else $user_status = false;
387         if($user_status === false) return false;
388       }
389       else $user_status[] = 0;
390       if(!$user_status) $user_status[] = 0;
391       return $user_status;
392     }
393
394     function init_action_status($status = array()){
395       if(!isset($this->user_status)) return false;
396       $this->action_status = $this->read_action_status();
397       return $this->action_status;
398     }
399
400     function read_action_status($params = array()){
401       if(!isset($this->user_status)) return false;
402       $group_by_action = isset($params["group_by_action"]) ? $params["group_by_action"] : false;
403       $sgbd = $this->sgbd();
404       $action_status = array();
405       if($rst = $sgbd->open_data("action_status")){
406         while($v_rst = $sgbd->fetch_data($rst)){
407           if(isset($v_rst)){
408             if(isset($v_rst["action"]) && isset($v_rst["id_status"])){
409               if($group_by_action){
410                 if(!isset($action_status[$v_rst["action"]])) $action_status[$v_rst["action"]] = array();
411                 $action_status[$v_rst["action"]][] = $v_rst["id_status"];
412               }
413               else $action_status[$v_rst["id"]] = $v_rst;
414             }
415           }
416           else{
417             $action_status = false;
418             break;
419           }
420         }
421         $sgbd->close_data($rst);
422       }
423       else $action_status = false;
424       return $action_status;
425     }
426
427     function get_action_status($mod, $controller = "index", $action = "index", $set_status = array()){
428       $sgbd = $this->sgbd();
429       $status = array();
430       if($rst = $sgbd->open_data("action_status")){
431         while($status !==false && $v_rst = $sgbd->fetch_data($rst)){
432           if(isset($v_rst) && isset($v_rst["action"]) && isset($v_rst["id_status"])){
433             if(
434                  $v_rst["action"] == $mod
435               || $v_rst["action"] == $mod."/".$controller
436               || $v_rst["action"] == $mod."/".$controller."/".$action
437             ){
438               if(!isset($status[$v_rst["action"]])) $status[$v_rst["action"]] = array();
439               $status[$v_rst["action"]][$v_rst["id_status"]] = true;
440             }
441           }
442           else $status = false;
443         }
444         $sgbd->close_data($rst);
445       }
446       else $status = false;
447       return $status;
448     }
449
450     function get_actions($id_role = null){
451       $env = $this->env();
452       if($actions = $env->get_actions()){
453         if(($action_status = $this->read_action_status(array("group_by_action" => true))) !== false){
454           foreach($actions as $module_name => $module){
455             if(isset($id_role)) $actions[$module_name]["module_allowed"] =
456                 isset($action_status[$module_name])
457             &&  in_array($id_role, $action_status[$module_name]);
458             $actions[$module_name]["is_public"] =
459                 isset($action_status[$module_name])
460             &&  in_array(0, $action_status[$module_name]);
461             foreach($module["controleurs"] as $controleur_name => $controleur){
462               if(isset($id_role)) $actions[$module_name]["controleurs"][$controleur_name]["controleur_allowed"] =
463                   isset($action_status[$module_name."/".$controleur_name])
464               &&  in_array($id_role, $action_status[$module_name."/".$controleur_name]);
465               $actions[$module_name]["controleurs"][$controleur_name]["is_public"] =
466                   isset($action_status[$module_name."/".$controleur_name])
467               &&  in_array(0, $action_status[$module_name."/".$controleur_name]);
468               foreach($controleur["als"] as $index_als => $al){
469                 if($al["actions"]){
470                   if(isset($id_role)){
471                     $HAS_ACTION_NOT_ALLOWED = false;
472                     foreach($al["actions"] as $action_name){
473                       if(
474                           !isset($action_status[$module_name."/".$controleur_name."/".$action_name])
475                       ||  !in_array($id_role, $action_status[$module_name."/".$controleur_name."/".$action_name])
476                       ){
477                         $HAS_ACTION_NOT_ALLOWED = true;
478                         break;
479                       }
480                     }
481                     if(!$HAS_ACTION_NOT_ALLOWED){
482                       $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["action_allowed"] = true;
483                     }
484                   }
485                   $HAS_ACTION_NOT_ALLOWED = false;
486                   foreach($al["actions"] as $action_name){
487                     if(
488                         !isset($action_status[$module_name."/".$controleur_name."/".$action_name])
489                     ||  !in_array(0, $action_status[$module_name."/".$controleur_name."/".$action_name])
490                     ){
491                       $HAS_ACTION_NOT_ALLOWED = true;
492                       break;
493                     }
494                   }
495                   if(!$HAS_ACTION_NOT_ALLOWED){
496                     $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["is_public"] = true;
497                   }
498                 }
499               }
500             }
501           }
502           return $actions;
503         }
504       }
505       return array();
506     }
507
508     # ----------------------------------------------------------------------------------------
509     #                                                                             log in / out
510     #
511
512     function login($login, $password){
513       if(($user = $this->user($login)) !== false){
514         if($this->password_ok($user, $password)){
515           if(!$this->set_session($user)) $user = false;
516         }
517         else{
518           $this->clear_session();
519           $user = array();
520         }
521       }
522       return $user;
523     }
524
525     function logout(){
526       return $this->clear_session();
527     }
528
529     function user_ok($user){
530       return
531       strcmp(md5($user["password"].$_SESSION["id"]), $_SESSION["pass"]) == 0
532       && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
533     }
534
535     function password_ok($user, $password){
536       if(!$user) return false;
537       return
538            strcmp(md5($user["password"].$_SESSION["id"]), $password) == 0
539         && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
540     }
541
542     # ----------------------------------------------------------------------------------------
543     #                                                                                  session
544     #
545
546     function load_session(){
547       session_start();
548       if(!isset($_SESSION["id"])) $this->clear_session();
549       if(
550         $user = (
551           isset($_COOKIE["user"]) || isset($_SESSION["user"]) ?
552             $this->user(isset($_COOKIE["user"]) ? $_COOKIE["user"] : $_SESSION["user"])
553           : array()
554         )
555       ){
556         if(isset($_COOKIE["user"])) $this->set_session($user);
557         if(!$this->user_ok($user)){
558           $this->clear_session();
559           $user = array();
560         }
561       }
562       $this->_user = $user;
563       return $user;
564     }
565
566     function set_session($user){
567       $_SESSION["user"] = $user["login"];
568       $_SESSION["pass"] = md5($user["password"].$_SESSION["id"]);
569       $env = $this->env();
570       return setcookie("user", $user["login"], time() + (60 * 60 * 24 * 7), $env->path("web"));
571     }
572
573     function clear_session(){
574       unset($_SESSION["user"]);
575       unset($_SESSION["pass"]);
576       $_SESSION["ip"] = $_SERVER["REMOTE_ADDR"];
577       $_SESSION["id"] = md5(rand());
578       $env = $this->env();
579       return setcookie("user", "", 0, $env->path("web"));
580     }
581
582     function get_session_user(){
583       return $this->_user;
584     }
585
586     # ----------------------------------------------------------------------------------------
587     #                                                                                  uploads
588     #
589
590     function check_user_uploads_dir($user = null){
591       $env = $this->env();
592       $user_dir = $env->path("content")."uploads/".(isset($user) ? $user : $this->_user["id"]);
593       if(!file_exists($user_dir)) @mkdir($user_dir);
594       return file_exists($user_dir);
595     }
596
597   }
598
599 ?>