git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@6095 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
630 lines
16 KiB
PHP
630 lines
16 KiB
PHP
<?php
|
|
/** DomFramework
|
|
* @package domframework
|
|
* @author Dominique Fournier <dominique@fournier38.fr>
|
|
*/
|
|
|
|
/** Permit to check the validity of fields
|
|
*/
|
|
class verify
|
|
{
|
|
/////////////////
|
|
// NETWORK //
|
|
/////////////////
|
|
/** Check if $val is a valid IPv4
|
|
* Return true or false
|
|
* @param string $val The IP to test
|
|
*/
|
|
public function isIp ($val)
|
|
{
|
|
return \verify::staticIsIp ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid IPv4 in static mode
|
|
* Return true or false
|
|
* @param string $val The IP to test
|
|
*/
|
|
public static function staticIsIp ($val)
|
|
{
|
|
if (filter_var ($val, FILTER_VALIDATE_IP))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
/** Check if $val is a valid IPv4
|
|
* Return true or false
|
|
* @param string $val The IP to test
|
|
* @deprecated 0.39
|
|
*/
|
|
public function is_ip ($val)
|
|
{
|
|
return \verify::staticIsIp ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid IPv4 in static mode
|
|
* Return true or false
|
|
* @param string $val The IP to test
|
|
* @deprecated 0.39
|
|
*/
|
|
public static function staticIs_ip ($val)
|
|
{
|
|
return \verify::staticIsIp ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid IPv4
|
|
* Return true or false
|
|
* @param string $val The IPv4 to test
|
|
*/
|
|
public function isIpv4 ($val)
|
|
{
|
|
return \verify::staticIsIpv4 ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid IPv4 in static mode
|
|
* Return true or false
|
|
* @param string $val The IPv4 to test
|
|
*/
|
|
public static function staticIsIpv4 ($val)
|
|
{
|
|
if (filter_var ($val, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
/** Check if $val is a valid IPv4
|
|
* Return true or false
|
|
* @param string $val The IPv4 to test
|
|
* @deprecated 0.39
|
|
*/
|
|
public function is_ipv4 ($val)
|
|
{
|
|
return \verify::staticIsIpv4 ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid IPv4 in static mode
|
|
* Return true or false
|
|
* @param string $val The IPv4 to test
|
|
* @deprecated 0.39
|
|
*/
|
|
public static function staticIs_ipv4 ($val)
|
|
{
|
|
return \verify::staticIsIpv4 ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid IPv6
|
|
* Return true or false
|
|
* @param string $val The IPv6 to test
|
|
*/
|
|
public function isIpv6 ($val)
|
|
{
|
|
return \verify::staticIsIpv6 ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid IPv6 in static mode
|
|
* Return true or false
|
|
* @param string $val The IPv6 to test
|
|
*/
|
|
public static function staticIsIpv6 ($val)
|
|
{
|
|
if (filter_var ($val, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
/** Check if $val is a valid IPv6
|
|
* Return true or false
|
|
* @param string $val The IPv6 to test
|
|
* @deprecated 0.39
|
|
*/
|
|
public function is_ipv6 ($val)
|
|
{
|
|
return \verify::staticIsipv6 ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid IPv6 in static mode
|
|
* Return true or false
|
|
* @param string $val The IPv6 to test
|
|
* @deprecated 0.39
|
|
*/
|
|
public static function staticIs_ipv6 ($val)
|
|
{
|
|
return \verify::staticIsipv6 ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid hostname (without domain)
|
|
* Return true or false
|
|
* @param string $val The hostname to check
|
|
*/
|
|
public function isHostname ($val)
|
|
{
|
|
return \verify::staticIsHostname ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid hostname (without domain)
|
|
* Return true or false
|
|
* @param string $val The hostname to check
|
|
*/
|
|
public static function staticIsHostname ($val)
|
|
{
|
|
if (strlen ($val) < 2 || strlen ($val) > 128)
|
|
return false;
|
|
if (strspn ($val, "abcdefghijklmnopqrstuvwxyz".
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
|
"0123456789-.") !==
|
|
strlen ($val))
|
|
return false;
|
|
// No end dot ?
|
|
if (substr ($val, -1) === ".")
|
|
return false;
|
|
return true;
|
|
}
|
|
/** Check if $val is a valid hostname (without domain)
|
|
* Return true or false
|
|
* @param string $val The hostname to check
|
|
* @deprecated 0.39
|
|
*/
|
|
public function is_hostname ($val)
|
|
{
|
|
return \verify::staticIs_hostname ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid hostname (without domain)
|
|
* Return true or false
|
|
* @param string $val The hostname to check
|
|
* @deprecated 0.39
|
|
*/
|
|
public static function staticIs_hostname ($val)
|
|
{
|
|
return \verify::staticIsHostname ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid domain (without hostname)
|
|
* Return true or false
|
|
* @param string $val The domain to check
|
|
*/
|
|
public function isDomain ($val)
|
|
{
|
|
return \verify::staticIsDomain ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid domain (without hostname) in static mode
|
|
* Return true or false
|
|
* @param string $val The domain to check
|
|
*/
|
|
public static function staticIsDomain ($val)
|
|
{
|
|
if (strlen ($val) < 2 || strlen ($val) > 128)
|
|
return false;
|
|
if (strspn ($val, "abcdefghijklmnopqrstuvwxyz".
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
|
"0123456789-.") !==
|
|
strlen ($val))
|
|
return false;
|
|
// No end dot ?
|
|
if (substr ($val, -1) === ".")
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
/** Check if $val is a valid domain (without hostname)
|
|
* Return true or false
|
|
* @param string $val The domain to check
|
|
* @deprecated 0.39
|
|
*/
|
|
public function is_domain ($val)
|
|
{
|
|
return \verify::staticIsDomain ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid domain (without hostname) in static mode
|
|
* Return true or false
|
|
* @param string $val The domain to check
|
|
* @deprecated 0.39
|
|
*/
|
|
public static function staticIs_domain ($val)
|
|
{
|
|
return \verify::staticIsDomain ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid FQDN
|
|
* Return true or false
|
|
* @param string $val The FQDN to check
|
|
*/
|
|
public function isFQDN ($val)
|
|
{
|
|
return \verify::staticIsFQDN ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid FQDN in static mode
|
|
* Return true or false
|
|
* @param string $val The FQDN to check
|
|
*/
|
|
public static function staticIsFQDN ($val)
|
|
{
|
|
if (strlen ($val) < 2 || strlen ($val) > 255)
|
|
return false;
|
|
if (\verify::is_ip ($val))
|
|
return false;
|
|
if (strspn ($val, "abcdefghijklmnopqrstuvwxyz".
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
|
"0123456789-.") !==
|
|
strlen ($val))
|
|
return false;
|
|
// No end dot ?
|
|
if (substr ($val, -1) === ".")
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
/** Check if $val is a valid FQDN
|
|
* Return true or false
|
|
* @param string $val The FQDN to check
|
|
* @deprecated 0.39
|
|
*/
|
|
public function is_FQDN ($val)
|
|
{
|
|
return \verify::staticIsFQDN ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid FQDN in static mode
|
|
* Return true or false
|
|
* @param string $val The FQDN to check
|
|
* @deprecated 0.39
|
|
*/
|
|
public static function staticIs_FQDN ($val)
|
|
{
|
|
return \verify::staticIsFQDN ($val);
|
|
}
|
|
|
|
///////////////
|
|
// DATES //
|
|
///////////////
|
|
/** Check if $val is a valid date and time in SQL
|
|
* A valid date is 2014-03-20 12:27:34
|
|
* Return true or false
|
|
* @param string $val The date to check
|
|
*/
|
|
public function isDatetimeSQL ($val)
|
|
{
|
|
return \verify::staticIsDatetimeSQL ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid date and time in SQL in static mode
|
|
* A valid date is 2014-03-20 12:27:34
|
|
* Return true or false
|
|
* @param string $val The date to check
|
|
*/
|
|
public static function staticIsDatetimeSQL ($val)
|
|
{
|
|
if (strlen ($val) !== 19)
|
|
return false;
|
|
$arr = \date_parse ($val);
|
|
if ($arr["warning_count"] !==0)
|
|
return false;
|
|
if ($arr["error_count"] !==0)
|
|
return false;
|
|
if (isset ($arr["tz_abbr"]))
|
|
return false;
|
|
if (strspn ($val, "0123456789 :-") !== strlen ($val))
|
|
return false;
|
|
if (\DateTime::createFromFormat("Y-m-d H:i:s", $val) === false)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
/** Check if $val is a valid date and time in SQL
|
|
* A valid date is 2014-03-20 12:27:34
|
|
* Return true or false
|
|
* @param string $val The date to check
|
|
* @deprecated 0.39
|
|
*/
|
|
public function is_datetimeSQL ($val)
|
|
{
|
|
return \verify::staticIsDatetimeSQL ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid date and time in SQL in static mode
|
|
* A valid date is 2014-03-20 12:27:34
|
|
* Return true or false
|
|
* @param string $val The date to check
|
|
* @deprecated 0.39
|
|
*/
|
|
public static function staticIs_datetimeSQL ($val)
|
|
{
|
|
return \verify::staticIsDatetimeSQL ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid date in SQL
|
|
* A valid date is 2014-03-20
|
|
* Return true or false
|
|
* @param string $val The date to check
|
|
*/
|
|
public function isDateSQL ($val)
|
|
{
|
|
return \verify::staticIsDateSQL ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid date in SQL in static mode
|
|
* A valid date is 2014-03-20
|
|
* Return true or false
|
|
* @param string $val The date to check
|
|
*/
|
|
public static function staticIsDateSQL ($val)
|
|
{
|
|
if (strlen ($val) !== 10)
|
|
return false;
|
|
$arr = \date_parse ($val);
|
|
if ($arr["warning_count"] !==0)
|
|
return false;
|
|
if ($arr["error_count"] !==0)
|
|
return false;
|
|
if (isset ($arr["tz_abbr"]))
|
|
return false;
|
|
if (strspn ($val, "0123456789-") !== strlen ($val))
|
|
return false;
|
|
if (\DateTime::createFromFormat("Y-m-d", $val) === false)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
/** Check if $val is a valid date in SQL
|
|
* A valid date is 2014-03-20
|
|
* Return true or false
|
|
* @param string $val The date to check
|
|
* @deprecated 0.39
|
|
*/
|
|
public function is_dateSQL ($val)
|
|
{
|
|
return \verify::staticIsDateSQL ($val);
|
|
}
|
|
|
|
/** Check if $val is a valid date in SQL in static mode
|
|
* A valid date is 2014-03-20
|
|
* Return true or false
|
|
* @param string $val The date to check
|
|
* @deprecated 0.39
|
|
*/
|
|
public static function staticIs_dateSQL ($val)
|
|
{
|
|
return \verify::staticIsDateSQL ($val);
|
|
}
|
|
|
|
|
|
/////////////////
|
|
// STRINGS //
|
|
/////////////////
|
|
/** Check if the provided value is only contains the provided chars.
|
|
* All the strings must be in UTF-8
|
|
* @param string $val The string to test
|
|
* @param string $allowedChars The allowed chars
|
|
*/
|
|
public function isAllowedChars ($val, $allowedChars)
|
|
{
|
|
return \verify::staticIsAllowedChars ($val, $allowedChars);
|
|
}
|
|
|
|
/** Check if the provided value is only contains the provided chars.
|
|
* In static mode
|
|
* All the strings must be in UTF-8
|
|
* @param string $val The string to test
|
|
* @param string $allowedChars The allowed chars
|
|
*/
|
|
public static function staticIsAllowedChars ($val, $allowedChars)
|
|
{
|
|
$allowedChars = preg_quote ($allowedChars, "#");
|
|
preg_match ('#^['.$allowedChars.']+#u', $val, $matches);
|
|
if (isset ($matches[0]) &&
|
|
mb_strlen ($matches[0]) === mb_strlen ($val))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
/////////////////
|
|
// NUMBERS //
|
|
/////////////////
|
|
/** Return true if the provided value is an integer in decimal (not octal)
|
|
* @param string $val The Integer val to check
|
|
*/
|
|
public function isInteger ($val)
|
|
{
|
|
return \verify::staticIsInteger ($val);
|
|
}
|
|
|
|
/** Return true if the provided value is an integer in decimal (not octal)
|
|
* In static mode
|
|
* @param string $val The Integer val to check
|
|
*/
|
|
public static function staticIsInteger ($val)
|
|
{
|
|
if (! is_string ($val) && ! is_int ($val))
|
|
return false;
|
|
if (trim ($val) === "")
|
|
return false;
|
|
if (strspn ($val, "0123456789") !== strlen ($val))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
/** Return true if the provided value is an integer in decimal (not octal)
|
|
* @param string $val The Integer val to check
|
|
* @deprecated 0.39
|
|
*/
|
|
public function is_integer ($val)
|
|
{
|
|
return \verify::staticIsInteger ($val);
|
|
}
|
|
|
|
/** Return true if the provided value is an integer in decimal (not octal)
|
|
* In static mode
|
|
* @param string $val The Integer val to check
|
|
* @deprecated 0.39
|
|
*/
|
|
public static function staticIs_integer ($val)
|
|
{
|
|
return \verify::staticIsInteger ($val);
|
|
}
|
|
|
|
////////////////
|
|
// EMAILS //
|
|
////////////////
|
|
/** Return true if the provided value is a valid email address
|
|
* @param string $val The value to check
|
|
* @return boolean
|
|
*/
|
|
public function isEmail ($val)
|
|
{
|
|
return \verify::staticIsEmail ($val);
|
|
}
|
|
|
|
/** Return true if the provided value is a valid email address in static mode
|
|
* @param string $val The value to check
|
|
* @return boolean
|
|
*/
|
|
public static function staticIsEmail ($val)
|
|
{
|
|
return !! filter_var ($val, FILTER_VALIDATE_EMAIL);
|
|
}
|
|
|
|
/** Return true if the provided value is a valid email address
|
|
* @param string $val The value to check
|
|
* @return boolean
|
|
* @deprecated 0.39
|
|
*/
|
|
public function is_email ($val)
|
|
{
|
|
return \verify::staticIsEmail ($val);
|
|
}
|
|
|
|
/** Return true if the provided value is a valid email address in static mode
|
|
* @param string $val The value to check
|
|
* @return boolean
|
|
* @deprecated 0.39
|
|
*/
|
|
public static function staticIs_email ($val)
|
|
{
|
|
return \verify::staticIsEmail ($val);
|
|
}
|
|
|
|
/////////////
|
|
// URL //
|
|
/////////////
|
|
/** Return true if the provided value is a valid URL
|
|
* @param string $val The value to check
|
|
* @return boolean
|
|
*/
|
|
public function isURL ($val)
|
|
{
|
|
return \verify::staticIsURL ($val);
|
|
}
|
|
|
|
/** Return true if the provided value is a valid URL in static mode
|
|
* @param string $val The value to check
|
|
* @return boolean
|
|
*/
|
|
public static function staticIsURL ($val)
|
|
{
|
|
return !! filter_var ($val, FILTER_VALIDATE_URL);
|
|
}
|
|
|
|
/** Return true if the provided value is a valid URL
|
|
* @param string $val The value to check
|
|
* @return boolean
|
|
* @deprecated 0.39
|
|
*/
|
|
public function is_URL ($val)
|
|
{
|
|
return \verify::staticIsURL ($val);
|
|
}
|
|
|
|
/** Return true if the provided value is a valid URL in static mode
|
|
* @param string $val The value to check
|
|
* @return boolean
|
|
* @deprecated 0.39
|
|
*/
|
|
public static function staticIs_URL ($val)
|
|
{
|
|
return \verify::staticIsURL ($val);
|
|
}
|
|
|
|
//////////////////
|
|
/// OTHERS ///
|
|
//////////////////
|
|
/** Return true if the provided value is a valid UUID
|
|
* %04x%04x-%04x-%04x-%04x-%04x%04x%04x
|
|
* @param string $val The value to check
|
|
* @return boolean
|
|
*/
|
|
public function isUUID ($val)
|
|
{
|
|
return \verify::staticIsUUID ($val);
|
|
}
|
|
|
|
/** Return true if the provided value is a valid UUID in static mode
|
|
* %04x%04x-%04x-%04x-%04x-%04x%04x%04x
|
|
* @param string $val The value to check
|
|
* @return boolean
|
|
*/
|
|
public static function staticIsUUID ($val)
|
|
{
|
|
if (! is_string ($val))
|
|
return false;
|
|
if (strlen ($val) !== 36)
|
|
return false;
|
|
if (strspn ($val, "0123456789abcdefABCDEF-") !== strlen ($val))
|
|
return false;
|
|
if ($val[8] !== "-" || $val[13] !== "-" || $val[18] !== "-" ||
|
|
$val[23] !== "-")
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
/** Return true if the provided value is a valid UUID
|
|
* %04x%04x-%04x-%04x-%04x-%04x%04x%04x
|
|
* @param string $val The value to check
|
|
* @return boolean
|
|
* @deprecated 0.39
|
|
*/
|
|
public function is_UUID ($val)
|
|
{
|
|
return \verify::staticIsUUID ($val);
|
|
}
|
|
|
|
/** Return true if the provided value is a valid UUID in static mode
|
|
* %04x%04x-%04x-%04x-%04x-%04x%04x%04x
|
|
* @param string $val The value to check
|
|
* @return boolean
|
|
* @deprecated 0.39
|
|
*/
|
|
public static function staticIs_UUID ($val)
|
|
{
|
|
return \verify::staticIsUUID ($val);
|
|
}
|
|
|
|
/** Return true if the provided string is a valid hash
|
|
* @param string $algo The hash algo to test
|
|
* @param string $val The value to test
|
|
*/
|
|
public function isHash ($algo, $val)
|
|
{
|
|
return \verify::IsHash ($algo, $val);
|
|
}
|
|
|
|
/** Return true if the provided string is a valid hash in static
|
|
* @param string $algo The hash algo to test
|
|
* @param string $val The value to test
|
|
*/
|
|
public static function staticIsHash ($algo, $val)
|
|
{
|
|
if (! is_string ($algo) || ! is_string ($val))
|
|
return false;
|
|
if (! in_array ($algo, hash_algos ()))
|
|
throw new \Exception ("Unknown algo provided to verify the hash", 500);
|
|
$len = strlen (hash ($algo, ""));
|
|
if (strlen ($val) !== $len)
|
|
return false;
|
|
if (\verify::staticIsAllowedChars ($val, "0123456789abcdef") === false)
|
|
return false;
|
|
return true;
|
|
}
|
|
}
|