5 use Mso\IdnaConvert\IdnaConvert
;
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 RecordEditPage
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);
19 $idna = new IdnaConvert();
21 $sql = "SELECT * FROM dns_rr WHERE id = ?";
22 $res = DNS
::getDB()->query($sql, array($_GET['id']));
23 $rr = DNS
::getDB()->fetch_array($res);
25 $soaIDs = User
::getAccessibleDomains();
26 if (!in_array($rr['zone'], $soaIDs)) {
27 throw new \
Exception('Access denied. You\'re not authorized to view this page.', 403);
30 $sql = "SELECT * FROM dns_soa WHERE id = ?";
31 $res = DNS
::getDB()->query($sql, array($rr['zone']));
32 $soa = DNS
::getDB()->fetch_array($res);
34 $soa['origin'] = $idna->decode($soa['origin']);
36 DNS
::getTPL()->assign(array("soa" => $soa, "rr" => $rr));
38 $types = array('A', 'AAAA', 'CNAME', 'MX', 'PTR', 'SRV', 'TXT', 'TLSA', 'NS', 'DS');
40 if (isset($_POST['submit']) && !empty($_POST['submit'])) {
41 if (isset($_POST['name']) && isset($_POST['ttl']) && !empty($_POST['ttl']) && isset($_POST['type']) && !empty($_POST['type']) && isset($_POST['data']) && !empty($_POST['data'])) {
42 $type = trim($_POST['type']);
44 if (!empty($_POST['name'])) {
45 $name = $idna->encode(trim($_POST['name']));
48 $name = $idna->encode(trim($soa['origin']));
51 if (in_array($type, $types)) {
53 if (($type == "MX" ||
$type == "TLSA" ||
$type == "SRV" ||
$type == "DS") && isset($_POST['aux']) && !empty($_POST['aux'])) {
54 $aux = trim($_POST['aux']);
57 $data = trim($_POST['data']);
58 if ($type == "SRV" ||
$type == "DS") {
59 if (isset($_POST['weight']) && !empty($_POST['weight']) && isset($_POST['port']) && !empty($_POST['port'])) {
61 $data = $idna->encode($data);
63 $data = trim($_POST['weight']).' '.trim($_POST['port']).' '.$data;
66 $error = array_merge($error, array('weight', 'port', 'data'));
71 if ($ttl < DNS_SOA_MINIMUM_TTL
) {
72 $ttl = DNS_SOA_MINIMUM_TTL
;
75 if ($type == "TLSA") {
81 if (isset($_POST['weight']) && isset($_POST['port'])) {
82 if (!is_numeric($_POST['weight'])) {
83 $error = array_merge($error, array('weight'));
85 else if (!is_numeric($_POST['port'])) {
86 $error = array_merge($error, array('weight'));
88 else if (strlen($_POST['data']) != 64) {
89 $error = array_merge($error, array('data'));
92 $data = trim($_POST['weight']).' '.trim($_POST['port']).' '.$data;
96 $error = array_merge($error, array('weight', 'port', 'data'));
101 if (filter_var($data, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV4
) === false) {
102 $error = array_merge($error, array('data'));
105 else if ($type == "AAAA") {
106 if (filter_var($data, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV6
) === false) {
107 $error = array_merge($error, array('data'));
112 $error = array_merge($error, array('type'));
116 $error = array_merge($error, array('name', 'ttl', 'data'));
119 $sql = 'SELECT * FROM dns_rr WHERE zone = ? AND name = ? AND type = ? AND data = ? AND id != ?';
120 $res = DNS
::getDB()->query($sql, array($rr['zone'], $name, $type, $data, $_GET['id']));
121 $rr = DNS
::getDB()->fetch_array($res);
123 $error = array_merge($error, array('type', 'data'));
128 $sql = 'UPDATE dns_rr SET name = ?, type = ?, aux = ?, data = ?, ttl = ? WHERE id = ?';
129 if ($type == "SRV" ||
$type == "DS" ||
$type == "TLSA") {
130 DNS
::getDB()->query($sql, array($name, $type, $aux, $data, $ttl, $_GET['id']));
133 DNS
::getDB()->query($sql, array($name, $type, $aux, $idna->encode($data), $ttl, $_GET['id']));
136 $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
137 DNS
::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soa['id']));
139 $sql = "SELECT * FROM dns_rr WHERE id = ?";
140 $res = DNS
::getDB()->query($sql, array($_GET['id']));
141 $rr = DNS
::getDB()->fetch_array($res);
147 $name = $idna->decode($rr['name']);
150 if ($type == "SRV" ||
$type == "DS" ||
$type == "TLSA") {
151 $datae = explode(" ", $data);
154 if ($type == "SRV") {
155 $data = $idna->decode($datae[2]);
162 $data = $idna->decode($data);
165 DNS
::getTPL()->assign(array('name' => $name, 'type' => $type, 'weight' => $weight, 'port' => $port, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl));
166 DNS
::getTPL()->assign(array('success' => true));
169 if ($type == "SRV" ||
$type == "DS" ||
$type == "TLSA") {
170 DNS
::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'weight' => $_POST['weight'], 'port' => $_POST['port'], 'data' => $_POST['data'], 'aux' => $aux, 'ttl' => $ttl));
173 DNS
::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'data' => $idna->decode($data), 'aux' => $aux, 'ttl' => $ttl));
182 $name = $idna->decode($rr['name']);
185 if ($type == "SRV" ||
$type == "DS" ||
$type == "TLSA") {
186 $datae = explode(" ", $data);
189 if ($type == "SRV") {
190 $data = $idna->decode($datae[2]);
197 $data = $idna->decode($data);
200 DNS
::getTPL()->assign(array('name' => $name, 'type' => $type, 'weight' => $weight, 'port' => $port, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl));
203 DNS
::getTPL()->assign(array("error" => $error));
206 public function fixSerial ($old) {
207 if (substr($old, 0, -2) == date("Ymd")) {
211 $new = date("Ymd")."01";