Merge pull request #1450 from RaspAP/feat/form-validation

Custom Bootstrap form validation for required fields
This commit is contained in:
Bill Zimmerman
2023-11-11 09:39:57 +01:00
committed by GitHub
20 changed files with 327 additions and 48 deletions

View File

@@ -95,7 +95,9 @@ function saveDHCPConfig($status)
if (empty($errors)) {
$return = updateDHCPConfig($iface,$status);
} else {
$status->addMessage($errors, 'danger');
foreach ($errors as $error) {
$status->addMessage($error, 'danger');
}
}
if ($return == 1) {
$status->addMessage('Dnsmasq configuration failed to be updated.', 'danger');
@@ -118,41 +120,42 @@ function saveDHCPConfig($status)
/**
* Validates DHCP user input from the $_POST object
*
* @return string $errors
* @return array $errors
*/
function validateDHCPInput()
{
$errors = [];
define('IFNAMSIZ', 16);
$iface = $_POST['interface'];
if (!preg_match('/^[^\s\/\\0]+$/', $iface)
|| strlen($iface) >= IFNAMSIZ
) {
$errors .= _('Invalid interface name.').'<br />'.PHP_EOL;
$errors[] = _('Invalid interface name.');
}
if (!filter_var($_POST['StaticIP'], FILTER_VALIDATE_IP) && !empty($_POST['StaticIP'])) {
$errors .= _('Invalid static IP address.').'<br />'.PHP_EOL;
$errors[] = _('Invalid static IP address.');
}
if (!filter_var($_POST['SubnetMask'], FILTER_VALIDATE_IP) && !empty($_POST['SubnetMask'])) {
$errors .= _('Invalid subnet mask.').'<br />'.PHP_EOL;
$errors[] = _('Invalid subnet mask.');
}
if (!filter_var($_POST['DefaultGateway'], FILTER_VALIDATE_IP) && !empty($_POST['DefaultGateway'])) {
$errors .= _('Invalid default gateway.').'<br />'.PHP_EOL;
$errors[] = _('Invalid default gateway.');
}
if (($_POST['dhcp-iface'] == "1")) {
if (!filter_var($_POST['RangeStart'], FILTER_VALIDATE_IP) && !empty($_POST['RangeStart'])) {
$errors .= _('Invalid DHCP range start.').'<br />'.PHP_EOL;
$errors[] = _('Invalid DHCP range start.');
}
if (!filter_var($_POST['RangeEnd'], FILTER_VALIDATE_IP) && !empty($_POST['RangeEnd'])) {
$errors .= _('Invalid DHCP range end.').'<br />'.PHP_EOL;
$errors[] = _('Invalid DHCP range end.');
}
if (!ctype_digit($_POST['RangeLeaseTime']) && $_POST['RangeLeaseTimeUnits'] !== 'i') {
$errors .= _('Invalid DHCP lease time, not a number.').'<br />'.PHP_EOL;
$errors[] = _('Invalid DHCP lease time, not a number.');
}
if (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'i'))) {
$errors .= _('Unknown DHCP lease time unit.').'<br />'.PHP_EOL;
$errors[] = _('Unknown DHCP lease time unit.');
}
if ($_POST['Metric'] !== '' && !ctype_digit($_POST['Metric'])) {
$errors .= _('Invalid metric value, not a number.').'<br />'.PHP_EOL;
$errors[] = _('Invalid metric value, not a number.');
}
}
return $errors;

View File

@@ -435,18 +435,25 @@ function GetDistString($input, $string, $offset, $separator)
}
/**
*
* @param array $arrConfig
* Parses a configuration file
* Options and values are mapped with "=" characters
* Optional $wg flag is used for parsing WireGuard .conf files
* @param array $arrConfig
* @param boolean $wg
* @return $config
*/
function ParseConfig($arrConfig)
function ParseConfig($arrConfig, $wg = false)
{
$config = array();
foreach ($arrConfig as $line) {
$line = trim($line);
if ($line == "" || $line[0] == "#") {
$config[$option] = null;
continue;
if ($wg) {
$config[$option] = null;
continue;
} else {
continue;
}
}
if (strpos($line, "=") !== false) {

View File

@@ -10,15 +10,15 @@
<img src="app/img/raspAP-logo.php" class="navbar-logo" width="64" height="64">
</div>
<div class="col-xs ml-2">
<div class="ml-1">Status</div>
<div class="ml-1 sb-status">Status</div>
<div class="info-item-xs"><span class="icon">
<i class="fas fa-circle <?php echo ($hostapd_led); ?>"></i></span> <?php echo _("Hotspot").' '. _($hostapd_status); ?>
</div>
<div class="info-item-xs"><span class="icon">
<i class="fas fa-circle <?php echo ($memused_led); ?>"></i></span> <?php echo _("Memory Use").': '. htmlspecialchars(strval($memused), ENT_QUOTES); ?>%
<i class="fas fa-circle <?php echo ($memused_led); ?>"></i></span> <?php echo _("Mem Use").': '. htmlspecialchars(strval($memused), ENT_QUOTES); ?>%
</div>
<div class="info-item-xs"><span class="icon">
<i class="fas fa-circle <?php echo ($cputemp_led); ?>"></i></span> <?php echo _("CPU Temp").': '. htmlspecialchars($cputemp, ENT_QUOTES); ?>°C
<i class="fas fa-circle <?php echo ($cputemp_led); ?>"></i></span> <?php echo _("CPU").': '. htmlspecialchars($cputemp, ENT_QUOTES); ?>°C
</div>
</div>
</div>
@@ -57,7 +57,7 @@
<?php endif; ?>
<?php if (RASPI_WIREGUARD_ENABLED) : ?>
<li class="nav-item">
<a class="nav-link" href="wg_conf"><span class="ra-wireguard mr-2"></span><span class="nav-label"><?php echo _("WireGuard"); ?></a>
<a class="nav-link" href="wg_conf"><i class="ra-wireguard mr-2"></i><span class="nav-label"><?php echo _("WireGuard"); ?></a>
</li>
<?php endif; ?>
<?php if (RASPI_VPN_PROVIDER_ENABLED) : ?>

View File

@@ -46,10 +46,10 @@ if ($cputemp > 70) {
// hostapd status
$hostapd = $system->hostapdStatus();
if ($hostapd[0] ==1) {
$hostapd_status = "active";
$hostapd_status = "up";
$hostapd_led = "service-status-up";
} else {
$hostapd_status = "inactive";
$hostapd_status = "down";
$hostapd_led = "service-status-down";
}

View File

@@ -8,6 +8,7 @@ require_once 'config.php';
function DisplayWireGuardConfig()
{
$status = new \RaspAP\Messages\StatusMessage;
$parseFlag = true;
if (!RASPI_MONITOR_ENABLED) {
$optRules = $_POST['wgRules'];
$optConf = $_POST['wgCnfOpt'];
@@ -37,7 +38,7 @@ function DisplayWireGuardConfig()
// fetch server config
exec('sudo cat '. RASPI_WIREGUARD_CONFIG, $return);
$conf = ParseConfig($return);
$conf = ParseConfig($return, $parseFlag);
$wg_srvpubkey = exec('sudo cat '. RASPI_WIREGUARD_PATH .'wg-server-public.key', $return);
$wg_srvport = ($conf['ListenPort'] == '') ? getDefaultNetValue('wireguard','server','ListenPort') : $conf['ListenPort'];
$wg_srvipaddress = ($conf['Address'] == '') ? getDefaultNetValue('wireguard','server','Address') : $conf['Address'];
@@ -49,7 +50,7 @@ function DisplayWireGuardConfig()
// fetch client config
exec('sudo cat '. RASPI_WIREGUARD_PATH.'client.conf', $preturn);
$conf = ParseConfig($preturn);
$conf = ParseConfig($preturn, $parseFlag);
$wg_pipaddress = ($conf['Address'] == '') ? getDefaultNetValue('wireguard','peer','Address') : $conf['Address'];
$wg_plistenport = ($conf['ListenPort'] == '') ? getDefaultNetValue('wireguard','peer','ListenPort') : $conf['ListenPort'];
$wg_pendpoint = ($conf['Endpoint'] == '') ? getDefaultNetValue('wireguard','peer','Endpoint') : $conf['Endpoint'];