3 class mw_data_users extends mw_data
11 # ----------------------------------------------------------------------------------------
15 function users($start = 0, $alpha = null, $status = null)
16 { $sgbd = $this->sgbd();
18 $users = array("list" => array(), "total" => 0);
20 if($rst = $sgbd->open_data("users"))
21 { while($v_rst = $sgbd->fetch_data($rst))
23 { if(!isset($alpha) || (isset($v_rst["login"]) && strtolower(substr($v_rst["login"], 0, 1)) == strtolower($alpha)))
24 { if(!isset($status) || (isset($v_rst["status"]) && $v_rst["status"] == $status))
25 { $res[$v_rst["id"]] = $v_rst;
35 $sgbd->close_data($rst);
38 foreach($res as $id_user => $user)
40 if(!$env->config("max_list") || ($n > $start && $n <= ($start + $env->config("max_list"))))
41 { $users["list"][$user["id"]] = $user;
42 if(!isset($this->users)) $this->users = array();
43 $this->users[$user["id"]] = $user;
53 function user_by_id($id)
54 { if(!isset($this->users)) $this->users = array();
55 if(isset($this->users[$id])) return $this->users[$id];
56 $sgbd = $this->sgbd();
57 if(($user = $sgbd->get_data("users", $id)) !== false)
58 { $this->users[$id] = $user;
64 { $sgbd = $this->sgbd();
66 if($rst = $sgbd->open_data("users"))
67 { while($v_rst = $sgbd->fetch_data($rst))
69 { if(isset($v_rst["login"]) && $v_rst["login"] == $login)
76 $sgbd->close_data($rst);
80 { if(!isset($this->users)) $this->users = array();
81 $this->users[$user["id"]] = $user;
86 function user_exists($login)
87 { $sgbd = $this->sgbd();
89 if($rst = $sgbd->open_data("users"))
90 { while($v_rst = $sgbd->fetch_data($rst))
92 { if(isset($v_rst["login"]) && $v_rst["login"] == $login)
101 $sgbd->close_data($rst);
103 else $EXISTS = false;
107 function add_user($login, $password, $email, $status)
108 { $sgbd = $this->sgbd();
109 return $sgbd->add_data
113 "password" => $password,
120 function set_user($id, $login, $password, $email, $status)
121 { $sgbd = $this->sgbd();
122 return $sgbd->set_data
127 "password" => $password,
134 function del_user($login)
135 { if(($user = $this->user($login)) !== false)
136 { $sgbd = $this->sgbd();
137 return $sgbd->del_data("users", $user["id"]);
142 # ----------------------------------------------------------------------------------------
147 { if(!isset($this->user_status)) return false;
148 return $this->user_status;
151 function init_user_status($status = array())
152 { $sgbd = $this->sgbd();
153 $this->user_status = array();
154 if($rst = $sgbd->open_data("user_status"))
155 { while($v_rst = $sgbd->fetch_data($rst))
157 { $this->user_status[$v_rst["id"]] = $v_rst;
160 { $this->user_status = false;
164 $sgbd->close_data($rst);
166 else $this->user_status = false;
167 if($status && $this->user_status !== false)
168 { foreach($status as $new_user_status)
169 { $id_status = false;
170 foreach($this->user_status as $user_status) if($new_user_status["nom"] == $user_status["nom"])
171 { $id_status = $user_status["id"];
176 foreach($new_user_status as $status_key => $status_value)
177 { if(!isset($this->user_status[$id_status][$status_key]) || $this->user_status[$id_status][$status_key] != $status_value)
178 { $SAME = false; break;
182 { if($sgbd->set_data("user_status", $id_status, $new_user_status)) $this->user_status[$id_status] = $new_user_status;
183 else { $this->user_status = false; break; }
187 { if($id_status = $sgbd->add_data("user_status", $new_user_status)) $this->user_status[$id_status] = $new_user_status;
188 else { $this->user_status = false; break; }
192 return $this->user_status;
195 function init_action_status($status = array())
196 { if(!isset($this->user_status)) return false;
197 $sgbd = $this->sgbd();
198 $this->action_status = array();
199 if($rst = $sgbd->open_data("action_status"))
200 { while($v_rst = $sgbd->fetch_data($rst))
202 { $this->action_status[$v_rst["id"]] = $v_rst;
205 { $this->action_status = false;
209 $sgbd->close_data($rst);
211 else $this->action_status = false;
212 if($status && $this->action_status !== false)
214 foreach($status as $id_new_action_status => $new_action_status)
215 { $FOUND = $new_action_status["id_status"] == "0";
216 if(!$FOUND) foreach($this->user_status as $user_status)
217 { if($new_action_status["id_status"] == $user_status["nom"])
219 $status[$id_new_action_status]["id_status"] = $user_status["id"];
223 { $STATUS_OK = false;
228 { foreach($status as $new_action_status)
229 { $id_status = false;
230 foreach($this->action_status as $action_status)
232 ( $new_action_status["action"] == $action_status["action"]
233 && $new_action_status["id_status"] == $action_status["id_status"]
235 { $id_status = $action_status["id"];
241 foreach($new_action_status as $status_key => $status_value)
242 { if(!isset($this->action_status[$id_status][$status_key]) || $this->action_status[$id_status][$status_key] != $status_value)
243 { $SAME = false; break;
247 { if($id_status = $sgbd->add_data("action_status", $new_action_status)) $this->action_status[$id_status] = $new_action_status;
248 else { $this->action_status = false; break; }
252 { if($id_status = $sgbd->add_data("action_status", $new_action_status)) $this->action_status[$id_status] = $new_action_status;
253 else { $this->action_status = false; break; }
257 else $this->action_status = false;
259 return $this->action_status;
262 function get_user_status()
263 { $user = $this->get_session_user();
264 if($user && isset($user["status"])) return $user["status"];
268 function get_action_status($mod, $controller = "index", $action = "index", $set_status = array())
269 { $sgbd = $this->sgbd();
270 if($rst = $sgbd->open_data("action_status"))
271 { while($status !==false && $v_rst = $sgbd->fetch_data($rst))
272 { if(isset($v_rst) && isset($v_rst["action"]) && isset($v_rst["id_status"]))
274 ( $v_rst["action"] == $mod
275 || $v_rst["action"] == $mod."/".$controller
276 || $v_rst["action"] == $mod."/".$controller."/".$action
278 { if(!isset($status[$v_rst["action"]])) $status[$v_rst["action"]] = array();
279 $status[$v_rst["action"]][$v_rst["id_status"]] = true;
282 else $status = false;
284 $sgbd->close_data($rst);
286 else $status = false;
287 if($status !== false)
289 { foreach($set_status as $new_action_status)
290 { $id_status = false;
291 foreach($status as $user_status) if($new_user_status["nom"] == $user_status["nom"])
292 { $id_status = $user_status["id"];
297 foreach($new_user_status as $status_key => $status_value)
298 { if(!isset($status[$id_status][$status_key]) || $status[$id_status][$status_key] != $status_value)
299 { $SAME = false; break;
303 { if($sgbd->set_data("user_status", $id_status, $new_user_status)) $status[$id_status] = $new_user_status;
304 else { $status = false; break; }
308 { if($id_status = $sgbd->add_data("user_status", $new_user_status)) $status[$id_status] = $new_user_status;
309 else { $status = false; break; }
317 function creation_default_status()
318 { $sgbd = $this->sgbd();
320 if($rst = $sgbd->open_data("user_status"))
321 { while($v_rst = $sgbd->fetch_data($rst))
323 { if(isset($v_rst["creation_default"]) && $v_rst["creation_default"] == 1)
324 { $default_status = $v_rst["id"];
329 { $default_status = false;
333 $sgbd->close_data($rst);
335 else $default_status = false;
336 return $default_status;
339 # ----------------------------------------------------------------------------------------
343 function login($login, $password)
344 { if(($user = $this->user($login)) !== false)
345 { if($this->password_ok($user, $password))
346 { if(!$this->set_session($user)) $user = false;
349 { $this->clear_session();
357 { return $this->clear_session();
360 function user_ok($user)
362 strcmp(md5($user["password"].$_SESSION["id"]), $_SESSION["pass"]) == 0
363 && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
366 function password_ok($user, $password)
368 strcmp(md5($user["password"].$_SESSION["id"]), $password) == 0
369 && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
372 # ----------------------------------------------------------------------------------------
376 function load_session()
378 if(!isset($_SESSION["id"])) $this->clear_session();
381 ( isset($_COOKIE["user"]) || isset($_SESSION["user"]) ?
382 $this->user(isset($_COOKIE["user"]) ? $_COOKIE["user"] : $_SESSION["user"])
386 { if(isset($_COOKIE["user"])) $this->set_session($user);
387 if(!$this->user_ok($user))
388 { $this->clear_session();
392 $this->_user = $user;
396 function set_session($user)
397 { $_SESSION["user"] = $user["login"];
398 $_SESSION["pass"] = md5($user["password"].$_SESSION["id"]);
400 return setcookie("user", $user["login"], time() + (60 * 60 * 24 * 7), $env->path("web"));
403 function clear_session()
404 { unset($_SESSION["user"]);
405 unset($_SESSION["pass"]);
406 $_SESSION["ip"] = $_SERVER["REMOTE_ADDR"];
407 $_SESSION["id"] = md5(rand());
409 return setcookie("user", "", 0, $env->path("web"));
412 function get_session_user() { return $this->_user; }
414 # ----------------------------------------------------------------------------------------
418 function check_user_uploads_dir($user = null)
419 { $env = $this->env();
420 $user_dir = $env->path("content")."uploads/".(isset($user) ? $user : $this->_user["id"]);
421 if(!file_exists($user_dir)) @mkdir($user_dir);
422 return file_exists($user_dir);