From 32bd5920981f8be8f39787cd78d47dc73294325d Mon Sep 17 00:00:00 2001 From: Thomas Hooge Date: Fri, 3 Mar 2023 19:33:52 +0100 Subject: [PATCH] Refactored user, vlan and zone --- submit.php | 26 ++++---- tpl/user.tpl | 4 +- tpl/useradd.tpl | 5 +- tpl/userdel.tpl | 9 ++- tpl/useredit.tpl | 7 +-- tpl/userview.tpl | 8 +-- tpl/vlan.tpl | 4 +- tpl/vlanadd.tpl | 5 +- tpl/vlandel.tpl | 9 ++- tpl/vlanedit.tpl | 7 +-- tpl/vlanview.tpl | 6 +- tpl/zone.tpl | 4 +- tpl/zoneadd.tpl | 5 +- tpl/zonedel.tpl | 11 ++-- tpl/zoneedit.tpl | 9 ++- tpl/zoneview.tpl | 4 +- user.php | 152 ++++++++++++++++++++++++++++++++++++++++++++++- useradd.php | 27 --------- userdel.php | 28 --------- useredit.php | 39 ------------ userview.php | 35 ----------- vlan.php | 123 +++++++++++++++++++++++++++++++++++++- vlanadd.php | 18 ------ vlandel.php | 26 -------- vlanedit.php | 28 --------- vlanview.php | 37 ------------ zone.php | 149 ++++++++++++++++++++++++++++++++++++++++++++-- zoneadd.php | 16 ----- zonedel.php | 23 ------- zoneedit.php | 27 --------- zoneview.php | 29 --------- 31 files changed, 473 insertions(+), 407 deletions(-) delete mode 100644 useradd.php delete mode 100644 userdel.php delete mode 100644 useredit.php delete mode 100644 userview.php delete mode 100644 vlanadd.php delete mode 100644 vlandel.php delete mode 100644 vlanedit.php delete mode 100644 vlanview.php delete mode 100644 zoneadd.php delete mode 100644 zonedel.php delete mode 100644 zoneedit.php delete mode 100644 zoneview.php diff --git a/submit.php b/submit.php index 144afb4..1397421 100644 --- a/submit.php +++ b/submit.php @@ -119,7 +119,7 @@ if (isset($_POST['add'])) { header_location("assetview.php?asset_id=" . $dbh->lastInsertId()); break; -/* refactored +/* case ("assetclass") : $name = sanitize($_POST['assetclass_name']); $description = sanitize($_POST['assetclass_description']); @@ -304,7 +304,7 @@ if (isset($_POST['add'])) { header_location("subnetview.php?subnet_id=" . $subnet_id); break; - case ("user") : +/* case ("user") : $user_name = strtolower(sanitize($_POST['user_name'])); $user_displayname = sanitize($_POST['user_displayname']); $user_password = md5(sanitize($_POST['user_password'])); @@ -338,7 +338,7 @@ if (isset($_POST['add'])) { $sth->execute([$vlan_name, $vlan_number, $vlan_color, $vlan_info]); header_location("vlanview.php?vlan_id=" . $dbh->lastInsertId()); - break; + break; */ case ("vlansubnet") : $subnet_id = sanitize($_POST['subnet_id']); @@ -352,7 +352,7 @@ if (isset($_POST['add'])) { header_location("vlanview.php?vlan_id=" . $vlan_id); break; - case ("zone") : +/* case ("zone") : $zone_origin = sanitize($_POST['zone_origin']); $zone_ttl_default = sanitize($_POST['zone_ttl_default']); $zone_soa = sanitize($_POST['zone_soa']); @@ -380,7 +380,7 @@ if (isset($_POST['add'])) { $zone_ns1, $zone_ns2, $zone_ns3, $zone_mx1, $zone_mx2, $zone_info]); header_location("zoneview.php?zone_id=" . $dbh->lastInsertId()); - break; + break; */ } } @@ -398,7 +398,7 @@ if (isset($_POST['del'])) { header_location("asset.php"); break; -/* refactored +/* case ("assetclass") : $assetclass_id = sanitize($_POST['assetclass_id']); @@ -487,7 +487,7 @@ if (isset($_POST['del'])) { header_location("subnetview.php?subnet_id=" . $subnet_id); break; - case ("user") : +/* case ("user") : $user_id = sanitize($_POST['user_id']); $sth = $dbh->prepare("DELETE FROM user WHERE user_id=?"); @@ -503,7 +503,7 @@ if (isset($_POST['del'])) { $sth->execute([$vlan_id]); header_location("vlan.php"); - break; + break; */ case ("vlansubnet") : $subnet_id = sanitize($_POST['subnet_id']); @@ -515,14 +515,14 @@ if (isset($_POST['del'])) { header_location("vlanview.php?vlan_id=" . $vlan_id); break; - case ("zone") : +/* case ("zone") : $zone_id = sanitize($_POST['zone_id']); $sth = $dbh->prepare("DELETE FROM zone WHERE zone_id=?"); $sth->execute([$zone_id]); header_location("zone.php"); - break; + break; */ } } @@ -548,7 +548,7 @@ if (isset($_POST['edit'])) { $asset_id]); header_location("assetview.php?asset_id=" . $asset_id); -/* refactored +/* case ("assetclass") : $id = sanitize($_POST['assetclass_id']); $name = sanitize($_POST['assetclass_name']); @@ -729,7 +729,7 @@ if (isset($_POST['edit'])) { header_location("subnetview.php?subnet_id=" . $subnet_id); break; - case ("user") : +/* case ("user") : $user_id = sanitize($_POST['user_id']); $user_name = sanitize($_POST['user_name']); $user_displayname = sanitize($_POST['user_displayname']); @@ -803,7 +803,7 @@ if (isset($_POST['edit'])) { $id]); header_location("zoneview.php?zone_id=" . $zone_id); - break; + break; */ } } diff --git a/tpl/user.tpl b/tpl/user.tpl index c9b3a5b..baee6fa 100644 --- a/tpl/user.tpl +++ b/tpl/user.tpl @@ -5,7 +5,7 @@ {$lang_users} ({$users|@count}) - {$lang_user_add} + {$lang_user_add} @@ -28,7 +28,7 @@ {foreach item=user from=$users} - {$user.name} + {$user.name} {$user.realm} diff --git a/tpl/useradd.tpl b/tpl/useradd.tpl index 43f5026..62848ff 100644 --- a/tpl/useradd.tpl +++ b/tpl/useradd.tpl @@ -1,5 +1,4 @@ -
- + @@ -9,7 +8,7 @@
{$lang_cancel} - +
diff --git a/tpl/userdel.tpl b/tpl/userdel.tpl index 4d60f8f..9ac864f 100644 --- a/tpl/userdel.tpl +++ b/tpl/userdel.tpl @@ -1,6 +1,5 @@ - - - + + @@ -10,7 +9,7 @@
{$lang_cancel} - +
@@ -29,7 +28,7 @@ {$lang_user_name} - {$user->name} + {$user->name} diff --git a/tpl/useredit.tpl b/tpl/useredit.tpl index bbe14c6..f11e167 100644 --- a/tpl/useredit.tpl +++ b/tpl/useredit.tpl @@ -1,6 +1,5 @@ - - - + + @@ -10,7 +9,7 @@
{$lang_cancel} - +
diff --git a/tpl/userview.tpl b/tpl/userview.tpl index c920811..a2bc45b 100644 --- a/tpl/userview.tpl +++ b/tpl/userview.tpl @@ -2,11 +2,11 @@ - {$user_name} + {$user->name} - {$lang_user_edit} - {$lang_user_del} + {$lang_user_edit} + {$lang_user_del} @@ -25,7 +25,7 @@ {$lang_user_name} - {$user->name} + {$user->name} diff --git a/tpl/vlan.tpl b/tpl/vlan.tpl index 71ed3a9..6f10d07 100644 --- a/tpl/vlan.tpl +++ b/tpl/vlan.tpl @@ -6,7 +6,7 @@ {if $suser_add || $suser_admin} - {$lang_vlan_add} + {$lang_vlan_add} {/if} @@ -31,7 +31,7 @@ #{$vlan.color} - {$vlan.name} + {$vlan.name} {$vlan.info} diff --git a/tpl/vlanadd.tpl b/tpl/vlanadd.tpl index 3fc12eb..d998f4e 100644 --- a/tpl/vlanadd.tpl +++ b/tpl/vlanadd.tpl @@ -1,5 +1,4 @@ - - + @@ -8,7 +7,7 @@
{$lang_cancel} - +
diff --git a/tpl/vlandel.tpl b/tpl/vlandel.tpl index 3a59c3a..1e8e404 100644 --- a/tpl/vlandel.tpl +++ b/tpl/vlandel.tpl @@ -1,6 +1,5 @@ - - - + + @@ -9,7 +8,7 @@
{$lang_cancel} - +
@@ -28,7 +27,7 @@ {$lang_vlan_name} - {$vlan->name} ({$vlan->number}) + {$vlan->name} ({$vlan->number}) diff --git a/tpl/vlanedit.tpl b/tpl/vlanedit.tpl index 8252072..bdbe5de 100644 --- a/tpl/vlanedit.tpl +++ b/tpl/vlanedit.tpl @@ -1,6 +1,5 @@ - - - + + @@ -10,7 +9,7 @@
{$lang_cancel} - +
diff --git a/tpl/vlanview.tpl b/tpl/vlanview.tpl index 52ee51b..1e0a2e9 100644 --- a/tpl/vlanview.tpl +++ b/tpl/vlanview.tpl @@ -9,10 +9,10 @@ {$lang_assignvlantosubnet} {/if} {if $suser_edit} - {$lang_vlan_edit} + {$lang_vlan_edit} {/if} {if $suser_delete} - {$lang_vlan_del} + {$lang_vlan_del} {/if} @@ -32,7 +32,7 @@ {$lang_vlan_name} - {$vlan->name} + {$vlan->name} diff --git a/tpl/zone.tpl b/tpl/zone.tpl index c180d5f..810b0dd 100644 --- a/tpl/zone.tpl +++ b/tpl/zone.tpl @@ -6,7 +6,7 @@ {if $suser_add || $suser_admin} - {$lang_zone_add} + {$lang_zone_add} {/if} @@ -27,7 +27,7 @@ {foreach item=zone from=$zones} - {$zone.origin} + {$zone.origin} {$zone.hostmaster} diff --git a/tpl/zoneadd.tpl b/tpl/zoneadd.tpl index 6a124b5..50fed7c 100644 --- a/tpl/zoneadd.tpl +++ b/tpl/zoneadd.tpl @@ -1,5 +1,4 @@ - - + @@ -8,7 +7,7 @@
{$lang_cancel} - +
diff --git a/tpl/zonedel.tpl b/tpl/zonedel.tpl index c7a6f77..8822fee 100644 --- a/tpl/zonedel.tpl +++ b/tpl/zonedel.tpl @@ -1,6 +1,5 @@ - - - + + @@ -9,7 +8,7 @@
{$lang_cancel} - +
@@ -28,8 +27,8 @@ {$lang_zone} - {$zone.zone_origin} - ({$zone.zone_serial}) + {$zone->zone_origin} + ({$zone->zone_serial}) diff --git a/tpl/zoneedit.tpl b/tpl/zoneedit.tpl index b705992..b3341c7 100644 --- a/tpl/zoneedit.tpl +++ b/tpl/zoneedit.tpl @@ -1,6 +1,5 @@ - - - + + @@ -10,7 +9,7 @@
{$lang_cancel} - +
@@ -141,7 +140,7 @@ Zone Info - + diff --git a/tpl/zoneview.tpl b/tpl/zoneview.tpl index 1b2631e..7f8e809 100644 --- a/tpl/zoneview.tpl +++ b/tpl/zoneview.tpl @@ -6,10 +6,10 @@ {if $suser_edit || $suser_admin} - {$lang_zone_edit} + {$lang_zone_edit} {/if} {if $suser_delete || $suser_admin} - {$lang_zone_del} + {$lang_zone_del} {/if} diff --git a/user.php b/user.php index 595fc46..03449d3 100644 --- a/user.php +++ b/user.php @@ -13,8 +13,90 @@ if (($_SESSION['suser_role_admin'] == 0) and ($_SESSION['suser_role_manage'] == header_location('comments.php?comments=accessdenied'); } +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': + $user_name = strtolower(sanitize($_POST['user_name'])); + $user_displayname = sanitize($_POST['user_displayname']); + $user_password = md5(sanitize($_POST['user_password'])); + + // check if username exists + $sth = $dbh->prepare("SELECT COUNT(*) FROM user WHERE user_name=?"); + $sth->execute([$user_name]); + + if ($sth->fetchColumn() == 0) { + $sql = "INSERT INTO user (user_name, user_displayname, user_pass) + VALUE (?, ?, ?)"; + $sth = $dbh->prepare($sql); + $sth->execute([$user_name, $user_displayname, $user_password]); + $id = $dbh->lastInsertId(); + $action = ACT_VIEW; + } else { + $g_error->Add(_("Username already in use.")); + $action = ACT_ADD; + } + break; + + case 'update': + $user_name = sanitize($_POST['user_name']); + $user_displayname = sanitize($_POST['user_displayname']); + $user_realm = sanitize($_POST['user_realm']); + // roles + $role_add = sanitize($_POST['role_add']); + $role_edit = sanitize($_POST['role_edit']); + $role_delete = sanitize($_POST['role_delete']); + $role_manage = sanitize($_POST['role_manage']); + $role_admin = sanitize($_POST['role_admin']); + + // construct menu set + $role = array(); + if ($role_add) $role[] = 'add'; + if ($role_edit) $role[] = 'edit'; + if ($role_delete) $role[] = 'delete'; + if ($role_manage) $role[] = 'manage'; + if ($role_admin) $role[] = 'admin'; + + $sql = "UPDATE user SET + user_name=?, user_displayname=?, user_realm=?, + user_role=? + WHERE user_id=?"; + $sth = $dbh->prepare($sql); + $sth->execute([$user_name ,$user_displayname, $user_realm, + implode(',', $role), $id]); + $action = ACT_VIEW; + break; + + case 'delete': + $sth = $dbh->prepare("DELETE FROM user WHERE user_id=?"); + $sth->execute([$id]); + $g_message->Add(_("User deleted.")); + $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 user_id AS id, user_name AS name, user_displayname AS displayname, user_realm AS realm, user_role AS role @@ -27,10 +109,74 @@ $users = $sth->fetchAll(PDO::FETCH_ASSOC); for($i = 0; $i < count($users); $i++) { $users[$i]['role'] = explode(',', $users[$i]['role'] ); } - $smarty->assign("users", $users); $smarty->display("user.tpl"); - -include("footer.php"); + +elseif ($action == ACT_ADD): +// ========== VARIANT: add record ============================================= + +$realms = db_load_enum('user','user_realm'); + +$smarty->assign("realm_ids", $realms); +$smarty->assign("realm_names", $realms); +$smarty->assign("realm_selected", $realms[0]); + +$smarty->display("useradd.tpl"); + +elseif ($action == ACT_VIEW): +// ========== VARIANT: view single record ===================================== + +$sql = "SELECT user_id AS id, user_name AS name, user_displayname AS displayname, + user_realm as realm, user_role AS role, user_flags AS flags + FROM user + WHERE user_id=?"; +$sth = $dbh->prepare($sql); +$sth->execute([$id]); +$user = $sth->fetch(PDO::FETCH_OBJ); +$user->role = explode(',', $user->role); +$user->flags = explode(',', $user->flags); +$smarty->assign("user", $user); + +$smarty->display("userview.tpl"); + +elseif ($action == ACT_EDIT): +// ========== VARIANT: edit single record ===================================== + +$sql = "SELECT user_id AS id, user_name AS name, user_displayname AS displayname, + user_realm AS realm, user_role AS role, user_flags AS flags + FROM user + WHERE user_id=?"; +$sth = $dbh->prepare($sql); +$sth->execute([$id]); + +$user = $sth->fetch(PDO::FETCH_OBJ); +$user->role = explode(',', $user->role); +$smarty->assign("user", $user); + +// auth realms +$smarty->assign("realm_ids", ['local', 'ldap']); +$smarty->assign("realm_names", ['Local', 'LDAP']); +$smarty->assign("realm_selected", $user->realm); + +$smarty->display("useredit.tpl"); + +elseif ($action == ACT_DELETE): +// ========== VARIANT: delete record ========================================== + +$sth = $dbh->prepare("SELECT user_id AS id, user_name AS name FROM user WHERE user_id=?"); +$sth->execute([$id]); +$smarty->assign("user", $sth->fetch(PDO::FETCH_OBJ)); + +$smarty->display("userdel.tpl"); + +else: +// ========== ERROR UNKNOWN VARIANT =========================================== + +echo "

Unknown function call: Please report to system development!

\n"; + +endif; // $action == ... +// ========== END OF VARIANTS ================================================= + +$smarty->display('footer.tpl'); ?> diff --git a/useradd.php b/useradd.php deleted file mode 100644 index f64b999..0000000 --- a/useradd.php +++ /dev/null @@ -1,27 +0,0 @@ -assign("realm_ids", $realms); -$smarty->assign("realm_names", $realms); -$smarty->assign("realm_selected", $realms[0]); - -$smarty->display("useradd.tpl"); - -include("footer.php"); -?> diff --git a/userdel.php b/userdel.php deleted file mode 100644 index fd2dad3..0000000 --- a/userdel.php +++ /dev/null @@ -1,28 +0,0 @@ -prepare("SELECT user_id AS id user_name AS user_name FROM user WHERE user_id=?"); -$dbh->execute([$user_id]); - -$smarty->assign("user", $sth->fetch(PDO::FETCH_OBJ)); - -$smarty->display("userdel.tpl"); - -include("footer.php"); -?> diff --git a/useredit.php b/useredit.php deleted file mode 100644 index 8cfbd96..0000000 --- a/useredit.php +++ /dev/null @@ -1,39 +0,0 @@ -prepare($sql); -$sth->execute([$user_id]); - -$user = $sth->fetch(PDO::FETCH_OBJ); -$user->role = explode(',', $user->role); -$smarty->assign("user", $user); - -// auth realms -$smarty->assign("realm_ids", ['local', 'ldap']); -$smarty->assign("realm_names", ['Local', 'LDAP']); -$smarty->assign("realm_selected", $user->realm); - -$smarty->display("useredit.tpl"); - -include("footer.php"); -?> diff --git a/userview.php b/userview.php deleted file mode 100644 index fe9d518..0000000 --- a/userview.php +++ /dev/null @@ -1,35 +0,0 @@ -prepare($sql); -$sth->execute([$user_id]); - -$user = $sth->fetch(PDO::FETCH_OBJ); -$user->role = explode(',', $user->role); -$user->flags = explode(',', $user->flags); -$smarty->assign("user", $user); - -$smarty->display("userview.tpl"); - -include("footer.php"); -?> diff --git a/vlan.php b/vlan.php index 2e35d39..75a8f94 100644 --- a/vlan.php +++ b/vlan.php @@ -8,8 +8,68 @@ 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': + $vlan_name = sanitize($_POST['vlan_name']); + $vlan_number = sanitize($_POST['vlan_number']); + $vlan_info = sanitize($_POST['vlan_info']); + $vlan_color = sanitize($_POST['vlan_color']); + + $sql = "INSERT INTO vlan (vlan_name, vlan_number, vlan_color, vlan_info) + VALUE (?, ?, ?, ?)"; + $sth = $dbh->prepare($sql); + $sth->execute([$vlan_name, $vlan_number, $vlan_color, $vlan_info]); + + $id = $dbh->lastInsertId(); + $action = ACT_VIEW; + break; + + case 'update': + $vlan_name = sanitize($_POST['vlan_name']); + $vlan_number = sanitize($_POST['vlan_number']); + $vlan_info = sanitize($_POST['vlan_info']); + $vlan_color = sanitize($_POST['vlan_color']); + + $sql = "UPDATE vlan SET vlan_name=?, vlan_number=?, vlan_color=?, vlan_info=? WHERE vlan_id=?"; + $sth = $dbh->prepare($sql); + $sth->execute([$vlan_name, $vlan_number, $vlan_color, $vlan_info, $id]); + $action = ACT_VIEW; + break; + + case 'delete': + $sth = $dbh->prepare("DELETE FROM vlan WHERE vlan_id=?"); + $sth->execute([$id]); + $g_message->Add(_("VLAN deleted.")); + $action = ACT_DEFAULT; + break; + + default: + $g_error->Add(submit_error($submit)); + $valid = FALSE; +} + +// ========== ACTIONS END ===================================================== + +$smarty->assign("scripts", 'jscolor.js'); include("header.php"); +if ($action == ACT_DEFAULT): +// ========== VARIANT: default behavior ======================================= + $sql = "SELECT vlan_id AS id, vlan_number AS number, vlan_name AS name, vlan_color AS color, LEFT(vlan_info, 60) AS info FROM vlan @@ -19,5 +79,66 @@ $smarty->assign("vlans", $sth->fetchAll()); $smarty->display("vlan.tpl"); -include("footer.php"); +elseif ($action == ACT_ADD): +// ========== VARIANT: add record ============================================= + +$smarty->display("vlanadd.tpl"); + +elseif ($action == ACT_VIEW): +// ========== VARIANT: view single record ===================================== + +// vlan +$sql = "SELECT vlan_id AS id, vlan_name AS name, vlan_number AS number, + vlan_info AS info, vlan_color AS color + FROM vlan + WHERE vlan_id=?"; +$sth = $dbh->prepare($sql); +$sth->execute([$id]); +$smarty->assign("vlan", $sth->fetch(PDO::FETCH_OBJ)); + +// subnets +$sql = "SELECT s.subnet_id, s.subnet_address, s.subnet_mask, s.subnet_info + FROM subnet AS s LEFT JOIN subnetvlan AS v USING (subnet_id) + WHERE v.vlan_id=? + ORDER BY INET_ATON(s.subnet_address)"; +$sth = $dbh->prepare($sql); +$sth->execute([$id]); +$smarty->assign("subnets", $sth->fetchAll()); + +$smarty->display("vlanview.tpl"); + +elseif ($action == ACT_EDIT): +// ========== VARIANT: edit single record ===================================== + +$sql = "SELECT vlan_id AS id, vlan_name AS name, vlan_number AS number, + vlan_color AS color, vlan_info AS info + FROM vlan + WHERE vlan_id=?"; +$sth = $dbh->prepare($sql); +$sth->execute([$id]); +$smarty->assign("vlan", $sth->fetch(PDO::FETCH_OBJ)); + +$smarty->display("vlanedit.tpl"); + +elseif ($action == ACT_DELETE): +// ========== VARIANT: delete record ========================================== + +$sql = "SELECT vlan_id AS id, vlan_name AS name, vlan_number AS number + FROM vlan + WHERE vlan_id=?"; +$sth = $dbh->prepare($sql); +$sth->execute([$id]); +$smarty->assign("vlan", $sth->fetch(PDO::FETCH_OBJ)); + +$smarty->display("vlandel.tpl"); + +else: +// ========== ERROR UNKNOWN VARIANT =========================================== + +echo "

Unknown function call: Please report to system development!

\n"; + +endif; // $action == ... +// ========== END OF VARIANTS ================================================= + +$smarty->display('footer.tpl'); ?> diff --git a/vlanadd.php b/vlanadd.php deleted file mode 100644 index f169b93..0000000 --- a/vlanadd.php +++ /dev/null @@ -1,18 +0,0 @@ -assign("scripts", 'jscolor.js'); -include("header.php"); - -$smarty->display("vlanadd.tpl"); - -include("footer.php"); -?> diff --git a/vlandel.php b/vlandel.php deleted file mode 100644 index 6b73b3a..0000000 --- a/vlandel.php +++ /dev/null @@ -1,26 +0,0 @@ -prepare($sql); -$sth->execute([$vlan_id]); -$smarty->assign("vlan", $sth->fetch(PDO::FETCH_OBJ)); - -$smarty->display("vlandel.tpl"); - -include("footer.php"); -?> diff --git a/vlanedit.php b/vlanedit.php deleted file mode 100644 index c40dc5e..0000000 --- a/vlanedit.php +++ /dev/null @@ -1,28 +0,0 @@ -assign("scripts", 'jscolor.js'); -include("header.php"); - -$sql = "SELECT vlan_id AS id, vlan_name AS name, vlan_number AS number, - vlan_color AS color, vlan_info AS info - FROM vlan - WHERE vlan_id=?"; -$sth = $dbh->prepare($sql); -$sth->execute([$vlan_id]); -$smarty->assign("vlan", $sth->fetch(PDO::FETCH_OBJ)); - -$smarty->display("vlanedit.tpl"); - -include("footer.php"); -?> diff --git a/vlanview.php b/vlanview.php deleted file mode 100644 index ff8fdf9..0000000 --- a/vlanview.php +++ /dev/null @@ -1,37 +0,0 @@ -prepare($sql); -$sth->execute([$vlan_id]); -$smarty->assign("vlan", $sth->fetch(PDO::FETCH_OBJ)); - -// subnets -$sql = "SELECT s.subnet_id, s.subnet_address, s.subnet_mask, s.subnet_info - FROM subnet AS s LEFT JOIN subnetvlan AS v USING (subnet_id) - WHERE v.vlan_id=? - ORDER BY INET_ATON(s.subnet_address)"; -$sth = $dbh->prepare($sql); -$sth->execute([$vlan_id]); -$smarty->assign("subnets", $sth->fetchAll()); - -$smarty->display("vlanview.tpl"); - -include("footer.php"); -?> diff --git a/zone.php b/zone.php index 9c076ca..bf63a13 100644 --- a/zone.php +++ b/zone.php @@ -8,9 +8,99 @@ 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("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 @@ -19,6 +109,57 @@ $sth = $dbh->query($sql); $smarty->assign("zones", $sth->fetchAll()); $smarty->display("zone.tpl"); - -include("footer.php"); + +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 "

Unknown function call: Please report to system development!

\n"; + +endif; // $action == ... +// ========== END OF VARIANTS ================================================= + +$smarty->display('footer.tpl'); ?> diff --git a/zoneadd.php b/zoneadd.php deleted file mode 100644 index 403dd08..0000000 --- a/zoneadd.php +++ /dev/null @@ -1,16 +0,0 @@ -display("zoneadd.tpl"); - -include("footer.php"); -?> \ No newline at end of file diff --git a/zonedel.php b/zonedel.php deleted file mode 100644 index e827c4f..0000000 --- a/zonedel.php +++ /dev/null @@ -1,23 +0,0 @@ -prepare("SELECT zone_id, zone_origin, zone_serial FROM zone WHERE zone_id=?"); -$sth->execute($sql); -$smarty->assign("zone", $sth->fetchAll(PDO::FETCH_ASSOC)); - -$smarty->display("zonedel.tpl"); - -include("footer.php"); -?> \ No newline at end of file diff --git a/zoneedit.php b/zoneedit.php deleted file mode 100644 index ba7d8b9..0000000 --- a/zoneedit.php +++ /dev/null @@ -1,27 +0,0 @@ -prepare($sql); -$sth->execute([$zone_id]); -$smarty->assign("zone", $sth->fetch(PDO::FETCH_OBJ)); - -$smarty->display("zoneedit.tpl"); - -include("footer.php"); -?> diff --git a/zoneview.php b/zoneview.php deleted file mode 100644 index 61ed164..0000000 --- a/zoneview.php +++ /dev/null @@ -1,29 +0,0 @@ -prepare($sql); -$sth->execute([$zone_id]); -$smarty->assign("zone", $sth->fetch(PDO::FETCH_OBJ)); - -$smarty->display("zoneview.tpl"); - -include("footer.php"); -?>