5 use dns\util\ParseZone
;
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)
12 class ActionPage
extends AbstractPage
{
13 public function prepare() {
14 if (!isset($_POST['action']) ||
empty($_POST['action']) ||
!isset($_POST['dataID'])) {
19 $action = trim($_POST['action']);
20 $dataID = intval(trim($_POST['dataID']));
21 if ($action == "toggleDomain") {
22 if (User
::isReseller() === false) {
27 $soaIDs = User
::getAccessibleDomains();
28 if (!in_array($dataID, $soaIDs)) {
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);
37 $active = ($soa['active'] ?
0 : 1);
39 $sql = "UPDATE dns_soa SET active = ?, serial = ? WHERE id = ?";
40 DNS
::getDB()->query($sql, array($active, $this->fixSerial($soa['serial']), $dataID));
45 else if ($action == "deleteDomain") {
46 if (User
::isReseller() === false) {
51 $soaIDs = User
::getAccessibleDomains();
52 if (!in_array($dataID, $soaIDs)) {
57 $sql = "DELETE FROM dns_soa WHERE id = ?";
58 DNS
::getDB()->query($sql, array($dataID));
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);
69 $soaIDs = User
::getAccessibleDomains();
70 if (!in_array($soaID, $soaIDs)) {
75 $sql = "SELECT active FROM dns_rr WHERE id = ?";
76 $res = DNS
::getDB()->query($sql, array($dataID));
77 $rr = DNS
::getDB()->fetch_array($res);
79 $active = ($rr['active'] ?
0 : 1);
81 $sql = "UPDATE dns_rr SET active = ? WHERE id = ?";
82 DNS
::getDB()->query($sql, array($active, $dataID));
84 $sql = "SELECT serial FROM dns_soa WHERE id = ?";
85 $res = DNS
::getDB()->query($sql, array($soaID));
86 $soa = DNS
::getDB()->fetch_array($res);
88 $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
89 DNS
::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID));
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);
100 $soaIDs = User
::getAccessibleDomains();
101 if (!in_array($soaID, $soaIDs)) {
106 $sql = "DELETE FROM dns_rr WHERE id = ?";
107 DNS
::getDB()->query($sql, array($dataID));
109 $sql = "SELECT serial FROM dns_soa WHERE id = ?";
110 $res = DNS
::getDB()->query($sql, array($soaID));
111 $soa = DNS
::getDB()->fetch_array($res);
113 $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
114 DNS
::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID));
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'];
125 $soaIDs = User
::getAccessibleDomains();
126 if (!in_array($soaID, $soaIDs)) {
131 $sql = "SELECT active FROM dns_sec WHERE id = ?";
132 $res = DNS
::getDB()->query($sql, array($dataID));
133 $rr = DNS
::getDB()->fetch_array($res);
135 $active = ($rr['active'] ?
0 : 1);
137 $sql = "UPDATE dns_sec SET active = ? WHERE id = ?";
138 DNS
::getDB()->query($sql, array($active, $dataID));
140 $sql = "SELECT serial FROM dns_soa WHERE id = ?";
141 $res = DNS
::getDB()->query($sql, array($soaID));
142 $soa = DNS
::getDB()->fetch_array($res);
144 $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
145 DNS
::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID));
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'];
156 $soaIDs = User
::getAccessibleDomains();
157 if (!in_array($soaID, $soaIDs)) {
162 $sql = "DELETE FROM dns_sec WHERE id = ?";
163 DNS
::getDB()->query($sql, array($dataID));
165 $sql = "SELECT serial FROM dns_soa WHERE id = ?";
166 $res = DNS
::getDB()->query($sql, array($soaID));
167 $soa = DNS
::getDB()->fetch_array($res);
169 $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
170 DNS
::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID));
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);
182 $apiKey = DNS
::generateUUID();
184 $sql = "INSERT INTO dns_api (id, userID, apiKey) VALUES (NULL, ?, ?)";
185 DNS
::getDB()->query($sql, array(DNS
::getSession()->userID
, $apiKey));
192 else if ($action == "import") {
193 if (isset($_POST['zone']) && !empty($_POST['zone'])) {
195 if (isset($_POST['origin']) && !empty($_POST['origin'])) {
197 if (User::isReseller() === false) {
206 $soaIDs = User
::getAccessibleDomains();
207 if (!in_array($dataID, $soaIDs)) {
212 $sql = 'SELECT * FROM dns_soa where id = ?';
213 $res = DNS
::getDB()->query($sql, array($dataID));
214 $res = DNS
::getDB()->fetch_array($res);
217 $parser = new ParseZone($_POST['zone'], $soa['origin']);
222 catch (\Exception
$e) {
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'])) {
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);
250 $soaIDs = User
::getAccessibleDomains();
251 if (!in_array($soa['id'], $soaIDs)) {
256 $out = ";; Domain:\t".$soa['origin']."\n";
257 $out .= ";; Exported:\t".date("Y-m-d H:i:s")."\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";
266 $out .= ";; For further information, please consult the BIND documentation\n";
267 $out .= ";; located on the following website:\n";
269 $out .= ";; http://www.isc.org/\n";
271 $out .= ";; And RFC 1035:\n";
273 $out .= ";; http://www.ietf.org/rfc/rfc1035.txt\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";
279 $out .= ";;\tUse at your own risk.\n";
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";
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']) {
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";
300 else if ($record['type'] == "TXT") {
301 $txt = $record['data'];
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.'"';
311 if (strpos($record['data'], "v=spf1") !== false) {
312 $out .= $record['name']."\t".$record['ttl']."\tIN\tSPF\t" . $record['data']."\n";
315 $out .= $record['name']."\t".$record['ttl']."\tIN\t".$record['type']."\t" . $record['data']."\n";
318 $out .= $record['name']."\t".$record['ttl']."\tIN\t".$record['type']."\t\t" . $record['data']."\n";
330 public function fixSerial ($old) {
331 if (substr($old, 0, -2) == date("Ymd")) {
335 $new = date("Ymd")."01";