PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/valid.php

https://github.com/MHordecki/milionkostek
PHP | 291 lines | 125 code | 35 blank | 131 comment | 12 complexity | e89324ba2ee09d65b15d09a34986ae11 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Validation helper class.
  4. *
  5. * $Id: valid.php 2742 2008-06-02 17:51:45Z Geert $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class valid_Core {
  13. /**
  14. * Validate email, commonly used characters only
  15. *
  16. * @param string email address
  17. * @return boolean
  18. */
  19. public static function email($email)
  20. {
  21. return (bool) preg_match('/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD', (string) $email);
  22. }
  23. /**
  24. * Validate the domain of an email address by checking if the domain has a
  25. * valid MX record.
  26. *
  27. * @param string email address
  28. * @return boolean
  29. */
  30. public static function email_domain($email)
  31. {
  32. // Check if the email domain has a valid MX record
  33. return (bool) checkdnsrr(preg_replace('/^[^@]+@/', '', $email), 'MX');
  34. }
  35. /**
  36. * Validate email, RFC compliant version
  37. * Note: This function is LESS strict than valid_email. Choose carefully.
  38. *
  39. * @see Originally by Cal Henderson, modified to fit Kohana syntax standards:
  40. * @see http://www.iamcal.com/publish/articles/php/parsing_email/
  41. * @see http://www.w3.org/Protocols/rfc822/
  42. *
  43. * @param string email address
  44. * @return boolean
  45. */
  46. public static function email_rfc($email)
  47. {
  48. $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  49. $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  50. $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  51. $pair = '\\x5c[\\x00-\\x7f]';
  52. $domain_literal = "\\x5b($dtext|$pair)*\\x5d";
  53. $quoted_string = "\\x22($qtext|$pair)*\\x22";
  54. $sub_domain = "($atom|$domain_literal)";
  55. $word = "($atom|$quoted_string)";
  56. $domain = "$sub_domain(\\x2e$sub_domain)*";
  57. $local_part = "$word(\\x2e$word)*";
  58. $addr_spec = "$local_part\\x40$domain";
  59. return (bool) preg_match('/^'.$addr_spec.'$/D', (string) $email);
  60. }
  61. /**
  62. * Validate URL
  63. *
  64. * @param string URL
  65. * @return boolean
  66. */
  67. public static function url($url)
  68. {
  69. return (bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED);
  70. }
  71. /**
  72. * Validate IP
  73. *
  74. * @param string IP address
  75. * @param boolean allow IPv6 addresses
  76. * @return boolean
  77. */
  78. public static function ip($ip, $ipv6 = FALSE)
  79. {
  80. // Do not allow private and reserved range IPs
  81. $flags = FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
  82. if ($ipv6 === TRUE)
  83. return (bool) filter_var($ip, FILTER_VALIDATE_IP, $flags);
  84. return (bool) filter_var($ip, FILTER_VALIDATE_IP, $flags | FILTER_FLAG_IPV4);
  85. }
  86. /**
  87. * Validates a credit card number using the Luhn (mod10) formula.
  88. * @see http://en.wikipedia.org/wiki/Luhn_algorithm
  89. *
  90. * @param integer credit card number
  91. * @param string card type
  92. * @return boolean
  93. */
  94. public static function credit_card($number, $type = 'default')
  95. {
  96. // Remove all non-digit characters from the number
  97. if (($number = preg_replace('/\D+/', '', $number)) === '')
  98. return FALSE;
  99. $cards = Config::item('credit_cards');
  100. // Check card type
  101. $type = strtolower($type);
  102. if ( ! isset($cards[$type]))
  103. return FALSE;
  104. // Check card number length
  105. $length = strlen($number);
  106. // Validate the card length by the card type
  107. if ( ! in_array($length, preg_split('/\D+/', $cards[$type]['length'])))
  108. return FALSE;
  109. // Check card number prefix
  110. if ( ! preg_match('/^'.$cards[$type]['prefix'].'/', $number))
  111. return FALSE;
  112. // No Luhn check required
  113. if ($cards[$type]['luhn'] == FALSE)
  114. return TRUE;
  115. // Checksum of the card number
  116. $checksum = 0;
  117. for ($i = $length - 1; $i >= 0; $i -= 2)
  118. {
  119. // Add up every 2nd digit, starting from the right
  120. $checksum += substr($number, $i, 1);
  121. }
  122. for ($i = $length - 2; $i >= 0; $i -= 2)
  123. {
  124. // Add up every 2nd digit doubled, starting from the right
  125. $double = substr($number, $i, 1) * 2;
  126. // Subtract 9 from the double where value is greater than 10
  127. $checksum += ($double >= 10) ? $double - 9 : $double;
  128. }
  129. // If the checksum is a multiple of 10, the number is valid
  130. return ($checksum % 10 === 0);
  131. }
  132. /**
  133. * Checks if a phone number is valid.
  134. *
  135. * @param string phone number to check
  136. * @return boolean
  137. */
  138. public static function phone($number, $lengths = NULL)
  139. {
  140. if ( ! is_array($lengths))
  141. {
  142. $lengths = array(7,10,11);
  143. }
  144. // Remove all non-digit characters from the number
  145. $number = preg_replace('/\D+/', '', $number);
  146. // Check if the number is within range
  147. return in_array(strlen($number), $lengths);
  148. }
  149. /**
  150. * Checks whether a string consists of alphabetical characters only.
  151. *
  152. * @param string input string
  153. * @param boolean trigger UTF-8 compatibility
  154. * @return boolean
  155. */
  156. public static function alpha($str, $utf8 = FALSE)
  157. {
  158. return ($utf8 === TRUE)
  159. ? (bool) preg_match('/^\pL++$/uD', (string) $str)
  160. : ctype_alpha((string) $str);
  161. }
  162. /**
  163. * Checks whether a string consists of alphabetical characters and numbers only.
  164. *
  165. * @param string input string
  166. * @param boolean trigger UTF-8 compatibility
  167. * @return boolean
  168. */
  169. public static function alpha_numeric($str, $utf8 = FALSE)
  170. {
  171. return ($utf8 === TRUE)
  172. ? (bool) preg_match('/^[\pL\pN]++$/uD', (string) $str)
  173. : ctype_alnum((string) $str);
  174. }
  175. /**
  176. * Checks whether a string consists of alphabetical characters, numbers, underscores and dashes only.
  177. *
  178. * @param string input string
  179. * @param boolean trigger UTF-8 compatibility
  180. * @return boolean
  181. */
  182. public static function alpha_dash($str, $utf8 = FALSE)
  183. {
  184. return ($utf8 === TRUE)
  185. ? (bool) preg_match('/^[-\pL\pN_]++$/uD', (string) $str)
  186. : (bool) preg_match('/^[-a-z0-9_]++$/iD', (string) $str);
  187. }
  188. /**
  189. * Checks whether a string consists of digits only (no dots or dashes).
  190. *
  191. * @param string input string
  192. * @param boolean trigger UTF-8 compatibility
  193. * @return boolean
  194. */
  195. public static function digit($str, $utf8 = FALSE)
  196. {
  197. return ($utf8 === TRUE)
  198. ? (bool) preg_match('/^\pN++$/uD', (string) $str)
  199. : ctype_digit((string) $str);
  200. }
  201. /**
  202. * Checks whether a string is a valid number (negative and decimal numbers allowed).
  203. *
  204. * @param string input string
  205. * @return boolean
  206. */
  207. public static function numeric($str)
  208. {
  209. return (is_numeric($str) AND preg_match('/^[-0-9.]++$/D', (string) $str));
  210. }
  211. /**
  212. * Checks whether a string is a valid text. Letters, numbers, whitespace,
  213. * dashes, periods, and underscores are allowed.
  214. *
  215. * @param string $str
  216. * @return boolean
  217. */
  218. public static function standard_text($str)
  219. {
  220. return (bool) preg_match('/^[-\pL\pN\pZ_.]++$/uD', (string) $str);
  221. }
  222. /**
  223. * Checks if a string is a proper decimal format. The format array can be
  224. * used to specify a decimal length, or a number and decimal length, eg:
  225. * array(2) would force the number to have 2 decimal places, array(4,2)
  226. * would force the number to have 4 digits and 2 decimal places.
  227. *
  228. * @param string input string
  229. * @param array decimal format: y or x,y
  230. * @return boolean
  231. */
  232. public function decimal($str, $format = NULL)
  233. {
  234. // Create the pattern
  235. $pattern = '/^[0-9]%s\.[0-9]%s$/';
  236. if ( ! empty($format))
  237. {
  238. if (count($format) > 1)
  239. {
  240. // Use the format for number and decimal length
  241. $pattern = sprintf($pattern, '{'.$format[0].'}', '{'.$format[1].'}');
  242. }
  243. elseif (count($format) > 0)
  244. {
  245. // Use the format as decimal length
  246. $pattern = sprintf($pattern, '+', '{'.$format[0].'}');
  247. }
  248. }
  249. else
  250. {
  251. // No format
  252. $pattern = sprintf($pattern, '+', '+');
  253. }
  254. return (bool) preg_match($pattern, (string) $str);
  255. }
  256. } // End valid