update copyright year
[GitHub/Stricted/Domain-Control-Panel.git] / lib / page / DomainAddPage.class.php
1 <?php
2 namespace dns\page;
3 use dns\system\api\idna\idna_convert;
4 use dns\system\DNS;
5 use dns\system\User;
6
7 /**
8 * @author Jan Altensen (Stricted)
9 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
10 * @copyright 2014-2016 Jan Altensen (Stricted)
11 */
12 class DomainAddPage extends AbstractPage {
13 public $activeMenuItem = 'add';
14
15 public function prepare() {
16 if (User::isReseller() === false) {
17 throw new \Exeption('Forbidden', 403);
18 }
19 if (isset($_POST['origin']) && isset($_POST['submit'])) {
20 if (!empty($_POST['origin'])) {
21 $idna = new idna_convert();
22 $origin = $_POST['origin'];
23 if (substr($origin, -1) != ".") {
24 $origin = $origin.".";
25 }
26
27 $origin = $idna->encode($origin);
28
29 $serial = date("Ymd")."01";
30
31 $sql = "SELECT * FROM dns_soa WHERE origin = ?";
32 $res = DNS::getDB()->query($sql, array($origin));
33 $soa = DNS::getDB()->fetch_array($res);
34
35 if (empty($soa)) {
36 $soaData = array($origin, DNS_SOA_NS, DNS_SOA_MBOX, $serial, DNS_SOA_REFRESH, DNS_SOA_RETRY, DNS_SOA_EXPIRE, DNS_SOA_MINIMUM_TTL, DNS_SOA_TTL, 1);
37
38 $sql = "INSERT INTO dns_soa (id, origin, ns, mbox, serial, refresh, retry, expire, minimum, ttl, active) VALUES (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
39 DNS::getDB()->query($sql, $soaData);
40 $soaID = DNS::getDB()->last_id();
41
42 $sql = "INSERT INTO dns_soa_to_user (id, userID, soaID) VALUES (null, ?, ?)";
43 DNS::getDB()->query($sql, array(DNS::getSession()->userID, $soaID));
44
45 $sql = "SELECT * FROM dns_template WHERE userID = ?";
46 $res = DNS::getDB()->query($sql, array(DNS::getSession()->userID));
47 $tpl = DNS::getDB()->fetch_array($res);
48
49 $records = array();
50 if (!empty($tpl) && !empty($tpl['template'])) {
51 $records = explode("\n", $tpl['template']);
52 }
53 else {
54 $records = explode("\n", DNS_DEFAULT_RECORDS);
55 }
56
57 if (!empty($records)) {
58 foreach ($records as $record) {
59 $record = str_replace("{domain}", $origin, $record);
60 $record = explode(":", $record, 3);
61
62 $rrData = array($soaID, $record[0], $record[1], $record[2], ($record[1] == "MX" ? 10 : 0), DNS_SOA_MINIMUM_TTL);
63 $sql = 'INSERT INTO dns_rr (id, zone, name, type, data, aux, ttl) VALUES (NULL, ?, ?, ?, ?, ?, ?)';
64 DNS::getDB()->query($sql, $rrData);
65 }
66 }
67 DNS::getTPL()->assign(array("error" => '', 'success' => true));
68 }
69 else {
70 DNS::getTPL()->assign(array("error" => 'origin', 'origin' => $_POST['origin']));
71 }
72 }
73 else {
74 DNS::getTPL()->assign(array("error" => 'origin'));
75 }
76 }
77 else {
78 DNS::getTPL()->assign(array("error" => ''));
79 }
80 }
81 }