modules/domains/include/domains.php
e4b92297
 <?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.
 */
e4b92297
 
 require_once('inc/debug.php');
 
9afc4f3e
 
e4b92297
 function mailman_subdomains($domain)
 {
2626dd47
     if (! in_array('mailman', config('modules'))) {
         return array();
     }
     $domain = (int) $domain;
     $result = db_query("SELECT id, hostname FROM mail.mailman_domains WHERE domain=?", array($domain));
     $ret = array();
     while ($line = $result->fetch()) {
         $ret[] = $line;
     }
     return $ret;
e4b92297
 }
 
b2eb0cbe
 function dns_in_use($domain)
 {
2626dd47
     if (! in_array('dns', config('modules'))) {
         return false;
     }
     $domain = (int) $domain;
     $result = db_query("SELECT id FROM dns.custom_records WHERE domain=?", array($domain));
     return ($result->rowCount() > 0);
b2eb0cbe
 }
 
 
 function mail_in_use($domain)
 {
2626dd47
     if (! in_array('email', config('modules'))) {
         return false;
     }
     $domain = (int) $domain;
     $result = db_query("SELECT mail FROM kundendaten.domains WHERE id=?", array($domain));
     if ($result->rowCount() < 1) {
         system_failure("Domain not found");
     }
     $d = $result->fetch();
     if ($d['mail'] == 'none') {
         return false;
     } // manually disabled
     $result = db_query("SELECT id FROM mail.virtual_mail_domains WHERE domain=?", array($domain));
     if ($result->rowCount() < 1) {
         return true;
     } // .courier
     $result = db_query("SELECT acc.id FROM mail.vmail_accounts acc LEFT JOIN mail.virtual_mail_domains dom ON (acc.domain=dom.id) WHERE dom.domain=?", array($domain));
     return ($result->rowCount() > 0);
b2eb0cbe
 }
 
2626dd47
 function count_vmail($domain)
 {
ecf9bd5a
     $domain = (int) $domain;
     $result = db_query("SELECT acc.id FROM mail.vmail_accounts acc LEFT JOIN mail.virtual_mail_domains dom ON (acc.domain=dom.id) WHERE dom.domain=?", array($domain));
     return $result->rowCount();
 }
 
b2eb0cbe
 function web_in_use($domain)
 {
2626dd47
     if (! in_array('vhosts', config('modules'))) {
         return false;
     }
b2eb0cbe
 
2626dd47
     $domain = (int) $domain;
b2eb0cbe
 
2626dd47
     $result = db_query("SELECT id FROM kundendaten.domains WHERE id=? AND webserver=1", array($domain));
     if ($result->rowCount() < 1) {
         return false;
     }
b2eb0cbe
 
2626dd47
     $result = db_query("SELECT id FROM vhosts.vhost WHERE domain=?", array($domain));
     $result2 = db_query("SELECT id FROM vhosts.alias WHERE domain=?", array($domain));
     return ($result->rowCount() > 0 || $result2->rowCount() > 0);
b2eb0cbe
 }
e4b92297
 
2626dd47
 function domain_ownerchange($fqdn, $owner, $admin_c)
740b2bce
 {
     $cid = (int) $_SESSION['customerinfo']['customerno'];
2626dd47
     $dom = new Domain($fqdn);
60ba2d3f
     db_query("UPDATE kundendaten.domains SET owner=?, admin_c=? WHERE id=? AND kunde=?", array($owner, $admin_c, $dom->id, $cid));
     if (update_possible($dom->id)) {
eb4750e8
         require_once('domainapi.php');
60ba2d3f
         DEBUG("Rufe Domain-API auf!");
         api_upload_domain($dom->fqdn);
eb4750e8
     }
740b2bce
 }
 
e4b92297
 
2626dd47
 function update_possible($domain)
 {
b7ec211c
     $dom = new Domain((int) $domain);
     if ($dom->provider != 'terions' || $dom->billing=='external') {
         // Domain nicht über uns verwaltet
60ba2d3f
         DEBUG("Domain nicht über uns verwaltet!");
b7ec211c
         return false;
     }
     $result = db_query("SELECT aenderung_eigentuemer, ruecksprache FROM misc.domainpreise WHERE tld=?", array($dom->tld));
     if ($result->rowCount() < 1) {
         // Endung nicht bei uns in der Liste erfasst
60ba2d3f
         DEBUG("Endung nicht in der Preisliste!");
b7ec211c
         return false;
     }
     $data = $result->fetch();
2626dd47
     if ($data['aenderung_eigentuemer'] != null || $data['ruecksprache'] == 'Y') {
60ba2d3f
         DEBUG("Endung hat Eigenheiten (ruecksprache=Y)");
b7ec211c
         // Endung mit speziellen Eigenheiten
         return false;
     }
60ba2d3f
     DEBUG("Änderungen sollten möglich sein: {$dom->fqdn}");
b7ec211c
     return true;
 }
 
2626dd47
 function unset_mailserver_lock($dom)
 {
f4cab189
     $id = $dom->id;
     db_query("UPDATE kundendaten.domains SET secret=NULL, mailserver_lock=0 WHERE id=?", array($id));
 }
 
2626dd47
 function create_domain_secret($dom)
 {
f4cab189
     $id = $dom->id;
     $secret = md5(random_string(20));
     db_query("UPDATE kundendaten.domains SET secret=? WHERE id=?", array($secret, $id));
     $dom->secret = $secret;
     return $secret;
 }
 
f915faad
 
2626dd47
 function get_auth_dns($domainname, $tld)
 {
     $domain=idn_to_ascii($domainname.".".$tld, 0, INTL_IDNA_VARIANT_UTS46);
f4cab189
 
2626dd47
     $resp = shell_exec('dig @a.root-servers.net. +noall +authority -t ns '.$tld.'.');
     $line = explode("\n", $resp, 2)[0];
     $NS = preg_replace("/^.*\\sIN\\s+NS\\s+(\\S+)$/", '\1', $line);
f4cab189
 
2626dd47
     $resp = shell_exec('dig @'.$NS.' -t ns '.$domain.'.');
     $lines = explode("\n", $resp);
9086c9ad
 
2626dd47
     $NS = null;
     $NS_IP = null;
     $sec = null;
     foreach ($lines as $l) {
         if (preg_match("/;; AUTHORITY SECTION:.*/", $l)) {
             $sec = 'auth';
         } elseif (preg_match("/;; ADDITIONAL SECTION:.*/", $l)) {
             $sec = 'add';
         }
         if ($sec == 'auth' && preg_match("/^.*\\sIN\\s+NS\\s+\\S+$/", $l)) {
             $NS = preg_replace("/^.*\\sIN\\s+NS\\s+(\\S+)\\.$/", '\1', $l);
         }
         if ($sec == 'add' && $NS && preg_match("/^.*\\sIN\\s+A\\s+\\S+$/", $l)) {
             $NS_IP = preg_replace("/^.*\\sIN\\s+A\\s+(\\S+)$/", '\1', $l);
         }
     }
     return array("$NS" => $NS_IP);
f915faad
 }
 
f4cab189
 
2626dd47
 function own_ns()
 {
02afaaa0
     $auth = dns_get_record(config('masterdomain'), DNS_NS);
     $own_ns = array();
     foreach ($auth as $ns) {
         $own_ns[] = $ns['target'];
     }
 
2626dd47
     return $own_ns;
02afaaa0
 }
 
 
 
 
f915faad
 function has_own_ns($domainname, $tld)
 {
2626dd47
     $nsdata = get_auth_dns($domainname, $tld);
     $NS = null;
     foreach ($nsdata as $host => $ip) {
         $NS=$host;
     }
     if (in_array($NS, own_ns())) {
         DEBUG('Domain hat unsere DNS-Server!');
         return true;
     }
     return false;
f915faad
 }
 
f4cab189
 
2626dd47
 function get_txt_record($hostname, $domainname, $tld)
 {
     $domain=idn_to_ascii($domainname.".".$tld, 0, INTL_IDNA_VARIANT_UTS46);
     $nsdata = get_auth_dns($domainname, $tld);
     $NS = null;
     foreach ($nsdata as $host => $ip) {
         $NS = $host;
         if ($ip) {
             $NS = $ip;
         }
     }
     DEBUG('dig @'.$NS.' +short -t txt '.$hostname.'.'.$domain.'.');
     $resp = shell_exec('dig @'.$NS.' +short -t txt '.$hostname.'.'.$domain.'.');
     $TXT = trim($resp, "\n \"");
     DEBUG($TXT);
     return $TXT;
f4cab189
 }
 
 
84e7f97b
 function list_useraccounts()
 {
2626dd47
     $customerno = (int) $_SESSION['customerinfo']['customerno'];
     $result = db_query("SELECT uid,username,name FROM system.useraccounts WHERE kunde=?", array($customerno));
     $ret = array();
     while ($item = $result->fetch()) {
         $ret[] = $item;
     }
     DEBUG($ret);
     return $ret;
84e7f97b
 }
 
 
2626dd47
 function change_user($domain, $uid)
5bd1e31b
 {
     $domain = new Domain($domain);
     $domain->ensure_customerdomain();
2626dd47
     $targetuser = null;
5bd1e31b
     $useraccounts = list_useraccounts();
     foreach ($useraccounts as $u) {
         if ($u['uid'] == $uid) {
             $targetuser = $u['uid'];
             break;
         }
     }
     if (! $targetuser) {
         system_failure("Ungültiger Useraccount!");
     }
     db_query("UPDATE kundendaten.domains SET useraccount=? WHERE id=?", array($targetuser, $domain->id));
 }
 
dd9ad0b0
 
2626dd47
 function get_domain_offer($tld)
84e7f97b
 {
2626dd47
     $tld = filter_input_hostname($tld);
     $cid = (int) $_SESSION['customerinfo']['customerno'];
84e7f97b
 
2626dd47
     $data = array("tld" => $tld);
84e7f97b
 
2626dd47
     $result = db_query("SELECT tld, gebuehr, `interval`, setup FROM misc.domainpreise_kunde WHERE kunde=:cid AND tld=:tld AND ruecksprache='N'", array(":cid" => $cid, ":tld" => $tld));
     if ($result->rowCount() != 1) {
         $result = db_query("SELECT tld, gebuehr, `interval`, setup FROM misc.domainpreise WHERE tld=:tld AND ruecksprache='N'", array(":tld" => $tld));
     }
     if ($result->rowCount() != 1) {
         return false;
     }
     $temp = $result->fetch();
     $data["gebuehr"] = $temp["gebuehr"];
     $data["interval"] = $temp["interval"];
     $data["setup"] = ($temp["setup"] ? $temp["setup"] : 0.0);
9086c9ad
 
2626dd47
     return $data;
84e7f97b
 }
 
a9f83204
 function set_domain_pretransfer($domain)
 {
     $cid = (int) $_SESSION['customerinfo']['customerno'];
     $uid = (int) $_SESSION['userinfo']['uid'];
     $domain = (int) $domain;
2626dd47
     db_query(
         "UPDATE kundendaten.domains SET status='pretransfer', dns=1 WHERE id=? AND kunde=?",
             array($domain, $cid)
     );
a9f83204
 }
 
84e7f97b
 
a9f83204
 
 function set_domain_prereg($domain)
dd9ad0b0
 {
     $cid = (int) $_SESSION['customerinfo']['customerno'];
     $uid = (int) $_SESSION['userinfo']['uid'];
a9f83204
     $domain = (int) $domain;
2626dd47
     db_query(
         "UPDATE kundendaten.domains SET status='prereg', dns=1 WHERE id=? AND kunde=?",
             array($domain, $cid)
     );
dd9ad0b0
 }
 
 
 function insert_domain_external($domain, $dns = false, $mail = true)
84e7f97b
 {
     $cid = (int) $_SESSION['customerinfo']['customerno'];
02afaaa0
     $uid = (int) $_SESSION['userinfo']['uid'];
492ec955
     require_once("domainapi.php");
     $info = api_domain_available($domain);
     if (in_array($info['status'], array('nameContainsForbiddenCharacter', 'suffixDoesNotExist'))) {
         system_failure("Diese Domain scheint ungültig zu sein!");
84e7f97b
     }
492ec955
     $tld = $info['domainSuffix'];
     $domainname = str_replace(".$tld", "", $info['domainNameUnicode']);
cad0f3e7
     logger(LOG_WARNING, 'modules/domains/include/domains', 'domains', 'Inserting external domain '.$info['domainNameUnicode']." DNS:{$dns} / Mail:{$mail}");
9086c9ad
 
dd9ad0b0
     db_query("INSERT INTO kundendaten.domains (status, kunde, useraccount, domainname, tld, billing, provider, dns, mail, mailserver_lock) VALUES 
         ('external', ?, ?, ?, ?, 'external', 'other', 0, ?, 1)", array($cid, $uid, $domainname, $tld, ($mail ? 'auto' : 'none')));
84e7f97b
     $id = db_insert_id();
     if ($dns) {
         db_query("UPDATE kundendaten.domains SET dns=1 WHERE id=?", array($id));
     }
dd9ad0b0
     if ($mail) {
         $vmailserver = (int) $_SESSION['userinfo']['server'];
         db_query("INSERT INTO mail.virtual_mail_domains (domain, server) VALUES (?, ?)", array($id, $vmailserver));
     }
84e7f97b
     return $id;
 }
 
7569c300
 function delete_domain($id)
 {
     $cid = (int) $_SESSION['customerinfo']['customerno'];
cad0f3e7
     logger(LOG_WARNING, 'modules/domains/include/domains', 'domains', 'Deleting domain '.$id);
2626dd47
     db_query("DELETE FROM kundendaten.domains WHERE id=? AND kunde=?", array($id, $cid));
7569c300
 }