root/alternc/tags/0.9.7/bureau/class/m_admin.php

Revision 1945, 28.7 kB (checked in by azerttyu, 1 year ago)

revert des commit [1944], [1939], [1933], [1925] : nouveau bureau.
On recommencera mais en patchant la branche /franck-desktop

Line 
1 <?php
2 /*
3  $Id: m_admin.php,v 1.16 2006/02/09 20:12:22 benjamin Exp $
4  ----------------------------------------------------------------------
5  AlternC - Web Hosting System
6  Copyright (C) 2006 Le réseau Koumbit Inc.
7  http://koumbit.org/
8  ----------------------------------------------------------------------
9  LICENSE
10
11  This program is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License (GPL)
13  as published by the Free Software Foundation; either version 2
14  of the License, or (at your option) any later version.
15
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU General Public License for more details.
20
21  To read the license please visit http://www.gnu.org/copyleft/gpl.html
22  ----------------------------------------------------------------------
23  Original Author of file: Benjamin Sonntag
24  Purpose of file: Administrate members and rights.
25  ----------------------------------------------------------------------
26 */
27 /* ----------------------------------------------------------------- */
28 /**
29 * Classe de gestion de l'administration du serveur par les super-admin.
30 *
31 * Cette classe permet de créer / modifier / détruire les comptes, ainsi que de
32 * modifier les paramètres du serveur.<br />
33 * Copyleft {@link http://alternc.net/ AlternC Team}
34 *
35 *
36 */
37 class m_admin {
38
39   /* ----------------------------------------------------------------- */
40   /**
41    * $enabled precises if the logged user is super-admin or not
42    */
43   var $enabled=0;
44
45   /* ----------------------------------------------------------------- */
46     /** List of the controls made for each TLD
47     *
48     * $tldmode is used by the administration panel, while choosing
49     * the authorized TLDs. It's an array of strings explaining the current state of the TLD.
50     */
51
52   var $tldmode=array(
53              0 => "This TLD is forbidden",
54              1 => "primary DNS is checked in WHOIS db",
55              2 => "primary & secondary DNS are checked in WHOIS db",
56              3 => "Domain must exist, but don't do any DNS check",
57              4 => "Domain can be installed, no check at all",
58              5 => "Domain can be installed, force NO DNS hosting",
59              );
60
61   /* ----------------------------------------------------------------- */
62   /**
63    * Constructeur
64    */
65   function m_admin() {
66     global $db,$cuid;
67     $db->query("SELECT su FROM membres WHERE uid='$cuid';");
68     $db->next_record();
69     $this->enabled=$db->f("su");
70   }
71
72   /* ----------------------------------------------------------------- */
73   /**
74    * Returns the known information about a hosted account
75    *
76    * Returns all what we know about an account (contents of the tables
77    *  <code>membres</code> et <code>local</code>)
78    * Ckecks if the account is super-admin
79    * @param integer $uid a unique integer identifying the account
80    * @return an associative array containing all the fields of the
81    * table <code>membres</code> and <code>local</code> of the corresponding account.
82    * Returns FALSE if an error occurs.
83    *
84    * Retourne tout ce que l'on sait sur un membre (contenu des tables <code>membres et local</code>)
85    * vérifie que le compte appelant est super-admin
86    * @param integer $uid Numéro de l'utilisateur dont on veut les informations.
87    * @return array Retourne un tableau associatif contenant l'ensemble des champs des tables 'membres'
88    *  et 'local' pour le membre demandé. Retourne FALSE si une erreur s'est produite.
89    *
90    */
91   function get($uid) {
92     global $err,$db;
93     //    $err->log("admin","get",$uid);
94     if (!$this->enabled) {
95       $err->raise("admin",1);
96       return false;
97     }
98     $db->query("SELECT * FROM membres WHERE uid='$uid';");
99     if ($db->num_rows()) {
100       $db->next_record();
101       $c=$db->Record;
102     } else {
103       $err->raise("admin",2);
104       return false;
105     }
106     $db->query("SELECT * FROM local WHERE uid='$uid';");
107     if ($db->num_rows()) {
108       $db->next_record();
109       reset($db->Record);
110       while (list($key,$val)=each($db->Record)) {
111     $c[$key]=$val;
112       }
113     }
114     return $c;
115   }
116
117   /* ----------------------------------------------------------------- */
118   /**
119    * @return TRUE if there's only ONE admin account
120    * Retourne true s'il n'existe qu'un seul compte administrateur
121    */
122   function onesu() {
123     global $db;
124     $db->query("SELECT COUNT(*) AS cnt FROM membres WHERE su=1");
125     $db->next_record();
126     return ($db->f("cnt")==1);
127   }
128
129   /* ----------------------------------------------------------------- */
130   /**
131    * Returns the list of the hosted accounts
132    * Retourne la liste des membres hébergés
133    *
134    * Returns all what we know about ALL the accounts (contents of the tables
135    *  <code>membres</code> et <code>local</code>)
136    * Check for super-admin accounts
137    * @param
138    * @return an associative array containing all the fields of the
139    * table <code>membres</code> and <code>local</code> of all the accounts.
140    * Returns FALSE if an error occurs.
141    *
142    * Retourne tout ce que l'on sait sur LES membres (contenu de membres et local)
143    * vérifie que le compte appelant est super-admin
144    * @return array Retourne un tableau indexé de tableaux associatifs contenant l'ensemble des
145    *  champs des tables 'membres' et 'local' pour les membre. Retourne FALSE si une erreur s'est
146    *  produite.
147    *
148    */
149   function get_list($all=0) {
150     // PATCHBEN pour ne voir que les comptes que l'on a créé (sauf admin)
151     global $err,$mem,$cuid;
152     $err->log("admin","get_list");
153     if (!$this->enabled) {
154       $err->raise("admin",1);
155       return false;
156     }
157     $db=new DB_System();
158     if ($mem->user[uid]==2000 || $all) {
159       $db->query("SELECT uid FROM membres ORDER BY login;");
160     } else {
161       $db->query("SELECT uid FROM membres WHERE creator='".$cuid."' ORDER BY login;");
162     }
163     if ($db->num_rows()) {
164       while ($db->next_record()) {
165     $c[]=$this->get($db->f("uid"));
166       }
167       return $c;
168     } else {
169       return false;
170     }
171   }
172
173   /* ----------------------------------------------------------------- */
174   /**
175    * Check if I am the creator of the member $uid
176    *
177    * @param integer $uid a unique integer identifying the account
178    */
179   function checkcreator($uid) {
180     global $err,$mem,$db,$cuid;
181     // DONE PATCHBEN Check that the current user is editing one of it's own account !
182     // but ADMIN (always uid 2000) is almighty
183     if ($cuid==2000) {
184       return true;
185     }
186     $db->query("SELECT creator FROM membres WHERE uid='$uid';");
187     $db->next_record();
188     if ($db->Record[creator]!=$cuid) {
189       $err->raise("admin",1);
190       return false;
191     }
192     return true;
193   }
194
195   /* ----------------------------------------------------------------- */
196   /**
197    * Creates a new hosted account
198    * 
199    * Creates a new hosted account (in the tables <code>membres</code>
200    * and <code>local</code>). Prevents any manipulation of the account if
201    * the account $mid is not super-admin.
202    *
203    * @param $login string Login name like [a-z][a-z0-9]*
204    * @param $pass string Password (max. 64 characters)
205    * @param $nom string Name of the account owner
206    * @param $prenom string First name of the account owner
207    * @param $mail string Email address of the account owner, useful to get
208    * one's lost password
209    * @pararm $type string Account type for quotas
210    * @return boolean Returns FALSE if an error occurs, TRUE if not.
211    *
212    *
213    * Crée un nouveau membre hébergé
214    * Création d'un nouveau membre (dans membres et local) Refuse l'utilisation de l'objet
215    * si le compte $mid n'est pas super-admin
216    *
217    * @param $login Nom d'utilisateur, de la forme [a-z][a-z0-9]*
218    * @param $pass Mot de passe, maxi 64 caractères
219    * @param $nom Nom de la personne ou structure
220    * @param $prenom Prénom de la personne ou structure
221    * @param $mail Adresse email du propriétaire du compte, permet de récupérer son mot de passe
222    * @param $type Type de compte pour les quotas
223    * @param $duration integer Durée du compte en mois
224    * @return boolean Retourne FALSE si une erreur s'est produite, TRUE sinon.
225    *
226    */
227   function add_mem($login, $pass, $nom, $prenom, $mail, $canpass=1, $type='default', $duration=0) {
228     global $err,$quota,$classes,$cuid,$mem,$L_MYSQL_DATABASE,$L_MYSQL_LOGIN;
229     $err->log("admin","add_mem",$login."/".$mail);
230     if (!$this->enabled) {
231       $err->raise("admin",1);
232       return false;
233     }
234     if (($login=="")||($pass=="")||($mail=="")){
235       $err->raise("admin",6);
236       return false;
237     }
238     if (checkmail($mail)!=0){
239       $err->raise("admin",5);
240       return false;
241     }
242     // Vérification de la conformité du login
243     $login=strtolower($login);
244     if (!ereg("^[a-z0-9-]*$",$login)) { //$
245       $err->raise("admin",10);
246       return false;
247     }
248     if (strlen($login) > 16) {
249       $err->raise("admin",13);
250       return false;
251     }
252     // Il ne peut pas être égal au login ou au nom de base systeme !
253     if ($login==$L_MYSQL_DATABASE || $login==$L_MYSQL_LOGIN || $login=="mysql" || $login=="root") {
254       $err->raise("admin",10);
255       return false;
256     }
257     //$pass=stripslashes($pass);
258     $pass=_md5cr($pass);
259     $db=new DB_System();
260     // vérification de l'inexistence du membre dans system.membres
261     $db->query("SELECT count(*) AS cnt FROM membres WHERE login='$login';");
262     $db->next_record();
263     if (!$db->f("cnt")) {
264       $db->query("SELECT m.uid+1 as nextid FROM membres m LEFT JOIN membres n ON m.uid=n.uid-1 WHERE n.uid IS NULL ORDER BY 1 LIMIT 0,1");
265       if (!$db->next_record()) {
266     $uid=2000;
267       } else {
268     $uid=$db->Record["nextid"];
269     if ($uid<=2000) $uid=2000;
270       }
271       // on le créé ensuite dans system.membres et system.local
272       $db->query("INSERT INTO membres (uid,login,pass,mail,creator,canpass,type,created) VALUES ('$uid','$login','$pass','$mail','$cuid','$canpass', '$type', NOW());");
273       $db->query("INSERT INTO local(uid,nom,prenom) VALUES('$uid','$nom','$prenom');");
274       $this->renew_update($uid, $duration);
275       exec("/usr/lib/alternc/mem_add ".$login." ".$uid);
276       // Declenchons les autres classes.
277       $mem->su($uid);
278       foreach($classes as $c) {
279     if (method_exists($GLOBALS[$c],"alternc_add_member")) {
280       $GLOBALS[$c]->alternc_add_member();
281     }
282       }
283       $mem->unsu();
284       return $uid;
285     } else {
286       $err->raise("admin",3);
287       return false;
288     }
289   }
290
291   /* ----------------------------------------------------------------- */
292   /**
293    * Modifies an account
294    * 
295    * Modifies an account (in the tables <code>membres</code>
296    * and <code>local</code>). Prevents any manipulation of the account if
297    * the account $mid is not super-admin.
298    *
299    * @param $uid integer the uid number of the account we want to modify
300    * @param login string new login name like [a-z][a-z0-9]*
301    * @param $pass string new password (max. 64 characters)
302    * @param $nom string new name of the account owner
303    * @param $prenom string new first name of the account owner
304    * @param $mail string new email address of the account owner
305    * @param $enabled integer (value: 0 or 1) activates or desactivates the
306    * @param $type string new type of account
307    * access to the virtual desktop of this account.
308    * @return boolean Returns FALSE if an error occurs, TRUE if not.
309    *
310    * Modifie un membre hébergé
311    *
312    * modifie les données d'un membre. Refuse l'utilisation de l'objet
313    * si le compte $mid n'est pas super-admin
314    *
315    * @param $uid integer Numéro uid de l'utilisateur que l'on souhaite modifier.
316    * @param $mail string Nouvelle adresse email
317    * @param $nom $prenom string Nouveaux nom et prénom de l'utilisateur
318    * @param $pass string Nouveau mot de passe.
319    * @param $enabled integer vaut 0 ou 1, active ou désactive l'accès au bureau virtuel de ce compte.
320    * @param $type string Nouveau type de compte
321    * @param $duration integer Durée du compte en mois
322    * @return boolean Retourne FALSE si une erreur s'est produite, TRUE sinon.
323    *
324    */
325   function update_mem($uid, $mail, $nom, $prenom, $pass, $enabled, $canpass, $type='default', $duration=0) {
326     global $err,$db;
327     global $cuid, $quota;
328
329     $err->log("admin","update_mem",$uid);
330     if (!$this->enabled) {
331       $err->raise("admin",1);
332       return false;
333     }
334     $db=new DB_System();
335     if ($pass) {
336       // on modifie aussi le password :
337       $pass=_md5cr($pass);
338       $ssq=" ,pass='$pass' ";
339     } else {
340       $ssq="";
341     }
342     if (($db->query("UPDATE local SET nom='$nom', prenom='$prenom' WHERE uid='$uid';"))
343     &&($db->query("UPDATE membres SET mail='$mail', canpass='$canpass', enabled='$enabled', type='$type' $ssq WHERE uid='$uid';"))){
344       if($_POST['reset_quotas'] == "on")
345     $quota->addquotas();
346       $this->renew_update($uid, $duration);
347       return true;
348     }
349     else {
350       $err->raise("admin",2);
351       return false;
352     }
353   }
354
355   /* ----------------------------------------------------------------- */
356   /**
357    * Lock an account
358    *
359    * Lock an account and prevent the user to access its account.
360    *
361    * @param $uid integer the uid number of the account we want to lock
362    * @return boolean Returns FALSE if an error occurs, TRUE if not.
363    */
364   function lock_mem($uid) {
365     global $err,$db;
366     $err->log("admin","lock_mem",$uid);
367     if (!$this->enabled) {
368       $err->raise("admin",1);
369       return false;
370     }
371     $db=new DB_System();
372     if ($db->query("UPDATE membres SET enabled='0' WHERE uid='$uid';")) {
373       return true;
374     }
375     else {
376       $err->raise("admin",2);
377       return false;
378     }
379   }
380
381
382   /* ----------------------------------------------------------------- */
383   /**
384    * UnLock an account
385    *
386    * UnLock an account and prevent the user to access its account.
387    *
388    * @param $uid integer the uid number of the account we want to unlock
389    * @return boolean Returns FALSE if an error occurs, TRUE if not.
390    */
391   function unlock_mem($uid) {
392     global $err,$db;
393     $err->log("admin","unlock_mem",$uid);
394     if (!$this->enabled) {
395       $err->raise("admin",1);
396       return false;
397     }
398     $db=new DB_System();
399     if ($db->query("UPDATE membres SET enabled='1' WHERE uid='$uid';")) {
400       return true;
401     }
402     else {
403       $err->raise("admin",2);
404       return false;
405     }
406   }
407
408
409
410   /* ----------------------------------------------------------------- */
411   /**
412    * Deletes an account
413    *
414    * Deletes the specified account. Prevents any manipulation of the account if
415    * the account $mid is not super-admin.
416    *
417    * @param $uid integer the uid number of the account we want to delete
418    * @return boolean Returns FALSE if an error occurs, TRUE if not.
419    *
420    *
421    * Efface un membre hébergé
422    *
423    * Supprime le membre spécifié. Refuse l'utilisation de l'objet si le compte $mid n'est pas super-admin
424    * @param $uid Numéro du membre à supprimer.
425    * @return Retourne FALSE si une erreur s'est produite, TRUE sinon.
426    *
427    */
428   function del_mem($uid) {
429     global $err,$quota,$classes,$cuid,$mem,$dom;
430     $err->log("admin","del_mem",$uid);
431
432     if (!$this->enabled) {
433       $err->raise("admin",1);
434       return false;
435     }
436     $db=new DB_System();
437     $tt=$this->get($uid);
438     
439     // On devient l'utilisateur :
440     $mem->su($uid);
441
442     // WE MUST call m_dom before all others because of conflicts ...
443     $dom->alternc_del_member();
444
445       // Send the event to the other classes :
446       foreach($classes as $c) {
447     if (method_exists($GLOBALS[$c],"alternc_del_member")) {
448       $GLOBALS[$c]->alternc_del_member();
449     }
450       }
451       if (($db->query("DELETE FROM membres WHERE uid='$uid';")) &&
452       ($db->query("DELETE FROM local WHERE uid='$uid';"))) {
453     exec("/usr/lib/alternc/mem_del ".$tt["login"]);
454     $mem->unsu();
455     return true;
456       } else {
457     $err->raise("admin",2);
458     $mem->unsu();
459     return false;
460       }
461   }
462
463   /* ----------------------------------------------------------------- */
464   /**
465    * Renew an account
466    *
467    * Renew an account for its duration
468    *
469    * @param $uid integer the uid number of the account we want to renew
470    * @param $periods integer the number of periods we renew for
471    * @return boolean Returns FALSE if an error occurs, TRUE if not.
472    */
473   function renew_mem($uid, $periods=1) {
474     global $err,$db;
475
476     $periods = intval($periods);
477     if($periods == 0)
478       return false;
479
480     $query =
481       "UPDATE membres SET renewed = renewed + INTERVAL (duration * $periods) MONTH WHERE uid=${uid};";
482
483     if ($db->query($query)) {
484       return true;
485     } else {
486       $err->raise("admin",2);
487       return false;
488     }
489   }
490
491   /* ----------------------------------------------------------------- */
492   /**
493    * Update the duration information for an account
494    *
495    * @param $uid integer the uid number of the account we want to update
496    * @param $duration integer the new duration, in months, of the account
497    * @return boolean Returns FALSE if an error occurs, TRUE if not.
498    */
499   function renew_update($uid, $duration) {
500     global $err,$db;
501
502     if($duration == 0) {
503       if($db->query("UPDATE membres SET duration = NULL, renewed = NULL WHERE uid=$uid;"))
504     return true;
505     } else {
506       if($db->query("UPDATE membres SET duration = $duration WHERE uid=$uid") &&
507      $db->query("UPDATE membres SET renewed = NOW() WHERE uid=$uid and renewed is null;"))
508     return true;
509     }
510
511     $err->raise("admin",2);
512     return false;
513   }
514
515   /* ----------------------------------------------------------------- */
516   /**
517    *
518    * Get the expiry date for an account
519    *
520    * @param $uid integer The uid number of the account
521    * @return string The expiry date, a string as printed by MySQL
522    */
523   function renew_get_expiry($uid) {
524     global $db;
525
526     $db->query("SELECT renewed + INTERVAL duration MONTH 'expiry' FROM membres WHERE uid='$uid' ;");
527     if ($db->num_rows()) {
528       $db->next_record();
529       return $db->Record['expiry'];
530     }
531
532     return '';
533   }
534
535   /* ----------------------------------------------------------------- */
536   /**
537    *
538    * Get the expiry status for an account
539    *
540    * @param $uid integer The uid number of the account
541    * @return integer The expiry status:
542    *  0: account does not expire
543    *  1: expires in more than duration,
544    *  2: expires within the duration
545    *  3: has expired past the duration
546    */
547   function renew_get_status($uid) {
548     global $db;
549
550     $db->query(
551       "SELECT CASE" .
552       " WHEN duration IS NULL THEN 0" .
553       " WHEN renewed + INTERVAL duration MONTH <= NOW() THEN 3" .
554       " WHEN renewed <= NOW() THEN 2" .
555       " ELSE 1 END 'status' FROM membres where uid=$uid;");
556
557     if($db->num_rows()) {
558       $db->next_record();
559       return $db->Record['status'];
560     }
561
562     return 0;
563   }
564
565   /* ----------------------------------------------------------------- */
566   /**
567    *
568    * Get the expired/about to expire accounts.
569    *
570    * @return resource The recordset of the corresponding accounts
571    */
572   function renew_get_expiring_accounts() {
573     global $db;
574
575     if(!$db->query("SELECT *, m.renewed + INTERVAL duration MONTH 'expiry'," .
576            " CASE WHEN m.duration IS NULL THEN 0" .
577            " WHEN m.renewed + INTERVAL m.duration MONTH <= NOW() THEN 3" .
578            " WHEN m.renewed <= NOW() THEN 2" .
579            " ELSE 1 END 'status' FROM membres m, local l" .
580            " WHERE m.uid = l.uid" .
581            " HAVING status=2 or status=3 ORDER BY status DESC, expiry;"))
582       return false;
583     else {
584       while($db->next_record())
585     $res[] = $db->Record;
586       return $res;
587     }
588   }
589
590  
591
592   /* ----------------------------------------------------------------- */
593   /**
594    * Turns a common account into a super-admin account
595    *
596    * @param $uid integer the uid number of the common account we want to turn into a
597    *  super-admin account.
598    * @return Returns FALSE if an error occurs, TRUE if not.
599    *
600    *
601    * Transforme un membre Normal en membre Administrateur
602    *
603    * @param $uid Numéro du compte à transformer
604    * @return Retourne FALSE si une erreur s'est produite.
605    *
606    */
607   function normal2su($uid) {
608     global $err,$db;
609     $db->query("SELECT su FROM membres WHERE uid='$uid';");
610     if (!$db->next_record()) {
611       $err->raise