blob: 3c928529451d70d9345984544d0d918116a349ef [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 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000652
Andrey Andreev78f55772012-04-03 19:59:08 +0300653 continue;
654 }
655 else
656 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 $result = $this->$rule($postdata, $param);
658
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200659 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300661 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 }
663 else
664 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300665 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 }
667 }
Barry Mienydd671972010-10-04 16:33:58 +0200668
Andrey Andreev901573c2012-01-11 01:40:48 +0200669 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200671 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 if ( ! isset($this->_error_messages[$rule]))
673 {
674 if (FALSE === ($line = $this->CI->lang->line($rule)))
675 {
676 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200677 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 }
679 else
680 {
681 $line = $this->_error_messages[$rule];
682 }
Barry Mienydd671972010-10-04 16:33:58 +0200683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200685 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200686 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
Pascal Krietec1895832009-10-13 12:56:43 +0000688 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
Barry Mienydd671972010-10-04 16:33:58 +0200690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 // Build the error message
692 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
693
694 // Save the error message
695 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 if ( ! isset($this->_error_array[$row['field']]))
698 {
699 $this->_error_array[$row['field']] = $message;
700 }
Barry Mienydd671972010-10-04 16:33:58 +0200701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 return;
703 }
704 }
705 }
706
707 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 /**
710 * Translate a field name
711 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 * @param string the field name
713 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200714 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100715 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 {
717 // Do we need to translate the field name?
718 // We look for the prefix lang: to determine this
Andrey Andreev901573c2012-01-11 01:40:48 +0200719 if (strpos($fieldname, 'lang:') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
721 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200722 $line = substr($fieldname, 5);
723
Derek Jones37f4b9c2011-07-01 17:56:50 -0500724 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
726 {
727 return $line;
728 }
729 }
730
731 return $fieldname;
732 }
733
734 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 /**
737 * Get the value from a form
738 *
739 * Permits you to repopulate a form field with the value it was submitted
740 * with, or, if that value doesn't exist, with the default
741 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 * @param string the field name
743 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200744 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200745 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100746 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200748 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 {
750 return $default;
751 }
Barry Mienydd671972010-10-04 16:33:58 +0200752
Phil Sturgeon5c561802011-01-05 16:31:59 +0000753 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600754 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000755 if (is_array($this->_field_data[$field]['postdata']))
756 {
757 return array_shift($this->_field_data[$field]['postdata']);
758 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 return $this->_field_data[$field]['postdata'];
761 }
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 /**
766 * Set Select
767 *
768 * Enables pull-down lists to be set to the value the user
769 * selected in the event of an error
770 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 * @param string
772 * @param string
773 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200774 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100775 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200776 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200777 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200779 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 }
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 if (is_array($field))
784 {
785 if ( ! in_array($value, $field))
786 {
787 return '';
788 }
789 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200790 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200792 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 }
Barry Mienydd671972010-10-04 16:33:58 +0200794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 return ' selected="selected"';
796 }
Barry Mienydd671972010-10-04 16:33:58 +0200797
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200799
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 /**
801 * Set Radio
802 *
803 * Enables radio buttons to be set to the value the user
804 * selected in the event of an error
805 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 * @param string
807 * @param string
808 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200809 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100810 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200812 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200814 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 }
Barry Mienydd671972010-10-04 16:33:58 +0200816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 if (is_array($field))
819 {
820 if ( ! in_array($value, $field))
821 {
822 return '';
823 }
824 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200825 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200827 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 }
Barry Mienydd671972010-10-04 16:33:58 +0200829
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 return ' checked="checked"';
831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 /**
836 * Set Checkbox
837 *
838 * Enables checkboxes to be set to the value the user
839 * selected in the event of an error
840 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 * @param string
842 * @param string
843 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200844 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100845 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200847 // Logic is exactly the same as for radio fields
848 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 }
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 /**
854 * Required
855 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 * @param string
857 * @return bool
858 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100859 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300861 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 }
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500867 * Performs a Regular Expression match test.
868 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500869 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300870 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500871 * @return bool
872 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100873 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500874 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200875 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500876 }
877
878 // --------------------------------------------------------------------
879
880 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 * Match one field to another
882 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300884 * @param string field
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 * @return bool
886 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100887 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300889 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000890 if ( ! isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 {
Barry Mienydd671972010-10-04 16:33:58 +0200892 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 }
Barry Mienydd671972010-10-04 16:33:58 +0200894
JonoB099c4782012-03-04 14:37:30 +0000895 return ($str === $validation_array[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 }
Eric Barnescccde962011-12-04 00:01:17 -0500897
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100898 // --------------------------------------------------------------------
899
900 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200901 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100902 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200903 * Check if the input value doesn't already exist
904 * in the specified database field.
905 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100906 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300907 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100908 * @return bool
909 */
910 public function is_unique($str, $field)
911 {
Eric Barnescccde962011-12-04 00:01:17 -0500912 list($table, $field) = explode('.', $field);
913 if (isset($this->CI->db))
914 {
915 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
916 return $query->num_rows() === 0;
917 }
918 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600919 }
Barry Mienydd671972010-10-04 16:33:58 +0200920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200922
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 /**
924 * Minimum Length
925 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300927 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200929 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100930 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200932 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 {
934 return FALSE;
935 }
936
Andrey Andreev78f55772012-04-03 19:59:08 +0300937 return (MB_ENABLED === TRUE)
938 ? ($val <= mb_strlen($str))
939 : ($val <= strlen(str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 }
Barry Mienydd671972010-10-04 16:33:58 +0200941
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200943
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 /**
945 * Max Length
946 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300948 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200950 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100951 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200953 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
955 return FALSE;
956 }
957
Andrey Andreev78f55772012-04-03 19:59:08 +0300958 return (MB_ENABLED === TRUE)
959 ? ($val >= mb_strlen($str))
960 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 /**
966 * Exact Length
967 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300969 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200971 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100972 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200974 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 {
976 return FALSE;
977 }
978
Andrey Andreev78f55772012-04-03 19:59:08 +0300979 return (MB_ENABLED === TRUE)
980 ? (mb_strlen($str) == $val)
981 : (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
Barry Mienydd671972010-10-04 16:33:58 +0200983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 /**
987 * Valid Email
988 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 * @param string
990 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200991 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100992 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200994 return (bool) preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 }
996
997 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 /**
1000 * Valid Emails
1001 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 * @param string
1003 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001004 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001005 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 {
1007 if (strpos($str, ',') === FALSE)
1008 {
1009 return $this->valid_email(trim($str));
1010 }
Barry Mienydd671972010-10-04 16:33:58 +02001011
Pascal Kriete14287f32011-02-14 13:39:34 -05001012 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001014 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
1016 return FALSE;
1017 }
1018 }
Barry Mienydd671972010-10-04 16:33:58 +02001019
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 return TRUE;
1021 }
1022
1023 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001024
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 /**
1026 * Validate IP Address
1027 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001029 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001031 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 {
1033 return $this->CI->input->valid_ip($ip);
1034 }
1035
1036 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001037
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 /**
1039 * Alpha
1040 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 * @param string
1042 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001043 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001044 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001046 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 }
Barry Mienydd671972010-10-04 16:33:58 +02001048
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 /**
1052 * Alpha-numeric
1053 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 * @param string
1055 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001056 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001057 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001059 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 }
Barry Mienydd671972010-10-04 16:33:58 +02001061
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001063
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 /**
1065 * Alpha-numeric with underscores and dashes
1066 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 * @param string
1068 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001069 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001070 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001072 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 }
Barry Mienydd671972010-10-04 16:33:58 +02001074
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001076
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 /**
1078 * Numeric
1079 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 * @param string
1081 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001082 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001083 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001085 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001086
1087 }
1088
1089 // --------------------------------------------------------------------
1090
Barry Mienydd671972010-10-04 16:33:58 +02001091 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 * Integer
1093 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 * @param string
1095 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001096 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001097 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001099 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1100 }
1101
1102 // --------------------------------------------------------------------
1103
1104 /**
1105 * Decimal number
1106 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001107 * @param string
1108 * @return bool
1109 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001110 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001111 {
1112 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1113 }
1114
1115 // --------------------------------------------------------------------
1116
1117 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001118 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001119 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001120 * @param string
1121 * @return bool
1122 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001123 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001124 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001125 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001126 }
1127
1128 // --------------------------------------------------------------------
1129
1130 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001131 * Equal to or Greater than
1132 *
Nick Busey98c347d2012-02-02 11:07:03 -07001133 * @param string
1134 * @return bool
1135 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001136 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001137 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001138 return is_numeric($str) ? ($str >= $min) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001139 }
1140
1141 // --------------------------------------------------------------------
Phil Sturgeonef112c02011-02-07 13:01:47 +00001142
1143 /**
1144 * Less than
1145 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001146 * @param string
1147 * @return bool
1148 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001149 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001150 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001151 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001153
1154 // --------------------------------------------------------------------
1155
Barry Mienydd671972010-10-04 16:33:58 +02001156 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001157 * Equal to or Less than
1158 *
Nick Busey98c347d2012-02-02 11:07:03 -07001159 * @param string
1160 * @return bool
1161 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001162 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001163 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001164 return is_numeric($str) ? ($str <= $max) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001165 }
1166
1167 // --------------------------------------------------------------------
1168
1169 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001170 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001171 *
Barry Mienydd671972010-10-04 16:33:58 +02001172 * @param string
1173 * @return bool
1174 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001175 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001176 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001177 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001178 }
1179
1180 // --------------------------------------------------------------------
1181
1182 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001183 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001184 *
Barry Mienydd671972010-10-04 16:33:58 +02001185 * @param string
1186 * @return bool
1187 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001188 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001189 {
Andrey Andreev6de924c2012-01-20 13:18:18 +02001190 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001191 }
1192
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001194
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 /**
1196 * Valid Base64
1197 *
1198 * Tests a string for characters outside of the Base64 alphabet
1199 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1200 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 * @param string
1202 * @return bool
1203 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001204 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 {
1206 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1207 }
Barry Mienydd671972010-10-04 16:33:58 +02001208
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001210
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 /**
1212 * Prep data for form
1213 *
1214 * This function allows HTML to be safely shown in a form.
1215 * Special characters are converted.
1216 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 * @param string
1218 * @return string
1219 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001220 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 {
1222 if (is_array($data))
1223 {
1224 foreach ($data as $key => $val)
1225 {
1226 $data[$key] = $this->prep_for_form($val);
1227 }
Barry Mienydd671972010-10-04 16:33:58 +02001228
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 return $data;
1230 }
Barry Mienydd671972010-10-04 16:33:58 +02001231
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 if ($this->_safe_form_data == FALSE OR $data === '')
1233 {
1234 return $data;
1235 }
1236
Andrey Andreev901573c2012-01-11 01:40:48 +02001237 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 }
Barry Mienydd671972010-10-04 16:33:58 +02001239
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001241
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 /**
1243 * Prep URL
1244 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 * @param string
1246 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001247 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001248 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001250 if ($str === 'http://' OR $str == '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 {
1252 return '';
1253 }
Barry Mienydd671972010-10-04 16:33:58 +02001254
Andrey Andreev901573c2012-01-11 01:40:48 +02001255 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001257 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 }
Barry Mienydd671972010-10-04 16:33:58 +02001259
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 return $str;
1261 }
Barry Mienydd671972010-10-04 16:33:58 +02001262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001264
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 /**
1266 * Strip Image Tags
1267 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 * @param string
1269 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001270 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001271 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 {
1273 return $this->CI->input->strip_image_tags($str);
1274 }
Barry Mienydd671972010-10-04 16:33:58 +02001275
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001277
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 /**
1279 * XSS Clean
1280 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 * @param string
1282 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001283 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001284 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 {
Derek Jones5640a712010-04-23 11:22:40 -05001286 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 }
Barry Mienydd671972010-10-04 16:33:58 +02001288
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001290
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 /**
1292 * Convert PHP tags to entities
1293 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 * @param string
1295 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001296 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001297 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001299 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 }
1301
JonoB099c4782012-03-04 14:37:30 +00001302 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001303
1304 /**
1305 * Reset validation vars
1306 *
1307 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001308 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001309 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001310 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001311 */
JonoB883f80f2012-03-05 09:51:27 +00001312 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001313 {
JonoB099c4782012-03-04 14:37:30 +00001314 $this->_field_data = array();
1315 $this->_config_rules = array();
1316 $this->_error_array = array();
1317 $this->_error_messages = array();
1318 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001319 }
1320
Derek Allard2067d1a2008-11-13 22:59:24 +00001321}
Derek Allard2067d1a2008-11-13 22:59:24 +00001322
1323/* End of file Form_validation.php */
Andrey Andreev31cf46e2012-03-20 15:48:00 +02001324/* Location: ./system/libraries/Form_validation.php */