blob: 5069a44c16d9677665429335b75af5fa2d527b81 [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
Derek Allard2067d1a2008-11-13 22:59:24 +000058 // Validation rules can be stored in a config file.
59 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 // Automatically load the form helper
62 $this->CI->load->helper('form');
63
64 // Set the character encoding in MB.
65 if (function_exists('mb_internal_encoding'))
66 {
67 mb_internal_encoding($this->CI->config->item('charset'));
68 }
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 log_message('debug', "Form Validation Class Initialized");
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075 /**
76 * Set Rules
77 *
78 * This function takes an array of field names and validation
79 * rules as input, validates the info, and stores it
80 *
Derek Allard2067d1a2008-11-13 22:59:24 +000081 * @param mixed
82 * @param string
83 * @return void
84 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010085 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
87 // No reason to set rules if we have no POST data
Andrey Andreev8dc532d2011-12-24 17:57:54 +020088 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
Greg Aker9f9af602010-11-10 15:41:51 -060090 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +000091 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 // If an array was passed via the first parameter instead of indidual string
94 // values we cycle through it and recursively call this function.
95 if (is_array($field))
96 {
97 foreach ($field as $row)
98 {
99 // Houston, we have a problem...
100 if ( ! isset($row['field']) OR ! isset($row['rules']))
101 {
102 continue;
103 }
104
105 // If the field label wasn't passed we use the field name
106 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
107
108 // Here we go!
109 $this->set_rules($row['field'], $label, $row['rules']);
110 }
Greg Aker9f9af602010-11-10 15:41:51 -0600111 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200115 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
Greg Aker9f9af602010-11-10 15:41:51 -0600117 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 }
119
120 // If the field label wasn't passed we use the field name
121 $label = ($label == '') ? $field : $label;
122
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200123 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200124 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200125 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200126 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 // Note: Due to a bug in current() that affects some versions
128 // of PHP we can not pass function call directly into it
129 $x = explode('[', $field);
130 $indexes[] = current($x);
131
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200132 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200134 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200136 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 $is_array = TRUE;
141 }
142 else
143 {
Barry Mienydd671972010-10-04 16:33:58 +0200144 $indexes = array();
145 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
148 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000150 'field' => $field,
151 'label' => $label,
152 'rules' => $rules,
153 'is_array' => $is_array,
154 'keys' => $indexes,
155 'postdata' => NULL,
156 'error' => ''
157 );
Greg Aker9f9af602010-11-10 15:41:51 -0600158
159 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
161
162 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 /**
165 * Set Error Message
166 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500167 * Lets users set their own error messages on the fly. Note: The key
168 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * @param string
171 * @param string
172 * @return string
173 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100174 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
176 if ( ! is_array($lang))
177 {
178 $lang = array($lang => $val);
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 $this->_error_messages = array_merge($this->_error_messages, $lang);
Phil Sturgeonc3828712011-01-19 12:31:47 +0000182
Greg Aker9f9af602010-11-10 15:41:51 -0600183 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 /**
189 * Set The Error Delimiter
190 *
191 * Permits a prefix/suffix to be added to each error message
192 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @param string
194 * @param string
195 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200196 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100197 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
199 $this->_error_prefix = $prefix;
200 $this->_error_suffix = $suffix;
Phil Sturgeonc3828712011-01-19 12:31:47 +0000201
Greg Aker9f9af602010-11-10 15:41:51 -0600202 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
204
205 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 /**
208 * Get Error Message
209 *
210 * Gets the error message associated with a particular field
211 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 * @param string the field name
213 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200214 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100215 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200216 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
218 {
219 return '';
220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 if ($prefix == '')
223 {
224 $prefix = $this->_error_prefix;
225 }
226
227 if ($suffix == '')
228 {
229 $suffix = $this->_error_suffix;
230 }
231
232 return $prefix.$this->_field_data[$field]['error'].$suffix;
233 }
234
235 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100238 * Get Array of Error Messages
239 *
240 * Returns the error messages as an array
241 *
242 * @return array
243 */
244 public function error_array()
245 {
246 return $this->_error_array;
247 }
248
249 // --------------------------------------------------------------------
250
251 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 * Error String
253 *
254 * Returns the error messages as a string, wrapped in the error delimiters
255 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 * @param string
257 * @param string
258 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200259 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100260 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
262 // No errrors, validation passes!
263 if (count($this->_error_array) === 0)
264 {
265 return '';
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 if ($prefix == '')
269 {
270 $prefix = $this->_error_prefix;
271 }
272
273 if ($suffix == '')
274 {
275 $suffix = $this->_error_suffix;
276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 // Generate the error string
279 $str = '';
280 foreach ($this->_error_array as $val)
281 {
282 if ($val != '')
283 {
284 $str .= $prefix.$val.$suffix."\n";
285 }
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 return $str;
289 }
290
291 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 /**
294 * Run the Validator
295 *
296 * This function does all the work.
297 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200299 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100300 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500302 // Do we even have any data to process? Mm?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200303 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
305 return FALSE;
306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 // Does the _field_data array containing the validation rules exist?
309 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200310 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500312 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200313 if (count($this->_config_rules) === 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 // Is there a validation rule for the particular URI being accessed?
319 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 if ($uri != '' AND isset($this->_config_rules[$uri]))
322 {
323 $this->set_rules($this->_config_rules[$uri]);
324 }
325 else
326 {
327 $this->set_rules($this->_config_rules);
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // We're we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200331 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
333 log_message('debug', "Unable to find validation rules");
334 return FALSE;
335 }
336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // Load the language file containing error messages
339 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200340
341 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // corresponding $_POST item and test for errors
343 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200344 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
346 // 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 +0200347
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200348 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
350 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
351 }
352 else
353 {
354 if (isset($_POST[$field]) AND $_POST[$field] != "")
355 {
356 $this->_field_data[$field]['postdata'] = $_POST[$field];
357 }
358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
360 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
362
363 // Did we end up with any errors?
364 $total_errors = count($this->_error_array);
365
366 if ($total_errors > 0)
367 {
368 $this->_safe_form_data = TRUE;
369 }
370
371 // Now we need to re-set the POST data with the new, processed data
372 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200373
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200374 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
376
377 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 /**
380 * Traverse a multidimensional $_POST array index until the data is found
381 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 * @param array
383 * @param array
384 * @param integer
385 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200386 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100387 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200389 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200391 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 return $array;
395 }
396
397 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 /**
400 * Re-populate the _POST array with our finalized and processed data
401 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200403 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100404 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
406 foreach ($this->_field_data as $field => $row)
407 {
408 if ( ! is_null($row['postdata']))
409 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200410 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
412 if (isset($_POST[$row['field']]))
413 {
414 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
415 }
416 }
417 else
418 {
Derek Jones63eeae32009-02-10 19:08:56 +0000419 // start with a reference
420 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Jones63eeae32009-02-10 19:08:56 +0000422 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200423 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Derek Jones63eeae32009-02-10 19:08:56 +0000425 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
427 else
428 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 foreach ($row['keys'] as $val)
430 {
Derek Jones63eeae32009-02-10 19:08:56 +0000431 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 }
433 }
Derek Jones63eeae32009-02-10 19:08:56 +0000434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000436 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 $array = array();
438 foreach ($row['postdata'] as $k => $v)
439 {
440 $array[$k] = $this->prep_for_form($v);
441 }
Derek Jones63eeae32009-02-10 19:08:56 +0000442
443 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
445 else
Derek Jones63eeae32009-02-10 19:08:56 +0000446 {
447 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450 }
451 }
452 }
453
454 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 /**
457 * Executes the Validation routines
458 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 * @param array
460 * @param array
461 * @param mixed
462 * @param integer
463 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200464 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100465 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
467 // If the $_POST data is an array we will run a recursive call
468 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200469 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 foreach ($postdata as $key => $val)
471 {
472 $this->_execute($row, $rules, $val, $cycles);
473 $cycles++;
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 return;
477 }
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 // --------------------------------------------------------------------
480
481 // If the field is blank, but NOT required, no further tests are necessary
482 $callback = FALSE;
483 if ( ! in_array('required', $rules) AND is_null($postdata))
484 {
485 // Before we bail out, does the rule contain a callback?
Marcos Coelhoc28b2852011-07-05 12:59:41 -0700486 if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
488 $callback = TRUE;
489 $rules = (array('1' => $match[1]));
490 }
491 else
492 {
493 return;
494 }
495 }
496
497 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200500 if (is_null($postdata) AND $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
502 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
503 {
504 // Set the message type
505 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 if ( ! isset($this->_error_messages[$type]))
508 {
509 if (FALSE === ($line = $this->CI->lang->line($type)))
510 {
511 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200512 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
514 else
515 {
516 $line = $this->_error_messages[$type];
517 }
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 // Build the error message
520 $message = sprintf($line, $this->_translate_fieldname($row['label']));
521
522 // Save the error message
523 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 if ( ! isset($this->_error_array[$row['field']]))
526 {
527 $this->_error_array[$row['field']] = $message;
528 }
529 }
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 return;
532 }
533
534 // --------------------------------------------------------------------
535
536 // Cycle through each rule and run it
537 foreach ($rules As $rule)
538 {
539 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 // We set the $postdata variable with the current data in our master array so that
542 // each cycle of the loop is dealing with the processed data from the last cycle
543 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
544 {
545 // We shouldn't need this safety, but just in case there isn't an array index
546 // associated with this cycle we'll bail out
547 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
548 {
549 continue;
550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
553 $_in_array = TRUE;
554 }
555 else
556 {
557 $postdata = $this->_field_data[$row['field']]['postdata'];
558 }
559
560 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200561
562 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 $callback = FALSE;
564 if (substr($rule, 0, 9) == 'callback_')
565 {
566 $rule = substr($rule, 9);
567 $callback = TRUE;
568 }
Barry Mienydd671972010-10-04 16:33:58 +0200569
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 // Strip the parameter (if exists) from the rule
571 // Rules can contain a parameter: max_length[5]
572 $param = FALSE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500573 if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
575 $rule = $match[1];
576 $param = $match[2];
577 }
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 // Call the function that corresponds to the rule
580 if ($callback === TRUE)
581 {
582 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200583 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 continue;
585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 // Run the function and grab the result
588 $result = $this->CI->$rule($postdata, $param);
589
590 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200591 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
594 }
595 else
596 {
597 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 // If the field isn't required and we just processed a callback we'll move on...
601 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
602 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000603 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 }
605 }
606 else
Barry Mienydd671972010-10-04 16:33:58 +0200607 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 if ( ! method_exists($this, $rule))
609 {
Barry Mienydd671972010-10-04 16:33:58 +0200610 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 // Users can use any native PHP function call that has one param.
612 if (function_exists($rule))
613 {
614 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200615
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200616 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
618 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
619 }
620 else
621 {
622 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
623 }
624 }
patwork02404a12011-04-08 15:45:46 +0200625 else
626 {
627 log_message('debug', "Unable to find validation rule: ".$rule);
628 }
Barry Mienydd671972010-10-04 16:33:58 +0200629
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 continue;
631 }
632
633 $result = $this->$rule($postdata, $param);
634
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200635 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
637 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
638 }
639 else
640 {
641 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
642 }
643 }
Barry Mienydd671972010-10-04 16:33:58 +0200644
Derek Jones37f4b9c2011-07-01 17:56:50 -0500645 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200647 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 if ( ! isset($this->_error_messages[$rule]))
649 {
650 if (FALSE === ($line = $this->CI->lang->line($rule)))
651 {
652 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200653 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 }
655 else
656 {
657 $line = $this->_error_messages[$rule];
658 }
Barry Mienydd671972010-10-04 16:33:58 +0200659
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 // Is the parameter we are inserting into the error message the name
Derek Jones37f4b9c2011-07-01 17:56:50 -0500661 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200662 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
Pascal Krietec1895832009-10-13 12:56:43 +0000664 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // Build the error message
668 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
669
670 // Save the error message
671 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 if ( ! isset($this->_error_array[$row['field']]))
674 {
675 $this->_error_array[$row['field']] = $message;
676 }
Barry Mienydd671972010-10-04 16:33:58 +0200677
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 return;
679 }
680 }
681 }
682
683 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 /**
686 * Translate a field name
687 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 * @param string the field name
689 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200690 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100691 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
693 // Do we need to translate the field name?
694 // We look for the prefix lang: to determine this
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200695 if (substr($fieldname, 0, 5) === 'lang:')
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 {
697 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200698 $line = substr($fieldname, 5);
699
Derek Jones37f4b9c2011-07-01 17:56:50 -0500700 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
702 {
703 return $line;
704 }
705 }
706
707 return $fieldname;
708 }
709
710 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200711
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 /**
713 * Get the value from a form
714 *
715 * Permits you to repopulate a form field with the value it was submitted
716 * with, or, if that value doesn't exist, with the default
717 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 * @param string the field name
719 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200720 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200721 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100722 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200724 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 {
726 return $default;
727 }
Barry Mienydd671972010-10-04 16:33:58 +0200728
Phil Sturgeon5c561802011-01-05 16:31:59 +0000729 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600730 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000731 if (is_array($this->_field_data[$field]['postdata']))
732 {
733 return array_shift($this->_field_data[$field]['postdata']);
734 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 return $this->_field_data[$field]['postdata'];
737 }
Barry Mienydd671972010-10-04 16:33:58 +0200738
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200740
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 /**
742 * Set Select
743 *
744 * Enables pull-down lists to be set to the value the user
745 * selected in the event of an error
746 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 * @param string
748 * @param string
749 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200750 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100751 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200752 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200753 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
Andrey Andreev0adff1b2012-02-28 14:36:40 +0200755 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 }
Barry Mienydd671972010-10-04 16:33:58 +0200757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 if (is_array($field))
761 {
762 if ( ! in_array($value, $field))
763 {
764 return '';
765 }
766 }
Andrey Andreev46ac8812012-02-28 14:32:54 +0200767 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200769 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 }
Barry Mienydd671972010-10-04 16:33:58 +0200771
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 return ' selected="selected"';
773 }
Barry Mienydd671972010-10-04 16:33:58 +0200774
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 /**
778 * Set Radio
779 *
780 * Enables radio buttons to be set to the value the user
781 * selected in the event of an error
782 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 * @param string
784 * @param string
785 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200786 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100787 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200789 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200791 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 }
Barry Mienydd671972010-10-04 16:33:58 +0200793
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 if (is_array($field))
797 {
798 if ( ! in_array($value, $field))
799 {
800 return '';
801 }
802 }
803 else
804 {
805 if (($field == '' OR $value == '') OR ($field != $value))
806 {
807 return '';
808 }
809 }
Barry Mienydd671972010-10-04 16:33:58 +0200810
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 return ' checked="checked"';
812 }
Barry Mienydd671972010-10-04 16:33:58 +0200813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 /**
817 * Set Checkbox
818 *
819 * Enables checkboxes to be set to the value the user
820 * selected in the event of an error
821 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 * @param string
823 * @param string
824 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200825 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100826 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200828 // Logic is exactly the same as for radio fields
829 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 }
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 /**
835 * Required
836 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 * @param string
838 * @return bool
839 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100840 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200842 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 }
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200846
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500848 * Performs a Regular Expression match test.
849 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500850 * @param string
851 * @param regex
852 * @return bool
853 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100854 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500855 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200856 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500857 }
858
859 // --------------------------------------------------------------------
860
861 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 * Match one field to another
863 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 * @param string
865 * @param field
866 * @return bool
867 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100868 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 {
870 if ( ! isset($_POST[$field]))
871 {
Barry Mienydd671972010-10-04 16:33:58 +0200872 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 }
Barry Mienydd671972010-10-04 16:33:58 +0200874
Andrey Andreev46ac8812012-02-28 14:32:54 +0200875 return ($str === $_POST[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 }
Eric Barnescccde962011-12-04 00:01:17 -0500877
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100878 // --------------------------------------------------------------------
879
880 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200881 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100882 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200883 * Check if the input value doesn't already exist
884 * in the specified database field.
885 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100886 * @param string
887 * @param field
888 * @return bool
889 */
890 public function is_unique($str, $field)
891 {
Eric Barnescccde962011-12-04 00:01:17 -0500892 list($table, $field) = explode('.', $field);
893 if (isset($this->CI->db))
894 {
895 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
896 return $query->num_rows() === 0;
897 }
898 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600899 }
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 /**
904 * Minimum Length
905 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 * @param string
907 * @param value
908 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200909 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100910 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200912 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
914 return FALSE;
915 }
916
917 if (function_exists('mb_strlen'))
918 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200919 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 }
Barry Mienydd671972010-10-04 16:33:58 +0200921
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200922 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 }
Barry Mienydd671972010-10-04 16:33:58 +0200924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 /**
928 * Max Length
929 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 * @param string
931 * @param value
932 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200933 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100934 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200936 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
938 return FALSE;
939 }
940
941 if (function_exists('mb_strlen'))
942 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200943 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 }
Barry Mienydd671972010-10-04 16:33:58 +0200945
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200946 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 }
Barry Mienydd671972010-10-04 16:33:58 +0200948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200950
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 /**
952 * Exact Length
953 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 * @param string
955 * @param value
956 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200957 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100958 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200960 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
962 return FALSE;
963 }
964
965 if (function_exists('mb_strlen'))
966 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200967 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 }
Barry Mienydd671972010-10-04 16:33:58 +0200969
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200970 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 }
Barry Mienydd671972010-10-04 16:33:58 +0200972
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 /**
976 * Valid Email
977 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 * @param string
979 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200980 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100981 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200983 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 +0000984 }
985
986 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 /**
989 * Valid Emails
990 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 * @param string
992 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200993 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100994 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
996 if (strpos($str, ',') === FALSE)
997 {
998 return $this->valid_email(trim($str));
999 }
Barry Mienydd671972010-10-04 16:33:58 +02001000
Pascal Kriete14287f32011-02-14 13:39:34 -05001001 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001003 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 {
1005 return FALSE;
1006 }
1007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 return TRUE;
1010 }
1011
1012 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001013
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 /**
1015 * Validate IP Address
1016 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001018 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001020 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 {
1022 return $this->CI->input->valid_ip($ip);
1023 }
1024
1025 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001026
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 /**
1028 * Alpha
1029 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 * @param string
1031 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001032 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001033 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001035 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 }
Barry Mienydd671972010-10-04 16:33:58 +02001037
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001039
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 /**
1041 * Alpha-numeric
1042 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 * @param string
1044 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001045 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001046 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001048 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 }
Barry Mienydd671972010-10-04 16:33:58 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001052
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 /**
1054 * Alpha-numeric with underscores and dashes
1055 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 * @param string
1057 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001058 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001059 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001061 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 }
Barry Mienydd671972010-10-04 16:33:58 +02001063
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001065
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 /**
1067 * Numeric
1068 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 * @param string
1070 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001071 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001072 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001074 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001075
1076 }
1077
1078 // --------------------------------------------------------------------
1079
Barry Mienydd671972010-10-04 16:33:58 +02001080 /**
1081 * Is Numeric
1082 *
Barry Mienydd671972010-10-04 16:33:58 +02001083 * @param string
1084 * @return bool
1085 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001086 public function is_numeric($str)
Barry Mienydd671972010-10-04 16:33:58 +02001087 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001088 return is_numeric($str);
Barry Mienydd671972010-10-04 16:33:58 +02001089 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001090
1091 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001092
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 /**
1094 * Integer
1095 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 * @param string
1097 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001098 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001099 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001101 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1102 }
1103
1104 // --------------------------------------------------------------------
1105
1106 /**
1107 * Decimal number
1108 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001109 * @param string
1110 * @return bool
1111 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001112 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001113 {
1114 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1115 }
1116
1117 // --------------------------------------------------------------------
1118
1119 /**
1120 * Greather than
1121 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001122 * @param string
1123 * @return bool
1124 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001125 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001126 {
1127 if ( ! is_numeric($str))
1128 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001129 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001130 }
1131 return $str > $min;
1132 }
1133
1134 // --------------------------------------------------------------------
1135
1136 /**
1137 * Less than
1138 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001139 * @param string
1140 * @return bool
1141 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001142 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001143 {
1144 if ( ! is_numeric($str))
1145 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001146 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001147 }
1148 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001150
1151 // --------------------------------------------------------------------
1152
Barry Mienydd671972010-10-04 16:33:58 +02001153 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001154 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001155 *
Barry Mienydd671972010-10-04 16:33:58 +02001156 * @param string
1157 * @return bool
1158 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001159 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001160 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001161 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001162 }
1163
1164 // --------------------------------------------------------------------
1165
1166 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001167 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001168 *
Barry Mienydd671972010-10-04 16:33:58 +02001169 * @param string
1170 * @return bool
1171 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001172 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001173 {
Andrey Andreev46ac8812012-02-28 14:32:54 +02001174 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001175 }
1176
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001178
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 /**
1180 * Valid Base64
1181 *
1182 * Tests a string for characters outside of the Base64 alphabet
1183 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1184 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 * @param string
1186 * @return bool
1187 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001188 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 {
1190 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1191 }
Barry Mienydd671972010-10-04 16:33:58 +02001192
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001194
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 /**
1196 * Prep data for form
1197 *
1198 * This function allows HTML to be safely shown in a form.
1199 * Special characters are converted.
1200 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 * @param string
1202 * @return string
1203 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001204 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 {
1206 if (is_array($data))
1207 {
1208 foreach ($data as $key => $val)
1209 {
1210 $data[$key] = $this->prep_for_form($val);
1211 }
Barry Mienydd671972010-10-04 16:33:58 +02001212
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 return $data;
1214 }
Barry Mienydd671972010-10-04 16:33:58 +02001215
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 if ($this->_safe_form_data == FALSE OR $data === '')
1217 {
1218 return $data;
1219 }
1220
Andrey Andreev46ac8812012-02-28 14:32:54 +02001221 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 }
Barry Mienydd671972010-10-04 16:33:58 +02001223
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001225
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 /**
1227 * Prep URL
1228 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 * @param string
1230 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001231 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001232 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 {
1234 if ($str == 'http://' OR $str == '')
1235 {
1236 return '';
1237 }
Barry Mienydd671972010-10-04 16:33:58 +02001238
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001239 if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://')
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 {
1241 $str = 'http://'.$str;
1242 }
Barry Mienydd671972010-10-04 16:33:58 +02001243
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 return $str;
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 * Strip Image Tags
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 strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 {
1257 return $this->CI->input->strip_image_tags($str);
1258 }
Barry Mienydd671972010-10-04 16:33:58 +02001259
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001261
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 /**
1263 * XSS Clean
1264 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 * @param string
1266 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001267 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001268 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 {
Derek Jones5640a712010-04-23 11:22:40 -05001270 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 }
Barry Mienydd671972010-10-04 16:33:58 +02001272
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001274
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 /**
1276 * Convert PHP tags to entities
1277 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 * @param string
1279 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001280 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001281 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001283 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 }
1285
1286}
Derek Allard2067d1a2008-11-13 22:59:24 +00001287
1288/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001289/* Location: ./system/libraries/Form_validation.php */