blob: 93ec8b34a02fd5f7e9dd3823938781e8749b8b60 [file] [log] [blame]
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnescccde962011-12-04 00:01:17 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnescccde962011-12-04 00:01:17 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Form Validation Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
38 */
39class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020040
Phil Sturgeon3837ae72011-05-09 21:12:26 +010041 protected $CI;
42 protected $_field_data = array();
43 protected $_config_rules = array();
44 protected $_error_array = array();
45 protected $_error_messages = array();
46 protected $_error_prefix = '<p>';
47 protected $_error_suffix = '</p>';
48 protected $error_string = '';
49 protected $_safe_form_data = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000050
51 /**
52 * Constructor
Barry Mienydd671972010-10-04 16:33:58 +020053 */
Greg Akera9263282010-11-10 15:26:43 -060054 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +020055 {
Derek Allard2067d1a2008-11-13 22:59:24 +000056 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020057
Mike Funk326a5e72012-02-24 10:06:28 -050058 // applies delimiters set in config file.
59 $this->_config_delimiters();
60
Derek Allard2067d1a2008-11-13 22:59:24 +000061 // Validation rules can be stored in a config file.
62 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 // Automatically load the form helper
65 $this->CI->load->helper('form');
66
67 // Set the character encoding in MB.
68 if (function_exists('mb_internal_encoding'))
69 {
70 mb_internal_encoding($this->CI->config->item('charset'));
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 log_message('debug', "Form Validation Class Initialized");
74 }
Mike Funk326a5e72012-02-24 10:06:28 -050075
76 // --------------------------------------------------------------------
77
78 /**
79 * if prefixes/suffixes set in config, assign and unset.
80 *
81 * @return void
82 */
83 private function _config_delimiters()
84 {
85 if (isset($rules['error_prefix']))
86 {
87 $this->_error_prefix = $rules['error_prefix']);
88 unset $rules['error_prefix']);
89 }
90 if (isset($rules['error_suffix']))
91 {
92 $this->_error_suffix = $rules['error_suffix']);
93 unset $rules['error_suffix']);
94 }
95 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Allard2067d1a2008-11-13 22:59:24 +000099 /**
100 * Set Rules
101 *
102 * This function takes an array of field names and validation
103 * rules as input, validates the info, and stores it
104 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 * @param mixed
106 * @param string
107 * @return void
108 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100109 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
111 // No reason to set rules if we have no POST data
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200112 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
Greg Aker9f9af602010-11-10 15:41:51 -0600114 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 }
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 // If an array was passed via the first parameter instead of indidual string
118 // values we cycle through it and recursively call this function.
119 if (is_array($field))
120 {
121 foreach ($field as $row)
122 {
123 // Houston, we have a problem...
124 if ( ! isset($row['field']) OR ! isset($row['rules']))
125 {
126 continue;
127 }
128
129 // If the field label wasn't passed we use the field name
130 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
131
132 // Here we go!
133 $this->set_rules($row['field'], $label, $row['rules']);
134 }
Greg Aker9f9af602010-11-10 15:41:51 -0600135 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200139 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Greg Aker9f9af602010-11-10 15:41:51 -0600141 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
143
144 // If the field label wasn't passed we use the field name
145 $label = ($label == '') ? $field : $label;
146
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200147 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200148 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200149 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200150 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 // Note: Due to a bug in current() that affects some versions
152 // of PHP we can not pass function call directly into it
153 $x = explode('[', $field);
154 $indexes[] = current($x);
155
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200156 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200158 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200160 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 $is_array = TRUE;
165 }
166 else
167 {
Barry Mienydd671972010-10-04 16:33:58 +0200168 $indexes = array();
169 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
172 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000174 'field' => $field,
175 'label' => $label,
176 'rules' => $rules,
177 'is_array' => $is_array,
178 'keys' => $indexes,
179 'postdata' => NULL,
180 'error' => ''
181 );
Greg Aker9f9af602010-11-10 15:41:51 -0600182
183 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185
186 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 /**
189 * Set Error Message
190 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500191 * Lets users set their own error messages on the fly. Note: The key
192 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @param string
195 * @param string
196 * @return string
197 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100198 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
200 if ( ! is_array($lang))
201 {
202 $lang = array($lang => $val);
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 $this->_error_messages = array_merge($this->_error_messages, $lang);
Phil Sturgeonc3828712011-01-19 12:31:47 +0000206
Greg Aker9f9af602010-11-10 15:41:51 -0600207 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 /**
213 * Set The Error Delimiter
214 *
215 * Permits a prefix/suffix to be added to each error message
216 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 * @param string
218 * @param string
219 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200220 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100221 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 $this->_error_prefix = $prefix;
224 $this->_error_suffix = $suffix;
Phil Sturgeonc3828712011-01-19 12:31:47 +0000225
Greg Aker9f9af602010-11-10 15:41:51 -0600226 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
228
229 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 /**
232 * Get Error Message
233 *
234 * Gets the error message associated with a particular field
235 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * @param string the field name
237 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200238 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100239 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200240 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
242 {
243 return '';
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 if ($prefix == '')
247 {
248 $prefix = $this->_error_prefix;
249 }
250
251 if ($suffix == '')
252 {
253 $suffix = $this->_error_suffix;
254 }
255
256 return $prefix.$this->_field_data[$field]['error'].$suffix;
257 }
258
259 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 /**
262 * Error String
263 *
264 * Returns the error messages as a string, wrapped in the error delimiters
265 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 * @param string
267 * @param string
268 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200269 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100270 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
272 // No errrors, validation passes!
273 if (count($this->_error_array) === 0)
274 {
275 return '';
276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 if ($prefix == '')
279 {
280 $prefix = $this->_error_prefix;
281 }
282
283 if ($suffix == '')
284 {
285 $suffix = $this->_error_suffix;
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 // Generate the error string
289 $str = '';
290 foreach ($this->_error_array as $val)
291 {
292 if ($val != '')
293 {
294 $str .= $prefix.$val.$suffix."\n";
295 }
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 return $str;
299 }
300
301 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 /**
304 * Run the Validator
305 *
306 * This function does all the work.
307 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200309 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100310 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500312 // Do we even have any data to process? Mm?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200313 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
315 return FALSE;
316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // Does the _field_data array containing the validation rules exist?
319 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200320 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500322 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200323 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
325 return FALSE;
326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // Is there a validation rule for the particular URI being accessed?
329 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 if ($uri != '' AND isset($this->_config_rules[$uri]))
332 {
333 $this->set_rules($this->_config_rules[$uri]);
334 }
335 else
336 {
337 $this->set_rules($this->_config_rules);
338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 // We're we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200341 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
343 log_message('debug', "Unable to find validation rules");
344 return FALSE;
345 }
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // Load the language file containing error messages
349 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200350
351 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 // corresponding $_POST item and test for errors
353 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200354 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
356 // Depending on whether the field name is an array or a string will determine where we get it from.
Barry Mienydd671972010-10-04 16:33:58 +0200357
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200358 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
361 }
362 else
363 {
364 if (isset($_POST[$field]) AND $_POST[$field] != "")
365 {
366 $this->_field_data[$field]['postdata'] = $_POST[$field];
367 }
368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
370 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
372
373 // Did we end up with any errors?
374 $total_errors = count($this->_error_array);
375
376 if ($total_errors > 0)
377 {
378 $this->_safe_form_data = TRUE;
379 }
380
381 // Now we need to re-set the POST data with the new, processed data
382 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200383
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200384 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
386
387 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 /**
390 * Traverse a multidimensional $_POST array index until the data is found
391 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 * @param array
393 * @param array
394 * @param integer
395 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200396 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100397 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200399 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200401 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 return $array;
405 }
406
407 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 /**
410 * Re-populate the _POST array with our finalized and processed data
411 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200413 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100414 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
416 foreach ($this->_field_data as $field => $row)
417 {
418 if ( ! is_null($row['postdata']))
419 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200420 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 if (isset($_POST[$row['field']]))
423 {
424 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
425 }
426 }
427 else
428 {
Derek Jones63eeae32009-02-10 19:08:56 +0000429 // start with a reference
430 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Jones63eeae32009-02-10 19:08:56 +0000432 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200433 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Derek Jones63eeae32009-02-10 19:08:56 +0000435 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
437 else
438 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 foreach ($row['keys'] as $val)
440 {
Derek Jones63eeae32009-02-10 19:08:56 +0000441 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
443 }
Derek Jones63eeae32009-02-10 19:08:56 +0000444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000446 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 $array = array();
448 foreach ($row['postdata'] as $k => $v)
449 {
450 $array[$k] = $this->prep_for_form($v);
451 }
Derek Jones63eeae32009-02-10 19:08:56 +0000452
453 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455 else
Derek Jones63eeae32009-02-10 19:08:56 +0000456 {
457 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
460 }
461 }
462 }
463
464 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 /**
467 * Executes the Validation routines
468 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 * @param array
470 * @param array
471 * @param mixed
472 * @param integer
473 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200474 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100475 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
477 // If the $_POST data is an array we will run a recursive call
478 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200479 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 foreach ($postdata as $key => $val)
481 {
482 $this->_execute($row, $rules, $val, $cycles);
483 $cycles++;
484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 return;
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 // --------------------------------------------------------------------
490
491 // If the field is blank, but NOT required, no further tests are necessary
492 $callback = FALSE;
493 if ( ! in_array('required', $rules) AND is_null($postdata))
494 {
495 // Before we bail out, does the rule contain a callback?
Marcos Coelhoc28b2852011-07-05 12:59:41 -0700496 if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
498 $callback = TRUE;
499 $rules = (array('1' => $match[1]));
500 }
501 else
502 {
503 return;
504 }
505 }
506
507 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200510 if (is_null($postdata) AND $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
512 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
513 {
514 // Set the message type
515 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 if ( ! isset($this->_error_messages[$type]))
518 {
519 if (FALSE === ($line = $this->CI->lang->line($type)))
520 {
521 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200522 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
524 else
525 {
526 $line = $this->_error_messages[$type];
527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 // Build the error message
530 $message = sprintf($line, $this->_translate_fieldname($row['label']));
531
532 // Save the error message
533 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 if ( ! isset($this->_error_array[$row['field']]))
536 {
537 $this->_error_array[$row['field']] = $message;
538 }
539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 return;
542 }
543
544 // --------------------------------------------------------------------
545
546 // Cycle through each rule and run it
547 foreach ($rules As $rule)
548 {
549 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 // We set the $postdata variable with the current data in our master array so that
552 // each cycle of the loop is dealing with the processed data from the last cycle
553 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
554 {
555 // We shouldn't need this safety, but just in case there isn't an array index
556 // associated with this cycle we'll bail out
557 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
558 {
559 continue;
560 }
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
563 $_in_array = TRUE;
564 }
565 else
566 {
567 $postdata = $this->_field_data[$row['field']]['postdata'];
568 }
569
570 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200571
572 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 $callback = FALSE;
574 if (substr($rule, 0, 9) == 'callback_')
575 {
576 $rule = substr($rule, 9);
577 $callback = TRUE;
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // Strip the parameter (if exists) from the rule
581 // Rules can contain a parameter: max_length[5]
582 $param = FALSE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500583 if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 $rule = $match[1];
586 $param = $match[2];
587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 // Call the function that corresponds to the rule
590 if ($callback === TRUE)
591 {
592 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200593 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 continue;
595 }
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 // Run the function and grab the result
598 $result = $this->CI->$rule($postdata, $param);
599
600 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200601 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 {
603 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
604 }
605 else
606 {
607 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
608 }
Barry Mienydd671972010-10-04 16:33:58 +0200609
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 // If the field isn't required and we just processed a callback we'll move on...
611 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
612 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000613 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
615 }
616 else
Barry Mienydd671972010-10-04 16:33:58 +0200617 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 if ( ! method_exists($this, $rule))
619 {
Barry Mienydd671972010-10-04 16:33:58 +0200620 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // Users can use any native PHP function call that has one param.
622 if (function_exists($rule))
623 {
624 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200625
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200626 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
628 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
629 }
630 else
631 {
632 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
633 }
634 }
patwork02404a12011-04-08 15:45:46 +0200635 else
636 {
637 log_message('debug', "Unable to find validation rule: ".$rule);
638 }
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 continue;
641 }
642
643 $result = $this->$rule($postdata, $param);
644
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200645 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
647 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
648 }
649 else
650 {
651 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
652 }
653 }
Barry Mienydd671972010-10-04 16:33:58 +0200654
Derek Jones37f4b9c2011-07-01 17:56:50 -0500655 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200657 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 if ( ! isset($this->_error_messages[$rule]))
659 {
660 if (FALSE === ($line = $this->CI->lang->line($rule)))
661 {
662 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200663 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
665 else
666 {
667 $line = $this->_error_messages[$rule];
668 }
Barry Mienydd671972010-10-04 16:33:58 +0200669
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 // Is the parameter we are inserting into the error message the name
Derek Jones37f4b9c2011-07-01 17:56:50 -0500671 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200672 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 {
Pascal Krietec1895832009-10-13 12:56:43 +0000674 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 }
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 // Build the error message
678 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
679
680 // Save the error message
681 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 if ( ! isset($this->_error_array[$row['field']]))
684 {
685 $this->_error_array[$row['field']] = $message;
686 }
Barry Mienydd671972010-10-04 16:33:58 +0200687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 return;
689 }
690 }
691 }
692
693 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 /**
696 * Translate a field name
697 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 * @param string the field name
699 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200700 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100701 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
703 // Do we need to translate the field name?
704 // We look for the prefix lang: to determine this
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200705 if (substr($fieldname, 0, 5) === 'lang:')
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 {
707 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200708 $line = substr($fieldname, 5);
709
Derek Jones37f4b9c2011-07-01 17:56:50 -0500710 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
712 {
713 return $line;
714 }
715 }
716
717 return $fieldname;
718 }
719
720 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200721
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 /**
723 * Get the value from a form
724 *
725 * Permits you to repopulate a form field with the value it was submitted
726 * with, or, if that value doesn't exist, with the default
727 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 * @param string the field name
729 * @param string
730 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200731 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100732 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 {
734 if ( ! isset($this->_field_data[$field]))
735 {
736 return $default;
737 }
Barry Mienydd671972010-10-04 16:33:58 +0200738
Phil Sturgeon5c561802011-01-05 16:31:59 +0000739 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600740 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000741 if (is_array($this->_field_data[$field]['postdata']))
742 {
743 return array_shift($this->_field_data[$field]['postdata']);
744 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 return $this->_field_data[$field]['postdata'];
747 }
Barry Mienydd671972010-10-04 16:33:58 +0200748
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200750
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 /**
752 * Set Select
753 *
754 * Enables pull-down lists to be set to the value the user
755 * selected in the event of an error
756 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 * @param string
758 * @param string
759 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200760 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100761 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200762 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
764 {
765 if ($default === TRUE AND count($this->_field_data) === 0)
766 {
767 return ' selected="selected"';
768 }
769 return '';
770 }
Barry Mienydd671972010-10-04 16:33:58 +0200771
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200773
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 if (is_array($field))
775 {
776 if ( ! in_array($value, $field))
777 {
778 return '';
779 }
780 }
781 else
782 {
783 if (($field == '' OR $value == '') OR ($field != $value))
784 {
785 return '';
786 }
787 }
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 return ' selected="selected"';
790 }
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200793
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 /**
795 * Set Radio
796 *
797 * Enables radio buttons to be set to the value the user
798 * selected in the event of an error
799 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 * @param string
801 * @param string
802 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200803 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100804 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 {
806 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
807 {
808 if ($default === TRUE AND count($this->_field_data) === 0)
809 {
810 return ' checked="checked"';
811 }
812 return '';
813 }
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 if (is_array($field))
818 {
819 if ( ! in_array($value, $field))
820 {
821 return '';
822 }
823 }
824 else
825 {
826 if (($field == '' OR $value == '') OR ($field != $value))
827 {
828 return '';
829 }
830 }
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 return ' checked="checked"';
833 }
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 /**
838 * Set Checkbox
839 *
840 * Enables checkboxes to be set to the value the user
841 * selected in the event of an error
842 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 * @param string
844 * @param string
845 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200846 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100847 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200849 // Logic is exactly the same as for radio fields
850 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 }
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200854
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 /**
856 * Required
857 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 * @param string
859 * @return bool
860 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100861 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200863 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 }
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200867
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500869 * Performs a Regular Expression match test.
870 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500871 * @param string
872 * @param regex
873 * @return bool
874 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100875 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500876 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200877 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500878 }
879
880 // --------------------------------------------------------------------
881
882 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 * Match one field to another
884 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 * @param string
886 * @param field
887 * @return bool
888 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100889 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 {
891 if ( ! isset($_POST[$field]))
892 {
Barry Mienydd671972010-10-04 16:33:58 +0200893 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 }
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 $field = $_POST[$field];
897
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200898 return ($str === $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 }
Eric Barnescccde962011-12-04 00:01:17 -0500900
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100901 // --------------------------------------------------------------------
902
903 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200904 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100905 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200906 * Check if the input value doesn't already exist
907 * in the specified database field.
908 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100909 * @param string
910 * @param field
911 * @return bool
912 */
913 public function is_unique($str, $field)
914 {
Eric Barnescccde962011-12-04 00:01:17 -0500915 list($table, $field) = explode('.', $field);
916 if (isset($this->CI->db))
917 {
918 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
919 return $query->num_rows() === 0;
920 }
921 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 /**
927 * Minimum Length
928 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 * @param string
930 * @param value
931 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200932 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100933 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 {
935 if (preg_match("/[^0-9]/", $val))
936 {
937 return FALSE;
938 }
939
940 if (function_exists('mb_strlen'))
941 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200942 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
Barry Mienydd671972010-10-04 16:33:58 +0200944
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200945 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200949
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 /**
951 * Max Length
952 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 * @param string
954 * @param value
955 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200956 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100957 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 {
959 if (preg_match("/[^0-9]/", $val))
960 {
961 return FALSE;
962 }
963
964 if (function_exists('mb_strlen'))
965 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200966 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200969 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 }
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 /**
975 * Exact Length
976 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 * @param string
978 * @param value
979 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200980 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100981 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 {
983 if (preg_match("/[^0-9]/", $val))
984 {
985 return FALSE;
986 }
987
988 if (function_exists('mb_strlen'))
989 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200990 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 }
Barry Mienydd671972010-10-04 16:33:58 +0200992
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200993 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 }
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 /**
999 * Valid Email
1000 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 * @param string
1002 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001003 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001004 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001006 return (bool) preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 }
1008
1009 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 /**
1012 * Valid Emails
1013 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 * @param string
1015 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001016 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001017 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 {
1019 if (strpos($str, ',') === FALSE)
1020 {
1021 return $this->valid_email(trim($str));
1022 }
Barry Mienydd671972010-10-04 16:33:58 +02001023
Pascal Kriete14287f32011-02-14 13:39:34 -05001024 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001026 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
1028 return FALSE;
1029 }
1030 }
Barry Mienydd671972010-10-04 16:33:58 +02001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 return TRUE;
1033 }
1034
1035 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001036
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 /**
1038 * Validate IP Address
1039 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001041 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001043 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 {
1045 return $this->CI->input->valid_ip($ip);
1046 }
1047
1048 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 /**
1051 * Alpha
1052 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 * @param string
1054 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001055 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001056 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001058 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 }
Barry Mienydd671972010-10-04 16:33:58 +02001060
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 /**
1064 * Alpha-numeric
1065 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 * @param string
1067 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001068 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001069 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001071 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 }
Barry Mienydd671972010-10-04 16:33:58 +02001073
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001075
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 /**
1077 * Alpha-numeric with underscores and dashes
1078 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 * @param string
1080 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001081 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001082 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001084 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 }
Barry Mienydd671972010-10-04 16:33:58 +02001086
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 /**
1090 * Numeric
1091 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 * @param string
1093 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001094 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001095 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001097 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001098
1099 }
1100
1101 // --------------------------------------------------------------------
1102
Barry Mienydd671972010-10-04 16:33:58 +02001103 /**
1104 * Is Numeric
1105 *
Barry Mienydd671972010-10-04 16:33:58 +02001106 * @param string
1107 * @return bool
1108 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001109 public function is_numeric($str)
Barry Mienydd671972010-10-04 16:33:58 +02001110 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001111 return is_numeric($str);
Barry Mienydd671972010-10-04 16:33:58 +02001112 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001113
1114 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001115
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 /**
1117 * Integer
1118 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 * @param string
1120 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001121 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001122 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001124 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1125 }
1126
1127 // --------------------------------------------------------------------
1128
1129 /**
1130 * Decimal number
1131 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001132 * @param string
1133 * @return bool
1134 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001135 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001136 {
1137 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1138 }
1139
1140 // --------------------------------------------------------------------
1141
1142 /**
1143 * Greather than
1144 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001145 * @param string
1146 * @return bool
1147 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001148 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001149 {
1150 if ( ! is_numeric($str))
1151 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001152 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001153 }
1154 return $str > $min;
1155 }
1156
1157 // --------------------------------------------------------------------
1158
1159 /**
1160 * Less than
1161 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001162 * @param string
1163 * @return bool
1164 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001165 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001166 {
1167 if ( ! is_numeric($str))
1168 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001169 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001170 }
1171 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001173
1174 // --------------------------------------------------------------------
1175
Barry Mienydd671972010-10-04 16:33:58 +02001176 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001177 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001178 *
Barry Mienydd671972010-10-04 16:33:58 +02001179 * @param string
1180 * @return bool
1181 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001182 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001183 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001184 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001185 }
1186
1187 // --------------------------------------------------------------------
1188
1189 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001190 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001191 *
Barry Mienydd671972010-10-04 16:33:58 +02001192 * @param string
1193 * @return bool
1194 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001195 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001196 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001197 return ($str != 0 AND preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001198 }
1199
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001201
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 /**
1203 * Valid Base64
1204 *
1205 * Tests a string for characters outside of the Base64 alphabet
1206 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1207 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 * @param string
1209 * @return bool
1210 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001211 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 {
1213 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1214 }
Barry Mienydd671972010-10-04 16:33:58 +02001215
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001217
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 /**
1219 * Prep data for form
1220 *
1221 * This function allows HTML to be safely shown in a form.
1222 * Special characters are converted.
1223 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 * @param string
1225 * @return string
1226 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001227 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 {
1229 if (is_array($data))
1230 {
1231 foreach ($data as $key => $val)
1232 {
1233 $data[$key] = $this->prep_for_form($val);
1234 }
Barry Mienydd671972010-10-04 16:33:58 +02001235
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 return $data;
1237 }
Barry Mienydd671972010-10-04 16:33:58 +02001238
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 if ($this->_safe_form_data == FALSE OR $data === '')
1240 {
1241 return $data;
1242 }
1243
1244 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1245 }
Barry Mienydd671972010-10-04 16:33:58 +02001246
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001248
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 /**
1250 * Prep URL
1251 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 * @param string
1253 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001254 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001255 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 {
1257 if ($str == 'http://' OR $str == '')
1258 {
1259 return '';
1260 }
Barry Mienydd671972010-10-04 16:33:58 +02001261
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001262 if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://')
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 {
1264 $str = 'http://'.$str;
1265 }
Barry Mienydd671972010-10-04 16:33:58 +02001266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 return $str;
1268 }
Barry Mienydd671972010-10-04 16:33:58 +02001269
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001271
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 /**
1273 * Strip Image Tags
1274 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 * @param string
1276 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001277 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001278 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
1280 return $this->CI->input->strip_image_tags($str);
1281 }
Barry Mienydd671972010-10-04 16:33:58 +02001282
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001284
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 /**
1286 * XSS Clean
1287 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 * @param string
1289 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001290 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001291 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 {
Derek Jones5640a712010-04-23 11:22:40 -05001293 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 }
Barry Mienydd671972010-10-04 16:33:58 +02001295
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001297
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 /**
1299 * Convert PHP tags to entities
1300 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 * @param string
1302 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001303 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001304 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001306 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 }
1308
1309}
1310// END Form Validation Class
1311
1312/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001313/* Location: ./system/libraries/Form_validation.php */