diff --git a/src/psm/Util/Server/ServerValidator.php b/src/psm/Util/Server/ServerValidator.php index e2f72d7f..75cdf8b1 100644 --- a/src/psm/Util/Server/ServerValidator.php +++ b/src/psm/Util/Server/ServerValidator.php @@ -1,4 +1,5 @@ db = $db; } @@ -49,8 +52,9 @@ class ServerValidator { * @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')); + 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'); @@ -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'); }