import mtweb.0.4.1
[mtweb] / web / app / data / modules / xml / mw_data_users.php
1 <?php
2
3   class mw_data_users extends mw_data
4   {
5
6     var $users;
7     var $_user;
8     var $user_status;
9     var $action_status;
10
11     # ----------------------------------------------------------------------------------------
12     #                                                                                    users
13     #
14
15     function users($start = 0, $alpha = null, $status = null)
16     { $sgbd = $this->sgbd();
17       $env = $this->env();
18       $users = array("list" => array(), "total" => 0);
19       $res = array();
20       if($rst = $sgbd->open_data("users"))
21       { while($v_rst = $sgbd->fetch_data($rst))
22         { if(isset($v_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;
26                 $users["total"]++;
27               }
28             }
29           }
30           else
31           { $res = false;
32             break;
33           }
34         }
35         $sgbd->close_data($rst);
36         if($res !== false)
37         { $n = 0;
38           foreach($res as $id_user => $user)
39           { $n++;
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;
44             }
45           }
46         }
47         else $users = false;
48       }
49       else $users = false;
50       return $users;
51     }
52
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;
59       }
60       return $user;
61     }
62
63     function user($login)
64     { $sgbd = $this->sgbd();
65       $user = array();
66       if($rst = $sgbd->open_data("users"))
67       { while($v_rst = $sgbd->fetch_data($rst))
68         { if(isset($v_rst))
69           { if(isset($v_rst["login"]) && $v_rst["login"] == $login)
70             { $user = $v_rst;
71               break;
72             }
73           }
74           else $user = false;
75         }
76         $sgbd->close_data($rst);
77       }
78       else $user = false;
79       if($user !== false)
80       { if(!isset($this->users)) $this->users = array();
81         $this->users[$user["id"]] = $user;
82       }
83       return $user;
84     }
85
86     function user_exists($login)
87     { $sgbd = $this->sgbd();
88       $EXISTS = 0;
89       if($rst = $sgbd->open_data("users"))
90       { while($v_rst = $sgbd->fetch_data($rst))
91         { if(isset($v_rst))
92           { if(isset($v_rst["login"]) && $v_rst["login"] == $login)
93             { $EXISTS++;
94             }
95           }
96           else
97           { $EXISTS = false;
98             break;
99           }
100         }
101         $sgbd->close_data($rst);
102       }
103       else $EXISTS = false;
104       return $EXISTS;
105     }
106
107     function add_user($login, $password, $email, $status)
108     { $sgbd = $this->sgbd();
109       return $sgbd->add_data
110       ( "users",
111         array
112         ( "login" => $login,
113           "password" => $password,
114           "email" => $email,
115           "status" => $status
116         )
117       );
118     }
119
120     function set_user($id, $login, $password, $email, $status)
121     { $sgbd = $this->sgbd();
122       return $sgbd->set_data
123       ( "users",
124         $id,
125         array
126         ( "login" => $login,
127           "password" => $password,
128           "email" => $email,
129           "status" => $status
130         )
131       );
132     }
133
134     function del_user($login)
135     { if(($user = $this->user($login)) !== false)
136       { $sgbd = $this->sgbd();
137         return $sgbd->del_data("users", $user["id"]);
138       }
139       return false;
140     }
141
142     # ----------------------------------------------------------------------------------------
143     #                                                                                   status
144     #
145
146     function status()
147     { if(!isset($this->user_status)) return false;
148       return $this->user_status;
149     }
150
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))
156         { if(isset($v_rst))
157           { $this->user_status[$v_rst["id"]] = $v_rst;
158           }
159           else
160           { $this->user_status = false;
161             break;
162           }
163         }
164         $sgbd->close_data($rst);
165       }
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"];
172             break;
173           }
174           if($id_status)
175           { $SAME = true;
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;
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 { $this->user_status = false; break; }
184             }
185           }
186           else
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; }
189           }
190         }
191       }
192       return $this->user_status;
193     }
194
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))
201         { if(isset($v_rst))
202           { $this->action_status[$v_rst["id"]] = $v_rst;
203           }
204           else
205           { $this->action_status = false;
206             break;
207           }
208         }
209         $sgbd->close_data($rst);
210       }
211       else $this->action_status = false;
212       if($status && $this->action_status !== false)
213       { $STATUS_OK = true;
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"])
218             { $FOUND = true;
219               $status[$id_new_action_status]["id_status"] = $user_status["id"];
220             }
221           }
222           if(!$FOUND)
223           { $STATUS_OK = false;
224             break;
225           }
226         }
227         if($STATUS_OK)
228         { foreach($status as $new_action_status)
229           { $id_status = false;
230             foreach($this->action_status as $action_status)
231             { if
232               (    $new_action_status["action"] == $action_status["action"]
233                 && $new_action_status["id_status"] == $action_status["id_status"]
234               )
235               { $id_status = $action_status["id"];
236                 break;
237               }
238             }
239             if($id_status)
240             { $SAME = true;
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;
244                 }
245               }
246               if(!$SAME)
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; }
249               }
250             }
251             else
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; }
254             }
255           }
256         }
257         else $this->action_status = false;
258       }
259       return $this->action_status;
260     }
261
262     function get_user_status()
263     { $user = $this->get_session_user();
264       if($user && isset($user["status"])) return $user["status"];
265       return 0;
266     }
267
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"]))
273           { if
274             (    $v_rst["action"] == $mod
275               || $v_rst["action"] == $mod."/".$controller
276               || $v_rst["action"] == $mod."/".$controller."/".$action
277             )
278             { if(!isset($status[$v_rst["action"]])) $status[$v_rst["action"]] = array();
279               $status[$v_rst["action"]][$v_rst["id_status"]] = true;
280             }
281           }
282           else $status = false;
283         }
284         $sgbd->close_data($rst);
285       }
286       else $status = false;
287       if($status !== false)
288       { if($set_status)
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"];
293               break;
294             }
295             if($id_status)
296             { $SAME = true;
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;
300                 }
301               }
302               if(!$SAME)
303               { if($sgbd->set_data("user_status", $id_status, $new_user_status)) $status[$id_status] = $new_user_status;
304                 else { $status = false; break; }
305               }
306             }
307             else
308             { if($id_status = $sgbd->add_data("user_status", $new_user_status)) $status[$id_status] = $new_user_status;
309               else { $status = false; break; }
310             }
311           }
312         }
313       }
314       return $status;
315     }
316
317     function creation_default_status()
318     { $sgbd = $this->sgbd();
319       $default_status = 0;
320       if($rst = $sgbd->open_data("user_status"))
321       { while($v_rst = $sgbd->fetch_data($rst))
322         { if(isset($v_rst))
323           { if(isset($v_rst["creation_default"]) && $v_rst["creation_default"] == 1)
324             { $default_status = $v_rst["id"];
325               break;
326             }
327           }
328           else
329           { $default_status = false;
330             break;
331           }
332         }
333         $sgbd->close_data($rst);
334       }
335       else $default_status = false;
336       return $default_status;
337     }
338
339     # ----------------------------------------------------------------------------------------
340     #                                                                             log in / out
341     #
342
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;
347         }
348         else
349         { $this->clear_session();
350           $user = array();
351         }
352       }
353       return $user;
354     }
355
356     function logout()
357     { return $this->clear_session();
358     }
359
360     function user_ok($user)
361     { return
362       strcmp(md5($user["password"].$_SESSION["id"]), $_SESSION["pass"]) == 0
363       && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
364     }
365
366     function password_ok($user, $password)
367     { return
368       strcmp(md5($user["password"].$_SESSION["id"]), $password) == 0
369       && $_SESSION["ip"] == $_SERVER["REMOTE_ADDR"];
370     }
371
372     # ----------------------------------------------------------------------------------------
373     #                                                                                  session
374     #
375
376     function load_session()
377     { session_start();
378       if(!isset($_SESSION["id"])) $this->clear_session();
379       if
380       ( $user =
381         ( isset($_COOKIE["user"]) || isset($_SESSION["user"]) ?
382             $this->user(isset($_COOKIE["user"]) ? $_COOKIE["user"] : $_SESSION["user"])
383           : array()
384         )
385       )
386       { if(isset($_COOKIE["user"])) $this->set_session($user);
387         if(!$this->user_ok($user))
388         { $this->clear_session();
389           $user = array();
390         }
391       }
392       $this->_user = $user;
393       return $user;
394     }
395
396     function set_session($user)
397     { $_SESSION["user"] = $user["login"];
398       $_SESSION["pass"] = md5($user["password"].$_SESSION["id"]);
399       $env = $this->env();
400       return setcookie("user", $user["login"], time() + (60 * 60 * 24 * 7), $env->path("web"));
401     }
402
403     function clear_session()
404     { unset($_SESSION["user"]);
405       unset($_SESSION["pass"]);
406       $_SESSION["ip"] = $_SERVER["REMOTE_ADDR"];
407       $_SESSION["id"] = md5(rand());
408       $env = $this->env();
409       return setcookie("user", "", 0, $env->path("web"));
410     }
411
412     function get_session_user() { return $this->_user; }
413
414     # ----------------------------------------------------------------------------------------
415     #                                                                                  uploads
416     #
417
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);
423     }
424
425   }
426
427 ?>