Commit | Line | Data |
---|---|---|
6706658b S |
1 | <?php |
2 | namespace dns\page; | |
01c0ad42 | 3 | use dns\system\api\idna\idna_convert; |
6706658b S |
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> | |
d4779364 | 10 | * @copyright 2014-2016 Jan Altensen (Stricted) |
6706658b S |
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)) { | |
580f0b08 | 22 | throw new \Exception('Access denied. You\'re not authorized to view this page.', 403); |
6706658b S |
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']) && isset($_POST['ttl']) && !empty($_POST['ttl']) && isset($_POST['type']) && !empty($_POST['type']) && isset($_POST['data']) && !empty($_POST['data'])) { | |
38 | $type = trim($_POST['type']); | |
39 | ||
40 | if (!empty($_POST['name'])) { | |
41 | $name = $idna->encode(trim($_POST['name'])); | |
42 | } | |
43 | else { | |
44 | $name = $idna->encode(trim($soa['origin'])); | |
45 | } | |
46 | ||
47 | if (in_array($type, $types)) { | |
48 | $aux = 0; | |
49 | if (($type == "MX" || $type == "TLSA" || $type == "SRV" || $type == "DS") && isset($_POST['aux']) && !empty($_POST['aux'])) { | |
50 | $aux = trim($_POST['aux']); | |
51 | } | |
52 | ||
53 | $data = trim($_POST['data']); | |
54 | if ($type == "SRV" || $type == "DS") { | |
55 | if (isset($_POST['weight']) && !empty($_POST['weight']) && isset($_POST['port']) && !empty($_POST['port'])) { | |
56 | if ($type == "SRV") { | |
57 | $data = $idna->encode($data); | |
58 | } | |
59 | $data = trim($_POST['weight']).' '.trim($_POST['port']).' '.$data; | |
60 | } | |
61 | else { | |
62 | $error = array_merge($error, array('weight', 'port', 'data')); | |
63 | } | |
64 | } | |
65 | ||
66 | $ttl = $_POST['ttl']; | |
67 | if ($ttl < DNS_SOA_MINIMUM_TTL) { | |
68 | $ttl = DNS_SOA_MINIMUM_TTL; | |
69 | } | |
70 | ||
71 | if ($type == "TLSA") { | |
72 | if ($aux != 3) { | |
73 | // fallback | |
74 | $aux = 3; | |
75 | } | |
76 | ||
77 | if (isset($_POST['weight']) && isset($_POST['port'])) { | |
78 | if (!is_numeric($_POST['weight'])) { | |
79 | $error = array_merge($error, array('weight')); | |
80 | } | |
81 | else if (!is_numeric($_POST['port'])) { | |
82 | $error = array_merge($error, array('weight')); | |
83 | } | |
84 | else if (strlen($_POST['data']) != 64) { | |
85 | $error = array_merge($error, array('data')); | |
86 | } | |
87 | else { | |
88 | $data = trim($_POST['weight']).' '.trim($_POST['port']).' '.$data; | |
89 | } | |
90 | } | |
91 | else { | |
92 | $error = array_merge($error, array('weight', 'port', 'data')); | |
93 | } | |
94 | } | |
95 | ||
96 | if ($type == "A") { | |
97 | if (filter_var($data, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { | |
98 | $error = array_merge($error, array('data')); | |
99 | } | |
100 | } | |
101 | else if ($type == "AAAA") { | |
102 | if (filter_var($data, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) { | |
103 | $error = array_merge($error, array('data')); | |
104 | } | |
105 | } | |
106 | } | |
107 | else { | |
108 | $error = array_merge($error, array('type')); | |
109 | } | |
110 | } | |
111 | else { | |
112 | $error = array_merge($error, array('name', 'ttl', 'data')); | |
113 | } | |
114 | ||
115 | $sql = 'SELECT * FROM dns_rr WHERE zone = ? AND name = ? AND type = ? AND data = ?'; | |
116 | $res = DNS::getDB()->query($sql, array($_GET['id'], $name, $type, $data)); | |
117 | $rr = DNS::getDB()->fetch_array($res); | |
118 | if (!empty($rr)) { | |
119 | $error = array_merge($error, array('type', 'data')); | |
120 | } | |
121 | ||
122 | if (empty($error)) { | |
123 | $sql = 'INSERT INTO dns_rr (id, zone, name, type, data, aux, ttl) VALUES (NULL, ?, ?, ?, ?, ?, ?)'; | |
124 | if ($type == "SRV" || $type == "DS" || $type == "TLSA") { | |
125 | DNS::getDB()->query($sql, array($_GET['id'], $name, $type, $data, $aux, $ttl)); | |
126 | } | |
127 | else { | |
128 | DNS::getDB()->query($sql, array($_GET['id'], $name, $type, $idna->encode($data), $aux, $ttl)); | |
129 | } | |
130 | ||
131 | $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?"; | |
132 | DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soa['id'])); | |
133 | DNS::getTPL()->assign(array('success' => true)); | |
134 | } | |
135 | else { | |
136 | if ($type == "SRV" || $type == "DS" || $type == "TLSA") { | |
137 | DNS::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'weight' => $_POST['weight'], 'port' => $_POST['port'], 'data' => $_POST['data'], 'aux' => $aux, 'ttl' => $ttl)); | |
138 | } | |
139 | else { | |
140 | DNS::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl)); | |
141 | } | |
142 | } | |
143 | } | |
144 | ||
145 | DNS::getTPL()->assign(array("error" => $error)); | |
146 | } | |
147 | ||
148 | public function fixSerial ($old) { | |
149 | if (substr($old, 0, -2) == date("Ymd")) { | |
150 | $new = $old + 1; | |
151 | } | |
152 | else { | |
153 | $new = date("Ymd")."01"; | |
154 | } | |
155 | ||
156 | return $new; | |
157 | } | |
158 | } |