From 2302fee1eb8ceaaad71dc8c09dcdbe5444044710 Mon Sep 17 00:00:00 2001 From: Stricted Date: Thu, 21 Jul 2016 10:22:19 +0200 Subject: [PATCH] add more DI stuff --- lib/page/AbstractPage.class.php | 14 +++-- lib/page/ActionPage.class.php | 88 ++++++++++++++------------- lib/page/ApiManagementPage.class.php | 11 ++-- lib/page/DomainAddPage.class.php | 29 +++++---- lib/page/IndexPage.class.php | 16 +++-- lib/page/LoginPage.class.php | 2 +- lib/page/LogoutPage.class.php | 2 +- lib/page/RecordAddPage.class.php | 29 +++++---- lib/page/RecordEditPage.class.php | 41 +++++++------ lib/page/RecordListPage.class.php | 19 +++--- lib/page/SecAddPage.class.php | 11 ++-- lib/page/SecListPage.class.php | 15 +++-- lib/system/DNS.class.php | 1 + lib/system/RequestHandler.class.php | 12 +++- lib/system/helper/ITemplate.class.php | 6 ++ lib/system/helper/TDatabase.class.php | 2 +- lib/system/helper/TTemplate.class.php | 10 +++ 17 files changed, 184 insertions(+), 124 deletions(-) create mode 100644 lib/system/helper/ITemplate.class.php create mode 100644 lib/system/helper/TTemplate.class.php diff --git a/lib/page/AbstractPage.class.php b/lib/page/AbstractPage.class.php index c79661b..e58f0e5 100644 --- a/lib/page/AbstractPage.class.php +++ b/lib/page/AbstractPage.class.php @@ -1,5 +1,7 @@ * @copyright 2014-2016 Jan Altensen (Stricted) */ -abstract class AbstractPage { +abstract class AbstractPage implements ITemplate { + use TTemplate; + public $activeMenuItem = ''; public $template = ""; - - public final function __construct() { + + public function init() { $this->prepare(); $this->show(); } @@ -30,7 +34,7 @@ abstract class AbstractPage { } } - DNS::getTPL()->assign(array("activeMenuItem" => $this->activeMenuItem)); - DNS::getTPL()->display($this->template); + $this->tpl->assign(array("activeMenuItem" => $this->activeMenuItem)); + $this->tpl->display($this->template); } } diff --git a/lib/page/ActionPage.class.php b/lib/page/ActionPage.class.php index e006ee9..a6e4eef 100644 --- a/lib/page/ActionPage.class.php +++ b/lib/page/ActionPage.class.php @@ -1,5 +1,7 @@ * @copyright 2014-2016 Jan Altensen (Stricted) */ -class ActionPage extends AbstractPage { +class ActionPage extends AbstractPage implements IDatabase { + use TDatabase; + public function prepare() { if (!isset($_POST['action']) || empty($_POST['action']) || !isset($_POST['dataID'])) { echo "failure"; @@ -31,13 +35,13 @@ class ActionPage extends AbstractPage { } $sql = "SELECT active, serial FROM dns_soa WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($dataID)); - $soa = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($dataID)); + $soa = $this->db->fetch_array($res); $active = ($soa['active'] ? 0 : 1); $sql = "UPDATE dns_soa SET active = ?, serial = ? WHERE id = ?"; - DNS::getDB()->query($sql, array($active, $this->fixSerial($soa['serial']), $dataID)); + $this->db->query($sql, array($active, $this->fixSerial($soa['serial']), $dataID)); echo "success"; exit; @@ -55,15 +59,15 @@ class ActionPage extends AbstractPage { } $sql = "DELETE FROM dns_soa WHERE id = ?"; - DNS::getDB()->query($sql, array($dataID)); + $this->db->query($sql, array($dataID)); echo "success"; exit; } else if ($action == "toggleRecord") { $sql = "SELECT zone FROM dns_rr WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($dataID)); - $rr = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($dataID)); + $rr = $this->db->fetch_array($res); $soaID = $rr['zone']; $soaIDs = User::getAccessibleDomains(); @@ -73,28 +77,28 @@ class ActionPage extends AbstractPage { } $sql = "SELECT active FROM dns_rr WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($dataID)); - $rr = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($dataID)); + $rr = $this->db->fetch_array($res); $active = ($rr['active'] ? 0 : 1); $sql = "UPDATE dns_rr SET active = ? WHERE id = ?"; - DNS::getDB()->query($sql, array($active, $dataID)); + $this->db->query($sql, array($active, $dataID)); $sql = "SELECT serial FROM dns_soa WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($soaID)); - $soa = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($soaID)); + $soa = $this->db->fetch_array($res); $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?"; - DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID)); + $this->db->query($sql, array($this->fixSerial($soa['serial']), $soaID)); echo "success"; exit; } else if ($action == "deleteRecord") { $sql = "SELECT zone FROM dns_rr WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($dataID)); - $rr = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($dataID)); + $rr = $this->db->fetch_array($res); $soaID = $rr['zone']; $soaIDs = User::getAccessibleDomains(); @@ -104,22 +108,22 @@ class ActionPage extends AbstractPage { } $sql = "DELETE FROM dns_rr WHERE id = ?"; - DNS::getDB()->query($sql, array($dataID)); + $this->db->query($sql, array($dataID)); $sql = "SELECT serial FROM dns_soa WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($soaID)); - $soa = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($soaID)); + $soa = $this->db->fetch_array($res); $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?"; - DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID)); + $this->db->query($sql, array($this->fixSerial($soa['serial']), $soaID)); echo "success"; exit; } else if ($action == "toggleSec") { $sql = "SELECT zone FROM dns_sec WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($dataID)); - $rr = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($dataID)); + $rr = $this->db->fetch_array($res); $soaID = $rr['zone']; $soaIDs = User::getAccessibleDomains(); @@ -129,28 +133,28 @@ class ActionPage extends AbstractPage { } $sql = "SELECT active FROM dns_sec WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($dataID)); - $rr = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($dataID)); + $rr = $this->db->fetch_array($res); $active = ($rr['active'] ? 0 : 1); $sql = "UPDATE dns_sec SET active = ? WHERE id = ?"; - DNS::getDB()->query($sql, array($active, $dataID)); + $this->db->query($sql, array($active, $dataID)); $sql = "SELECT serial FROM dns_soa WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($soaID)); - $soa = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($soaID)); + $soa = $this->db->fetch_array($res); $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?"; - DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID)); + $this->db->query($sql, array($this->fixSerial($soa['serial']), $soaID)); echo "success"; exit; } else if ($action == "deleteSec") { $sql = "SELECT zone FROM dns_sec WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($dataID)); - $rr = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($dataID)); + $rr = $this->db->fetch_array($res); $soaID = $rr['zone']; $soaIDs = User::getAccessibleDomains(); @@ -160,14 +164,14 @@ class ActionPage extends AbstractPage { } $sql = "DELETE FROM dns_sec WHERE id = ?"; - DNS::getDB()->query($sql, array($dataID)); + $this->db->query($sql, array($dataID)); $sql = "SELECT serial FROM dns_soa WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($soaID)); - $soa = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($soaID)); + $soa = $this->db->fetch_array($res); $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?"; - DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID)); + $this->db->query($sql, array($this->fixSerial($soa['serial']), $soaID)); echo "success"; exit; @@ -175,14 +179,14 @@ class ActionPage extends AbstractPage { else if ($action == "requestApiKey") { if (User::isLoggedIn()) { $sql = "SELECT * FROM dns_api WHERE userID = ?"; - $res = DNS::getDB()->query($sql, array(DNS::getSession()->userID)); - $row = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array(DNS::getSession()->userID)); + $row = $this->db->fetch_array($res); if (empty($row)) { $apiKey = DNS::generateUUID(); $sql = "INSERT INTO dns_api (id, userID, apiKey) VALUES (NULL, ?, ?)"; - DNS::getDB()->query($sql, array(DNS::getSession()->userID, $apiKey)); + $this->db->query($sql, array(DNS::getSession()->userID, $apiKey)); echo $apiKey; exit; @@ -210,8 +214,8 @@ class ActionPage extends AbstractPage { } $sql = 'SELECT * FROM dns_soa where id = ?'; - $res = DNS::getDB()->query($sql, array($dataID)); - $res = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($dataID)); + $res = $this->db->fetch_array($res); $soa = $res; $parser = new ParseZone($_POST['zone'], $soa['origin']); @@ -243,8 +247,8 @@ class ActionPage extends AbstractPage { } else if ($action == "export") { $sql = 'SELECT * FROM dns_soa where id = ?'; - $res = DNS::getDB()->query($sql, array($dataID)); - $res = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($dataID)); + $res = $this->db->fetch_array($res); $soa = $res; $soaIDs = User::getAccessibleDomains(); @@ -288,8 +292,8 @@ class ActionPage extends AbstractPage { $out .= ";;\n"; $sql = 'SELECT * FROM dns_rr where zone = ?'; - $res = DNS::getDB()->query($sql, array($soa['id'])); - while ($record = DNS::getDB()->fetch_array($res)) { + $res = $this->db->query($sql, array($soa['id'])); + while ($record = $this->db->fetch_array($res)) { if (!$record['active']) { $out .= ";; "; } diff --git a/lib/page/ApiManagementPage.class.php b/lib/page/ApiManagementPage.class.php index 325ca0a..5f3c8f7 100644 --- a/lib/page/ApiManagementPage.class.php +++ b/lib/page/ApiManagementPage.class.php @@ -1,5 +1,7 @@ * @copyright 2014-2016 Jan Altensen (Stricted) */ -class ApiManagementPage extends AbstractPage { +class ApiManagementPage extends AbstractPage implements IDatabase { + use TDatabase; public $activeMenuItem = 'api'; public function prepare() { $sql = "SELECT * FROM dns_api WHERE userID = ?"; - $res = DNS::getDB()->query($sql, array(DNS::getSession()->userID)); - $row = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array(DNS::getSession()->userID)); + $row = $this->db->fetch_array($res); $apiKey = ""; @@ -22,6 +25,6 @@ class ApiManagementPage extends AbstractPage { $apiKey = $row['apiKey']; } - DNS::getTPL()->assign(array("userID" => DNS::getSession()->userID,"apiKey" => $apiKey)); + $this->tpl->assign(array("userID" => DNS::getSession()->userID,"apiKey" => $apiKey)); } } diff --git a/lib/page/DomainAddPage.class.php b/lib/page/DomainAddPage.class.php index b3700f1..2feea01 100644 --- a/lib/page/DomainAddPage.class.php +++ b/lib/page/DomainAddPage.class.php @@ -1,5 +1,7 @@ * @copyright 2014-2016 Jan Altensen (Stricted) */ -class DomainAddPage extends AbstractPage { +class DomainAddPage extends AbstractPage implements IDatabase { + use TDatabase; public $activeMenuItem = 'add'; public function prepare() { @@ -29,22 +32,22 @@ class DomainAddPage extends AbstractPage { $serial = date("Ymd")."01"; $sql = "SELECT * FROM dns_soa WHERE origin = ?"; - $res = DNS::getDB()->query($sql, array($origin)); - $soa = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($origin)); + $soa = $this->db->fetch_array($res); if (empty($soa)) { $soaData = array($origin, DNS_SOA_NS, DNS_SOA_MBOX, $serial, DNS_SOA_REFRESH, DNS_SOA_RETRY, DNS_SOA_EXPIRE, DNS_SOA_MINIMUM_TTL, DNS_SOA_TTL, 1); $sql = "INSERT INTO dns_soa (id, origin, ns, mbox, serial, refresh, retry, expire, minimum, ttl, active) VALUES (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - DNS::getDB()->query($sql, $soaData); - $soaID = DNS::getDB()->last_id(); + $this->db->query($sql, $soaData); + $soaID = $this->db->last_id(); $sql = "INSERT INTO dns_soa_to_user (id, userID, soaID) VALUES (null, ?, ?)"; - DNS::getDB()->query($sql, array(DNS::getSession()->userID, $soaID)); + $this->db->query($sql, array(DNS::getSession()->userID, $soaID)); $sql = "SELECT * FROM dns_template WHERE userID = ?"; - $res = DNS::getDB()->query($sql, array(DNS::getSession()->userID)); - $tpl = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array(DNS::getSession()->userID)); + $tpl = $this->db->fetch_array($res); $records = array(); if (!empty($tpl) && !empty($tpl['template'])) { @@ -61,21 +64,21 @@ class DomainAddPage extends AbstractPage { $rrData = array($soaID, $record[0], $record[1], $record[2], ($record[1] == "MX" ? 10 : 0), DNS_SOA_MINIMUM_TTL); $sql = 'INSERT INTO dns_rr (id, zone, name, type, data, aux, ttl) VALUES (NULL, ?, ?, ?, ?, ?, ?)'; - DNS::getDB()->query($sql, $rrData); + $this->db->query($sql, $rrData); } } - DNS::getTPL()->assign(array("error" => '', 'success' => true)); + $this->tpl->assign(array("error" => '', 'success' => true)); } else { - DNS::getTPL()->assign(array("error" => 'origin', 'origin' => $_POST['origin'])); + $this->tpl->assign(array("error" => 'origin', 'origin' => $_POST['origin'])); } } else { - DNS::getTPL()->assign(array("error" => 'origin')); + $this->tpl->assign(array("error" => 'origin')); } } else { - DNS::getTPL()->assign(array("error" => '')); + $this->tpl->assign(array("error" => '')); } } } diff --git a/lib/page/IndexPage.class.php b/lib/page/IndexPage.class.php index 9021f5e..277fb39 100644 --- a/lib/page/IndexPage.class.php +++ b/lib/page/IndexPage.class.php @@ -1,5 +1,7 @@ * @copyright 2014-2016 Jan Altensen (Stricted) */ -class IndexPage extends AbstractPage { +class IndexPage extends AbstractPage implements IDatabase { + use TDatabase; + public $activeMenuItem = 'index'; public function prepare() { @@ -52,18 +56,18 @@ class IndexPage extends AbstractPage { if (count($soaIDs) > 0) { $sql = "SELECT * FROM dns_soa WHERE id IN (".str_repeat('?, ', count($soaIDs) - 1). "?)".(!empty($sqlOrderBy) ? " ORDER BY ".$sqlOrderBy : '')." LIMIT " . $sqlLimit . " OFFSET " . $sqlOffset; - $res = DNS::getDB()->query($sql, $soaIDs); - while ($row = DNS::getDB()->fetch_array($res)) { + $res = $this->db->query($sql, $soaIDs); + while ($row = $this->db->fetch_array($res)) { $sql2 = "SELECT count(*) as count FROM dns_rr WHERE zone = ?"; - $res2 = DNS::getDB()->query($sql2, array($row['id'])); - $row2 = DNS::getDB()->fetch_array($res2); + $res2 = $this->db->query($sql2, array($row['id'])); + $row2 = $this->db->fetch_array($res2); $row['origin'] = $idna->decode($row['origin']); $row['rrc'] = $row2['count']; $domains[] = $row; } } - DNS::getTPL()->assign(array( + $this->tpl->assign(array( 'domains' => $domains, 'pageNo' => $pageNo, 'pages' => $pages, diff --git a/lib/page/LoginPage.class.php b/lib/page/LoginPage.class.php index 260a2f4..cbdc814 100644 --- a/lib/page/LoginPage.class.php +++ b/lib/page/LoginPage.class.php @@ -20,7 +20,7 @@ class LoginPage extends AbstractPage { } User::login(trim($_POST['username']), trim($_POST['password']), $remember); - header("Location: index.php?index"); + header("Location: index.php?index"); // TODO: use link builder } } } diff --git a/lib/page/LogoutPage.class.php b/lib/page/LogoutPage.class.php index 3666395..81d9800 100644 --- a/lib/page/LogoutPage.class.php +++ b/lib/page/LogoutPage.class.php @@ -12,7 +12,7 @@ class LogoutPage extends AbstractPage { public function prepare() { if (User::isLoggedIn()) { User::logout(); - header("Location: ?page=index"); + header("Location: ?page=index"); // TODO: use link builder } } } diff --git a/lib/page/RecordAddPage.class.php b/lib/page/RecordAddPage.class.php index e8c997c..4270ef1 100644 --- a/lib/page/RecordAddPage.class.php +++ b/lib/page/RecordAddPage.class.php @@ -1,5 +1,7 @@ * @copyright 2014-2016 Jan Altensen (Stricted) */ -class RecordAddPage extends AbstractPage { +class RecordAddPage extends AbstractPage implements IDatabase { + use TDatabase; public $activeMenuItem = 'index'; public function prepare() { @@ -24,12 +27,12 @@ class RecordAddPage extends AbstractPage { $idna = new IdnaConvert(); $sql = "SELECT * FROM dns_soa WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($_GET['id'])); - $soa = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($_GET['id'])); + $soa = $this->db->fetch_array($res); $soa['origin'] = $idna->decode($soa['origin']); - DNS::getTPL()->assign(array("soa" => $soa)); + $this->tpl->assign(array("soa" => $soa)); $types = array('A', 'AAAA', 'CNAME', 'MX', 'PTR', 'SRV', 'TXT', 'TLSA', 'NS', 'DS'); $error = array(); @@ -113,8 +116,8 @@ class RecordAddPage extends AbstractPage { } $sql = 'SELECT * FROM dns_rr WHERE zone = ? AND name = ? AND type = ? AND data = ?'; - $res = DNS::getDB()->query($sql, array($_GET['id'], $name, $type, $data)); - $rr = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($_GET['id'], $name, $type, $data)); + $rr = $this->db->fetch_array($res); if (!empty($rr)) { $error = array_merge($error, array('type', 'data')); } @@ -122,27 +125,27 @@ class RecordAddPage extends AbstractPage { if (empty($error)) { $sql = 'INSERT INTO dns_rr (id, zone, name, type, data, aux, ttl) VALUES (NULL, ?, ?, ?, ?, ?, ?)'; if ($type == "SRV" || $type == "DS" || $type == "TLSA") { - DNS::getDB()->query($sql, array($_GET['id'], $name, $type, $data, $aux, $ttl)); + $this->db->query($sql, array($_GET['id'], $name, $type, $data, $aux, $ttl)); } else { - DNS::getDB()->query($sql, array($_GET['id'], $name, $type, $idna->encode($data), $aux, $ttl)); + $this->db->query($sql, array($_GET['id'], $name, $type, $idna->encode($data), $aux, $ttl)); } $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?"; - DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soa['id'])); - DNS::getTPL()->assign(array('success' => true)); + $this->db->query($sql, array($this->fixSerial($soa['serial']), $soa['id'])); + $this->tpl->assign(array('success' => true)); } else { if ($type == "SRV" || $type == "DS" || $type == "TLSA") { - DNS::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'weight' => $_POST['weight'], 'port' => $_POST['port'], 'data' => $_POST['data'], 'aux' => $aux, 'ttl' => $ttl)); + $this->tpl->assign(array('name' => $idna->decode($name), 'type' => $type, 'weight' => $_POST['weight'], 'port' => $_POST['port'], 'data' => $_POST['data'], 'aux' => $aux, 'ttl' => $ttl)); } else { - DNS::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl)); + $this->tpl->assign(array('name' => $idna->decode($name), 'type' => $type, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl)); } } } - DNS::getTPL()->assign(array("error" => $error)); + $this->tpl->assign(array("error" => $error)); } public function fixSerial ($old) { diff --git a/lib/page/RecordEditPage.class.php b/lib/page/RecordEditPage.class.php index c22bd42..a31e645 100644 --- a/lib/page/RecordEditPage.class.php +++ b/lib/page/RecordEditPage.class.php @@ -1,5 +1,7 @@ * @copyright 2014-2016 Jan Altensen (Stricted) */ -class RecordEditPage extends AbstractPage { +class RecordEditPage extends AbstractPage implements IDatabase { + use TDatabase; public $activeMenuItem = 'index'; public function prepare() { @@ -19,8 +22,8 @@ class RecordEditPage extends AbstractPage { $idna = new IdnaConvert(); $sql = "SELECT * FROM dns_rr WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($_GET['id'])); - $rr = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($_GET['id'])); + $rr = $this->db->fetch_array($res); $soaIDs = User::getAccessibleDomains(); if (!in_array($rr['zone'], $soaIDs)) { @@ -28,12 +31,12 @@ class RecordEditPage extends AbstractPage { } $sql = "SELECT * FROM dns_soa WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($rr['zone'])); - $soa = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($rr['zone'])); + $soa = $this->db->fetch_array($res); $soa['origin'] = $idna->decode($soa['origin']); - DNS::getTPL()->assign(array("soa" => $soa, "rr" => $rr)); + $this->tpl->assign(array("soa" => $soa, "rr" => $rr)); $types = array('A', 'AAAA', 'CNAME', 'MX', 'PTR', 'SRV', 'TXT', 'TLSA', 'NS', 'DS'); $error = array(); @@ -117,8 +120,8 @@ class RecordEditPage extends AbstractPage { } $sql = 'SELECT * FROM dns_rr WHERE zone = ? AND name = ? AND type = ? AND data = ? AND id != ?'; - $res = DNS::getDB()->query($sql, array($rr['zone'], $name, $type, $data, $_GET['id'])); - $rr = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($rr['zone'], $name, $type, $data, $_GET['id'])); + $rr = $this->db->fetch_array($res); if (!empty($rr)) { $error = array_merge($error, array('type', 'data')); } @@ -127,18 +130,18 @@ class RecordEditPage extends AbstractPage { $sql = 'UPDATE dns_rr SET name = ?, type = ?, aux = ?, data = ?, ttl = ? WHERE id = ?'; if ($type == "SRV" || $type == "DS" || $type == "TLSA") { - DNS::getDB()->query($sql, array($name, $type, $aux, $data, $ttl, $_GET['id'])); + $this->db->query($sql, array($name, $type, $aux, $data, $ttl, $_GET['id'])); } else { - DNS::getDB()->query($sql, array($name, $type, $aux, $idna->encode($data), $ttl, $_GET['id'])); + $this->db->query($sql, array($name, $type, $aux, $idna->encode($data), $ttl, $_GET['id'])); } $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?"; - DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soa['id'])); + $this->db->query($sql, array($this->fixSerial($soa['serial']), $soa['id'])); $sql = "SELECT * FROM dns_rr WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($_GET['id'])); - $rr = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($_GET['id'])); + $rr = $this->db->fetch_array($res); $weight = 0; $port = 0; @@ -162,15 +165,15 @@ class RecordEditPage extends AbstractPage { $data = $idna->decode($data); } - DNS::getTPL()->assign(array('name' => $name, 'type' => $type, 'weight' => $weight, 'port' => $port, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl)); - DNS::getTPL()->assign(array('success' => true)); + $this->tpl->assign(array('name' => $name, 'type' => $type, 'weight' => $weight, 'port' => $port, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl)); + $this->tpl->assign(array('success' => true)); } else { if ($type == "SRV" || $type == "DS" || $type == "TLSA") { - DNS::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'weight' => $_POST['weight'], 'port' => $_POST['port'], 'data' => $_POST['data'], 'aux' => $aux, 'ttl' => $ttl)); + $this->tpl->assign(array('name' => $idna->decode($name), 'type' => $type, 'weight' => $_POST['weight'], 'port' => $_POST['port'], 'data' => $_POST['data'], 'aux' => $aux, 'ttl' => $ttl)); } else { - DNS::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'data' => $idna->decode($data), 'aux' => $aux, 'ttl' => $ttl)); + $this->tpl->assign(array('name' => $idna->decode($name), 'type' => $type, 'data' => $idna->decode($data), 'aux' => $aux, 'ttl' => $ttl)); } } } @@ -197,10 +200,10 @@ class RecordEditPage extends AbstractPage { $data = $idna->decode($data); } - DNS::getTPL()->assign(array('name' => $name, 'type' => $type, 'weight' => $weight, 'port' => $port, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl)); + $this->tpl->assign(array('name' => $name, 'type' => $type, 'weight' => $weight, 'port' => $port, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl)); } - DNS::getTPL()->assign(array("error" => $error)); + $this->tpl->assign(array("error" => $error)); } public function fixSerial ($old) { diff --git a/lib/page/RecordListPage.class.php b/lib/page/RecordListPage.class.php index 490c60c..e0da8e0 100644 --- a/lib/page/RecordListPage.class.php +++ b/lib/page/RecordListPage.class.php @@ -1,5 +1,7 @@ * @copyright 2014-2016 Jan Altensen (Stricted) */ -class RecordListPage extends AbstractPage { +class RecordListPage extends AbstractPage implements IDatabase { + use TDatabase; public $activeMenuItem = 'index'; public function prepare() { @@ -25,8 +28,8 @@ class RecordListPage extends AbstractPage { $idna = new IdnaConvert(); $sql = "SELECT count(*) as count FROM dns_rr WHERE zone = ?"; - $res = DNS::getDB()->query($sql, array($_GET['id'])); - $row = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($_GET['id'])); + $row = $this->db->fetch_array($res); $count = $row['count']; $sortField = "type"; @@ -63,16 +66,16 @@ class RecordListPage extends AbstractPage { $pages = intval(ceil($count / $itemsPerPage)); $sql = "SELECT * FROM dns_soa WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($_GET['id'])); - $soa = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($_GET['id'])); + $soa = $this->db->fetch_array($res); $soa['origin'] = $idna->decode($soa['origin']); $records = array(); $sql = "SELECT * FROM dns_rr WHERE zone = ?".(!empty($sqlOrderBy) ? " ORDER BY ".$sqlOrderBy : '')." LIMIT " . $sqlLimit . " OFFSET " . $sqlOffset; - $res = DNS::getDB()->query($sql, array($_GET['id'])); - while ($row = DNS::getDB()->fetch_array($res)) { + $res = $this->db->query($sql, array($_GET['id'])); + while ($row = $this->db->fetch_array($res)) { $row['name'] = $idna->decode($row['name']); if ($row['type'] == "SRV") { $data = explode(" ", $row['data']); @@ -95,7 +98,7 @@ class RecordListPage extends AbstractPage { $records[] = $row; } - DNS::getTPL()->assign(array( + $this->tpl->assign(array( 'records' => $records, 'soa' => $soa, 'pageNo' => $pageNo, diff --git a/lib/page/SecAddPage.class.php b/lib/page/SecAddPage.class.php index b695a3c..2928aea 100644 --- a/lib/page/SecAddPage.class.php +++ b/lib/page/SecAddPage.class.php @@ -1,5 +1,7 @@ * @copyright 2014-2016 Jan Altensen (Stricted) */ -class SecAddPage extends AbstractPage { +class SecAddPage extends AbstractPage implements IDatabase { + use TDatabase; public function prepare() { if (!isset($_GET['id']) || empty($_GET['id']) || !ENABLE_DNSSEC) { @@ -21,9 +24,9 @@ class SecAddPage extends AbstractPage { } $sql = "SELECT * FROM dns_soa WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($_GET['id'])); - $soa = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($_GET['id'])); + $soa = $this->db->fetch_array($res); - DNS::getTPL()->assign(array("soa" => $soa)); + $this->tpl->assign(array("soa" => $soa)); } } diff --git a/lib/page/SecListPage.class.php b/lib/page/SecListPage.class.php index 8b71641..45c6053 100644 --- a/lib/page/SecListPage.class.php +++ b/lib/page/SecListPage.class.php @@ -1,5 +1,7 @@ * @copyright 2014-2016 Jan Altensen (Stricted) */ -class SecListPage extends AbstractPage { +class SecListPage extends AbstractPage implements IDatabase { + use TDatabase; public $activeMenuItem = 'index'; public function prepare() { @@ -23,15 +26,15 @@ class SecListPage extends AbstractPage { } $sql = "SELECT * FROM dns_soa WHERE id = ?"; - $res = DNS::getDB()->query($sql, array($_GET['id'])); - $soa = DNS::getDB()->fetch_array($res); + $res = $this->db->query($sql, array($_GET['id'])); + $soa = $this->db->fetch_array($res); $records = array(); $ds = array(); $sql = "SELECT * FROM dns_sec WHERE zone = ?"; - $res = DNS::getDB()->query($sql, array($_GET['id'])); - while ($row = DNS::getDB()->fetch_array($res)) { + $res = $this->db->query($sql, array($_GET['id'])); + while ($row = $this->db->fetch_array($res)) { if ($row['type'] == 'KSK') { preg_match("/".$soa['origin']." IN DNSKEY 257 3 ([0-9]+) ([\s\S]+)/i", $row['public'], $match); preg_match("/; This is a key-signing key, keyid ([0-9]+), for ".$soa['origin']."/i", $row['public'], $match2); @@ -46,6 +49,6 @@ class SecListPage extends AbstractPage { $records[] = $row; } - DNS::getTPL()->assign(array("records" => $records, "soa" => $soa, 'ds' => $ds)); + $this->tpl->assign(array("records" => $records, "soa" => $soa, 'ds' => $ds)); } } diff --git a/lib/system/DNS.class.php b/lib/system/DNS.class.php index 5b26c32..a342d4f 100644 --- a/lib/system/DNS.class.php +++ b/lib/system/DNS.class.php @@ -70,6 +70,7 @@ class DNS { $requestHandler = RequestHandler::getInstance(); $requestHandler->setDB(self::getDB()); + $requestHandler->setTPL(self::getTPL()); $requestHandler->setRoutes($module); $requestHandler->handle(); } diff --git a/lib/system/RequestHandler.class.php b/lib/system/RequestHandler.class.php index 0e889e3..a8f2f7f 100644 --- a/lib/system/RequestHandler.class.php +++ b/lib/system/RequestHandler.class.php @@ -3,6 +3,8 @@ namespace dns\system; use dns\system\cache\builder\ControllerCacheBuilder; use dns\system\helper\IDatabase; use dns\system\helper\TDatabase; +use dns\system\helper\ITemplate; +use dns\system\helper\TTemplate; use dns\system\route\Request; use dns\system\route\Segment; use Zend\Router\Http\RouteMatch; @@ -13,8 +15,9 @@ use Zend\Router\SimpleRouteStack; * @license GNU Lesser General Public License * @copyright 2013-2016 Jan Altensen (Stricted) */ -class RequestHandler extends SingletonFactory implements IDatabase { +class RequestHandler extends SingletonFactory implements IDatabase, ITemplate { use TDatabase; + use TTemplate; protected $router = null; protected $apiModule = false; @@ -114,9 +117,16 @@ class RequestHandler extends SingletonFactory implements IDatabase { try { $page = new $className(); + if ($page instanceof IDatabase) { $page->setDB($this->db); } + + if ($page instanceof ITemplate) { + $page->setTPL($this->tpl); + } + + $page->init(); } catch (\Exception $e) { if ($e->getCode() == 404) { diff --git a/lib/system/helper/ITemplate.class.php b/lib/system/helper/ITemplate.class.php new file mode 100644 index 0000000..0197240 --- /dev/null +++ b/lib/system/helper/ITemplate.class.php @@ -0,0 +1,6 @@ +db = $database; diff --git a/lib/system/helper/TTemplate.class.php b/lib/system/helper/TTemplate.class.php new file mode 100644 index 0000000..0b2581b --- /dev/null +++ b/lib/system/helper/TTemplate.class.php @@ -0,0 +1,10 @@ +tpl = $tpl; + } +} -- 2.20.1