3 use dns\system\api\idna\idna_convert
;
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 RecordAddPage
extends AbstractPage
{
13 public $activeMenuItem = 'index';
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);
20 $soaIDs = User
::getAccessibleDomains();
21 if (!in_array($_GET['id'], $soaIDs)) {
22 throw new \
Exception('Access denied. You\'re not authorized to view this page.', 403);
24 $idna = new idna_convert();
26 $sql = "SELECT * FROM dns_soa WHERE id = ?";
27 $res = DNS
::getDB()->query($sql, array($_GET['id']));
28 $soa = DNS
::getDB()->fetch_array($res);
30 $soa['origin'] = $idna->decode($soa['origin']);
32 DNS
::getTPL()->assign(array("soa" => $soa));
34 $types = array('A', 'AAAA', 'CNAME', 'MX', 'PTR', 'SRV', 'TXT', 'TLSA', 'NS', 'DS');
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']);
40 if (!empty($_POST['name'])) {
41 $name = $idna->encode(trim($_POST['name']));
44 $name = $idna->encode(trim($soa['origin']));
47 if (in_array($type, $types)) {
49 if (($type == "MX" ||
$type == "TLSA" ||
$type == "SRV" ||
$type == "DS") && isset($_POST['aux']) && !empty($_POST['aux'])) {
50 $aux = trim($_POST['aux']);
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'])) {
57 $data = $idna->encode($data);
59 $data = trim($_POST['weight']).' '.trim($_POST['port']).' '.$data;
62 $error = array_merge($error, array('weight', 'port', 'data'));
67 if ($ttl < DNS_SOA_MINIMUM_TTL
) {
68 $ttl = DNS_SOA_MINIMUM_TTL
;
71 if ($type == "TLSA") {
77 if (isset($_POST['weight']) && isset($_POST['port'])) {
78 if (!is_numeric($_POST['weight'])) {
79 $error = array_merge($error, array('weight'));
81 else if (!is_numeric($_POST['port'])) {
82 $error = array_merge($error, array('weight'));
84 else if (strlen($_POST['data']) != 64) {
85 $error = array_merge($error, array('data'));
88 $data = trim($_POST['weight']).' '.trim($_POST['port']).' '.$data;
92 $error = array_merge($error, array('weight', 'port', 'data'));
97 if (filter_var($data, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV4
) === false) {
98 $error = array_merge($error, array('data'));
101 else if ($type == "AAAA") {
102 if (filter_var($data, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV6
) === false) {
103 $error = array_merge($error, array('data'));
108 $error = array_merge($error, array('type'));
112 $error = array_merge($error, array('name', 'ttl', 'data'));
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);
119 $error = array_merge($error, array('type', 'data'));
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));
128 DNS
::getDB()->query($sql, array($_GET['id'], $name, $type, $idna->encode($data), $aux, $ttl));
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));
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));
140 DNS
::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl));
145 DNS
::getTPL()->assign(array("error" => $error));
148 public function fixSerial ($old) {
149 if (substr($old, 0, -2) == date("Ymd")) {
153 $new = date("Ymd")."01";