PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/valid.php

https://github.com/lmorchard/friendfeedarchiver
PHP | 275 lines | 124 code | 36 blank | 115 comment | 19 complexity | ded0731e644e9e68663a58d39b2f3937 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Validation helper class.
  4. *
  5. * $Id: valid.php 1865 2008-01-29 19:48:29Z Geert $
  6. *
  7. * @package Validation
  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-z]{2,6}$/iD', $email);
  22. }
  23. /**
  24. * Validate email, RFC compliant version
  25. * Note: This function is LESS strict than valid_email. Choose carefully.
  26. *
  27. * @see Originally by Cal Henderson, modified to fit Kohana syntax standards:
  28. * @see http://www.iamcal.com/publish/articles/php/parsing_email/
  29. * @see http://www.w3.org/Protocols/rfc822/
  30. *
  31. * @param string email address
  32. * @return boolean
  33. */
  34. public static function email_rfc($email)
  35. {
  36. $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  37. $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  38. $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  39. $pair = '\\x5c[\\x00-\\x7f]';
  40. $domain_literal = "\\x5b($dtext|$pair)*\\x5d";
  41. $quoted_string = "\\x22($qtext|$pair)*\\x22";
  42. $sub_domain = "($atom|$domain_literal)";
  43. $word = "($atom|$quoted_string)";
  44. $domain = "$sub_domain(\\x2e$sub_domain)*";
  45. $local_part = "$word(\\x2e$word)*";
  46. $addr_spec = "$local_part\\x40$domain";
  47. return (bool) preg_match('/^'.$addr_spec.'$/D', $email);
  48. }
  49. /**
  50. * Validate URL
  51. *
  52. * @param string URL
  53. * @param string protocol
  54. * @return boolean
  55. */
  56. public static function url($url, $scheme = 'http')
  57. {
  58. // Scheme is always lowercase
  59. $scheme = strtolower($scheme);
  60. // Disable error reporting
  61. $ER = error_reporting(0);
  62. // Use parse_url to validate the URL
  63. $url = parse_url($url);
  64. // Restore error reporting
  65. error_reporting($ER);
  66. // If the boolean check returns TRUE, return FALSE, and vice versa
  67. return ! (empty($url['host']) OR empty($url['scheme']) OR $url['scheme'] !== $scheme);
  68. }
  69. /**
  70. * Validate IP
  71. *
  72. * @param string IP address
  73. * @return boolean
  74. */
  75. public static function ip($ip)
  76. {
  77. if ( ! preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/D', $ip))
  78. return FALSE;
  79. $octets = explode('.', $ip);
  80. for ($i = 1; $i < 5; $i++)
  81. {
  82. $octet = (int) $octets[($i-1)];
  83. if ($i === 1)
  84. {
  85. if ($octet > 223 OR $octet < 1)
  86. return FALSE;
  87. }
  88. elseif ($i === 4)
  89. {
  90. if ($octet < 1)
  91. return FALSE;
  92. }
  93. else
  94. {
  95. if ($octet > 254)
  96. return FALSE;
  97. }
  98. }
  99. return TRUE;
  100. }
  101. /**
  102. * Validates a credit card number using the Luhn (mod10) formula.
  103. * @see http://en.wikipedia.org/wiki/Luhn_algorithm
  104. *
  105. * @param integer credit card number
  106. * @param string card type
  107. * @return boolean
  108. */
  109. public static function credit_card($number, $type = 'default')
  110. {
  111. // Remove all non-digit characters from the number
  112. if (($number = preg_replace('/\D+/', '', $number)) === '')
  113. return FALSE;
  114. $cards = Config::item('credit_cards');
  115. // Check card type
  116. $type = strtolower($type);
  117. if ( ! isset($cards[$type]))
  118. return FALSE;
  119. // Check card number length
  120. $length = strlen($number);
  121. // Validate the card length by the card type
  122. if ( ! in_array($length, preg_split('/\D+/', $cards[$type]['length'])))
  123. return FALSE;
  124. // Check card number prefix
  125. if ( ! preg_match('/^'.$cards[$type]['prefix'].'/', $number))
  126. return FALSE;
  127. // No Luhn check required
  128. if ($cards[$type]['luhn'] == FALSE)
  129. return TRUE;
  130. // Checksum of the card number
  131. $checksum = 0;
  132. for ($i = $length - 1; $i >= 0; $i -= 2)
  133. {
  134. // Add up every 2nd digit, starting from the right
  135. $checksum += substr($number, $i, 1);
  136. }
  137. for ($i = $length - 2; $i >= 0; $i -= 2)
  138. {
  139. // Add up every 2nd digit doubled, starting from the right
  140. $double = substr($number, $i, 1) * 2;
  141. // Subtract 9 from the double where value is greater than 10
  142. $checksum += ($double >= 10) ? $double - 9 : $double;
  143. }
  144. // If the checksum is a multiple of 10, the number is valid
  145. return ($checksum % 10 === 0);
  146. }
  147. /**
  148. * Checks if a phone number is valid.
  149. *
  150. * @todo This function is not l10n-compatible.
  151. *
  152. * @param string phone number to check
  153. * @return boolean
  154. */
  155. public static function phone($number)
  156. {
  157. // Remove all non-digit characters from the number
  158. $number = preg_replace('/\D+/', '', $number);
  159. if (strlen($number) > 10 AND substr($number, 0, 1) === '1')
  160. {
  161. // Remove the "1" prefix from the number
  162. $number = substr($number, 1);
  163. }
  164. // If the length is not 10, it's not a valid number
  165. return (strlen($number) === 10);
  166. }
  167. /**
  168. * Checks whether a string consists of alphabetical characters only.
  169. *
  170. * @param string input string
  171. * @param boolean trigger UTF-8 compatibility
  172. * @return boolean
  173. */
  174. public static function alpha($str, $utf8 = FALSE)
  175. {
  176. return (bool) ($utf8 == TRUE)
  177. ? preg_match('/^\pL++$/uD', (string) $str)
  178. : ctype_alpha((string) $str);
  179. }
  180. /**
  181. * Checks whether a string consists of alphabetical characters and numbers only.
  182. *
  183. * @param string input string
  184. * @param boolean trigger UTF-8 compatibility
  185. * @return boolean
  186. */
  187. public static function alpha_numeric($str, $utf8 = FALSE)
  188. {
  189. return (bool) ($utf8 == TRUE)
  190. ? preg_match('/^[\pL\pN]++$/uD', (string) $str)
  191. : ctype_alnum((string) $str);
  192. }
  193. /**
  194. * Checks whether a string consists of alphabetical characters, numbers, underscores and dashes only.
  195. *
  196. * @param string input string
  197. * @param boolean trigger UTF-8 compatibility
  198. * @return boolean
  199. */
  200. public static function alpha_dash($str, $utf8 = FALSE)
  201. {
  202. return (bool) ($utf8 == TRUE)
  203. ? preg_match('/^[-\pL\pN_]++$/uD', (string) $str)
  204. : preg_match('/^[-a-z0-9_]++$/iD', (string) $str);
  205. }
  206. /**
  207. * Checks whether a string consists of digits only (no dots or dashes).
  208. *
  209. * @param string input string
  210. * @param boolean trigger UTF-8 compatibility
  211. * @return boolean
  212. */
  213. public static function digit($str, $utf8 = FALSE)
  214. {
  215. return (bool) ($utf8 == TRUE)
  216. ? preg_match('/^\pN++$/uD', (string) $str)
  217. : ctype_digit((string) $str);
  218. }
  219. /**
  220. * Checks whether a string is a valid number (negative and decimal numbers allowed).
  221. *
  222. * @param string input string
  223. * @return boolean
  224. */
  225. public static function numeric($str)
  226. {
  227. return (is_numeric($str) AND preg_match('/^[-0-9.]++$/D', $str));
  228. }
  229. /**
  230. * Checks whether a string is a valid text.
  231. *
  232. * @param string $str
  233. * @return boolean
  234. */
  235. public static function standard_text($str)
  236. {
  237. return preg_match('/^[-\pL\pN\pZ_]++$/uD', (string) $str);
  238. }
  239. } // End valid