update copyright year
[GitHub/Stricted/Domain-Control-Panel.git] / lib / page / ActionPage.class.php
1 <?php
2 namespace dns\page;
3 use dns\system\DNS;
4 use dns\system\User;
5 use dns\util\ParseZone;
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 ActionPage extends AbstractPage {
13 public function prepare() {
14 if (!isset($_POST['action']) || empty($_POST['action']) || !isset($_POST['dataID'])) {
15 echo "failure";
16 exit;
17 }
18
19 $action = trim($_POST['action']);
20 $dataID = intval(trim($_POST['dataID']));
21 if ($action == "toggleDomain") {
22 if (User::isReseller() === false) {
23 echo "failure";
24 exit;
25 }
26
27 $soaIDs = User::getAccessibleDomains();
28 if (!in_array($dataID, $soaIDs)) {
29 echo "failure";
30 exit;
31 }
32
33 $sql = "SELECT active, serial FROM dns_soa WHERE id = ?";
34 $res = DNS::getDB()->query($sql, array($dataID));
35 $soa = DNS::getDB()->fetch_array($res);
36
37 $active = ($soa['active'] ? 0 : 1);
38
39 $sql = "UPDATE dns_soa SET active = ?, serial = ? WHERE id = ?";
40 DNS::getDB()->query($sql, array($active, $this->fixSerial($soa['serial']), $dataID));
41
42 echo "success";
43 exit;
44 }
45 else if ($action == "deleteDomain") {
46 if (User::isReseller() === false) {
47 echo "failure";
48 exit;
49 }
50
51 $soaIDs = User::getAccessibleDomains();
52 if (!in_array($dataID, $soaIDs)) {
53 echo "failure";
54 exit;
55 }
56
57 $sql = "DELETE FROM dns_soa WHERE id = ?";
58 DNS::getDB()->query($sql, array($dataID));
59
60 echo "success";
61 exit;
62 }
63 else if ($action == "toggleRecord") {
64 $sql = "SELECT zone FROM dns_rr WHERE id = ?";
65 $res = DNS::getDB()->query($sql, array($dataID));
66 $rr = DNS::getDB()->fetch_array($res);
67 $soaID = $rr['zone'];
68
69 $soaIDs = User::getAccessibleDomains();
70 if (!in_array($soaID, $soaIDs)) {
71 echo "failure";
72 exit;
73 }
74
75 $sql = "SELECT active FROM dns_rr WHERE id = ?";
76 $res = DNS::getDB()->query($sql, array($dataID));
77 $rr = DNS::getDB()->fetch_array($res);
78
79 $active = ($rr['active'] ? 0 : 1);
80
81 $sql = "UPDATE dns_rr SET active = ? WHERE id = ?";
82 DNS::getDB()->query($sql, array($active, $dataID));
83
84 $sql = "SELECT serial FROM dns_soa WHERE id = ?";
85 $res = DNS::getDB()->query($sql, array($soaID));
86 $soa = DNS::getDB()->fetch_array($res);
87
88 $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
89 DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID));
90
91 echo "success";
92 exit;
93 }
94 else if ($action == "deleteRecord") {
95 $sql = "SELECT zone FROM dns_rr WHERE id = ?";
96 $res = DNS::getDB()->query($sql, array($dataID));
97 $rr = DNS::getDB()->fetch_array($res);
98 $soaID = $rr['zone'];
99
100 $soaIDs = User::getAccessibleDomains();
101 if (!in_array($soaID, $soaIDs)) {
102 echo "failure";
103 exit;
104 }
105
106 $sql = "DELETE FROM dns_rr WHERE id = ?";
107 DNS::getDB()->query($sql, array($dataID));
108
109 $sql = "SELECT serial FROM dns_soa WHERE id = ?";
110 $res = DNS::getDB()->query($sql, array($soaID));
111 $soa = DNS::getDB()->fetch_array($res);
112
113 $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
114 DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID));
115
116 echo "success";
117 exit;
118 }
119 else if ($action == "toggleSec") {
120 $sql = "SELECT zone FROM dns_sec WHERE id = ?";
121 $res = DNS::getDB()->query($sql, array($dataID));
122 $rr = DNS::getDB()->fetch_array($res);
123 $soaID = $rr['zone'];
124
125 $soaIDs = User::getAccessibleDomains();
126 if (!in_array($soaID, $soaIDs)) {
127 echo "failure";
128 exit;
129 }
130
131 $sql = "SELECT active FROM dns_sec WHERE id = ?";
132 $res = DNS::getDB()->query($sql, array($dataID));
133 $rr = DNS::getDB()->fetch_array($res);
134
135 $active = ($rr['active'] ? 0 : 1);
136
137 $sql = "UPDATE dns_sec SET active = ? WHERE id = ?";
138 DNS::getDB()->query($sql, array($active, $dataID));
139
140 $sql = "SELECT serial FROM dns_soa WHERE id = ?";
141 $res = DNS::getDB()->query($sql, array($soaID));
142 $soa = DNS::getDB()->fetch_array($res);
143
144 $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
145 DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID));
146
147 echo "success";
148 exit;
149 }
150 else if ($action == "deleteSec") {
151 $sql = "SELECT zone FROM dns_sec WHERE id = ?";
152 $res = DNS::getDB()->query($sql, array($dataID));
153 $rr = DNS::getDB()->fetch_array($res);
154 $soaID = $rr['zone'];
155
156 $soaIDs = User::getAccessibleDomains();
157 if (!in_array($soaID, $soaIDs)) {
158 echo "failure";
159 exit;
160 }
161
162 $sql = "DELETE FROM dns_sec WHERE id = ?";
163 DNS::getDB()->query($sql, array($dataID));
164
165 $sql = "SELECT serial FROM dns_soa WHERE id = ?";
166 $res = DNS::getDB()->query($sql, array($soaID));
167 $soa = DNS::getDB()->fetch_array($res);
168
169 $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
170 DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID));
171
172 echo "success";
173 exit;
174 }
175 else if ($action == "requestApiKey") {
176 if (User::isLoggedIn()) {
177 $sql = "SELECT * FROM dns_api WHERE userID = ?";
178 $res = DNS::getDB()->query($sql, array(DNS::getSession()->userID));
179 $row = DNS::getDB()->fetch_array($res);
180
181 if (empty($row)) {
182 $apiKey = DNS::generateUUID();
183
184 $sql = "INSERT INTO dns_api (id, userID, apiKey) VALUES (NULL, ?, ?)";
185 DNS::getDB()->query($sql, array(DNS::getSession()->userID, $apiKey));
186
187 echo $apiKey;
188 exit;
189 }
190 }
191 }
192 else if ($action == "import") {
193 if (isset($_POST['zone']) && !empty($_POST['zone'])) {
194 if ($dataID == 0) {
195 if (isset($_POST['origin']) && !empty($_POST['origin'])) {
196 /*
197 if (User::isReseller() === false) {
198 echo "failure";
199 exit;
200 }
201 */
202 // new zone
203 }
204 }
205 else {
206 $soaIDs = User::getAccessibleDomains();
207 if (!in_array($dataID, $soaIDs)) {
208 echo "failure";
209 exit;
210 }
211
212 $sql = 'SELECT * FROM dns_soa where id = ?';
213 $res = DNS::getDB()->query($sql, array($dataID));
214 $res = DNS::getDB()->fetch_array($res);
215 $soa = $res;
216
217 $parser = new ParseZone($_POST['zone'], $soa['origin']);
218
219 try {
220 $parser->parse();
221 }
222 catch (\Exception $e) {
223 echo "failure";
224 exit;
225 }
226
227 $data = $parser->getParsedData();
228 if (!empty($data['rr'])) {
229 // delete existing records
230 foreach ($data['rr'] as $rr) {
231 // dont update the default ns entrys, we add them automatically, all other ns entrys will be updated
232 if (strtolower($rr['type']) != "ns" && strtolower($rr['name']) != strtolower($soa['origin'])) {
233 // import data
234 }
235 }
236 }
237 else {
238 echo "failure";
239 exit;
240 }
241 }
242 }
243 }
244 else if ($action == "export") {
245 $sql = 'SELECT * FROM dns_soa where id = ?';
246 $res = DNS::getDB()->query($sql, array($dataID));
247 $res = DNS::getDB()->fetch_array($res);
248 $soa = $res;
249
250 $soaIDs = User::getAccessibleDomains();
251 if (!in_array($soa['id'], $soaIDs)) {
252 echo "failure";
253 exit;
254 }
255
256 $out = ";; Domain:\t".$soa['origin']."\n";
257 $out .= ";; Exported:\t".date("Y-m-d H:i:s")."\n";
258 $out .= ";; \n";
259 $out .= ";; This file is intended for use for informational and archival\n";
260 $out .= ";; purposes ONLY and MUST be edited before use on a production\n";
261 $out .= ";; DNS server. In particular, you must:\n";
262 $out .= ";; -- update the SOA record with the correct authoritative name server\n";
263 $out .= ";; -- update the SOA record with the contact e-mail address information\n";
264 $out .= ";; -- update the NS record(s) with the authoritative name servers for this domain.\n";
265 $out .= ";; \n";
266 $out .= ";; For further information, please consult the BIND documentation\n";
267 $out .= ";; located on the following website:\n";
268 $out .= ";; \n";
269 $out .= ";; http://www.isc.org/\n";
270 $out .= ";; \n";
271 $out .= ";; And RFC 1035:\n";
272 $out .= ";; \n";
273 $out .= ";; http://www.ietf.org/rfc/rfc1035.txt\n";
274 $out .= ";; \n";
275 $out .= ";; Please note that we do NOT offer technical support for any use\n";
276 $out .= ";; of this zone data, the BIND name server, or any other third-party\n";
277 $out .= ";; DNS software.\n";
278 $out .= ";; \n";
279 $out .= ";;\tUse at your own risk.\n";
280 $out .= ";; \n";
281
282 $out .= $soa['origin']."\t".$soa['minimum']."\tIN\tSOA\t".$soa['ns']."\t".$soa['mbox']."\t(\n";
283 $out .= "\t\t".$soa['serial']."\t; Serial\n";
284 $out .= "\t\t".$soa['refresh']."\t\t; Refresh\n";
285 $out .= "\t\t".$soa['retry']."\t\t; Retry\n";
286 $out .= "\t\t".$soa['expire']."\t\t; Expire\n";
287 $out .= "\t\t180 )\t\t; Negative Cache TTL\n";
288 $out .= ";;\n";
289
290 $sql = 'SELECT * FROM dns_rr where zone = ?';
291 $res = DNS::getDB()->query($sql, array($soa['id']));
292 while ($record = DNS::getDB()->fetch_array($res)) {
293 if (!$record['active']) {
294 $out .= ";; ";
295 }
296
297 if ($record['type'] == "MX" || $record['type'] == "SRV" || $record['type'] == "TLSA" || $record['type'] == "DS") {
298 $out .= $record['name']."\t".$record['ttl']."\tIN\t".$record['type']."\t".$record['aux']."\t".$record['data']."\n";
299 }
300 else if ($record['type'] == "TXT") {
301 $txt = $record['data'];
302
303 if (strpos($txt, " ") !== false) {
304 if (substr($txt, -1) != '"' && substr($txt, 0, 1) != '"') {
305 if (substr($txt, -1) != "'" && substr($txt, 0, 1) != "'") {
306 $record['data'] = '"'.$txt.'"';
307 }
308 }
309 }
310
311 if (strpos($record['data'], "v=spf1") !== false) {
312 $out .= $record['name']."\t".$record['ttl']."\tIN\tSPF\t" . $record['data']."\n";
313 }
314
315 $out .= $record['name']."\t".$record['ttl']."\tIN\t".$record['type']."\t" . $record['data']."\n";
316 }
317 else {
318 $out .= $record['name']."\t".$record['ttl']."\tIN\t".$record['type']."\t\t" . $record['data']."\n";
319 }
320 }
321
322 echo $out;
323 exit;
324 }
325
326 echo "failure";
327 exit;
328 }
329
330 public function fixSerial ($old) {
331 if (substr($old, 0, -2) == date("Ymd")) {
332 $new = $old + 1;
333 }
334 else {
335 $new = date("Ymd")."01";
336 }
337
338 return $new;
339 }
340 }