3 class mw_data_users extends mw_data{
10 # ----------------------------------------------------------------------------------------
14 function users($start = 0, $alpha = null, $status = null){
15 $sgbd = $this->sgbd();
17 $users = array("list" => array(), "total" => 0);
19 if($rst = $sgbd->open_data("users")){
20 while($v_rst = $sgbd->fetch_data($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;
34 $sgbd->close_data($rst);
37 foreach($res as $id_user => $user){
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;
52 function user_by_id($id){
53 if(!isset($this->users)) $this->users = array();
54 if(isset($this->users[$id])) return $this->users[$id];
55 $sgbd = $this->sgbd();
56 if(($user = $sgbd->get_data("users", $id)) !== false){
57 $this->users[$id] = $user;
62 function user($login){
63 $sgbd = $this->sgbd();
65 if($rst = $sgbd->open_data("users")){
66 while($v_rst = $sgbd->fetch_data($rst)){
68 if(isset($v_rst["login"]) && $v_rst["login"] == $login){
75 $sgbd->close_data($rst);
79 if(!isset($this->users)) $this->users = array();
80 $this->users[$user["id"]] = $user;
85 function user_exists($login){
86 $sgbd = $this->sgbd();
88 if($rst = $sgbd->open_data("users")){
89 while($v_rst = $sgbd->fetch_data($rst)){
91 if(isset($v_rst["login"]) && $v_rst["login"] == $login){
100 $sgbd->close_data($rst);
102 else $EXISTS = false;
106 function add_user($login, $password, $email, $status){
107 $sgbd = $this->sgbd();
108 return $sgbd->add_data(
112 "password" => $password,
119 function set_user($id, $login, $password, $email, $status){
120 $sgbd = $this->sgbd();
121 return $sgbd->set_data(
126 "password" => $password,
133 function del_user($login){
134 if(($user = $this->user($login)) !== false){
135 $sgbd = $this->sgbd();
136 return $sgbd->del_data("users", $user["id"]);
141 # ----------------------------------------------------------------------------------------
146 if(!isset($this->user_status)) return false;
147 return $this->user_status;
150 function init_user_status($status = array()){
151 $sgbd = $this->sgbd();
152 $this->user_status = array();
153 if($rst = $sgbd->open_data("user_status")){
154 while($v_rst = $sgbd->fetch_data($rst)){
156 $this->user_status[$v_rst["id"]] = $v_rst;
159 $this->user_status = false;
163 $sgbd->close_data($rst);
165 else $this->user_status = false;
166 if($status && $this->user_status !== false){
167 foreach($status as $new_user_status){
169 foreach($this->user_status as $user_status) if($new_user_status["nom"] == $user_status["nom"]){
170 $id_status = $user_status["id"];
175 foreach($new_user_status as $status_key => $status_value){
176 if(!isset($this->user_status[$id_status][$status_key]) || $this->user_status[$id_status][$status_key] != $status_value){
182 if($sgbd->set_data("user_status", $id_status, $new_user_status)) $this->user_status[$id_status] = $new_user_status;
184 $this->user_status = false;
190 if($id_status = $sgbd->add_data("user_status", $new_user_status)) $this->user_status[$id_status] = $new_user_status;
192 $this->user_status = false;
198 return $this->user_status;
201 function init_action_status($status = array()){
202 if(!isset($this->user_status)) return false;
203 $sgbd = $this->sgbd();
204 $this->action_status = array();
205 if($rst = $sgbd->open_data("action_status")){
206 while($v_rst = $sgbd->fetch_data($rst)){
208 $this->action_status[$v_rst["id"]] = $v_rst;
211 $this->action_status = false;
215 $sgbd->close_data($rst);
217 else $this->action_status = false;
218 if($status && $this->action_status !== false){
220 foreach($status as $id_new_action_status => $new_action_status){
221 $FOUND = $new_action_status["id_status"] == "0";
222 if(!$FOUND) foreach($this->user_status as $user_status){
223 if($new_action_status["id_status"] == $user_status["nom"]){
225 $status[$id_new_action_status]["id_status"] = $user_status["id"];
234 foreach($status as $new_action_status){
236 foreach($this->action_status as $action_status){
238 $new_action_status["action"] == $action_status["action"]
239 && $new_action_status["id_status"] == $action_status["id_status"]
241 $id_status = $action_status["id"];
247 foreach($new_action_status as $status_key => $status_value){
248 if(!isset($this->action_status[$id_status][$status_key]) || $this->action_status[$id_status][$status_key] != $status_value){
254 if($id_status = $sgbd->add_data("action_status", $new_action_status)) $this->action_status[$id_status] = $new_action_status;
256 $this->action_status = false;
262 if($id_status = $sgbd->add_data("action_status", $new_action_status)) $this->action_status[$id_status] = $new_action_status;
264 $this->action_status = false;
270 else $this->action_status = false;
272 return $this->action_status;
275 function get_user_status(){
276 $user = $this->get_session_user();
277 if($user && isset($user["status"])) return $user["status"];
281 function get_action_status($mod, $controller = "index", $action = "index", $set_status = array()){
282 $sgbd = $this->sgbd();
283 if($rst = $sgbd->open_data("action_status")){
284 while($status !==false && $v_rst = $sgbd->fetch_data($rst)){
285 if(isset($v_rst) && isset($v_rst["action"]) && isset($v_rst["id_status"])){
287 $v_rst["action"] == $mod
288 || $v_rst["action"] == $mod."/".$controller
289 || $v_rst["action"] == $mod."/".$controller."/".$action
291 if(!isset($status[$v_rst["action"]])) $status[$v_rst["action"]] = array();
292 $status[$v_rst["action"]][$v_rst["id_status"]] = true;
295 else $status = false;
297 $sgbd->close_data($rst);
299 else $status = false;
300 if($status !== false){
302 foreach($set_status as $new_action_status){
304 foreach($status as $user_status) if($new_user_status["nom"] == $user_status["nom"]){
305 $id_status = $user_status["id"];
310 foreach($new_user_status as $status_key => $status_value){
311 if(!isset($status[$id_status][$status_key]) || $status[$id_status][$status_key] != $status_value){
317 if($sgbd->set_data("user_status", $id_status, $new_user_status)) $status[$id_status] = $new_user_status;
325 if($id_status = $sgbd->add_data("user_status", $new_user_status)) $status[$id_status] = $new_user_status;
337 function creation_default_status(){
338 $sgbd = $this->sgbd();
340 if($rst = $sgbd->open_data("user_status")){
341 while($v_rst = $sgbd->fetch_data($rst)){
343 if(isset($v_rst["creation_default"]) && $v_rst["creation_default"] == 1){
344 $default_status = $v_rst["id"];
349 $default_status = false;
353 $sgbd->close_data($rst);
355 else $default_status = false;
356 return $default_status;
359 # ----------------------------------------------------------------------------------------
363 function login($login, $password){
364 if(($user = $this->user($login)) !== false){
365 if($this->password_ok($user, $password)){
366 if(!$this->set_session($user)) $user = false;
369 $this->clear_session();
377 return $this->clear_session();
380 function user_ok($user){
382 strcmp(md5($user["password"].$_SESSION["id"]), $_SESSION["pass"]) == 0
383 && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
386 function password_ok($user, $password){
388 strcmp(md5($user["password"].$_SESSION["id"]), $password) == 0
389 && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
392 # ----------------------------------------------------------------------------------------
396 function load_session(){
398 if(!isset($_SESSION["id"])) $this->clear_session();
401 isset($_COOKIE["user"]) || isset($_SESSION["user"]) ?
402 $this->user(isset($_COOKIE["user"]) ? $_COOKIE["user"] : $_SESSION["user"])
406 if(isset($_COOKIE["user"])) $this->set_session($user);
407 if(!$this->user_ok($user)){
408 $this->clear_session();
412 $this->_user = $user;
416 function set_session($user){
417 $_SESSION["user"] = $user["login"];
418 $_SESSION["pass"] = md5($user["password"].$_SESSION["id"]);
420 return setcookie("user", $user["login"], time() + (60 * 60 * 24 * 7), $env->path("web"));
423 function clear_session(){
424 unset($_SESSION["user"]);
425 unset($_SESSION["pass"]);
426 $_SESSION["ip"] = $_SERVER["REMOTE_ADDR"];
427 $_SESSION["id"] = md5(rand());
429 return setcookie("user", "", 0, $env->path("web"));
432 function get_session_user(){
436 # ----------------------------------------------------------------------------------------
440 function check_user_uploads_dir($user = null){
442 $user_dir = $env->path("content")."uploads/".(isset($user) ? $user : $this->_user["id"]);
443 if(!file_exists($user_dir)) @mkdir($user_dir);
444 return file_exists($user_dir);