source: trunk/bureau/class/m_dom.php @ 845

Revision 845, 31.1 KB checked in by nahuel, 7 years ago (diff)

Correction du check du whois pour les domaines en .be

Line 
1<?php
2/*
3 $Id: m_dom.php,v 1.27 2006/02/17 18:34:30 olivier 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 update_domains.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','','http://www.".$domain."',1);");
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($found)
388             $tmp = trim($ligne);
389          if ($tmp)
390             $server[]=$tmp;
391          if ($ligne=="Nameservers:") {
392            $state=1;
393            $found=true;
394          }
395          break;
396        case "it":
397          if (ereg("nserver:", $ligne)) {
398            $found=true;
399            $tmp=strtolower(preg_replace("/nserver:\s*[^ ]*\s*([^\s]*)$/","\\1", $ligne));
400            if ($tmp)
401              $server[]=$tmp;
402          }
403          break;
404        case "fr":
405          if (ereg("nserver:", $ligne)) {
406            $found=true;
407            $tmp=strtolower(preg_replace("/nserver:\s*([^\s]*)\s*.*$/","\\1", $ligne));
408            if ($tmp)
409              $server[]=$tmp;
410          }
411          break;
412        case "ws";
413/* e.g.
414Welcome to the .WS Whois Server
415
416Use of this service for any purpose other
417than determining the availability of a domain
418in the .WS TLD to be registered is strictly
419prohibited.
420
421  Domain Name: DRONE.WS
422
423  Registrant: Registered through Go Daddy Software, Inc. (GoDaddy.com
424
425  Domain created on 2005-01-11 08:56:25
426  Domain last updated on 2005-01-11 08:56:25
427
428  Name servers:
429
430    ns2.koumbit.net
431    ns1.koumbit.net
432*/
433
434/*
435failure:
436
437Welcome to the .WS Whois Server
438
439Use of this service for any purpose other
440than determining the availability of a domain
441in the .WS TLD to be registered is strictly
442prohibited.
443
444No match for "dronefdasfsa.ws".
445
446*/
447          if (ereg('^[[:space:]]*Name servers:[[:space:]]*$', $ligne)) {
448                // found the server
449                $state = 1;
450          } elseif ($state) {
451                if ($ligne = ereg_replace('[[:space:]]', "", $ligne)) {
452                  // first non-whitespace line is considered to be the nameservers themselves
453                  $found = true;
454                  $server[] = $ligne;
455                }
456          }
457          break;
458        } // switch
459      } // while
460      fclose($fp);
461    } else {
462      $err->raise("dom",11);
463      return false;
464    }
465
466    if ($found) {
467      return $server;
468    } else {
469      $err->raise("dom",12);
470      return false;
471    }
472  } // whois
473
474  /* ----------------------------------------------------------------- */
475  /**
476   *  retourne TOUTES les infos d'un domaine
477   *
478   * <b>Note</b> : si le domaine est en attente (présent dans
479   *  domaines_standby), une erreur est retournée
480   *
481   * @param string $dom Domaine dont on souhaite les informations
482   * @return array Retourne toutes les infos du domaine sous la forme d'un
483   * tableau associatif comme suit :<br /><pre>
484   *  $r["name"] =  Nom fqdn
485   *  $r["dns"]  =  Gestion du dns ou pas ?
486   *  $r["mx"]   =  Valeur du champs MX si "dns"=true
487   *  $r["mail"] =  Heberge-t-on le mail ou pas ? (si "dns"=false)
488   *  $r["nsub"] =  Nombre de sous-domaines
489   *  $r["sub"]  =  tableau associatif des sous-domaines
490   *  $r["sub"][0-(nsub-1)]["name"] = nom du sous-domaine (NON-complet)
491   *  $r["sub"][0-(nsub-1)]["dest"] = Destination (url, ip, local ...)
492   *  $r["sub"][0-(nsub-1)]["type"] = Type (0-n) de la redirection.
493   *  </pre>
494   *  Retourne FALSE si une erreur s'est produite.
495   *
496   */
497  function get_domain_all($dom) {
498    global $db,$err,$cuid;
499    $err->log("dom","get_domain_all",$dom);
500    // Locked ?
501    if (!$this->islocked) {
502      $err->raise("dom",3);
503      return false;
504    }
505    $t=checkfqdn($dom);
506    if ($t) {
507      $err->raise("dom",3+$t);
508      return false;
509    }
510    $r["name"]=$dom;
511    $db->query("select * from domaines_standby where compte='$cuid' and domaine='$dom'");
512    if ($db->num_rows()>0) {
513      $err->raise("dom",13);
514      return false;
515    }
516    $db->query("select * from domaines where compte='$cuid' and domaine='$dom'");
517    if ($db->num_rows()==0) {
518      $err->raise("dom",1,$dom);
519      return false;
520    }
521    $db->next_record();
522    $r["dns"]=$db->Record["gesdns"];
523    $r["mail"]=$db->Record["gesmx"];
524    $r["mx"]=$db->Record["mx"];
525    $r[noerase]=$db->Record[noerase];
526    $db->free();
527    $db->query("select count(*) as cnt from sub_domaines where compte='$cuid' and domaine='$dom'");
528    $db->next_record();
529    $r["nsub"]=$db->Record["cnt"];
530    $db->free();
531    $db->query("select * from sub_domaines where compte='$cuid' and domaine='$dom'");
532    // Pas de webmail, on le cochera si on le trouve.
533    $this->webmail=0;
534    for($i=0;$i<$r["nsub"];$i++) {
535      $db->next_record();
536      $r["sub"][$i]=array();
537      $r["sub"][$i]["name"]=$db->Record["sub"];
538      $r["sub"][$i]["dest"]=$db->Record["valeur"];
539      $r["sub"][$i]["type"]=$db->Record["type"];
540      if ($db->Record["type"]==3) { // Webmail
541        $this->webmail=1;
542        $r["sub"][$i]["dest"]=_("Webmail access");
543      }
544    }
545    $db->free();
546    return $r;
547  } // get_domain_all
548
549  /* ----------------------------------------------------------------- */
550  /**
551   * Retourne TOUTES les infos d'un sous domaine du compte courant.
552   *
553   * @param string $dom Domaine fqdn concerné
554   * @param string $sub Sous-domaine dont on souhaite les informations
555   * @return arrray Retourne un tableau associatif contenant les
556   *  informations du sous-domaine demandé.<pre>
557   *  $r["name"]= nom du sous-domaine (NON-complet)
558   *  $r["dest"]= Destination (url, ip, local ...)
559   *  </pre>
560   *  $r["type"]= Type (0-n) de la redirection.
561   *  Retourne FALSE si une erreur s'est produite.
562   */
563  function get_sub_domain_all($dom,$sub) {
564    global $db,$err,$cuid;
565    $err->log("dom","get_sub_domain_all",$dom."/".$sub);
566    // Locked ?
567    if (!$this->islocked) {
568      $err->raise("dom",3);
569      return false;
570    }
571    $t=checkfqdn($dom);
572    if ($t) {
573      $err->raise("dom",3+$t);
574      return false;
575    }
576    $db->query("select * from sub_domaines where compte='$cuid' and domaine='$dom' and sub='$sub'");
577    if ($db->num_rows()==0) {
578      $err->raise("dom",14);
579      return false;
580    }
581    $db->next_record();
582    $r=array();
583    $r["name"]=$db->Record["sub"];
584    $r["dest"]=$db->Record["valeur"];
585    $r["type"]=$db->Record["type"];
586    $db->free();
587    return $r;
588  } // get_sub_domain_all
589
590  /* ----------------------------------------------------------------- */
591  /**
592   * Modifier les information du sous-domaine demandé.
593   *
594   * <b>Note</b> : si le sous-domaine $sub.$dom n'existe pas, il est créé.<br />
595   * <b>Note : TODO</b> : vérification de concordance de $dest<br />
596   *
597   * @param string $dom Domaine dont on souhaite modifier/ajouter un sous domaine
598   * @param string $subk Sous domaine à modifier / créer
599   * @param integer $type Type de sous-domaine (local, ip, url ...)
600   * @param string $action Action : vaut "add" ou "edit" selon que l'on
601   *  Crée (add) ou Modifie (edit) le sous-domaine
602   * @param string $dest Destination du sous-domaine, dépend de la valeur
603   *  de $type (url, ip, dossier...)
604   * @return boolean Retourne FALSE si une erreur s'est produite, TRUE sinon.
605   */
606  function set_sub_domain($dom,$sub,$type,$action,$dest) {
607    global $db,$err,$cuid;
608    $err->log("dom","set_sub_domain",$dom."/".$sub);
609    // Locked ?
610    if (!$this->islocked) {
611      $err->raise("dom",3);
612      return false;
613    }
614    $dest=trim($dest);
615    $dom=strtolower($dom);
616    $sub=strtolower($sub);
617    if ($sub != '*' && $sub != '' && preg_match('`[^a-z0-9-]`', $sub)) {
618      $err->raise("dom",24);
619      return false;
620    }
621    if ($type==2) { // IP
622      if (!checkip($dest)) {
623        $err->raise("dom",19);
624        return false;
625      }
626    }
627    if ($type==1) { // URL
628      if (!checkurl($dest)) {
629        $err->raise("dom",20);
630        return false;
631      }
632    }
633    if ($type==0) { // LOCAL
634      if (substr($dest,0,1)!="/") {
635        $dest="/".$dest;
636      }
637      if (!checkuserpath($dest)) {
638        $err->raise("dom",21);
639        return false;
640      }
641    }
642    // On a épuré $dir des problèmes eventuels ... On est en DESSOUS du dossier de l'utilisateur.
643    $t=checkfqdn($dom);
644    if ($t) {
645      $err->raise("dom",3+$t);
646      return false;
647    }
648    if (!$r=$this->get_sub_domain_all($dom,$sub)) {
649      // Le sous-domaine n'existe pas, on le crée seulement si $action vaut add
650      if ($action=="add") {
651        $db->query("insert into sub_domaines (compte,domaine,sub,valeur,type) values ('$cuid','$dom','$sub','$dest',$type);");
652        $db->query("delete from sub_domaines_standby where domaine='$dom' and sub='$sub';");
653        $db->query("insert into sub_domaines_standby (compte,domaine,sub,valeur,type,action) values ('$cuid','$dom','$sub','$dest','$type',0);"); // INSERT
654      } else {
655        $err->raise("dom",14);
656        return false;
657      }
658    } else {
659      if ($action=="edit") {
660        // On vérifie que des modifications ont bien eu lieu :)
661        if ($r["type"]==$type && $r["dest"]==$dest) {
662          $err->raise("dom",15);
663          return false;
664        }
665        // OK, des modifs ont été faites, on valide :
666        $db->query("update sub_domaines set type='$type', valeur='$dest' where domaine='$dom' and sub='$sub'");
667        $db->query("delete from sub_domaines_standby where domaine='$dom' and sub='$sub'");
668        $db->query("insert into sub_domaines_standby (compte,domaine,sub,valeur,type,action) values ('$cuid','$dom','$sub','$dest','$type',1);"); // UPDATE
669      } else {
670        $err->raise("dom",16);
671        return false;
672      }
673    }
674    return true;
675  } // set_sub_domain
676
677  /* ----------------------------------------------------------------- */
678  /**
679   *  Supprime le sous-domaine demandé
680   *
681   * @param string $dom Domaine dont on souhaite supprimer un sous-domaine
682   * @param string $sub Sous-domaine que l'on souhaite supprimer
683   * @return boolean Retourne FALSE si une erreur s'est produite, TRUE sinon.
684   *
685   */
686  function del_sub_domain($dom,$sub) {
687    global $db,$err,$cuid;
688    $err->log("dom","del_sub_domain",$dom."/".$sub);
689    // Locked ?
690    if (!$this->islocked) {
691      $err->raise("dom",3);
692      return false;
693    }
694    $t=checkfqdn($dom);
695    if ($t) {
696      $err->raise("dom",3+$t);
697      return false;
698    }
699    if (!$r=$this->get_sub_domain_all($dom,$sub)) {
700      // Le sous-domaine n'existe pas, erreur
701      $err->raise("dom",14);
702      return false;
703    } else {
704      // OK, on valide :
705      $db->query("delete from sub_domaines where domaine='$dom' and sub='$sub'");
706      $db->query("delete from sub_domaines_standby where domaine='$dom' and sub='$sub'");
707      $db->query("insert into sub_domaines_standby (compte,domaine,sub,valeur,type,action) values ('$cuid','$dom','$sub','".$r["dest"]."','".$r["type"]."',2);"); // DELETE
708    }
709    return true;
710  } // del_sub_domain
711
712  /* ----------------------------------------------------------------- */
713  /**
714   * Modifie les information du domaine précisé.
715   *
716   * @param string $dom Domaine du compte courant que l'on souhaite modifier
717   * @param integer $dns Vaut 1 ou 0 pour héberger ou pas le DNS du domaine
718   * @param integer $mx Nom fqdn du serveur mx, si le mx local est précisé,
719   *  on héberge alors les mails du domaine.
720   * @return boolean appelle $mail->add_dom ou $ma->del_dom si besoin, en
721   *  fonction du champs MX. Retourne FALSE si une erreur s'est produite,
722   *  TRUE sinon.
723   *
724   */
725  function edit_domain($dom,$dns,$mx) {
726    global $db,$err,$L_MX,$classes,$cuid;
727    $err->log("dom","edit_domain",$dom);
728    // Locked ?
729    if (!$this->islocked) {
730      $err->raise("dom",3);
731      return false;
732    }
733    $t=checkfqdn($dom);
734    if ($t) {
735      $err->raise("dom",3+$t);
736      return false;
737    }
738    if (!$r=$this->get_domain_all($dom)) {
739      // Le domaine n'existe pas, Failure
740      $err->raise("dom",4,$dom);
741      return false;
742    }
743    if ($dns!="1") $dns="0";
744    // On vérifie que des modifications ont bien eu lieu :)
745    if ($r["dns"]==$dns && $r["mx"]==$mx) {
746      $err->raise("dom",15);
747      return false;
748    }
749    // MX ?
750    if ($mx==$L_MX)
751      $gesmx="1";
752    else
753      $gesmx="0";
754    // OK, des modifs ont été faites, on valide :
755    // DEPENDANCE :
756    if ($gesmx && !$r["mail"]) { // on a associé le MX : on cree donc l'entree dans LDAP
757      // Lancement de add_dom sur les classes domain_sensitive :
758     for($i=0;$i<count($classes);$i++) {
759      if (method_exists($GLOBALS[$classes[$i]],"alternc_add_mx_domain")) {
760        $GLOBALS[$classes[$i]]->alternc_add_mx_domain($dom);
761      }
762        }
763    }
764
765        if (!$gesmx && $r["mail"]) { // on a dissocié le MX : on détruit donc l'entree dans LDAP
766      // Lancement de del_dom sur les classes domain_sensitive :
767     for($i=0;$i<count($classes);$i++) {
768      if (method_exists($GLOBALS[$classes[$i]],"alternc_del_mx_domain")) {
769        $GLOBALS[$classes[$i]]->alternc_del_mx_domain($dom);
770      }
771        }
772    }
773
774    $db->query("update domaines set gesdns='$dns', mx='$mx', gesmx='$gesmx' where domaine='$dom'");
775    $db->query("insert into domaines_standby (compte,domaine,mx,gesdns,gesmx,action) values ('$cuid','$dom','$mx','$dns','$gesmx',1);"); 
776    // UPDATE
777    return true;
778  } // edit_domain
779
780
781
782  /****************************/
783  /*  Slave dns ip managment  */
784  /****************************/
785  /* ----------------------------------------------------------------- */
786  /**
787   * Return the list of ip addresses and classes that are allowed access to domain list
788   * through AXFR Transfers from the bind server.
789   */
790  function enum_slave_ip() {
791        global $db,$err;
792        $db->query("SELECT * FROM slaveip;");
793        if (!$db->next_record()) {
794          return false;
795        }
796        do {
797          $res[]=$db->Record;
798        } while ($db->next_record());
799        return $res;
800  }
801
802  /* ----------------------------------------------------------------- */
803  /**
804   * Add an ip address (or a ip class) to the list of allowed slave ip access list.
805   */
806  function add_slave_ip($ip,$class="32") {
807        global $db,$err;
808        if (!checkip($ip)) {
809                $err->raise("dom",19);
810                return false;
811        }
812        $class=intval($class);
813        if ($class<8 || $class>32) $class=32;
814        $db->query("SELECT * FROM slaveip WHERE ip='$ip' AND class='$class';");
815        if ($db->next_record()) {
816          $err->raise("err",22);
817          return false;
818        }
819        $db->query("INSERT INTO slaveip (ip,class) VALUES ('$ip','$class');");
820        $f=fopen(SLAVE_FLAG,"w");
821        fputs($f,"yopla");
822        fclose($f);     
823        return true;
824  }
825
826  /* ----------------------------------------------------------------- */
827  /**
828   * Remove an ip address (or a ip class) from the list of allowed slave ip access list.
829   */
830  function del_slave_ip($ip) {
831        global $db,$err;
832        if (!checkip($ip)) {
833                $err->raise("dom",19);
834                return false;
835        }
836        $db->query("DELETE FROM slaveip WHERE ip='$ip'");
837        $f=fopen(SLAVE_FLAG,"w");
838        fputs($f,"yopla");
839        fclose($f);     
840        return true;
841  }
842
843
844
845  /* ----------------------------------------------------------------- */
846  /**
847   * Check for a slave account
848   */
849  function check_slave_account($login,$pass) {
850        global $db,$err;
851        $db->query("SELECT * FROM slaveaccount WHERE login='$login' AND pass='$pass';");
852        if ($db->next_record()) { 
853                return true;
854        }
855        return false;
856  }
857
858  /* ----------------------------------------------------------------- */
859  /**
860   * Out (echo) the complete hosted domain list :
861   */
862  function echo_domain_list() {
863        global $db,$err;
864        $db->query("SELECT domaine FROM domaines WHERE gesdns=1 ORDER BY domaine");
865        while ($db->next_record()) {
866                echo $db->f("domaine")."\n";
867        }
868        return true;
869  }
870
871  /* ----------------------------------------------------------------- */
872  /**
873   * Return the list of allowed slave accounts
874   */
875  function enum_slave_account() {
876        global $db,$err;
877        $db->query("SELECT * FROM slaveaccount;");
878        $res=array();
879        while ($db->next_record()) {
880                $res[]=$db->Record;
881        }
882        if (!count($res)) return false;
883        return $res;
884  }
885
886  /* ----------------------------------------------------------------- */
887  /**
888   * Add a slave account that will be allowed to access the domain list
889   */
890  function add_slave_account($login,$pass) {
891        global $db,$err;
892        $db->query("SELECT * FROM slaveaccount WHERE login='$login'");
893        if ($db->next_record()) {
894          $err->raise("err",23);
895          return false;
896        }
897        $db->query("INSERT INTO slaveaccount (login,pass) VALUES ('$login','$pass')");
898        return true;
899  }
900
901  /* ----------------------------------------------------------------- */
902  /**
903   * Remove a slave account
904   */
905  function del_slave_account($login) {
906        global $db,$err;
907        $db->query("DELETE FROM slaveaccount WHERE login='$login'");
908        return true;
909  }
910
911  /*************/
912  /*  Private  */
913  /*************/
914
915
916  /* ----------------------------------------------------------------- */
917  /**
918   * Lock tente de verrouiller le fichier lock du cron. Si tout va bien (toujours?)
919   * retourne True, sinon retourne False
920   * NOTE : le systeme de lock est asymétrique, si on a un fichier CRONLOCK, on
921   * attends (que le cron ait fini son execution).
922   * @access private
923   */
924  function lock() {
925    global $db,$err;
926    $err->log("dom","lock");
927    if ($this->islocked) {
928      $err->raise("dom",17);
929    }
930    while (file_exists($this->fic_lock_cron)) {
931      sleep(2);
932    }
933    $this->islocked=true;
934    return true;
935  }
936
937  /* ----------------------------------------------------------------- */
938  /**
939   * unlock déverrouille le fichier lock du cron. Si tout va bien (toujours?)
940   * retourne True, sinon retourne False
941   * NOTE : actuellement, vu le système de lock asymetrique, on ne fait rien ;)
942   * @access private
943   */
944  function unlock() {
945    global $db,$err;
946    $err->log("dom","unlock");
947    if (!$this->islocked) {
948      $err->raise("dom",3);
949    }
950    $this->islocked=false;
951    return true;
952  }
953
954  /* ----------------------------------------------------------------- */
955  /**
956   * Efface un compte (tous ses domaines)
957   */
958  function alternc_del_member() {
959    global $err;
960    $err->log("dom","alternc_del_member");
961    $li=$this->enum_domains();
962    foreach($li as $dom) {
963      $this->del_domain($dom);
964    }
965    return true;
966  }
967
968  /* ----------------------------------------------------------------- */
969  /**
970   * Returns the used quota for the $name service for the current user.
971   * @param $name string name of the quota
972   * @return integer the number of service used or false if an error occured
973   * @access private
974   */
975  function alternc_get_quota($name) {
976    global $db,$err,$cuid;
977    if ($name=="dom") {
978      $err->log("dom","get_quota");
979      $db->query("SELECT COUNT(*) AS cnt FROM domaines WHERE compte='$cuid'");
980      $db->next_record();
981      return $db->f("cnt");
982    } else return false;
983  }
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>".xml_entities($d)."</name>\n";
999      $s=$this->get_domain_all($d);
1000      $str.="    <hasdns>".xml_entities($s[dns])."</hasdns>\n";
1001      $str.="    <hasmx>".xml_entities($s[mx])."</hasmx>\n";
1002      $str.="    <mx>".xml_entities($s[mail])."</mx>\n";
1003      if (is_array($s[sub])) {
1004        foreach ($s[sub] as $sub) {
1005          $str.="    <subdomain>";
1006          $str.="<name>".xml_entities($sub[name])."</name>";
1007          $str.="<dest>".xml_entities($sub[dest])."</dest>";
1008          $str.="<type>".xml_entities($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.