blob: 8a5d8eaa972e03e90d56b7a7374cfe34ba20eb6a [file] [log] [blame]
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnescccde962011-12-04 00:01:17 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnescccde962011-12-04 00:01:17 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreevb998f3e2012-01-24 15:31:26 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Form Validation Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
36 */
37class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020038
Phil Sturgeon3837ae72011-05-09 21:12:26 +010039 protected $CI;
40 protected $_field_data = array();
41 protected $_config_rules = array();
42 protected $_error_array = array();
43 protected $_error_messages = array();
44 protected $_error_prefix = '<p>';
45 protected $_error_suffix = '</p>';
46 protected $error_string = '';
47 protected $_safe_form_data = FALSE;
JonoB099c4782012-03-04 14:37:30 +000048 protected $validation_data = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000049
Greg Akera9263282010-11-10 15:26:43 -060050 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +020051 {
Derek Allard2067d1a2008-11-13 22:59:24 +000052 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020053
Derek Allard2067d1a2008-11-13 22:59:24 +000054 // Validation rules can be stored in a config file.
55 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020056
Derek Allard2067d1a2008-11-13 22:59:24 +000057 // Automatically load the form helper
58 $this->CI->load->helper('form');
59
60 // Set the character encoding in MB.
61 if (function_exists('mb_internal_encoding'))
62 {
63 mb_internal_encoding($this->CI->config->item('charset'));
64 }
Barry Mienydd671972010-10-04 16:33:58 +020065
Andrey Andreev901573c2012-01-11 01:40:48 +020066 log_message('debug', 'Form Validation Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000067 }
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 /**
72 * Set Rules
73 *
74 * This function takes an array of field names and validation
75 * rules as input, validates the info, and stores it
76 *
Derek Allard2067d1a2008-11-13 22:59:24 +000077 * @param mixed
78 * @param string
79 * @return void
80 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010081 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
83 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +000084 // or a validation array has not been specified
Andrey Andreev3b2c5082012-03-07 22:49:24 +020085 if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
Greg Aker9f9af602010-11-10 15:41:51 -060087 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +000088 }
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 // If an array was passed via the first parameter instead of indidual string
91 // values we cycle through it and recursively call this function.
92 if (is_array($field))
93 {
94 foreach ($field as $row)
95 {
96 // Houston, we have a problem...
97 if ( ! isset($row['field']) OR ! isset($row['rules']))
98 {
99 continue;
100 }
101
102 // If the field label wasn't passed we use the field name
103 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
104
105 // Here we go!
106 $this->set_rules($row['field'], $label, $row['rules']);
107 }
Greg Aker9f9af602010-11-10 15:41:51 -0600108 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200112 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
Greg Aker9f9af602010-11-10 15:41:51 -0600114 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 }
116
117 // If the field label wasn't passed we use the field name
118 $label = ($label == '') ? $field : $label;
119
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200120 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200121 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200122 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200123 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // Note: Due to a bug in current() that affects some versions
125 // of PHP we can not pass function call directly into it
126 $x = explode('[', $field);
127 $indexes[] = current($x);
128
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200129 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200131 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200133 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 $is_array = TRUE;
138 }
139 else
140 {
Barry Mienydd671972010-10-04 16:33:58 +0200141 $indexes = array();
142 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
145 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000147 'field' => $field,
148 'label' => $label,
149 'rules' => $rules,
150 'is_array' => $is_array,
151 'keys' => $indexes,
152 'postdata' => NULL,
153 'error' => ''
154 );
Greg Aker9f9af602010-11-10 15:41:51 -0600155
156 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
158
159 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 /**
JonoB099c4782012-03-04 14:37:30 +0000162 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200163 *
JonoB099c4782012-03-04 14:37:30 +0000164 * If an array is set through this method, then this array will
165 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200166 *
167 * Note that if you are validating multiple arrays, then the
168 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000169 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200170 *
171 * @param array $data
172 * @return void
JonoB099c4782012-03-04 14:37:30 +0000173 */
174 public function set_data($data = '')
175 {
176 if ( ! empty($data) && is_array($data))
177 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200178 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000179 }
180 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200181
JonoB099c4782012-03-04 14:37:30 +0000182 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000183
184 /**
185 * Set Error Message
186 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500187 * Lets users set their own error messages on the fly. Note: The key
JonoB099c4782012-03-04 14:37:30 +0000188 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 * @param string
191 * @param string
192 * @return string
193 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100194 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
196 if ( ! is_array($lang))
197 {
198 $lang = array($lang => $val);
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600202 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 /**
208 * Set The Error Delimiter
209 *
210 * Permits a prefix/suffix to be added to each error message
211 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 * @param string
213 * @param string
214 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200215 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100216 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
218 $this->_error_prefix = $prefix;
219 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600220 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222
223 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 /**
226 * Get Error Message
227 *
228 * Gets the error message associated with a particular field
229 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 * @param string the field name
231 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200232 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100233 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200234 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
236 {
237 return '';
238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 if ($prefix == '')
241 {
242 $prefix = $this->_error_prefix;
243 }
244
245 if ($suffix == '')
246 {
247 $suffix = $this->_error_suffix;
248 }
249
250 return $prefix.$this->_field_data[$field]['error'].$suffix;
251 }
252
253 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100256 * Get Array of Error Messages
257 *
258 * Returns the error messages as an array
259 *
260 * @return array
261 */
262 public function error_array()
263 {
264 return $this->_error_array;
265 }
266
267 // --------------------------------------------------------------------
268
269 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * Error String
271 *
272 * Returns the error messages as a string, wrapped in the error delimiters
273 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @param string
275 * @param string
276 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200277 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100278 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 // No errrors, validation passes!
281 if (count($this->_error_array) === 0)
282 {
283 return '';
284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 if ($prefix == '')
287 {
288 $prefix = $this->_error_prefix;
289 }
290
291 if ($suffix == '')
292 {
293 $suffix = $this->_error_suffix;
294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 // Generate the error string
297 $str = '';
298 foreach ($this->_error_array as $val)
299 {
300 if ($val != '')
301 {
302 $str .= $prefix.$val.$suffix."\n";
303 }
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 return $str;
307 }
308
309 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 /**
312 * Run the Validator
313 *
314 * This function does all the work.
315 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200317 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100318 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500320 // Do we even have any data to process? Mm?
JonoB099c4782012-03-04 14:37:30 +0000321 $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST;
322 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
324 return FALSE;
325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // Does the _field_data array containing the validation rules exist?
328 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200329 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500331 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200332 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 return FALSE;
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // Is there a validation rule for the particular URI being accessed?
338 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200339
Andrey Andreev6de924c2012-01-20 13:18:18 +0200340 if ($uri != '' && isset($this->_config_rules[$uri]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 $this->set_rules($this->_config_rules[$uri]);
343 }
344 else
345 {
346 $this->set_rules($this->_config_rules);
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Andrey Andreev901573c2012-01-11 01:40:48 +0200349 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200350 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200352 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 return FALSE;
354 }
355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // Load the language file containing error messages
358 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200359
360 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // corresponding $_POST item and test for errors
362 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200363 {
JonoB099c4782012-03-04 14:37:30 +0000364 // 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 +0000365 // 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 +0200366 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
JonoB099c4782012-03-04 14:37:30 +0000368 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
Andrey Andreev6de924c2012-01-20 13:18:18 +0200370 elseif (isset($_POST[$field]) && $_POST[$field] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
Andrey Andreevb68e70b2012-03-09 14:44:31 +0200372 if (isset($validation_array[$field]) && $validation_array[$field] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
JonoB099c4782012-03-04 14:37:30 +0000374 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
378 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
380
381 // Did we end up with any errors?
382 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 if ($total_errors > 0)
384 {
385 $this->_safe_form_data = TRUE;
386 }
387
388 // Now we need to re-set the POST data with the new, processed data
389 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200390
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200391 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
393
394 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 /**
397 * Traverse a multidimensional $_POST array index until the data is found
398 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * @param array
400 * @param array
401 * @param integer
402 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200403 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100404 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200406 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200408 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 return $array;
412 }
413
414 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 /**
417 * Re-populate the _POST array with our finalized and processed data
418 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200419 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200420 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100421 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 foreach ($this->_field_data as $field => $row)
424 {
425 if ( ! is_null($row['postdata']))
426 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200427 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
429 if (isset($_POST[$row['field']]))
430 {
431 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
432 }
433 }
434 else
435 {
Derek Jones63eeae32009-02-10 19:08:56 +0000436 // start with a reference
437 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Jones63eeae32009-02-10 19:08:56 +0000439 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200440 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
Derek Jones63eeae32009-02-10 19:08:56 +0000442 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
444 else
445 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 foreach ($row['keys'] as $val)
447 {
Derek Jones63eeae32009-02-10 19:08:56 +0000448 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450 }
Derek Jones63eeae32009-02-10 19:08:56 +0000451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000453 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 $array = array();
455 foreach ($row['postdata'] as $k => $v)
456 {
457 $array[$k] = $this->prep_for_form($v);
458 }
Derek Jones63eeae32009-02-10 19:08:56 +0000459
460 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462 else
Derek Jones63eeae32009-02-10 19:08:56 +0000463 {
464 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
467 }
468 }
469 }
470
471 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 /**
474 * Executes the Validation routines
475 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * @param array
477 * @param array
478 * @param mixed
479 * @param integer
480 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200481 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100482 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
484 // If the $_POST data is an array we will run a recursive call
485 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200486 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 foreach ($postdata as $key => $val)
488 {
489 $this->_execute($row, $rules, $val, $cycles);
490 $cycles++;
491 }
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 return;
494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 // If the field is blank, but NOT required, no further tests are necessary
497 $callback = FALSE;
Andrey Andreev6de924c2012-01-20 13:18:18 +0200498 if ( ! in_array('required', $rules) && is_null($postdata))
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
500 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200501 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
503 $callback = TRUE;
504 $rules = (array('1' => $match[1]));
505 }
506 else
507 {
508 return;
509 }
510 }
511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev6de924c2012-01-20 13:18:18 +0200513 if (is_null($postdata) && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
515 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
516 {
517 // Set the message type
518 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 if ( ! isset($this->_error_messages[$type]))
521 {
522 if (FALSE === ($line = $this->CI->lang->line($type)))
523 {
524 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200525 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 }
527 else
528 {
529 $line = $this->_error_messages[$type];
530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 // Build the error message
533 $message = sprintf($line, $this->_translate_fieldname($row['label']));
534
535 // Save the error message
536 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 if ( ! isset($this->_error_array[$row['field']]))
539 {
540 $this->_error_array[$row['field']] = $message;
541 }
542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 return;
545 }
546
547 // --------------------------------------------------------------------
548
549 // Cycle through each rule and run it
550 foreach ($rules As $rule)
551 {
552 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 // We set the $postdata variable with the current data in our master array so that
555 // each cycle of the loop is dealing with the processed data from the last cycle
Andrey Andreev6de924c2012-01-20 13:18:18 +0200556 if ($row['is_array'] == TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
558 // We shouldn't need this safety, but just in case there isn't an array index
559 // associated with this cycle we'll bail out
560 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
561 {
562 continue;
563 }
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
566 $_in_array = TRUE;
567 }
568 else
569 {
570 $postdata = $this->_field_data[$row['field']]['postdata'];
571 }
572
Barry Mienydd671972010-10-04 16:33:58 +0200573 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200575 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 {
577 $rule = substr($rule, 9);
578 $callback = TRUE;
579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 // Strip the parameter (if exists) from the rule
582 // Rules can contain a parameter: max_length[5]
583 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200584 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 $rule = $match[1];
587 $param = $match[2];
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 // Call the function that corresponds to the rule
591 if ($callback === TRUE)
592 {
593 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200594 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200595 log_message('debug', 'Unable to find callback validation rule: '.$rule);
596 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200598 else
599 {
600 // Run the function and grab the result
601 $result = $this->CI->$rule($postdata, $param);
602 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000603
604 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200605 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
607 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
608 }
609 else
610 {
611 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
612 }
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200615 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000617 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 }
619 }
620 else
Barry Mienydd671972010-10-04 16:33:58 +0200621 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 if ( ! method_exists($this, $rule))
623 {
Barry Mienydd671972010-10-04 16:33:58 +0200624 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 // Users can use any native PHP function call that has one param.
626 if (function_exists($rule))
627 {
Andrey Andreevcde43682012-01-13 20:16:35 +0200628 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200629
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200630 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
632 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
633 }
634 else
635 {
636 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
637 }
638 }
patwork02404a12011-04-08 15:45:46 +0200639 else
640 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200641 log_message('debug', 'Unable to find validation rule: '.$rule);
642 $result = FALSE;
patwork02404a12011-04-08 15:45:46 +0200643 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200645 else
646 {
647 $result = $this->$rule($postdata, $param);
648 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000649
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200650 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
652 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
653 }
654 else
655 {
656 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
657 }
658 }
Barry Mienydd671972010-10-04 16:33:58 +0200659
Andrey Andreev901573c2012-01-11 01:40:48 +0200660 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200662 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 if ( ! isset($this->_error_messages[$rule]))
664 {
665 if (FALSE === ($line = $this->CI->lang->line($rule)))
666 {
667 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200668 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 }
670 else
671 {
672 $line = $this->_error_messages[$rule];
673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200676 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200677 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
Pascal Krietec1895832009-10-13 12:56:43 +0000679 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 }
Barry Mienydd671972010-10-04 16:33:58 +0200681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 // Build the error message
683 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
684
685 // Save the error message
686 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 if ( ! isset($this->_error_array[$row['field']]))
689 {
690 $this->_error_array[$row['field']] = $message;
691 }
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 return;
694 }
695 }
696 }
697
698 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 /**
701 * Translate a field name
702 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 * @param string the field name
704 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200705 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100706 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 {
708 // Do we need to translate the field name?
709 // We look for the prefix lang: to determine this
Andrey Andreev901573c2012-01-11 01:40:48 +0200710 if (strpos($fieldname, 'lang:') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 {
712 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200713 $line = substr($fieldname, 5);
714
Derek Jones37f4b9c2011-07-01 17:56:50 -0500715 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
717 {
718 return $line;
719 }
720 }
721
722 return $fieldname;
723 }
724
725 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200726
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 /**
728 * Get the value from a form
729 *
730 * Permits you to repopulate a form field with the value it was submitted
731 * with, or, if that value doesn't exist, with the default
732 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 * @param string the field name
734 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200735 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200736 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100737 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200739 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 {
741 return $default;
742 }
Barry Mienydd671972010-10-04 16:33:58 +0200743
Phil Sturgeon5c561802011-01-05 16:31:59 +0000744 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600745 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000746 if (is_array($this->_field_data[$field]['postdata']))
747 {
748 return array_shift($this->_field_data[$field]['postdata']);
749 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000750
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 return $this->_field_data[$field]['postdata'];
752 }
Barry Mienydd671972010-10-04 16:33:58 +0200753
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200755
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 /**
757 * Set Select
758 *
759 * Enables pull-down lists to be set to the value the user
760 * selected in the event of an error
761 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 * @param string
763 * @param string
764 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200765 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100766 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200767 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200768 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200770 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 }
Barry Mienydd671972010-10-04 16:33:58 +0200772
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 if (is_array($field))
775 {
776 if ( ! in_array($value, $field))
777 {
778 return '';
779 }
780 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200781 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200783 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 return ' selected="selected"';
787 }
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200790
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 /**
792 * Set Radio
793 *
794 * Enables radio buttons to be set to the value the user
795 * selected in the event of an error
796 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 * @param string
798 * @param string
799 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200800 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100801 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200803 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200805 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 }
Barry Mienydd671972010-10-04 16:33:58 +0200807
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 if (is_array($field))
810 {
811 if ( ! in_array($value, $field))
812 {
813 return '';
814 }
815 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200816 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200818 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 }
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 return ' checked="checked"';
822 }
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 /**
827 * Set Checkbox
828 *
829 * Enables checkboxes to be set to the value the user
830 * selected in the event of an error
831 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 * @param string
833 * @param string
834 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200835 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100836 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200838 // Logic is exactly the same as for radio fields
839 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 }
Barry Mienydd671972010-10-04 16:33:58 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200843
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 /**
845 * Required
846 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 * @param string
848 * @return bool
849 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100850 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200852 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 }
Barry Mienydd671972010-10-04 16:33:58 +0200854
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200856
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500858 * Performs a Regular Expression match test.
859 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500860 * @param string
861 * @param regex
862 * @return bool
863 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100864 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500865 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200866 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500867 }
868
869 // --------------------------------------------------------------------
870
871 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 * Match one field to another
873 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * @param string
875 * @param field
876 * @return bool
877 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100878 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200880 $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST;
JonoB099c4782012-03-04 14:37:30 +0000881 if ( ! isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 {
Barry Mienydd671972010-10-04 16:33:58 +0200883 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
JonoB099c4782012-03-04 14:37:30 +0000886 return ($str === $validation_array[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
Eric Barnescccde962011-12-04 00:01:17 -0500888
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100889 // --------------------------------------------------------------------
890
891 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200892 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100893 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200894 * Check if the input value doesn't already exist
895 * in the specified database field.
896 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100897 * @param string
898 * @param field
899 * @return bool
900 */
901 public function is_unique($str, $field)
902 {
Eric Barnescccde962011-12-04 00:01:17 -0500903 list($table, $field) = explode('.', $field);
904 if (isset($this->CI->db))
905 {
906 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
907 return $query->num_rows() === 0;
908 }
909 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600910 }
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200913
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 /**
915 * Minimum Length
916 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 * @param string
918 * @param value
919 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200920 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100921 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200923 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 {
925 return FALSE;
926 }
927
928 if (function_exists('mb_strlen'))
929 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200930 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 }
Barry Mienydd671972010-10-04 16:33:58 +0200932
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200933 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200937
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 /**
939 * Max Length
940 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 * @param string
942 * @param value
943 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200944 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100945 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200947 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
949 return FALSE;
950 }
951
952 if (function_exists('mb_strlen'))
953 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200954 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 }
Barry Mienydd671972010-10-04 16:33:58 +0200956
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200957 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 /**
963 * Exact Length
964 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 * @param string
966 * @param value
967 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200968 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100969 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200971 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 {
973 return FALSE;
974 }
975
976 if (function_exists('mb_strlen'))
977 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200978 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 }
Barry Mienydd671972010-10-04 16:33:58 +0200980
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200981 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
Barry Mienydd671972010-10-04 16:33:58 +0200983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 /**
987 * Valid Email
988 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 * @param string
990 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200991 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100992 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200994 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 +0000995 }
996
997 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 /**
1000 * Valid Emails
1001 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 * @param string
1003 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001004 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001005 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 {
1007 if (strpos($str, ',') === FALSE)
1008 {
1009 return $this->valid_email(trim($str));
1010 }
Barry Mienydd671972010-10-04 16:33:58 +02001011
Pascal Kriete14287f32011-02-14 13:39:34 -05001012 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001014 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
1016 return FALSE;
1017 }
1018 }
Barry Mienydd671972010-10-04 16:33:58 +02001019
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 return TRUE;
1021 }
1022
1023 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001024
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 /**
1026 * Validate IP Address
1027 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001029 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001031 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 {
1033 return $this->CI->input->valid_ip($ip);
1034 }
1035
1036 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001037
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 /**
1039 * Alpha
1040 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 * @param string
1042 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001043 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001044 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001046 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 }
Barry Mienydd671972010-10-04 16:33:58 +02001048
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 /**
1052 * Alpha-numeric
1053 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 * @param string
1055 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001056 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001057 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001059 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 }
Barry Mienydd671972010-10-04 16:33:58 +02001061
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001063
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 /**
1065 * Alpha-numeric with underscores and dashes
1066 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 * @param string
1068 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001069 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001070 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001072 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 }
Barry Mienydd671972010-10-04 16:33:58 +02001074
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001076
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 /**
1078 * Numeric
1079 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 * @param string
1081 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001082 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001083 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001085 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001086
1087 }
1088
1089 // --------------------------------------------------------------------
1090
Barry Mienydd671972010-10-04 16:33:58 +02001091 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 * Integer
1093 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 * @param string
1095 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001096 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001097 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001099 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1100 }
1101
1102 // --------------------------------------------------------------------
1103
1104 /**
1105 * Decimal number
1106 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001107 * @param string
1108 * @return bool
1109 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001110 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001111 {
1112 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1113 }
1114
1115 // --------------------------------------------------------------------
1116
1117 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001118 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001119 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001120 * @param string
1121 * @return bool
1122 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001123 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001124 {
1125 if ( ! is_numeric($str))
1126 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001127 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001128 }
1129 return $str > $min;
1130 }
1131
1132 // --------------------------------------------------------------------
1133
1134 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001135 * Equal to or Greater than
1136 *
Nick Busey98c347d2012-02-02 11:07:03 -07001137 * @param string
1138 * @return bool
1139 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001140 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001141 {
1142 if ( ! is_numeric($str))
1143 {
1144 return FALSE;
1145 }
1146 return $str >= $min;
1147 }
1148
1149 // --------------------------------------------------------------------
Phil Sturgeonef112c02011-02-07 13:01:47 +00001150
1151 /**
1152 * Less than
1153 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001154 * @param string
1155 * @return bool
1156 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001157 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001158 {
1159 if ( ! is_numeric($str))
1160 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001161 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001162 }
1163 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001165
1166 // --------------------------------------------------------------------
1167
Barry Mienydd671972010-10-04 16:33:58 +02001168 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001169 * Equal to or Less than
1170 *
Nick Busey98c347d2012-02-02 11:07:03 -07001171 * @param string
1172 * @return bool
1173 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001174 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001175 {
1176 if ( ! is_numeric($str))
1177 {
1178 return FALSE;
1179 }
1180 return $str <= $max;
1181 }
1182
1183 // --------------------------------------------------------------------
1184
1185 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001186 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001187 *
Barry Mienydd671972010-10-04 16:33:58 +02001188 * @param string
1189 * @return bool
1190 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001191 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001192 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001193 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001194 }
1195
1196 // --------------------------------------------------------------------
1197
1198 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001199 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001200 *
Barry Mienydd671972010-10-04 16:33:58 +02001201 * @param string
1202 * @return bool
1203 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001204 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001205 {
Andrey Andreev6de924c2012-01-20 13:18:18 +02001206 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001207 }
1208
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001210
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 /**
1212 * Valid Base64
1213 *
1214 * Tests a string for characters outside of the Base64 alphabet
1215 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1216 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 * @param string
1218 * @return bool
1219 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001220 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 {
1222 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1223 }
Barry Mienydd671972010-10-04 16:33:58 +02001224
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001226
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 /**
1228 * Prep data for form
1229 *
1230 * This function allows HTML to be safely shown in a form.
1231 * Special characters are converted.
1232 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 * @param string
1234 * @return string
1235 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001236 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 {
1238 if (is_array($data))
1239 {
1240 foreach ($data as $key => $val)
1241 {
1242 $data[$key] = $this->prep_for_form($val);
1243 }
Barry Mienydd671972010-10-04 16:33:58 +02001244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 return $data;
1246 }
Barry Mienydd671972010-10-04 16:33:58 +02001247
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 if ($this->_safe_form_data == FALSE OR $data === '')
1249 {
1250 return $data;
1251 }
1252
Andrey Andreev901573c2012-01-11 01:40:48 +02001253 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 }
Barry Mienydd671972010-10-04 16:33:58 +02001255
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001257
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 /**
1259 * Prep URL
1260 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 * @param string
1262 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001263 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001264 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001266 if ($str === 'http://' OR $str == '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 {
1268 return '';
1269 }
Barry Mienydd671972010-10-04 16:33:58 +02001270
Andrey Andreev901573c2012-01-11 01:40:48 +02001271 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001273 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 }
Barry Mienydd671972010-10-04 16:33:58 +02001275
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 return $str;
1277 }
Barry Mienydd671972010-10-04 16:33:58 +02001278
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 /**
1282 * Strip Image Tags
1283 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 * @param string
1285 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001286 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001287 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 {
1289 return $this->CI->input->strip_image_tags($str);
1290 }
Barry Mienydd671972010-10-04 16:33:58 +02001291
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001293
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 /**
1295 * XSS Clean
1296 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 * @param string
1298 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001299 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001300 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 {
Derek Jones5640a712010-04-23 11:22:40 -05001302 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 }
Barry Mienydd671972010-10-04 16:33:58 +02001304
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001306
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 /**
1308 * Convert PHP tags to entities
1309 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 * @param string
1311 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001312 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001313 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001315 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 }
1317
JonoB099c4782012-03-04 14:37:30 +00001318 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001319
1320 /**
1321 * Reset validation vars
1322 *
1323 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001324 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001325 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001326 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001327 */
JonoB883f80f2012-03-05 09:51:27 +00001328 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001329 {
JonoB099c4782012-03-04 14:37:30 +00001330 $this->_field_data = array();
1331 $this->_config_rules = array();
1332 $this->_error_array = array();
1333 $this->_error_messages = array();
1334 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001335 }
1336
Derek Allard2067d1a2008-11-13 22:59:24 +00001337}
Derek Allard2067d1a2008-11-13 22:59:24 +00001338
1339/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001340/* Location: ./system/libraries/Form_validation.php */