blob: b3efe82cf2261b0f3a4d5e5523d3e1550e9cd426 [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;
JonoB099c4782012-03-04 14:37:30 +000050 protected $validation_data = array();
51
Derek Allard2067d1a2008-11-13 22:59:24 +000052 /**
53 * Constructor
Barry Mienydd671972010-10-04 16:33:58 +020054 */
Greg Akera9263282010-11-10 15:26:43 -060055 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +020056 {
Derek Allard2067d1a2008-11-13 22:59:24 +000057 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Allard2067d1a2008-11-13 22:59:24 +000059 // Validation rules can be stored in a config file.
60 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 // Automatically load the form helper
63 $this->CI->load->helper('form');
64
65 // Set the character encoding in MB.
66 if (function_exists('mb_internal_encoding'))
67 {
68 mb_internal_encoding($this->CI->config->item('charset'));
69 }
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 log_message('debug', "Form Validation Class Initialized");
72 }
Barry Mienydd671972010-10-04 16:33:58 +020073
Derek Allard2067d1a2008-11-13 22:59:24 +000074 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Allard2067d1a2008-11-13 22:59:24 +000076 /**
77 * Set Rules
78 *
79 * This function takes an array of field names and validation
80 * rules as input, validates the info, and stores it
81 *
Derek Allard2067d1a2008-11-13 22:59:24 +000082 * @param mixed
83 * @param string
84 * @return void
85 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010086 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
JonoB099c4782012-03-04 14:37:30 +000088 // No reason to set rules if we have no POST data
89 // or a validation array has not been specified
90 if (count($_POST) === 0 && count($this->validation_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Greg Aker9f9af602010-11-10 15:41:51 -060092 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +000093 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 // If an array was passed via the first parameter instead of indidual string
96 // values we cycle through it and recursively call this function.
97 if (is_array($field))
98 {
99 foreach ($field as $row)
100 {
101 // Houston, we have a problem...
102 if ( ! isset($row['field']) OR ! isset($row['rules']))
103 {
104 continue;
105 }
106
107 // If the field label wasn't passed we use the field name
108 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
109
110 // Here we go!
111 $this->set_rules($row['field'], $label, $row['rules']);
112 }
Greg Aker9f9af602010-11-10 15:41:51 -0600113 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200117 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Greg Aker9f9af602010-11-10 15:41:51 -0600119 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 }
121
122 // If the field label wasn't passed we use the field name
123 $label = ($label == '') ? $field : $label;
124
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200125 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200126 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200127 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200128 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 // Note: Due to a bug in current() that affects some versions
130 // of PHP we can not pass function call directly into it
131 $x = explode('[', $field);
132 $indexes[] = current($x);
133
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200134 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200136 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200138 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 $is_array = TRUE;
143 }
144 else
145 {
Barry Mienydd671972010-10-04 16:33:58 +0200146 $indexes = array();
147 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
150 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000152 'field' => $field,
153 'label' => $label,
154 'rules' => $rules,
155 'is_array' => $is_array,
156 'keys' => $indexes,
157 'postdata' => NULL,
158 'error' => ''
159 );
Greg Aker9f9af602010-11-10 15:41:51 -0600160
161 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
163
164 // --------------------------------------------------------------------
JonoB099c4782012-03-04 14:37:30 +0000165
166 /**
167 * By default, form validation uses the $_POST array to validate
168 *
169 * If an array is set through this method, then this array will
170 * be used instead of the $_POST array
171 *
172 * @param array $data
173 */
174 public function set_data($data = '')
175 {
176 if ( ! empty($data) && is_array($data))
177 {
178 $this->validation_data = $data;
179 }
180 }
181
182 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 /**
185 * Set Error Message
186 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500187 * Lets users set their own error messages on the fly. Note: The key
JonoB099c4782012-03-04 14:37:30 +0000188 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 * @param string
191 * @param string
192 * @return string
193 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100194 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
196 if ( ! is_array($lang))
197 {
198 $lang = array($lang => $val);
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 $this->_error_messages = array_merge($this->_error_messages, $lang);
Phil Sturgeonc3828712011-01-19 12:31:47 +0000202
Greg Aker9f9af602010-11-10 15:41:51 -0600203 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 /**
209 * Set The Error Delimiter
210 *
211 * Permits a prefix/suffix to be added to each error message
212 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * @param string
214 * @param string
215 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200216 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100217 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
219 $this->_error_prefix = $prefix;
220 $this->_error_suffix = $suffix;
Phil Sturgeonc3828712011-01-19 12:31:47 +0000221
Greg Aker9f9af602010-11-10 15:41:51 -0600222 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
224
225 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 /**
228 * Get Error Message
229 *
230 * Gets the error message associated with a particular field
231 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @param string the field name
233 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200234 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100235 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200236 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
238 {
239 return '';
240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 if ($prefix == '')
243 {
244 $prefix = $this->_error_prefix;
245 }
246
247 if ($suffix == '')
248 {
249 $suffix = $this->_error_suffix;
250 }
251
252 return $prefix.$this->_field_data[$field]['error'].$suffix;
253 }
254
255 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100258 * Get Array of Error Messages
259 *
260 * Returns the error messages as an array
261 *
262 * @return array
263 */
264 public function error_array()
265 {
266 return $this->_error_array;
267 }
268
269 // --------------------------------------------------------------------
270
271 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 * Error String
273 *
274 * Returns the error messages as a string, wrapped in the error delimiters
275 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @param string
277 * @param string
278 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200279 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100280 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
282 // No errrors, validation passes!
283 if (count($this->_error_array) === 0)
284 {
285 return '';
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 if ($prefix == '')
289 {
290 $prefix = $this->_error_prefix;
291 }
292
293 if ($suffix == '')
294 {
295 $suffix = $this->_error_suffix;
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // Generate the error string
299 $str = '';
300 foreach ($this->_error_array as $val)
301 {
302 if ($val != '')
303 {
304 $str .= $prefix.$val.$suffix."\n";
305 }
306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 return $str;
309 }
310
311 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 /**
314 * Run the Validator
315 *
316 * This function does all the work.
317 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200319 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100320 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500322 // Do we even have any data to process? Mm?
JonoB099c4782012-03-04 14:37:30 +0000323 $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST;
324 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
326 return FALSE;
327 }
JonoB099c4782012-03-04 14:37:30 +0000328
329 // Clear any previous validation data
330 $this->_reset_validation();
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 // Does the _field_data array containing the validation rules exist?
333 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200334 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500336 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200337 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 return FALSE;
340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // Is there a validation rule for the particular URI being accessed?
343 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 if ($uri != '' AND isset($this->_config_rules[$uri]))
346 {
347 $this->set_rules($this->_config_rules[$uri]);
348 }
349 else
350 {
351 $this->set_rules($this->_config_rules);
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // We're we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200355 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
357 log_message('debug', "Unable to find validation rules");
358 return FALSE;
359 }
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // Load the language file containing error messages
363 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200364
365 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // corresponding $_POST item and test for errors
367 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200368 {
JonoB099c4782012-03-04 14:37:30 +0000369 // Fetch the data from the corresponding $_POST or validation array and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // 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 +0200371
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200372 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
JonoB099c4782012-03-04 14:37:30 +0000374 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
376 else
377 {
JonoB099c4782012-03-04 14:37:30 +0000378 if (isset($validation_array[$field]) AND $validation_array[$field] != "")
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
JonoB099c4782012-03-04 14:37:30 +0000380 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 }
382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
384 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
386
387 // Did we end up with any errors?
388 $total_errors = count($this->_error_array);
389
390 if ($total_errors > 0)
391 {
392 $this->_safe_form_data = TRUE;
393 }
394
395 // Now we need to re-set the POST data with the new, processed data
396 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200397
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200398 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 }
400
401 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 /**
404 * Traverse a multidimensional $_POST array index until the data is found
405 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 * @param array
407 * @param array
408 * @param integer
409 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200410 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100411 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200413 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200415 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 return $array;
419 }
420
421 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 /**
424 * Re-populate the _POST array with our finalized and processed data
425 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200427 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100428 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
430 foreach ($this->_field_data as $field => $row)
431 {
432 if ( ! is_null($row['postdata']))
433 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200434 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
436 if (isset($_POST[$row['field']]))
437 {
438 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
439 }
440 }
441 else
442 {
Derek Jones63eeae32009-02-10 19:08:56 +0000443 // start with a reference
444 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Jones63eeae32009-02-10 19:08:56 +0000446 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200447 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
Derek Jones63eeae32009-02-10 19:08:56 +0000449 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
451 else
452 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 foreach ($row['keys'] as $val)
454 {
Derek Jones63eeae32009-02-10 19:08:56 +0000455 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
457 }
Derek Jones63eeae32009-02-10 19:08:56 +0000458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000460 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 $array = array();
462 foreach ($row['postdata'] as $k => $v)
463 {
464 $array[$k] = $this->prep_for_form($v);
465 }
Derek Jones63eeae32009-02-10 19:08:56 +0000466
467 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
469 else
Derek Jones63eeae32009-02-10 19:08:56 +0000470 {
471 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474 }
475 }
476 }
477
478 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 /**
481 * Executes the Validation routines
482 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @param array
484 * @param array
485 * @param mixed
486 * @param integer
487 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200488 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100489 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
491 // If the $_POST data is an array we will run a recursive call
492 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200493 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 foreach ($postdata as $key => $val)
495 {
496 $this->_execute($row, $rules, $val, $cycles);
497 $cycles++;
498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 return;
501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 // --------------------------------------------------------------------
504
505 // If the field is blank, but NOT required, no further tests are necessary
506 $callback = FALSE;
507 if ( ! in_array('required', $rules) AND is_null($postdata))
508 {
509 // Before we bail out, does the rule contain a callback?
Marcos Coelhoc28b2852011-07-05 12:59:41 -0700510 if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
512 $callback = TRUE;
513 $rules = (array('1' => $match[1]));
514 }
515 else
516 {
517 return;
518 }
519 }
520
521 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200524 if (is_null($postdata) AND $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
526 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
527 {
528 // Set the message type
529 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 if ( ! isset($this->_error_messages[$type]))
532 {
533 if (FALSE === ($line = $this->CI->lang->line($type)))
534 {
535 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200536 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
538 else
539 {
540 $line = $this->_error_messages[$type];
541 }
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 // Build the error message
544 $message = sprintf($line, $this->_translate_fieldname($row['label']));
545
546 // Save the error message
547 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 if ( ! isset($this->_error_array[$row['field']]))
550 {
551 $this->_error_array[$row['field']] = $message;
552 }
553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 return;
556 }
557
558 // --------------------------------------------------------------------
559
560 // Cycle through each rule and run it
561 foreach ($rules As $rule)
562 {
563 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 // We set the $postdata variable with the current data in our master array so that
566 // each cycle of the loop is dealing with the processed data from the last cycle
567 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
568 {
569 // We shouldn't need this safety, but just in case there isn't an array index
570 // associated with this cycle we'll bail out
571 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
572 {
573 continue;
574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
577 $_in_array = TRUE;
578 }
579 else
580 {
581 $postdata = $this->_field_data[$row['field']]['postdata'];
582 }
583
584 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200585
586 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 $callback = FALSE;
588 if (substr($rule, 0, 9) == 'callback_')
589 {
590 $rule = substr($rule, 9);
591 $callback = TRUE;
592 }
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 // Strip the parameter (if exists) from the rule
595 // Rules can contain a parameter: max_length[5]
596 $param = FALSE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500597 if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
599 $rule = $match[1];
600 $param = $match[2];
601 }
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 // Call the function that corresponds to the rule
604 if ($callback === TRUE)
605 {
606 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200607 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 continue;
609 }
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 // Run the function and grab the result
612 $result = $this->CI->$rule($postdata, $param);
613
614 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200615 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
617 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
618 }
619 else
620 {
621 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
622 }
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 // If the field isn't required and we just processed a callback we'll move on...
625 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
626 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000627 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 }
629 }
630 else
Barry Mienydd671972010-10-04 16:33:58 +0200631 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 if ( ! method_exists($this, $rule))
633 {
Barry Mienydd671972010-10-04 16:33:58 +0200634 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 // Users can use any native PHP function call that has one param.
636 if (function_exists($rule))
637 {
638 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200639
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200640 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
642 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
643 }
644 else
645 {
646 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
647 }
648 }
patwork02404a12011-04-08 15:45:46 +0200649 else
650 {
651 log_message('debug', "Unable to find validation rule: ".$rule);
652 }
Barry Mienydd671972010-10-04 16:33:58 +0200653
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 continue;
655 }
656
657 $result = $this->$rule($postdata, $param);
658
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200659 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
661 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
662 }
663 else
664 {
665 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
666 }
667 }
Barry Mienydd671972010-10-04 16:33:58 +0200668
Derek Jones37f4b9c2011-07-01 17:56:50 -0500669 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200671 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 if ( ! isset($this->_error_messages[$rule]))
673 {
674 if (FALSE === ($line = $this->CI->lang->line($rule)))
675 {
676 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200677 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 }
679 else
680 {
681 $line = $this->_error_messages[$rule];
682 }
Barry Mienydd671972010-10-04 16:33:58 +0200683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 // Is the parameter we are inserting into the error message the name
Derek Jones37f4b9c2011-07-01 17:56:50 -0500685 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200686 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
Pascal Krietec1895832009-10-13 12:56:43 +0000688 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
Barry Mienydd671972010-10-04 16:33:58 +0200690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 // Build the error message
692 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
693
694 // Save the error message
695 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 if ( ! isset($this->_error_array[$row['field']]))
698 {
699 $this->_error_array[$row['field']] = $message;
700 }
Barry Mienydd671972010-10-04 16:33:58 +0200701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 return;
703 }
704 }
705 }
706
707 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 /**
710 * Translate a field name
711 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 * @param string the field name
713 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200714 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100715 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 {
717 // Do we need to translate the field name?
718 // We look for the prefix lang: to determine this
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200719 if (substr($fieldname, 0, 5) === 'lang:')
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
721 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200722 $line = substr($fieldname, 5);
723
Derek Jones37f4b9c2011-07-01 17:56:50 -0500724 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
726 {
727 return $line;
728 }
729 }
730
731 return $fieldname;
732 }
733
734 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 /**
737 * Get the value from a form
738 *
739 * Permits you to repopulate a form field with the value it was submitted
740 * with, or, if that value doesn't exist, with the default
741 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 * @param string the field name
743 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200744 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200745 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100746 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200748 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 {
750 return $default;
751 }
Barry Mienydd671972010-10-04 16:33:58 +0200752
Phil Sturgeon5c561802011-01-05 16:31:59 +0000753 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600754 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000755 if (is_array($this->_field_data[$field]['postdata']))
756 {
757 return array_shift($this->_field_data[$field]['postdata']);
758 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 return $this->_field_data[$field]['postdata'];
761 }
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 /**
766 * Set Select
767 *
768 * Enables pull-down lists to be set to the value the user
769 * selected in the event of an error
770 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 * @param string
772 * @param string
773 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200774 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100775 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200776 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200777 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 {
Andrey Andreev0adff1b2012-02-28 14:36:40 +0200779 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 }
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200783
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 if (is_array($field))
785 {
786 if ( ! in_array($value, $field))
787 {
788 return '';
789 }
790 }
Andrey Andreev46ac8812012-02-28 14:32:54 +0200791 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200793 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 }
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 return ' selected="selected"';
797 }
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 * Set Radio
803 *
804 * Enables radio buttons to be set to the value the user
805 * selected in the event of an error
806 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 * @param string
808 * @param string
809 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200810 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100811 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200813 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200815 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 }
Barry Mienydd671972010-10-04 16:33:58 +0200817
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 if (is_array($field))
821 {
822 if ( ! in_array($value, $field))
823 {
824 return '';
825 }
826 }
827 else
828 {
829 if (($field == '' OR $value == '') OR ($field != $value))
830 {
831 return '';
832 }
833 }
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 return ' checked="checked"';
836 }
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200839
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 /**
841 * Set Checkbox
842 *
843 * Enables checkboxes to be set to the value the user
844 * selected in the event of an error
845 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 * @param string
847 * @param string
848 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200849 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100850 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200852 // Logic is exactly the same as for radio fields
853 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 }
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200857
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 /**
859 * Required
860 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 * @param string
862 * @return bool
863 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100864 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200866 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 }
Barry Mienydd671972010-10-04 16:33:58 +0200868
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500872 * Performs a Regular Expression match test.
873 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500874 * @param string
875 * @param regex
876 * @return bool
877 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100878 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500879 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200880 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500881 }
882
883 // --------------------------------------------------------------------
884
885 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 * Match one field to another
887 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 * @param string
889 * @param field
890 * @return bool
891 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100892 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 {
JonoB099c4782012-03-04 14:37:30 +0000894 $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST;
895 if ( ! isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
Barry Mienydd671972010-10-04 16:33:58 +0200897 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
JonoB099c4782012-03-04 14:37:30 +0000900 return ($str === $validation_array[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 }
Eric Barnescccde962011-12-04 00:01:17 -0500902
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100903 // --------------------------------------------------------------------
904
905 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200906 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100907 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200908 * Check if the input value doesn't already exist
909 * in the specified database field.
910 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100911 * @param string
912 * @param field
913 * @return bool
914 */
915 public function is_unique($str, $field)
916 {
Eric Barnescccde962011-12-04 00:01:17 -0500917 list($table, $field) = explode('.', $field);
918 if (isset($this->CI->db))
919 {
920 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
921 return $query->num_rows() === 0;
922 }
923 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600924 }
Barry Mienydd671972010-10-04 16:33:58 +0200925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200927
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 /**
929 * Minimum Length
930 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 * @param string
932 * @param value
933 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200934 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100935 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200937 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
939 return FALSE;
940 }
941
942 if (function_exists('mb_strlen'))
943 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200944 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200947 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 }
Barry Mienydd671972010-10-04 16:33:58 +0200949
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200951
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 /**
953 * Max Length
954 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 * @param string
956 * @param value
957 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200958 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100959 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200961 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 {
963 return FALSE;
964 }
965
966 if (function_exists('mb_strlen'))
967 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200968 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
Barry Mienydd671972010-10-04 16:33:58 +0200970
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200971 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200975
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 /**
977 * Exact Length
978 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 * @param string
980 * @param value
981 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200982 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100983 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200985 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 {
987 return FALSE;
988 }
989
990 if (function_exists('mb_strlen'))
991 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200992 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 }
Barry Mienydd671972010-10-04 16:33:58 +0200994
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200995 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 }
Barry Mienydd671972010-10-04 16:33:58 +0200997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 /**
1001 * Valid Email
1002 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 * @param string
1004 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001005 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001006 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001008 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 +00001009 }
1010
1011 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001012
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 /**
1014 * Valid Emails
1015 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 * @param string
1017 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001018 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001019 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 {
1021 if (strpos($str, ',') === FALSE)
1022 {
1023 return $this->valid_email(trim($str));
1024 }
Barry Mienydd671972010-10-04 16:33:58 +02001025
Pascal Kriete14287f32011-02-14 13:39:34 -05001026 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001028 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 {
1030 return FALSE;
1031 }
1032 }
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 return TRUE;
1035 }
1036
1037 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001038
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 /**
1040 * Validate IP Address
1041 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001043 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001045 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 {
1047 return $this->CI->input->valid_ip($ip);
1048 }
1049
1050 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001051
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 /**
1053 * Alpha
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($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001060 return (bool) preg_match('/^[a-z]+$/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 * Alpha-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 alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001073 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 }
Barry Mienydd671972010-10-04 16:33:58 +02001075
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 /**
1079 * Alpha-numeric with underscores and dashes
1080 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 * @param string
1082 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001083 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001084 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001086 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 }
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001090
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 /**
1092 * Numeric
1093 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 * @param string
1095 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001096 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001097 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001099 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001100
1101 }
1102
1103 // --------------------------------------------------------------------
1104
Barry Mienydd671972010-10-04 16:33:58 +02001105 /**
1106 * Is Numeric
1107 *
Barry Mienydd671972010-10-04 16:33:58 +02001108 * @param string
1109 * @return bool
1110 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001111 public function is_numeric($str)
Barry Mienydd671972010-10-04 16:33:58 +02001112 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001113 return is_numeric($str);
Barry Mienydd671972010-10-04 16:33:58 +02001114 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001115
1116 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 /**
1119 * Integer
1120 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 * @param string
1122 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001123 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001124 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001126 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1127 }
1128
1129 // --------------------------------------------------------------------
1130
1131 /**
1132 * Decimal number
1133 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001134 * @param string
1135 * @return bool
1136 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001137 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001138 {
1139 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1140 }
1141
1142 // --------------------------------------------------------------------
1143
1144 /**
1145 * Greather than
1146 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001147 * @param string
1148 * @return bool
1149 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001150 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001151 {
1152 if ( ! is_numeric($str))
1153 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001154 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001155 }
1156 return $str > $min;
1157 }
1158
1159 // --------------------------------------------------------------------
1160
1161 /**
1162 * Less than
1163 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001164 * @param string
1165 * @return bool
1166 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001167 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001168 {
1169 if ( ! is_numeric($str))
1170 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001171 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001172 }
1173 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001175
1176 // --------------------------------------------------------------------
1177
Barry Mienydd671972010-10-04 16:33:58 +02001178 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001179 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001180 *
Barry Mienydd671972010-10-04 16:33:58 +02001181 * @param string
1182 * @return bool
1183 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001184 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001185 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001186 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001187 }
1188
1189 // --------------------------------------------------------------------
1190
1191 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001192 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001193 *
Barry Mienydd671972010-10-04 16:33:58 +02001194 * @param string
1195 * @return bool
1196 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001197 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001198 {
Andrey Andreev46ac8812012-02-28 14:32:54 +02001199 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001200 }
1201
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001203
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 /**
1205 * Valid Base64
1206 *
1207 * Tests a string for characters outside of the Base64 alphabet
1208 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1209 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 * @param string
1211 * @return bool
1212 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001213 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 {
1215 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1216 }
Barry Mienydd671972010-10-04 16:33:58 +02001217
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001219
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 /**
1221 * Prep data for form
1222 *
1223 * This function allows HTML to be safely shown in a form.
1224 * Special characters are converted.
1225 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 * @param string
1227 * @return string
1228 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001229 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 {
1231 if (is_array($data))
1232 {
1233 foreach ($data as $key => $val)
1234 {
1235 $data[$key] = $this->prep_for_form($val);
1236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 return $data;
1239 }
Barry Mienydd671972010-10-04 16:33:58 +02001240
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 if ($this->_safe_form_data == FALSE OR $data === '')
1242 {
1243 return $data;
1244 }
1245
Andrey Andreev46ac8812012-02-28 14:32:54 +02001246 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 }
Barry Mienydd671972010-10-04 16:33:58 +02001248
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001250
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 /**
1252 * Prep URL
1253 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 * @param string
1255 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001256 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001257 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 {
1259 if ($str == 'http://' OR $str == '')
1260 {
1261 return '';
1262 }
Barry Mienydd671972010-10-04 16:33:58 +02001263
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001264 if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://')
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 {
1266 $str = 'http://'.$str;
1267 }
Barry Mienydd671972010-10-04 16:33:58 +02001268
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 return $str;
1270 }
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 * Strip Image Tags
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 strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 {
1282 return $this->CI->input->strip_image_tags($str);
1283 }
Barry Mienydd671972010-10-04 16:33:58 +02001284
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001286
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 /**
1288 * XSS Clean
1289 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 * @param string
1291 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001292 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001293 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 {
Derek Jones5640a712010-04-23 11:22:40 -05001295 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 }
Barry Mienydd671972010-10-04 16:33:58 +02001297
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001299
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 /**
1301 * Convert PHP tags to entities
1302 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 * @param string
1304 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001305 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001306 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001308 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 }
JonoB099c4782012-03-04 14:37:30 +00001310
1311 // --------------------------------------------------------------------
1312
1313 /**
1314 * Reset validation vars
1315 *
1316 * Prevents subsequent validation routines from being affected by the
1317 * results of any previous validation routine due to the CI singleton.
1318 *
1319 * @return void
1320 */
1321 protected function _reset_validation()
1322 {
1323 $this->_field_data = array();
1324 $this->_config_rules = array();
1325 $this->_error_array = array();
1326 $this->_error_messages = array();
1327 $this->error_string = '';
1328 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001329}
Derek Allard2067d1a2008-11-13 22:59:24 +00001330
1331/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001332/* Location: ./system/libraries/Form_validation.php */