issue #50: adding validation to servers page
parent
34a22a7914
commit
0f17b8b612
|
@ -4,6 +4,7 @@ Changelog
|
|||
not yet released
|
||||
----------------
|
||||
|
||||
* #50: Validation on servers page.
|
||||
* #54: Improved phone/tablet compatibility.
|
||||
* #62: Replace javascript confirm dialogs with Bootstrap modal dialogs.
|
||||
* #66: Unable to add users with MySQL in strict mode.
|
||||
|
|
|
@ -149,6 +149,13 @@ $sm_lang = array(
|
|||
'chart_long_date_format' => '%d.%m.%Y %H:%M:%S',
|
||||
'chart_short_date_format' => '%d.%m %H:%M',
|
||||
'chart_short_time_format' => '%H:%M',
|
||||
'error_server_no_match' => 'Server not found.',
|
||||
'error_server_label_bad_length' => 'The label must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_length' => 'The domain / IP must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_service' => 'The IP address is not valid.',
|
||||
'error_server_ip_bad_website' => 'The website URL is not valid.',
|
||||
'error_server_type_invalid' => 'The selected server type is invalid.',
|
||||
'error_server_warning_threshold_invalid' => 'The warning threshold must be a valid integer greater than 0.',
|
||||
),
|
||||
'config' => array(
|
||||
'general' => 'Основни настройки',
|
||||
|
|
|
@ -149,6 +149,13 @@ $sm_lang = array(
|
|||
'chart_long_date_format' => '%d-%m-%Y %H:%M:%S',
|
||||
'chart_short_date_format' => '%d/%m %H:%M',
|
||||
'chart_short_time_format' => '%H:%M',
|
||||
'error_server_no_match' => 'Server not found.',
|
||||
'error_server_label_bad_length' => 'The label must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_length' => 'The domain / IP must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_service' => 'The IP address is not valid.',
|
||||
'error_server_ip_bad_website' => 'The website URL is not valid.',
|
||||
'error_server_type_invalid' => 'The selected server type is invalid.',
|
||||
'error_server_warning_threshold_invalid' => 'The warning threshold must be a valid integer greater than 0.',
|
||||
),
|
||||
'config' => array(
|
||||
'general' => 'Generelt',
|
||||
|
|
|
@ -149,6 +149,13 @@ $sm_lang = array(
|
|||
'chart_long_date_format' => '%d.%m.%Y %H:%M:%S',
|
||||
'chart_short_date_format' => '%d.%m %H:%M',
|
||||
'chart_short_time_format' => '%H:%M',
|
||||
'error_server_no_match' => 'Server not found.',
|
||||
'error_server_label_bad_length' => 'The label must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_length' => 'The domain / IP must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_service' => 'The IP address is not valid.',
|
||||
'error_server_ip_bad_website' => 'The website URL is not valid.',
|
||||
'error_server_type_invalid' => 'The selected server type is invalid.',
|
||||
'error_server_warning_threshold_invalid' => 'The warning threshold must be a valid integer greater than 0.',
|
||||
),
|
||||
'config' => array(
|
||||
'general' => 'General',
|
||||
|
|
|
@ -149,6 +149,13 @@ $sm_lang = array(
|
|||
'chart_long_date_format' => '%Y-%m-%d %H:%M:%S',
|
||||
'chart_short_date_format' => '%m/%d %H:%M',
|
||||
'chart_short_time_format' => '%H:%M',
|
||||
'error_server_no_match' => 'Server not found.',
|
||||
'error_server_label_bad_length' => 'The label must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_length' => 'The domain / IP must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_service' => 'The IP address is not valid.',
|
||||
'error_server_ip_bad_website' => 'The website URL is not valid.',
|
||||
'error_server_type_invalid' => 'The selected server type is invalid.',
|
||||
'error_server_warning_threshold_invalid' => 'The warning threshold must be a valid integer greater than 0.',
|
||||
),
|
||||
'config' => array(
|
||||
'general' => 'General',
|
||||
|
|
|
@ -148,6 +148,13 @@ $sm_lang = array(
|
|||
'chart_long_date_format' => '%Y-%m-%d %H:%M:%S',
|
||||
'chart_short_date_format' => '%m/%d %H:%M',
|
||||
'chart_short_time_format' => '%H:%M',
|
||||
'error_server_no_match' => 'Server not found.',
|
||||
'error_server_label_bad_length' => 'The label must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_length' => 'The domain / IP must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_service' => 'The IP address is not valid.',
|
||||
'error_server_ip_bad_website' => 'The website URL is not valid.',
|
||||
'error_server_type_invalid' => 'The selected server type is invalid.',
|
||||
'error_server_warning_threshold_invalid' => 'The warning threshold must be a valid integer greater than 0.',
|
||||
),
|
||||
'config' => array(
|
||||
'general' => 'General',
|
||||
|
|
|
@ -149,6 +149,13 @@ $sm_lang = array(
|
|||
'chart_long_date_format' => '%d/%m/%Y %H:%M:%S',
|
||||
'chart_short_date_format' => '%d/%m %H:%M',
|
||||
'chart_short_time_format' => '%H:%M',
|
||||
'error_server_no_match' => 'Server not found.',
|
||||
'error_server_label_bad_length' => 'The label must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_length' => 'The domain / IP must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_service' => 'The IP address is not valid.',
|
||||
'error_server_ip_bad_website' => 'The website URL is not valid.',
|
||||
'error_server_type_invalid' => 'The selected server type is invalid.',
|
||||
'error_server_warning_threshold_invalid' => 'The warning threshold must be a valid integer greater than 0.',
|
||||
),
|
||||
'config' => array(
|
||||
'general' => 'Général',
|
||||
|
|
|
@ -148,6 +148,13 @@ $sm_lang = array(
|
|||
'chart_long_date_format' => '%Y-%m-%d %H:%M:%S',
|
||||
'chart_short_date_format' => '%m/%d %H:%M',
|
||||
'chart_short_time_format' => '%H:%M',
|
||||
'error_server_no_match' => 'Server not found.',
|
||||
'error_server_label_bad_length' => 'The label must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_length' => 'The domain / IP must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_service' => 'The IP address is not valid.',
|
||||
'error_server_ip_bad_website' => 'The website URL is not valid.',
|
||||
'error_server_type_invalid' => 'The selected server type is invalid.',
|
||||
'error_server_warning_threshold_invalid' => 'The warning threshold must be a valid integer greater than 0.',
|
||||
),
|
||||
'config' => array(
|
||||
'general' => 'Generale',
|
||||
|
|
|
@ -149,6 +149,13 @@ $sm_lang = array(
|
|||
'chart_long_date_format' => '%Y-%m-%d %H:%M:%S',
|
||||
'chart_short_date_format' => '%m/%d %H:%M',
|
||||
'chart_short_time_format' => '%H:%M',
|
||||
'error_server_no_match' => 'Server not found.',
|
||||
'error_server_label_bad_length' => 'The label must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_length' => 'The domain / IP must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_service' => 'The IP address is not valid.',
|
||||
'error_server_ip_bad_website' => 'The website URL is not valid.',
|
||||
'error_server_type_invalid' => 'The selected server type is invalid.',
|
||||
'error_server_warning_threshold_invalid' => 'The warning threshold must be a valid integer greater than 0.',
|
||||
),
|
||||
'config' => array(
|
||||
'general' => '일반',
|
||||
|
|
|
@ -149,6 +149,13 @@ $sm_lang = array(
|
|||
'chart_long_date_format' => '%d-%m-%Y %H:%M:%S',
|
||||
'chart_short_date_format' => '%d-%m %H:%M',
|
||||
'chart_short_time_format' => '%H:%M',
|
||||
'error_server_no_match' => 'Server niet gevonden.',
|
||||
'error_server_label_bad_length' => 'Het label moet tussen de 1 en 255 karakters lang zijn.',
|
||||
'error_server_ip_bad_length' => 'Het domein / IP moet tussen de 1 en 255 karakters lang zijn.',
|
||||
'error_server_ip_bad_service' => 'Het IP adres is ongeldig.',
|
||||
'error_server_ip_bad_website' => 'De website URL is ongeldig.',
|
||||
'error_server_type_invalid' => 'Het geselecteerde server type is ongeldig.',
|
||||
'error_server_warning_threshold_invalid' => 'De warning threshold moet een numerieke waarde zijn groter dan 0.',
|
||||
),
|
||||
'config' => array(
|
||||
'general' => 'Algemeen',
|
||||
|
|
|
@ -149,6 +149,13 @@ $sm_lang = array(
|
|||
'chart_long_date_format' => '%d/%m/%Y %H:%M:%S',
|
||||
'chart_short_date_format' => '%d/%m %H:%M',
|
||||
'chart_short_time_format' => '%H:%M',
|
||||
'error_server_no_match' => 'Server not found.',
|
||||
'error_server_label_bad_length' => 'The label must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_length' => 'The domain / IP must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_service' => 'The IP address is not valid.',
|
||||
'error_server_ip_bad_website' => 'The website URL is not valid.',
|
||||
'error_server_type_invalid' => 'The selected server type is invalid.',
|
||||
'error_server_warning_threshold_invalid' => 'The warning threshold must be a valid integer greater than 0.',
|
||||
),
|
||||
'config' => array(
|
||||
'general' => 'Geral',
|
||||
|
|
|
@ -149,6 +149,13 @@ $sm_lang = array(
|
|||
'chart_long_date_format' => '%Y-%m-%d %H:%M:%S',
|
||||
'chart_short_date_format' => '%m/%d %H:%M',
|
||||
'chart_short_time_format' => '%H:%M',
|
||||
'error_server_no_match' => 'Server not found.',
|
||||
'error_server_label_bad_length' => 'The label must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_length' => 'The domain / IP must be between 1 and 255 characters.',
|
||||
'error_server_ip_bad_service' => 'The IP address is not valid.',
|
||||
'error_server_ip_bad_website' => 'The website URL is not valid.',
|
||||
'error_server_type_invalid' => 'The selected server type is invalid.',
|
||||
'error_server_warning_threshold_invalid' => 'The warning threshold must be a valid integer greater than 0.',
|
||||
),
|
||||
'config' => array(
|
||||
'general' => '通用',
|
||||
|
|
|
@ -143,6 +143,7 @@ class ServerController extends AbstractServerController {
|
|||
$back_to = isset($_GET['back_to']) ? $_GET['back_to'] : '';
|
||||
|
||||
$tpl_data = array(
|
||||
'edit_server_id' => $this->server_id,
|
||||
// form url:
|
||||
'url_save' => psm_build_url(array(
|
||||
'mod' => 'server',
|
||||
|
@ -163,34 +164,39 @@ class ServerController extends AbstractServerController {
|
|||
case 0:
|
||||
// insert mode
|
||||
$tpl_data['titlemode'] = psm_get_lang('system', 'insert');
|
||||
$tpl_data['edit_server_id'] = '0';
|
||||
$tpl_data['edit_value_warning_threshold'] = '1';
|
||||
|
||||
$edit_server = $_POST;
|
||||
break;
|
||||
default:
|
||||
// edit mode
|
||||
// get server entry
|
||||
$edit_server = $this->getServers($this->server_id);
|
||||
if (empty($edit_server)) {
|
||||
$this->addMessage('Invalid server', 'error');
|
||||
if(empty($edit_server)) {
|
||||
$this->addMessage(psm_get_lang('servers', 'error_server_no_match'), 'error');
|
||||
return $this->initializeAction('index');
|
||||
}
|
||||
$tpl_data['titlemode'] = psm_get_lang('system', 'edit') . ' ' . $edit_server['label'];
|
||||
|
||||
$tpl_data = array_merge($tpl_data, array(
|
||||
'titlemode' => psm_get_lang('system', 'edit') . ' ' . $edit_server['label'],
|
||||
'edit_server_id' => $edit_server['server_id'],
|
||||
'edit_value_label' => $edit_server['label'],
|
||||
'edit_value_ip' => $edit_server['ip'],
|
||||
'edit_value_port' => $edit_server['port'],
|
||||
'edit_value_pattern' => $edit_server['pattern'],
|
||||
'edit_value_warning_threshold' => $edit_server['warning_threshold'],
|
||||
'edit_type_selected_' . $edit_server['type'] => 'selected="selected"',
|
||||
'edit_active_selected_' . $edit_server['active'] => 'selected="selected"',
|
||||
'edit_email_selected_' . $edit_server['email'] => 'selected="selected"',
|
||||
'edit_sms_selected_' . $edit_server['sms'] => 'selected="selected"',
|
||||
));
|
||||
|
||||
break;
|
||||
}
|
||||
// attempt to prefill previously posted fields
|
||||
foreach($edit_server as $key => $value) {
|
||||
$edit_server[$key] = psm_POST($key, $value);
|
||||
}
|
||||
|
||||
$tpl_data = array_merge($tpl_data, array(
|
||||
'edit_value_label' => $edit_server['label'],
|
||||
'edit_value_ip' => $edit_server['ip'],
|
||||
'edit_value_port' => $edit_server['port'],
|
||||
'edit_value_pattern' => $edit_server['pattern'],
|
||||
'edit_value_warning_threshold' => $edit_server['warning_threshold'],
|
||||
'edit_type_selected_' . $edit_server['type'] => 'selected="selected"',
|
||||
'edit_active_selected_' . $edit_server['active'] => 'selected="selected"',
|
||||
'edit_email_selected_' . $edit_server['email'] => 'selected="selected"',
|
||||
'edit_sms_selected_' . $edit_server['sms'] => 'selected="selected"',
|
||||
));
|
||||
|
||||
$this->tpl->addTemplateData(
|
||||
$this->getTemplateId(),
|
||||
|
@ -202,39 +208,57 @@ class ServerController extends AbstractServerController {
|
|||
* Executes the saving of one of the servers
|
||||
*/
|
||||
protected function executeSave() {
|
||||
// check for add/edit mode
|
||||
if(isset($_POST['label']) && isset($_POST['ip']) && isset($_POST['port'])) {
|
||||
$clean = array(
|
||||
'label' => strip_tags($_POST['label']),
|
||||
'ip' => strip_tags($_POST['ip']),
|
||||
'port' => intval($_POST['port']),
|
||||
'type' => in_array($_POST['type'], array('website', 'service')) ? $_POST['type'] : 'website',
|
||||
'pattern' => $_POST['pattern'],
|
||||
'warning_threshold' => intval($_POST['warning_threshold']),
|
||||
'active' => in_array($_POST['active'], array('yes', 'no')) ? $_POST['active'] : 'no',
|
||||
'email' => in_array($_POST['email'], array('yes', 'no')) ? $_POST['email'] : 'no',
|
||||
'sms' => in_array($_POST['sms'], array('yes', 'no')) ? $_POST['sms'] : 'no',
|
||||
);
|
||||
// make sure websites start with http://
|
||||
if($clean['type'] == 'website' && substr($clean['ip'], 0, 4) != 'http') {
|
||||
$clean['ip'] = 'http://' . $clean['ip'];
|
||||
}
|
||||
if(empty($_POST)) {
|
||||
// dont process anything if no data has been posted
|
||||
return $this->executeIndex();
|
||||
}
|
||||
|
||||
// check for edit or add
|
||||
$clean = array(
|
||||
'label' => trim(strip_tags(psm_POST('label', ''))),
|
||||
'ip' => trim(strip_tags(psm_POST('ip', ''))),
|
||||
'port' => intval(psm_POST('port', 0)),
|
||||
'type' => psm_POST('type', ''),
|
||||
'pattern' => psm_POST('pattern', ''),
|
||||
'warning_threshold' => intval(psm_POST('warning_threshold', 0)),
|
||||
'active' => in_array($_POST['active'], array('yes', 'no')) ? $_POST['active'] : 'no',
|
||||
'email' => in_array($_POST['email'], array('yes', 'no')) ? $_POST['email'] : 'no',
|
||||
'sms' => in_array($_POST['sms'], array('yes', 'no')) ? $_POST['sms'] : 'no',
|
||||
);
|
||||
// make sure websites start with http://
|
||||
if($clean['type'] == 'website' && substr($clean['ip'], 0, 4) != 'http') {
|
||||
$clean['ip'] = 'http://' . $clean['ip'];
|
||||
}
|
||||
|
||||
// validate the lot
|
||||
$server_validator = new \psm\Util\Server\ServerValidator($this->db);
|
||||
|
||||
try {
|
||||
if($this->server_id > 0) {
|
||||
// edit
|
||||
$this->db->save(
|
||||
PSM_DB_PREFIX.'servers',
|
||||
$clean,
|
||||
array('server_id' => $this->server_id)
|
||||
);
|
||||
$this->addMessage(psm_get_lang('servers', 'updated'), 'success');
|
||||
} else {
|
||||
// add
|
||||
$clean['status'] = 'on';
|
||||
$this->server_id = $this->db->save(PSM_DB_PREFIX.'servers', $clean);
|
||||
$this->addMessage(psm_get_lang('servers', 'inserted'), 'success');
|
||||
$server_validator->serverId($this->server_id);
|
||||
}
|
||||
$server_validator->label($clean['label']);
|
||||
$server_validator->type($clean['type']);
|
||||
$server_validator->ip($clean['ip'], $clean['type']);
|
||||
$server_validator->warningThreshold($clean['warning_threshold']);
|
||||
} catch(\InvalidArgumentException $ex) {
|
||||
$this->addMessage(psm_get_lang('servers', 'error_' . $ex->getMessage()), 'error');
|
||||
return $this->executeEdit();
|
||||
}
|
||||
|
||||
// check for edit or add
|
||||
if($this->server_id > 0) {
|
||||
// edit
|
||||
$this->db->save(
|
||||
PSM_DB_PREFIX.'servers',
|
||||
$clean,
|
||||
array('server_id' => $this->server_id)
|
||||
);
|
||||
$this->addMessage(psm_get_lang('servers', 'updated'), 'success');
|
||||
} else {
|
||||
// add
|
||||
$clean['status'] = 'on';
|
||||
$this->server_id = $this->db->save(PSM_DB_PREFIX.'servers', $clean);
|
||||
$this->addMessage(psm_get_lang('servers', 'inserted'), 'success');
|
||||
}
|
||||
|
||||
$back_to = isset($_GET['back_to']) ? $_GET['back_to'] : 'index';
|
||||
|
|
|
@ -0,0 +1,130 @@
|
|||
<?php
|
||||
/**
|
||||
* PHP Server Monitor
|
||||
* Monitor your servers and websites.
|
||||
*
|
||||
* This file is part of PHP Server Monitor.
|
||||
* PHP Server Monitor is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PHP Server Monitor is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package phpservermon
|
||||
* @author Pepijn Over <pep@neanderthal-technology.com>
|
||||
* @copyright Copyright (c) 2008-2014 Pepijn Over <pep@neanderthal-technology.com>
|
||||
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
|
||||
* @version Release: @package_version@
|
||||
* @link http://www.phpservermonitor.org/
|
||||
* @since phpservermon 3.1.0
|
||||
**/
|
||||
|
||||
namespace psm\Util\Server;
|
||||
|
||||
/**
|
||||
* The ServerValidator helps you to check input data for servers.
|
||||
*/
|
||||
class ServerValidator {
|
||||
|
||||
/**
|
||||
* Database service
|
||||
* @var \psm\Service\Database $db
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
public function __construct(\psm\Service\Database $db) {
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the server id exists
|
||||
* @param int $server_id
|
||||
* @return boolean
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function serverId($server_id) {
|
||||
$server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array('server_id' => $server_id), array('server_id'));
|
||||
|
||||
if(empty($server)) {
|
||||
throw new \InvalidArgumentException('server_no_match');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check label
|
||||
* @param string $label
|
||||
* @return boolean
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function label($label) {
|
||||
$label = trim($label);
|
||||
if(empty($label) || strlen($label) > 255) {
|
||||
throw new \InvalidArgumentException('server_label_bad_length');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check server domain/ip
|
||||
* @param string $value
|
||||
* @param string $type if given, it can be checked for "website"/"ip"
|
||||
* @return boolean
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function ip($value, $type = null) {
|
||||
$value = trim($value);
|
||||
|
||||
if(empty($value) || strlen($value) > 255) {
|
||||
throw new \InvalidArgumentException('server_ip_bad_length');
|
||||
}
|
||||
|
||||
switch($type) {
|
||||
case 'website':
|
||||
if(!filter_var($value, FILTER_VALIDATE_URL)) {
|
||||
throw new \InvalidArgumentException('server_ip_bad_website');
|
||||
}
|
||||
break;
|
||||
case 'service':
|
||||
if(!filter_var($value, FILTER_VALIDATE_IP)) {
|
||||
throw new \InvalidArgumentException('server_ip_bad_service');
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check server type
|
||||
* @param string $type
|
||||
* @return boolean
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function type($type) {
|
||||
if(!in_array($type, array('service', 'website'))) {
|
||||
throw new \InvalidArgumentException('server_type_invalid');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check warning threshold
|
||||
* @param int $value
|
||||
* @return boolean
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function warningThreshold($value) {
|
||||
if(!is_numeric($value) || intval($value) == 0) {
|
||||
throw new \InvalidArgumentException('server_warning_threshold_invalid');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue