3 class mw_data_users extends mw_data{
10 # ----------------------------------------------------------------------------------------
14 function users($start = 0, $alpha = null, $id_role = null){
15 $sgbd = $this->sgbd();
17 $users = array("list" => array(), "total" => 0);
19 $SELECT = "SELECT #--users.*";
20 $FROM = " FROM #--users";
22 $WHERE .= (isset($alpha) ? ($WHERE ? " AND" : " WHERE")." LEFT(login, 1)=".$this->eq($alpha) : "");
24 $SELECT .= ", #--users_roles.id_role";
26 " LEFT JOIN #--users_roles ON ("
27 ." #--users_roles.id_user=#--users.id"
28 ." AND #--users_roles.id_role=".$this->eq($id_role)
30 $WHERE .= ($WHERE ? " AND" : " WHERE")." mw_users_roles.id_role IS NOT NULL";
32 $LIMIT = ($env->config("max_list") ? " LIMIT ".$env->config("max_list")." OFFSET ".$start : "");
33 $sql = "SELECT count(*) as n FROM(".$SELECT.$FROM.$WHERE.") res";
34 $rst = $sgbd->query($sql);
35 if($v_rst = $sgbd->fetch_assoc($rst)) $users["total"] = $v_rst["n"];
36 $sgbd->free_result($rst);
37 if($users["total"] > 0){
38 $sql = "SELECT * FROM(".$SELECT.$FROM.$WHERE.$LIMIT.") res";
39 $rst = $sgbd->query($sql);
40 while($v_rst = $sgbd->fetch_assoc($rst)) $users["list"][$v_rst["id"]] = $v_rst;
41 $sgbd->free_result($rst);
42 foreach($users["list"] as $id_user => $user){
43 if(($roles = $this->list_user_roles($id_user)) !== false){
44 $users["list"][$id_user]["roles"] = $roles;
53 catch(Exception $e) { $users = false; }
57 function list_user_roles($id_user){
58 $sgbd = $this->sgbd();
61 $rst = $sgbd->query("SELECT id_role FROM #--users_roles WHERE id_user=".$this->eq($id_user));
62 while($v_rst = $sgbd->fetch_assoc($rst)) $roles[] = $v_rst["id_role"];
63 $sgbd->free_result($rst);
65 catch(Exception $e) { $roles = false; }
69 function user_by_id($id){
70 $sgbd = $this->sgbd();
73 $sql = "SELECT * from #--users WHERE id=".$this->eq($id);
74 $rst = $sgbd->query($sql);
75 if($v_rst = $sgbd->fetch_assoc($rst)) $user = $v_rst;
76 $sgbd->free_result($rst);
77 if(($roles = $this->list_user_roles($user["id"])) !== false) $user["roles"] = $roles;
80 catch(Exception $e) { $user = false; }
84 function user($login){
85 $sgbd = $this->sgbd();
88 $sql = "SELECT * from #--users WHERE login=".$this->eq($login);
89 $rst = $sgbd->query($sql);
90 if($v_rst = $sgbd->fetch_assoc($rst)) $user = $v_rst;
91 $sgbd->free_result($rst);
93 if(($roles = $this->list_user_roles($user["id"])) !== false) $user["roles"] = $roles;
97 catch(Exception $e) { $user = false; }
101 function user_exists($login){
102 $sgbd = $this->sgbd();
105 $sql = "SELECT count(*) as n from #--users WHERE login=".$this->eq($login);
106 $rst = $sgbd->query($sql);
107 if($v_rst = $sgbd->fetch_assoc($rst)) $EXISTS = $v_rst["n"];
108 $sgbd->free_result($rst);
110 catch(Exception $e) { $EXISTS = false; }
114 function add_user($login, $password, $email, $roles){
115 $sgbd = $this->sgbd();
119 "INSERT INTO #--users(login, password, email) VALUES"
120 ."( ".$this->eq($login)
121 .", ".$this->eq($password)
122 .", ".$this->eq($email)
125 $user_id = $sgbd->insert_id();
126 foreach($roles as $id_role){
128 "INSERT INTO #--users_roles(id_user, id_role) VALUES"
130 .", ".$this->eq($id_role)
135 catch(Exception $e) { $user_id = false; }
139 function set_user($id, $login, $password, $email, $roles){
140 $sgbd = $this->sgbd();
143 "UPDATE #--users SET"
144 ." login=".$this->eq($login)
145 .", password=".$this->eq($password)
146 .", email=".$this->eq($email)
147 ." WHERE id=".$this->eq($id);
149 $sql = "DELETE FROM #--users_roles WHERE id_user=".$this->eq($id);
151 foreach($roles as $id_role){
153 "INSERT INTO #--users_roles(id_user, id_role) VALUES"
155 .", ".$this->eq($id_role)
160 catch(Exception $e) { return false; }
164 function del_user($login){
165 if(($user = $this->user($login)) !== false){
166 $sgbd = $this->sgbd();
168 $sql = "DELETE FROM #--users_roles WHERE id_user=".$user["id"];
170 $sql = "DELETE FROM #--users WHERE login=".$this->eq($login)." AND id=".$user["id"];
173 catch(Exception $e) { return false; }
179 # ----------------------------------------------------------------------------------------
183 function init_roles(){
184 $sgbd = $this->sgbd();
185 $this->roles = array();
187 $sql = "SELECT * FROM #--roles";
188 $rst = $sgbd->query($sql);
189 while($v_rst = $sgbd->fetch_assoc($rst)) $this->roles[$v_rst["id"]] = $v_rst;
190 $sgbd->free_result($rst);
192 catch(Exception $e) { $this->roles = false; }
197 if(!isset($this->roles)) return false;
201 function add_role($nom, $intitule){
202 $sgbd = $this->sgbd();
205 "INSERT INTO #--roles(nom, intitule) VALUES("
207 .", ".$this->eq($intitule)
209 $rst = $sgbd->query($sql);
210 $id_role = $sgbd->insert_id();
212 catch(Exception $e) { $id_role = false; }
216 function get_role($id){
217 if($id === "0") return array(
222 $sgbd = $this->sgbd();
225 $sql = "SELECT * FROM #--roles WHERE id=".$this->eq($id);
226 $rst = $sgbd->query($sql);
227 if($v_rst = $sgbd->fetch_assoc($rst)) $role = $v_rst;
228 $sgbd->free_result($rst);
230 catch(Exception $e) { $role = false; }
234 function set_role($id, $nom, $intitule){
235 $sgbd = $this->sgbd();
238 "UPDATE #--roles SET"
239 ." nom=".$this->eq($nom)
240 .", intitule=".$this->eq($intitule)
241 ." WHERE id=".$this->eq($id);
242 $rst = $sgbd->query($sql);
244 catch(Exception $e) { return false; }
248 function clear_role_actions($id_role){
249 $sgbd = $this->sgbd();
251 $sql = "DELETE FROM #--actions_roles WHERE id_role=".$this->eq($id_role);
254 catch(Exception $e) { return false; }
258 function clear_role_users($id_role){
259 $sgbd = $this->sgbd();
261 $sql = "DELETE FROM #--users_roles WHERE id_role=".$this->eq($id_role);
264 catch(Exception $e) { return false; }
268 function add_role_action($id_role, $action){
269 $sgbd = $this->sgbd();
271 $sql = "INSERT INTO #--actions_roles(action, id_role) VALUES(".$this->eq($action).", ".$this->eq($id_role).")";
273 $id_action_role = $sgbd->insert_id();
275 catch(Exception $e) { $id_action_role = false; }
276 return $id_action_role;
279 function del_role($id_role){
280 $sgbd = $this->sgbd();
282 $sql = "DELETE FROM #--roles WHERE id=".$this->eq($id_role);
285 catch(Exception $e) { return false; }
289 function get_user_roles(){
290 $user_roles = array();
291 $user = $this->get_session_user();
292 if($user && isset($user["id"])){
293 $sgbd = $this->sgbd();
295 $sql = "SELECT id_role FROM #--users_roles WHERE id_user=".$this->eq($user["id"]);
296 $rst = $sgbd->query($sql);
297 while($v_rst = $sgbd->fetch_assoc($rst)) $user_roles[] = $v_rst["id_role"];
298 $sgbd->free_result($rst);
300 catch(Exception $_e){ return false; }
302 else $user_roles[] = 0;
303 if(!$user_roles) $user_roles[] = 0;
307 function init_actions_roles(){
308 if(!isset($this->roles)) return false;
309 $this->actions_roles = $this->read_actions_roles();
310 return $this->actions_roles;
313 function read_actions_roles($params = array()){
314 $group_by_action = isset($params["group_by_action"]) ? $params["group_by_action"] : false;
315 $sgbd = $this->sgbd();
316 $actions_roles = array();
318 $sql = "SELECT * FROM #--actions_roles";
319 $rst = $sgbd->query($sql);
320 while($v_rst = $sgbd->fetch_assoc($rst)){
321 if($group_by_action){
322 if(!isset($actions_roles[$v_rst["action"]])) $actions_roles[$v_rst["action"]] = array();
323 $actions_roles[$v_rst["action"]][] = $v_rst["id_role"];
325 else $actions_roles[$v_rst["id"]] = $v_rst;
327 $sgbd->free_result($rst);
329 catch(Exception $e) { $actions_roles = false; }
330 return $actions_roles;
333 function get_action_roles($mod, $controller = "index", $action = "index"){
334 $sgbd = $this->sgbd();
338 "SELECT action, id_role"
339 ." FROM #--actions_roles"
340 ." WHERE action=".$this->eq($mod)
341 ." OR action=".$this->eq($mod."/".$controller)
342 ." OR action=".$this->eq($mod."/".$controller."/".$action);
343 $rst = $sgbd->query($sql);
344 while($v_rst = $sgbd->fetch_assoc($rst)){
345 if(!isset($roles[$v_rst["action"]])) $roles[$v_rst["action"]] = array();
346 $roles[$v_rst["action"]][$v_rst["id_role"]] = true;
348 $sgbd->free_result($rst);
350 catch(Exception $e) { $roles = false; }
354 function get_actions($id_role = null){
356 if($actions = $env->get_actions()){
357 if(($actions_roles = $this->read_actions_roles(array("group_by_action" => true))) !== false){
358 foreach($actions as $module_name => $module){
359 if(isset($id_role)) $actions[$module_name]["module_allowed"] =
360 isset($actions_roles[$module_name])
361 && in_array($id_role, $actions_roles[$module_name]);
362 $actions[$module_name]["is_public"] =
363 isset($actions_roles[$module_name])
364 && in_array(0, $actions_roles[$module_name]);
365 foreach($module["controleurs"] as $controleur_name => $controleur){
366 if(isset($id_role)) $actions[$module_name]["controleurs"][$controleur_name]["controleur_allowed"] =
367 isset($actions_roles[$module_name."/".$controleur_name])
368 && in_array($id_role, $actions_roles[$module_name."/".$controleur_name]);
369 $actions[$module_name]["controleurs"][$controleur_name]["is_public"] =
370 isset($actions_roles[$module_name."/".$controleur_name])
371 && in_array(0, $actions_roles[$module_name."/".$controleur_name]);
372 foreach($controleur["als"] as $index_als => $al){
375 $HAS_ACTION_NOT_ALLOWED = false;
376 foreach($al["actions"] as $action_name){
378 !isset($actions_roles[$module_name."/".$controleur_name."/".$action_name])
379 || !in_array($id_role, $actions_roles[$module_name."/".$controleur_name."/".$action_name])
381 $HAS_ACTION_NOT_ALLOWED = true;
385 if(!$HAS_ACTION_NOT_ALLOWED){
386 $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["action_allowed"] = true;
389 $HAS_ACTION_NOT_ALLOWED = false;
390 foreach($al["actions"] as $action_name){
392 !isset($actions_roles[$module_name."/".$controleur_name."/".$action_name])
393 || !in_array(0, $actions_roles[$module_name."/".$controleur_name."/".$action_name])
395 $HAS_ACTION_NOT_ALLOWED = true;
399 if(!$HAS_ACTION_NOT_ALLOWED){
400 $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["is_public"] = true;
412 # ----------------------------------------------------------------------------------------
416 function login($login, $password){
417 if(($user = $this->user($login)) !== false){
418 if($this->password_ok($user, $password)){
419 if(!$this->set_session($user)) $user = false;
422 $this->clear_session();
430 return $this->clear_session();
433 function user_ok($user){
435 strcmp(md5($user["password"].$_SESSION["id"]), $_SESSION["pass"]) == 0
436 && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
439 function password_ok($user, $password){
440 if(!$user) return false;
442 strcmp(md5($user["password"].$_SESSION["id"]), $password) == 0
443 && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
446 # ----------------------------------------------------------------------------------------
450 function load_session(){
452 if(!isset($_SESSION["id"])) $this->clear_session();
454 if(isset($_SESSION["user"])){
455 $user = $this->user($_SESSION["user"]);
457 elseif(isset($_COOKIE["user"]) && isset($_COOKIE["pass"])){
458 if($user = $this->user($_COOKIE["user"])){
459 $user["password"] = $_COOKIE["pass"];
460 $this->set_session($user);
464 if(!$this->user_ok($user)){
465 $this->clear_session();
469 else $user = array();
474 function set_session($user){
475 $_SESSION["user"] = $user["login"];
476 $_SESSION["pass"] = md5($user["password"].$_SESSION["id"]);
479 setcookie("user", $user["login"], time() + (60 * 60 * 24 * 7), $env->path("web"))
480 && setcookie("pass", $user["password"], time() + (60 * 60 * 24 * 7), $env->path("web"));
483 function clear_session(){
484 unset($_SESSION["user"]);
485 unset($_SESSION["pass"]);
486 $_SESSION["ip"] = $_SERVER["REMOTE_ADDR"];
487 $_SESSION["id"] = md5(rand());
490 setcookie("user", "", 0, $env->path("web"))
491 && setcookie("pass", "", 0, $env->path("web"));
494 function get_session_user(){
498 # ----------------------------------------------------------------------------------------
502 function check_user_uploads_dir($user = null){
504 if((!isset($user) || !$user) && !isset($this->user["id"])) return false;
505 $user_dir = $env->path("content")."uploads/".(isset($user) && $user ? $user : $this->user["id"]);
506 if(!file_exists($user_dir)) @mkdir($user_dir);
507 return file_exists($user_dir);