<?php
/*****************************************************************************
IP Reg, a PHP/MySQL IPAM tool
Copyright (C) 2007-2009 Wietse Warendorff (up to v0.5)
Copyright (C) 2011-2023 Thomas Hooge

SPDX-License-Identifier: GPL-3.0-or-later
*****************************************************************************/
	
include("includes.php");

if (isset($_REQUEST['id'])) {
    $id = (int) $_REQUEST['id'] or $id = 0;
}

// ========== ACTIONS START ===================================================
switch ($submit = form_get_action()) {

    case NULL: break;

    case 'add':   $action = ACT_ADD; break;
    case 'view':  $action = ACT_VIEW; break;
    case 'edit':  $action = ACT_EDIT; break;
    case 'del':   $action = ACT_DELETE; break;

    case 'insert':
        $zone_origin = sanitize($_POST['zone_origin']);
        $zone_ttl_default = sanitize($_POST['zone_ttl_default']);
        $zone_soa = sanitize($_POST['zone_soa']);
        $zone_hostmaster = sanitize($_POST['zone_hostmaster']);
        $zone_refresh = sanitize($_POST['zone_refresh']);
        $zone_retry = sanitize($_POST['zone_retry']);
        $zone_expire = sanitize($_POST['zone_expire']);
        $zone_ttl = sanitize($_POST['zone_ttl']);
        $zone_serial = sanitize($_POST['zone_serial']);
        $zone_ns1 = sanitize($_POST['zone_ns1']);
        $zone_ns2 = sanitize($_POST['zone_ns2']);
        $zone_ns3 = sanitize($_POST['zone_ns3']);
        $zone_mx1 = sanitize($_POST['zone_mx1']);
        $zone_mx2 = sanitize($_POST['zone_mx2']);
        $zone_info = sanitize($_POST['zone_info']);

        $sql = "INSERT INTO zone (
                    zone_origin, zone_ttl_default, zone_soa, zone_hostmaster,
                    zone_refresh, zone_retry, zone_expire, zone_ttl, zone_serial,
                    zone_ns1, zone_ns2, zone_ns3, zone_mx1, zone_mx2, zone_info)
                VALUE (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        $sth = $dbh->prepare($sql);
        $sth->execute([$zone_origin, $zone_ttl_default, $zone_soa, $zone_hostmaster,
                       $zone_refresh, $zone_retry, $zone_expire, $zone_ttl, $zone_serial,
                    $zone_ns1, $zone_ns2, $zone_ns3, $zone_mx1, $zone_mx2, $zone_info]);

        $id = $dbh->lastInsertId();
        $action = ACT_VIEW;
        break;

    case 'update':
        $origin = sanitize($_POST['zone_origin']);
        $ttl_default = sanitize($_POST['zone_ttl_default']);
        $soa = sanitize($_POST['zone_soa']);
        $hostmaster = sanitize($_POST['zone_hostmaster']);
        $refresh = sanitize($_POST['zone_refresh']);
        $retry = sanitize($_POST['zone_retry']);
        $expire = sanitize($_POST['zone_expire']);
        $ttl = sanitize($_POST['zone_ttl']);
        $serial = sanitize($_POST['zone_serial']);
        $ns1 = sanitize($_POST['zone_ns1']);
        $ns2 = sanitize($_POST['zone_ns2']);
        $ns3 = sanitize($_POST['zone_ns3']);
        $mx1 = sanitize($_POST['zone_mx1']);
        $mx2 = sanitize($_POST['zone_mx2']);
        $info = sanitize($_POST['zone_info']);
        $sql = "UPDATE zone SET
                    zone_origin=?, zone_ttl_default=?, zone_soa=?, zone_hostmaster=?,
                    zone_refresh=?, zone_retry=?, zone_expire=?, zone_ttl=?, zone_serial=?,
                    zone_ns1=?, zone_ns2=?, zone_ns3=?, zone_mx1=?, zone_mx2=?, zone_info=?
                WHERE zone_id=?";
        $sth = $dbh->prepare($sql);
        $sth->execute([$origin, $ttl_default, $soa, $hostmaster, $refresh, $retry,
                       $expire, $ttl, $serial, $ns1, $ns2, $ns3, $mx1, $mx2, $info,
                       $id]);
        $action = ACT_VIEW;
        break;

    case 'delete':
        $sth = $dbh->prepare("SELECT COUNT(*) FROM node WHERE zone_id=?");
        $sth->execute([$id]);
        if ($sth->fetchColumn() > 0) {
            $g_warning->Add("Zone can not be removed. There are node-references.");
            $action = ACT_VIEW;
            break;
        }
        $sth = $dbh->prepare("DELETE FROM zone WHERE zone_id=?");
        $sth->execute([$id]);
        $g_message->Add(_("Deleted zone"));
        $action = ACT_DEFAULT;
        break;

    default:
        $g_error->Add(submit_error($submit));
        $valid = FALSE;
}

// ========== ACTIONS END =====================================================

include("header.php");

if ($action == ACT_DEFAULT):
// ========== VARIANT: default behavior =======================================

$sql = "SELECT zone_id AS id, zone_origin AS origin, zone_soa AS soa,
            zone_hostmaster AS hostmaster, zone_serial AS serial
        FROM zone
        ORDER BY zone_origin";
$sth = $dbh->query($sql);
$smarty->assign("zones", $sth->fetchAll());

$smarty->display("zone.tpl");

elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================

$smarty->display("zoneadd.tpl");

elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================

$sql = "SELECT zone_id, zone_soa, zone_hostmaster, zone_origin,
            zone_ttl_default, zone_refresh, zone_retry, zone_expire,
            zone_ttl, zone_serial, zone_ns1, zone_ns2, zone_ns3,
            zone_mx1, zone_mx2, zone_info
        FROM zone
        WHERE zone_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("zone", $sth->fetch(PDO::FETCH_OBJ));

$smarty->display("zoneview.tpl");

elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================

$sql = "SELECT zone_id, zone_soa, zone_hostmaster, zone_origin, zone_ttl_default,
            zone_refresh, zone_retry, zone_expire, zone_ttl, zone_serial,
            zone_ns1, zone_ns2, zone_ns3, zone_mx1, zone_mx2, zone_info
        FROM zone
        WHERE zone_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("zone", $sth->fetch(PDO::FETCH_OBJ));

$smarty->display("zoneedit.tpl");

elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================

$sth = $dbh->prepare("SELECT zone_id, zone_origin, zone_serial FROM zone WHERE zone_id=?");
$sth->execute([$id]);
$smarty->assign("zone", $sth->fetch(PDO::FETCH_OBJ));

$smarty->display("zonedel.tpl");

else:
// ========== ERROR UNKNOWN VARIANT ===========================================

echo "<p>Unknown function call: Please report to system development!</p>\n";

endif; // $action == ...
// ========== END OF VARIANTS =================================================

$smarty->display('footer.tpl');