PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/public/codeCore/Classes/php/Filters.php

https://github.com/IAmCorbin/MooKit
PHP | 197 lines | 103 code | 2 blank | 92 comment | 30 complexity | 2d93573f1e8e33b44ca9966b2ae6f544 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Contains Filters Class
  4. * @package MooKit
  5. */
  6. /**
  7. *Class to encapsulate php 5.2 filter_var for simple form validation, also encapulates htmLawed usage
  8. *
  9. * @author Corbin Tarrant
  10. * @copyright Febuary 19th, 2010
  11. * @link http://www.IAmCorbin.net
  12. * @package MooKit
  13. */
  14. Class Filters {
  15. /** @var array $errors array of thrown filter errors */
  16. var $errors;
  17. /** @var array $htmLawedConfig htmLawed configuration settings */
  18. var $htmLawedConfig;
  19. /**
  20. * Constructor
  21. *
  22. * Initialize the $errors array to 'none'
  23. * @param array $htmLawed htmLawed configuration settings
  24. */
  25. public function __construct($htmLawed = null) {
  26. $errors = array();
  27. if(!$htmLawed)
  28. $htmLawed = array('safe'=>1,
  29. 'tidy'=>0,
  30. 'deny_attribute'=>'* -href -target -style -class',
  31. 'schemes'=>'style: *; href: *; target: *');
  32. $this->htmLawedConfig = $htmLawed;
  33. }
  34. /**
  35. * Filter an Email
  36. *
  37. * Filter a user entered email address
  38. * @param string $user_email
  39. * @returns string
  40. */
  41. public function email($user_email) {
  42. //if not blank
  43. if($user_email !== '') {
  44. //sanitize to remove invalid characters
  45. $email = filter_var($user_email, FILTER_SANITIZE_EMAIL);
  46. //validate
  47. if(filter_var($email, FILTER_VALIDATE_EMAIL))
  48. return $email;
  49. else
  50. $this->errors[sizeof($this->errors)] = 'Invalid Email';
  51. }
  52. else {
  53. $this->errors[sizeof($this->errors)] = 'Invalid Email';
  54. }
  55. }
  56. /**
  57. * Filter text
  58. *
  59. * Filter user entered text
  60. * @param string $user_text
  61. * @param bool $stripWS Switch to optionally strip all whitespace
  62. * @param bool $allowBlank Switch to allow blank field
  63. * @returns string
  64. */
  65. public function text($user_text, $stripWS = false,$allowBlank=false) {
  66. //optional check for blank field
  67. if(!$allowBlank && $user_text == '') {
  68. $this->errors[] = 'Blank Field : '.$user_text;
  69. return $user_text;
  70. }
  71. //optionally remove whitespace
  72. if($stripWS && preg_match("/\ /",$user_text)) {
  73. $user_text = str_replace(" ","",$user_text);
  74. $this->errors[] = 'Whitespace Removed : '.$user_text;
  75. }
  76. //sanitize to remove invalid characters
  77. $text = filter_var($user_text, FILTER_SANITIZE_STRING);
  78. if($text !== $user_text ) {
  79. $this->errors[] = $user_text.'-> FILTER_SANITIZE_STRING -> '.$text;
  80. }
  81. //optional recheck for blank field
  82. if(!$allowBlank && $text == '') {
  83. $this->errors[] = 'Blank Field : '.$text;
  84. }
  85. return $text;
  86. }
  87. /**
  88. * Require All Alphanumeric or Underscore for characters
  89. *
  90. * Remove all non-alphanumeric characters
  91. * @param string $text
  92. * @param bool $stripWS Passed to text() - Switch to optionally strip all whitespace
  93. * @param bool $allowBlank Passed to text() - Switch to allow blank field
  94. * @returns string
  95. */
  96. public function alphnum_($user_text, $stripWS=FALSE, $allowBlank=FALSE) {
  97. $user_text = $this->text($user_text, $stripWS, $allowBlank);
  98. if(!preg_match("/^([a-zA-Z0-9\_\ ]*)$/",$user_text)) {
  99. $text = preg_replace("/[^a-zA-Z1-9\_]/","",$user_text);
  100. $this->errors[] = 'Non-Alphanumberic Characters Removed : '.$text;
  101. return $text;
  102. } else {
  103. return $user_text;
  104. }
  105. }
  106. /**
  107. * Require a valid number
  108. * @param string $user_input
  109. * @returns filtered input
  110. */
  111. public function number($user_text) {
  112. if($user_text !== '') {
  113. if(!preg_match("/^\-?([0-9]+)$/",$user_text)) {
  114. $this->errors[sizeof($this->errors)] = 'Non-Numeric Characters Removed : '.$user_text;
  115. $text = preg_replace("/[^1-9\-]/","",$user_text);
  116. return $text;
  117. } else {
  118. return $user_text;
  119. }
  120. }
  121. else {
  122. $this->errors[sizeof($this->errors)] = 'Blank Field : '.$user_text;
  123. }
  124. }
  125. /**
  126. * Filter a URL
  127. *
  128. * Filter a user entered URL
  129. * @param string $user_url
  130. * @returns string
  131. */
  132. public function url($user_url) {
  133. if($user_url !== '') {
  134. //sanitize to remove invalid characters
  135. $url = filter_var($user_url, FILTER_SANITIZE_URL);
  136. //validate
  137. if(filter_var($url, FILTER_VALIDATE_URL))
  138. return $url;
  139. else
  140. $this->errors[] = 'Invalid URL : '.$user_ur;;
  141. }
  142. else {
  143. $this->errors[] = 'Invalid URL : '.$user_url;
  144. }
  145. }
  146. /**
  147. * Filter against a regEx
  148. *
  149. * Filter user entered input against a provided regEx
  150. * @param regEx $regEx a valid regular expression
  151. * @param string $subject the string to test against
  152. * @returns string
  153. */
  154. public function regEx($regEx, $subject) {
  155. if(preg_match($regEx, $subject))
  156. return $subject;
  157. else
  158. $this->errors[sizeof($this->errors)] = 'RegEx Failed';
  159. }
  160. /**
  161. * Filter input with htmLawed
  162. * @link http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/
  163. * @param string $input the input to filter
  164. * @param bool $allowBlank Switch to allow blank field
  165. * @return string
  166. */
  167. public function htmLawed($input,$allowBlank=TRUE) {
  168. if(!$allowBlank && $input == '')
  169. $this->errors[] = 'Blank Field : '.$input;
  170. //run through htmLawed
  171. $lawedText = htmLawed($input,$this->htmLawedConfig);
  172. //flag error if text was changed
  173. if($lawedText !== $input)
  174. $this->errors[] = $input.' -> htmLawed -> '.$input;
  175. return $lawedText;
  176. }
  177. /**
  178. * ERRORS
  179. *
  180. * Returns the array of errors or null if none
  181. * @returns array|NULL
  182. */
  183. public function ERRORS() {
  184. if( sizeof($this->errors) > 0 ) {
  185. if(DEBUG) { echo "|-FILTER ERRORS-|"; var_dump($this->errors); }
  186. return $this->errors;
  187. } else {
  188. return NULL;
  189. }
  190. }
  191. //possible functions later
  192. //$filteredInput['state'] = $inputFilter->regEx('/^[a-z]{2}$/i',$_POST['state']);
  193. //$filteredInput['zip'] = $inputFilter->regEx('/^[0-9]{5}$|^[0-9]{5}\-[0-9]{4}$/',$_POST['zip']);
  194. }
  195. ?>