PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/valid.php

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