blob: 8a27935382318b533021848970cf92ceaff15e03 [file] [log] [blame]
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnescccde962011-12-04 00:01:17 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnescccde962011-12-04 00:01:17 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreevb998f3e2012-01-24 15:31:26 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Form Validation Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
36 */
37class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020038
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
Mike Funk326a5e72012-02-24 10:06:28 -050054 // applies delimiters set in config file.
Mike Funk7f42d062012-03-08 09:00:57 -050055 if (isset($rules['error_prefix']))
56 {
57 $this->_error_prefix = $rules['error_prefix'];
58 unset($rules['error_prefix']);
59 }
60 if (isset($rules['error_suffix']))
61 {
62 $this->_error_suffix = $rules['error_suffix'];
63 unset($rules['error_suffix']);
64 }
Andrey Andreev31cf46e2012-03-20 15:48:00 +020065
Derek Allard2067d1a2008-11-13 22:59:24 +000066 // Validation rules can be stored in a config file.
67 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069 // Automatically load the form helper
70 $this->CI->load->helper('form');
71
72 // Set the character encoding in MB.
tiyowan5b9fd2d2012-03-12 20:26:59 +040073 if (MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000074 {
75 mb_internal_encoding($this->CI->config->item('charset'));
76 }
Barry Mienydd671972010-10-04 16:33:58 +020077
Andrey Andreev901573c2012-01-11 01:40:48 +020078 log_message('debug', 'Form Validation Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000079 }
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020082
Derek Allard2067d1a2008-11-13 22:59:24 +000083 /**
84 * Set Rules
85 *
86 * This function takes an array of field names and validation
87 * rules as input, validates the info, and stores it
88 *
Derek Allard2067d1a2008-11-13 22:59:24 +000089 * @param mixed
90 * @param string
91 * @return void
92 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010093 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
95 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +000096 // or a validation array has not been specified
Andrey Andreev3b2c5082012-03-07 22:49:24 +020097 if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
Greg Aker9f9af602010-11-10 15:41:51 -060099 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
Barry Mienydd671972010-10-04 16:33:58 +0200101
tiyowanc2acb232012-03-14 21:24:00 +0400102 // If an array was passed via the first parameter instead of individual string
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 // values we cycle through it and recursively call this function.
104 if (is_array($field))
105 {
106 foreach ($field as $row)
107 {
108 // Houston, we have a problem...
109 if ( ! isset($row['field']) OR ! isset($row['rules']))
110 {
111 continue;
112 }
113
114 // If the field label wasn't passed we use the field name
115 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
116
117 // Here we go!
118 $this->set_rules($row['field'], $label, $row['rules']);
119 }
Greg Aker9f9af602010-11-10 15:41:51 -0600120 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200124 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
Greg Aker9f9af602010-11-10 15:41:51 -0600126 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 }
128
129 // If the field label wasn't passed we use the field name
130 $label = ($label == '') ? $field : $label;
131
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200132 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200133 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200134 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200135 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // Note: Due to a bug in current() that affects some versions
137 // of PHP we can not pass function call directly into it
138 $x = explode('[', $field);
139 $indexes[] = current($x);
140
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200141 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200143 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200145 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 $is_array = TRUE;
150 }
151 else
152 {
Barry Mienydd671972010-10-04 16:33:58 +0200153 $indexes = array();
154 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
157 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000159 'field' => $field,
160 'label' => $label,
161 'rules' => $rules,
162 'is_array' => $is_array,
163 'keys' => $indexes,
164 'postdata' => NULL,
165 'error' => ''
166 );
Greg Aker9f9af602010-11-10 15:41:51 -0600167
168 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170
171 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 /**
JonoB099c4782012-03-04 14:37:30 +0000174 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200175 *
JonoB099c4782012-03-04 14:37:30 +0000176 * If an array is set through this method, then this array will
177 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200178 *
179 * Note that if you are validating multiple arrays, then the
180 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000181 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200182 *
183 * @param array $data
184 * @return void
JonoB099c4782012-03-04 14:37:30 +0000185 */
186 public function set_data($data = '')
187 {
188 if ( ! empty($data) && is_array($data))
189 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200190 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000191 }
192 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200193
JonoB099c4782012-03-04 14:37:30 +0000194 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000195
196 /**
197 * Set Error Message
198 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500199 * Lets users set their own error messages on the fly. Note: The key
JonoB099c4782012-03-04 14:37:30 +0000200 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 * @param string
203 * @param string
204 * @return string
205 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100206 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
208 if ( ! is_array($lang))
209 {
210 $lang = array($lang => $val);
211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600214 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 /**
220 * Set The Error Delimiter
221 *
222 * Permits a prefix/suffix to be added to each error message
223 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 * @param string
225 * @param string
226 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200227 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100228 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
230 $this->_error_prefix = $prefix;
231 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600232 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
234
235 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 /**
238 * Get Error Message
239 *
240 * Gets the error message associated with a particular field
241 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * @param string the field name
243 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200244 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100245 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200246 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
248 {
249 return '';
250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 if ($prefix == '')
253 {
254 $prefix = $this->_error_prefix;
255 }
256
257 if ($suffix == '')
258 {
259 $suffix = $this->_error_suffix;
260 }
261
262 return $prefix.$this->_field_data[$field]['error'].$suffix;
263 }
264
265 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100268 * Get Array of Error Messages
269 *
270 * Returns the error messages as an array
271 *
272 * @return array
273 */
274 public function error_array()
275 {
276 return $this->_error_array;
277 }
278
279 // --------------------------------------------------------------------
280
281 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 * Error String
283 *
284 * Returns the error messages as a string, wrapped in the error delimiters
285 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 * @param string
287 * @param string
288 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200289 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100290 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 // No errrors, validation passes!
293 if (count($this->_error_array) === 0)
294 {
295 return '';
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 if ($prefix == '')
299 {
300 $prefix = $this->_error_prefix;
301 }
302
303 if ($suffix == '')
304 {
305 $suffix = $this->_error_suffix;
306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 // Generate the error string
309 $str = '';
310 foreach ($this->_error_array as $val)
311 {
312 if ($val != '')
313 {
314 $str .= $prefix.$val.$suffix."\n";
315 }
316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 return $str;
319 }
320
321 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 /**
324 * Run the Validator
325 *
326 * This function does all the work.
327 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200329 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100330 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500332 // Do we even have any data to process? Mm?
JonoB099c4782012-03-04 14:37:30 +0000333 $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST;
334 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 return FALSE;
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // Does the _field_data array containing the validation rules exist?
340 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200341 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500343 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200344 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
346 return FALSE;
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // Is there a validation rule for the particular URI being accessed?
350 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200351
Andrey Andreev6de924c2012-01-20 13:18:18 +0200352 if ($uri != '' && isset($this->_config_rules[$uri]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
354 $this->set_rules($this->_config_rules[$uri]);
355 }
356 else
357 {
358 $this->set_rules($this->_config_rules);
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Andrey Andreev901573c2012-01-11 01:40:48 +0200361 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200362 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200364 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 return FALSE;
366 }
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // Load the language file containing error messages
370 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200371
372 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 // corresponding $_POST item and test for errors
374 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200375 {
JonoB099c4782012-03-04 14:37:30 +0000376 // 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 +0000377 // 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 +0200378 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
JonoB099c4782012-03-04 14:37:30 +0000380 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 }
Andrey Andreev6de924c2012-01-20 13:18:18 +0200382 elseif (isset($_POST[$field]) && $_POST[$field] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Andrey Andreevb68e70b2012-03-09 14:44:31 +0200384 if (isset($validation_array[$field]) && $validation_array[$field] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
JonoB099c4782012-03-04 14:37:30 +0000386 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
390 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
392
393 // Did we end up with any errors?
394 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 if ($total_errors > 0)
396 {
397 $this->_safe_form_data = TRUE;
398 }
399
400 // Now we need to re-set the POST data with the new, processed data
401 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200402
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200403 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
405
406 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 /**
409 * Traverse a multidimensional $_POST array index until the data is found
410 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @param array
412 * @param array
413 * @param integer
414 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200415 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100416 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200418 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200420 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 return $array;
424 }
425
426 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 /**
429 * Re-populate the _POST array with our finalized and processed data
430 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200431 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200432 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100433 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
435 foreach ($this->_field_data as $field => $row)
436 {
437 if ( ! is_null($row['postdata']))
438 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200439 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 if (isset($_POST[$row['field']]))
442 {
443 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
444 }
445 }
446 else
447 {
Derek Jones63eeae32009-02-10 19:08:56 +0000448 // start with a reference
449 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Jones63eeae32009-02-10 19:08:56 +0000451 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200452 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
Derek Jones63eeae32009-02-10 19:08:56 +0000454 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
456 else
457 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 foreach ($row['keys'] as $val)
459 {
Derek Jones63eeae32009-02-10 19:08:56 +0000460 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462 }
Derek Jones63eeae32009-02-10 19:08:56 +0000463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000465 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 $array = array();
467 foreach ($row['postdata'] as $k => $v)
468 {
469 $array[$k] = $this->prep_for_form($v);
470 }
Derek Jones63eeae32009-02-10 19:08:56 +0000471
472 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474 else
Derek Jones63eeae32009-02-10 19:08:56 +0000475 {
476 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
479 }
480 }
481 }
482
483 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 /**
486 * Executes the Validation routines
487 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 * @param array
489 * @param array
490 * @param mixed
491 * @param integer
492 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200493 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100494 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
496 // If the $_POST data is an array we will run a recursive call
497 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200498 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 foreach ($postdata as $key => $val)
500 {
501 $this->_execute($row, $rules, $val, $cycles);
502 $cycles++;
503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 return;
506 }
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 // If the field is blank, but NOT required, no further tests are necessary
509 $callback = FALSE;
Andrey Andreev6de924c2012-01-20 13:18:18 +0200510 if ( ! in_array('required', $rules) && is_null($postdata))
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
512 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200513 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
515 $callback = TRUE;
516 $rules = (array('1' => $match[1]));
517 }
518 else
519 {
520 return;
521 }
522 }
523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev6de924c2012-01-20 13:18:18 +0200525 if (is_null($postdata) && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
527 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
528 {
529 // Set the message type
530 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 if ( ! isset($this->_error_messages[$type]))
533 {
534 if (FALSE === ($line = $this->CI->lang->line($type)))
535 {
536 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200537 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
539 else
540 {
541 $line = $this->_error_messages[$type];
542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 // Build the error message
545 $message = sprintf($line, $this->_translate_fieldname($row['label']));
546
547 // Save the error message
548 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 if ( ! isset($this->_error_array[$row['field']]))
551 {
552 $this->_error_array[$row['field']] = $message;
553 }
554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 return;
557 }
558
559 // --------------------------------------------------------------------
560
561 // Cycle through each rule and run it
562 foreach ($rules As $rule)
563 {
564 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 // We set the $postdata variable with the current data in our master array so that
567 // each cycle of the loop is dealing with the processed data from the last cycle
Andrey Andreev6de924c2012-01-20 13:18:18 +0200568 if ($row['is_array'] == TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
570 // We shouldn't need this safety, but just in case there isn't an array index
571 // associated with this cycle we'll bail out
572 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
573 {
574 continue;
575 }
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
578 $_in_array = TRUE;
579 }
580 else
581 {
582 $postdata = $this->_field_data[$row['field']]['postdata'];
583 }
584
Barry Mienydd671972010-10-04 16:33:58 +0200585 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200587 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 {
589 $rule = substr($rule, 9);
590 $callback = TRUE;
591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 // Strip the parameter (if exists) from the rule
594 // Rules can contain a parameter: max_length[5]
595 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200596 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
598 $rule = $match[1];
599 $param = $match[2];
600 }
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 // Call the function that corresponds to the rule
603 if ($callback === TRUE)
604 {
605 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200606 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200607 log_message('debug', 'Unable to find callback validation rule: '.$rule);
608 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200610 else
611 {
612 // Run the function and grab the result
613 $result = $this->CI->$rule($postdata, $param);
614 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000615
616 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200617 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
619 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
620 }
621 else
622 {
623 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200627 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000629 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 }
631 }
632 else
Barry Mienydd671972010-10-04 16:33:58 +0200633 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 if ( ! method_exists($this, $rule))
635 {
Barry Mienydd671972010-10-04 16:33:58 +0200636 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 // Users can use any native PHP function call that has one param.
638 if (function_exists($rule))
639 {
Andrey Andreevcde43682012-01-13 20:16:35 +0200640 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200641
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200642 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
644 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
645 }
646 else
647 {
648 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
649 }
650 }
patwork02404a12011-04-08 15:45:46 +0200651 else
652 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200653 log_message('debug', 'Unable to find validation rule: '.$rule);
654 $result = FALSE;
patwork02404a12011-04-08 15:45:46 +0200655 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200657 else
658 {
659 $result = $this->$rule($postdata, $param);
660 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000661
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200662 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
664 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
665 }
666 else
667 {
668 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
669 }
670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Andrey Andreev901573c2012-01-11 01:40:48 +0200672 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200674 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 if ( ! isset($this->_error_messages[$rule]))
676 {
677 if (FALSE === ($line = $this->CI->lang->line($rule)))
678 {
679 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200680 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
682 else
683 {
684 $line = $this->_error_messages[$rule];
685 }
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200688 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200689 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 {
Pascal Krietec1895832009-10-13 12:56:43 +0000691 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 }
Barry Mienydd671972010-10-04 16:33:58 +0200693
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 // Build the error message
695 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
696
697 // Save the error message
698 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 if ( ! isset($this->_error_array[$row['field']]))
701 {
702 $this->_error_array[$row['field']] = $message;
703 }
Barry Mienydd671972010-10-04 16:33:58 +0200704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 return;
706 }
707 }
708 }
709
710 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200711
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 /**
713 * Translate a field name
714 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 * @param string the field name
716 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200717 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100718 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 {
720 // Do we need to translate the field name?
721 // We look for the prefix lang: to determine this
Andrey Andreev901573c2012-01-11 01:40:48 +0200722 if (strpos($fieldname, 'lang:') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 {
724 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200725 $line = substr($fieldname, 5);
726
Derek Jones37f4b9c2011-07-01 17:56:50 -0500727 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
729 {
730 return $line;
731 }
732 }
733
734 return $fieldname;
735 }
736
737 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200738
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 /**
740 * Get the value from a form
741 *
742 * Permits you to repopulate a form field with the value it was submitted
743 * with, or, if that value doesn't exist, with the default
744 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 * @param string the field name
746 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200747 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200748 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100749 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200751 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 {
753 return $default;
754 }
Barry Mienydd671972010-10-04 16:33:58 +0200755
Phil Sturgeon5c561802011-01-05 16:31:59 +0000756 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600757 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000758 if (is_array($this->_field_data[$field]['postdata']))
759 {
760 return array_shift($this->_field_data[$field]['postdata']);
761 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 return $this->_field_data[$field]['postdata'];
764 }
Barry Mienydd671972010-10-04 16:33:58 +0200765
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 /**
769 * Set Select
770 *
771 * Enables pull-down lists to be set to the value the user
772 * selected in the event of an error
773 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 * @param string
775 * @param string
776 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200777 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100778 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200779 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200780 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200782 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 }
Barry Mienydd671972010-10-04 16:33:58 +0200784
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 if (is_array($field))
787 {
788 if ( ! in_array($value, $field))
789 {
790 return '';
791 }
792 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200793 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200795 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 }
Barry Mienydd671972010-10-04 16:33:58 +0200797
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 return ' selected="selected"';
799 }
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200802
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 /**
804 * Set Radio
805 *
806 * Enables radio buttons to be set to the value the user
807 * selected in the event of an error
808 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 * @param string
810 * @param string
811 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200812 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100813 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200815 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200817 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 }
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 if (is_array($field))
822 {
823 if ( ! in_array($value, $field))
824 {
825 return '';
826 }
827 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200828 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200830 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 return ' checked="checked"';
834 }
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 /**
839 * Set Checkbox
840 *
841 * Enables checkboxes to be set to the value the user
842 * selected in the event of an error
843 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 * @param string
845 * @param string
846 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200847 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100848 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200850 // Logic is exactly the same as for radio fields
851 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 }
Barry Mienydd671972010-10-04 16:33:58 +0200853
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 /**
857 * Required
858 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 * @param string
860 * @return bool
861 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100862 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200864 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 }
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200868
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500870 * Performs a Regular Expression match test.
871 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500872 * @param string
873 * @param regex
874 * @return bool
875 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100876 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500877 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200878 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500879 }
880
881 // --------------------------------------------------------------------
882
883 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * Match one field to another
885 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 * @param string
887 * @param field
888 * @return bool
889 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100890 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200892 $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST;
JonoB099c4782012-03-04 14:37:30 +0000893 if ( ! isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
Barry Mienydd671972010-10-04 16:33:58 +0200895 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 }
Barry Mienydd671972010-10-04 16:33:58 +0200897
JonoB099c4782012-03-04 14:37:30 +0000898 return ($str === $validation_array[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 }
Eric Barnescccde962011-12-04 00:01:17 -0500900
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100901 // --------------------------------------------------------------------
902
903 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200904 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100905 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200906 * Check if the input value doesn't already exist
907 * in the specified database field.
908 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100909 * @param string
910 * @param field
911 * @return bool
912 */
913 public function is_unique($str, $field)
914 {
Eric Barnescccde962011-12-04 00:01:17 -0500915 list($table, $field) = explode('.', $field);
916 if (isset($this->CI->db))
917 {
918 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
919 return $query->num_rows() === 0;
920 }
921 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 /**
927 * Minimum Length
928 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 * @param string
930 * @param value
931 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200932 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100933 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200935 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
937 return FALSE;
938 }
939
tiyowan5b9fd2d2012-03-12 20:26:59 +0400940 if (MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200942 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
Barry Mienydd671972010-10-04 16:33:58 +0200944
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200945 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200949
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 /**
951 * Max Length
952 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 * @param string
954 * @param value
955 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200956 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100957 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200959 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 {
961 return FALSE;
962 }
963
tiyowan5b9fd2d2012-03-12 20:26:59 +0400964 if (MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200966 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200969 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 }
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 /**
975 * Exact Length
976 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 * @param string
978 * @param value
979 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200980 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100981 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200983 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
985 return FALSE;
986 }
987
tiyowan5b9fd2d2012-03-12 20:26:59 +0400988 if (MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200990 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 }
Barry Mienydd671972010-10-04 16:33:58 +0200992
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200993 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 }
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 /**
999 * Valid Email
1000 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 * @param string
1002 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001003 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001004 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001006 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 +00001007 }
1008
1009 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 /**
1012 * Valid Emails
1013 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 * @param string
1015 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001016 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001017 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 {
1019 if (strpos($str, ',') === FALSE)
1020 {
1021 return $this->valid_email(trim($str));
1022 }
Barry Mienydd671972010-10-04 16:33:58 +02001023
Pascal Kriete14287f32011-02-14 13:39:34 -05001024 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001026 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
1028 return FALSE;
1029 }
1030 }
Barry Mienydd671972010-10-04 16:33:58 +02001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 return TRUE;
1033 }
1034
1035 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001036
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 /**
1038 * Validate IP Address
1039 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001041 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001043 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 {
1045 return $this->CI->input->valid_ip($ip);
1046 }
1047
1048 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 /**
1051 * Alpha
1052 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 * @param string
1054 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001055 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001056 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001058 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 }
Barry Mienydd671972010-10-04 16:33:58 +02001060
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 /**
1064 * Alpha-numeric
1065 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 * @param string
1067 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001068 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001069 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001071 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 }
Barry Mienydd671972010-10-04 16:33:58 +02001073
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001075
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 /**
1077 * Alpha-numeric with underscores and dashes
1078 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 * @param string
1080 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001081 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001082 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001084 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 }
Barry Mienydd671972010-10-04 16:33:58 +02001086
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 /**
1090 * Numeric
1091 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 * @param string
1093 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001094 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001095 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001097 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001098
1099 }
1100
1101 // --------------------------------------------------------------------
1102
Barry Mienydd671972010-10-04 16:33:58 +02001103 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 * Integer
1105 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 * @param string
1107 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001108 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001109 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001111 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1112 }
1113
1114 // --------------------------------------------------------------------
1115
1116 /**
1117 * Decimal number
1118 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001119 * @param string
1120 * @return bool
1121 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001122 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001123 {
1124 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1125 }
1126
1127 // --------------------------------------------------------------------
1128
1129 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001130 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001131 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001132 * @param string
1133 * @return bool
1134 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001135 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001136 {
1137 if ( ! is_numeric($str))
1138 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001139 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001140 }
1141 return $str > $min;
1142 }
1143
1144 // --------------------------------------------------------------------
1145
1146 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001147 * Equal to or Greater than
1148 *
Nick Busey98c347d2012-02-02 11:07:03 -07001149 * @param string
1150 * @return bool
1151 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001152 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001153 {
1154 if ( ! is_numeric($str))
1155 {
1156 return FALSE;
1157 }
1158 return $str >= $min;
1159 }
1160
1161 // --------------------------------------------------------------------
Phil Sturgeonef112c02011-02-07 13:01:47 +00001162
1163 /**
1164 * Less than
1165 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001166 * @param string
1167 * @return bool
1168 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001169 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001170 {
1171 if ( ! is_numeric($str))
1172 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001173 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001174 }
1175 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001177
1178 // --------------------------------------------------------------------
1179
Barry Mienydd671972010-10-04 16:33:58 +02001180 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001181 * Equal to or Less than
1182 *
Nick Busey98c347d2012-02-02 11:07:03 -07001183 * @param string
1184 * @return bool
1185 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001186 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001187 {
1188 if ( ! is_numeric($str))
1189 {
1190 return FALSE;
1191 }
1192 return $str <= $max;
1193 }
1194
1195 // --------------------------------------------------------------------
1196
1197 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001198 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001199 *
Barry Mienydd671972010-10-04 16:33:58 +02001200 * @param string
1201 * @return bool
1202 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001203 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001204 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001205 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001206 }
1207
1208 // --------------------------------------------------------------------
1209
1210 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001211 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001212 *
Barry Mienydd671972010-10-04 16:33:58 +02001213 * @param string
1214 * @return bool
1215 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001216 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001217 {
Andrey Andreev6de924c2012-01-20 13:18:18 +02001218 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001219 }
1220
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001222
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 /**
1224 * Valid Base64
1225 *
1226 * Tests a string for characters outside of the Base64 alphabet
1227 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1228 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 * @param string
1230 * @return bool
1231 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001232 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 {
1234 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1235 }
Barry Mienydd671972010-10-04 16:33:58 +02001236
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001238
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 /**
1240 * Prep data for form
1241 *
1242 * This function allows HTML to be safely shown in a form.
1243 * Special characters are converted.
1244 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 * @param string
1246 * @return string
1247 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001248 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 {
1250 if (is_array($data))
1251 {
1252 foreach ($data as $key => $val)
1253 {
1254 $data[$key] = $this->prep_for_form($val);
1255 }
Barry Mienydd671972010-10-04 16:33:58 +02001256
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 return $data;
1258 }
Barry Mienydd671972010-10-04 16:33:58 +02001259
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 if ($this->_safe_form_data == FALSE OR $data === '')
1261 {
1262 return $data;
1263 }
1264
Andrey Andreev901573c2012-01-11 01:40:48 +02001265 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 }
Barry Mienydd671972010-10-04 16:33:58 +02001267
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001269
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 /**
1271 * Prep URL
1272 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 * @param string
1274 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001275 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001276 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001278 if ($str === 'http://' OR $str == '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
1280 return '';
1281 }
Barry Mienydd671972010-10-04 16:33:58 +02001282
Andrey Andreev901573c2012-01-11 01:40:48 +02001283 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001285 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 }
Barry Mienydd671972010-10-04 16:33:58 +02001287
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 return $str;
1289 }
Barry Mienydd671972010-10-04 16:33:58 +02001290
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001292
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 /**
1294 * Strip Image Tags
1295 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 * @param string
1297 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001298 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001299 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 {
1301 return $this->CI->input->strip_image_tags($str);
1302 }
Barry Mienydd671972010-10-04 16:33:58 +02001303
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001305
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 /**
1307 * XSS Clean
1308 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 * @param string
1310 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001311 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001312 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 {
Derek Jones5640a712010-04-23 11:22:40 -05001314 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 }
Barry Mienydd671972010-10-04 16:33:58 +02001316
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 /**
1320 * Convert PHP tags to entities
1321 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 * @param string
1323 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001324 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001325 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001327 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 }
1329
JonoB099c4782012-03-04 14:37:30 +00001330 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001331
1332 /**
1333 * Reset validation vars
1334 *
1335 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001336 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001337 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001338 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001339 */
JonoB883f80f2012-03-05 09:51:27 +00001340 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001341 {
JonoB099c4782012-03-04 14:37:30 +00001342 $this->_field_data = array();
1343 $this->_config_rules = array();
1344 $this->_error_array = array();
1345 $this->_error_messages = array();
1346 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001347 }
1348
Derek Allard2067d1a2008-11-13 22:59:24 +00001349}
Derek Allard2067d1a2008-11-13 22:59:24 +00001350
1351/* End of file Form_validation.php */
Andrey Andreev31cf46e2012-03-20 15:48:00 +02001352/* Location: ./system/libraries/Form_validation.php */