eesyphp/src/Check.php

98 lines
2.5 KiB
PHP

<?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, $domain=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;
}
$nd = explode('@', $value);
$nd=$nd[1];
if ($domain) {
if(is_array($domain)) {
if (!in_array($nd,$domain)) {
return false;
}
}
else {
if($nd!=$domain) {
return false;
}
}
}
if ($checkDns && function_exists('checkdnsrr')) {
if (!(checkdnsrr($nd, 'MX') || checkdnsrr($nd, 'A'))) {
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