/**
  * init RequestHandler
  */
 public function __construct($module = '')
 {
     $this->pattern = '~/?(?:(?P<controller>[A-Za-z0-9\\-]+)(?:/(?P<id>\\d+)(?:-(?P<title>[^/]+))?)?)?~x';
     $controllers = ControllerCacheBuilder::getInstance()->getData(array('module' => $module));
     if (DNS::getSession()->username !== null) {
         DNS::getTPL()->assign(array("username" => DNS::getSession()->username));
     } else {
         DNS::getTPL()->assign(array("username" => ''));
     }
     $className = "";
     if (!empty($_SERVER['QUERY_STRING'])) {
         $this->matches($_SERVER['QUERY_STRING']);
         $this->registerRouteData();
     } else {
         $className = '\\dns' . (empty($module) ? '' : '\\' . $module) . '\\page\\IndexPage';
     }
     if (isset($this->routeData['controller']) && !empty($this->routeData['controller'])) {
         $controller = strtolower($this->routeData['controller']);
         if (isset($controllers[$controller]) && !empty($controllers[$controller])) {
             $className = $controllers[$controller];
         } else {
             @header('HTTP/1.0 404 Not Found');
             DNS::getTPL()->assign(array("activeMenuItem" => '', "error" => 'The link you are trying to reach is no longer available or invalid.'));
             DNS::getTPL()->display('error.tpl');
             exit;
         }
     }
     if (!User::isLoggedIn() && $className != '\\dns\\page\\LoginPage' && $className != '\\dns\\page\\ApiPage') {
         DNS::getTPL()->display('login.tpl');
         exit;
     }
     // handle offline mode
     if (defined('OFFLINE') && OFFLINE) {
         $admin = User::isAdmin();
         $available = false;
         if (defined($className . '::AVAILABLE_DURING_OFFLINE_MODE') && constant($className . '::AVAILABLE_DURING_OFFLINE_MODE')) {
             $available = true;
         }
         if (!$admin && !$available) {
             @header('HTTP/1.1 503 Service Unavailable');
             DNS::getTPL()->display('offline.tpl');
             exit;
         }
     }
     try {
         new $className();
     } catch (\Exception $e) {
         if ($e->getCode() == 404) {
             @header('HTTP/1.0 404 Not Found');
         } else {
             if ($e->getCode() == 403) {
                 @header('HTTP/1.0 403 Forbidden');
             }
         }
         /* show error page */
         DNS::getTPL()->assign(array("activeMenuItem" => '', "error" => $e->getMessage()));
         DNS::getTPL()->display('error.tpl');
         exit;
     }
 }
 public function prepare()
 {
     if (!isset($_GET['id']) || empty($_GET['id']) || !ENABLE_DNSSEC) {
         throw new \Exception('The link you are trying to reach is no longer available or invalid.', 404);
     }
     $soaIDs = User::getAccessibleDomains();
     if (!in_array($_GET['id'], $soaIDs)) {
         throw new \Exception('Access denied. You\'re not authorized to view this page.', 403);
     }
     $sql = "SELECT * FROM dns_soa WHERE id = ?";
     $res = DNS::getDB()->query($sql, array($_GET['id']));
     $soa = DNS::getDB()->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)) {
         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);
             if (!empty($match) && !empty($match2)) {
                 if ($match[1] == $row['algo']) {
                     $ds = DNSSECUtil::calculateDS($soa['origin'], $match[1], $match[2]);
                     $ds['algo'] = $match[1];
                     $ds['keyid'] = $match2[1];
                 }
             }
         }
         $records[] = $row;
     }
     DNS::getTPL()->assign(array("records" => $records, "soa" => $soa, 'ds' => $ds));
 }
 public function prepare()
 {
     if (User::isLoggedIn()) {
         User::logout();
         header("Location: ?page=index");
     }
 }
 public function prepare()
 {
     if (User::isReseller() === false) {
         throw new \Exeption('Forbidden', 403);
     }
     if (isset($_POST['origin']) && isset($_POST['submit'])) {
         if (!empty($_POST['origin'])) {
             $idna = new idna_convert();
             $origin = $_POST['origin'];
             if (substr($origin, -1) != ".") {
                 $origin = $origin . ".";
             }
             $origin = $idna->encode($origin);
             $serial = date("Ymd") . "01";
             $sql = "SELECT * FROM dns_soa WHERE origin = ?";
             $res = DNS::getDB()->query($sql, array($origin));
             $soa = DNS::getDB()->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();
                 $sql = "INSERT INTO dns_soa_to_user (id, userID, soaID) VALUES (null, ?, ?)";
                 DNS::getDB()->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);
                 $records = array();
                 if (!empty($tpl) && !empty($tpl['template'])) {
                     $records = explode("\n", $tpl['template']);
                 } else {
                     $records = explode("\n", DNS_DEFAULT_RECORDS);
                 }
                 if (!empty($records)) {
                     foreach ($records as $record) {
                         $record = str_replace("{domain}", $origin, $record);
                         $record = explode(":", $record, 3);
                         $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);
                     }
                 }
                 DNS::getTPL()->assign(array("error" => '', 'success' => true));
             } else {
                 DNS::getTPL()->assign(array("error" => 'origin', 'origin' => $_POST['origin']));
             }
         } else {
             DNS::getTPL()->assign(array("error" => 'origin'));
         }
     } else {
         DNS::getTPL()->assign(array("error" => ''));
     }
 }
 public function prepare()
 {
     if (isset($_POST['submit']) && isset($_POST['username']) && isset($_POST['password'])) {
         if (!empty($_POST['submit']) && !empty($_POST['username']) && !empty($_POST['password'])) {
             $remember = false;
             if (isset($_POST['remember']) && !empty($_POST['remember'])) {
                 $remember = true;
             }
             User::login(trim($_POST['username']), trim($_POST['password']), $remember);
             header("Location: index.php?index");
         }
     }
 }
 public function prepare()
 {
     if (!isset($_GET['id']) || empty($_GET['id']) || !ENABLE_DNSSEC) {
         throw new \Exception('The link you are trying to reach is no longer available or invalid.', 404);
     }
     print_r($_REQUEST);
     $soaIDs = User::getAccessibleDomains();
     if (!in_array($_GET['id'], $soaIDs)) {
         throw new \Exception('Access denied. You\'re not authorized to view this page.', 403);
     }
     $sql = "SELECT * FROM dns_soa WHERE id = ?";
     $res = DNS::getDB()->query($sql, array($_GET['id']));
     $soa = DNS::getDB()->fetch_array($res);
     DNS::getTPL()->assign(array("soa" => $soa));
 }
 public function prepare()
 {
     $domains = array();
     $soaIDs = User::getAccessibleDomains();
     $idna = new idna_convert();
     $sortField = "id";
     $sortOrder = "ASC";
     $sqlOrderBy = "";
     $validSortFields = array('id', 'origin', 'serial');
     if (isset($_GET['sortField'])) {
         if (in_array($_GET['sortField'], $validSortFields)) {
             $sortField = $_GET['sortField'];
         }
     }
     if (isset($_GET['sortOrder'])) {
         if ($_GET['sortOrder'] == "ASC" || $_GET['sortOrder'] == "DESC") {
             $sortOrder = $_GET['sortOrder'];
         }
     }
     if (!empty($sortField) && !empty($sortField)) {
         $sqlOrderBy = $sortField . " " . $sortOrder;
     }
     $pageNo = 1;
     if (isset($_GET['pageNo']) && !empty($_GET['pageNo'])) {
         $pageNo = intval($_GET['pageNo']);
     }
     $itemsPerPage = 20;
     $pages = 0;
     $sqlLimit = $itemsPerPage;
     $sqlOffset = ($pageNo - 1) * $itemsPerPage;
     $pages = intval(ceil(count($soaIDs) / $itemsPerPage));
     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)) {
             $sql2 = "SELECT count(*) as count FROM dns_rr WHERE zone = ?";
             $res2 = DNS::getDB()->query($sql2, array($row['id']));
             $row2 = DNS::getDB()->fetch_array($res2);
             $row['origin'] = $idna->decode($row['origin']);
             $row['rrc'] = $row2['count'];
             $domains[] = $row;
         }
     }
     DNS::getTPL()->assign(array('domains' => $domains, 'pageNo' => $pageNo, 'pages' => $pages, 'count' => count($soaIDs), 'sortField' => $sortField, 'sortOrder' => $sortOrder));
 }
 public function prepare()
 {
     if (!isset($_GET['id']) || empty($_GET['id'])) {
         throw new \Exception('The link you are trying to reach is no longer available or invalid.', 404);
     }
     $soaIDs = User::getAccessibleDomains();
     if (!in_array($_GET['id'], $soaIDs)) {
         throw new \Exception('Access denied. You\'re not authorized to view this page.', 403);
     }
     $idna = new idna_convert();
     $sql = "SELECT * FROM dns_soa WHERE id = ?";
     $res = DNS::getDB()->query($sql, array($_GET['id']));
     $soa = DNS::getDB()->fetch_array($res);
     $soa['origin'] = $idna->decode($soa['origin']);
     DNS::getTPL()->assign(array("soa" => $soa));
     $types = array('A', 'AAAA', 'CNAME', 'MX', 'PTR', 'SRV', 'TXT', 'TLSA', 'NS', 'DS');
     $error = array();
     if (isset($_POST['submit']) && !empty($_POST['submit'])) {
         if (isset($_POST['name']) && isset($_POST['ttl']) && !empty($_POST['ttl']) && isset($_POST['type']) && !empty($_POST['type']) && isset($_POST['data']) && !empty($_POST['data'])) {
             $type = trim($_POST['type']);
             if (!empty($_POST['name'])) {
                 $name = $idna->encode(trim($_POST['name']));
             } else {
                 $name = $idna->encode(trim($soa['origin']));
             }
             if (in_array($type, $types)) {
                 $aux = 0;
                 if (($type == "MX" || $type == "TLSA" || $type == "SRV" || $type == "DS") && isset($_POST['aux']) && !empty($_POST['aux'])) {
                     $aux = trim($_POST['aux']);
                 }
                 $data = trim($_POST['data']);
                 if ($type == "SRV" || $type == "DS") {
                     if (isset($_POST['weight']) && !empty($_POST['weight']) && isset($_POST['port']) && !empty($_POST['port'])) {
                         if ($type == "SRV") {
                             $data = $idna->encode($data);
                         }
                         $data = trim($_POST['weight']) . ' ' . trim($_POST['port']) . ' ' . $data;
                     } else {
                         $error = array_merge($error, array('weight', 'port', 'data'));
                     }
                 }
                 $ttl = $_POST['ttl'];
                 if ($ttl < DNS_SOA_MINIMUM_TTL) {
                     $ttl = DNS_SOA_MINIMUM_TTL;
                 }
                 if ($type == "TLSA") {
                     if ($aux != 3) {
                         // fallback
                         $aux = 3;
                     }
                     if (isset($_POST['weight']) && isset($_POST['port'])) {
                         if (!is_numeric($_POST['weight'])) {
                             $error = array_merge($error, array('weight'));
                         } else {
                             if (!is_numeric($_POST['port'])) {
                                 $error = array_merge($error, array('weight'));
                             } else {
                                 if (strlen($_POST['data']) != 64) {
                                     $error = array_merge($error, array('data'));
                                 } else {
                                     $data = trim($_POST['weight']) . ' ' . trim($_POST['port']) . ' ' . $data;
                                 }
                             }
                         }
                     } else {
                         $error = array_merge($error, array('weight', 'port', 'data'));
                     }
                 }
                 if ($type == "A") {
                     if (filter_var($data, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
                         $error = array_merge($error, array('data'));
                     }
                 } else {
                     if ($type == "AAAA") {
                         if (filter_var($data, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
                             $error = array_merge($error, array('data'));
                         }
                     }
                 }
             } else {
                 $error = array_merge($error, array('type'));
             }
         } else {
             $error = array_merge($error, array('name', 'ttl', 'data'));
         }
         $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);
         if (!empty($rr)) {
             $error = array_merge($error, array('type', 'data'));
         }
         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));
             } else {
                 DNS::getDB()->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));
         } 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));
             } else {
                 DNS::getTPL()->assign(array('name' => $idna->decode($name), 'type' => $type, 'data' => $data, 'aux' => $aux, 'ttl' => $ttl));
             }
         }
     }
     DNS::getTPL()->assign(array("error" => $error));
 }
 public function prepare()
 {
     if (!isset($_POST['action']) || empty($_POST['action']) || !isset($_POST['dataID'])) {
         echo "failure";
         exit;
     }
     $action = trim($_POST['action']);
     $dataID = intval(trim($_POST['dataID']));
     if ($action == "toggleDomain") {
         if (User::isReseller() === false) {
             echo "failure";
             exit;
         }
         $soaIDs = User::getAccessibleDomains();
         if (!in_array($dataID, $soaIDs)) {
             echo "failure";
             exit;
         }
         $sql = "SELECT active, serial FROM dns_soa WHERE id = ?";
         $res = DNS::getDB()->query($sql, array($dataID));
         $soa = DNS::getDB()->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));
         echo "success";
         exit;
     } else {
         if ($action == "deleteDomain") {
             if (User::isReseller() === false) {
                 echo "failure";
                 exit;
             }
             $soaIDs = User::getAccessibleDomains();
             if (!in_array($dataID, $soaIDs)) {
                 echo "failure";
                 exit;
             }
             $sql = "DELETE FROM dns_soa WHERE id = ?";
             DNS::getDB()->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);
                 $soaID = $rr['zone'];
                 $soaIDs = User::getAccessibleDomains();
                 if (!in_array($soaID, $soaIDs)) {
                     echo "failure";
                     exit;
                 }
                 $sql = "SELECT active FROM dns_rr WHERE id = ?";
                 $res = DNS::getDB()->query($sql, array($dataID));
                 $rr = DNS::getDB()->fetch_array($res);
                 $active = $rr['active'] ? 0 : 1;
                 $sql = "UPDATE dns_rr SET active = ? WHERE id = ?";
                 DNS::getDB()->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);
                 $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
                 DNS::getDB()->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);
                     $soaID = $rr['zone'];
                     $soaIDs = User::getAccessibleDomains();
                     if (!in_array($soaID, $soaIDs)) {
                         echo "failure";
                         exit;
                     }
                     $sql = "DELETE FROM dns_rr WHERE id = ?";
                     DNS::getDB()->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);
                     $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
                     DNS::getDB()->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);
                         $soaID = $rr['zone'];
                         $soaIDs = User::getAccessibleDomains();
                         if (!in_array($soaID, $soaIDs)) {
                             echo "failure";
                             exit;
                         }
                         $sql = "SELECT active FROM dns_sec WHERE id = ?";
                         $res = DNS::getDB()->query($sql, array($dataID));
                         $rr = DNS::getDB()->fetch_array($res);
                         $active = $rr['active'] ? 0 : 1;
                         $sql = "UPDATE dns_sec SET active = ? WHERE id = ?";
                         DNS::getDB()->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);
                         $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
                         DNS::getDB()->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);
                             $soaID = $rr['zone'];
                             $soaIDs = User::getAccessibleDomains();
                             if (!in_array($soaID, $soaIDs)) {
                                 echo "failure";
                                 exit;
                             }
                             $sql = "DELETE FROM dns_sec WHERE id = ?";
                             DNS::getDB()->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);
                             $sql = "UPDATE dns_soa SET serial = ? WHERE id = ?";
                             DNS::getDB()->query($sql, array($this->fixSerial($soa['serial']), $soaID));
                             echo "success";
                             exit;
                         } 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);
                                     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));
                                         echo $apiKey;
                                         exit;
                                     }
                                 }
                             } else {
                                 if ($action == "import") {
                                     if (isset($_POST['zone']) && !empty($_POST['zone'])) {
                                         if ($dataID == 0) {
                                             if (isset($_POST['origin']) && !empty($_POST['origin'])) {
                                                 /*
                                                 if (User::isReseller() === false) {
                                                 	echo "failure";
                                                 	exit;
                                                 }
                                                 */
                                                 // new zone
                                             }
                                         } else {
                                             $soaIDs = User::getAccessibleDomains();
                                             if (!in_array($dataID, $soaIDs)) {
                                                 echo "failure";
                                                 exit;
                                             }
                                             $sql = 'SELECT * FROM dns_soa where id = ?';
                                             $res = DNS::getDB()->query($sql, array($dataID));
                                             $res = DNS::getDB()->fetch_array($res);
                                             $soa = $res;
                                             $parser = new ParseZone($_POST['zone'], $soa['origin']);
                                             try {
                                                 $parser->parse();
                                             } catch (\Exception $e) {
                                                 echo "failure";
                                                 exit;
                                             }
                                             $data = $parser->getParsedData();
                                             if (!empty($data['rr'])) {
                                                 // delete existing records
                                                 foreach ($data['rr'] as $rr) {
                                                     // dont update the default ns entrys, we add them automatically, all other ns entrys will be updated
                                                     if (strtolower($rr['type']) != "ns" && strtolower($rr['name']) != strtolower($soa['origin'])) {
                                                         // import data
                                                     }
                                                 }
                                             } else {
                                                 echo "failure";
                                                 exit;
                                             }
                                         }
                                     }
                                 } else {
                                     if ($action == "export") {
                                         $sql = 'SELECT * FROM dns_soa where id = ?';
                                         $res = DNS::getDB()->query($sql, array($dataID));
                                         $res = DNS::getDB()->fetch_array($res);
                                         $soa = $res;
                                         $soaIDs = User::getAccessibleDomains();
                                         if (!in_array($soa['id'], $soaIDs)) {
                                             echo "failure";
                                             exit;
                                         }
                                         $out = ";; Domain:\t" . $soa['origin'] . "\n";
                                         $out .= ";; Exported:\t" . date("Y-m-d H:i:s") . "\n";
                                         $out .= ";; \n";
                                         $out .= ";; This file is intended for use for informational and archival\n";
                                         $out .= ";; purposes ONLY and MUST be edited before use on a production\n";
                                         $out .= ";; DNS server.  In particular, you must:\n";
                                         $out .= ";;   -- update the SOA record with the correct authoritative name server\n";
                                         $out .= ";;   -- update the SOA record with the contact e-mail address information\n";
                                         $out .= ";;   -- update the NS record(s) with the authoritative name servers for this domain.\n";
                                         $out .= ";; \n";
                                         $out .= ";; For further information, please consult the BIND documentation\n";
                                         $out .= ";; located on the following website:\n";
                                         $out .= ";; \n";
                                         $out .= ";; http://www.isc.org/\n";
                                         $out .= ";; \n";
                                         $out .= ";; And RFC 1035:\n";
                                         $out .= ";; \n";
                                         $out .= ";; http://www.ietf.org/rfc/rfc1035.txt\n";
                                         $out .= ";; \n";
                                         $out .= ";; Please note that we do NOT offer technical support for any use\n";
                                         $out .= ";; of this zone data, the BIND name server, or any other third-party\n";
                                         $out .= ";; DNS software.\n";
                                         $out .= ";; \n";
                                         $out .= ";;\tUse at your own risk.\n";
                                         $out .= ";; \n";
                                         $out .= $soa['origin'] . "\t" . $soa['minimum'] . "\tIN\tSOA\t" . $soa['ns'] . "\t" . $soa['mbox'] . "\t(\n";
                                         $out .= "\t\t" . $soa['serial'] . "\t; Serial\n";
                                         $out .= "\t\t" . $soa['refresh'] . "\t\t; Refresh\n";
                                         $out .= "\t\t" . $soa['retry'] . "\t\t; Retry\n";
                                         $out .= "\t\t" . $soa['expire'] . "\t\t; Expire\n";
                                         $out .= "\t\t180 )\t\t; Negative Cache TTL\n";
                                         $out .= ";;\n";
                                         $sql = 'SELECT * FROM dns_rr where zone = ?';
                                         $res = DNS::getDB()->query($sql, array($soa['id']));
                                         while ($record = DNS::getDB()->fetch_array($res)) {
                                             if (!$record['active']) {
                                                 $out .= ";; ";
                                             }
                                             if ($record['type'] == "MX" || $record['type'] == "SRV" || $record['type'] == "TLSA" || $record['type'] == "DS") {
                                                 $out .= $record['name'] . "\t" . $record['ttl'] . "\tIN\t" . $record['type'] . "\t" . $record['aux'] . "\t" . $record['data'] . "\n";
                                             } else {
                                                 if ($record['type'] == "TXT") {
                                                     $txt = $record['data'];
                                                     if (strpos($txt, " ") !== false) {
                                                         if (substr($txt, -1) != '"' && substr($txt, 0, 1) != '"') {
                                                             if (substr($txt, -1) != "'" && substr($txt, 0, 1) != "'") {
                                                                 $record['data'] = '"' . $txt . '"';
                                                             }
                                                         }
                                                     }
                                                     if (strpos($record['data'], "v=spf1") !== false) {
                                                         $out .= $record['name'] . "\t" . $record['ttl'] . "\tIN\tSPF\t" . $record['data'] . "\n";
                                                     }
                                                     $out .= $record['name'] . "\t" . $record['ttl'] . "\tIN\t" . $record['type'] . "\t" . $record['data'] . "\n";
                                                 } else {
                                                     $out .= $record['name'] . "\t" . $record['ttl'] . "\tIN\t" . $record['type'] . "\t\t" . $record['data'] . "\n";
                                                 }
                                             }
                                         }
                                         echo $out;
                                         exit;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     echo "failure";
     exit;
 }