maj syntaxe accolades, maj jQuery, correction layout contact
[mtweb] / web / 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         $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       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"])){
286             if(
287                  $v_rst["action"] == $mod
288               || $v_rst["action"] == $mod."/".$controller
289               || $v_rst["action"] == $mod."/".$controller."/".$action
290             ){
291               if(!isset($status[$v_rst["action"]])) $status[$v_rst["action"]] = array();
292               $status[$v_rst["action"]][$v_rst["id_status"]] = true;
293             }
294           }
295           else $status = false;
296         }
297         $sgbd->close_data($rst);
298       }
299       else $status = false;
300       if($status !== false){
301         if($set_status){
302           foreach($set_status as $new_action_status){
303             $id_status = false;
304             foreach($status as $user_status) if($new_user_status["nom"] == $user_status["nom"]){
305               $id_status = $user_status["id"];
306               break;
307             }
308             if($id_status){
309               $SAME = true;
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){
312                   $SAME = false;
313                   break;
314                 }
315               }
316               if(!$SAME){
317                 if($sgbd->set_data("user_status", $id_status, $new_user_status)) $status[$id_status] = $new_user_status;
318                 else{
319                   $status = false;
320                   break;
321                 }
322               }
323             }
324             else{
325               if($id_status = $sgbd->add_data("user_status", $new_user_status)) $status[$id_status] = $new_user_status;
326               else{
327                 $status = false;
328                 break;
329               }
330             }
331           }
332         }
333       }
334       return $status;
335     }
336
337     function creation_default_status(){
338       $sgbd = $this->sgbd();
339       $default_status = 0;
340       if($rst = $sgbd->open_data("user_status")){
341         while($v_rst = $sgbd->fetch_data($rst)){
342           if(isset($v_rst)){
343             if(isset($v_rst["creation_default"]) && $v_rst["creation_default"] == 1){
344               $default_status = $v_rst["id"];
345               break;
346             }
347           }
348           else{
349             $default_status = false;
350             break;
351           }
352         }
353         $sgbd->close_data($rst);
354       }
355       else $default_status = false;
356       return $default_status;
357     }
358
359     # ----------------------------------------------------------------------------------------
360     #                                                                             log in / out
361     #
362
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;
367         }
368         else{
369           $this->clear_session();
370           $user = array();
371         }
372       }
373       return $user;
374     }
375
376     function logout(){
377       return $this->clear_session();
378     }
379
380     function user_ok($user){
381       return
382       strcmp(md5($user["password"].$_SESSION["id"]), $_SESSION["pass"]) == 0
383       && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
384     }
385
386     function password_ok($user, $password){
387       return
388       strcmp(md5($user["password"].$_SESSION["id"]), $password) == 0
389       && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
390     }
391
392     # ----------------------------------------------------------------------------------------
393     #                                                                                  session
394     #
395
396     function load_session(){
397       session_start();
398       if(!isset($_SESSION["id"])) $this->clear_session();
399       if(
400         $user = (
401           isset($_COOKIE["user"]) || isset($_SESSION["user"]) ?
402             $this->user(isset($_COOKIE["user"]) ? $_COOKIE["user"] : $_SESSION["user"])
403           : array()
404         )
405       ){
406         if(isset($_COOKIE["user"])) $this->set_session($user);
407         if(!$this->user_ok($user)){
408           $this->clear_session();
409           $user = array();
410         }
411       }
412       $this->_user = $user;
413       return $user;
414     }
415
416     function set_session($user){
417       $_SESSION["user"] = $user["login"];
418       $_SESSION["pass"] = md5($user["password"].$_SESSION["id"]);
419       $env = $this->env();
420       return setcookie("user", $user["login"], time() + (60 * 60 * 24 * 7), $env->path("web"));
421     }
422
423     function clear_session(){
424       unset($_SESSION["user"]);
425       unset($_SESSION["pass"]);
426       $_SESSION["ip"] = $_SERVER["REMOTE_ADDR"];
427       $_SESSION["id"] = md5(rand());
428       $env = $this->env();
429       return setcookie("user", "", 0, $env->path("web"));
430     }
431
432     function get_session_user(){
433       return $this->_user;
434     }
435
436     # ----------------------------------------------------------------------------------------
437     #                                                                                  uploads
438     #
439
440     function check_user_uploads_dir($user = null){
441       $env = $this->env();
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);
445     }
446
447   }
448
449 ?>