source: alternc/branches/stable-1.0/bureau/class/m_dom.php @ 3054

Revision 3054, 40.9 KB checked in by benjamin, 2 years ago (diff)

fixing the dns/mx bug. Should Fixes #1247

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 = "VHOST";
70  var $type_url = "URL";
71  var $type_ip = "IP";
72  var $type_webmail = "WEBMAIL";
73  var $type_ipv6 = "IPV6";
74  var $type_cname = "CNAME";
75  var $type_txt = "TXT";
76  var $type_defmx = "DEFMX";
77  var $type_defmx2 = "DEFMX2";
78
79  var $action_insert = "0";
80  var $action_update= "1";
81  var $action_delete = "2";
82
83  /* ----------------------------------------------------------------- */
84  /**
85   * Constructeur
86   */
87  function m_dom() {
88  }
89
90  /* ----------------------------------------------------------------- */
91  /**
92   * Quota name
93   */
94  function alternc_quota_names() {
95    return "dom";
96  }
97  /* ----------------------------------------------------------------- */
98  /**
99   * Retourne un tableau contenant les types de domaines
100   *
101   * @return array retourne un tableau indexé contenant la liste types de domaines
102   *  authorisé. Retourne FALSE si une erreur s'est produite.
103   */
104  function domains_type_lst() {
105    global $db,$err,$cuid;
106    $err->log("dom","domains_type_lst");
107    $db->query("select * from domaines_type order by advanced;");
108    $this->domains_type_lst=false;
109    while ($db->next_record()) {
110      $this->domains_type_lst[strtolower($db->Record["name"])] = $db->Record;
111    }
112    return $this->domains_type_lst;
113  }
114
115  function domains_type_enable_values() {
116    global $db,$err,$cuid;
117    $err->log("dom","domains_type_target_values");
118    $db->query("desc domaines_type;");
119    $r = array();
120    while ($db->next_record()) {
121      if ($db->f('Field') == 'enable') {
122        $tab = explode(",", substr($db->f('Type'), 5, -1));
123        foreach($tab as $t) { $r[]=substr($t,1,-1); }
124      }
125    }
126    return $r;
127  }
128
129  function domains_type_target_values($type=null) {
130    global $db,$err,$cuid;
131    $err->log("dom","domains_type_target_values");
132    if (is_null($type)) {
133      $db->query("desc domaines_type;");
134      $r = array();
135      while ($db->next_record()) {
136        if ($db->f('Field') == 'target') {
137          $tab = explode(",", substr($db->f('Type'), 5, -1));
138          foreach($tab as $t) { $r[]=substr($t,1,-1); }
139        }
140      }
141      return $r;
142    } else {
143      $db->query("select target from domaines_type where name='$type';");
144      if (! $db->next_record()) return false;
145      return $db->f('target');
146    }
147  }
148
149  function domains_type_regenerate($name) {
150    global $db,$err,$cuid; 
151    $name=mysql_real_escape_string($name);
152    $db->query("update sub_domaines set web_action='UPDATE' where lower(type) = lower('$name') ;");
153    $db->query("update domaines d, sub_domaines sd set d.dns_action = 'UPDATE' where lower(sd.type)=lower('$name');");
154    return true;
155  }
156
157  function domains_type_get($name) {
158    global $db,$err,$cuid; 
159    $name=mysql_real_escape_string($name);
160    $db->query("select * from domaines_type where name='$name' ;");
161    $db->next_record();
162    return $db->Record;
163  }
164
165  function domains_type_del($name) {
166    global $db,$err,$cuid;
167    $name=mysql_real_escape_string($name);
168    $db->query("delete domaines_type where name='$name';");
169    return true;
170  }
171
172  function domains_type_disable($id) {
173    global $db,$err,$cuid;
174    $id=intval($id);
175    $db->query("update domaines_type set enable=false where id=$id;");
176    return true;
177  }
178
179  function domains_type_enable($id) {
180    global $db,$err,$cuid;
181    $id=intval($id);
182    $db->query("update domaines_type set enable=true where id=$id;");
183    return true;
184  }
185
186  function domains_type_update($name, $description, $target, $entry, $compatibility, $enable, $only_dns, $need_dns,$advanced) {
187    global $err,$cuid,$db;
188    $id=intval($id);
189    $name=mysql_real_escape_string($name);
190    $description=mysql_real_escape_string($description);
191    $target=mysql_real_escape_string($target);
192    $entry=mysql_real_escape_string($entry);
193    $compatibility=mysql_real_escape_string($compatibility);
194    $enable=mysql_real_escape_string($enable);
195    $only_dns=intval($only_dns);
196    $need_dns=intval($need_dns);
197    $advanced=intval($advanced);
198    $db->query("UPDATE domaines_type SET description='$description', target='$target', entry='$entry', compatibility='$compatibility', enable='$enable', need_dns=$need_dns, only_dns=$only_dns, advanced='$advanced' where name='$name';");
199    return true;
200  }   
201
202  function sub_domain_change_status($domain,$sub,$type,$value,$status) {
203    global $db,$err,$cuid;
204    $err->log("dom","sub_domain_change_status");
205    $status=strtoupper($status);
206    if (! in_array($status,array('ENABLE', 'DISABLE'))) return false;
207
208    $db->query("update sub_domaines set enable='$status' where domaine='$domain' and sub='$sub' and lower(type)=lower('$type') and valeur='$value'");
209
210    return true;
211  } 
212
213  /* ----------------------------------------------------------------- */
214  /**
215   * Retourne un tableau contenant les domaines d'un membre.
216   *
217   * @return array retourne un tableau indexé contenant la liste des
218   *  domaines hébergés sur le compte courant. Retourne FALSE si une
219   *  erreur s'est produite.
220   */
221  function enum_domains() {
222    global $db,$err,$cuid;
223    $err->log("dom","enum_domains");
224    $db->query("select * from domaines where compte='$cuid' order by domaine asc;");
225    $this->domains=array();
226    if ($db->num_rows()>0) {
227      while ($db->next_record()) {
228      $this->domains[]=$db->f("domaine");
229      }
230    }
231    return $this->domains;
232  }
233
234  function del_domain_cancel($dom) {
235    global $db,$err,$classes,$cuid;
236    $err->log("dom","del_domaini_canl",$dom);
237    $dom=strtolower($dom);
238    $db->query("UPDATE sub_domaines SET web_action='UPDATE'  WHERE domaine='$dom';");
239    $db->query("UPDATE domaines SET dns_action='UPDATE'  WHERE domaine='$dom';");
240
241    # TODO : some work with domain sensitive classes
242
243    return true;
244  }
245
246  /* ----------------------------------------------------------------- */
247  /**
248   *  Efface un domaine du membre courant, et tous ses sous-domaines
249   *
250   * Cette fonction efface un domaine et tous ses sous-domaines, ainsi que
251   * les autres services attachés à celui-ci. Elle appelle donc les autres
252   * classe. Chaque classe peut déclarer une fonction del_dom qui sera
253   * appellée lors de la destruction d'un domaine.
254   *
255   * @param string $dom nom de domaine à effacer
256   * @return boolean Retourne FALSE si une erreur s'est produite, TRUE sinon.
257   */
258  function del_domain($dom) {
259    global $db,$err,$classes,$cuid;
260    $err->log("dom","del_domain",$dom);
261    $dom=strtolower($dom);
262    $db->query("SELECT * FROM domaines WHERE domaine='$dom';");
263    if ($db->num_rows()==0) {
264      $err->raise("dom",1,$dom);
265      return false;
266    }
267    $db->next_record();
268    if ($db->f("compte")!=$cuid) {
269      $err->raise("dom",2,$dom);
270      return false;
271    }
272    $db->query("UPDATE sub_domaines SET web_action='DELETE'  WHERE domaine='$dom';");
273    $db->query("UPDATE domaines SET dns_action='DELETE'  WHERE domaine='$dom';");
274
275    // DEPENDANCE :
276    // Lancement de del_dom sur les classes domain_sensitive :
277    // Declenchons les autres classes.
278    foreach($classes as $c) {
279      if (method_exists($GLOBALS[$c],"alternc_del_domain")) {
280          $GLOBALS[$c]->alternc_del_domain($dom);
281      }
282    }
283    foreach($classes as $c) {
284      if (method_exists($GLOBALS[$c],"alternc_del_mx_domain")) {
285          $GLOBALS[$c]->alternc_del_mx_domain($dom);
286      }
287    }
288    return true;
289  }
290
291  /* ----------------------------------------------------------------- */
292  /**
293   *  Installe un domaine sur le compte courant.
294   *
295   * <p>Si le domaine existe déjà ou est interdit, ou est celui du serveur,
296   * l'installation est refusée. Si l'hébergement DNS est demandé, la fonction
297   * checkhostallow vérifiera que le domaine peut être installé conformément
298   * aux demandes des super-admin.
299   * Si le dns n'est pas demandé, le domaine peut être installé s'il est en
300   * seconde main d'un tld (exemple : test.eu.org ou test.com, mais pas
301   * toto.test.org ou test.test.asso.fr)</p>
302   * <p>Chaque classe peut définir une fonction add_dom($dom) qui sera
303   * appellée lors de l'installation d'un nouveau domaine.</p>
304   *
305   * @param string $dom nom fqdn du domaine à installer
306   * @param integer $dns 1 ou 0 pour héberger le DNS du domaine ou pas.
307   * @param integer $noerase 1 ou 0 pour rendre le domaine inamovible ou non
308   * @param integer $force 1 ou 0, si 1, n'effectue pas les tests de DNS.
309   *  force ne devrait être utilisé que par le super-admin.
310   $ @return boolean Retourne FALSE si une erreur s'est produite, TRUE sinon.
311  */
312  function add_domain($domain,$dns,$noerase=0,$force=0,$isslave=0,$slavedom="") {
313    global $db,$err,$quota,$classes,$L_MX,$L_FQDN,$tld,$cuid,$bro;
314    $err->log("dom","add_domain",$domain);
315
316    // Locked ?
317    if (!$this->islocked) {
318      $err->raise("dom",3);
319      return false;
320    }
321    // Verifie que le domaine est rfc-compliant
322    $domain=strtolower($domain);
323    $t=checkfqdn($domain);
324    if ($t) {
325      $err->raise("dom",3+$t);
326      return false;
327    }
328    // Interdit les domaines clés (table forbidden_domains) sauf en cas FORCE
329    $db->query("select domain from forbidden_domains where domain='$domain'");
330    if ($db->num_rows() && !$force) {
331      $err->raise("dom",22);
332      return false;
333    }
334    if ($domain==$L_FQDN || $domain=="www.$L_FQDN") {
335      $err->raise("dom",18);
336      return false;
337    }
338    $db->query("SELECT compte FROM domaines WHERE domaine='$domain';");
339    if ($db->num_rows()) {
340      $err->raise("dom",8);
341      return false;
342    }
343    $db->query("SELECT compte FROM `sub_domaines` WHERE sub != \"\" AND concat( sub, \".\", domaine )='$domain' OR domaine='$domain';");
344    if ($db->num_rows()) {
345      $err->raise("dom",8);
346      return false;
347    }
348    $this->dns=$this->whois($domain);
349    if (!$force) {
350      $v=checkhostallow($domain,$this->dns);
351      if ($v==-1) {
352        $err->raise("dom",7);   // TLD interdit
353        return false;
354      }
355      if ($dns && $v==-2) {
356        $err->raise("dom",12);   // Domaine non trouvé dans le whois
357        return false;
358      }
359      if ($dns && $v==-3) {
360        $err->raise("dom",23);   // Domaine non trouvé dans le whois
361        return false;
362      }
363
364      if ($dns) $dns="1"; else $dns="0";
365
366      // mode 5 : force DNS to NO.
367      if ($tld[$v]==5) $dns=0;
368      // It must be a real domain (no subdomain)
369      if (!$dns) {
370         $v=checkhostallow_nodns($domain);
371         if ($v) {
372           $err->raise("dom",22);
373           return false;
374         }
375      }
376    }
377    // Check the quota :
378    if (!$quota->cancreate("dom")) {
379      $err->raise("dom",10);
380      return false;
381    }
382    if ($noerase) $noerase="1"; else $noerase="0";
383    $db->query("insert into domaines (compte,domaine,gesdns,gesmx,noerase,dns_action) values ('$cuid','$domain','$dns','1','$noerase','UPDATE');");
384
385    if ($isslave) {
386      $isslave=true;
387      $db->query("SELECT domaine FROM domaines WHERE compte='$cuid' AND domaine='$slavedom';");
388      $db->next_record();
389      if (!$db->Record["domaine"]) {
390        $err->raise("dom",1,$slavedom);
391        $isslave=false;
392      }
393      // Point to the master domain :
394      $this->set_sub_domain($domain, '',     $this->type_url, 'http://www.'.$slavedom);
395      $this->set_sub_domain($domain, 'www',  $this->type_url, 'http://www.'.$slavedom);
396      $this->set_sub_domain($domain, 'mail', $this->type_url, 'http://mail.'.$slavedom);     
397    }
398    if (!$isslave) {
399      // Creation du repertoire dans www
400      $dest_root = $bro->get_userid_root($cuid);
401      $domshort=str_replace("-","",str_replace(".","",$domain));
402     
403      if (! is_dir($dest_root . "/". $domshort)) {
404        mkdir($dest_root . "/". $domshort);
405      }
406     
407      // Creation des 3 sous-domaines par défaut : Vide, www et mail
408      $this->set_sub_domain($domain, '',     $this->type_url,     'http://www.'.$domain);
409      $this->set_sub_domain($domain, 'www',  $this->type_local,   '/'. $domshort);
410      $this->set_sub_domain($domain, 'mail', $this->type_webmail, '');
411    }
412
413    // DEPENDANCE :
414    // Lancement de add_dom sur les classes domain_sensitive :
415    // Declenchons les autres classes.   
416    foreach($classes as $c) {
417      if (method_exists($GLOBALS[$c],"alternc_add_domain")) {
418        $GLOBALS[$c]->alternc_add_domain($domain);
419      }
420    }
421    foreach($classes as $c) {
422      if (method_exists($GLOBALS[$c],"alternc_add_mx_domain")) {
423        $GLOBALS[$c]->alternc_add_mx_domain($domain);
424      }
425    }
426    if ($isslave) {
427      foreach($classes as $c) {
428        if (method_exists($GLOBALS[$c],"alternc_add_slave_domain")) {
429          $GLOBALS[$c]->alternc_add_slave_domain($domain,$slavedom);
430        }
431      } 
432    }
433    return true;
434  }
435
436  /* ----------------------------------------------------------------- */
437  /**
438   * Retourne les entrées DNS du domaine $domain issues du WHOIS.
439   *
440   * Cette fonction effectue un appel WHOIS($domain) sur Internet,
441   * et extrait du whois les serveurs DNS du domaine demandé. En fonction
442   * du TLD, on sait (ou pas) faire le whois correspondant.
443   * Actuellement, les tld suivants sont supportés :
444   * .com .net .org .be .info .ca .cx .fr .biz .name
445   *
446   * @param string $domain Domaine fqdn dont on souhaite les serveurs DNS
447   * @return array Retourne un tableau indexé avec les NOMS fqdn des dns
448   *   du domaine demandé. Retourne FALSE si une erreur s'est produite.
449   *
450   */
451  function whois($domain) {
452    global $db,$err;
453    $err->log("dom","whois",$domain);
454    // pour ajouter un nouveau TLD, utiliser le code ci-dessous.
455    //  echo "whois : $domain<br />";
456    ereg(".*\.([^\.]*)",$domain,$out);
457    $ext=$out[1];
458    // pour ajouter un nouveau TLD, utiliser le code ci-dessous.
459    //  echo "ext: $ext<br />";
460
461    if (($fp=@fsockopen("whois.iana.org", 43))>0) {
462      fputs($fp, "$domain\r\n");
463      $found = false;
464      $state=0;
465      while (!feof($fp)) {
466        $ligne = fgets($fp,128);
467        if (ereg('^whois:[[:space:]]+.*$', $ligne)) { $serveur=preg_replace('/whois:\ */','',$ligne,1); }
468      }
469    }
470
471    $egal="";
472    switch($ext) {
473    case "net":
474      $egal="=";
475      break;
476    case "name":
477      $egal="domain = ";
478      break;
479    }
480    // pour ajouter un nouveau TLD, utiliser le code ci-dessous.
481    //  echo "serveur : $serveur <br />";
482    if (($fp=@fsockopen($serveur, 43))>0) {
483      fputs($fp, "$egal$domain\r\n");
484      $found = false;
485      $state=0;
486      while (!feof($fp)) {
487  $ligne = fgets($fp,128);
488  // pour ajouter un nouveau TLD, utiliser le code ci-dessous.
489  //  echo "| $ligne<br />";
490  switch($ext) {
491  case "org":
492  case "com":
493  case "net":
494  case "info":
495  case "biz":
496  case "name":
497  case "cc":
498    if (ereg("Name Server:", $ligne)) {
499      $found = true;
500      $tmp=strtolower(ereg_replace(chr(10), "",ereg_replace(chr(13),"",ereg_replace(" ","", ereg_replace("Name Server:","", $ligne)))));
501      if ($tmp)
502        $server[]=$tmp;
503    }
504    break;
505  case "cx":
506    $ligne = ereg_replace(chr(10), "",ereg_replace(chr(13),"",ereg_replace(" ","", $ligne)));
507    if ($ligne=="" && $state==1)
508      $state=2;
509    if ($state==1)
510      $server[]=strtolower($ligne);
511    if ($ligne=="Nameservers:" && $state==0) {
512      $state=1;
513      $found = true;
514    }
515    break;
516        case "eu":
517  case "be":
518          $ligne=preg_replace("/^ *([^ ]*) \(.*\)$/","\\1",trim($ligne));
519          if($found)
520             $tmp = trim($ligne);
521          if ($tmp)
522             $server[]=$tmp;
523          if ($ligne=="Nameservers:") {
524            $state=1;
525            $found=true;
526          }
527          break;
528    case "im":
529          if (preg_match('/Name Server:/', $ligne)) {
530            $found = true;
531            // weird regexp (trailing garbage after name server), but I could not make it work otherwise
532            $tmp = strtolower(preg_replace('/Name Server: ([^ ]+)\..$/',"\\1", $ligne));
533            $tmp = preg_replace('/[^-_a-z0-9\.]/', '', $tmp);
534            if ($tmp)
535              $server[]=$tmp;
536          }
537          break;
538    case "it":
539          if (ereg("nserver:", $ligne)) {
540            $found=true;
541            $tmp=strtolower(preg_replace("/nserver:\s*[^ ]*\s*([^\s]*)$/","\\1", $ligne));
542            if ($tmp)
543              $server[]=$tmp;
544          }
545          break;
546  case "fr":
547  case "re":
548          if (ereg("nserver:", $ligne)) {
549            $found=true;
550            $tmp=strtolower(preg_replace("/nserver:\s*([^\s]*)\s*.*$/","\\1", $ligne));
551            if ($tmp)
552              $server[]=$tmp;
553          }
554          break;
555  case "ca":
556  case "ws";
557    if (ereg('^[[:space:]]*Name servers:[[:space:]]*$', $ligne)) {
558          // found the server
559      $state = 1;
560    } elseif ($state) {
561      if (ereg('^[^%]', $ligne) && $ligne = ereg_replace('[[:space:]]', "", $ligne)) {
562      // first non-whitespace line is considered to be the nameservers themselves
563      $found = true;
564      $server[] = $ligne;
565    }
566    }
567    break;
568        case "coop":
569          if (preg_match('/Host Name:\s*([^\s]+)/', $ligne, $matches)) {
570            $found = true;
571            $server[] = $matches[1];
572          }
573  } // switch
574      } // while
575      fclose($fp);
576    } else {
577      $err->raise("dom",11);
578      return false;
579    }
580
581    if ($found) {
582      return $server;
583    } else {
584      $err->raise("dom",12);
585      return false;
586    }
587  } // whois
588
589  /* ----------------------------------------------------------------- */
590  /**
591   *  vérifie la presence d'un champs mx valide sur un serveur DNS
592   *
593  */
594 
595  function checkmx($domaine,$mx) {
596    //initialise variables
597    $mxhosts = array();
598   
599    //récupére les champs mx
600    if (!getmxrr($domaine,$mxhosts)) {
601      //aucune hôte mx spécifié
602      return 1;
603    }
604    else {
605      //vérifie qu'un des hôtes est bien sur alternc
606      $bolmx = 0;
607      //décompose les différents champ MX coté alternc
608      $arrlocalmx = split(",",$mx);
609      //parcours les différents champ MX retournés
610      foreach($mxhosts as $mxhost) {
611        foreach($arrlocalmx as $localmx) {
612          if ($mxhost==$localmx) {
613            $bolmx = 1;
614          }
615        }
616      }
617      //définition de l'erreur selon reponse du parcours de mxhosts
618      if ($bolmx == 0) {
619        //aucun des champs MX ne correspond au serveur
620        return 2;         
621      }
622      else {
623        //un champ mx correct a été trouvé
624        return 0;
625      }
626    }
627  } //checkmx
628
629
630  /* ----------------------------------------------------------------- */
631  /**
632   *  retourne TOUTES les infos d'un domaine
633   *
634   * @param string $dom Domaine dont on souhaite les informations
635   * @return array Retourne toutes les infos du domaine sous la forme d'un
636   * tableau associatif comme suit :<br /><pre>
637   *  $r["name"] =  Nom fqdn
638   *  $r["dns"]  =  Gestion du dns ou pas ?
639   *  $r["mx"]   =  Valeur du champs MX si "dns"=true
640   *  $r["mail"] =  Heberge-t-on le mail ou pas ? (si "dns"=false)
641   *  $r["nsub"] =  Nombre de sous-domaines
642   *  $r["sub"]  =  tableau associatif des sous-domaines
643   *  $r["sub"][0-(nsub-1)]["name"] = nom du sous-domaine (NON-complet)
644   *  $r["sub"][0-(nsub-1)]["dest"] = Destination (url, ip, local ...)
645   *  $r["sub"][0-(nsub-1)]["type"] = Type (0-n) de la redirection.
646   *  </pre>
647   *  Retourne FALSE si une erreur s'est produite.
648   *
649   */
650  function get_domain_all($dom) {
651    global $db,$err,$cuid;
652    $err->log("dom","get_domain_all",$dom);
653    // Locked ?
654    if (!$this->islocked) {
655      $err->raise("dom",3);
656      return false;
657    }
658    $t=checkfqdn($dom);
659    if ($t) {
660      $err->raise("dom",3+$t);
661      return false;
662    }
663    $r["name"]=$dom;
664    $db->query("select * from domaines where compte='$cuid' and domaine='$dom'");
665    if ($db->num_rows()==0) {
666      $err->raise("dom",1,$dom);
667      return false;
668    }
669    $db->next_record();
670    $r["dns"]=$db->Record["gesdns"];
671    $r["dns_action"]=$db->Record["dns_action"];
672    $r["dns_result"]=$db->Record["dns_result"];
673    $r["mail"]=$db->Record["gesmx"];
674    $r["mx"]=$db->Record["mx"];
675    $r['noerase']=$db->Record['noerase'];
676    $db->free();
677    $db->query("select count(*) as cnt from sub_domaines where compte='$cuid' and domaine='$dom'");
678    $db->next_record();
679    $r["nsub"]=$db->Record["cnt"];
680    $db->free();
681    $db->query("select sd.*, dt.description as type_desc, dt.only_dns from sub_domaines sd, domaines_type dt where compte='$cuid' and domaine='$dom' and upper(dt.name)=upper(sd.type) order by sd.sub,sd.type");
682    // Pas de webmail, on le cochera si on le trouve.
683    $this->webmail=0;
684    for($i=0;$i<$r["nsub"];$i++) {
685      $db->next_record();
686      $r["sub"][$i]=array();
687      $r["sub"][$i]["name"]=$db->Record["sub"];
688      $r["sub"][$i]["dest"]=$db->Record["valeur"];
689      $r["sub"][$i]["type"]=$db->Record["type"];
690      $r["sub"][$i]["enable"]=$db->Record["enable"];
691      $r["sub"][$i]["type_desc"]=$db->Record["type_desc"];
692      $r["sub"][$i]["only_dns"]=$db->Record["only_dns"];
693      $r["sub"][$i]["web_action"]=$db->Record["web_action"];
694/*
695      if ($db->Record["type"]==3) { // Webmail
696  $this->webmail=1;
697  $r["sub"][$i]["dest"]=_("Webmail access");
698      }
699*/
700    }
701    $db->free();
702    return $r;
703  } // get_domain_all
704
705
706  /* ----------------------------------------------------------------- */
707  /**
708   * Retourne TOUTES les infos d'un sous domaine du compte courant.
709   *
710   * @param string $dom Domaine fqdn concerné
711   * @param string $sub Sous-domaine dont on souhaite les informations
712   * @return arrray Retourne un tableau associatif contenant les
713   *  informations du sous-domaine demandé.<pre>
714   *  $r["name"]= nom du sous-domaine (NON-complet)
715   *  $r["dest"]= Destination (url, ip, local ...)
716   *  </pre>
717   *  $r["type"]= Type (0-n) de la redirection.
718   *  Retourne FALSE si une erreur s'est produite.
719   */
720  function get_sub_domain_all($dom,$sub, $type="", $value='') {
721    global $db,$err,$cuid;
722    $err->log("dom","get_sub_domain_all",$dom."/".$sub);
723    // Locked ?
724    if (!$this->islocked) {
725      $err->raise("dom",3);
726      return false;
727    }
728    $t=checkfqdn($dom);
729    if ($t) {
730      $err->raise("dom",3+$t);
731      return false;
732    }
733/*
734    if ( ! empty($value)) {
735        $type = " and valeur=\"".mysql_real_escape_string($value)."\"";
736    }
737    if ( ! empty($type)) {
738        $type = " and type=\"".mysql_real_escape_string($type)."\"";
739    }
740*/
741    $db->query("select sd.*, dt.description as type_desc, dt.only_dns from sub_domaines sd, domaines_type dt where compte='$cuid' and domaine='$dom' and sub='$sub' and ( length('$type')=0 or type='$type') and (length('$value')=0 or '$value'=valeur) and upper(dt.name)=upper(sd.type);");
742    if ($db->num_rows()==0) {
743      $err->raise("dom",14);
744      return false;
745    }
746    $db->next_record();
747    $r=array();
748    $r["name"]=$db->Record["sub"];
749    $r["dest"]=$db->Record["valeur"];
750    $r["enable"]=$db->Record["enable"];
751    $r["type_desc"]=$db->Record["type_desc"];
752    $r["only_dns"]=$db->Record["only_dns"];
753    $r["web_action"]=$db->Record["web_action"];
754    $db->free();
755    return $r;
756  } // get_sub_domain_all
757
758
759  function check_type_value($type, $value) {
760    global $db,$err,$cuid;
761
762    // check the type we can have in domaines_type.target
763
764    switch ($this->domains_type_target_values($type)) {
765      case 'NONE':
766        if (empty($value) or is_null($value)) {return true;}
767        break;
768      case 'URL': 
769        if ( $value == strval($value)) {return true;}
770        break;
771      case 'DIRECTORY': 
772        if (substr($value,0,1)!="/") {
773          $value="/".$value;
774        }
775        if (!checkuserpath($value)) {
776          $err->raise("dom",21);
777        return false;
778        }
779        return true;
780        break;
781      case 'IP': 
782        if (checkip($value)) {return true;}
783        break;
784      case 'IPV6': 
785        if (checkipv6($value)) {return true;}
786        break;
787      case 'DOMAIN': 
788        if (checkcname($value)) {return true;}
789        break;
790      case 'TXT':
791        if ( $value == strval($value)) {return true;}
792        break;
793      default:
794        return false;
795        break;
796    }
797    return false;
798  } //check_type_value
799
800
801  function can_create_subdomain($dom,$sub,$type,$type_old='', $value_old='') {
802    global $db,$err,$cuid;
803    $err->log("dom","can_create_subdomain",$dom."/".$sub);
804
805    # Get the compatibility list for this domain type
806    $db->query("select upper(compatibility) as compatibility from domaines_type where upper(name)=upper('$type');");
807    if (!$db->next_record()) return false;
808    $compatibility_lst = explode(",",$db->f('compatibility'));
809
810    # Get the list of type of subdomains already here who have the same name
811    $db->query("select * from sub_domaines where sub='$sub' and domaine='$dom' and not (type='$type_old' and valeur='$value_old') and web_action != 'DELETE'");
812    #$db->query("select * from sub_domaines where sub='$sub' and domaine='$dom';");
813    while ($db->next_record()) {
814      # And if there is a domain with a incompatible type, return false
815      if (! in_array(strtoupper($db->f('type')),$compatibility_lst)) return false;
816    }
817   
818    # All is right, go ! Create ur domain !
819    return true;
820  }
821
822  //  /* ----------------------------------------------------------------- */
823  /**
824   * Modifier les information du sous-domaine demandé.
825   *
826   * <b>Note</b> : si le sous-domaine $sub.$dom n'existe pas, il est créé.<br />
827   * <b>Note : TODO</b> : vérification de concordance de $dest<br />
828   *
829   * @param string $dom Domaine dont on souhaite modifier/ajouter un sous domaine
830   * @param string $subk Sous domaine à modifier / créer
831   * @param integer $type Type de sous-domaine (local, ip, url ...)
832   * @param string $action Action : vaut "add" ou "edit" selon que l'on
833   *  Crée (add) ou Modifie (edit) le sous-domaine
834   * @param string $dest Destination du sous-domaine, dépend de la valeur
835   *  de $type (url, ip, dossier...)
836   * @return boolean Retourne FALSE si une erreur s'est produite, TRUE sinon.
837   */
838   // TODO : j'ai viré le type action, valider que plus personne ne l'utilise (quatrieme argument)
839  function set_sub_domain($dom,$sub,$type,$dest, $type_old=null,$sub_old=null,$value_old=null) {
840    global $db,$err,$cuid;
841    $err->log("dom","set_sub_domain",$dom."/".$sub."/".$type."/".$dest);
842    // Locked ?
843    if (!$this->islocked) {
844      $err->raise("dom",3);
845      return false;
846    }
847    $dest=trim($dest);
848    $sub=trim(trim($sub),".");
849    $dom=strtolower($dom);
850    $sub=strtolower($sub);
851
852    //    if (!(($sub == '*') || ($sub=="") || (preg_match('/([a-z0-9][\.\-a-z0-9]*)?[a-z0-9]/', $sub)))) {
853    $fqdn=checkfqdn($sub);
854    // Special cases : * (all subdomains at once) and '' empty subdomain are allowed.
855    if (($sub != '*' && $sub!='') && !($fqdn==0 || $fqdn==4)) {
856      $err->raise("dom",24);
857      return false;
858    }
859
860    if (! $this->check_type_value($type,$dest)) {
861      # TODO have a real err code
862      $err->raise("dom",667);
863      return false;
864    }
865
866    // On a épuré $dir des problèmes eventuels ... On est en DESSOUS du dossier de l'utilisateur.
867    if ($t=checkfqdn($dom)) {
868      $err->raise("dom",3+$t);
869      return false;
870    }
871
872    if (! $this->can_create_subdomain($dom,$sub,$type,$type_old,$value_old)) {
873      # TODO have a real error code
874      $err->raise("dom", 654);
875      return false;
876    }
877
878    if (! is_null($type_old )) { // It's not a creation, it's an edit. Delete the old one
879      $db->query("update sub_domaines set web_action='DELETE' where domaine='$dom' and sub='$sub' and upper(type)=upper('$type_old') and valeur='$value_old';");
880    }
881
882    // Re-create the one we want
883    if (! $db->query("insert into sub_domaines (compte,domaine,sub,valeur,type,web_action) values ('$cuid','$dom','$sub','$dest','$type','UPDATE');") ) {
884      echo "query failed: ".$db->Error;
885      return false;
886    }
887
888    // Tell to update the DNS file
889    $db->query("update domaines set dns_action='UPDATE' where domaine='$dom';");
890
891    return true;
892  } // set_sub_domain
893
894
895  /* ----------------------------------------------------------------- */
896  /**
897   *  Supprime le sous-domaine demandé
898   *
899   * @param string $dom Domaine dont on souhaite supprimer un sous-domaine
900   * @param string $sub Sous-domaine que l'on souhaite supprimer
901   * @return boolean Retourne FALSE si une erreur s'est produite, TRUE sinon.
902   *
903   */
904  function del_sub_domain($dom,$sub,$type,$value='') {
905    global $db,$err,$cuid;
906    $err->log("dom","del_sub_domain",$dom."/".$sub);
907    // Locked ?
908    if (!$this->islocked) {
909      $err->raise("dom",3);
910      return false;
911    }
912    $t=checkfqdn($dom);
913    if ($t) {
914      $err->raise("dom",3+$t);
915      return false;
916    }
917    if (!$r=$this->get_sub_domain_all($dom,$sub,$type)) {
918      // Le sous-domaine n'existe pas, erreur
919      $err->raise("dom",14);
920      return false;
921    } else {
922      // OK, on valide :
923      $db->query("update sub_domaines set web_action='DELETE' where domaine='$dom' and sub='$sub' and type='$type' and ( length('$value')=0 or valeur='$value') ");
924      $db->query("update domaines set dns_action='UPDATE' where domaine='$dom';");
925    }
926    return true;
927  } // del_sub_domain
928
929  /* ----------------------------------------------------------------- */
930  /**
931   * Modifie les information du domaine précisé.
932   *
933   * @param string $dom Domaine du compte courant que l'on souhaite modifier
934   * @param integer $dns Vaut 1 ou 0 pour héberger ou pas le DNS du domaine
935   * @param integer $gesmx Héberge-t-on le emails du domaines sur ce serveur ?
936   * @param boolean $force Faut-il passer les checks DNS ou MX ? (admin only)
937   * @return boolean appelle $mail->add_dom ou $ma->del_dom si besoin, en
938   *  fonction du champs MX. Retourne FALSE si une erreur s'est produite,
939   *  TRUE sinon.
940   *
941   */
942  function edit_domain($dom,$dns,$gesmx,$force=0) {
943    global $db,$err,$L_MX,$classes,$cuid;
944    $err->log("dom","edit_domain",$dom."/".$dns."/".$gesmx);
945    // Locked ?
946    if (!$this->islocked && !$force) {
947      $err->raise("dom",3);
948      return false;
949    }
950    if ($dns == 1 && !$force) {
951      $this->dns=$this->whois($dom);
952      $v=checkhostallow($dom,$this->dns);
953      if ($v==-1) {
954        $err->raise("dom",7);   // TLD interdit
955        return false;
956      }
957      if ($dns && $v==-2) {
958        $err->raise("dom",12);  // Domaine non trouvé dans le whois
959        return false;
960      }
961      if ($dns && $v==-3) {
962        $err->raise("dom",23);  // Domaine non trouvé dans le whois
963        return false;
964      }
965    }
966    $t=checkfqdn($dom);
967    if ($t) {
968      $err->raise("dom",3+$t);
969      return false;
970    }
971    if (!$r=$this->get_domain_all($dom)) {
972      // Le domaine n'existe pas, Failure
973      $err->raise("dom",4,$dom);
974      return false;
975    }
976    if ($dns!="1") $dns="0";
977    // On vérifie que des modifications ont bien eu lieu :)
978    if ($r["dns"]==$dns && $r["mail"]==$gesmx) {
979      $err->raise("dom",15);
980      return false;
981    }
982     
983    //si gestion mx uniquement, vérification du dns externe
984    if ($dns=="0" && $gesmx=="1" && !$force) {
985      $vmx = $this->checkmx($dom,$mx);
986      if ($vmx == 1) {
987        // Aucun champ mx de spécifié sur le dns
988        $err->raise("dom",25);
989        return false;
990      }
991     
992      if ($vmx == 2) {
993        // Serveur non spécifié parmi les champx mx
994        $err->raise("dom",25);
995        return false;
996      }
997    }
998     
999    // OK, des modifs ont été faites, on valide :
1000    // DEPENDANCE :
1001    if ($gesmx && !$r["mail"]) { // on a associé le MX : on cree donc l'entree dans MySQL
1002      // Lancement de add_dom sur les classes domain_sensitive :
1003      foreach($classes as $c) {
1004        if (method_exists($GLOBALS[$c],"alternc_add_mx_domain")) {
1005          $GLOBALS[$c]->alternc_add_mx_domain($dom);
1006        }
1007      }
1008    }
1009   
1010    if (!$gesmx && $r["mail"]) { // on a dissocié le MX : on détruit donc l'entree dans LDAP
1011      // Lancement de del_dom sur les classes domain_sensitive :
1012      foreach($classes as $c) {
1013        if (method_exists($GLOBALS[$c],"alternc_del_mx_domain")) {
1014          $GLOBALS[$c]->alternc_del_mx_domain($dom);
1015        }
1016      }
1017    }
1018   
1019    $db->query("UPDATE domaines SET gesdns='$dns', gesmx='$gesmx' WHERE domaine='$dom'");
1020    $db->query("UPDATE domaines set dns_action='UPDATE' where domaine='$dom';");
1021   
1022    return true;
1023  } // edit_domain
1024 
1025
1026
1027  /****************************/
1028  /*  Slave dns ip managment  */
1029  /****************************/
1030  /* ----------------------------------------------------------------- */
1031  /**
1032   * Return the list of ip addresses and classes that are allowed access to domain list
1033   * through AXFR Transfers from the bind server.
1034   */
1035  function enum_slave_ip() {
1036  global $db,$err;
1037  $db->query("SELECT * FROM slaveip;");
1038  if (!$db->next_record()) {
1039    return false;
1040  }
1041  do {
1042    $res[]=$db->Record;
1043  } while ($db->next_record());
1044  return $res;
1045  }
1046
1047  /* ----------------------------------------------------------------- */
1048  /**
1049   * Add an ip address (or a ip class) to the list of allowed slave ip access list.
1050   */
1051  function add_slave_ip($ip,$class="32") {
1052  global $db,$err;
1053  if (!checkip($ip)) {
1054    $err->raise("dom",19);
1055    return false;
1056  }
1057  $class=intval($class);
1058  if ($class<8 || $class>32) $class=32;
1059  $db->query("SELECT * FROM slaveip WHERE ip='$ip' AND class='$class';");
1060  if ($db->next_record()) {
1061    $err->raise("err",22);
1062    return false;
1063  }
1064  $db->query("INSERT INTO slaveip (ip,class) VALUES ('$ip','$class');");
1065  $f=fopen(SLAVE_FLAG,"w");
1066  fputs($f,"yopla");
1067  fclose($f); 
1068  return true;
1069  }
1070
1071  /* ----------------------------------------------------------------- */
1072  /**
1073   * Remove an ip address (or a ip class) from the list of allowed slave ip access list.
1074   */
1075  function del_slave_ip($ip) {
1076  global $db,$err;
1077  if (!checkip($ip)) {
1078    $err->raise("dom",19);
1079    return false;
1080  }
1081  $db->query("DELETE FROM slaveip WHERE ip='$ip'");
1082  $f=fopen(SLAVE_FLAG,"w");
1083  fputs($f,"yopla");
1084  fclose($f); 
1085  return true;
1086  }
1087
1088
1089
1090  /* ----------------------------------------------------------------- */
1091  /**
1092   * Check for a slave account
1093   */
1094  function check_slave_account($login,$pass) {
1095  global $db,$err;
1096  $db->query("SELECT * FROM slaveaccount WHERE login='$login' AND pass='$pass';");
1097  if ($db->next_record()) { 
1098    return true;
1099  }
1100  return false;
1101  }
1102
1103  /* ----------------------------------------------------------------- */
1104  /**
1105   * Out (echo) the complete hosted domain list :
1106   */
1107  function echo_domain_list() {
1108  global $db,$err;
1109  $db->query("SELECT domaine FROM domaines WHERE gesdns=1 ORDER BY domaine");
1110  while ($db->next_record()) {
1111    echo $db->f("domaine")."\n";
1112  }
1113  return true;
1114  }
1115
1116
1117  /* ----------------------------------------------------------------- */
1118  /**
1119   * Returns the complete hosted domain list :
1120   */
1121  function get_domain_list($uid=-1) {
1122  global $db,$err;
1123  $uid=intval($uid);
1124  $res=array();
1125  if ($uid!=-1) {
1126    $sql=" AND compte='$uid' ";
1127  }
1128  $db->query("SELECT domaine FROM domaines WHERE gesdns=1 $sql ORDER BY domaine");
1129  while ($db->next_record()) {
1130    $res[]=$db->f("domaine");
1131  }
1132  return $res;
1133  }
1134
1135
1136  /* ----------------------------------------------------------------- */
1137  /**
1138   * Return the list of allowed slave accounts
1139   */
1140  function enum_slave_account() {
1141  global $db,$err;
1142  $db->query("SELECT * FROM slaveaccount;");
1143  $res=array();
1144  while ($db->next_record()) {
1145    $res[]=$db->Record;
1146  }
1147  if (!count($res)) return false;
1148  return $res;
1149  }
1150
1151  /* ----------------------------------------------------------------- */
1152  /**
1153   * Add a slave account that will be allowed to access the domain list
1154   */
1155  function add_slave_account($login,$pass) {
1156  global $db,$err;
1157  $db->query("SELECT * FROM slaveaccount WHERE login='$login'");
1158  if ($db->next_record()) {
1159    $err->raise("err",23);
1160    return false;
1161  }
1162  $db->query("INSERT INTO slaveaccount (login,pass) VALUES ('$login','$pass')");
1163  return true;
1164  }
1165
1166  /* ----------------------------------------------------------------- */
1167  /**
1168   * Remove a slave account
1169   */
1170  function del_slave_account($login) {
1171  global $db,$err;
1172  $db->query("DELETE FROM slaveaccount WHERE login='$login'");
1173  return true;
1174  }
1175
1176  /*************/
1177  /*  Private  */
1178  /*************/
1179
1180
1181  /* ----------------------------------------------------------------- */
1182  /**
1183   * Lock tente de verrouiller le fichier lock du cron. Si tout va bien (toujours?)
1184   * retourne True, sinon retourne False
1185   * NOTE : le systeme de lock est asymétrique, si on a un fichier CRONLOCK, on
1186   * attends (que le cron ait fini son execution).
1187   * @access private
1188   */
1189  function lock() {
1190    global $db,$err;
1191    $err->log("dom","lock");
1192    if ($this->islocked) {
1193      $err->raise("dom",17);
1194    }
1195    while (file_exists($this->fic_lock_cron)) {
1196      sleep(2);
1197    }
1198    $this->islocked=true;
1199    return true;
1200  }
1201
1202  /* ----------------------------------------------------------------- */
1203  /**
1204   * unlock déverrouille le fichier lock du cron. Si tout va bien (toujours?)
1205   * retourne True, sinon retourne False
1206   * NOTE : actuellement, vu le système de lock asymetrique, on ne fait rien ;)
1207   * @access private
1208   */
1209  function unlock() {
1210    global $db,$err;
1211    $err->log("dom","unlock");
1212    if (!$this->islocked) {
1213      $err->raise("dom",3);
1214    }
1215    $this->islocked=false;
1216    return true;
1217  }
1218
1219
1220  /* ----------------------------------------------------------------- */
1221  /**
1222   * Declare that a domain's emails are hosted in this server :
1223   * This adds 2 MX entries in this domain (if required)
1224   */
1225  function alternc_add_mx_domain($domain) {
1226    global $err;
1227    $err->log("dom","alternc_add_mx_domain");
1228    $this->set_sub_domain($domain, '', $this->type_defmx, '');
1229    if (! empty($GLOBALS['L_DEFAULT_SECONDARY_MX'])) {
1230      $this->set_sub_domain($domain, '', $this->type_defmx2, '');
1231    }
1232    return true;
1233  }
1234
1235
1236  /* ----------------------------------------------------------------- */
1237  /**
1238   * Efface un compte (tous ses domaines)
1239   */
1240  function alternc_del_member() {
1241    global $err;
1242    $err->log("dom","alternc_del_member");
1243    $li=$this->enum_domains();
1244    foreach($li as $dom) {
1245      $this->del_domain($dom);
1246    }
1247    return true;
1248  }
1249
1250
1251  /* ----------------------------------------------------------------- */
1252  /**
1253   * Returns the used quota for the $name service for the current user.
1254   * @param $name string name of the quota
1255   * @return integer the number of service used or false if an error occured
1256   * @access private
1257   */
1258  function alternc_get_quota($name) {
1259    global $db,$err,$cuid;
1260    if ($name=="dom") {
1261      $err->log("dom","get_quota");
1262      $db->query("SELECT COUNT(*) AS cnt FROM domaines WHERE compte='$cuid'");
1263      $db->next_record();
1264      return $db->f("cnt");
1265    } else return false;
1266  }
1267
1268
1269  /* ----------------------------------------------------------------- */
1270  /**
1271   * Exporte toutes les informations domaine du compte.
1272   * @access private
1273   * EXPERIMENTAL 'sid' function ;)
1274   */
1275  function alternc_export() {
1276    global $db,$err;
1277    $err->log("dom","export");
1278    $this->enum_domains();
1279    $str="<dom>\n";
1280    foreach ($this->domains as $d) {
1281      $str.="  <domain>\n    <name>".xml_entities($d)."</name>\n";
1282      $s=$this->get_domain_all($d);
1283      $str.="    <hasdns>".xml_entities($s[dns])."</hasdns>\n";
1284      $str.="    <hasmx>".xml_entities($s[mx])."</hasmx>\n";
1285      $str.="    <mx>".xml_entities($s[mail])."</mx>\n";
1286      if (is_array($s[sub])) {
1287        foreach ($s[sub] as $sub) {
1288          $str.="    <subdomain>";
1289          $str.="<name>".xml_entities($sub[name])."</name>";
1290          $str.="<dest>".xml_entities($sub[dest])."</dest>";
1291          $str.="<type>".xml_entities($sub[type])."</type>";
1292          $str.="</subdomain>\n";
1293        }
1294      }
1295      $str.="  </domain>\n";
1296    }
1297    $str.="</dom>\n";
1298    return $str;
1299  }
1300
1301
1302} /* Class m_domains */
1303
1304?>
Note: See TracBrowser for help on using the repository browser.