blob: edaa9c361da890e2c4e9f0e9ef6b2001aedce59a [file] [log] [blame]
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnescccde962011-12-04 00:01:17 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnescccde962011-12-04 00:01:17 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreevb998f3e2012-01-24 15:31:26 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Form Validation Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
36 */
37class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020038
Phil Sturgeon3837ae72011-05-09 21:12:26 +010039 protected $CI;
40 protected $_field_data = array();
41 protected $_config_rules = array();
42 protected $_error_array = array();
43 protected $_error_messages = array();
44 protected $_error_prefix = '<p>';
45 protected $_error_suffix = '</p>';
46 protected $error_string = '';
47 protected $_safe_form_data = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000048
Greg Akera9263282010-11-10 15:26:43 -060049 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +020050 {
Derek Allard2067d1a2008-11-13 22:59:24 +000051 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020052
Derek Allard2067d1a2008-11-13 22:59:24 +000053 // Validation rules can be stored in a config file.
54 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 // Automatically load the form helper
57 $this->CI->load->helper('form');
58
59 // Set the character encoding in MB.
60 if (function_exists('mb_internal_encoding'))
61 {
62 mb_internal_encoding($this->CI->config->item('charset'));
63 }
Barry Mienydd671972010-10-04 16:33:58 +020064
Andrey Andreev901573c2012-01-11 01:40:48 +020065 log_message('debug', 'Form Validation Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000066 }
Barry Mienydd671972010-10-04 16:33:58 +020067
Derek Allard2067d1a2008-11-13 22:59:24 +000068 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 /**
71 * Set Rules
72 *
73 * This function takes an array of field names and validation
74 * rules as input, validates the info, and stores it
75 *
Derek Allard2067d1a2008-11-13 22:59:24 +000076 * @param mixed
77 * @param string
78 * @return void
79 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010080 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 // No reason to set rules if we have no POST data
Andrey Andreev8dc532d2011-12-24 17:57:54 +020083 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
Greg Aker9f9af602010-11-10 15:41:51 -060085 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +000086 }
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 // If an array was passed via the first parameter instead of indidual string
89 // values we cycle through it and recursively call this function.
90 if (is_array($field))
91 {
92 foreach ($field as $row)
93 {
94 // Houston, we have a problem...
95 if ( ! isset($row['field']) OR ! isset($row['rules']))
96 {
97 continue;
98 }
99
100 // If the field label wasn't passed we use the field name
101 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
102
103 // Here we go!
104 $this->set_rules($row['field'], $label, $row['rules']);
105 }
Greg Aker9f9af602010-11-10 15:41:51 -0600106 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200110 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Greg Aker9f9af602010-11-10 15:41:51 -0600112 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114
115 // If the field label wasn't passed we use the field name
116 $label = ($label == '') ? $field : $label;
117
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200118 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200119 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200120 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200121 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // Note: Due to a bug in current() that affects some versions
123 // of PHP we can not pass function call directly into it
124 $x = explode('[', $field);
125 $indexes[] = current($x);
126
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200127 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200129 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200131 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 $is_array = TRUE;
136 }
137 else
138 {
Barry Mienydd671972010-10-04 16:33:58 +0200139 $indexes = array();
140 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
143 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000145 'field' => $field,
146 'label' => $label,
147 'rules' => $rules,
148 'is_array' => $is_array,
149 'keys' => $indexes,
150 'postdata' => NULL,
151 'error' => ''
152 );
Greg Aker9f9af602010-11-10 15:41:51 -0600153
154 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156
157 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 /**
160 * Set Error Message
161 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500162 * Lets users set their own error messages on the fly. Note: The key
163 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 * @param string
166 * @param string
167 * @return string
168 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100169 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 if ( ! is_array($lang))
172 {
173 $lang = array($lang => $val);
174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600177 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 /**
183 * Set The Error Delimiter
184 *
185 * Permits a prefix/suffix to be added to each error message
186 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @param string
188 * @param string
189 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200190 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100191 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
193 $this->_error_prefix = $prefix;
194 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600195 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
197
198 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 /**
201 * Get Error Message
202 *
203 * Gets the error message associated with a particular field
204 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 * @param string the field name
206 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200207 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100208 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200209 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
211 {
212 return '';
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 if ($prefix == '')
216 {
217 $prefix = $this->_error_prefix;
218 }
219
220 if ($suffix == '')
221 {
222 $suffix = $this->_error_suffix;
223 }
224
225 return $prefix.$this->_field_data[$field]['error'].$suffix;
226 }
227
228 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 /**
231 * Error String
232 *
233 * Returns the error messages as a string, wrapped in the error delimiters
234 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 * @param string
236 * @param string
237 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200238 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100239 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 // No errrors, validation passes!
242 if (count($this->_error_array) === 0)
243 {
244 return '';
245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 if ($prefix == '')
248 {
249 $prefix = $this->_error_prefix;
250 }
251
252 if ($suffix == '')
253 {
254 $suffix = $this->_error_suffix;
255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 // Generate the error string
258 $str = '';
259 foreach ($this->_error_array as $val)
260 {
261 if ($val != '')
262 {
263 $str .= $prefix.$val.$suffix."\n";
264 }
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 return $str;
268 }
269
270 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 /**
273 * Run the Validator
274 *
275 * This function does all the work.
276 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200278 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100279 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500281 // Do we even have any data to process? Mm?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200282 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
284 return FALSE;
285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 // Does the _field_data array containing the validation rules exist?
288 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200289 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500291 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200292 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
294 return FALSE;
295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 // Is there a validation rule for the particular URI being accessed?
298 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200299
Andrey Andreev6de924c2012-01-20 13:18:18 +0200300 if ($uri != '' && isset($this->_config_rules[$uri]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
302 $this->set_rules($this->_config_rules[$uri]);
303 }
304 else
305 {
306 $this->set_rules($this->_config_rules);
307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Andrey Andreev901573c2012-01-11 01:40:48 +0200309 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200310 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200312 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 return FALSE;
314 }
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // Load the language file containing error messages
318 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200319
320 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 // corresponding $_POST item and test for errors
322 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200323 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
325 // Depending on whether the field name is an array or a string will determine where we get it from.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200326 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
329 }
Andrey Andreev6de924c2012-01-20 13:18:18 +0200330 elseif (isset($_POST[$field]) && $_POST[$field] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200332 $this->_field_data[$field]['postdata'] = $_POST[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
335 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
337
338 // Did we end up with any errors?
339 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 if ($total_errors > 0)
341 {
342 $this->_safe_form_data = TRUE;
343 }
344
345 // Now we need to re-set the POST data with the new, processed data
346 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200347
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200348 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
350
351 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 /**
354 * Traverse a multidimensional $_POST array index until the data is found
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @param array
357 * @param array
358 * @param integer
359 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200360 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100361 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200363 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200365 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 return $array;
369 }
370
371 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 /**
374 * Re-populate the _POST array with our finalized and processed data
375 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200376 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200377 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100378 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 foreach ($this->_field_data as $field => $row)
381 {
382 if ( ! is_null($row['postdata']))
383 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200384 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
386 if (isset($_POST[$row['field']]))
387 {
388 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
389 }
390 }
391 else
392 {
Derek Jones63eeae32009-02-10 19:08:56 +0000393 // start with a reference
394 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Jones63eeae32009-02-10 19:08:56 +0000396 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200397 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
Derek Jones63eeae32009-02-10 19:08:56 +0000399 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
401 else
402 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 foreach ($row['keys'] as $val)
404 {
Derek Jones63eeae32009-02-10 19:08:56 +0000405 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
407 }
Derek Jones63eeae32009-02-10 19:08:56 +0000408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000410 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 $array = array();
412 foreach ($row['postdata'] as $k => $v)
413 {
414 $array[$k] = $this->prep_for_form($v);
415 }
Derek Jones63eeae32009-02-10 19:08:56 +0000416
417 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
419 else
Derek Jones63eeae32009-02-10 19:08:56 +0000420 {
421 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 }
424 }
425 }
426 }
427
428 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 /**
431 * Executes the Validation routines
432 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 * @param array
434 * @param array
435 * @param mixed
436 * @param integer
437 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200438 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100439 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 // If the $_POST data is an array we will run a recursive call
442 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200443 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 foreach ($postdata as $key => $val)
445 {
446 $this->_execute($row, $rules, $val, $cycles);
447 $cycles++;
448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 return;
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 // If the field is blank, but NOT required, no further tests are necessary
454 $callback = FALSE;
Andrey Andreev6de924c2012-01-20 13:18:18 +0200455 if ( ! in_array('required', $rules) && is_null($postdata))
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
457 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200458 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
460 $callback = TRUE;
461 $rules = (array('1' => $match[1]));
462 }
463 else
464 {
465 return;
466 }
467 }
468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev6de924c2012-01-20 13:18:18 +0200470 if (is_null($postdata) && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
472 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
473 {
474 // Set the message type
475 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 if ( ! isset($this->_error_messages[$type]))
478 {
479 if (FALSE === ($line = $this->CI->lang->line($type)))
480 {
481 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200482 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
484 else
485 {
486 $line = $this->_error_messages[$type];
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 // Build the error message
490 $message = sprintf($line, $this->_translate_fieldname($row['label']));
491
492 // Save the error message
493 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 if ( ! isset($this->_error_array[$row['field']]))
496 {
497 $this->_error_array[$row['field']] = $message;
498 }
499 }
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 return;
502 }
503
504 // --------------------------------------------------------------------
505
506 // Cycle through each rule and run it
507 foreach ($rules As $rule)
508 {
509 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 // We set the $postdata variable with the current data in our master array so that
512 // each cycle of the loop is dealing with the processed data from the last cycle
Andrey Andreev6de924c2012-01-20 13:18:18 +0200513 if ($row['is_array'] == TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
515 // We shouldn't need this safety, but just in case there isn't an array index
516 // associated with this cycle we'll bail out
517 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
518 {
519 continue;
520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
523 $_in_array = TRUE;
524 }
525 else
526 {
527 $postdata = $this->_field_data[$row['field']]['postdata'];
528 }
529
Barry Mienydd671972010-10-04 16:33:58 +0200530 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200532 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
534 $rule = substr($rule, 9);
535 $callback = TRUE;
536 }
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 // Strip the parameter (if exists) from the rule
539 // Rules can contain a parameter: max_length[5]
540 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200541 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
543 $rule = $match[1];
544 $param = $match[2];
545 }
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 // Call the function that corresponds to the rule
548 if ($callback === TRUE)
549 {
550 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200551 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200552 log_message('debug', 'Unable to find callback validation rule: '.$rule);
553 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200555 else
556 {
557 // Run the function and grab the result
558 $result = $this->CI->$rule($postdata, $param);
559 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000560
561 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200562 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 {
564 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
565 }
566 else
567 {
568 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
569 }
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200572 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000574 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
576 }
577 else
Barry Mienydd671972010-10-04 16:33:58 +0200578 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 if ( ! method_exists($this, $rule))
580 {
Barry Mienydd671972010-10-04 16:33:58 +0200581 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 // Users can use any native PHP function call that has one param.
583 if (function_exists($rule))
584 {
Andrey Andreevcde43682012-01-13 20:16:35 +0200585 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200586
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200587 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 {
589 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
590 }
591 else
592 {
593 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
594 }
595 }
patwork02404a12011-04-08 15:45:46 +0200596 else
597 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200598 log_message('debug', 'Unable to find validation rule: '.$rule);
599 $result = FALSE;
patwork02404a12011-04-08 15:45:46 +0200600 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200602 else
603 {
604 $result = $this->$rule($postdata, $param);
605 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000606
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200607 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
609 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
610 }
611 else
612 {
613 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
614 }
615 }
Barry Mienydd671972010-10-04 16:33:58 +0200616
Andrey Andreev901573c2012-01-11 01:40:48 +0200617 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200619 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 if ( ! isset($this->_error_messages[$rule]))
621 {
622 if (FALSE === ($line = $this->CI->lang->line($rule)))
623 {
624 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200625 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 }
627 else
628 {
629 $line = $this->_error_messages[$rule];
630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200633 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200634 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
Pascal Krietec1895832009-10-13 12:56:43 +0000636 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 }
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 // Build the error message
640 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
641
642 // Save the error message
643 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200644
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 if ( ! isset($this->_error_array[$row['field']]))
646 {
647 $this->_error_array[$row['field']] = $message;
648 }
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 return;
651 }
652 }
653 }
654
655 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 /**
658 * Translate a field name
659 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 * @param string the field name
661 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200662 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100663 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 {
665 // Do we need to translate the field name?
666 // We look for the prefix lang: to determine this
Andrey Andreev901573c2012-01-11 01:40:48 +0200667 if (strpos($fieldname, 'lang:') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 {
669 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200670 $line = substr($fieldname, 5);
671
Derek Jones37f4b9c2011-07-01 17:56:50 -0500672 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
674 {
675 return $line;
676 }
677 }
678
679 return $fieldname;
680 }
681
682 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 /**
685 * Get the value from a form
686 *
687 * Permits you to repopulate a form field with the value it was submitted
688 * with, or, if that value doesn't exist, with the default
689 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 * @param string the field name
691 * @param string
692 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200693 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100694 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
696 if ( ! isset($this->_field_data[$field]))
697 {
698 return $default;
699 }
Barry Mienydd671972010-10-04 16:33:58 +0200700
Phil Sturgeon5c561802011-01-05 16:31:59 +0000701 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600702 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000703 if (is_array($this->_field_data[$field]['postdata']))
704 {
705 return array_shift($this->_field_data[$field]['postdata']);
706 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 return $this->_field_data[$field]['postdata'];
709 }
Barry Mienydd671972010-10-04 16:33:58 +0200710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200712
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 /**
714 * Set Select
715 *
716 * Enables pull-down lists to be set to the value the user
717 * selected in the event of an error
718 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 * @param string
720 * @param string
721 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200722 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100723 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200724 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
726 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200727 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 }
Barry Mienydd671972010-10-04 16:33:58 +0200729
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 if (is_array($field))
732 {
733 if ( ! in_array($value, $field))
734 {
735 return '';
736 }
737 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200738 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200740 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 }
Barry Mienydd671972010-10-04 16:33:58 +0200742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 return ' selected="selected"';
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 Radio
750 *
751 * Enables radio buttons 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_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 {
760 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
761 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200762 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 }
Barry Mienydd671972010-10-04 16:33:58 +0200764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 if (is_array($field))
767 {
768 if ( ! in_array($value, $field))
769 {
770 return '';
771 }
772 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200773 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200775 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 }
Barry Mienydd671972010-10-04 16:33:58 +0200777
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 return ' checked="checked"';
779 }
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200782
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 /**
784 * Set Checkbox
785 *
786 * Enables checkboxes to be set to the value the user
787 * selected in the event of an error
788 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 * @param string
790 * @param string
791 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200792 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100793 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200795 // Logic is exactly the same as for radio fields
796 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 }
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 /**
802 * Required
803 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 * @param string
805 * @return bool
806 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100807 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200809 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 }
Barry Mienydd671972010-10-04 16:33:58 +0200811
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500815 * Performs a Regular Expression match test.
816 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500817 * @param string
818 * @param regex
819 * @return bool
820 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100821 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500822 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200823 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500824 }
825
826 // --------------------------------------------------------------------
827
828 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 * Match one field to another
830 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 * @param string
832 * @param field
833 * @return bool
834 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100835 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 {
837 if ( ! isset($_POST[$field]))
838 {
Barry Mienydd671972010-10-04 16:33:58 +0200839 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 }
Barry Mienydd671972010-10-04 16:33:58 +0200841
Andrey Andreev901573c2012-01-11 01:40:48 +0200842 return ($str === $_POST[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 }
Eric Barnescccde962011-12-04 00:01:17 -0500844
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100845 // --------------------------------------------------------------------
846
847 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200848 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100849 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200850 * Check if the input value doesn't already exist
851 * in the specified database field.
852 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100853 * @param string
854 * @param field
855 * @return bool
856 */
857 public function is_unique($str, $field)
858 {
Eric Barnescccde962011-12-04 00:01:17 -0500859 list($table, $field) = explode('.', $field);
860 if (isset($this->CI->db))
861 {
862 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
863 return $query->num_rows() === 0;
864 }
865 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600866 }
Barry Mienydd671972010-10-04 16:33:58 +0200867
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 /**
871 * Minimum Length
872 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 * @param string
874 * @param value
875 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200876 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100877 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200879 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
881 return FALSE;
882 }
883
884 if (function_exists('mb_strlen'))
885 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200886 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200889 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 }
Barry Mienydd671972010-10-04 16:33:58 +0200891
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 /**
895 * Max Length
896 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 * @param string
898 * @param value
899 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200900 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100901 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200903 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 {
905 return FALSE;
906 }
907
908 if (function_exists('mb_strlen'))
909 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200910 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 }
Barry Mienydd671972010-10-04 16:33:58 +0200912
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200913 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 }
Barry Mienydd671972010-10-04 16:33:58 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200917
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 /**
919 * Exact Length
920 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 * @param string
922 * @param value
923 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200924 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100925 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200927 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 {
929 return FALSE;
930 }
931
932 if (function_exists('mb_strlen'))
933 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200934 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200937 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 }
Barry Mienydd671972010-10-04 16:33:58 +0200939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200941
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 /**
943 * Valid Email
944 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 * @param string
946 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200947 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100948 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200950 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 +0000951 }
952
953 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200954
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 /**
956 * Valid Emails
957 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 * @param string
959 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200960 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100961 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 {
963 if (strpos($str, ',') === FALSE)
964 {
965 return $this->valid_email(trim($str));
966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967
Pascal Kriete14287f32011-02-14 13:39:34 -0500968 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200970 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 {
972 return FALSE;
973 }
974 }
Barry Mienydd671972010-10-04 16:33:58 +0200975
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 return TRUE;
977 }
978
979 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 /**
982 * Validate IP Address
983 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +0800985 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100987 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 {
989 return $this->CI->input->valid_ip($ip);
990 }
991
992 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200993
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 /**
995 * Alpha
996 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 * @param string
998 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200999 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001000 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001002 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
Barry Mienydd671972010-10-04 16:33:58 +02001004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001006
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 /**
1008 * Alpha-numeric
1009 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 * @param string
1011 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001012 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001013 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001015 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001019
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 /**
1021 * Alpha-numeric with underscores and dashes
1022 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 * @param string
1024 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001025 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001026 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001028 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 }
Barry Mienydd671972010-10-04 16:33:58 +02001030
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001032
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 /**
1034 * Numeric
1035 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 * @param string
1037 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001038 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001039 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001041 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001042
1043 }
1044
1045 // --------------------------------------------------------------------
1046
Barry Mienydd671972010-10-04 16:33:58 +02001047 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 * Integer
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 integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001055 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1056 }
1057
1058 // --------------------------------------------------------------------
1059
1060 /**
1061 * Decimal number
1062 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001063 * @param string
1064 * @return bool
1065 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001066 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001067 {
1068 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1069 }
1070
1071 // --------------------------------------------------------------------
1072
1073 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001074 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001075 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001076 * @param string
1077 * @return bool
1078 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001079 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001080 {
1081 if ( ! is_numeric($str))
1082 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001083 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001084 }
1085 return $str > $min;
1086 }
1087
1088 // --------------------------------------------------------------------
1089
1090 /**
1091 * Less than
1092 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001093 * @param string
1094 * @return bool
1095 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001096 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001097 {
1098 if ( ! is_numeric($str))
1099 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001100 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001101 }
1102 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001104
1105 // --------------------------------------------------------------------
1106
Barry Mienydd671972010-10-04 16:33:58 +02001107 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001108 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001109 *
Barry Mienydd671972010-10-04 16:33:58 +02001110 * @param string
1111 * @return bool
1112 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001113 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001114 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001115 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001116 }
1117
1118 // --------------------------------------------------------------------
1119
1120 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001121 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001122 *
Barry Mienydd671972010-10-04 16:33:58 +02001123 * @param string
1124 * @return bool
1125 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001126 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001127 {
Andrey Andreev6de924c2012-01-20 13:18:18 +02001128 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001129 }
1130
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001132
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 /**
1134 * Valid Base64
1135 *
1136 * Tests a string for characters outside of the Base64 alphabet
1137 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1138 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 * @param string
1140 * @return bool
1141 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001142 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 {
1144 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1145 }
Barry Mienydd671972010-10-04 16:33:58 +02001146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001148
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 /**
1150 * Prep data for form
1151 *
1152 * This function allows HTML to be safely shown in a form.
1153 * Special characters are converted.
1154 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 * @param string
1156 * @return string
1157 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001158 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 {
1160 if (is_array($data))
1161 {
1162 foreach ($data as $key => $val)
1163 {
1164 $data[$key] = $this->prep_for_form($val);
1165 }
Barry Mienydd671972010-10-04 16:33:58 +02001166
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 return $data;
1168 }
Barry Mienydd671972010-10-04 16:33:58 +02001169
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 if ($this->_safe_form_data == FALSE OR $data === '')
1171 {
1172 return $data;
1173 }
1174
Andrey Andreev901573c2012-01-11 01:40:48 +02001175 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 }
Barry Mienydd671972010-10-04 16:33:58 +02001177
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001179
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 /**
1181 * Prep URL
1182 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 * @param string
1184 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001185 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001186 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001188 if ($str === 'http://' OR $str == '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 {
1190 return '';
1191 }
Barry Mienydd671972010-10-04 16:33:58 +02001192
Andrey Andreev901573c2012-01-11 01:40:48 +02001193 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001195 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 return $str;
1199 }
Barry Mienydd671972010-10-04 16:33:58 +02001200
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001202
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 /**
1204 * Strip Image Tags
1205 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 * @param string
1207 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001208 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001209 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 {
1211 return $this->CI->input->strip_image_tags($str);
1212 }
Barry Mienydd671972010-10-04 16:33:58 +02001213
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001215
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 /**
1217 * XSS Clean
1218 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 * @param string
1220 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001221 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001222 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 {
Derek Jones5640a712010-04-23 11:22:40 -05001224 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 }
Barry Mienydd671972010-10-04 16:33:58 +02001226
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001228
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 /**
1230 * Convert PHP tags to entities
1231 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 * @param string
1233 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001234 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001235 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001237 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 }
1239
1240}
Derek Allard2067d1a2008-11-13 22:59:24 +00001241
1242/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001243/* Location: ./system/libraries/Form_validation.php */