import from private repository
[GitHub/Stricted/Domain-Control-Panel.git] / lib / page / RecordAddPage.class.php
1 <?php
2 namespace dns\page;
3 use dns\system\DNS;
4 use dns\system\User;
5 use dns\api\idna\idna_convert;
6
7 /**
8 * @author Jan Altensen (Stricted)
9 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
10 * @copyright 2014-2015 Jan Altensen (Stricted)
11 */
12 class RecordAddPage extends AbstractPage {
13 public $activeMenuItem = 'index';
14
15 public function prepare() {
16 if (!isset($_GET['id']) || empty($_GET['id'])) {
17 throw new \Exception('The link you are trying to reach is no longer available or invalid.', 404);
18 }
19
20 $soaIDs = User::getAccessibleDomains();
21 if (!in_array($_GET['id'], $soaIDs)) {
22 throw new \Exception('Access denied. You\92re not authorized to view this page.', 403);
23 }
24 $idna = new idna_convert();
25
26 $sql = "SELECT * FROM dns_soa WHERE id = ?";
27 $res = DNS::getDB()->query($sql, array($_GET['id']));
28 $soa = DNS::getDB()->fetch_array($res);
29
30 $soa['origin'] = $idna->decode($soa['origin']);
31
32 DNS::getTPL()->assign(array("soa" => $soa));
33
34 $types = array('A', 'AAAA', 'CNAME', 'MX', 'PTR', 'SRV', 'TXT', 'TLSA', 'NS', 'DS');
35 $error = array();
36 if (isset($_POST['submit']) && !empty($_POST['submit'])) {
37 if (isset($_POST['name']) && !empty($_POST['name']) && isset($_POST['ttl']) && !empty($_POST['ttl']) && isset($_POST['type']) && !empty($_POST['type']) && isset($_POST['data']) && !empty($_POST['data'])) {
38 $type = trim($_POST['type']);
39 $name = $idna->encode(trim($_POST['name']));
40 if (in_array($type, $types)) {
41 $aux = 0;
42 if (($type == "MX" || $type == "TLSA" || $type == "SRV" || $type == "DS") && isset($_POST['aux']) && !empty($_POST['aux'])) {
43 $aux = trim($_POST['aux']);
44 }
45
46 $data = trim($_POST['data']);
47 if ($type == "SRV" || $type == "DS") {
48 if (isset($_POST['weight']) && !empty($_POST['weight']) && isset($_POST['port']) && !empty($_POST['port'])) {
49 if ($type == "SRV") {
50 $data = $idna->encode($data);
51 }
52 $data = trim($_POST['weight']).' '.trim($_POST['port']).' '.$data;
53 }
54 else {
55 $error = array_merge($error, array('weight', 'port', 'data'));
56 }
57 }
58
59 $ttl = $_POST['ttl'];
60 if ($ttl < DNS_SOA_MINIMUM_TTL) {
61 $ttl = DNS_SOA_MINIMUM_TTL;
62 }
63
64 if ($type == "TLSA") {
65 if ($aux != 3) {
66 // fallback
67 $aux = 3;
68 }
69
70 if (isset($_POST['weight']) && isset($_POST['port'])) {
71 if (!is_numeric($_POST['weight'])) {
72 $error = array_merge($error, array('weight'));
73 }
74 else if (!is_numeric($_POST['port'])) {
75 $error = array_merge($error, array('weight'));
76 }
77 else if (strlen($_POST['data']) != 64) {
78 $error = array_merge($error, array('data'));
79 }
80 else {
81 $data = trim($_POST['weight']).' '.trim($_POST['port']).' '.$data;
82 }
83 }
84 else {
85 $error = array_merge($error, array('weight', 'port', 'data'));
86 }
87 }
88
89 if ($type == "A") {
90 if (filter_var($data, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
91 $error = array_merge($error, array('data'));
92 }
93 }
94 else if ($type == "AAAA") {
95 if (filter_var($data, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
96 $error = array_merge($error, array('data'));
97 }
98 }
99 }
100 else {
101 $error = array_merge($error, array('type'));
102 }
103 }
104 else {
105 $error = array_merge($error, array('name', 'ttl', 'data'));
106 }
107
108 $sql = 'SELECT * FROM dns_rr WHERE zone = ? AND name = ? AND type = ? AND data = ?';
109 $res = DNS::getDB()->query($sql, array($_GET['id'], $name, $type, $data));
110 $rr = DNS::getDB()->fetch_array($res);
111 if (!empty($rr)) {
112 $error = array_merge($error, array('name', 'type', 'data'));
113 }
114
115 if (empty($error)) {
116 $sql = 'INSERT INTO dns_rr (id, zone, name, type, data, aux, ttl) VALUES (NULL, ?, ?, ?, ?, ?, ?)';
117 if ($type == "SRV" || $type == "DS" || $type == "TLSA") {
118 DNS::getDB()->query($sql, array($_GET['id'], $name, $type, $data, $aux, $ttl));
119 }
120 else {
121 DNS::getDB()->query($sql, array($_GET['id'], $name, $type, $idna->encode($data), $aux, $ttl));
122 }
123
124 $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
125 DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soa['id']));
126 }
127 else {
128 if ($type == "SRV" || $type == "DS" || $type == "TLSA") {
129 DNS::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'weight' => $_POST['weight'], 'port' => $_POST['port'], 'data' => $_POST['data'], 'aux' => $aux, 'ttl' => $ttl));
130 }
131 else {
132 DNS::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl));
133 }
134 }
135 }
136
137 DNS::getTPL()->assign(array("error" => $error));
138 }
139
140 public function fixSerial ($old) {
141 if (substr($old, 0, -2) == date("Ymd")) {
142 $new = $old + 1;
143 }
144 else {
145 $new = date("Ymd")."01";
146 }
147
148 return $new;
149 }
150 }