eesyphp/src/Check.php

126 lines
3.5 KiB
PHP
Raw Normal View History

<?php
namespace EesyPHP;
class Check {
public static function name($name) {
if (preg_match('/^[\w \-]{2,}$/iu',$name))
return true;
return false;
}
public static function id(&$id) {
if (is_int($id))
return true;
if (preg_match('/^[0-9]+$/', $id)) {
$id = intval($id);
return true;
}
return false;
}
public static function search_pattern($pattern) {
foreach(preg_split('/\s+/', trim($pattern)) as $word) {
if (!self :: id($word) && !self :: name($word))
return false;
}
return true;
}
public static function time(&$time) {
if (!is_int($time)) {
if (preg_match('/^[0-9]+$/', $time))
$time = intval($time);
else
return false;
}
return ($time >= 1577833200); // 2020-01-01 - date of birth of this soft
}
public static function description($comment) {
if (preg_match("/^[\p{L}0-9\p{P}\p{Zs}\p{Zl}\p{Sc}\=\+]+$/uim", $comment))
return true;
return false;
}
public static function email($value, $domains=NULL, $checkDns=true) {
$regex = '/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/';
if (!preg_match($regex, $value)) {
return false;
}
$domain = explode('@', $value);
$domain = $domain[1];
if ($domains && !in_array($domain, ensure_is_array($domains)))
return false;
if ($checkDns) {
if (!function_exists('checkdnsrr')) {
Log::warning(
"Check::email(): PHP function checkdnsrr is missing, can't check domain against DNS. ".
"Assume is OK."
);
}
else if (!(checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A'))) {
return false;
}
}
return true;
}
2023-02-28 01:15:11 +01:00
public static function ip_address($ip, $allow_private_ip_address=true, $allow_reserved_ip_address=true,
$ipv4_only=false, $ipv6_only=false) {
$flag = null;
if ($ipv4_only)
$flag |= FILTER_FLAG_IPV4;
if ($ipv6_only)
$flag |= FILTER_FLAG_IPV6;
if (!isset($allow_private_ip_address) || !$allow_private_ip_address)
$flag |= FILTER_FLAG_NO_PRIV_RANGE;
if (!isset($allow_reserved_ip_address) || !$allow_reserved_ip_address)
$flag |= FILTER_FLAG_NO_RES_RANGE;
if (!is_null($flag))
return (bool)filter_var($ip, FILTER_VALIDATE_IP, $flag);
else
return (bool)filter_var($ip, FILTER_VALIDATE_IP);
}
public static function tcp_or_udp_port($value) {
if (!is_int($value)) {
if (!preg_match('/^[0-9]+$/', $value))
return false;
$value = intval($value);
}
if ($value <= 0) return false;
if ($value > 65635) return false;
return true;
}
public static function is_empty($val) {
switch(gettype($val)) {
case "boolean":
case "integer":
case "double":
case "object":
case "resource":
return False;
case "array":
case "string":
if ($val == "0") return false;
return empty($val);
case "NULL":
return True;
}
}
}
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab