PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/www/wptmonitor/lib/FormValidator.php

http://webpagetest.googlecode.com/
PHP | 201 lines | 139 code | 18 blank | 44 comment | 10 complexity | 6f5eaba205cbdc25caaa1dd6ad965699 MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-3.0, LGPL-3.0, MIT, BSD-3-Clause, ISC, LGPL-2.1
  1. <?php
  2. /**
  3. * Pork Formvalidator. validates fields by regexes and can sanatize them. Uses PHP filter_var built-in functions and extra regexes
  4. * @package pork
  5. */
  6. /**
  7. * Pork.FormValidator
  8. * Validates arrays or properties by setting up simple arrays
  9. *
  10. * @package pork
  11. * @author SchizoDuckie
  12. * @copyright SchizoDuckie 2009
  13. * @version 1.0
  14. * @access public
  15. */
  16. class FormValidator
  17. {
  18. public static $regexes = Array(
  19. 'date' => "^[0-9]{4}[-/][0-9]{1,2}[-/][0-9]{1,2}\$",
  20. 'amount' => "^[-]?[0-9]+\$",
  21. 'number' => "^[-]?[0-9,]+\$",
  22. 'alfanum' => "^[0-9a-zA-Z ,.-_\\s\?\!]+\$",
  23. 'not_empty' => "[a-z0-9A-Z]+",
  24. 'words' => "^[A-Za-z]+[A-Za-z \\s]*\$",
  25. 'phone' => "^[0-9]{10,11}\$",
  26. 'zipcode' => "^[1-9][0-9]{3}[a-zA-Z]{2}\$",
  27. 'plate' => "^([0-9a-zA-Z]{2}[-]){2}[0-9a-zA-Z]{2}\$",
  28. 'price' => "^[0-9.,]*(([.,][-])|([.,][0-9]{2}))?\$",
  29. '2digitopt' => "^\d+(\,\d{2})?\$",
  30. '2digitforce' => "^\d+\,\d\d\$",
  31. 'anything' => "^[\d\D]{1,}\$"
  32. );
  33. private $validations, $sanatations, $mandatories, $errors, $corrects, $fields;
  34. public function __construct($validations=array(), $mandatories = array(), $sanatations = array())
  35. {
  36. $this->validations = $validations;
  37. $this->sanatations = $sanatations;
  38. $this->mandatories = $mandatories;
  39. $this->errors = array();
  40. $this->corrects = array();
  41. }
  42. /**
  43. * Validates an array of items (if needed) and returns true or false
  44. *
  45. */
  46. public function validate($items)
  47. {
  48. $this->fields = $items;
  49. $havefailures = false;
  50. foreach($items as $key=>$val)
  51. {
  52. if((strlen($val) == 0 || array_search($key, $this->validations) === false) && array_search($key, $this->mandatories) === false)
  53. {
  54. $this->corrects[] = $key;
  55. continue;
  56. }
  57. $result = self::validateItem($val, $this->validations[$key]);
  58. if($result === false) {
  59. $havefailures = true;
  60. $this->addError($key, $this->validations[$key]);
  61. }
  62. else
  63. {
  64. $this->corrects[] = $key;
  65. }
  66. }
  67. return(!$havefailures);
  68. }
  69. /**
  70. *
  71. * Adds unvalidated class to thos elements that are not validated. Removes them from classes that are.
  72. */
  73. public function getScript() {
  74. if(!empty($this->errors))
  75. {
  76. $errors = array();
  77. foreach($this->errors as $key=>$val) { $errors[] = "'INPUT[name={$key}]'"; }
  78. $output = '$$('.implode(',', $errors).').addClass("unvalidated");';
  79. $output .= "alert('there are errors in the form');"; // or your nice validation here
  80. }
  81. if(!empty($this->corrects))
  82. {
  83. $corrects = array();
  84. foreach($this->corrects as $key) { $corrects[] = "'INPUT[name={$key}]'"; }
  85. $output .= '$$('.implode(',', $corrects).').removeClass("unvalidated");';
  86. }
  87. $output = "<script type='text/javascript'>{$output} </script>";
  88. return($output);
  89. }
  90. /**
  91. *
  92. * Sanatizes an array of items according to the $this->sanatations
  93. * sanatations will be standard of type string, but can also be specified.
  94. * For ease of use, this syntax is accepted:
  95. * $sanatations = array('fieldname', 'otherfieldname'=>'float');
  96. */
  97. public function sanatize($items)
  98. {
  99. foreach($items as $key=>$val)
  100. {
  101. if(array_search($key, $this->sanatations) === false && !array_key_exists($key, $this->sanatations)) continue;
  102. $items[$key] = self::sanatizeItem($val, $this->validations[$key]);
  103. }
  104. return($items);
  105. }
  106. /**
  107. *
  108. * Adds an error to the errors array.
  109. */
  110. private function addError($field, $type='string')
  111. {
  112. $this->errors[$field] = $type;
  113. }
  114. /**
  115. *
  116. * Sanatize a single var according to $type.
  117. * Allows for static calling to allow simple sanatization
  118. */
  119. public static function sanatizeItem($var, $type)
  120. {
  121. $flags = NULL;
  122. switch($type)
  123. {
  124. case 'url':
  125. $filter = FILTER_SANITIZE_URL;
  126. break;
  127. case 'int':
  128. $filter = FILTER_SANITIZE_NUMBER_INT;
  129. break;
  130. case 'float':
  131. $filter = FILTER_SANITIZE_NUMBER_FLOAT;
  132. $flags = FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND;
  133. break;
  134. case 'email':
  135. $var = substr($var, 0, 254);
  136. $filter = FILTER_SANITIZE_EMAIL;
  137. break;
  138. case 'string':
  139. default:
  140. $filter = FILTER_SANITIZE_STRING;
  141. $flags = FILTER_FLAG_NO_ENCODE_QUOTES;
  142. break;
  143. }
  144. $output = filter_var($var, $filter, $flags);
  145. return($output);
  146. }
  147. /**
  148. *
  149. * Validates a single var according to $type.
  150. * Allows for static calling to allow simple validation.
  151. *
  152. */
  153. public static function validateItem($var, $type)
  154. {
  155. if(array_key_exists($type, self::$regexes))
  156. {
  157. $returnval = filter_var($var, FILTER_VALIDATE_REGEXP, array("options"=> array("regexp"=>'!'.self::$regexes[$type].'!i'))) !== false;
  158. return($returnval);
  159. }
  160. $filter = false;
  161. switch($type)
  162. {
  163. case 'email':
  164. $var = substr($var, 0, 254);
  165. $filter = FILTER_VALIDATE_EMAIL;
  166. break;
  167. case 'int':
  168. $filter = FILTER_VALIDATE_INT;
  169. break;
  170. case 'boolean':
  171. $filter = FILTER_VALIDATE_BOOLEAN;
  172. break;
  173. case 'ip':
  174. $filter = FILTER_VALIDATE_IP;
  175. break;
  176. case 'url':
  177. $filter = FILTER_VALIDATE_URL;
  178. break;
  179. }
  180. return ($filter === false) ? false : filter_var($var, $filter) !== false ? true : false;
  181. }
  182. }
  183. ?>