correction notive data users xml
[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         }
46         else $users = false;
47       }
48       else $users = false;
49       return $users;
50     }
51
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;
58       }
59       return $user;
60     }
61
62     function user($login){
63       $sgbd = $this->sgbd();
64       $user = array();
65       if($rst = $sgbd->open_data("users")){
66         while($v_rst = $sgbd->fetch_data($rst)){
67           if(isset($v_rst)){
68             if(isset($v_rst["login"]) && $v_rst["login"] == $login){
69               $user = $v_rst;
70               break;
71             }
72           }
73           else $user = false;
74         }
75         $sgbd->close_data($rst);
76       }
77       else $user = false;
78       if($user !== false){
79         if(!isset($this->users)) $this->users = array();
80         if($user) $this->users[$user["id"]] = $user;
81       }
82       return $user;
83     }
84
85     function user_exists($login){
86       $sgbd = $this->sgbd();
87       $EXISTS = 0;
88       if($rst = $sgbd->open_data("users")){
89         while($v_rst = $sgbd->fetch_data($rst)){
90           if(isset($v_rst)){
91             if(isset($v_rst["login"]) && $v_rst["login"] == $login){
92               $EXISTS++;
93             }
94           }
95           else{
96             $EXISTS = false;
97             break;
98           }
99         }
100         $sgbd->close_data($rst);
101       }
102       else $EXISTS = false;
103       return $EXISTS;
104     }
105
106     function add_user($login, $password, $email, $status){
107       $sgbd = $this->sgbd();
108       return $sgbd->add_data(
109         "users",
110         array(
111           "login" => $login,
112           "password" => $password,
113           "email" => $email,
114           "status" => $status
115         )
116       );
117     }
118
119     function set_user($id, $login, $password, $email, $status){
120       $sgbd = $this->sgbd();
121       return $sgbd->set_data(
122         "users",
123         $id,
124         array(
125           "login" => $login,
126           "password" => $password,
127           "email" => $email,
128           "status" => $status
129         )
130       );
131     }
132
133     function del_user($login){
134       if(($user = $this->user($login)) !== false){
135         $sgbd = $this->sgbd();
136         return $sgbd->del_data("users", $user["id"]);
137       }
138       return false;
139     }
140
141     # ----------------------------------------------------------------------------------------
142     #                                                                                   status
143     #
144
145     function status(){
146       if(!isset($this->user_status)) return false;
147       return $this->user_status;
148     }
149
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)){
155           if(isset($v_rst)){
156             $this->user_status[$v_rst["id"]] = $v_rst;
157           }
158           else{
159             $this->user_status = false;
160             break;
161           }
162         }
163         $sgbd->close_data($rst);
164       }
165       else $this->user_status = false;
166       if($status && $this->user_status !== false){
167         foreach($status as $new_user_status){
168           $id_status = false;
169           foreach($this->user_status as $user_status) if($new_user_status["nom"] == $user_status["nom"]){
170             $id_status = $user_status["id"];
171             break;
172           }
173           if($id_status){
174             $SAME = true;
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){
177                 $SAME = false;
178                 break;
179               }
180             }
181             if(!$SAME){
182               if($sgbd->set_data("user_status", $id_status, $new_user_status)) $this->user_status[$id_status] = $new_user_status;
183               else{
184                 $this->user_status = false;
185                 break;
186               }
187             }
188           }
189           else{
190             if($id_status = $sgbd->add_data("user_status", $new_user_status)) $this->user_status[$id_status] = $new_user_status;
191             else{
192               $this->user_status = false;
193               break;
194             }
195           }
196         }
197       }
198       return $this->user_status;
199     }
200
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)){
207           if(isset($v_rst)){
208             $this->action_status[$v_rst["id"]] = $v_rst;
209           }
210           else{
211             $this->action_status = false;
212             break;
213           }
214         }
215         $sgbd->close_data($rst);
216       }
217       else $this->action_status = false;
218       if($status && $this->action_status !== false){
219         $STATUS_OK = true;
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"]){
224               $FOUND = true;
225               $status[$id_new_action_status]["id_status"] = $user_status["id"];
226             }
227           }
228           if(!$FOUND){
229             $STATUS_OK = false;
230             break;
231           }
232         }
233         if($STATUS_OK){
234           foreach($status as $new_action_status){
235             $id_status = false;
236             foreach($this->action_status as $action_status){
237               if(
238                    $new_action_status["action"] == $action_status["action"]
239                 && $new_action_status["id_status"] == $action_status["id_status"]
240               ){
241                 $id_status = $action_status["id"];
242                 break;
243               }
244             }
245             if($id_status){
246               $SAME = true;
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){
249                   $SAME = false;
250                   break;
251                 }
252               }
253               if(!$SAME){
254                 if($id_status = $sgbd->add_data("action_status", $new_action_status)) $this->action_status[$id_status] = $new_action_status;
255                 else{
256                   $this->action_status = false;
257                   break;
258                 }
259               }
260             }
261             else{
262               if($id_status = $sgbd->add_data("action_status", $new_action_status)) $this->action_status[$id_status] = $new_action_status;
263               else{
264                 $this->action_status = false;
265                 break;
266               }
267             }
268           }
269         }
270         else $this->action_status = false;
271       }
272       return $this->action_status;
273     }
274
275     function get_user_status(){
276       $user = $this->get_session_user();
277       if($user && isset($user["status"])) return $user["status"];
278       return 0;
279     }
280
281     function get_action_status($mod, $controller = "index", $action = "index", $set_status = array()){
282       $sgbd = $this->sgbd();
283       $status = array();
284       if($rst = $sgbd->open_data("action_status")){
285         while($status !==false && $v_rst = $sgbd->fetch_data($rst)){
286           if(isset($v_rst) && isset($v_rst["action"]) && isset($v_rst["id_status"])){
287             if(
288                  $v_rst["action"] == $mod
289               || $v_rst["action"] == $mod."/".$controller
290               || $v_rst["action"] == $mod."/".$controller."/".$action
291             ){
292               if(!isset($status[$v_rst["action"]])) $status[$v_rst["action"]] = array();
293               $status[$v_rst["action"]][$v_rst["id_status"]] = true;
294             }
295           }
296           else $status = false;
297         }
298         $sgbd->close_data($rst);
299       }
300       else $status = false;
301       if($status !== false){
302         if($set_status){
303           foreach($set_status as $new_action_status){
304             $id_status = false;
305             foreach($status as $user_status) if($new_user_status["nom"] == $user_status["nom"]){
306               $id_status = $user_status["id"];
307               break;
308             }
309             if($id_status){
310               $SAME = true;
311               foreach($new_user_status as $status_key => $status_value){
312                 if(!isset($status[$id_status][$status_key]) || $status[$id_status][$status_key] != $status_value){
313                   $SAME = false;
314                   break;
315                 }
316               }
317               if(!$SAME){
318                 if($sgbd->set_data("user_status", $id_status, $new_user_status)) $status[$id_status] = $new_user_status;
319                 else{
320                   $status = false;
321                   break;
322                 }
323               }
324             }
325             else{
326               if($id_status = $sgbd->add_data("user_status", $new_user_status)) $status[$id_status] = $new_user_status;
327               else{
328                 $status = false;
329                 break;
330               }
331             }
332           }
333         }
334       }
335       return $status;
336     }
337
338     function creation_default_status(){
339       $sgbd = $this->sgbd();
340       $default_status = 0;
341       if($rst = $sgbd->open_data("user_status")){
342         while($v_rst = $sgbd->fetch_data($rst)){
343           if(isset($v_rst)){
344             if(isset($v_rst["creation_default"]) && $v_rst["creation_default"] == 1){
345               $default_status = $v_rst["id"];
346               break;
347             }
348           }
349           else{
350             $default_status = false;
351             break;
352           }
353         }
354         $sgbd->close_data($rst);
355       }
356       else $default_status = false;
357       return $default_status;
358     }
359
360     # ----------------------------------------------------------------------------------------
361     #                                                                             log in / out
362     #
363
364     function login($login, $password){
365       if(($user = $this->user($login)) !== false){
366         if($this->password_ok($user, $password)){
367           if(!$this->set_session($user)) $user = false;
368         }
369         else{
370           $this->clear_session();
371           $user = array();
372         }
373       }
374       return $user;
375     }
376
377     function logout(){
378       return $this->clear_session();
379     }
380
381     function user_ok($user){
382       return
383       strcmp(md5($user["password"].$_SESSION["id"]), $_SESSION["pass"]) == 0
384       && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
385     }
386
387     function password_ok($user, $password){
388       if(!$user) return false;
389       return
390            strcmp(md5($user["password"].$_SESSION["id"]), $password) == 0
391         && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
392     }
393
394     # ----------------------------------------------------------------------------------------
395     #                                                                                  session
396     #
397
398     function load_session(){
399       session_start();
400       if(!isset($_SESSION["id"])) $this->clear_session();
401       if(
402         $user = (
403           isset($_COOKIE["user"]) || isset($_SESSION["user"]) ?
404             $this->user(isset($_COOKIE["user"]) ? $_COOKIE["user"] : $_SESSION["user"])
405           : array()
406         )
407       ){
408         if(isset($_COOKIE["user"])) $this->set_session($user);
409         if(!$this->user_ok($user)){
410           $this->clear_session();
411           $user = array();
412         }
413       }
414       $this->_user = $user;
415       return $user;
416     }
417
418     function set_session($user){
419       $_SESSION["user"] = $user["login"];
420       $_SESSION["pass"] = md5($user["password"].$_SESSION["id"]);
421       $env = $this->env();
422       return setcookie("user", $user["login"], time() + (60 * 60 * 24 * 7), $env->path("web"));
423     }
424
425     function clear_session(){
426       unset($_SESSION["user"]);
427       unset($_SESSION["pass"]);
428       $_SESSION["ip"] = $_SERVER["REMOTE_ADDR"];
429       $_SESSION["id"] = md5(rand());
430       $env = $this->env();
431       return setcookie("user", "", 0, $env->path("web"));
432     }
433
434     function get_session_user(){
435       return $this->_user;
436     }
437
438     # ----------------------------------------------------------------------------------------
439     #                                                                                  uploads
440     #
441
442     function check_user_uploads_dir($user = null){
443       $env = $this->env();
444       $user_dir = $env->path("content")."uploads/".(isset($user) ? $user : $this->_user["id"]);
445       if(!file_exists($user_dir)) @mkdir($user_dir);
446       return file_exists($user_dir);
447     }
448
449   }
450
451 ?>