blob: f23378ef38d97d4327ef95193a0d5ffb7b7b1b43 [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
Andrey Andreevb998f3e2012-01-24 15:31:26 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * 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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Form Validation Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
36 */
37class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020038
Phil Sturgeon3837ae72011-05-09 21:12:26 +010039 protected $CI;
40 protected $_field_data = array();
41 protected $_config_rules = array();
42 protected $_error_array = array();
43 protected $_error_messages = array();
44 protected $_error_prefix = '<p>';
45 protected $_error_suffix = '</p>';
46 protected $error_string = '';
47 protected $_safe_form_data = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000048
Greg Akera9263282010-11-10 15:26:43 -060049 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +020050 {
Derek Allard2067d1a2008-11-13 22:59:24 +000051 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020052
Derek Allard2067d1a2008-11-13 22:59:24 +000053 // Validation rules can be stored in a config file.
54 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 // Automatically load the form helper
57 $this->CI->load->helper('form');
58
59 // Set the character encoding in MB.
60 if (function_exists('mb_internal_encoding'))
61 {
62 mb_internal_encoding($this->CI->config->item('charset'));
63 }
Barry Mienydd671972010-10-04 16:33:58 +020064
Andrey Andreev901573c2012-01-11 01:40:48 +020065 log_message('debug', 'Form Validation Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000066 }
Barry Mienydd671972010-10-04 16:33:58 +020067
Derek Allard2067d1a2008-11-13 22:59:24 +000068 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 /**
71 * Set Rules
72 *
73 * This function takes an array of field names and validation
74 * rules as input, validates the info, and stores it
75 *
Derek Allard2067d1a2008-11-13 22:59:24 +000076 * @param mixed
77 * @param string
78 * @return void
79 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010080 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 // No reason to set rules if we have no POST data
Andrey Andreev8dc532d2011-12-24 17:57:54 +020083 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
Greg Aker9f9af602010-11-10 15:41:51 -060085 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +000086 }
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 // If an array was passed via the first parameter instead of indidual string
89 // values we cycle through it and recursively call this function.
90 if (is_array($field))
91 {
92 foreach ($field as $row)
93 {
94 // Houston, we have a problem...
95 if ( ! isset($row['field']) OR ! isset($row['rules']))
96 {
97 continue;
98 }
99
100 // If the field label wasn't passed we use the field name
101 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
102
103 // Here we go!
104 $this->set_rules($row['field'], $label, $row['rules']);
105 }
Greg Aker9f9af602010-11-10 15:41:51 -0600106 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200110 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Greg Aker9f9af602010-11-10 15:41:51 -0600112 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114
115 // If the field label wasn't passed we use the field name
116 $label = ($label == '') ? $field : $label;
117
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200118 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200119 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200120 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200121 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // Note: Due to a bug in current() that affects some versions
123 // of PHP we can not pass function call directly into it
124 $x = explode('[', $field);
125 $indexes[] = current($x);
126
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200127 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200129 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200131 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 $is_array = TRUE;
136 }
137 else
138 {
Barry Mienydd671972010-10-04 16:33:58 +0200139 $indexes = array();
140 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
143 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000145 'field' => $field,
146 'label' => $label,
147 'rules' => $rules,
148 'is_array' => $is_array,
149 'keys' => $indexes,
150 'postdata' => NULL,
151 'error' => ''
152 );
Greg Aker9f9af602010-11-10 15:41:51 -0600153
154 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156
157 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 /**
160 * Set Error Message
161 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500162 * Lets users set their own error messages on the fly. Note: The key
163 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 * @param string
166 * @param string
167 * @return string
168 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100169 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 if ( ! is_array($lang))
172 {
173 $lang = array($lang => $val);
174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600177 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 /**
183 * Set The Error Delimiter
184 *
185 * Permits a prefix/suffix to be added to each error message
186 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @param string
188 * @param string
189 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200190 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100191 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
193 $this->_error_prefix = $prefix;
194 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600195 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
197
198 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 /**
201 * Get Error Message
202 *
203 * Gets the error message associated with a particular field
204 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 * @param string the field name
206 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200207 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100208 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200209 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
211 {
212 return '';
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 if ($prefix == '')
216 {
217 $prefix = $this->_error_prefix;
218 }
219
220 if ($suffix == '')
221 {
222 $suffix = $this->_error_suffix;
223 }
224
225 return $prefix.$this->_field_data[$field]['error'].$suffix;
226 }
227
228 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100231 * Get Array of Error Messages
232 *
233 * Returns the error messages as an array
234 *
235 * @return array
236 */
237 public function error_array()
238 {
239 return $this->_error_array;
240 }
241
242 // --------------------------------------------------------------------
243
244 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 * Error String
246 *
247 * Returns the error messages as a string, wrapped in the error delimiters
248 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 * @param string
250 * @param string
251 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200252 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100253 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 // No errrors, validation passes!
256 if (count($this->_error_array) === 0)
257 {
258 return '';
259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 if ($prefix == '')
262 {
263 $prefix = $this->_error_prefix;
264 }
265
266 if ($suffix == '')
267 {
268 $suffix = $this->_error_suffix;
269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // Generate the error string
272 $str = '';
273 foreach ($this->_error_array as $val)
274 {
275 if ($val != '')
276 {
277 $str .= $prefix.$val.$suffix."\n";
278 }
279 }
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 return $str;
282 }
283
284 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 /**
287 * Run the Validator
288 *
289 * This function does all the work.
290 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200292 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100293 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500295 // Do we even have any data to process? Mm?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200296 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
298 return FALSE;
299 }
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // Does the _field_data array containing the validation rules exist?
302 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200303 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500305 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200306 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
308 return FALSE;
309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // Is there a validation rule for the particular URI being accessed?
312 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200313
Andrey Andreev6de924c2012-01-20 13:18:18 +0200314 if ($uri != '' && isset($this->_config_rules[$uri]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
316 $this->set_rules($this->_config_rules[$uri]);
317 }
318 else
319 {
320 $this->set_rules($this->_config_rules);
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Andrey Andreev901573c2012-01-11 01:40:48 +0200323 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200324 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200326 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 return FALSE;
328 }
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // Load the language file containing error messages
332 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200333
334 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // corresponding $_POST item and test for errors
336 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200337 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
339 // Depending on whether the field name is an array or a string will determine where we get it from.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200340 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
343 }
Andrey Andreev6de924c2012-01-20 13:18:18 +0200344 elseif (isset($_POST[$field]) && $_POST[$field] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200346 $this->_field_data[$field]['postdata'] = $_POST[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
349 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
351
352 // Did we end up with any errors?
353 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 if ($total_errors > 0)
355 {
356 $this->_safe_form_data = TRUE;
357 }
358
359 // Now we need to re-set the POST data with the new, processed data
360 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200361
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200362 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
364
365 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 /**
368 * Traverse a multidimensional $_POST array index until the data is found
369 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * @param array
371 * @param array
372 * @param integer
373 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200374 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100375 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200377 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200379 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
Barry Mienydd671972010-10-04 16:33:58 +0200381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 return $array;
383 }
384
385 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 /**
388 * Re-populate the _POST array with our finalized and processed data
389 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200390 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200391 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100392 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
394 foreach ($this->_field_data as $field => $row)
395 {
396 if ( ! is_null($row['postdata']))
397 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200398 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 if (isset($_POST[$row['field']]))
401 {
402 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
403 }
404 }
405 else
406 {
Derek Jones63eeae32009-02-10 19:08:56 +0000407 // start with a reference
408 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Jones63eeae32009-02-10 19:08:56 +0000410 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200411 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Derek Jones63eeae32009-02-10 19:08:56 +0000413 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
415 else
416 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 foreach ($row['keys'] as $val)
418 {
Derek Jones63eeae32009-02-10 19:08:56 +0000419 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
421 }
Derek Jones63eeae32009-02-10 19:08:56 +0000422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000424 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 $array = array();
426 foreach ($row['postdata'] as $k => $v)
427 {
428 $array[$k] = $this->prep_for_form($v);
429 }
Derek Jones63eeae32009-02-10 19:08:56 +0000430
431 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 }
433 else
Derek Jones63eeae32009-02-10 19:08:56 +0000434 {
435 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438 }
439 }
440 }
441
442 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 /**
445 * Executes the Validation routines
446 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @param array
448 * @param array
449 * @param mixed
450 * @param integer
451 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200452 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100453 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
455 // If the $_POST data is an array we will run a recursive call
456 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200457 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 foreach ($postdata as $key => $val)
459 {
460 $this->_execute($row, $rules, $val, $cycles);
461 $cycles++;
462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 return;
465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 // If the field is blank, but NOT required, no further tests are necessary
468 $callback = FALSE;
Andrey Andreev6de924c2012-01-20 13:18:18 +0200469 if ( ! in_array('required', $rules) && is_null($postdata))
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
471 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200472 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
474 $callback = TRUE;
475 $rules = (array('1' => $match[1]));
476 }
477 else
478 {
479 return;
480 }
481 }
482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev6de924c2012-01-20 13:18:18 +0200484 if (is_null($postdata) && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
486 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
487 {
488 // Set the message type
489 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 if ( ! isset($this->_error_messages[$type]))
492 {
493 if (FALSE === ($line = $this->CI->lang->line($type)))
494 {
495 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200496 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
498 else
499 {
500 $line = $this->_error_messages[$type];
501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 // Build the error message
504 $message = sprintf($line, $this->_translate_fieldname($row['label']));
505
506 // Save the error message
507 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 if ( ! isset($this->_error_array[$row['field']]))
510 {
511 $this->_error_array[$row['field']] = $message;
512 }
513 }
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 return;
516 }
517
518 // --------------------------------------------------------------------
519
520 // Cycle through each rule and run it
521 foreach ($rules As $rule)
522 {
523 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 // We set the $postdata variable with the current data in our master array so that
526 // each cycle of the loop is dealing with the processed data from the last cycle
Andrey Andreev6de924c2012-01-20 13:18:18 +0200527 if ($row['is_array'] == TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
529 // We shouldn't need this safety, but just in case there isn't an array index
530 // associated with this cycle we'll bail out
531 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
532 {
533 continue;
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
537 $_in_array = TRUE;
538 }
539 else
540 {
541 $postdata = $this->_field_data[$row['field']]['postdata'];
542 }
543
Barry Mienydd671972010-10-04 16:33:58 +0200544 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200546 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 {
548 $rule = substr($rule, 9);
549 $callback = TRUE;
550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 // Strip the parameter (if exists) from the rule
553 // Rules can contain a parameter: max_length[5]
554 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200555 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
557 $rule = $match[1];
558 $param = $match[2];
559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 // Call the function that corresponds to the rule
562 if ($callback === TRUE)
563 {
564 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200565 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200566 log_message('debug', 'Unable to find callback validation rule: '.$rule);
567 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200569 else
570 {
571 // Run the function and grab the result
572 $result = $this->CI->$rule($postdata, $param);
573 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000574
575 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200576 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
578 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
579 }
580 else
581 {
582 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200586 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000588 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 }
590 }
591 else
Barry Mienydd671972010-10-04 16:33:58 +0200592 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 if ( ! method_exists($this, $rule))
594 {
Barry Mienydd671972010-10-04 16:33:58 +0200595 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 // Users can use any native PHP function call that has one param.
597 if (function_exists($rule))
598 {
Andrey Andreevcde43682012-01-13 20:16:35 +0200599 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200600
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 }
609 }
patwork02404a12011-04-08 15:45:46 +0200610 else
611 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200612 log_message('debug', 'Unable to find validation rule: '.$rule);
613 $result = FALSE;
patwork02404a12011-04-08 15:45:46 +0200614 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200616 else
617 {
618 $result = $this->$rule($postdata, $param);
619 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000620
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200621 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
623 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
624 }
625 else
626 {
627 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
628 }
629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Andrey Andreev901573c2012-01-11 01:40:48 +0200631 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200633 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 if ( ! isset($this->_error_messages[$rule]))
635 {
636 if (FALSE === ($line = $this->CI->lang->line($rule)))
637 {
638 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200639 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 }
641 else
642 {
643 $line = $this->_error_messages[$rule];
644 }
Barry Mienydd671972010-10-04 16:33:58 +0200645
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200647 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200648 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
Pascal Krietec1895832009-10-13 12:56:43 +0000650 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // Build the error message
654 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
655
656 // Save the error message
657 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 if ( ! isset($this->_error_array[$row['field']]))
660 {
661 $this->_error_array[$row['field']] = $message;
662 }
Barry Mienydd671972010-10-04 16:33:58 +0200663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 return;
665 }
666 }
667 }
668
669 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200670
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 /**
672 * Translate a field name
673 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 * @param string the field name
675 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200676 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100677 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
679 // Do we need to translate the field name?
680 // We look for the prefix lang: to determine this
Andrey Andreev901573c2012-01-11 01:40:48 +0200681 if (strpos($fieldname, 'lang:') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
683 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200684 $line = substr($fieldname, 5);
685
Derek Jones37f4b9c2011-07-01 17:56:50 -0500686 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
688 {
689 return $line;
690 }
691 }
692
693 return $fieldname;
694 }
695
696 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200697
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 /**
699 * Get the value from a form
700 *
701 * Permits you to repopulate a form field with the value it was submitted
702 * with, or, if that value doesn't exist, with the default
703 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 * @param string the field name
705 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200706 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200707 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100708 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200710 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 {
712 return $default;
713 }
Barry Mienydd671972010-10-04 16:33:58 +0200714
Phil Sturgeon5c561802011-01-05 16:31:59 +0000715 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600716 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000717 if (is_array($this->_field_data[$field]['postdata']))
718 {
719 return array_shift($this->_field_data[$field]['postdata']);
720 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000721
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 return $this->_field_data[$field]['postdata'];
723 }
Barry Mienydd671972010-10-04 16:33:58 +0200724
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200726
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 /**
728 * Set Select
729 *
730 * Enables pull-down lists to be set to the value the user
731 * selected in the event of an error
732 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 * @param string
734 * @param string
735 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200736 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100737 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200738 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200739 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200741 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
Barry Mienydd671972010-10-04 16:33:58 +0200743
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 if (is_array($field))
746 {
747 if ( ! in_array($value, $field))
748 {
749 return '';
750 }
751 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200752 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200754 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 }
Barry Mienydd671972010-10-04 16:33:58 +0200756
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 return ' selected="selected"';
758 }
Barry Mienydd671972010-10-04 16:33:58 +0200759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 /**
763 * Set Radio
764 *
765 * Enables radio buttons to be set to the value the user
766 * selected in the event of an error
767 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 * @param string
769 * @param string
770 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200771 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100772 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200774 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200776 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 }
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 if (is_array($field))
781 {
782 if ( ! in_array($value, $field))
783 {
784 return '';
785 }
786 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200787 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200789 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 }
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 return ' checked="checked"';
793 }
Barry Mienydd671972010-10-04 16:33:58 +0200794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 /**
798 * Set Checkbox
799 *
800 * Enables checkboxes to be set to the value the user
801 * selected in the event of an error
802 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 * @param string
804 * @param string
805 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200806 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100807 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200809 // Logic is exactly the same as for radio fields
810 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 }
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 /**
816 * Required
817 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 * @param string
819 * @return bool
820 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100821 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200823 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 }
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500829 * Performs a Regular Expression match test.
830 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500831 * @param string
832 * @param regex
833 * @return bool
834 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100835 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500836 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200837 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500838 }
839
840 // --------------------------------------------------------------------
841
842 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 * Match one field to another
844 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 * @param string
846 * @param field
847 * @return bool
848 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100849 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 {
851 if ( ! isset($_POST[$field]))
852 {
Barry Mienydd671972010-10-04 16:33:58 +0200853 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 }
Barry Mienydd671972010-10-04 16:33:58 +0200855
Andrey Andreev901573c2012-01-11 01:40:48 +0200856 return ($str === $_POST[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 }
Eric Barnescccde962011-12-04 00:01:17 -0500858
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100859 // --------------------------------------------------------------------
860
861 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200862 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100863 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200864 * Check if the input value doesn't already exist
865 * in the specified database field.
866 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100867 * @param string
868 * @param field
869 * @return bool
870 */
871 public function is_unique($str, $field)
872 {
Eric Barnescccde962011-12-04 00:01:17 -0500873 list($table, $field) = explode('.', $field);
874 if (isset($this->CI->db))
875 {
876 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
877 return $query->num_rows() === 0;
878 }
879 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600880 }
Barry Mienydd671972010-10-04 16:33:58 +0200881
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 /**
885 * Minimum Length
886 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 * @param string
888 * @param value
889 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200890 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100891 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200893 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
895 return FALSE;
896 }
897
898 if (function_exists('mb_strlen'))
899 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200900 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 }
Barry Mienydd671972010-10-04 16:33:58 +0200902
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200903 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 }
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200907
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 /**
909 * Max Length
910 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 * @param string
912 * @param value
913 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200914 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100915 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200917 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 {
919 return FALSE;
920 }
921
922 if (function_exists('mb_strlen'))
923 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200924 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 }
Barry Mienydd671972010-10-04 16:33:58 +0200926
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200927 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 }
Barry Mienydd671972010-10-04 16:33:58 +0200929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200931
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 /**
933 * Exact Length
934 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 * @param string
936 * @param value
937 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200938 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100939 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200941 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 {
943 return FALSE;
944 }
945
946 if (function_exists('mb_strlen'))
947 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200948 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 }
Barry Mienydd671972010-10-04 16:33:58 +0200950
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200951 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 }
Barry Mienydd671972010-10-04 16:33:58 +0200953
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200955
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 /**
957 * Valid Email
958 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 * @param string
960 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200961 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100962 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200964 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 +0000965 }
966
967 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200968
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 /**
970 * Valid Emails
971 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 * @param string
973 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200974 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100975 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
977 if (strpos($str, ',') === FALSE)
978 {
979 return $this->valid_email(trim($str));
980 }
Barry Mienydd671972010-10-04 16:33:58 +0200981
Pascal Kriete14287f32011-02-14 13:39:34 -0500982 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200984 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
986 return FALSE;
987 }
988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 return TRUE;
991 }
992
993 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200994
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 /**
996 * Validate IP Address
997 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +0800999 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001001 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 {
1003 return $this->CI->input->valid_ip($ip);
1004 }
1005
1006 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001007
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 /**
1009 * Alpha
1010 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 * @param string
1012 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001013 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001014 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001016 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 }
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001020
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 /**
1022 * Alpha-numeric
1023 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 * @param string
1025 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001026 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001027 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001029 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 }
Barry Mienydd671972010-10-04 16:33:58 +02001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 /**
1035 * Alpha-numeric with underscores and dashes
1036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @param string
1038 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001039 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001040 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001042 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 }
Barry Mienydd671972010-10-04 16:33:58 +02001044
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001046
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 /**
1048 * Numeric
1049 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 * @param string
1051 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001052 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001053 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001055 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001056
1057 }
1058
1059 // --------------------------------------------------------------------
1060
Barry Mienydd671972010-10-04 16:33:58 +02001061 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 * Integer
1063 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 * @param string
1065 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001066 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001067 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001069 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1070 }
1071
1072 // --------------------------------------------------------------------
1073
1074 /**
1075 * Decimal number
1076 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001077 * @param string
1078 * @return bool
1079 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001080 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001081 {
1082 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1083 }
1084
1085 // --------------------------------------------------------------------
1086
1087 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001088 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001089 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001090 * @param string
1091 * @return bool
1092 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001093 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001094 {
1095 if ( ! is_numeric($str))
1096 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001097 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001098 }
1099 return $str > $min;
1100 }
1101
1102 // --------------------------------------------------------------------
1103
1104 /**
1105 * Less than
1106 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001107 * @param string
1108 * @return bool
1109 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001110 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001111 {
1112 if ( ! is_numeric($str))
1113 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001114 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001115 }
1116 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001118
1119 // --------------------------------------------------------------------
1120
Barry Mienydd671972010-10-04 16:33:58 +02001121 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001122 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001123 *
Barry Mienydd671972010-10-04 16:33:58 +02001124 * @param string
1125 * @return bool
1126 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001127 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001128 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001129 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001130 }
1131
1132 // --------------------------------------------------------------------
1133
1134 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001135 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001136 *
Barry Mienydd671972010-10-04 16:33:58 +02001137 * @param string
1138 * @return bool
1139 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001140 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001141 {
Andrey Andreev6de924c2012-01-20 13:18:18 +02001142 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001143 }
1144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 /**
1148 * Valid Base64
1149 *
1150 * Tests a string for characters outside of the Base64 alphabet
1151 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1152 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 * @param string
1154 * @return bool
1155 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001156 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 {
1158 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1159 }
Barry Mienydd671972010-10-04 16:33:58 +02001160
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001162
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 /**
1164 * Prep data for form
1165 *
1166 * This function allows HTML to be safely shown in a form.
1167 * Special characters are converted.
1168 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 * @param string
1170 * @return string
1171 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001172 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 {
1174 if (is_array($data))
1175 {
1176 foreach ($data as $key => $val)
1177 {
1178 $data[$key] = $this->prep_for_form($val);
1179 }
Barry Mienydd671972010-10-04 16:33:58 +02001180
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 return $data;
1182 }
Barry Mienydd671972010-10-04 16:33:58 +02001183
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 if ($this->_safe_form_data == FALSE OR $data === '')
1185 {
1186 return $data;
1187 }
1188
Andrey Andreev901573c2012-01-11 01:40:48 +02001189 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 }
Barry Mienydd671972010-10-04 16:33:58 +02001191
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001193
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 /**
1195 * Prep URL
1196 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 * @param string
1198 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001199 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001200 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001202 if ($str === 'http://' OR $str == '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 {
1204 return '';
1205 }
Barry Mienydd671972010-10-04 16:33:58 +02001206
Andrey Andreev901573c2012-01-11 01:40:48 +02001207 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001209 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 }
Barry Mienydd671972010-10-04 16:33:58 +02001211
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 return $str;
1213 }
Barry Mienydd671972010-10-04 16:33:58 +02001214
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001216
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 /**
1218 * Strip Image Tags
1219 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 * @param string
1221 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001222 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001223 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 {
1225 return $this->CI->input->strip_image_tags($str);
1226 }
Barry Mienydd671972010-10-04 16:33:58 +02001227
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001229
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 /**
1231 * XSS Clean
1232 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 * @param string
1234 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001235 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001236 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 {
Derek Jones5640a712010-04-23 11:22:40 -05001238 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 }
Barry Mienydd671972010-10-04 16:33:58 +02001240
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001242
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 /**
1244 * Convert PHP tags to entities
1245 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 * @param string
1247 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001248 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001249 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001251 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 }
1253
1254}
Derek Allard2067d1a2008-11-13 22:59:24 +00001255
1256/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001257/* Location: ./system/libraries/Form_validation.php */