blob: a52cad5ff784d947ab4612bb19fb77f0c6fb9f18 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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
Timothy Warren0688ac92012-04-20 10:25:04 -040039 /**
40 * Reference to the CodeIgniter instance
41 *
42 * @var object
43 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010044 protected $CI;
Timothy Warren0688ac92012-04-20 10:25:04 -040045
46 /**
47 * Validation data for the current form submission
48 *
49 * @var array
50 */
51 protected $_field_data = array();
52
53 /**
54 * Validation rules for the current form
55 *
56 * @var array
57 */
58 protected $_config_rules = array();
59
60 /**
61 * Array of validation errors
62 *
63 * @var array
64 */
Andrey Andreev78f55772012-04-03 19:59:08 +030065 protected $_error_array = array();
Timothy Warren0688ac92012-04-20 10:25:04 -040066
67 /**
68 * Array of custom error messages
69 *
70 * @var array
71 */
Andrey Andreev78f55772012-04-03 19:59:08 +030072 protected $_error_messages = array();
Timothy Warren0688ac92012-04-20 10:25:04 -040073
74 /**
75 * Start tag for error wrapping
76 *
77 * @var string
78 */
Andrey Andreev78f55772012-04-03 19:59:08 +030079 protected $_error_prefix = '<p>';
Timothy Warren0688ac92012-04-20 10:25:04 -040080
81 /**
82 * End tag for error wrapping
83 *
84 * @var string
85 */
Andrey Andreev78f55772012-04-03 19:59:08 +030086 protected $_error_suffix = '</p>';
Timothy Warren0688ac92012-04-20 10:25:04 -040087
88 /**
89 * Custom error message
90 *
91 * @var string
92 */
Andrey Andreev78f55772012-04-03 19:59:08 +030093 protected $error_string = '';
Timothy Warren0688ac92012-04-20 10:25:04 -040094
95 /**
96 * Whether the form data has been validated as safe
97 *
98 * @var bool
99 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300100 protected $_safe_form_data = FALSE;
Timothy Warren0688ac92012-04-20 10:25:04 -0400101
102 /**
103 * Custom data to validate
104 *
105 * @var array
106 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300107 protected $validation_data = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000108
Timothy Warren0688ac92012-04-20 10:25:04 -0400109 /**
110 * Initialize Form_Validation class
111 *
112 * @param array $rules
113 */
Greg Akera9263282010-11-10 15:26:43 -0600114 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +0200115 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200117
Mike Funk326a5e72012-02-24 10:06:28 -0500118 // applies delimiters set in config file.
Mike Funk7f42d062012-03-08 09:00:57 -0500119 if (isset($rules['error_prefix']))
120 {
121 $this->_error_prefix = $rules['error_prefix'];
122 unset($rules['error_prefix']);
123 }
124 if (isset($rules['error_suffix']))
125 {
126 $this->_error_suffix = $rules['error_suffix'];
127 unset($rules['error_suffix']);
128 }
Andrey Andreev31cf46e2012-03-20 15:48:00 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 // Validation rules can be stored in a config file.
131 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // Automatically load the form helper
134 $this->CI->load->helper('form');
135
136 // Set the character encoding in MB.
tiyowan5b9fd2d2012-03-12 20:26:59 +0400137 if (MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
139 mb_internal_encoding($this->CI->config->item('charset'));
140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Andrey Andreev901573c2012-01-11 01:40:48 +0200142 log_message('debug', 'Form Validation Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 /**
148 * Set Rules
149 *
150 * This function takes an array of field names and validation
151 * rules as input, validates the info, and stores it
152 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400153 * @param mixed $field
154 * @param string $label
155 * @param mixed $rules
Andrey Andreev78f55772012-04-03 19:59:08 +0300156 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100158 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
160 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +0000161 // or a validation array has not been specified
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200162 if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Greg Aker9f9af602010-11-10 15:41:51 -0600164 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
tiyowanc2acb232012-03-14 21:24:00 +0400167 // If an array was passed via the first parameter instead of individual string
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // values we cycle through it and recursively call this function.
169 if (is_array($field))
170 {
171 foreach ($field as $row)
172 {
173 // Houston, we have a problem...
Andrey Andreev78f55772012-04-03 19:59:08 +0300174 if ( ! isset($row['field'], $row['rules']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
176 continue;
177 }
178
179 // If the field label wasn't passed we use the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300180 $label = isset($row['label']) ? $row['label'] : $row['field'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000181
182 // Here we go!
183 $this->set_rules($row['field'], $label, $row['rules']);
184 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300185
Greg Aker9f9af602010-11-10 15:41:51 -0600186 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200190 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Greg Aker9f9af602010-11-10 15:41:51 -0600192 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
194
195 // If the field label wasn't passed we use the field name
196 $label = ($label == '') ? $field : $label;
197
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200198 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200199 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200200 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200201 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 // Note: Due to a bug in current() that affects some versions
203 // of PHP we can not pass function call directly into it
204 $x = explode('[', $field);
205 $indexes[] = current($x);
206
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200207 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200209 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200211 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 $is_array = TRUE;
216 }
217 else
218 {
Barry Mienydd671972010-10-04 16:33:58 +0200219 $indexes = array();
220 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
223 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000225 'field' => $field,
226 'label' => $label,
227 'rules' => $rules,
228 'is_array' => $is_array,
229 'keys' => $indexes,
230 'postdata' => NULL,
231 'error' => ''
232 );
Greg Aker9f9af602010-11-10 15:41:51 -0600233
234 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
236
237 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 /**
JonoB099c4782012-03-04 14:37:30 +0000240 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200241 *
JonoB099c4782012-03-04 14:37:30 +0000242 * If an array is set through this method, then this array will
243 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200244 *
245 * Note that if you are validating multiple arrays, then the
246 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000247 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200248 *
249 * @param array $data
250 * @return void
JonoB099c4782012-03-04 14:37:30 +0000251 */
252 public function set_data($data = '')
253 {
254 if ( ! empty($data) && is_array($data))
255 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200256 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000257 }
258 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200259
JonoB099c4782012-03-04 14:37:30 +0000260 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000261
262 /**
263 * Set Error Message
264 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300265 * Lets users set their own error messages on the fly. Note:
266 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300268 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300270 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100272 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
274 if ( ! is_array($lang))
275 {
276 $lang = array($lang => $val);
277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600280 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 /**
286 * Set The Error Delimiter
287 *
288 * Permits a prefix/suffix to be added to each error message
289 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 * @param string
291 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300292 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200293 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100294 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
296 $this->_error_prefix = $prefix;
297 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600298 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
300
301 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 /**
304 * Get Error Message
305 *
306 * Gets the error message associated with a particular field
307 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @param string the field name
Timothy Warren0688ac92012-04-20 10:25:04 -0400309 * @param string the html start tag
310 * @param strign the html end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300311 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200312 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100313 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200314 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300315 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
317 return '';
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 if ($prefix == '')
321 {
322 $prefix = $this->_error_prefix;
323 }
324
325 if ($suffix == '')
326 {
327 $suffix = $this->_error_suffix;
328 }
329
330 return $prefix.$this->_field_data[$field]['error'].$suffix;
331 }
332
333 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100336 * Get Array of Error Messages
337 *
338 * Returns the error messages as an array
339 *
340 * @return array
341 */
342 public function error_array()
343 {
344 return $this->_error_array;
345 }
346
347 // --------------------------------------------------------------------
348
349 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 * Error String
351 *
352 * Returns the error messages as a string, wrapped in the error delimiters
353 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 * @param string
355 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300356 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200357 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100358 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 // No errrors, validation passes!
361 if (count($this->_error_array) === 0)
362 {
363 return '';
364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 if ($prefix == '')
367 {
368 $prefix = $this->_error_prefix;
369 }
370
371 if ($suffix == '')
372 {
373 $suffix = $this->_error_suffix;
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // Generate the error string
377 $str = '';
378 foreach ($this->_error_array as $val)
379 {
380 if ($val != '')
381 {
382 $str .= $prefix.$val.$suffix."\n";
383 }
384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 return $str;
387 }
388
389 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 /**
392 * Run the Validator
393 *
394 * This function does all the work.
395 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400396 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200398 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100399 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500401 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300402 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000403 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
405 return FALSE;
406 }
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 // Does the _field_data array containing the validation rules exist?
409 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200410 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500412 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200413 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 return FALSE;
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // Is there a validation rule for the particular URI being accessed?
419 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200420
Andrey Andreev6de924c2012-01-20 13:18:18 +0200421 if ($uri != '' && isset($this->_config_rules[$uri]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 $this->set_rules($this->_config_rules[$uri]);
424 }
425 else
426 {
427 $this->set_rules($this->_config_rules);
428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
Andrey Andreev901573c2012-01-11 01:40:48 +0200430 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200431 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200433 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 return FALSE;
435 }
436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 // Load the language file containing error messages
439 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200440
441 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 // corresponding $_POST item and test for errors
443 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200444 {
JonoB099c4782012-03-04 14:37:30 +0000445 // 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 +0000446 // 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 +0200447 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
JonoB099c4782012-03-04 14:37:30 +0000449 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300451 elseif ( ! empty($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300453 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
456 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
458
459 // Did we end up with any errors?
460 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 if ($total_errors > 0)
462 {
463 $this->_safe_form_data = TRUE;
464 }
465
466 // Now we need to re-set the POST data with the new, processed data
467 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200468
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200469 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
471
472 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 /**
475 * Traverse a multidimensional $_POST array index until the data is found
476 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 * @param array
478 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300479 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200481 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100482 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200484 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200486 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 return $array;
490 }
491
492 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 /**
495 * Re-populate the _POST array with our finalized and processed data
496 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200497 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200498 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100499 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
501 foreach ($this->_field_data as $field => $row)
502 {
503 if ( ! is_null($row['postdata']))
504 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200505 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 {
507 if (isset($_POST[$row['field']]))
508 {
509 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
510 }
511 }
512 else
513 {
Derek Jones63eeae32009-02-10 19:08:56 +0000514 // start with a reference
515 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Jones63eeae32009-02-10 19:08:56 +0000517 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200518 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
Derek Jones63eeae32009-02-10 19:08:56 +0000520 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 }
522 else
523 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 foreach ($row['keys'] as $val)
525 {
Derek Jones63eeae32009-02-10 19:08:56 +0000526 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
528 }
Derek Jones63eeae32009-02-10 19:08:56 +0000529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000531 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 $array = array();
533 foreach ($row['postdata'] as $k => $v)
534 {
535 $array[$k] = $this->prep_for_form($v);
536 }
Derek Jones63eeae32009-02-10 19:08:56 +0000537
538 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 }
540 else
Derek Jones63eeae32009-02-10 19:08:56 +0000541 {
542 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
545 }
546 }
547 }
548
549 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 /**
552 * Executes the Validation routines
553 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * @param array
555 * @param array
556 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300557 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200559 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100560 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 {
562 // If the $_POST data is an array we will run a recursive call
563 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200564 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 foreach ($postdata as $key => $val)
566 {
567 $this->_execute($row, $rules, $val, $cycles);
568 $cycles++;
569 }
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 return;
572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 // If the field is blank, but NOT required, no further tests are necessary
575 $callback = FALSE;
Andrey Andreev6de924c2012-01-20 13:18:18 +0200576 if ( ! in_array('required', $rules) && is_null($postdata))
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
578 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200579 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
581 $callback = TRUE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300582 $rules = array(1 => $match[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 }
584 else
585 {
586 return;
587 }
588 }
589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev6de924c2012-01-20 13:18:18 +0200591 if (is_null($postdata) && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
594 {
595 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300596 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 if ( ! isset($this->_error_messages[$type]))
599 {
600 if (FALSE === ($line = $this->CI->lang->line($type)))
601 {
602 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200603 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 }
605 else
606 {
607 $line = $this->_error_messages[$type];
608 }
Barry Mienydd671972010-10-04 16:33:58 +0200609
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 // Build the error message
611 $message = sprintf($line, $this->_translate_fieldname($row['label']));
612
613 // Save the error message
614 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 if ( ! isset($this->_error_array[$row['field']]))
617 {
618 $this->_error_array[$row['field']] = $message;
619 }
620 }
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 return;
623 }
624
625 // --------------------------------------------------------------------
626
627 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300628 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 {
630 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 // We set the $postdata variable with the current data in our master array so that
633 // each cycle of the loop is dealing with the processed data from the last cycle
Andrey Andreev6de924c2012-01-20 13:18:18 +0200634 if ($row['is_array'] == TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
636 // We shouldn't need this safety, but just in case there isn't an array index
637 // associated with this cycle we'll bail out
638 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
639 {
640 continue;
641 }
Barry Mienydd671972010-10-04 16:33:58 +0200642
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
644 $_in_array = TRUE;
645 }
646 else
647 {
648 $postdata = $this->_field_data[$row['field']]['postdata'];
649 }
650
Barry Mienydd671972010-10-04 16:33:58 +0200651 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200653 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 {
655 $rule = substr($rule, 9);
656 $callback = TRUE;
657 }
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 // Strip the parameter (if exists) from the rule
660 // Rules can contain a parameter: max_length[5]
661 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200662 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
664 $rule = $match[1];
665 $param = $match[2];
666 }
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 // Call the function that corresponds to the rule
669 if ($callback === TRUE)
670 {
671 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200672 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200673 log_message('debug', 'Unable to find callback validation rule: '.$rule);
674 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200676 else
677 {
678 // Run the function and grab the result
679 $result = $this->CI->$rule($postdata, $param);
680 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000681
682 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200683 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300685 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 }
687 else
688 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300689 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200693 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000695 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 }
697 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300698 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200699 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300700 // If our own wrapper function doesn't exist we see if a native PHP function does.
701 // Users can use any native PHP function call that has one param.
702 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
Andrey Andreev320d37c2012-04-03 20:21:39 +0300704 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200705
Andrey Andreev78f55772012-04-03 19:59:08 +0300706 if ($_in_array === TRUE)
707 {
708 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 }
patwork02404a12011-04-08 15:45:46 +0200710 else
711 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300712 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200713 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200715 else
716 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300717 log_message('debug', 'Unable to find validation rule: '.$rule);
718 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200719 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300720 }
721 else
722 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 $result = $this->$rule($postdata, $param);
724
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200725 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300727 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 }
729 else
730 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300731 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 }
733 }
Barry Mienydd671972010-10-04 16:33:58 +0200734
Andrey Andreev901573c2012-01-11 01:40:48 +0200735 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200737 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 if ( ! isset($this->_error_messages[$rule]))
739 {
740 if (FALSE === ($line = $this->CI->lang->line($rule)))
741 {
742 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200743 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 }
745 else
746 {
747 $line = $this->_error_messages[$rule];
748 }
Barry Mienydd671972010-10-04 16:33:58 +0200749
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200751 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200752 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
Pascal Krietec1895832009-10-13 12:56:43 +0000754 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 }
Barry Mienydd671972010-10-04 16:33:58 +0200756
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 // Build the error message
758 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
759
760 // Save the error message
761 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 if ( ! isset($this->_error_array[$row['field']]))
764 {
765 $this->_error_array[$row['field']] = $message;
766 }
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 return;
769 }
770 }
771 }
772
773 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200774
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 /**
776 * Translate a field name
777 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 * @param string the field name
779 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200780 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100781 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 {
783 // Do we need to translate the field name?
784 // We look for the prefix lang: to determine this
Andrey Andreev901573c2012-01-11 01:40:48 +0200785 if (strpos($fieldname, 'lang:') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 {
787 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200788 $line = substr($fieldname, 5);
789
Derek Jones37f4b9c2011-07-01 17:56:50 -0500790 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
792 {
793 return $line;
794 }
795 }
796
797 return $fieldname;
798 }
799
800 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200801
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 /**
803 * Get the value from a form
804 *
805 * Permits you to repopulate a form field with the value it was submitted
806 * with, or, if that value doesn't exist, with the default
807 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 * @param string the field name
809 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200810 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200811 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100812 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200814 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 {
816 return $default;
817 }
Barry Mienydd671972010-10-04 16:33:58 +0200818
Phil Sturgeon5c561802011-01-05 16:31:59 +0000819 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600820 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000821 if (is_array($this->_field_data[$field]['postdata']))
822 {
823 return array_shift($this->_field_data[$field]['postdata']);
824 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 return $this->_field_data[$field]['postdata'];
827 }
Barry Mienydd671972010-10-04 16:33:58 +0200828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 /**
832 * Set Select
833 *
834 * Enables pull-down lists to be set to the value the user
835 * selected in the event of an error
836 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 * @param string
838 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400839 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200841 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100842 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200843 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200844 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200846 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 }
Barry Mienydd671972010-10-04 16:33:58 +0200848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 if (is_array($field))
851 {
852 if ( ! in_array($value, $field))
853 {
854 return '';
855 }
856 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200857 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200859 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 }
Barry Mienydd671972010-10-04 16:33:58 +0200861
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 return ' selected="selected"';
863 }
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 /**
868 * Set Radio
869 *
870 * Enables radio buttons to be set to the value the user
871 * selected in the event of an error
872 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 * @param string
874 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400875 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200877 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100878 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200880 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200882 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 if (is_array($field))
887 {
888 if ( ! in_array($value, $field))
889 {
890 return '';
891 }
892 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200893 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200895 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 }
Barry Mienydd671972010-10-04 16:33:58 +0200897
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 return ' checked="checked"';
899 }
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 /**
904 * Set Checkbox
905 *
906 * Enables checkboxes to be set to the value the user
907 * selected in the event of an error
908 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 * @param string
910 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400911 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200913 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100914 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200916 // Logic is exactly the same as for radio fields
917 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 }
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200921
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 /**
923 * Required
924 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 * @param string
926 * @return bool
927 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100928 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300930 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 }
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200934
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500936 * Performs a Regular Expression match test.
937 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500938 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300939 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500940 * @return bool
941 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100942 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500943 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200944 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500945 }
946
947 // --------------------------------------------------------------------
948
949 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 * Match one field to another
951 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300953 * @param string field
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 * @return bool
955 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100956 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300958 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000959 if ( ! isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 {
Barry Mienydd671972010-10-04 16:33:58 +0200961 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 }
Barry Mienydd671972010-10-04 16:33:58 +0200963
JonoB099c4782012-03-04 14:37:30 +0000964 return ($str === $validation_array[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Eric Barnescccde962011-12-04 00:01:17 -0500966
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100967 // --------------------------------------------------------------------
968
969 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200970 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100971 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200972 * Check if the input value doesn't already exist
973 * in the specified database field.
974 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100975 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300976 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100977 * @return bool
978 */
979 public function is_unique($str, $field)
980 {
Eric Barnescccde962011-12-04 00:01:17 -0500981 list($table, $field) = explode('.', $field);
982 if (isset($this->CI->db))
983 {
984 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
985 return $query->num_rows() === 0;
986 }
987 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 /**
993 * Minimum Length
994 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300996 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200998 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100999 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001001 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 {
1003 return FALSE;
1004 }
1005
Andrey Andreev78f55772012-04-03 19:59:08 +03001006 return (MB_ENABLED === TRUE)
1007 ? ($val <= mb_strlen($str))
1008 : ($val <= strlen(str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 }
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001012
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 /**
1014 * Max Length
1015 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001017 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001019 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001020 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001022 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
1024 return FALSE;
1025 }
1026
Andrey Andreev78f55772012-04-03 19:59:08 +03001027 return (MB_ENABLED === TRUE)
1028 ? ($val >= mb_strlen($str))
1029 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 }
Barry Mienydd671972010-10-04 16:33:58 +02001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 /**
1035 * Exact Length
1036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001038 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001040 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001041 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001043 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 {
1045 return FALSE;
1046 }
1047
Andrey Andreev78f55772012-04-03 19:59:08 +03001048 return (MB_ENABLED === TRUE)
1049 ? (mb_strlen($str) == $val)
1050 : (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 }
Barry Mienydd671972010-10-04 16:33:58 +02001052
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 /**
1056 * Valid Email
1057 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 * @param string
1059 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001060 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001061 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001063 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 +00001064 }
1065
1066 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001067
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 /**
1069 * Valid Emails
1070 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 * @param string
1072 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001073 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001074 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 {
1076 if (strpos($str, ',') === FALSE)
1077 {
1078 return $this->valid_email(trim($str));
1079 }
Barry Mienydd671972010-10-04 16:33:58 +02001080
Pascal Kriete14287f32011-02-14 13:39:34 -05001081 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001083 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 {
1085 return FALSE;
1086 }
1087 }
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 return TRUE;
1090 }
1091
1092 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001093
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 /**
1095 * Validate IP Address
1096 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001098 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001100 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 {
1102 return $this->CI->input->valid_ip($ip);
1103 }
1104
1105 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001106
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 /**
1108 * Alpha
1109 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 * @param string
1111 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001112 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001113 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001115 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 }
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001119
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 /**
1121 * Alpha-numeric
1122 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 * @param string
1124 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001125 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001126 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001128 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 }
Barry Mienydd671972010-10-04 16:33:58 +02001130
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 * Alpha-numeric with underscores and dashes
1135 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 * @param string
1137 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001138 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001139 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001141 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 }
Barry Mienydd671972010-10-04 16:33:58 +02001143
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001145
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 /**
1147 * Numeric
1148 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 * @param string
1150 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001151 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001152 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001154 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001155
1156 }
1157
1158 // --------------------------------------------------------------------
1159
Barry Mienydd671972010-10-04 16:33:58 +02001160 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 * Integer
1162 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 * @param string
1164 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001165 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001166 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001168 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1169 }
1170
1171 // --------------------------------------------------------------------
1172
1173 /**
1174 * Decimal number
1175 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001176 * @param string
1177 * @return bool
1178 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001179 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001180 {
1181 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1182 }
1183
1184 // --------------------------------------------------------------------
1185
1186 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001187 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001188 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001189 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001190 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001191 * @return bool
1192 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001193 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001194 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001195 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001196 }
1197
1198 // --------------------------------------------------------------------
1199
1200 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001201 * Equal to or Greater than
1202 *
Nick Busey98c347d2012-02-02 11:07:03 -07001203 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001204 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001205 * @return bool
1206 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001207 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001208 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001209 return is_numeric($str) ? ($str >= $min) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001210 }
1211
1212 // --------------------------------------------------------------------
Phil Sturgeonef112c02011-02-07 13:01:47 +00001213
1214 /**
1215 * Less than
1216 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001217 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001218 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001219 * @return bool
1220 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001221 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001222 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001223 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001225
1226 // --------------------------------------------------------------------
1227
Barry Mienydd671972010-10-04 16:33:58 +02001228 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001229 * Equal to or Less than
1230 *
Nick Busey98c347d2012-02-02 11:07:03 -07001231 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001232 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001233 * @return bool
1234 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001235 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001236 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001237 return is_numeric($str) ? ($str <= $max) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001238 }
1239
1240 // --------------------------------------------------------------------
1241
1242 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001243 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001244 *
Barry Mienydd671972010-10-04 16:33:58 +02001245 * @param string
1246 * @return bool
1247 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001248 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001249 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001250 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001251 }
1252
1253 // --------------------------------------------------------------------
1254
1255 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001256 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001257 *
Barry Mienydd671972010-10-04 16:33:58 +02001258 * @param string
1259 * @return bool
1260 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001261 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001262 {
Andrey Andreev6de924c2012-01-20 13:18:18 +02001263 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001264 }
1265
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001267
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 /**
1269 * Valid Base64
1270 *
1271 * Tests a string for characters outside of the Base64 alphabet
1272 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1273 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 * @param string
1275 * @return bool
1276 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001277 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 {
1279 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1280 }
Barry Mienydd671972010-10-04 16:33:58 +02001281
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001283
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 /**
1285 * Prep data for form
1286 *
1287 * This function allows HTML to be safely shown in a form.
1288 * Special characters are converted.
1289 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 * @param string
1291 * @return string
1292 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001293 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 {
1295 if (is_array($data))
1296 {
1297 foreach ($data as $key => $val)
1298 {
1299 $data[$key] = $this->prep_for_form($val);
1300 }
Barry Mienydd671972010-10-04 16:33:58 +02001301
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 return $data;
1303 }
Barry Mienydd671972010-10-04 16:33:58 +02001304
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 if ($this->_safe_form_data == FALSE OR $data === '')
1306 {
1307 return $data;
1308 }
1309
Andrey Andreev901573c2012-01-11 01:40:48 +02001310 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 }
Barry Mienydd671972010-10-04 16:33:58 +02001312
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001314
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 /**
1316 * Prep URL
1317 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 * @param string
1319 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001320 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001321 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001323 if ($str === 'http://' OR $str == '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 {
1325 return '';
1326 }
Barry Mienydd671972010-10-04 16:33:58 +02001327
Andrey Andreev901573c2012-01-11 01:40:48 +02001328 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001330 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 }
Barry Mienydd671972010-10-04 16:33:58 +02001332
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 return $str;
1334 }
Barry Mienydd671972010-10-04 16:33:58 +02001335
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001337
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 /**
1339 * Strip Image Tags
1340 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 * @param string
1342 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001343 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001344 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 {
1346 return $this->CI->input->strip_image_tags($str);
1347 }
Barry Mienydd671972010-10-04 16:33:58 +02001348
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001350
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 /**
1352 * XSS Clean
1353 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 * @param string
1355 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001356 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001357 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 {
Derek Jones5640a712010-04-23 11:22:40 -05001359 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 }
Barry Mienydd671972010-10-04 16:33:58 +02001361
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001363
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 /**
1365 * Convert PHP tags to entities
1366 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 * @param string
1368 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001369 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001370 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001372 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 }
1374
JonoB099c4782012-03-04 14:37:30 +00001375 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001376
1377 /**
1378 * Reset validation vars
1379 *
1380 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001381 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001382 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001383 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001384 */
JonoB883f80f2012-03-05 09:51:27 +00001385 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001386 {
JonoB099c4782012-03-04 14:37:30 +00001387 $this->_field_data = array();
1388 $this->_config_rules = array();
1389 $this->_error_array = array();
1390 $this->_error_messages = array();
1391 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001392 }
1393
Derek Allard2067d1a2008-11-13 22:59:24 +00001394}
Derek Allard2067d1a2008-11-13 22:59:24 +00001395
1396/* End of file Form_validation.php */
Andrey Andreev31cf46e2012-03-20 15:48:00 +02001397/* Location: ./system/libraries/Form_validation.php */