Fix #704 - use regex instead of PHP's own function
parent
a0cdcffabc
commit
53c5ac7273
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHP Server Monitor
|
||||
* Monitor your servers and websites.
|
||||
|
@ -31,7 +32,8 @@ namespace psm\Util\Server;
|
|||
/**
|
||||
* The ServerValidator helps you to check input data for servers.
|
||||
*/
|
||||
class ServerValidator {
|
||||
class ServerValidator
|
||||
{
|
||||
|
||||
/**
|
||||
* Database service
|
||||
|
@ -39,7 +41,8 @@ class ServerValidator {
|
|||
*/
|
||||
protected $db;
|
||||
|
||||
public function __construct(\psm\Service\Database $db) {
|
||||
public function __construct(\psm\Service\Database $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
|
@ -49,7 +52,8 @@ class ServerValidator {
|
|||
* @return boolean
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function serverId($server_id) {
|
||||
public function serverId($server_id)
|
||||
{
|
||||
$server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array('server_id' => $server_id), array('server_id'));
|
||||
|
||||
if (empty($server)) {
|
||||
|
@ -64,7 +68,8 @@ class ServerValidator {
|
|||
* @return boolean
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function label($label) {
|
||||
public function label($label)
|
||||
{
|
||||
$label = trim($label);
|
||||
if (empty($label) || strlen($label) > 255) {
|
||||
throw new \InvalidArgumentException('server_label_bad_length');
|
||||
|
@ -79,7 +84,8 @@ class ServerValidator {
|
|||
* @return boolean
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function ip($value, $type = null) {
|
||||
public function ip($value, $type = null)
|
||||
{
|
||||
$value = trim($value);
|
||||
|
||||
if (empty($value) || strlen($value) > 255) {
|
||||
|
@ -88,16 +94,20 @@ class ServerValidator {
|
|||
|
||||
switch ($type) {
|
||||
case 'website':
|
||||
if (!filter_var($value, FILTER_VALIDATE_URL)) {
|
||||
// url regex as per https://stackoverflow.com/a/3809435
|
||||
if (!preg_match("/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)/g", $value)) {
|
||||
throw new \InvalidArgumentException('server_ip_bad_website');
|
||||
}
|
||||
break;
|
||||
case 'service':
|
||||
case 'ping':
|
||||
if (!filter_var($value, FILTER_VALIDATE_IP)
|
||||
if (
|
||||
!filter_var($value, FILTER_VALIDATE_IP)
|
||||
// domain regex as per http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address :
|
||||
&& !preg_match("/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/", $value)
|
||||
) {throw new \InvalidArgumentException('server_ip_bad_service'); }
|
||||
) {
|
||||
throw new \InvalidArgumentException('server_ip_bad_service');
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -110,7 +120,8 @@ class ServerValidator {
|
|||
* @return boolean
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function type($type) {
|
||||
public function type($type)
|
||||
{
|
||||
if (!in_array($type, array('ping', 'service', 'website'))) {
|
||||
throw new \InvalidArgumentException('server_type_invalid');
|
||||
}
|
||||
|
@ -123,7 +134,8 @@ class ServerValidator {
|
|||
* @return boolean
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function warningThreshold($value) {
|
||||
public function warningThreshold($value)
|
||||
{
|
||||
if (!is_numeric($value) || intval($value) == 0) {
|
||||
throw new \InvalidArgumentException('server_warning_threshold_invalid');
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue