Commit 392df2f1 authored by Ad Schellevis's avatar Ad Schellevis

(legacy) refactor vpn_ipsec_keys_edit.php

parent ddd26107
...@@ -31,80 +31,82 @@ require_once("guiconfig.inc"); ...@@ -31,80 +31,82 @@ require_once("guiconfig.inc");
require_once("vpn.inc"); require_once("vpn.inc");
require_once("services.inc"); require_once("services.inc");
if (!is_array($config['ipsec'])) { if (!isset($config['ipsec'])) {
$config['ipsec'] = array(); $config['ipsec'] = array();
} }
if (!is_array($config['ipsec']['mobilekey'])) { if (!isset($config['ipsec']['mobilekey'])) {
$config['ipsec']['mobilekey'] = array(); $config['ipsec']['mobilekey'] = array();
} else {
ipsec_mobilekey_sort();
} }
ipsec_mobilekey_sort();
$a_secret = &$config['ipsec']['mobilekey'];
if (is_numericint($_GET['id'])) { if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$id = $_GET['id']; $pconfig = array();
} if(isset($_GET['id']) && is_numericint($_GET['id']) && isset($config['ipsec']['mobilekey'][$_GET['id']])) {
if (isset($_POST['id']) && is_numericint($_POST['id'])) { // fetch record
$id = $_POST['id']; $id = $_GET['id'];
} $pconfig['ident'] = $config['ipsec']['mobilekey'][$id]['ident'];
$pconfig['psk'] = $config['ipsec']['mobilekey'][$id]['pre-shared-key'];
if (isset($id) && $a_secret[$id]) { } else {
$pconfig['ident'] = $a_secret[$id]['ident']; // init new
$pconfig['psk'] = $a_secret[$id]['pre-shared-key']; $pconfig['ident'] = '';
} $pconfig['psk'] = '';
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input_errors = array();
$pconfig = $_POST;
// fetch record number if valid
if (isset($_POST['id']) && is_numericint($_POST['id']) && isset($config['ipsec']['mobilekey'][$_POST['id']]) ) {
$id = $_POST['id'];
} else {
$id = null;
}
if ($_POST) { /* input validation */
$userids = array(); $userids = array();
foreach ($config['system']['user'] as $uid => $user) { foreach ($config['system']['user'] as $uid => $user) {
$userids[$user['name']] = $uid; $userids[$user['name']] = $uid;
} }
if (isset($pconfig['ident']) && array_key_exists($pconfig['ident'], $userids)) {
$input_errors[] = gettext("A user with this name already exists. Add the key to the user instead.");
}
unset($userids);
unset($input_errors);
$pconfig = $_POST;
/* input validation */
$reqdfields = explode(" ", "ident psk"); $reqdfields = explode(" ", "ident psk");
$reqdfieldsn = array(gettext("Identifier"),gettext("Pre-Shared Key")); $reqdfieldsn = array(gettext("Identifier"),gettext("Pre-Shared Key"));
do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors); do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
if (preg_match("/[^a-zA-Z0-9@\.\-]/", $_POST['ident'])) { if (empty($pconfig['ident']) || preg_match("/[^a-zA-Z0-9@\.\-]/", $pconfig['ident'])) {
$input_errors[] = gettext("The identifier contains invalid characters."); $input_errors[] = gettext("The identifier contains invalid characters.");
} }
if (array_key_exists($_POST['ident'], $userids)) { /* make sure there are no dupes on new entries */
$input_errors[] = gettext("A user with this name already exists. Add the key to the user instead."); $recidx = 0 ;
} foreach ($config['ipsec']['mobilekey'] as $secretent) {
unset($userids); if ($secretent['ident'] == $pconfig['ident'] && ($recidx != $id || $id === null)) {
$input_errors[] = gettext("Another entry with the same identifier already exists.");
if (!$input_errors && !(isset($id) && $a_secret[$id])) { break;
/* make sure there are no dupes */
foreach ($a_secret as $secretent) {
if ($secretent['ident'] == $_POST['ident']) {
$input_errors[] = gettext("Another entry with the same identifier already exists.");
break;
}
} }
$recidx++;
} }
if (!$input_errors) { if (count($input_errors) == 0) {
if (isset($id) && $a_secret[$id]) { $secretent = array();
$secretent = $a_secret[$id]; $secretent['ident'] = $pconfig['ident'];
} $secretent['pre-shared-key'] = $pconfig['psk'];
$secretent['ident'] = $_POST['ident'];
$secretent['pre-shared-key'] = $_POST['psk'];
$text = "";
if (isset($id) && $a_secret[$id]) { if ($id !== null) {
$a_secret[$id] = $secretent; // edit existing key
$text = gettext("Edited"); $config['ipsec']['mobilekey'][$id] = $secretent;
$config_write_text = gettext("Edited");
} else { } else {
$a_secret[] = $secretent; $config_write_text = gettext("Added");
$text = gettext("Added"); $config['ipsec']['mobilekey'][] = $secretent;
} }
write_config("{$text} IPsec Pre-Shared Keys"); write_config("{$config_write_text} IPsec Pre-Shared Keys");
mark_subsystem_dirty('ipsec'); mark_subsystem_dirty('ipsec');
header("Location: vpn_ipsec_keys.php"); header("Location: vpn_ipsec_keys.php");
...@@ -112,9 +114,12 @@ if ($_POST) { ...@@ -112,9 +114,12 @@ if ($_POST) {
} }
} }
$pgtitle = gettext("VPN: IPsec: Edit Pre-Shared Key"); $pgtitle = gettext("VPN: IPsec: Edit Pre-Shared Key");
$shortcut_section = "ipsec"; $shortcut_section = "ipsec";
legacy_html_escape_form_data($pconfig);
include("head.inc"); include("head.inc");
?> ?>
...@@ -123,62 +128,58 @@ include("head.inc"); ...@@ -123,62 +128,58 @@ include("head.inc");
<?php include("fbegin.inc"); ?> <?php include("fbegin.inc"); ?>
<section class="page-content-main"> <section class="page-content-main">
<div class="container-fluid"> <div class="container-fluid">
<div class="row"> <div class="row">
<?php if (isset($input_errors) && count($input_errors) > 0) { <?php if (isset($input_errors) && count($input_errors) > 0) {
print_input_errors($input_errors); print_input_errors($input_errors);
} ?> }
?>
<section class="col-xs-12"> <section class="col-xs-12">
<div class="content-box">
<div class="content-box"> <form action="vpn_ipsec_keys_edit.php" method="post" name="iform" id="iform">
<div class="table-responsive">
<form action="vpn_ipsec_keys_edit.php" method="post" name="iform" id="iform"> <table class="table table-striped">
<tr>
<div class="table-responsive"> <td><a id="help_for_ident" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Identifier"); ?></td>
<table class="table table-striped table-sort"> <td>
<tr> <input name="ident" type="text" class="formfld unknown" id="ident" size="30" value="<?=$pconfig['ident'];?>" />
<td valign="top" class="vncellreq"><?=gettext("Identifier"); ?></td> <div class="hidden" for="help_for_ident">
<td class="vtable"> <?=gettext("This can be either an IP address, fully qualified domain name or an e-mail address"); ?>.
<input name="ident" type="text" class="formfld unknown" id="ident" size="30" value="<?=htmlspecialchars($pconfig['ident']);?>" /> </div>
<br /> </td>
<?=gettext("This can be either an IP address, fully qualified domain name or an e-mail address"); ?>. </tr>
</td> <tr>
</tr> <td><i class="fa fa-info-circle text-muted"></i> <?=gettext("Pre-Shared Key"); ?></td>
<tr> <td>
<td width="22%" valign="top" class="vncellreq"><?=gettext("Pre-Shared Key"); ?></td> <input name="psk" type="text" class="formfld unknown" id="psk" size="40" value="<?=$pconfig['psk'];?>" />
<td width="78%" class="vtable"> </td>
<input name="psk" type="text" class="formfld unknown" id="psk" size="40" value="<?=htmlspecialchars($pconfig['psk']);?>" /> </tr>
</td> <tr>
</tr> <td>&nbsp;</td>
<tr> <td>
<td width="22%" valign="top">&nbsp;</td> <input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" />
<td width="78%"> <?php if (isset($id) && isset($config['ipsec']['mobilekey'][$id])) :
<input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" />
<?php if (isset($id) && $a_secret[$id]) :
?> ?>
<input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" /> <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
<?php <?php
endif; ?> endif; ?>
</td> </td>
</tr> </tr>
</table> <tr>
</div> <td>&nbsp;</td>
<td>
<div class="col-xs-12"> <span class="text-danger">
<span class="vexpl"> <strong><?=gettext("Note"); ?>:<br /></strong>
<span class="text-danger"> </span>
<strong><?=gettext("Note"); ?>:<br /></strong> <?=gettext("PSK for any user can be set by using an identifier of any/ANY");?>
</span> </td>
<?=gettext("PSK for any user can be set by using an identifier of any/ANY");?> </tr>
</span> </table>
</div> </div>
</form> </form>
</div> </div>
</section> </section>
</div> </div>
</div> </div>
</section> </section>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment