class/domain.php
e4c9511b
 <?php
c208bd90
 /*
 This file belongs to the Webinterface of schokokeks.org Hosting
 
cf54502a
 Written 2008-2018 by schokokeks.org Hosting, namely
c208bd90
   Bernd Wurst <bernd@schokokeks.org>
   Hanno Böck <hanno@schokokeks.org>
 
 To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
 
2626dd47
 You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see
c208bd90
 http://creativecommons.org/publicdomain/zero/1.0/
 
 Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
 */
e4c9511b
 
 require_once('inc/base.php');
 require_once('inc/debug.php');
 
 
fe1b7420
 class Domain
e4c9511b
 {
fe1b7420
     protected $data = array();
2626dd47
     public function __construct($init = null)
e4c9511b
     {
2626dd47
         $this->setup();
         switch (gettype($init)) {
e4c9511b
       case 'string':
         $this->loadByName($init);
         break;
       case 'integer':
         $this->loadByID($init);
         break;
       case 'NULL':
         break;
     }
fe1b7420
     }
2626dd47
 
     public function __set($key, $value)
     {
         if (array_key_exists($key, $this->data)) {
             $this->data[$key] = $value;
         } elseif (isset($this->$key)) {
             $this->$key = $value;
         } else {
             $this->data[$key] = $value;
         }
     }
 
 
     public function __get($key)
     {
         if (array_key_exists($key, $this->data)) {
             return $this->data[$key];
         } elseif (isset($this->$key)) {
             return $this->$key;
         }
         // else werfe fehler
     }
 
 
     public function loadByID($id)
     {
         $res = db_query("SELECT * FROM kundendaten.domains WHERE id=?", array($id));
         if ($res->rowCount() < 1) {
             return false;
         }
         $data = $res->fetch();
         $this->parse($data);
     }
 
     public function loadByName($name)
     {
         $raw = $name;
         $utf8 = idn_to_utf8($raw, 0, INTL_IDNA_VARIANT_UTS46);
         $res = db_query("SELECT * FROM kundendaten.domains WHERE CONCAT_WS('.', domainname, tld)=? OR CONCAT_WS('.', domainname, tld)=?", array($raw, $utf8));
         if ($res->rowCount() < 1) {
             return false;
         }
         $data = $res->fetch();
         $this->parse($data);
     }
 
     public function ensure_customerdomain()
     {
         if (! $this->is_customerdomain()) {
             system_failure('Diese Domain gehört nicht Ihrem Kundenaccount.');
         }
     }
 
     public function ensure_userdomain()
     {
         if (! $this->is_userdomain()) {
             system_failure('Diese Domain gehört nicht Ihrem Benutzeraccount.');
         }
     }
 
     public function is_customerdomain()
fe1b7420
     {
2626dd47
         if (! isset($_SESSION['customerinfo'])) {
             return false;
         }
         $customerno = (int) $_SESSION['customerinfo']['customerno'];
         return ($this->kunde == $customerno);
fe1b7420
     }
 
2626dd47
     public function is_userdomain()
     {
         if (! isset($_SESSION['userinfo'])) {
             return false;
         }
         $uid = (int) $_SESSION['userinfo']['uid'];
         return ($this->useraccount == $uid);
     }
 
     public function setup()
     {
         $fields = array();
         $res = db_query("DESCRIBE kundendaten.domains");
         while ($f = $res->fetch(PDO::FETCH_OBJ)) {
             $fields[$f->Field] = $f->Default;
         }
         $this->data = $fields;
         $this->data['id'] = null;
     }
fe1b7420
 
 
e4c9511b
 
2626dd47
     public function parse($data)
     {
         DEBUG($data);
         foreach (array_keys($this->data) as $key) {
             if (array_key_exists($key, $data)) {
                 $this->data[$key] = $data[$key];
             }
         }
         $this->data['fqdn'] = $data['domainname'].'.'.$data['tld'];
         $this->data['punycode'] = idn_to_ascii($this->data['fqdn'], 0, INTL_IDNA_VARIANT_UTS46);
         $this->data['is_idn'] = ($this->data['fqdn'] != $this->data['punycode']);
         $this->data['reg_date'] = $data['registrierungsdatum'];
         $this->data['cancel_date'] = $data['kuendigungsdatum'];
     }
e4c9511b
 }
 
 
 
 
 
 
 
2626dd47
 function get_domain_list($customerno, $uid = null)
e4c9511b
 {
2626dd47
     $customerno = (int) $customerno;
     $query = "SELECT id FROM kundendaten.domains WHERE";
     if ($uid !== null) {
         $uid = (int) $uid;
         $query .= " useraccount={$uid}";
     } else {
         $query .= " kunde={$customerno}";
     }
     $query .= " ORDER BY domainname,tld";
     $result = db_query($query, array()); // FIXME Übergebe leeren array um die Warnung zu unterdrücken
     $domains = array();
     DEBUG('Result set is '.$result->rowCount()." rows.<br />\n");
     if ($result->rowCount() > 0) {
         while ($domain = $result->fetch(PDO::FETCH_OBJ)) {
             array_push($domains, new Domain((int) $domain->id));
         }
     }
     DEBUG($domains);
     return $domains;
e4c9511b
 }
 
 
 
 function get_jabberable_domains()
 {
2626dd47
     require_role(ROLE_CUSTOMER);
     $customerno = (int) $_SESSION['customerinfo']['customerno'];
e4c9511b
 
2626dd47
     $domains = get_domain_list($customerno);
     DEBUG($domains);
     $result = array( new Domain() );
     $result[0]->id = 0;
     $result[0]->fqdn = config('masterdomain');
     foreach ($domains as $dom) {
         if ($dom->jabber) {
             $result[] = $dom;
         }
     }
     return $result;
e4c9511b
 }