blob: ebd96e40223a890ec0d46db8dd27db3676ed5bda [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 /**
238 * Error String
239 *
240 * Returns the error messages as a string, wrapped in the error delimiters
241 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * @param string
243 * @param string
244 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200245 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100246 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
248 // No errrors, validation passes!
249 if (count($this->_error_array) === 0)
250 {
251 return '';
252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 if ($prefix == '')
255 {
256 $prefix = $this->_error_prefix;
257 }
258
259 if ($suffix == '')
260 {
261 $suffix = $this->_error_suffix;
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 // Generate the error string
265 $str = '';
266 foreach ($this->_error_array as $val)
267 {
268 if ($val != '')
269 {
270 $str .= $prefix.$val.$suffix."\n";
271 }
272 }
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 return $str;
275 }
276
277 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 /**
280 * Run the Validator
281 *
282 * This function does all the work.
283 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200285 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100286 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500288 // Do we even have any data to process? Mm?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200289 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 return FALSE;
292 }
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 // Does the _field_data array containing the validation rules exist?
295 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200296 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500298 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200299 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 return FALSE;
302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // Is there a validation rule for the particular URI being accessed?
305 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 if ($uri != '' AND isset($this->_config_rules[$uri]))
308 {
309 $this->set_rules($this->_config_rules[$uri]);
310 }
311 else
312 {
313 $this->set_rules($this->_config_rules);
314 }
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 // We're we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200317 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 log_message('debug', "Unable to find validation rules");
320 return FALSE;
321 }
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // Load the language file containing error messages
325 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200326
327 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // corresponding $_POST item and test for errors
329 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200330 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
332 // 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 +0200333
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200334 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
337 }
338 else
339 {
340 if (isset($_POST[$field]) AND $_POST[$field] != "")
341 {
342 $this->_field_data[$field]['postdata'] = $_POST[$field];
343 }
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
346 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
348
349 // Did we end up with any errors?
350 $total_errors = count($this->_error_array);
351
352 if ($total_errors > 0)
353 {
354 $this->_safe_form_data = TRUE;
355 }
356
357 // Now we need to re-set the POST data with the new, processed data
358 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200359
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200360 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
362
363 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 /**
366 * Traverse a multidimensional $_POST array index until the data is found
367 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 * @param array
369 * @param array
370 * @param integer
371 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200372 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100373 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200375 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200377 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 }
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 return $array;
381 }
382
383 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 /**
386 * Re-populate the _POST array with our finalized and processed data
387 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200389 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100390 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 foreach ($this->_field_data as $field => $row)
393 {
394 if ( ! is_null($row['postdata']))
395 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200396 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 if (isset($_POST[$row['field']]))
399 {
400 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
401 }
402 }
403 else
404 {
Derek Jones63eeae32009-02-10 19:08:56 +0000405 // start with a reference
406 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Jones63eeae32009-02-10 19:08:56 +0000408 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200409 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Derek Jones63eeae32009-02-10 19:08:56 +0000411 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 }
413 else
414 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 foreach ($row['keys'] as $val)
416 {
Derek Jones63eeae32009-02-10 19:08:56 +0000417 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
419 }
Derek Jones63eeae32009-02-10 19:08:56 +0000420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000422 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 $array = array();
424 foreach ($row['postdata'] as $k => $v)
425 {
426 $array[$k] = $this->prep_for_form($v);
427 }
Derek Jones63eeae32009-02-10 19:08:56 +0000428
429 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
431 else
Derek Jones63eeae32009-02-10 19:08:56 +0000432 {
433 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 }
436 }
437 }
438 }
439
440 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 /**
443 * Executes the Validation routines
444 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 * @param array
446 * @param array
447 * @param mixed
448 * @param integer
449 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200450 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100451 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 // If the $_POST data is an array we will run a recursive call
454 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200455 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 foreach ($postdata as $key => $val)
457 {
458 $this->_execute($row, $rules, $val, $cycles);
459 $cycles++;
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 return;
463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 // --------------------------------------------------------------------
466
467 // If the field is blank, but NOT required, no further tests are necessary
468 $callback = FALSE;
469 if ( ! in_array('required', $rules) AND is_null($postdata))
470 {
471 // Before we bail out, does the rule contain a callback?
Marcos Coelhoc28b2852011-07-05 12:59:41 -0700472 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
483 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200486 if (is_null($postdata) AND $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
488 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
489 {
490 // Set the message type
491 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 if ( ! isset($this->_error_messages[$type]))
494 {
495 if (FALSE === ($line = $this->CI->lang->line($type)))
496 {
497 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200498 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
500 else
501 {
502 $line = $this->_error_messages[$type];
503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600506 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']));
Derek Allard2067d1a2008-11-13 22:59:24 +0000507
508 // Save the error message
509 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 if ( ! isset($this->_error_array[$row['field']]))
512 {
513 $this->_error_array[$row['field']] = $message;
514 }
515 }
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 return;
518 }
519
520 // --------------------------------------------------------------------
521
522 // Cycle through each rule and run it
523 foreach ($rules As $rule)
524 {
525 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 // We set the $postdata variable with the current data in our master array so that
528 // each cycle of the loop is dealing with the processed data from the last cycle
529 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
530 {
531 // We shouldn't need this safety, but just in case there isn't an array index
532 // associated with this cycle we'll bail out
533 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
534 {
535 continue;
536 }
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
539 $_in_array = TRUE;
540 }
541 else
542 {
543 $postdata = $this->_field_data[$row['field']]['postdata'];
544 }
545
546 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200547
548 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 $callback = FALSE;
550 if (substr($rule, 0, 9) == 'callback_')
551 {
552 $rule = substr($rule, 9);
553 $callback = TRUE;
554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // Strip the parameter (if exists) from the rule
557 // Rules can contain a parameter: max_length[5]
558 $param = FALSE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500559 if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
561 $rule = $match[1];
562 $param = $match[2];
563 }
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 // Call the function that corresponds to the rule
566 if ($callback === TRUE)
567 {
568 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200569 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 continue;
571 }
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 // Run the function and grab the result
574 $result = $this->CI->$rule($postdata, $param);
575
576 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200577 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
579 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
580 }
581 else
582 {
583 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
584 }
Barry Mienydd671972010-10-04 16:33:58 +0200585
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 // If the field isn't required and we just processed a callback we'll move on...
587 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
588 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000589 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
591 }
592 else
Barry Mienydd671972010-10-04 16:33:58 +0200593 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 if ( ! method_exists($this, $rule))
595 {
Barry Mienydd671972010-10-04 16:33:58 +0200596 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 // Users can use any native PHP function call that has one param.
598 if (function_exists($rule))
599 {
600 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200601
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200602 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
604 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
605 }
606 else
607 {
608 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
609 }
610 }
patwork02404a12011-04-08 15:45:46 +0200611 else
612 {
613 log_message('debug', "Unable to find validation rule: ".$rule);
614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 continue;
617 }
618
619 $result = $this->$rule($postdata, $param);
620
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
Derek Jones37f4b9c2011-07-01 17:56:50 -0500631 // 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
Derek Jones37f4b9c2011-07-01 17:56:50 -0500647 // 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
Eric Roberts41cc0902012-01-24 00:59:44 -0600654 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
Derek Allard2067d1a2008-11-13 22:59:24 +0000655
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 Andreev8dc532d2011-12-24 17:57:54 +0200681 if (substr($fieldname, 0, 5) === 'lang:')
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 // --------------------------------------------------------------------
Eric Roberts41cc0902012-01-24 00:59:44 -0600697
698 /**
699 * Build an error message using the field and param.
700 *
701 * @param string The error message line
702 * @param string A field's human name
703 * @param mixed A rule's optional parameter
704 * @return string
705 */
706 protected function _build_error_msg($line, $field = '', $param = '')
707 {
708 // Check for %s in the string for legacy support.
709 if (strpos($line, '%s') !== false)
710 {
711 return sprintf($line, $field, $param);
712 }
713
714 return str_replace(array('{field}', '{param}'), array($field, $param), $line);
715 }
716
717 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200718
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 /**
720 * Get the value from a form
721 *
722 * Permits you to repopulate a form field with the value it was submitted
723 * with, or, if that value doesn't exist, with the default
724 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 * @param string the field name
726 * @param string
727 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200728 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100729 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 {
731 if ( ! isset($this->_field_data[$field]))
732 {
733 return $default;
734 }
Barry Mienydd671972010-10-04 16:33:58 +0200735
Phil Sturgeon5c561802011-01-05 16:31:59 +0000736 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600737 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000738 if (is_array($this->_field_data[$field]['postdata']))
739 {
740 return array_shift($this->_field_data[$field]['postdata']);
741 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 return $this->_field_data[$field]['postdata'];
744 }
Barry Mienydd671972010-10-04 16:33:58 +0200745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 /**
749 * Set Select
750 *
751 * Enables pull-down lists to be set to the value the user
752 * selected in the event of an error
753 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 * @param string
755 * @param string
756 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200757 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100758 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200759 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
761 {
762 if ($default === TRUE AND count($this->_field_data) === 0)
763 {
764 return ' selected="selected"';
765 }
766 return '';
767 }
Barry Mienydd671972010-10-04 16:33:58 +0200768
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200770
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 if (is_array($field))
772 {
773 if ( ! in_array($value, $field))
774 {
775 return '';
776 }
777 }
778 else
779 {
780 if (($field == '' OR $value == '') OR ($field != $value))
781 {
782 return '';
783 }
784 }
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 return ' selected="selected"';
787 }
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200790
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 /**
792 * Set Radio
793 *
794 * Enables radio buttons to be set to the value the user
795 * selected in the event of an error
796 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 * @param string
798 * @param string
799 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200800 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100801 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 {
803 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
804 {
805 if ($default === TRUE AND count($this->_field_data) === 0)
806 {
807 return ' checked="checked"';
808 }
809 return '';
810 }
Barry Mienydd671972010-10-04 16:33:58 +0200811
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 if (is_array($field))
815 {
816 if ( ! in_array($value, $field))
817 {
818 return '';
819 }
820 }
821 else
822 {
823 if (($field == '' OR $value == '') OR ($field != $value))
824 {
825 return '';
826 }
827 }
Barry Mienydd671972010-10-04 16:33:58 +0200828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 return ' checked="checked"';
830 }
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 * Set Checkbox
836 *
837 * Enables checkboxes to be set to the value the user
838 * selected in the event of an error
839 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 * @param string
841 * @param string
842 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200843 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100844 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200846 // Logic is exactly the same as for radio fields
847 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 }
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200851
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 /**
853 * Required
854 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 * @param string
856 * @return bool
857 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100858 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200860 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500866 * Performs a Regular Expression match test.
867 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500868 * @param string
869 * @param regex
870 * @return bool
871 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100872 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500873 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200874 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500875 }
876
877 // --------------------------------------------------------------------
878
879 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 * Match one field to another
881 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 * @param string
883 * @param field
884 * @return bool
885 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100886 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 {
888 if ( ! isset($_POST[$field]))
889 {
Barry Mienydd671972010-10-04 16:33:58 +0200890 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 }
Barry Mienydd671972010-10-04 16:33:58 +0200892
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 $field = $_POST[$field];
894
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200895 return ($str === $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 }
Eric Barnescccde962011-12-04 00:01:17 -0500897
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100898 // --------------------------------------------------------------------
899
900 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200901 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100902 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200903 * Check if the input value doesn't already exist
904 * in the specified database field.
905 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100906 * @param string
907 * @param field
908 * @return bool
909 */
910 public function is_unique($str, $field)
911 {
Eric Barnescccde962011-12-04 00:01:17 -0500912 list($table, $field) = explode('.', $field);
913 if (isset($this->CI->db))
914 {
915 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
916 return $query->num_rows() === 0;
917 }
918 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600919 }
Barry Mienydd671972010-10-04 16:33:58 +0200920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200922
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 /**
924 * Minimum Length
925 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @param string
927 * @param value
928 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200929 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100930 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
932 if (preg_match("/[^0-9]/", $val))
933 {
934 return FALSE;
935 }
936
937 if (function_exists('mb_strlen'))
938 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200939 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 }
Barry Mienydd671972010-10-04 16:33:58 +0200941
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200942 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
Barry Mienydd671972010-10-04 16:33:58 +0200944
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 /**
948 * Max Length
949 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 * @param string
951 * @param value
952 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200953 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100954 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
956 if (preg_match("/[^0-9]/", $val))
957 {
958 return FALSE;
959 }
960
961 if (function_exists('mb_strlen'))
962 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200963 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 }
Barry Mienydd671972010-10-04 16:33:58 +0200965
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200966 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 /**
972 * Exact Length
973 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 * @param string
975 * @param value
976 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200977 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100978 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 {
980 if (preg_match("/[^0-9]/", $val))
981 {
982 return FALSE;
983 }
984
985 if (function_exists('mb_strlen'))
986 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200987 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200990 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 }
Barry Mienydd671972010-10-04 16:33:58 +0200992
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200994
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 /**
996 * Valid Email
997 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 * @param string
999 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001000 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001001 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001003 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 +00001004 }
1005
1006 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001007
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 /**
1009 * Valid Emails
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 valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
1016 if (strpos($str, ',') === FALSE)
1017 {
1018 return $this->valid_email(trim($str));
1019 }
Barry Mienydd671972010-10-04 16:33:58 +02001020
Pascal Kriete14287f32011-02-14 13:39:34 -05001021 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001023 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 {
1025 return FALSE;
1026 }
1027 }
Barry Mienydd671972010-10-04 16:33:58 +02001028
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 return TRUE;
1030 }
1031
1032 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 /**
1035 * Validate IP Address
1036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001038 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001040 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 {
1042 return $this->CI->input->valid_ip($ip);
1043 }
1044
1045 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001046
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 /**
1048 * Alpha
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 alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001055 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 }
Barry Mienydd671972010-10-04 16:33:58 +02001057
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001059
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 /**
1061 * Alpha-numeric
1062 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 * @param string
1064 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001065 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001066 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001068 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 }
Barry Mienydd671972010-10-04 16:33:58 +02001070
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001072
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 /**
1074 * Alpha-numeric with underscores and dashes
1075 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 * @param string
1077 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001078 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001079 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001081 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 }
Barry Mienydd671972010-10-04 16:33:58 +02001083
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001085
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 /**
1087 * Numeric
1088 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 * @param string
1090 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001091 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001092 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001094 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001095
1096 }
1097
1098 // --------------------------------------------------------------------
1099
Barry Mienydd671972010-10-04 16:33:58 +02001100 /**
1101 * Is Numeric
1102 *
Barry Mienydd671972010-10-04 16:33:58 +02001103 * @param string
1104 * @return bool
1105 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001106 public function is_numeric($str)
Barry Mienydd671972010-10-04 16:33:58 +02001107 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001108 return is_numeric($str);
Barry Mienydd671972010-10-04 16:33:58 +02001109 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001110
1111 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001112
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 /**
1114 * Integer
1115 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 * @param string
1117 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001118 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001119 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001121 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1122 }
1123
1124 // --------------------------------------------------------------------
1125
1126 /**
1127 * Decimal number
1128 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001129 * @param string
1130 * @return bool
1131 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001132 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001133 {
1134 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1135 }
1136
1137 // --------------------------------------------------------------------
1138
1139 /**
1140 * Greather than
1141 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001142 * @param string
1143 * @return bool
1144 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001145 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001146 {
1147 if ( ! is_numeric($str))
1148 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001149 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001150 }
1151 return $str > $min;
1152 }
1153
1154 // --------------------------------------------------------------------
1155
1156 /**
1157 * Less than
1158 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001159 * @param string
1160 * @return bool
1161 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001162 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001163 {
1164 if ( ! is_numeric($str))
1165 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001166 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001167 }
1168 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001170
1171 // --------------------------------------------------------------------
1172
Barry Mienydd671972010-10-04 16:33:58 +02001173 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001174 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001175 *
Barry Mienydd671972010-10-04 16:33:58 +02001176 * @param string
1177 * @return bool
1178 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001179 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001180 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001181 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001182 }
1183
1184 // --------------------------------------------------------------------
1185
1186 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001187 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001188 *
Barry Mienydd671972010-10-04 16:33:58 +02001189 * @param string
1190 * @return bool
1191 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001192 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001193 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001194 return ($str != 0 AND preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001195 }
1196
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001198
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 /**
1200 * Valid Base64
1201 *
1202 * Tests a string for characters outside of the Base64 alphabet
1203 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1204 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 * @param string
1206 * @return bool
1207 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001208 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 {
1210 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1211 }
Barry Mienydd671972010-10-04 16:33:58 +02001212
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001214
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 /**
1216 * Prep data for form
1217 *
1218 * This function allows HTML to be safely shown in a form.
1219 * Special characters are converted.
1220 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 * @param string
1222 * @return string
1223 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001224 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 {
1226 if (is_array($data))
1227 {
1228 foreach ($data as $key => $val)
1229 {
1230 $data[$key] = $this->prep_for_form($val);
1231 }
Barry Mienydd671972010-10-04 16:33:58 +02001232
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 return $data;
1234 }
Barry Mienydd671972010-10-04 16:33:58 +02001235
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 if ($this->_safe_form_data == FALSE OR $data === '')
1237 {
1238 return $data;
1239 }
1240
1241 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1242 }
Barry Mienydd671972010-10-04 16:33:58 +02001243
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001245
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 /**
1247 * Prep URL
1248 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 * @param string
1250 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001251 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001252 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 {
1254 if ($str == 'http://' OR $str == '')
1255 {
1256 return '';
1257 }
Barry Mienydd671972010-10-04 16:33:58 +02001258
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001259 if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://')
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 {
1261 $str = 'http://'.$str;
1262 }
Barry Mienydd671972010-10-04 16:33:58 +02001263
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 return $str;
1265 }
Barry Mienydd671972010-10-04 16:33:58 +02001266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001268
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 /**
1270 * Strip Image Tags
1271 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 * @param string
1273 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001274 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001275 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 {
1277 return $this->CI->input->strip_image_tags($str);
1278 }
Barry Mienydd671972010-10-04 16:33:58 +02001279
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001281
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 /**
1283 * XSS Clean
1284 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 * @param string
1286 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001287 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001288 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 {
Derek Jones5640a712010-04-23 11:22:40 -05001290 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 }
Barry Mienydd671972010-10-04 16:33:58 +02001292
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001294
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 /**
1296 * Convert PHP tags to entities
1297 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 * @param string
1299 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001300 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001301 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001303 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 }
1305
1306}
1307// END Form Validation Class
1308
1309/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001310/* Location: ./system/libraries/Form_validation.php */