blob: 22bc7ddf342c0d13d021c178a6f13cb2d33df61b [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;
Andrey Andreev78f55772012-04-03 19:59:08 +030040 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;
48 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
Andrey Andreev78f55772012-04-03 19:59:08 +030091 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +000092 */
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...
Andrey Andreev78f55772012-04-03 19:59:08 +0300109 if ( ! isset($row['field'], $row['rules']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
111 continue;
112 }
113
114 // If the field label wasn't passed we use the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300115 $label = isset($row['label']) ? $row['label'] : $row['field'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000116
117 // Here we go!
118 $this->set_rules($row['field'], $label, $row['rules']);
119 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300120
Greg Aker9f9af602010-11-10 15:41:51 -0600121 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200125 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Greg Aker9f9af602010-11-10 15:41:51 -0600127 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
129
130 // If the field label wasn't passed we use the field name
131 $label = ($label == '') ? $field : $label;
132
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200133 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200134 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200135 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200136 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 // Note: Due to a bug in current() that affects some versions
138 // of PHP we can not pass function call directly into it
139 $x = explode('[', $field);
140 $indexes[] = current($x);
141
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200142 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200144 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200146 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 $is_array = TRUE;
151 }
152 else
153 {
Barry Mienydd671972010-10-04 16:33:58 +0200154 $indexes = array();
155 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
158 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000160 'field' => $field,
161 'label' => $label,
162 'rules' => $rules,
163 'is_array' => $is_array,
164 'keys' => $indexes,
165 'postdata' => NULL,
166 'error' => ''
167 );
Greg Aker9f9af602010-11-10 15:41:51 -0600168
169 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 /**
JonoB099c4782012-03-04 14:37:30 +0000175 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200176 *
JonoB099c4782012-03-04 14:37:30 +0000177 * If an array is set through this method, then this array will
178 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200179 *
180 * Note that if you are validating multiple arrays, then the
181 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000182 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200183 *
184 * @param array $data
185 * @return void
JonoB099c4782012-03-04 14:37:30 +0000186 */
187 public function set_data($data = '')
188 {
189 if ( ! empty($data) && is_array($data))
190 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200191 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000192 }
193 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200194
JonoB099c4782012-03-04 14:37:30 +0000195 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000196
197 /**
198 * Set Error Message
199 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300200 * Lets users set their own error messages on the fly. Note:
201 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300203 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300205 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100207 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
209 if ( ! is_array($lang))
210 {
211 $lang = array($lang => $val);
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600215 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 /**
221 * Set The Error Delimiter
222 *
223 * Permits a prefix/suffix to be added to each error message
224 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 * @param string
226 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300227 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200228 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100229 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
231 $this->_error_prefix = $prefix;
232 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600233 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
235
236 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 /**
239 * Get Error Message
240 *
241 * Gets the error message associated with a particular field
242 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 * @param string the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300244 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200245 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100246 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200247 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300248 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
250 return '';
251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 if ($prefix == '')
254 {
255 $prefix = $this->_error_prefix;
256 }
257
258 if ($suffix == '')
259 {
260 $suffix = $this->_error_suffix;
261 }
262
263 return $prefix.$this->_field_data[$field]['error'].$suffix;
264 }
265
266 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100269 * Get Array of Error Messages
270 *
271 * Returns the error messages as an array
272 *
273 * @return array
274 */
275 public function error_array()
276 {
277 return $this->_error_array;
278 }
279
280 // --------------------------------------------------------------------
281
282 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 * Error String
284 *
285 * Returns the error messages as a string, wrapped in the error delimiters
286 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 * @param string
288 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300289 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200290 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100291 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
293 // No errrors, validation passes!
294 if (count($this->_error_array) === 0)
295 {
296 return '';
297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 if ($prefix == '')
300 {
301 $prefix = $this->_error_prefix;
302 }
303
304 if ($suffix == '')
305 {
306 $suffix = $this->_error_suffix;
307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 // Generate the error string
310 $str = '';
311 foreach ($this->_error_array as $val)
312 {
313 if ($val != '')
314 {
315 $str .= $prefix.$val.$suffix."\n";
316 }
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 return $str;
320 }
321
322 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 /**
325 * Run the Validator
326 *
327 * This function does all the work.
328 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200330 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100331 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500333 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300334 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000335 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 return FALSE;
338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 // Does the _field_data array containing the validation rules exist?
341 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200342 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500344 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200345 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
347 return FALSE;
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // Is there a validation rule for the particular URI being accessed?
351 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200352
Andrey Andreev6de924c2012-01-20 13:18:18 +0200353 if ($uri != '' && isset($this->_config_rules[$uri]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
355 $this->set_rules($this->_config_rules[$uri]);
356 }
357 else
358 {
359 $this->set_rules($this->_config_rules);
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Andrey Andreev901573c2012-01-11 01:40:48 +0200362 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200363 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200365 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 return FALSE;
367 }
368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // Load the language file containing error messages
371 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200372
373 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 // corresponding $_POST item and test for errors
375 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200376 {
JonoB099c4782012-03-04 14:37:30 +0000377 // 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 +0000378 // 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 +0200379 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
JonoB099c4782012-03-04 14:37:30 +0000381 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300383 elseif ( ! empty($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300385 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
388 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 }
390
391 // Did we end up with any errors?
392 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 if ($total_errors > 0)
394 {
395 $this->_safe_form_data = TRUE;
396 }
397
398 // Now we need to re-set the POST data with the new, processed data
399 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200400
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200401 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
403
404 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 /**
407 * Traverse a multidimensional $_POST array index until the data is found
408 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 * @param array
410 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300411 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200413 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100414 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200416 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200418 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 return $array;
422 }
423
424 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 /**
427 * Re-populate the _POST array with our finalized and processed data
428 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200429 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200430 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100431 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
433 foreach ($this->_field_data as $field => $row)
434 {
435 if ( ! is_null($row['postdata']))
436 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200437 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
439 if (isset($_POST[$row['field']]))
440 {
441 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
442 }
443 }
444 else
445 {
Derek Jones63eeae32009-02-10 19:08:56 +0000446 // start with a reference
447 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Jones63eeae32009-02-10 19:08:56 +0000449 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200450 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Derek Jones63eeae32009-02-10 19:08:56 +0000452 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
454 else
455 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 foreach ($row['keys'] as $val)
457 {
Derek Jones63eeae32009-02-10 19:08:56 +0000458 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
460 }
Derek Jones63eeae32009-02-10 19:08:56 +0000461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000463 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 $array = array();
465 foreach ($row['postdata'] as $k => $v)
466 {
467 $array[$k] = $this->prep_for_form($v);
468 }
Derek Jones63eeae32009-02-10 19:08:56 +0000469
470 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
472 else
Derek Jones63eeae32009-02-10 19:08:56 +0000473 {
474 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477 }
478 }
479 }
480
481 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 /**
484 * Executes the Validation routines
485 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 * @param array
487 * @param array
488 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300489 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200491 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100492 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
494 // If the $_POST data is an array we will run a recursive call
495 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200496 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 foreach ($postdata as $key => $val)
498 {
499 $this->_execute($row, $rules, $val, $cycles);
500 $cycles++;
501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 return;
504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 // If the field is blank, but NOT required, no further tests are necessary
507 $callback = FALSE;
Andrey Andreev6de924c2012-01-20 13:18:18 +0200508 if ( ! in_array('required', $rules) && is_null($postdata))
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
510 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200511 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
513 $callback = TRUE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300514 $rules = array(1 => $match[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 }
516 else
517 {
518 return;
519 }
520 }
521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev6de924c2012-01-20 13:18:18 +0200523 if (is_null($postdata) && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
525 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
526 {
527 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300528 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 if ( ! isset($this->_error_messages[$type]))
531 {
532 if (FALSE === ($line = $this->CI->lang->line($type)))
533 {
534 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200535 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 }
537 else
538 {
539 $line = $this->_error_messages[$type];
540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 // Build the error message
543 $message = sprintf($line, $this->_translate_fieldname($row['label']));
544
545 // Save the error message
546 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 if ( ! isset($this->_error_array[$row['field']]))
549 {
550 $this->_error_array[$row['field']] = $message;
551 }
552 }
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 return;
555 }
556
557 // --------------------------------------------------------------------
558
559 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300560 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 {
562 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 // We set the $postdata variable with the current data in our master array so that
565 // each cycle of the loop is dealing with the processed data from the last cycle
Andrey Andreev6de924c2012-01-20 13:18:18 +0200566 if ($row['is_array'] == TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
568 // We shouldn't need this safety, but just in case there isn't an array index
569 // associated with this cycle we'll bail out
570 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
571 {
572 continue;
573 }
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
576 $_in_array = TRUE;
577 }
578 else
579 {
580 $postdata = $this->_field_data[$row['field']]['postdata'];
581 }
582
Barry Mienydd671972010-10-04 16:33:58 +0200583 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200585 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 $rule = substr($rule, 9);
588 $callback = TRUE;
589 }
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 // Strip the parameter (if exists) from the rule
592 // Rules can contain a parameter: max_length[5]
593 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200594 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
596 $rule = $match[1];
597 $param = $match[2];
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 // Call the function that corresponds to the rule
601 if ($callback === TRUE)
602 {
603 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200604 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200605 log_message('debug', 'Unable to find callback validation rule: '.$rule);
606 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200608 else
609 {
610 // Run the function and grab the result
611 $result = $this->CI->$rule($postdata, $param);
612 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000613
614 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200615 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300617 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 }
619 else
620 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300621 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 }
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200625 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000627 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 }
629 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300630 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200631 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300632 // If our own wrapper function doesn't exist we see if a native PHP function does.
633 // Users can use any native PHP function call that has one param.
634 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
Andrey Andreev320d37c2012-04-03 20:21:39 +0300636 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200637
Andrey Andreev78f55772012-04-03 19:59:08 +0300638 if ($_in_array === TRUE)
639 {
640 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 }
patwork02404a12011-04-08 15:45:46 +0200642 else
643 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300644 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200645 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200647 else
648 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300649 log_message('debug', 'Unable to find validation rule: '.$rule);
650 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200651 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300652 }
653 else
654 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 $result = $this->$rule($postdata, $param);
656
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200657 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300659 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
661 else
662 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300663 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Andrey Andreev901573c2012-01-11 01:40:48 +0200667 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200669 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 if ( ! isset($this->_error_messages[$rule]))
671 {
672 if (FALSE === ($line = $this->CI->lang->line($rule)))
673 {
674 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200675 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 }
677 else
678 {
679 $line = $this->_error_messages[$rule];
680 }
Barry Mienydd671972010-10-04 16:33:58 +0200681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200683 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200684 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
Pascal Krietec1895832009-10-13 12:56:43 +0000686 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 // Build the error message
690 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
691
692 // Save the error message
693 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 if ( ! isset($this->_error_array[$row['field']]))
696 {
697 $this->_error_array[$row['field']] = $message;
698 }
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 return;
701 }
702 }
703 }
704
705 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 /**
708 * Translate a field name
709 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 * @param string the field name
711 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200712 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100713 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 {
715 // Do we need to translate the field name?
716 // We look for the prefix lang: to determine this
Andrey Andreev901573c2012-01-11 01:40:48 +0200717 if (strpos($fieldname, 'lang:') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
719 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200720 $line = substr($fieldname, 5);
721
Derek Jones37f4b9c2011-07-01 17:56:50 -0500722 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
724 {
725 return $line;
726 }
727 }
728
729 return $fieldname;
730 }
731
732 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 /**
735 * Get the value from a form
736 *
737 * Permits you to repopulate a form field with the value it was submitted
738 * with, or, if that value doesn't exist, with the default
739 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 * @param string the field name
741 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200742 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200743 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100744 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200746 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 {
748 return $default;
749 }
Barry Mienydd671972010-10-04 16:33:58 +0200750
Phil Sturgeon5c561802011-01-05 16:31:59 +0000751 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600752 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000753 if (is_array($this->_field_data[$field]['postdata']))
754 {
755 return array_shift($this->_field_data[$field]['postdata']);
756 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 return $this->_field_data[$field]['postdata'];
759 }
Barry Mienydd671972010-10-04 16:33:58 +0200760
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 /**
764 * Set Select
765 *
766 * Enables pull-down lists to be set to the value the user
767 * selected in the event of an error
768 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 * @param string
770 * @param string
771 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200772 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100773 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200774 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200775 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200777 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 }
Barry Mienydd671972010-10-04 16:33:58 +0200779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 if (is_array($field))
782 {
783 if ( ! in_array($value, $field))
784 {
785 return '';
786 }
787 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200788 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200790 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 }
Barry Mienydd671972010-10-04 16:33:58 +0200792
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 return ' selected="selected"';
794 }
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200797
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 /**
799 * Set Radio
800 *
801 * Enables radio buttons to be set to the value the user
802 * selected in the event of an error
803 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 * @param string
805 * @param string
806 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200807 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100808 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200810 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200812 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 }
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 if (is_array($field))
817 {
818 if ( ! in_array($value, $field))
819 {
820 return '';
821 }
822 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200823 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200825 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 }
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 return ' checked="checked"';
829 }
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 /**
834 * Set Checkbox
835 *
836 * Enables checkboxes to be set to the value the user
837 * selected in the event of an error
838 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 * @param string
840 * @param string
841 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200842 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100843 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200845 // Logic is exactly the same as for radio fields
846 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 }
Barry Mienydd671972010-10-04 16:33:58 +0200848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 /**
852 * Required
853 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 * @param string
855 * @return bool
856 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100857 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300859 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 }
Barry Mienydd671972010-10-04 16:33:58 +0200861
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500865 * Performs a Regular Expression match test.
866 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500867 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300868 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500869 * @return bool
870 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100871 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500872 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200873 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500874 }
875
876 // --------------------------------------------------------------------
877
878 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 * Match one field to another
880 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300882 * @param string field
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 * @return bool
884 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100885 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300887 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000888 if ( ! isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
Barry Mienydd671972010-10-04 16:33:58 +0200890 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 }
Barry Mienydd671972010-10-04 16:33:58 +0200892
JonoB099c4782012-03-04 14:37:30 +0000893 return ($str === $validation_array[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 }
Eric Barnescccde962011-12-04 00:01:17 -0500895
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100896 // --------------------------------------------------------------------
897
898 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200899 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100900 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200901 * Check if the input value doesn't already exist
902 * in the specified database field.
903 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100904 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300905 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100906 * @return bool
907 */
908 public function is_unique($str, $field)
909 {
Eric Barnescccde962011-12-04 00:01:17 -0500910 list($table, $field) = explode('.', $field);
911 if (isset($this->CI->db))
912 {
913 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
914 return $query->num_rows() === 0;
915 }
916 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600917 }
Barry Mienydd671972010-10-04 16:33:58 +0200918
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 /**
922 * Minimum Length
923 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300925 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200927 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100928 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200930 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
932 return FALSE;
933 }
934
Andrey Andreev78f55772012-04-03 19:59:08 +0300935 return (MB_ENABLED === TRUE)
936 ? ($val <= mb_strlen($str))
937 : ($val <= strlen(str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 }
Barry Mienydd671972010-10-04 16:33:58 +0200939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200941
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 /**
943 * Max Length
944 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300946 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200948 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100949 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200951 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
953 return FALSE;
954 }
955
Andrey Andreev78f55772012-04-03 19:59:08 +0300956 return (MB_ENABLED === TRUE)
957 ? ($val >= mb_strlen($str))
958 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 /**
964 * Exact Length
965 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300967 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200969 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100970 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200972 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
974 return FALSE;
975 }
976
Andrey Andreev78f55772012-04-03 19:59:08 +0300977 return (MB_ENABLED === TRUE)
978 ? (mb_strlen($str) == $val)
979 : (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
Barry Mienydd671972010-10-04 16:33:58 +0200981
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 /**
985 * Valid Email
986 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 * @param string
988 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200989 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100990 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200992 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 +0000993 }
994
995 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 /**
998 * Valid Emails
999 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 * @param string
1001 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001002 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001003 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 {
1005 if (strpos($str, ',') === FALSE)
1006 {
1007 return $this->valid_email(trim($str));
1008 }
Barry Mienydd671972010-10-04 16:33:58 +02001009
Pascal Kriete14287f32011-02-14 13:39:34 -05001010 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001012 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 {
1014 return FALSE;
1015 }
1016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 return TRUE;
1019 }
1020
1021 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001022
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 /**
1024 * Validate IP Address
1025 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001027 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001029 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 {
1031 return $this->CI->input->valid_ip($ip);
1032 }
1033
1034 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001035
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 /**
1037 * Alpha
1038 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 * @param string
1040 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001041 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001042 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001044 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 }
Barry Mienydd671972010-10-04 16:33:58 +02001046
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001048
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 /**
1050 * Alpha-numeric
1051 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 * @param string
1053 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001054 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001055 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001057 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 }
Barry Mienydd671972010-10-04 16:33:58 +02001059
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001061
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 /**
1063 * Alpha-numeric with underscores and dashes
1064 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 * @param string
1066 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001067 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001068 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001070 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 }
Barry Mienydd671972010-10-04 16:33:58 +02001072
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001074
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 /**
1076 * Numeric
1077 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 * @param string
1079 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001080 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001081 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001083 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001084
1085 }
1086
1087 // --------------------------------------------------------------------
1088
Barry Mienydd671972010-10-04 16:33:58 +02001089 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 * Integer
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 integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001097 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1098 }
1099
1100 // --------------------------------------------------------------------
1101
1102 /**
1103 * Decimal number
1104 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001105 * @param string
1106 * @return bool
1107 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001108 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001109 {
1110 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1111 }
1112
1113 // --------------------------------------------------------------------
1114
1115 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001116 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001117 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001118 * @param string
1119 * @return bool
1120 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001121 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001122 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001123 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001124 }
1125
1126 // --------------------------------------------------------------------
1127
1128 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001129 * Equal to or Greater than
1130 *
Nick Busey98c347d2012-02-02 11:07:03 -07001131 * @param string
1132 * @return bool
1133 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001134 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001135 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001136 return is_numeric($str) ? ($str >= $min) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001137 }
1138
1139 // --------------------------------------------------------------------
Phil Sturgeonef112c02011-02-07 13:01:47 +00001140
1141 /**
1142 * Less than
1143 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001144 * @param string
1145 * @return bool
1146 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001147 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001148 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001149 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001151
1152 // --------------------------------------------------------------------
1153
Barry Mienydd671972010-10-04 16:33:58 +02001154 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001155 * Equal to or Less than
1156 *
Nick Busey98c347d2012-02-02 11:07:03 -07001157 * @param string
1158 * @return bool
1159 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001160 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001161 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001162 return is_numeric($str) ? ($str <= $max) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001163 }
1164
1165 // --------------------------------------------------------------------
1166
1167 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001168 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001169 *
Barry Mienydd671972010-10-04 16:33:58 +02001170 * @param string
1171 * @return bool
1172 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001173 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001174 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001175 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001176 }
1177
1178 // --------------------------------------------------------------------
1179
1180 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001181 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001182 *
Barry Mienydd671972010-10-04 16:33:58 +02001183 * @param string
1184 * @return bool
1185 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001186 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001187 {
Andrey Andreev6de924c2012-01-20 13:18:18 +02001188 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001189 }
1190
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001192
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 /**
1194 * Valid Base64
1195 *
1196 * Tests a string for characters outside of the Base64 alphabet
1197 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1198 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 * @param string
1200 * @return bool
1201 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001202 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 {
1204 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1205 }
Barry Mienydd671972010-10-04 16:33:58 +02001206
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001208
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 /**
1210 * Prep data for form
1211 *
1212 * This function allows HTML to be safely shown in a form.
1213 * Special characters are converted.
1214 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 * @param string
1216 * @return string
1217 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001218 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 {
1220 if (is_array($data))
1221 {
1222 foreach ($data as $key => $val)
1223 {
1224 $data[$key] = $this->prep_for_form($val);
1225 }
Barry Mienydd671972010-10-04 16:33:58 +02001226
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 return $data;
1228 }
Barry Mienydd671972010-10-04 16:33:58 +02001229
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 if ($this->_safe_form_data == FALSE OR $data === '')
1231 {
1232 return $data;
1233 }
1234
Andrey Andreev901573c2012-01-11 01:40:48 +02001235 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001239
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 /**
1241 * Prep URL
1242 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 * @param string
1244 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001245 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001246 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001248 if ($str === 'http://' OR $str == '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 {
1250 return '';
1251 }
Barry Mienydd671972010-10-04 16:33:58 +02001252
Andrey Andreev901573c2012-01-11 01:40:48 +02001253 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001255 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 }
Barry Mienydd671972010-10-04 16:33:58 +02001257
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 return $str;
1259 }
Barry Mienydd671972010-10-04 16:33:58 +02001260
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 /**
1264 * Strip Image Tags
1265 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 * @param string
1267 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001268 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001269 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 {
1271 return $this->CI->input->strip_image_tags($str);
1272 }
Barry Mienydd671972010-10-04 16:33:58 +02001273
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001275
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 /**
1277 * XSS Clean
1278 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 * @param string
1280 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001281 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001282 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 {
Derek Jones5640a712010-04-23 11:22:40 -05001284 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 }
Barry Mienydd671972010-10-04 16:33:58 +02001286
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001288
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 /**
1290 * Convert PHP tags to entities
1291 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 * @param string
1293 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001294 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001295 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001297 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 }
1299
JonoB099c4782012-03-04 14:37:30 +00001300 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001301
1302 /**
1303 * Reset validation vars
1304 *
1305 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001306 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001307 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001308 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001309 */
JonoB883f80f2012-03-05 09:51:27 +00001310 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001311 {
JonoB099c4782012-03-04 14:37:30 +00001312 $this->_field_data = array();
1313 $this->_config_rules = array();
1314 $this->_error_array = array();
1315 $this->_error_messages = array();
1316 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001317 }
1318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319}
Derek Allard2067d1a2008-11-13 22:59:24 +00001320
1321/* End of file Form_validation.php */
Andrey Andreev31cf46e2012-03-20 15:48:00 +02001322/* Location: ./system/libraries/Form_validation.php */