source: bureau/class/m_dom.php @ 674

Revision 674, 30.9 KB checked in by anarcat, 7 years ago (diff)

[project @ alternc: changeset 2005-05-24 15:35:43 by arnaud-lb]
corection d'une petite erreur

Original author: arnaud-lb
Date: 2005-05-24 15:35:43

Line 
1<?php
2/*
3 $Id: m_dom.php,v 1.23 2005/05/24 15:35:43 arnaud-lb Exp $
4 ----------------------------------------------------------------------
5 LICENSE
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License (GPL)
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 To read the license please visit http://www.gnu.org/copyleft/gpl.html
18 ----------------------------------------------------------------------
19 Original Author of file: Benjamin Sonntag
20 Purpose of file: PHP Class that manage domain names installed on the server
21 ----------------------------------------------------------------------
22*/
23
24define('SLAVE_FLAG', "/var/run/alternc/refresh_slave");
25
26/**
27* Classe de gestion des domaines de l'hébergé.
28*
29* Cette classe permet de gérer les domaines / sous-domaines, redirections
30* dns et mx des domaines d'un membre hébergé.<br />
31* Copyleft {@link http://alternc.net/ AlternC Team}
32*
33* @copyright    AlternC-Team 2002-11-01 http://alternc.net/
34*
35*/
36class m_dom {
37
38  /** $domains : Cache des domaines du membre
39   * @access private
40   */
41  var $domains;
42
43  /** $dns : Liste des dns trouvés par la fonction whois
44   * @access private
45   */
46  var $dns;
47
48  /** Flag : a-t-on trouvé un sous-domaine Webmail pour ce domaine ?
49   * @access private
50   */
51  var $webmail;
52
53  /**
54   * Système de verrouillage du cron
55   * Ce fichier permet de verrouiller le cron en attendant la validation
56   * du domaine par do_domaines.sh
57   * @access private
58   */
59  var $fic_lock_cron="/var/run/alternc/cron.lock";
60
61  /**
62   * Le cron a-t-il été bloqué ?
63   * Il faut appeler les fonctions privées lock et unlock entre les
64   * appels aux domaines.
65   * @access private
66   */
67  var $islocked=false;
68
69  var $type_local = "0";
70  var $type_url = "1";
71  var $type_ip = "2";
72  var $type_webmail = "3";
73
74  var $action_insert = "0";
75  var $action_update= "1";
76  var $action_delete = "2";
77
78  /* ----------------------------------------------------------------- */
79  /**
80   * Constructeur
81   */
82  function m_dom() {
83  }
84
85  /* ----------------------------------------------------------------- */
86  /**
87   * Quota name
88   */
89  function alternc_quota_names() {
90    return "dom";
91  }
92
93  /* ----------------------------------------------------------------- */
94  /**
95   * Retourne un tableau contenant les domaines d'un membre.
96   *
97   * @return array retourne un tableau indexé contenant la liste des
98   *  domaines hébergés sur le compte courant. Retourne FALSE si une
99   *  erreur s'est produite.
100   */
101  function enum_domains() {
102    global $db,$err,$cuid;
103    $err->log("dom","enum_domains");
104    $db->query("select * from domaines where compte='$cuid';");
105    $this->domains=array();
106    if ($db->num_rows()>0) {
107      while ($db->next_record()) {
108        $this->domains[]=$db->f("domaine");
109      }
110    }
111    return $this->domains;
112  }
113
114  /* ----------------------------------------------------------------- */
115  /**
116   *  Efface un domaine du membre courant, et tous ses sous-domaines
117   *
118   * Cette fonction efface un domaine et tous ses sous-domaines, ainsi que
119   * les autres services attachés à celui-ci. Elle appelle donc les autres
120   * classe. Chaque classe peut déclarer une fonction del_dom qui sera
121   * appellée lors de la destruction d'un domaine.
122   *
123   * @param string $dom nom de domaine à effacer
124   * @return boolean Retourne FALSE si une erreur s'est produite, TRUE sinon.
125   */
126  function del_domain($dom) {
127    global $db,$err,$classes,$cuid;
128    $err->log("dom","del_domain",$dom);
129    $dom=strtolower($dom);
130    $db->query("SELECT * FROM domaines WHERE domaine='$dom';");
131    if ($db->num_rows()==0) {
132      $err->raise("dom",1,$dom);
133      return false;
134    }
135    $db->next_record();
136    if ($db->f("compte")!=$cuid) {
137      $err->raise("dom",2,$dom);
138      return false;
139    }
140    $db->query("INSERT INTO domaines_standby (compte,domaine,mx,gesdns,gesmx,action) SELECT compte,domaine,mx,gesdns,gesmx,2 FROM domaines WHERE domaine='$dom'"); // DELETE
141    $db->query("DELETE FROM domaines WHERE domaine='$dom';");
142    $db->query("DELETE FROM sub_domaines WHERE domaine='$dom';");
143
144    // DEPENDANCE :
145    // Lancement de del_dom sur les classes domain_sensitive :
146    // Declenchons les autres classes.
147    for($i=0;$i<count($classes);$i++) {
148      if (method_exists($GLOBALS[$classes[$i]],"alternc_del_domain")) {
149        $GLOBALS[$classes[$i]]->alternc_del_domain($dom);
150      }
151    }
152    for($i=0;$i<count($classes);$i++) {
153      if (method_exists($GLOBALS[$classes[$i]],"alternc_del_mx_domain")) {
154        $GLOBALS[$classes[$i]]->alternc_del_mx_domain($dom);
155      }
156    }
157    return true;
158  }
159
160  /* ----------------------------------------------------------------- */
161  /**
162   *  Installe un domaine sur le compte courant.
163   *
164   * <p>Si le domaine existe déjà ou est interdit, ou est celui du serveur,
165   * l'installation est refusée. Si l'hébergement DNS est demandé, la fonction
166   * checkhostallow vérifiera que le domaine peut être installé conformément
167   * aux demandes des super-admin.
168   * Si le dns n'est pas demandé, le domaine peut être installé s'il est en
169   * seconde main d'un tld (exemple : test.eu.org ou test.com, mais pas
170   * toto.test.org ou test.test.asso.fr)</p>
171   * <p>Chaque classe peut définir une fonction add_dom($dom) qui sera
172   * appellée lors de l'installation d'un nouveau domaine.</p>
173   *
174   * @param string $dom nom fqdn du domaine à installer
175   * @param integer $dns 1 ou 0 pour héberger le DNS du domaine ou pas.
176   * @param integer $noerase 1 ou 0 pour rendre le domaine inamovible ou non
177   * @param integer $force 1 ou 0, si 1, n'effectue pas les tests de DNS.
178   *  force ne devrait être utilisé que par le super-admin.
179   $ @return boolean Retourne FALSE si une erreur s'est produite, TRUE sinon.
180  */
181  function add_domain($domain,$dns,$noerase=0,$force=0) {
182    global $db,$err,$quota,$classes,$L_MX,$L_FQDN,$tld,$cuid;
183    $err->log("dom","add_domain",$domain);
184    $mx="1";
185    // Locked ?
186    if (!$this->islocked) {
187      $err->raise("dom",3);
188      return false;
189    }
190    // Verifie que le domaine est rfc-compliant
191    $domain=strtolower($domain);
192    $t=checkfqdn($domain);
193    if ($t) {
194      $err->raise("dom",3+$t);
195      return false;
196    }
197    // Interdit les domaines clés (table forbidden_domains) sauf en cas FORCE
198    $db->query("select domain from forbidden_domains where domain='$domain'");
199    if ($db->num_rows() && !$force) {
200      $err->raise("dom",22);
201      return false;
202    }
203    if ($domain==$L_FQDN || $domain=="www.$L_FQDN") {
204      $err->raise("dom",18);
205      return false;
206    }
207    $db->query("SELECT compte FROM domaines WHERE domaine='$domain';");
208    if ($db->num_rows()) {
209      $err->raise("dom",8);
210      return false;
211    }
212    $db->query("select compte from domaines_standby where domaine='$domain';");
213    if ($db->num_rows()!=0) {
214      $err->raise("dom",9);
215      return false;
216    }
217    $this->dns=$this->whois($domain);
218    if (!$force) {
219      $v=checkhostallow($domain,$this->dns);
220      if ($v==-1) {
221        $err->raise("dom",7);   // TLD interdit
222        return false;
223      }
224      if ($dns && $v==-2) {
225        $err->raise("dom",12);  // Domaine non trouvé dans le whois
226        return false;
227      }
228      if ($dns && $v==-3) {
229        $err->raise("dom",23);  // Domaine non trouvé dans le whois
230        return false;
231      }
232
233      if ($dns) $dns="1"; else $dns="0";
234
235      // mode 5 : force DNS to NO.
236      if ($tld[$v]==5) $dns=0;
237      // It must be a real domain (no subdomain)
238      if (!$dns) {
239        $v=checkhostallow_nodns($domain);
240        if ($v) {
241          $err->raise("dom",22);
242          return false;
243        }
244      }
245    }
246    // Check the quota :
247    if (!$quota->cancreate("dom")) {
248      $err->raise("dom",10);
249      return false;
250    }
251    if ($noerase) $noerase="1"; else $noerase="0";
252    $db->query("insert into domaines (compte,domaine,mx,gesdns,gesmx,noerase) values ('$cuid','$domain','$L_MX','$dns','$mx','$noerase');");
253    $db->query("insert into domaines_standby (compte,domaine,mx,gesdns,gesmx,action) values ('$cuid','$domain','$L_MX','$dns','$mx',0);"); // INSERT
254    // Creation des 3 sous-domaines par défaut : Vide, www et mail
255    $db->query("insert into sub_domaines (compte,domaine,sub,valeur,type) values ('$cuid','$domain','','/',0);");
256    $db->query("insert into sub_domaines (compte,domaine,sub,valeur,type) values ('$cuid','$domain','www','/',0);");
257    $db->query("insert into sub_domaines (compte,domaine,sub,valeur,type) values ('$cuid','$domain','mail','',3);");
258    // DEPENDANCE :
259    // Lancement de add_dom sur les classes domain_sensitive :
260     // Declenchons les autres classes.   
261    for($i=0;$i<count($classes);$i++) {
262      if (method_exists($GLOBALS[$classes[$i]],"alternc_add_domain")) {
263        $GLOBALS[$classes[$i]]->alternc_add_domain($domain);
264      }
265    }
266    for($i=0;$i<count($classes);$i++) {
267      if (method_exists($GLOBALS[$classes[$i]],"alternc_add_mx_domain")) {
268        $GLOBALS[$classes[$i]]->alternc_add_mx_domain($domain);
269      }
270    }
271   return true;
272  }
273
274  /* ----------------------------------------------------------------- */
275  /**
276   * Retourne les entrées DNS du domaine $domain issues du WHOIS.
277   *
278   * Cette fonction effectue un appel WHOIS($domain) sur Internet,
279   * et extrait du whois les serveurs DNS du domaine demandé. En fonction
280   * du TLD, on sait (ou pas) faire le whois correspondant.
281   * Actuellement, les tld suivants sont supportés :
282   * .com .net .org .be .info .ca .cx .fr .biz .name
283   *
284   * @param string $domain Domaine fqdn dont on souhaite les serveurs DNS
285   * @return array Retourne un tableau indexé avec les NOMS fqdn des dns
286   *   du domaine demandé. Retourne FALSE si une erreur s'est produite.
287   *
288   */
289  function whois($domain) {
290    global $db,$err;
291    $err->log("dom","whois",$domain);
292    // pour ajouter un nouveau TLD, utiliser le code ci-dessous.
293    //  echo "whois : $domain<br />";
294    ereg(".*\.([^\.]*)",$domain,$out);
295    $ext=$out[1];
296    // pour ajouter un nouveau TLD, utiliser le code ci-dessous.
297    //  echo "ext: $ext<br />";
298    $egal="";
299    switch($ext) {
300    case "com":
301    case "net":
302      $serveur="rs.internic.net";
303      $egal="=";
304      break;
305    case "org":
306      $serveur="whois.pir.org";
307      break;
308    case "be":
309      $serveur="whois.dns.be";
310      break;
311    case "info":
312      $serveur="whois.afilias.net";
313      break;
314    case "ca":
315      $serveur="whois.cira.ca";
316      break;
317    case "cx":
318      $serveur="whois.nic.cx";
319      break;
320    case "it":
321      $serveur="whois.nic.it";
322      break;
323    case "fr":
324      $serveur="whois.nic.fr";
325      break;
326    case "biz":
327      $serveur="whois.nic.biz";
328      break;
329    case "name":
330      $serveur="whois.nic.name";
331      break;
332    case "ws":
333      $serveur="whois.samoanic.ws";
334      break;
335    default:
336      $err->raise("dom",7);
337      return false;
338      break;
339    }
340    // pour ajouter un nouveau TLD, utiliser le code ci-dessous.
341    //  echo "serveur : $serveur <br />";
342    if (($fp=fsockopen($serveur, 43))>0) {
343      fputs($fp, "$egal$domain\r\n");
344      $found = false;
345      $state=0;
346      while (!feof($fp)) {
347        $ligne = fgets($fp,128);
348        // pour ajouter un nouveau TLD, utiliser le code ci-dessous.
349        //      echo "| $ligne<br />";
350        switch($ext) {
351        case "org":
352        case "com":
353        case "net":
354        case "info":
355        case "biz":
356        case "name":
357          if (ereg("Name Server:", $ligne)) {
358            $found = true;
359            $tmp=strtolower(ereg_replace(chr(10), "",ereg_replace(chr(13),"",ereg_replace(" ","", ereg_replace("Name Server:","", $ligne)))));
360            if ($tmp)
361              $server[]=$tmp;
362          }
363          break;
364        case "cx":
365          $ligne = ereg_replace(chr(10), "",ereg_replace(chr(13),"",ereg_replace(" ","", $ligne)));
366          if ($ligne=="" && $state==1)
367            $state=2;
368          if ($state==1)
369            $server[]=strtolower($ligne);
370          if ($ligne=="Nameservers:" && $state==0) {
371            $state=1;
372            $found = true;
373          }
374          break;
375        case "ca":
376          $ligne=ereg_replace(chr(10), "",ereg_replace(chr(13),"",ereg_replace(" ","", $ligne)));
377          if ($ligne=="Status:EXIST")
378            $found=true;
379          if (ereg("NS.-Hostname:", $ligne)) {
380            $tmp=strtolower(ereg_replace("NS.-Hostname:","", $ligne));
381            if ($tmp)
382              $server[]=$tmp;
383          }
384          break;
385        case "be":
386          $ligne=ereg_replace(chr(10), "",ereg_replace(chr(13),"",ereg_replace(" ","", $ligne)));
387          if ($ligne=="NameServers:") {
388            $state=1;
389            $found=true;
390          }
391          if ($state==1 && substr($ligne,0,5)=="Name:") {
392            $tmp=trim(strtolower(ereg_replace("Name:","", $ligne)));
393            if ($tmp)
394              $server[]=$tmp;
395          }
396          break;
397        case "it":
398          if (ereg("nserver:", $ligne)) {
399            $found=true;
400            $tmp=strtolower(preg_replace("/nserver:\s*[^ ]*\s*([^\s]*)$/","\\1", $ligne));
401            if ($tmp)
402              $server[]=$tmp;
403          }
404          break;
405        case "fr":
406          if (ereg("nserver:", $ligne)) {
407            $found=true;
408            $tmp=strtolower(preg_replace("/nserver:\s*([^\s]*)\s*.*$/","\\1", $ligne));
409            if ($tmp)
410              $server[]=$tmp;
411          }
412          break;
413        case "ws";
414/* e.g.
415Welcome to the .WS Whois Server
416
417Use of this service for any purpose other
418than determining the availability of a domain
419in the .WS TLD to be registered is strictly
420prohibited.
421
422  Domain Name: DRONE.WS
423
424  Registrant: Registered through Go Daddy Software, Inc. (GoDaddy.com
425
426  Domain created on 2005-01-11 08:56:25
427  Domain last updated on 2005-01-11 08:56:25
428
429  Name servers:
430
431    ns2.koumbit.net
432    ns1.koumbit.net
433*/
434
435/*
436failure:
437
438Welcome to the .WS Whois Server
439
440Use of this service for any purpose other
441than determining the availability of a domain
442in the .WS TLD to be registered is strictly
443prohibited.
444
445No match for "dronefdasfsa.ws".
446
447*/
448          if (ereg('^[[:space:]]*Name servers:[[:space:]]*$', $ligne)) {
449                // found the server
450                $state = 1;
451          } elseif ($state) {
452                if ($ligne = ereg_replace('[[:space:]]', "", $ligne)) {
453                  // first non-whitespace line is considered to be the nameservers themselves
454                  $found = true;
455                  $server[] = $ligne;
456                }
457          }
458          break;
459        } // switch
460      } // while
461      fclose($fp);
462    } else {
463      $err->raise("dom",11);
464      return false;
465    }
466
467    if ($found) {
468      return $server;
469    } else {
470      $err->raise("dom",12);
471      return false;
472    }
473  } // whois
474
475  /* ----------------------------------------------------------------- */
476  /**
477   *  retourne TOUTES les infos d'un domaine
478   *
479   * <b>Note</b> : si le domaine est en attente (présent dans
480   *  domaines_standby), une erreur est retournée
481   *
482   * @param string $dom Domaine dont on souhaite les informations
483   * @return array Retourne toutes les infos du domaine sous la forme d'un
484   * tableau associatif comme suit :<br /><pre>
485   *  $r["name"] =  Nom fqdn
486   *  $r["dns"]  =  Gestion du dns ou pas ?
487   *  $r["mx"]   =  Valeur du champs MX si "dns"=true
488   *  $r["mail"] =  Heberge-t-on le mail ou pas ? (si "dns"=false)
489   *  $r["nsub"] =  Nombre de sous-domaines
490   *  $r["sub"]  =  tableau associatif des sous-domaines
491   *  $r["sub"][0-(nsub-1)]["name"] = nom du sous-domaine (NON-complet)
492   *  $r["sub"][0-(nsub-1)]["dest"] = Destination (url, ip, local ...)
493   *  $r["sub"][0-(nsub-1)]["type"] = Type (0-n) de la redirection.
494   *  </pre>
495   *  Retourne FALSE si une erreur s'est produite.
496   *
497   */
498  function get_domain_all($dom) {
499    global $db,$err,$cuid;
500    $err->log("dom","get_domain_all",$dom);
501    // Locked ?
502    if (!$this->islocked) {
503      $err->raise("dom",3);
504      return false;
505    }
506    $t=checkfqdn($dom);
507    if ($t) {
508      $err->raise("dom",3+$t);
509      return false;
510    }
511    $r["name"]=$dom;
512    $db->query("select * from domaines_standby where compte='$cuid' and domaine='$dom'");
513    if ($db->num_rows()>0) {
514      $err->raise("dom",13);
515      return false;
516    }
517    $db->query("select * from domaines where compte='$cuid' and domaine='$dom'");
518    if ($db->num_rows()==0) {
519      $err->raise("dom",1,$dom);
520      return false;
521    }
522    $db->next_record();
523    $r["dns"]=$db->Record["gesdns"];
524    $r["mail"]=$db->Record["gesmx"];
525    $r["mx"]=$db->Record["mx"];
526    $r[noerase]=$db->Record[noerase];
527    $db->free();
528    $db->query("select count(*) as cnt from sub_domaines where compte='$cuid' and domaine='$dom'");
529    $db->next_record();
530    $r["nsub"]=$db->Record["cnt"];
531    $db->free();
532    $db->query("select * from sub_domaines where compte='$cuid' and domaine='$dom'");
533    // Pas de webmail, on le cochera si on le trouve.
534    $this->webmail=0;
535    for($i=0;$i<$r["nsub"];$i++) {
536      $db->next_record();
537      $r["sub"][$i]=array();
538      $r["sub"][$i]["name"]=$db->Record["sub"];
539      $r["sub"][$i]["dest"]=$db->Record["valeur"];
540      $r["sub"][$i]["type"]=$db->Record["type"];
541      if ($db->Record["type"]==3) { // Webmail
542        $this->webmail=1;
543        $r["sub"][$i]["dest"]=_("Webmail access");
544      }
545    }
546    $db->free();
547    return $r;
548  } // get_domain_all
549
550  /* ----------------------------------------------------------------- */
551  /**
552   * Retourne TOUTES les infos d'un sous domaine du compte courant.
553   *
554   * @param string $dom Domaine fqdn concerné
555   * @param string $sub Sous-domaine dont on souhaite les informations
556   * @return arrray Retourne un tableau associatif contenant les
557   *  informations du sous-domaine demandé.<pre>
558   *  $r["name"]= nom du sous-domaine (NON-complet)
559   *  $r["dest"]= Destination (url, ip, local ...)
560   *  </pre>
561   *  $r["type"]= Type (0-n) de la redirection.
562   *  Retourne FALSE si une erreur s'est produite.
563   */
564  function get_sub_domain_all($dom,$sub) {
565    global $db,$err,$cuid;
566    $err->log("dom","get_sub_domain_all",$dom."/".$sub);
567    // Locked ?
568    if (!$this->islocked) {
569      $err->raise("dom",3);
570      return false;
571    }
572    $t=checkfqdn($dom);
573    if ($t) {
574      $err->raise("dom",3+$t);
575      return false;
576    }
577    $db->query("select * from sub_domaines where compte='$cuid' and domaine='$dom' and sub='$sub'");
578    if ($db->num_rows()==0) {
579      $err->raise("dom",14);
580      return false;
581    }
582    $db->next_record();
583    $r=array();
584    $r["name"]=$db->Record["sub"];
585    $r["dest"]=$db->Record["valeur"];
586    $r["type"]=$db->Record["type"];
587    $db->free();
588    return $r;
589  } // get_sub_domain_all
590
591  /* ----------------------------------------------------------------- */
592  /**
593   * Modifier les information du sous-domaine demandé.
594   *
595   * <b>Note</b> : si le sous-domaine $sub.$dom n'existe pas, il est créé.<br />
596   * <b>Note : TODO</b> : vérification de concordance de $dest<br />
597   *
598   * @param string $dom Domaine dont on souhaite modifier/ajouter un sous domaine
599   * @param string $subk Sous domaine à modifier / créer
600   * @param integer $type Type de sous-domaine (local, ip, url ...)
601   * @param string $action Action : vaut "add" ou "edit" selon que l'on
602   *  Crée (add) ou Modifie (edit) le sous-domaine
603   * @param string $dest Destination du sous-domaine, dépend de la valeur
604   *  de $type (url, ip, dossier...)
605   * @return boolean Retourne FALSE si une erreur s'est produite, TRUE sinon.
606   */
607  function set_sub_domain($dom,$sub,$type,$action,$dest) {
608    global $db,$err,$cuid;
609    $err->log("dom","set_sub_domain",$dom."/".$sub);
610    // Locked ?
611    if (!$this->islocked) {
612      $err->raise("dom",3);
613      return false;
614    }
615    $dest=trim($dest);
616    $dom=strtolower($dom);
617    $sub=strtolower($sub);
618    if ($sub != '*' && $sub != '' && preg_match('`[^a-z0-9-]`', $sub)) {
619      err->raise("dom",19);
620      return false;
621    }
622    if ($type==2) { // IP
623      if (!checkip($dest)) {
624        $err->raise("dom",19);
625        return false;
626      }
627    }
628    if ($type==1) { // URL
629      if (!checkurl($dest)) {
630        $err->raise("dom",20);
631        return false;
632      }
633    }
634    if ($type==0) { // LOCAL
635      if (substr($dest,0,1)!="/") {
636        $dest="/".$dest;
637      }
638      if (!checkuserpath($dest)) {
639        $err->raise("dom",21);
640        return false;
641      }
642    }
643    // On a épuré $dir des problèmes eventuels ... On est en DESSOUS du dossier de l'utilisateur.
644    $t=checkfqdn($dom);
645    if ($t) {
646      $err->raise("dom",3+$t);
647      return false;
648    }
649    if (!$r=$this->get_sub_domain_all($dom,$sub)) {
650      // Le sous-domaine n'existe pas, on le crée seulement si $action vaut add
651      if ($action=="add") {
652        $db->query("insert into sub_domaines (compte,domaine,sub,valeur,type) values ('$cuid','$dom','$sub','$dest',$type);");
653        $db->query("delete from sub_domaines_standby where domaine='$dom' and sub='$sub';");
654        $db->query("insert into sub_domaines_standby (compte,domaine,sub,valeur,type,action) values ('$cuid','$dom','$sub','$dest','$type',0);"); // INSERT
655      } else {
656        $err->raise("dom",14);
657        return false;
658      }
659    } else {
660      if ($action=="edit") {
661        // On vérifie que des modifications ont bien eu lieu :)
662        if ($r["type"]==$type && $r["dest"]==$dest) {
663          $err->raise("dom",15);
664          return false;
665        }
666        // OK, des modifs ont été faites, on valide :
667        $db->query("update sub_domaines set type='$type', valeur='$dest' where domaine='$dom' and sub='$sub'");
668        $db->query("delete from sub_domaines_standby where domaine='$dom' and sub='$sub'");
669        $db->query("insert into sub_domaines_standby (compte,domaine,sub,valeur,type,action) values ('$cuid','$dom','$sub','$dest','$type',1);"); // UPDATE
670      } else {
671        $err->raise("dom",16);
672        return false;
673      }
674    }
675    return true;
676  } // set_sub_domain
677
678  /* ----------------------------------------------------------------- */
679  /**
680   *  Supprime le sous-domaine demandé
681   *
682   * @param string $dom Domaine dont on souhaite supprimer un sous-domaine
683   * @param string $sub Sous-domaine que l'on souhaite supprimer
684   * @return boolean Retourne FALSE si une erreur s'est produite, TRUE sinon.
685   *
686   */
687  function del_sub_domain($dom,$sub) {
688    global $db,$err,$cuid;
689    $err->log("dom","del_sub_domain",$dom."/".$sub);
690    // Locked ?
691    if (!$this->islocked) {
692      $err->raise("dom",3);
693      return false;
694    }
695    $t=checkfqdn($dom);
696    if ($t) {
697      $err->raise("dom",3+$t);
698      return false;
699    }
700    if (!$r=$this->get_sub_domain_all($dom,$sub)) {
701      // Le sous-domaine n'existe pas, erreur
702      $err->raise("dom",14);
703      return false;
704    } else {
705      // OK, on valide :
706      $db->query("delete from sub_domaines where domaine='$dom' and sub='$sub'");
707      $db->query("delete from sub_domaines_standby where domaine='$dom' and sub='$sub'");
708      $db->query("insert into sub_domaines_standby (compte,domaine,sub,valeur,type,action) values ('$cuid','$dom','$sub','".$r["dest"]."','".$r["type"]."',2);"); // DELETE
709    }
710    return true;
711  } // del_sub_domain
712
713  /* ----------------------------------------------------------------- */
714  /**
715   * Modifie les information du domaine précisé.
716   *
717   * @param string $dom Domaine du compte courant que l'on souhaite modifier
718   * @param integer $dns Vaut 1 ou 0 pour héberger ou pas le DNS du domaine
719   * @param integer $mx Nom fqdn du serveur mx, si le mx local est précisé,
720   *  on héberge alors les mails du domaine.
721   * @return boolean appelle $mail->add_dom ou $ma->del_dom si besoin, en
722   *  fonction du champs MX. Retourne FALSE si une erreur s'est produite,
723   *  TRUE sinon.
724   *
725   */
726  function edit_domain($dom,$dns,$mx) {
727    global $db,$err,$L_MX,$classes,$cuid;
728    $err->log("dom","edit_domain",$dom);
729    // Locked ?
730    if (!$this->islocked) {
731      $err->raise("dom",3);
732      return false;
733    }
734    $t=checkfqdn($dom);
735    if ($t) {
736      $err->raise("dom",3+$t);
737      return false;
738    }
739    if (!$r=$this->get_domain_all($dom)) {
740      // Le domaine n'existe pas, Failure
741      $err->raise("dom",4,$dom);
742      return false;
743    }
744    if ($dns!="1") $dns="0";
745    // On vérifie que des modifications ont bien eu lieu :)
746    if ($r["dns"]==$dns && $r["mx"]==$mx) {
747      $err->raise("dom",15);
748      return false;
749    }
750    // MX ?
751    if ($mx==$L_MX)
752      $gesmx="1";
753    else
754      $gesmx="0";
755    // OK, des modifs ont été faites, on valide :
756    // DEPENDANCE :
757    if ($gesmx && !$r["mail"]) { // on a associé le MX : on cree donc l'entree dans LDAP
758      // Lancement de add_dom sur les classes domain_sensitive :
759     for($i=0;$i<count($classes);$i++) {
760      if (method_exists($GLOBALS[$classes[$i]],"alternc_add_mx_domain")) {
761        $GLOBALS[$classes[$i]]->alternc_add_mx_domain($dom);
762      }
763        }
764    }
765
766        if (!$gesmx && $r["mail"]) { // on a dissocié le MX : on détruit donc l'entree dans LDAP
767      // Lancement de del_dom sur les classes domain_sensitive :
768     for($i=0;$i<count($classes);$i++) {
769      if (method_exists($GLOBALS[$classes[$i]],"alternc_del_mx_domain")) {
770        $GLOBALS[$classes[$i]]->alternc_del_mx_domain($dom);
771      }
772        }
773    }
774
775    $db->query("update domaines set gesdns='$dns', mx='$mx', gesmx='$gesmx' where domaine='$dom'");
776    $db->query("insert into domaines_standby (compte,domaine,mx,gesdns,gesmx,action) values ('$cuid','$dom','$mx','$dns','$gesmx',1);"); 
777    // UPDATE
778    return true;
779  } // edit_domain
780
781
782
783  /****************************/
784  /*  Slave dns ip managment  */
785  /****************************/
786  /* ----------------------------------------------------------------- */
787  /**
788   * Return the list of ip addresses and classes that are allowed access to domain list
789   * through AXFR Transfers from the bind server.
790   */
791  function enum_slave_ip() {
792        global $db,$err;
793        $db->query("SELECT * FROM slaveip;");
794        if (!$db->next_record()) {
795          return false;
796        }
797        do {
798          $res[]=$db->Record;
799        } while ($db->next_record());
800        return $res;
801  }
802
803  /* ----------------------------------------------------------------- */
804  /**
805   * Add an ip address (or a ip class) to the list of allowed slave ip access list.
806   */
807  function add_slave_ip($ip,$class="32") {
808        global $db,$err;
809        if (!checkip($ip)) {
810                $err->raise("dom",19);
811                return false;
812        }
813        $class=intval($class);
814        if ($class<8 || $class>32) $class=32;
815        $db->query("SELECT * FROM slaveip WHERE ip='$ip' AND class='$class';");
816        if ($db->next_record()) {
817          $err->raise("err",22);
818          return false;
819        }
820        $db->query("INSERT INTO slaveip (ip,class) VALUES ('$ip','$class');");
821        $f=fopen(SLAVE_FLAG,"w");
822        fputs($f,"yopla");
823        fclose($f);     
824        return true;
825  }
826
827  /* ----------------------------------------------------------------- */
828  /**
829   * Remove an ip address (or a ip class) from the list of allowed slave ip access list.
830   */
831  function del_slave_ip($ip) {
832        global $db,$err;
833        if (!checkip($ip)) {
834                $err->raise("dom",19);
835                return false;
836        }
837        $db->query("DELETE FROM slaveip WHERE ip='$ip'");
838        $f=fopen(SLAVE_FLAG,"w");
839        fputs($f,"yopla");
840        fclose($f);     
841        return true;
842  }
843
844
845
846  /* ----------------------------------------------------------------- */
847  /**
848   * Check for a slave account
849   */
850  function check_slave_account($login,$pass) {
851        global $db,$err;
852        $db->query("SELECT * FROM slaveaccount WHERE login='$login' AND pass='$pass';");
853        if ($db->next_record()) { 
854                return true;
855        }
856        return false;
857  }
858
859  /* ----------------------------------------------------------------- */
860  /**
861   * Out (echo) the complete hosted domain list :
862   */
863  function echo_domain_list() {
864        global $db,$err;
865        $db->query("SELECT domaine FROM domaines WHERE gesdns=1 ORDER BY domaine");
866        while ($db->next_record()) {
867                echo $db->f("domaine")."\n";
868        }
869        return true;
870  }
871
872  /* ----------------------------------------------------------------- */
873  /**
874   * Return the list of allowed slave accounts
875   */
876  function enum_slave_account() {
877        global $db,$err;
878        $db->query("SELECT * FROM slaveaccount;");
879        $res=array();
880        while ($db->next_record()) {
881                $res[]=$db->Record;
882        }
883        if (!count($res)) return false;
884        return $res;
885  }
886
887  /* ----------------------------------------------------------------- */
888  /**
889   * Add a slave account that will be allowed to access the domain list
890   */
891  function add_slave_account($login,$pass) {
892        global $db,$err;
893        $db->query("SELECT * FROM slaveaccount WHERE login='$login'");
894        if ($db->next_record()) {
895          $err->raise("err",23);
896          return false;
897        }
898        $db->query("INSERT INTO slaveaccount (login,pass) VALUES ('$login','$pass')");
899        return true;
900  }
901
902  /* ----------------------------------------------------------------- */
903  /**
904   * Remove a slave account
905   */
906  function del_slave_account($login) {
907        global $db,$err;
908        $db->query("DELETE FROM slaveaccount WHERE login='$login'");
909        return true;
910  }
911
912  /*************/
913  /*  Private  */
914  /*************/
915
916
917  /* ----------------------------------------------------------------- */
918  /**
919   * Lock tente de verrouiller le fichier lock du cron. Si tout va bien (toujours?)
920   * retourne True, sinon retourne False
921   * NOTE : le systeme de lock est asymétrique, si on a un fichier CRONLOCK, on
922   * attends (que le cron ait fini son execution).
923   * @access private
924   */
925  function lock() {
926    global $db,$err;
927    $err->log("dom","lock");
928    if ($this->islocked) {
929      $err->raise("dom",17);
930    }
931    while (file_exists($this->fic_lock_cron)) {
932      sleep(2);
933    }
934    $this->islocked=true;
935    return true;
936  }
937
938  /* ----------------------------------------------------------------- */
939  /**
940   * unlock déverrouille le fichier lock du cron. Si tout va bien (toujours?)
941   * retourne True, sinon retourne False
942   * NOTE : actuellement, vu le système de lock asymetrique, on ne fait rien ;)
943   * @access private
944   */
945  function unlock() {
946    global $db,$err;
947    $err->log("dom","unlock");
948    if (!$this->islocked) {
949      $err->raise("dom",3);
950    }
951    $this->islocked=false;
952    return true;
953  }
954
955  /* ----------------------------------------------------------------- */
956  /**
957   * Efface un compte (tous ses domaines)
958   */
959  function alternc_del_member() {
960    global $err;
961    $err->log("dom","alternc_del_member");
962    $li=$this->enum_domains();
963    foreach($li as $dom) {
964      $this->del_domain($dom);
965    }
966    return true;
967  }
968
969  /* ----------------------------------------------------------------- */
970  /**
971   * Returns the used quota for the $name service for the current user.
972   * @param $name string name of the quota
973   * @return integer the number of service used or false if an error occured
974   * @access private
975   */
976  function alternc_get_quota($name) {
977    global $db,$err,$cuid;
978    if ($name=="dom") {
979      $err->log("dom","get_quota");
980      $db->query("SELECT COUNT(*) AS cnt FROM domaines WHERE compte='$cuid'");
981      $db->next_record();
982      return $db->f("cnt");
983    } else return false;
984  }
985
986  /* ----------------------------------------------------------------- */
987  /**
988   * Exporte toutes les informations domaine du compte.
989   * @access private
990   * EXPERIMENTAL 'sid' function ;)
991   */
992  function alternc_export() {
993    global $db,$err;
994    $err->log("dom","export");
995    $this->enum_domains();
996    $str="<dom>\n";
997    foreach ($this->domains as $d) {
998      $str.="  <domain>\n    <name>$d</name>\n";
999      $s=$this->get_domain_all($d);
1000      $str.="    <hasdns>$s[dns]</hasdns>\n";
1001      $str.="    <hasmx>$s[mx]</hasmx>\n";
1002      $str.="    <mx>$s[mail]</mx>\n";
1003      if (is_array($s[sub])) {
1004        foreach ($s[sub] as $sub) {
1005          $str.="    <subdomain>";
1006          $str.="<name>".$sub[name]."</name>";
1007          $str.="<dest>".$sub[dest]."</dest>";
1008          $str.="<type>".$sub[type]."</type>";
1009          $str.="</subdomain>\n";
1010        }
1011      }
1012      $str.="  </domain>\n";
1013    }
1014    $str.="</dom>\n";
1015    return $str;
1016  }
1017
1018
1019} /* Class m_domains */
1020
1021?>
Note: See TracBrowser for help on using the repository browser.