blob: 0a6a2af0d397891a148983359a716a86866c5800 [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
506 $message = sprintf($line, $this->_translate_fieldname($row['label']));
507
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
654 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
655
656 // Save the error message
657 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 if ( ! isset($this->_error_array[$row['field']]))
660 {
661 $this->_error_array[$row['field']] = $message;
662 }
Barry Mienydd671972010-10-04 16:33:58 +0200663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 return;
665 }
666 }
667 }
668
669 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200670
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 /**
672 * Translate a field name
673 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 * @param string the field name
675 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200676 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100677 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
679 // Do we need to translate the field name?
680 // We look for the prefix lang: to determine this
Andrey 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 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200697
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 /**
699 * Get the value from a form
700 *
701 * Permits you to repopulate a form field with the value it was submitted
702 * with, or, if that value doesn't exist, with the default
703 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 * @param string the field name
705 * @param string
706 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200707 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100708 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 {
710 if ( ! isset($this->_field_data[$field]))
711 {
712 return $default;
713 }
Barry Mienydd671972010-10-04 16:33:58 +0200714
Phil Sturgeon5c561802011-01-05 16:31:59 +0000715 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600716 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000717 if (is_array($this->_field_data[$field]['postdata']))
718 {
719 return array_shift($this->_field_data[$field]['postdata']);
720 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000721
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 return $this->_field_data[$field]['postdata'];
723 }
Barry Mienydd671972010-10-04 16:33:58 +0200724
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200726
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 /**
728 * Set Select
729 *
730 * Enables pull-down lists to be set to the value the user
731 * selected in the event of an error
732 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 * @param string
734 * @param string
735 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200736 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100737 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200738 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
740 {
741 if ($default === TRUE AND count($this->_field_data) === 0)
742 {
743 return ' selected="selected"';
744 }
745 return '';
746 }
Barry Mienydd671972010-10-04 16:33:58 +0200747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200749
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 if (is_array($field))
751 {
752 if ( ! in_array($value, $field))
753 {
754 return '';
755 }
756 }
757 else
758 {
759 if (($field == '' OR $value == '') OR ($field != $value))
760 {
761 return '';
762 }
763 }
Barry Mienydd671972010-10-04 16:33:58 +0200764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 return ' selected="selected"';
766 }
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200769
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 /**
771 * Set Radio
772 *
773 * Enables radio buttons to be set to the value the user
774 * selected in the event of an error
775 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 * @param string
777 * @param string
778 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200779 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100780 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 {
782 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
783 {
784 if ($default === TRUE AND count($this->_field_data) === 0)
785 {
786 return ' checked="checked"';
787 }
788 return '';
789 }
Barry Mienydd671972010-10-04 16:33:58 +0200790
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200792
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 if (is_array($field))
794 {
795 if ( ! in_array($value, $field))
796 {
797 return '';
798 }
799 }
800 else
801 {
802 if (($field == '' OR $value == '') OR ($field != $value))
803 {
804 return '';
805 }
806 }
Barry Mienydd671972010-10-04 16:33:58 +0200807
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 return ' checked="checked"';
809 }
Barry Mienydd671972010-10-04 16:33:58 +0200810
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 /**
814 * Set Checkbox
815 *
816 * Enables checkboxes to be set to the value the user
817 * selected in the event of an error
818 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 * @param string
820 * @param string
821 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200822 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100823 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200825 // Logic is exactly the same as for radio fields
826 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 }
Barry Mienydd671972010-10-04 16:33:58 +0200828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 /**
832 * Required
833 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 * @param string
835 * @return bool
836 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100837 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200839 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 }
Barry Mienydd671972010-10-04 16:33:58 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200843
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500845 * Performs a Regular Expression match test.
846 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500847 * @param string
848 * @param regex
849 * @return bool
850 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100851 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500852 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200853 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500854 }
855
856 // --------------------------------------------------------------------
857
858 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 * Match one field to another
860 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 * @param string
862 * @param field
863 * @return bool
864 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100865 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 {
867 if ( ! isset($_POST[$field]))
868 {
Barry Mienydd671972010-10-04 16:33:58 +0200869 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 }
Barry Mienydd671972010-10-04 16:33:58 +0200871
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 $field = $_POST[$field];
873
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200874 return ($str === $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 }
Eric Barnescccde962011-12-04 00:01:17 -0500876
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100877 // --------------------------------------------------------------------
878
879 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200880 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100881 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200882 * Check if the input value doesn't already exist
883 * in the specified database field.
884 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100885 * @param string
886 * @param field
887 * @return bool
888 */
889 public function is_unique($str, $field)
890 {
Eric Barnescccde962011-12-04 00:01:17 -0500891 list($table, $field) = explode('.', $field);
892 if (isset($this->CI->db))
893 {
894 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
895 return $query->num_rows() === 0;
896 }
897 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200901
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 /**
903 * Minimum Length
904 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 * @param string
906 * @param value
907 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200908 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100909 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 {
911 if (preg_match("/[^0-9]/", $val))
912 {
913 return FALSE;
914 }
915
916 if (function_exists('mb_strlen'))
917 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200918 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 }
Barry Mienydd671972010-10-04 16:33:58 +0200920
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200921 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 /**
927 * Max Length
928 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 * @param string
930 * @param value
931 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200932 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100933 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 {
935 if (preg_match("/[^0-9]/", $val))
936 {
937 return FALSE;
938 }
939
940 if (function_exists('mb_strlen'))
941 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200942 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
Barry Mienydd671972010-10-04 16:33:58 +0200944
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200945 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200949
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 /**
951 * Exact Length
952 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 * @param string
954 * @param value
955 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200956 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100957 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 {
959 if (preg_match("/[^0-9]/", $val))
960 {
961 return FALSE;
962 }
963
964 if (function_exists('mb_strlen'))
965 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200966 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200969 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 }
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 /**
975 * Valid Email
976 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 * @param string
978 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200979 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100980 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200982 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 +0000983 }
984
985 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 /**
988 * Valid Emails
989 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 * @param string
991 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200992 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100993 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
995 if (strpos($str, ',') === FALSE)
996 {
997 return $this->valid_email(trim($str));
998 }
Barry Mienydd671972010-10-04 16:33:58 +0200999
Pascal Kriete14287f32011-02-14 13:39:34 -05001000 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001002 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 {
1004 return FALSE;
1005 }
1006 }
Barry Mienydd671972010-10-04 16:33:58 +02001007
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 return TRUE;
1009 }
1010
1011 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001012
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 /**
1014 * Validate IP Address
1015 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001017 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001019 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 {
1021 return $this->CI->input->valid_ip($ip);
1022 }
1023
1024 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001025
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 /**
1027 * Alpha
1028 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 * @param string
1030 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001031 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001032 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001034 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 }
Barry Mienydd671972010-10-04 16:33:58 +02001036
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001038
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 /**
1040 * Alpha-numeric
1041 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 * @param string
1043 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001044 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001045 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001047 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 }
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001051
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 /**
1053 * Alpha-numeric with underscores and dashes
1054 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 * @param string
1056 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001057 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001058 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001060 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 }
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001064
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 /**
1066 * Numeric
1067 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 * @param string
1069 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001070 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001071 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001073 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001074
1075 }
1076
1077 // --------------------------------------------------------------------
1078
Barry Mienydd671972010-10-04 16:33:58 +02001079 /**
1080 * Is Numeric
1081 *
Barry Mienydd671972010-10-04 16:33:58 +02001082 * @param string
1083 * @return bool
1084 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001085 public function is_numeric($str)
Barry Mienydd671972010-10-04 16:33:58 +02001086 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001087 return is_numeric($str);
Barry Mienydd671972010-10-04 16:33:58 +02001088 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001089
1090 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001091
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 /**
1093 * Integer
1094 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 * @param string
1096 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001097 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001098 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001100 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1101 }
1102
1103 // --------------------------------------------------------------------
1104
1105 /**
1106 * Decimal number
1107 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001108 * @param string
1109 * @return bool
1110 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001111 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001112 {
1113 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1114 }
1115
1116 // --------------------------------------------------------------------
1117
1118 /**
1119 * Greather than
1120 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001121 * @param string
1122 * @return bool
1123 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001124 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001125 {
1126 if ( ! is_numeric($str))
1127 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001128 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001129 }
1130 return $str > $min;
1131 }
1132
1133 // --------------------------------------------------------------------
1134
1135 /**
1136 * Less than
1137 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001138 * @param string
1139 * @return bool
1140 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001141 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001142 {
1143 if ( ! is_numeric($str))
1144 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001145 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001146 }
1147 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001149
1150 // --------------------------------------------------------------------
1151
Barry Mienydd671972010-10-04 16:33:58 +02001152 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001153 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001154 *
Barry Mienydd671972010-10-04 16:33:58 +02001155 * @param string
1156 * @return bool
1157 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001158 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001159 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001160 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001161 }
1162
1163 // --------------------------------------------------------------------
1164
1165 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001166 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001167 *
Barry Mienydd671972010-10-04 16:33:58 +02001168 * @param string
1169 * @return bool
1170 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001171 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001172 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001173 return ($str != 0 AND preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001174 }
1175
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001177
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 /**
1179 * Valid Base64
1180 *
1181 * Tests a string for characters outside of the Base64 alphabet
1182 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1183 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 * @param string
1185 * @return bool
1186 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001187 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 {
1189 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1190 }
Barry Mienydd671972010-10-04 16:33:58 +02001191
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001193
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 /**
1195 * Prep data for form
1196 *
1197 * This function allows HTML to be safely shown in a form.
1198 * Special characters are converted.
1199 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 * @param string
1201 * @return string
1202 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001203 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 {
1205 if (is_array($data))
1206 {
1207 foreach ($data as $key => $val)
1208 {
1209 $data[$key] = $this->prep_for_form($val);
1210 }
Barry Mienydd671972010-10-04 16:33:58 +02001211
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 return $data;
1213 }
Barry Mienydd671972010-10-04 16:33:58 +02001214
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 if ($this->_safe_form_data == FALSE OR $data === '')
1216 {
1217 return $data;
1218 }
1219
1220 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1221 }
Barry Mienydd671972010-10-04 16:33:58 +02001222
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001224
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 /**
1226 * Prep URL
1227 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 * @param string
1229 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001230 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001231 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 {
1233 if ($str == 'http://' OR $str == '')
1234 {
1235 return '';
1236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001238 if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://')
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 {
1240 $str = 'http://'.$str;
1241 }
Barry Mienydd671972010-10-04 16:33:58 +02001242
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 return $str;
1244 }
Barry Mienydd671972010-10-04 16:33:58 +02001245
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001247
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 /**
1249 * Strip Image Tags
1250 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 * @param string
1252 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001253 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001254 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 {
1256 return $this->CI->input->strip_image_tags($str);
1257 }
Barry Mienydd671972010-10-04 16:33:58 +02001258
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001260
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 /**
1262 * XSS Clean
1263 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 * @param string
1265 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001266 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001267 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 {
Derek Jones5640a712010-04-23 11:22:40 -05001269 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 }
Barry Mienydd671972010-10-04 16:33:58 +02001271
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001273
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 /**
1275 * Convert PHP tags to entities
1276 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 * @param string
1278 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001279 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001280 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001282 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 }
1284
1285}
1286// END Form Validation Class
1287
1288/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001289/* Location: ./system/libraries/Form_validation.php */