5 use dns\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-2015 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\92re 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']) && !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)) {
42 if (($type == "MX" ||
$type == "TLSA" ||
$type == "SRV" ||
$type == "DS") && isset($_POST['aux']) && !empty($_POST['aux'])) {
43 $aux = trim($_POST['aux']);
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'])) {
50 $data = $idna->encode($data);
52 $data = trim($_POST['weight']).' '.trim($_POST['port']).' '.$data;
55 $error = array_merge($error, array('weight', 'port', 'data'));
60 if ($ttl < DNS_SOA_MINIMUM_TTL
) {
61 $ttl = DNS_SOA_MINIMUM_TTL
;
64 if ($type == "TLSA") {
70 if (isset($_POST['weight']) && isset($_POST['port'])) {
71 if (!is_numeric($_POST['weight'])) {
72 $error = array_merge($error, array('weight'));
74 else if (!is_numeric($_POST['port'])) {
75 $error = array_merge($error, array('weight'));
77 else if (strlen($_POST['data']) != 64) {
78 $error = array_merge($error, array('data'));
81 $data = trim($_POST['weight']).' '.trim($_POST['port']).' '.$data;
85 $error = array_merge($error, array('weight', 'port', 'data'));
90 if (filter_var($data, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV4
) === false) {
91 $error = array_merge($error, array('data'));
94 else if ($type == "AAAA") {
95 if (filter_var($data, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV6
) === false) {
96 $error = array_merge($error, array('data'));
101 $error = array_merge($error, array('type'));
105 $error = array_merge($error, array('name', 'ttl', 'data'));
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);
112 $error = array_merge($error, array('name', 'type', 'data'));
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));
121 DNS
::getDB()->query($sql, array($_GET['id'], $name, $type, $idna->encode($data), $aux, $ttl));
124 $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
125 DNS
::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soa['id']));
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));
132 DNS
::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl));
137 DNS
::getTPL()->assign(array("error" => $error));
140 public function fixSerial ($old) {
141 if (substr($old, 0, -2) == date("Ymd")) {
145 $new = date("Ymd")."01";