Fix #704 - use regex instead of PHP's own function
parent
a0cdcffabc
commit
53c5ac7273
|
@ -1,4 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PHP Server Monitor
|
* PHP Server Monitor
|
||||||
* Monitor your servers and websites.
|
* Monitor your servers and websites.
|
||||||
|
@ -31,7 +32,8 @@ namespace psm\Util\Server;
|
||||||
/**
|
/**
|
||||||
* The ServerValidator helps you to check input data for servers.
|
* The ServerValidator helps you to check input data for servers.
|
||||||
*/
|
*/
|
||||||
class ServerValidator {
|
class ServerValidator
|
||||||
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Database service
|
* Database service
|
||||||
|
@ -39,7 +41,8 @@ class ServerValidator {
|
||||||
*/
|
*/
|
||||||
protected $db;
|
protected $db;
|
||||||
|
|
||||||
public function __construct(\psm\Service\Database $db) {
|
public function __construct(\psm\Service\Database $db)
|
||||||
|
{
|
||||||
$this->db = $db;
|
$this->db = $db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,8 +52,9 @@ class ServerValidator {
|
||||||
* @return boolean
|
* @return boolean
|
||||||
* @throws \InvalidArgumentException
|
* @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'));
|
{
|
||||||
|
$server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array('server_id' => $server_id), array('server_id'));
|
||||||
|
|
||||||
if (empty($server)) {
|
if (empty($server)) {
|
||||||
throw new \InvalidArgumentException('server_no_match');
|
throw new \InvalidArgumentException('server_no_match');
|
||||||
|
@ -64,7 +68,8 @@ class ServerValidator {
|
||||||
* @return boolean
|
* @return boolean
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function label($label) {
|
public function label($label)
|
||||||
|
{
|
||||||
$label = trim($label);
|
$label = trim($label);
|
||||||
if (empty($label) || strlen($label) > 255) {
|
if (empty($label) || strlen($label) > 255) {
|
||||||
throw new \InvalidArgumentException('server_label_bad_length');
|
throw new \InvalidArgumentException('server_label_bad_length');
|
||||||
|
@ -79,7 +84,8 @@ class ServerValidator {
|
||||||
* @return boolean
|
* @return boolean
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function ip($value, $type = null) {
|
public function ip($value, $type = null)
|
||||||
|
{
|
||||||
$value = trim($value);
|
$value = trim($value);
|
||||||
|
|
||||||
if (empty($value) || strlen($value) > 255) {
|
if (empty($value) || strlen($value) > 255) {
|
||||||
|
@ -88,16 +94,20 @@ class ServerValidator {
|
||||||
|
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case 'website':
|
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');
|
throw new \InvalidArgumentException('server_ip_bad_website');
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'service':
|
case 'service':
|
||||||
case 'ping':
|
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 :
|
// 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)
|
&& !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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,7 +120,8 @@ class ServerValidator {
|
||||||
* @return boolean
|
* @return boolean
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function type($type) {
|
public function type($type)
|
||||||
|
{
|
||||||
if (!in_array($type, array('ping', 'service', 'website'))) {
|
if (!in_array($type, array('ping', 'service', 'website'))) {
|
||||||
throw new \InvalidArgumentException('server_type_invalid');
|
throw new \InvalidArgumentException('server_type_invalid');
|
||||||
}
|
}
|
||||||
|
@ -123,7 +134,8 @@ class ServerValidator {
|
||||||
* @return boolean
|
* @return boolean
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function warningThreshold($value) {
|
public function warningThreshold($value)
|
||||||
|
{
|
||||||
if (!is_numeric($value) || intval($value) == 0) {
|
if (!is_numeric($value) || intval($value) == 0) {
|
||||||
throw new \InvalidArgumentException('server_warning_threshold_invalid');
|
throw new \InvalidArgumentException('server_warning_threshold_invalid');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue