PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/bonfire/libraries/BF_Form_validation.php

http://github.com/ci-bonfire/Bonfire
PHP | 330 lines | 144 code | 39 blank | 147 comment | 25 complexity | 05687a745e2cee3666678469e8b6bedd MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('BASEPATH') || exit('No direct script access allowed');
  2. /**
  3. * Bonfire
  4. *
  5. * An open source project to allow developers to jumpstart their development of
  6. * CodeIgniter applications
  7. *
  8. * @package Bonfire
  9. * @author Bonfire Dev Team
  10. * @copyright Copyright (c) 2011 - 2018, Bonfire Dev Team
  11. * @license http://opensource.org/licenses/MIT The MIT License
  12. * @link http://cibonfire.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. /**
  17. * Form Validation
  18. *
  19. * This class extends the CodeIgniter core Form_validation library to add extra
  20. * functionality used in Bonfire.
  21. *
  22. * @package Bonfire\Libraries\BF_Form_validation
  23. * @author Bonfire Dev Team
  24. * @link http://cibonfire.com/docs
  25. */
  26. class BF_Form_validation extends CI_Form_validation
  27. {
  28. /**
  29. * @var object The CodeIgniter core object.
  30. */
  31. public $CI;
  32. //--------------------------------------------------------------------
  33. /**
  34. * Constructor
  35. *
  36. * @return void
  37. */
  38. public function __construct($config = array())
  39. {
  40. // Merged super-global $_FILES to $_POST to allow for better file
  41. // validation inside of Form_validation library
  42. if (! empty($_FILES) && is_array($_FILES)) {
  43. $_POST = array_merge($_POST, $_FILES);
  44. }
  45. parent::__construct($config);
  46. }
  47. /**
  48. * Check if the field has an error associated with it.
  49. *
  50. * @param string $field The name of the field
  51. *
  52. * @return bool
  53. */
  54. public function has_error($field = null)
  55. {
  56. if (empty($field)) {
  57. return false;
  58. }
  59. return ! empty($this->_field_data[$field]['error']);
  60. }
  61. /**
  62. * Performs the actual form validation
  63. *
  64. * @param string $module Name of the module
  65. * @param string $group Name of the group array containing the rules
  66. *
  67. * @return bool Success or Failure
  68. */
  69. public function run($module = '', $group = '')
  70. {
  71. $this->CI->lang->load('bf_form_validation');
  72. is_object($module) && $this->CI =& $module;
  73. return parent::run($group);
  74. }
  75. /**
  76. * Returns Form Validation Errors in an HTML Un-ordered list format.
  77. *
  78. * @return string|bool Form Validation Errors in an HTML Un-ordered list, or
  79. * false when no errors are returned.
  80. */
  81. public function validation_errors_list()
  82. {
  83. $errors = $this->CI->form_validation->error_string('<li>', '</li>');
  84. if (empty($errors)) {
  85. return false;
  86. }
  87. return '<ul>' . PHP_EOL . "{$errors}</ul>";
  88. }
  89. //--------------------------------------------------------------------------
  90. // Validation Rules
  91. //--------------------------------------------------------------------------
  92. /**
  93. * Set allowed file-types in your form_validation rules.
  94. *
  95. * Please separate the allowed file types with a pipe or |.
  96. *
  97. * @author Shawn Crigger <support@s-vizion.com>
  98. *
  99. * @param string $str String field name to validate
  100. * @param string $types String allowed types
  101. *
  102. * @return bool If files are in the allowed type array then TRUE else FALSE
  103. */
  104. public function allowed_types($str, $types = null)
  105. {
  106. if (! $types) {
  107. log_message('debug', 'form_validation method allowed_types was called without any allowed types.');
  108. $this->CI->form_validation->set_message('allowed_types', lang('bf_form_allowed_types_none'));
  109. return false;
  110. }
  111. $type = explode('|', $types);
  112. $filetype = pathinfo($str['name'], PATHINFO_EXTENSION);
  113. if (in_array($filetype, $type)) {
  114. return true;
  115. }
  116. $this->CI->form_validation->set_message('allowed_types', lang('bf_form_allowed_types'));
  117. return false;
  118. }
  119. /**
  120. * Check that a string only contains Alpha-numeric characters with periods,
  121. * underscores, spaces, and dashes
  122. *
  123. * @param string $str The string value to check
  124. *
  125. * @return bool
  126. */
  127. public function alpha_extra($str)
  128. {
  129. if (preg_match("/^([\.\s-a-z0-9_-])+$/i", $str)) {
  130. return true;
  131. }
  132. $this->CI->form_validation->set_message('alpha_extra', lang('bf_form_alpha_extra'));
  133. return false;
  134. }
  135. /**
  136. * Check that the string matches a specific regex pattern
  137. *
  138. * @param string $str The string to check
  139. * @param string $pattern The pattern used to check the string
  140. *
  141. * @return bool
  142. */
  143. public function matches_pattern($str, $pattern)
  144. {
  145. if (preg_match('/^' . $pattern . '$/', $str)) {
  146. return true;
  147. }
  148. $this->CI->form_validation->set_message('matches_pattern', lang('bf_form_matches_pattern'));
  149. return false;
  150. }
  151. /**
  152. * Set maximum file upload size in your form validation rules.
  153. *
  154. * @author Shawn Crigger <support@s-vizion.com>
  155. *
  156. * @param string $str String field name to validate
  157. * @param integer $size Integer maximum upload size in bytes
  158. *
  159. * @return bool
  160. */
  161. public function max_file_size($str, $size = 0)
  162. {
  163. if (empty($size)) {
  164. log_message('error', 'Form_validation rule, max_file_size was called without setting an allowable file size.');
  165. $this->CI->form_validation->set_message('max_file_size', str_replace('{max_size}', '0', lang('bf_form_max_file_size')));
  166. return false;
  167. }
  168. if ($str['size'] <= $size) {
  169. return true;
  170. }
  171. $this->CI->form_validation->set_message('max_file_size', str_replace('{max_size}', $size, lang('bf_form_max_file_size')));
  172. return false;
  173. }
  174. /**
  175. * Verify that the entered string is one of the values entered as the second
  176. * parameter.
  177. *
  178. * Please separate the allowed values with a comma.
  179. *
  180. * @param string $str String field name to validate
  181. * @param string $options String allowed values
  182. *
  183. * @return bool If files are in the allowed type array then TRUE else FALSE
  184. */
  185. public function one_of($str, $options = null)
  186. {
  187. if (! $options) {
  188. log_message('debug', 'form_validation method one_of was called without any possible values.');
  189. $this->CI->form_validation->set_message('one_of', lang('bf_form_one_of_none'));
  190. return false;
  191. }
  192. log_message('debug', "form_validation one_of options: {$options}");
  193. $possible_values = explode(',', $options);
  194. if (in_array($str, $possible_values)) {
  195. return true;
  196. }
  197. $this->CI->form_validation->set_message('one_of', lang('bf_form_one_of'));
  198. return false;
  199. }
  200. /**
  201. * Checks that a value is unique in the database.
  202. *
  203. * i.e. '…|required|unique[users.name,users.id]|trim…'
  204. *
  205. * <code>
  206. * "unique[tablename.fieldname,tablename.(primaryKey-used-for-updates)]"
  207. * </code>
  208. *
  209. * @author Adapted from Burak Guzel <http://net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-masters/>
  210. *
  211. * @param mixed $value The value to be checked.
  212. * @param mixed $params The table and field to check against, if a second
  213. * field is passed in this is used as "AND NOT EQUAL".
  214. *
  215. * @return bool True if the value is unique for that field, else false.
  216. */
  217. public function unique($value, $params)
  218. {
  219. // Allow for more than 1 parameter.
  220. $fields = explode(",", $params);
  221. // Extract the table and field from the first parameter.
  222. list($table, $field) = explode('.', $fields[0], 2);
  223. // Setup the db request.
  224. $this->CI->db->select($field)
  225. ->from($table)
  226. ->where($field, $value)
  227. ->limit(1);
  228. // Check whether a second parameter was passed to be used as an
  229. // "AND NOT EQUAL" where clause
  230. // eg "select * from users where users.name='test' AND users.id != 4
  231. if (isset($fields[1])) {
  232. // Extract the table and field from the second parameter
  233. list($where_table, $where_field) = explode('.', $fields[1], 2);
  234. // Get the value from the post's $where_field. If the value is set,
  235. // add "AND NOT EQUAL" where clause.
  236. $where_value = $this->CI->input->post($where_field);
  237. if (isset($where_value)) {
  238. $this->CI->db->where("{$where_table}.{$where_field} <>", $where_value);
  239. }
  240. }
  241. // If any rows are returned from the database, validation fails
  242. $query = $this->CI->db->get();
  243. if ($query->row()) {
  244. $this->CI->form_validation->set_message('unique', lang('bf_form_unique'));
  245. return false;
  246. }
  247. return true;
  248. }
  249. /**
  250. * Check the entered password against the password strength settings.
  251. *
  252. * @param string $str The password string to check
  253. *
  254. * @return bool
  255. */
  256. public function valid_password($str)
  257. {
  258. // Get the password strength settings from the database
  259. $min_length = $this->CI->settings_lib->item('auth.password_min_length');
  260. $use_nums = $this->CI->settings_lib->item('auth.password_force_numbers');
  261. $use_syms = $this->CI->settings_lib->item('auth.password_force_symbols');
  262. $use_mixed = $this->CI->settings_lib->item('auth.password_force_mixed_case');
  263. // Check length
  264. if (strlen($str) < $min_length) {
  265. $this->CI->form_validation->set_message('valid_password', str_replace('{min_length}', $min_length, lang('bf_form_valid_password')));
  266. return false;
  267. }
  268. // Check numbers
  269. if ($use_nums && 0 === preg_match('/[0-9]/', $str)) {
  270. $this->CI->form_validation->set_message('valid_password', lang('bf_form_valid_password_nums'));
  271. return false;
  272. }
  273. // Check symbols
  274. if ($use_syms && 0 === preg_match('/[!@#$%^&*()._]/', $str)) {
  275. $this->CI->form_validation->set_message('valid_password', lang('bf_form_valid_password_syms'));
  276. return false;
  277. }
  278. // Mixed Case?
  279. if ($use_mixed) {
  280. if (0 === preg_match('/[A-Z]/', $str)) {
  281. $this->CI->form_validation->set_message('valid_password', lang('bf_form_valid_password_mixed_1'));
  282. return false;
  283. }
  284. if (0 === preg_match('/[a-z]/', $str)) {
  285. $this->CI->form_validation->set_message('valid_password', lang('bf_form_valid_password_mixed_2'));
  286. return false;
  287. }
  288. }
  289. return true;
  290. }
  291. }