PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/libraries/MY_Form_validation.php

http://github.com/pyrocms/pyrocms
PHP | 278 lines | 178 code | 39 blank | 61 comment | 33 complexity | a13cc6a049574d12c73ce81e43c4b716 MD5 | raw file
Possible License(s): CC0-1.0, MIT
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
  2. class MY_Form_validation extends CI_Form_validation
  3. {
  4. function __construct($rules = array())
  5. {
  6. parent::__construct($rules);
  7. $this->CI->load->language('extra_validation');
  8. }
  9. /**
  10. * Alpha-numeric with underscores dots and dashes
  11. *
  12. * @access public
  13. * @param string
  14. * @return bool
  15. */
  16. function alpha_dot_dash($str)
  17. {
  18. return ( ! preg_match("/^([-a-z0-9_\-\.])+$/i", $str)) ? FALSE : TRUE;
  19. }
  20. /**
  21. * Formats an UTF-8 string and removes potential harmful characters
  22. *
  23. * @access public
  24. * @param string
  25. * @return string
  26. * @author Jeroen v.d. Gulik
  27. * @since v1.0-beta1
  28. * @todo Find decent regex to check utf-8 strings for harmful characters
  29. */
  30. function utf8($str)
  31. {
  32. // If they don't have mbstring enabled (suckers) then we'll have to do with what we got
  33. if ( ! function_exists($str))
  34. {
  35. return $str;
  36. }
  37. $str = mb_convert_encoding($str, 'UTF-8', 'UTF-8');
  38. return htmlentities($str, ENT_QUOTES, 'UTF-8');
  39. }
  40. // NOTE: This was done because HMVC is not happy with $this->CI being used as a callback, instead it wants to look at CI::APP->controller
  41. // -- Phil
  42. /**
  43. * Executes the Validation routines
  44. *
  45. * @access private
  46. * @param array
  47. * @param array
  48. * @param mixed
  49. * @param integer
  50. * @return mixed
  51. */
  52. protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
  53. {
  54. // If the $_POST data is an array we will run a recursive call
  55. if (is_array($postdata))
  56. {
  57. foreach ($postdata as $key => $val)
  58. {
  59. $this->_execute($row, $rules, $val, $cycles);
  60. $cycles++;
  61. }
  62. return;
  63. }
  64. // --------------------------------------------------------------------
  65. // If the field is blank, but NOT required, no further tests are necessary
  66. $callback = FALSE;
  67. if ( ! in_array('required', $rules) AND is_null($postdata))
  68. {
  69. // Before we bail out, does the rule contain a callback?
  70. if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match))
  71. {
  72. $callback = TRUE;
  73. $rules = (array('1' => $match[1]));
  74. }
  75. else
  76. {
  77. return;
  78. }
  79. }
  80. // --------------------------------------------------------------------
  81. // Isset Test. Typically this rule will only apply to checkboxes.
  82. if (is_null($postdata) AND $callback == FALSE)
  83. {
  84. if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
  85. {
  86. // Set the message type
  87. $type = (in_array('required', $rules)) ? 'required' : 'isset';
  88. if ( ! isset($this->_error_messages[$type]))
  89. {
  90. if (FALSE === ($line = $this->CI->lang->line($type)))
  91. {
  92. $line = 'The field was not set';
  93. }
  94. }
  95. else
  96. {
  97. $line = $this->_error_messages[$type];
  98. }
  99. // Build the error message
  100. $message = sprintf($line, $this->_translate_fieldname($row['label']));
  101. // Save the error message
  102. $this->_field_data[$row['field']]['error'] = $message;
  103. if ( ! isset($this->_error_array[$row['field']]))
  104. {
  105. $this->_error_array[$row['field']] = $message;
  106. }
  107. }
  108. return;
  109. }
  110. // --------------------------------------------------------------------
  111. // Cycle through each rule and run it
  112. foreach ($rules As $rule)
  113. {
  114. $_in_array = FALSE;
  115. // We set the $postdata variable with the current data in our master array so that
  116. // each cycle of the loop is dealing with the processed data from the last cycle
  117. if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
  118. {
  119. // We shouldn't need this safety, but just in case there isn't an array index
  120. // associated with this cycle we'll bail out
  121. if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
  122. {
  123. continue;
  124. }
  125. $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
  126. $_in_array = TRUE;
  127. }
  128. else
  129. {
  130. $postdata = $this->_field_data[$row['field']]['postdata'];
  131. }
  132. // --------------------------------------------------------------------
  133. // Is the rule a callback?
  134. $callback = FALSE;
  135. if (substr($rule, 0, 9) == 'callback_')
  136. {
  137. $rule = substr($rule, 9);
  138. $callback = TRUE;
  139. }
  140. // Strip the parameter (if exists) from the rule
  141. // Rules can contain a parameter: max_length[5]
  142. $param = FALSE;
  143. if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
  144. {
  145. $rule = $match[1];
  146. $param = $match[2];
  147. }
  148. // Call the function that corresponds to the rule
  149. if ($callback === TRUE)
  150. {
  151. if ( ! method_exists(CI::$APP->controller, $rule))
  152. {
  153. throw new Exception('Undefined callback "$rule" in '.CI::$APP->controller);
  154. }
  155. // Run the function and grab the result
  156. $result = call_user_func(array(new CI::$APP->controller, $rule), $postdata, $param);
  157. // Re-assign the result to the master data array
  158. if ($_in_array == TRUE)
  159. {
  160. $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
  161. }
  162. else
  163. {
  164. $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
  165. }
  166. // If the field isn't required and we just processed a callback we'll move on...
  167. if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
  168. {
  169. continue;
  170. }
  171. }
  172. else
  173. {
  174. if ( ! method_exists($this, $rule))
  175. {
  176. // If our own wrapper function doesn't exist we see if a native PHP function does.
  177. // Users can use any native PHP function call that has one param.
  178. if (function_exists($rule))
  179. {
  180. $result = $rule($postdata);
  181. if ($_in_array == TRUE)
  182. {
  183. $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
  184. }
  185. else
  186. {
  187. $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
  188. }
  189. }
  190. else
  191. {
  192. log_message('debug', "Unable to find validation rule: ".$rule);
  193. }
  194. continue;
  195. }
  196. $result = $this->$rule($postdata, $param);
  197. if ($_in_array == TRUE)
  198. {
  199. $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
  200. }
  201. else
  202. {
  203. $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
  204. }
  205. }
  206. // Did the rule test negatively? If so, grab the error.
  207. if ($result === FALSE)
  208. {
  209. if ( ! isset($this->_error_messages[$rule]))
  210. {
  211. if (FALSE === ($line = $this->CI->lang->line($rule)))
  212. {
  213. $line = 'Unable to access an error message corresponding to your field name.';
  214. }
  215. }
  216. else
  217. {
  218. $line = $this->_error_messages[$rule];
  219. }
  220. // Is the parameter we are inserting into the error message the name
  221. // of another field? If so we need to grab its "field label"
  222. if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
  223. {
  224. $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
  225. }
  226. // Build the error message
  227. $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
  228. // Save the error message
  229. $this->_field_data[$row['field']]['error'] = $message;
  230. if ( ! isset($this->_error_array[$row['field']]))
  231. {
  232. $this->_error_array[$row['field']] = $message;
  233. }
  234. return;
  235. }
  236. }
  237. }
  238. }
  239. /* End of file MY_Form_validation.php */