blob: f2f3712d97c8648d0f6d43b5912cc16120b808c8 [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();
Derek Allard2067d1a2008-11-13 22:59:24 +000051
Greg Akera9263282010-11-10 15:26:43 -060052 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +020053 {
Derek Allard2067d1a2008-11-13 22:59:24 +000054 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020055
Mike Funk326a5e72012-02-24 10:06:28 -050056 // applies delimiters set in config file.
57 $this->_config_delimiters();
58
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 }
Mike Funk326a5e72012-02-24 10:06:28 -050073
74 // --------------------------------------------------------------------
75
76 /**
77 * if prefixes/suffixes set in config, assign and unset.
78 *
Mike Funka90b1f22012-02-28 13:44:19 -050079 * @return void
Mike Funk326a5e72012-02-24 10:06:28 -050080 */
Mike Funk1ccdb9a2012-02-28 13:32:19 -050081 protected function _config_delimiters()
Mike Funk326a5e72012-02-24 10:06:28 -050082 {
83 if (isset($rules['error_prefix']))
84 {
Mike Funk9af33372012-02-28 13:37:56 -050085 $this->_error_prefix = $rules['error_prefix'];
86 unset($rules['error_prefix']);
Mike Funk326a5e72012-02-24 10:06:28 -050087 }
88 if (isset($rules['error_suffix']))
89 {
Mike Funk9af33372012-02-28 13:37:56 -050090 $this->_error_suffix = $rules['error_suffix'];
91 unset($rules['error_suffix']);
Mike Funk326a5e72012-02-24 10:06:28 -050092 }
93 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 /**
98 * Set Rules
99 *
100 * This function takes an array of field names and validation
101 * rules as input, validates the info, and stores it
102 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 * @param mixed
104 * @param string
105 * @return void
106 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100107 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
109 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +0000110 // or a validation array has not been specified
111 if (count($_POST) === 0 && count($this->validation_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
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 // If an array was passed via the first parameter instead of indidual string
117 // values we cycle through it and recursively call this function.
118 if (is_array($field))
119 {
120 foreach ($field as $row)
121 {
122 // Houston, we have a problem...
123 if ( ! isset($row['field']) OR ! isset($row['rules']))
124 {
125 continue;
126 }
127
128 // If the field label wasn't passed we use the field name
129 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
130
131 // Here we go!
132 $this->set_rules($row['field'], $label, $row['rules']);
133 }
Greg Aker9f9af602010-11-10 15:41:51 -0600134 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200138 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Greg Aker9f9af602010-11-10 15:41:51 -0600140 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
142
143 // If the field label wasn't passed we use the field name
144 $label = ($label == '') ? $field : $label;
145
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200146 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200147 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200148 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200149 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 // Note: Due to a bug in current() that affects some versions
151 // of PHP we can not pass function call directly into it
152 $x = explode('[', $field);
153 $indexes[] = current($x);
154
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200155 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200157 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200159 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 $is_array = TRUE;
164 }
165 else
166 {
Barry Mienydd671972010-10-04 16:33:58 +0200167 $indexes = array();
168 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
171 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000173 'field' => $field,
174 'label' => $label,
175 'rules' => $rules,
176 'is_array' => $is_array,
177 'keys' => $indexes,
178 'postdata' => NULL,
179 'error' => ''
180 );
Greg Aker9f9af602010-11-10 15:41:51 -0600181
182 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
184
185 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 /**
JonoB099c4782012-03-04 14:37:30 +0000188 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200189 *
JonoB099c4782012-03-04 14:37:30 +0000190 * If an array is set through this method, then this array will
191 * be used instead of the $_POST array
JonoB883f80f2012-03-05 09:51:27 +0000192 *
193 * Note that if you are validating multiple arrays, then the
194 * reset_validation() function should be called after validating
195 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200196 *
197 * @param array $data
198 * @return void
JonoB099c4782012-03-04 14:37:30 +0000199 */
200 public function set_data($data = '')
201 {
202 if ( ! empty($data) && is_array($data))
203 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200204 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000205 }
206 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200207
JonoB099c4782012-03-04 14:37:30 +0000208 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000209
210 /**
211 * Set Error Message
212 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500213 * Lets users set their own error messages on the fly. Note: The key
JonoB099c4782012-03-04 14:37:30 +0000214 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 * @param string
217 * @param string
218 * @return string
219 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100220 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
222 if ( ! is_array($lang))
223 {
224 $lang = array($lang => $val);
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 $this->_error_messages = array_merge($this->_error_messages, $lang);
Phil Sturgeonc3828712011-01-19 12:31:47 +0000228
Greg Aker9f9af602010-11-10 15:41:51 -0600229 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 /**
235 * Set The Error Delimiter
236 *
237 * Permits a prefix/suffix to be added to each error message
238 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 * @param string
240 * @param string
241 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200242 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100243 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
245 $this->_error_prefix = $prefix;
246 $this->_error_suffix = $suffix;
Phil Sturgeonc3828712011-01-19 12:31:47 +0000247
Greg Aker9f9af602010-11-10 15:41:51 -0600248 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 }
250
251 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 /**
254 * Get Error Message
255 *
256 * Gets the error message associated with a particular field
257 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * @param string the field name
259 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200260 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100261 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200262 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
264 {
265 return '';
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 if ($prefix == '')
269 {
270 $prefix = $this->_error_prefix;
271 }
272
273 if ($suffix == '')
274 {
275 $suffix = $this->_error_suffix;
276 }
277
278 return $prefix.$this->_field_data[$field]['error'].$suffix;
279 }
280
281 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100284 * Get Array of Error Messages
285 *
286 * Returns the error messages as an array
287 *
288 * @return array
289 */
290 public function error_array()
291 {
292 return $this->_error_array;
293 }
294
295 // --------------------------------------------------------------------
296
297 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 * Error String
299 *
300 * Returns the error messages as a string, wrapped in the error delimiters
301 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 * @param string
303 * @param string
304 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200305 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100306 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
308 // No errrors, validation passes!
309 if (count($this->_error_array) === 0)
310 {
311 return '';
312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 if ($prefix == '')
315 {
316 $prefix = $this->_error_prefix;
317 }
318
319 if ($suffix == '')
320 {
321 $suffix = $this->_error_suffix;
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // Generate the error string
325 $str = '';
326 foreach ($this->_error_array as $val)
327 {
328 if ($val != '')
329 {
330 $str .= $prefix.$val.$suffix."\n";
331 }
332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 return $str;
335 }
336
337 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 /**
340 * Run the Validator
341 *
342 * This function does all the work.
343 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200345 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100346 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500348 // Do we even have any data to process? Mm?
JonoB099c4782012-03-04 14:37:30 +0000349 $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST;
350 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
352 return FALSE;
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // Does the _field_data array containing the validation rules exist?
356 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200357 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500359 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200360 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 return FALSE;
363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // Is there a validation rule for the particular URI being accessed?
366 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 if ($uri != '' AND isset($this->_config_rules[$uri]))
369 {
370 $this->set_rules($this->_config_rules[$uri]);
371 }
372 else
373 {
374 $this->set_rules($this->_config_rules);
375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // We're we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200378 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 log_message('debug', "Unable to find validation rules");
381 return FALSE;
382 }
383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // Load the language file containing error messages
386 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200387
388 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // corresponding $_POST item and test for errors
390 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200391 {
JonoB099c4782012-03-04 14:37:30 +0000392 // 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 +0000393 // 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 +0200394
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200395 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
JonoB099c4782012-03-04 14:37:30 +0000397 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
399 else
400 {
JonoB099c4782012-03-04 14:37:30 +0000401 if (isset($validation_array[$field]) AND $validation_array[$field] != "")
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
JonoB099c4782012-03-04 14:37:30 +0000403 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
407 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
409
410 // Did we end up with any errors?
411 $total_errors = count($this->_error_array);
412
413 if ($total_errors > 0)
414 {
415 $this->_safe_form_data = TRUE;
416 }
417
418 // Now we need to re-set the POST data with the new, processed data
419 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200420
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200421 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
423
424 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 /**
427 * Traverse a multidimensional $_POST array index until the data is found
428 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * @param array
430 * @param array
431 * @param integer
432 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200433 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100434 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200436 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200438 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 return $array;
442 }
443
444 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 /**
447 * Re-populate the _POST array with our finalized and processed data
448 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200450 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100451 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 foreach ($this->_field_data as $field => $row)
454 {
455 if ( ! is_null($row['postdata']))
456 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200457 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
459 if (isset($_POST[$row['field']]))
460 {
461 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
462 }
463 }
464 else
465 {
Derek Jones63eeae32009-02-10 19:08:56 +0000466 // start with a reference
467 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Jones63eeae32009-02-10 19:08:56 +0000469 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200470 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
Derek Jones63eeae32009-02-10 19:08:56 +0000472 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474 else
475 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 foreach ($row['keys'] as $val)
477 {
Derek Jones63eeae32009-02-10 19:08:56 +0000478 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480 }
Derek Jones63eeae32009-02-10 19:08:56 +0000481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000483 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 $array = array();
485 foreach ($row['postdata'] as $k => $v)
486 {
487 $array[$k] = $this->prep_for_form($v);
488 }
Derek Jones63eeae32009-02-10 19:08:56 +0000489
490 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
492 else
Derek Jones63eeae32009-02-10 19:08:56 +0000493 {
494 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
497 }
498 }
499 }
500
501 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 /**
504 * Executes the Validation routines
505 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * @param array
507 * @param array
508 * @param mixed
509 * @param integer
510 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200511 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100512 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
514 // If the $_POST data is an array we will run a recursive call
515 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200516 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 foreach ($postdata as $key => $val)
518 {
519 $this->_execute($row, $rules, $val, $cycles);
520 $cycles++;
521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 return;
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 // --------------------------------------------------------------------
527
528 // If the field is blank, but NOT required, no further tests are necessary
529 $callback = FALSE;
530 if ( ! in_array('required', $rules) AND is_null($postdata))
531 {
532 // Before we bail out, does the rule contain a callback?
Marcos Coelhoc28b2852011-07-05 12:59:41 -0700533 if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 {
535 $callback = TRUE;
536 $rules = (array('1' => $match[1]));
537 }
538 else
539 {
540 return;
541 }
542 }
543
544 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200547 if (is_null($postdata) AND $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
549 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
550 {
551 // Set the message type
552 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 if ( ! isset($this->_error_messages[$type]))
555 {
556 if (FALSE === ($line = $this->CI->lang->line($type)))
557 {
558 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200559 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 }
561 else
562 {
563 $line = $this->_error_messages[$type];
564 }
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 // Build the error message
567 $message = sprintf($line, $this->_translate_fieldname($row['label']));
568
569 // Save the error message
570 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 if ( ! isset($this->_error_array[$row['field']]))
573 {
574 $this->_error_array[$row['field']] = $message;
575 }
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 return;
579 }
580
581 // --------------------------------------------------------------------
582
583 // Cycle through each rule and run it
584 foreach ($rules As $rule)
585 {
586 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200587
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 // We set the $postdata variable with the current data in our master array so that
589 // each cycle of the loop is dealing with the processed data from the last cycle
590 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
591 {
592 // We shouldn't need this safety, but just in case there isn't an array index
593 // associated with this cycle we'll bail out
594 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
595 {
596 continue;
597 }
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
600 $_in_array = TRUE;
601 }
602 else
603 {
604 $postdata = $this->_field_data[$row['field']]['postdata'];
605 }
606
607 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200608
609 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 $callback = FALSE;
611 if (substr($rule, 0, 9) == 'callback_')
612 {
613 $rule = substr($rule, 9);
614 $callback = TRUE;
615 }
Barry Mienydd671972010-10-04 16:33:58 +0200616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 // Strip the parameter (if exists) from the rule
618 // Rules can contain a parameter: max_length[5]
619 $param = FALSE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500620 if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
622 $rule = $match[1];
623 $param = $match[2];
624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 // Call the function that corresponds to the rule
627 if ($callback === TRUE)
628 {
629 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200630 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 continue;
632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 // Run the function and grab the result
635 $result = $this->CI->$rule($postdata, $param);
636
637 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200638 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
640 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
641 }
642 else
643 {
644 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
645 }
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 // If the field isn't required and we just processed a callback we'll move on...
648 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
649 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000650 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 }
652 }
653 else
Barry Mienydd671972010-10-04 16:33:58 +0200654 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 if ( ! method_exists($this, $rule))
656 {
Barry Mienydd671972010-10-04 16:33:58 +0200657 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 // Users can use any native PHP function call that has one param.
659 if (function_exists($rule))
660 {
661 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200662
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200663 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 {
665 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
666 }
667 else
668 {
669 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
670 }
671 }
patwork02404a12011-04-08 15:45:46 +0200672 else
673 {
674 log_message('debug', "Unable to find validation rule: ".$rule);
675 }
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 continue;
678 }
679
680 $result = $this->$rule($postdata, $param);
681
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200682 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
684 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
685 }
686 else
687 {
688 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
689 }
690 }
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Jones37f4b9c2011-07-01 17:56:50 -0500692 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200694 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 if ( ! isset($this->_error_messages[$rule]))
696 {
697 if (FALSE === ($line = $this->CI->lang->line($rule)))
698 {
699 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200700 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 }
702 else
703 {
704 $line = $this->_error_messages[$rule];
705 }
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 // Is the parameter we are inserting into the error message the name
Derek Jones37f4b9c2011-07-01 17:56:50 -0500708 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200709 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
Pascal Krietec1895832009-10-13 12:56:43 +0000711 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 }
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 // Build the error message
715 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
716
717 // Save the error message
718 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 if ( ! isset($this->_error_array[$row['field']]))
721 {
722 $this->_error_array[$row['field']] = $message;
723 }
Barry Mienydd671972010-10-04 16:33:58 +0200724
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 return;
726 }
727 }
728 }
729
730 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 /**
733 * Translate a field name
734 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 * @param string the field name
736 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200737 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100738 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
740 // Do we need to translate the field name?
741 // We look for the prefix lang: to determine this
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200742 if (substr($fieldname, 0, 5) === 'lang:')
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 {
744 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200745 $line = substr($fieldname, 5);
746
Derek Jones37f4b9c2011-07-01 17:56:50 -0500747 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
749 {
750 return $line;
751 }
752 }
753
754 return $fieldname;
755 }
756
757 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200758
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 /**
760 * Get the value from a form
761 *
762 * Permits you to repopulate a form field with the value it was submitted
763 * with, or, if that value doesn't exist, with the default
764 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 * @param string the field name
766 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200767 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200768 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100769 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200771 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 {
773 return $default;
774 }
Barry Mienydd671972010-10-04 16:33:58 +0200775
Phil Sturgeon5c561802011-01-05 16:31:59 +0000776 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600777 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000778 if (is_array($this->_field_data[$field]['postdata']))
779 {
780 return array_shift($this->_field_data[$field]['postdata']);
781 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000782
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 return $this->_field_data[$field]['postdata'];
784 }
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200787
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 /**
789 * Set Select
790 *
791 * Enables pull-down lists to be set to the value the user
792 * selected in the event of an error
793 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 * @param string
795 * @param string
796 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200797 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100798 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200799 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200800 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Andrey Andreev0adff1b2012-02-28 14:36:40 +0200802 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 }
Barry Mienydd671972010-10-04 16:33:58 +0200804
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 if (is_array($field))
808 {
809 if ( ! in_array($value, $field))
810 {
811 return '';
812 }
813 }
Andrey Andreev46ac8812012-02-28 14:32:54 +0200814 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200816 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 }
Barry Mienydd671972010-10-04 16:33:58 +0200818
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 return ' selected="selected"';
820 }
Barry Mienydd671972010-10-04 16:33:58 +0200821
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 /**
825 * Set Radio
826 *
827 * Enables radio buttons to be set to the value the user
828 * selected in the event of an error
829 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 * @param string
831 * @param string
832 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200833 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100834 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200836 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200838 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 }
Barry Mienydd671972010-10-04 16:33:58 +0200840
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 if (is_array($field))
844 {
845 if ( ! in_array($value, $field))
846 {
847 return '';
848 }
849 }
850 else
851 {
852 if (($field == '' OR $value == '') OR ($field != $value))
853 {
854 return '';
855 }
856 }
Barry Mienydd671972010-10-04 16:33:58 +0200857
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 return ' checked="checked"';
859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 /**
864 * Set Checkbox
865 *
866 * Enables checkboxes to be set to the value the user
867 * selected in the event of an error
868 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 * @param string
870 * @param string
871 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200872 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100873 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200875 // Logic is exactly the same as for radio fields
876 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 }
Barry Mienydd671972010-10-04 16:33:58 +0200878
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 /**
882 * Required
883 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * @param string
885 * @return bool
886 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100887 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200889 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 }
Barry Mienydd671972010-10-04 16:33:58 +0200891
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500895 * Performs a Regular Expression match test.
896 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500897 * @param string
898 * @param regex
899 * @return bool
900 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100901 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500902 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200903 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500904 }
905
906 // --------------------------------------------------------------------
907
908 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 * Match one field to another
910 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 * @param string
912 * @param field
913 * @return bool
914 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100915 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200917 $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST;
JonoB099c4782012-03-04 14:37:30 +0000918 if ( ! isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 {
Barry Mienydd671972010-10-04 16:33:58 +0200920 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 }
Barry Mienydd671972010-10-04 16:33:58 +0200922
JonoB099c4782012-03-04 14:37:30 +0000923 return ($str === $validation_array[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 }
Eric Barnescccde962011-12-04 00:01:17 -0500925
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100926 // --------------------------------------------------------------------
927
928 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200929 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100930 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200931 * Check if the input value doesn't already exist
932 * in the specified database field.
933 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100934 * @param string
935 * @param field
936 * @return bool
937 */
938 public function is_unique($str, $field)
939 {
Eric Barnescccde962011-12-04 00:01:17 -0500940 list($table, $field) = explode('.', $field);
941 if (isset($this->CI->db))
942 {
943 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
944 return $query->num_rows() === 0;
945 }
946 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600947 }
Barry Mienydd671972010-10-04 16:33:58 +0200948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200950
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 /**
952 * Minimum Length
953 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 * @param string
955 * @param value
956 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200957 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100958 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200960 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
962 return FALSE;
963 }
964
965 if (function_exists('mb_strlen'))
966 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200967 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 }
Barry Mienydd671972010-10-04 16:33:58 +0200969
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200970 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 }
Barry Mienydd671972010-10-04 16:33:58 +0200972
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 /**
976 * Max Length
977 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 * @param string
979 * @param value
980 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200981 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100982 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200984 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
986 return FALSE;
987 }
988
989 if (function_exists('mb_strlen'))
990 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200991 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 }
Barry Mienydd671972010-10-04 16:33:58 +0200993
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200994 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 }
Barry Mienydd671972010-10-04 16:33:58 +0200996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 /**
1000 * Exact Length
1001 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 * @param string
1003 * @param value
1004 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001005 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001006 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 {
Andrey Andreev46ac8812012-02-28 14:32:54 +02001008 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 {
1010 return FALSE;
1011 }
1012
1013 if (function_exists('mb_strlen'))
1014 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001015 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001018 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 }
Barry Mienydd671972010-10-04 16:33:58 +02001020
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001022
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 /**
1024 * Valid Email
1025 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 * @param string
1027 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001028 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001029 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001031 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 +00001032 }
1033
1034 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001035
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 /**
1037 * Valid Emails
1038 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 * @param string
1040 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001041 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001042 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 {
1044 if (strpos($str, ',') === FALSE)
1045 {
1046 return $this->valid_email(trim($str));
1047 }
Barry Mienydd671972010-10-04 16:33:58 +02001048
Pascal Kriete14287f32011-02-14 13:39:34 -05001049 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001051 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 {
1053 return FALSE;
1054 }
1055 }
Barry Mienydd671972010-10-04 16:33:58 +02001056
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 return TRUE;
1058 }
1059
1060 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001061
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 /**
1063 * Validate IP Address
1064 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001066 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001068 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 {
1070 return $this->CI->input->valid_ip($ip);
1071 }
1072
1073 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001074
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 /**
1076 * Alpha
1077 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 * @param string
1079 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001080 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001081 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001083 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 }
Barry Mienydd671972010-10-04 16:33:58 +02001085
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001087
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 /**
1089 * Alpha-numeric
1090 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 * @param string
1092 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001093 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001094 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001096 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 }
Barry Mienydd671972010-10-04 16:33:58 +02001098
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001100
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 /**
1102 * Alpha-numeric with underscores and dashes
1103 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 * @param string
1105 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001106 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001107 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001109 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 }
Barry Mienydd671972010-10-04 16:33:58 +02001111
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001113
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 /**
1115 * Numeric
1116 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 * @param string
1118 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001119 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001120 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001122 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001123
1124 }
1125
1126 // --------------------------------------------------------------------
1127
Barry Mienydd671972010-10-04 16:33:58 +02001128 /**
1129 * Is Numeric
1130 *
Barry Mienydd671972010-10-04 16:33:58 +02001131 * @param string
1132 * @return bool
1133 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001134 public function is_numeric($str)
Barry Mienydd671972010-10-04 16:33:58 +02001135 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001136 return is_numeric($str);
Barry Mienydd671972010-10-04 16:33:58 +02001137 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001138
1139 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001140
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 /**
1142 * Integer
1143 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 * @param string
1145 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001146 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001147 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001149 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1150 }
1151
1152 // --------------------------------------------------------------------
1153
1154 /**
1155 * Decimal number
1156 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001157 * @param string
1158 * @return bool
1159 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001160 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001161 {
1162 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1163 }
1164
1165 // --------------------------------------------------------------------
1166
1167 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001168 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001169 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001170 * @param string
1171 * @return bool
1172 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001173 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001174 {
1175 if ( ! is_numeric($str))
1176 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001177 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001178 }
1179 return $str > $min;
1180 }
1181
1182 // --------------------------------------------------------------------
Nick Busey98c347d2012-02-02 11:07:03 -07001183
1184 /**
1185 * Equal to or Greater than
1186 *
1187 * @access public
1188 * @param string
1189 * @return bool
1190 */
Nick Buseyc1931662012-02-06 17:55:58 -07001191 function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001192 {
1193 if ( ! is_numeric($str))
1194 {
1195 return FALSE;
1196 }
1197 return $str >= $min;
1198 }
1199
1200 // --------------------------------------------------------------------
Phil Sturgeonef112c02011-02-07 13:01:47 +00001201
1202 /**
1203 * Less than
1204 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001205 * @param string
1206 * @return bool
1207 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001208 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001209 {
1210 if ( ! is_numeric($str))
1211 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001212 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001213 }
1214 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001216
1217 // --------------------------------------------------------------------
1218
Barry Mienydd671972010-10-04 16:33:58 +02001219 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001220 * Equal to or Less than
1221 *
1222 * @access public
1223 * @param string
1224 * @return bool
1225 */
Nick Buseyc1931662012-02-06 17:55:58 -07001226 function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001227 {
1228 if ( ! is_numeric($str))
1229 {
1230 return FALSE;
1231 }
1232 return $str <= $max;
1233 }
1234
1235 // --------------------------------------------------------------------
1236
1237 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001238 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001239 *
Barry Mienydd671972010-10-04 16:33:58 +02001240 * @param string
1241 * @return bool
1242 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001243 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001244 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001245 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001246 }
1247
1248 // --------------------------------------------------------------------
1249
1250 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001251 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001252 *
Barry Mienydd671972010-10-04 16:33:58 +02001253 * @param string
1254 * @return bool
1255 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001256 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001257 {
Andrey Andreev46ac8812012-02-28 14:32:54 +02001258 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001259 }
1260
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 /**
1264 * Valid Base64
1265 *
1266 * Tests a string for characters outside of the Base64 alphabet
1267 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1268 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 * @param string
1270 * @return bool
1271 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001272 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 {
1274 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1275 }
Barry Mienydd671972010-10-04 16:33:58 +02001276
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001278
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 /**
1280 * Prep data for form
1281 *
1282 * This function allows HTML to be safely shown in a form.
1283 * Special characters are converted.
1284 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 * @param string
1286 * @return string
1287 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001288 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 {
1290 if (is_array($data))
1291 {
1292 foreach ($data as $key => $val)
1293 {
1294 $data[$key] = $this->prep_for_form($val);
1295 }
Barry Mienydd671972010-10-04 16:33:58 +02001296
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 return $data;
1298 }
Barry Mienydd671972010-10-04 16:33:58 +02001299
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 if ($this->_safe_form_data == FALSE OR $data === '')
1301 {
1302 return $data;
1303 }
1304
Andrey Andreev46ac8812012-02-28 14:32:54 +02001305 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 }
Barry Mienydd671972010-10-04 16:33:58 +02001307
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001309
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 /**
1311 * Prep URL
1312 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 * @param string
1314 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001315 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001316 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 {
1318 if ($str == 'http://' OR $str == '')
1319 {
1320 return '';
1321 }
Barry Mienydd671972010-10-04 16:33:58 +02001322
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001323 if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://')
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 {
1325 $str = 'http://'.$str;
1326 }
Barry Mienydd671972010-10-04 16:33:58 +02001327
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 return $str;
1329 }
Barry Mienydd671972010-10-04 16:33:58 +02001330
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001332
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 /**
1334 * Strip Image Tags
1335 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 * @param string
1337 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001338 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001339 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 {
1341 return $this->CI->input->strip_image_tags($str);
1342 }
Barry Mienydd671972010-10-04 16:33:58 +02001343
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001345
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 /**
1347 * XSS Clean
1348 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 * @param string
1350 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001351 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001352 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 {
Derek Jones5640a712010-04-23 11:22:40 -05001354 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 }
Barry Mienydd671972010-10-04 16:33:58 +02001356
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001358
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 /**
1360 * Convert PHP tags to entities
1361 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 * @param string
1363 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001364 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001365 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001367 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 }
1369
JonoB099c4782012-03-04 14:37:30 +00001370 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001371
1372 /**
1373 * Reset validation vars
1374 *
1375 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001376 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001377 *
1378 * @return void
1379 */
JonoB883f80f2012-03-05 09:51:27 +00001380 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001381 {
JonoB099c4782012-03-04 14:37:30 +00001382 $this->_field_data = array();
1383 $this->_config_rules = array();
1384 $this->_error_array = array();
1385 $this->_error_messages = array();
1386 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001387 }
1388
Derek Allard2067d1a2008-11-13 22:59:24 +00001389}
Derek Allard2067d1a2008-11-13 22:59:24 +00001390
1391/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001392/* Location: ./system/libraries/Form_validation.php */