blob: ebbf123fdab1ae6e5ad4430d4fdbe7ccd10ae991 [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
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
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 Andreev78f55772012-04-03 19:59:08 +030065
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 Andreev3b2c5082012-03-07 22:49:24 +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;
Phil Sturgeonc3828712011-01-19 12:31:47 +0000233
Greg Aker9f9af602010-11-10 15:41:51 -0600234 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
236
237 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 /**
240 * Get Error Message
241 *
242 * Gets the error message associated with a particular field
243 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 * @param string the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300245 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200246 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100247 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200248 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300249 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
251 return '';
252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 if ($prefix == '')
255 {
256 $prefix = $this->_error_prefix;
257 }
258
259 if ($suffix == '')
260 {
261 $suffix = $this->_error_suffix;
262 }
263
264 return $prefix.$this->_field_data[$field]['error'].$suffix;
265 }
266
267 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100270 * Get Array of Error Messages
271 *
272 * Returns the error messages as an array
273 *
274 * @return array
275 */
276 public function error_array()
277 {
278 return $this->_error_array;
279 }
280
281 // --------------------------------------------------------------------
282
283 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 * Error String
285 *
286 * Returns the error messages as a string, wrapped in the error delimiters
287 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 * @param string
289 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300290 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200291 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100292 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
294 // No errrors, validation passes!
295 if (count($this->_error_array) === 0)
296 {
297 return '';
298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 if ($prefix == '')
301 {
302 $prefix = $this->_error_prefix;
303 }
304
305 if ($suffix == '')
306 {
307 $suffix = $this->_error_suffix;
308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // Generate the error string
311 $str = '';
312 foreach ($this->_error_array as $val)
313 {
314 if ($val != '')
315 {
316 $str .= $prefix.$val.$suffix."\n";
317 }
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 return $str;
321 }
322
323 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 /**
326 * Run the Validator
327 *
328 * This function does all the work.
329 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200331 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100332 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500334 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300335 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000336 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 return FALSE;
339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // Does the _field_data array containing the validation rules exist?
342 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200343 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500345 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200346 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
348 return FALSE;
349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // Is there a validation rule for the particular URI being accessed?
352 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200353
Andrey Andreev78f55772012-04-03 19:59:08 +0300354 if ($uri != '' && isset($this->_config_rules[$uri]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 $this->set_rules($this->_config_rules[$uri]);
357 }
358 else
359 {
360 $this->set_rules($this->_config_rules);
361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // We're we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200364 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300366 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 return FALSE;
368 }
369 }
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 // Load the language file containing error messages
372 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200373
374 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 // corresponding $_POST item and test for errors
376 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200377 {
JonoB099c4782012-03-04 14:37:30 +0000378 // 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 +0000379 // Depending on whether the field name is an array or a string will determine where we get it from.
Barry Mienydd671972010-10-04 16:33:58 +0200380
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200381 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
JonoB099c4782012-03-04 14:37:30 +0000383 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300385 elseif ( ! empty($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300387 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
390 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
392
393 // Did we end up with any errors?
394 $total_errors = count($this->_error_array);
395
396 if ($total_errors > 0)
397 {
398 $this->_safe_form_data = TRUE;
399 }
400
401 // Now we need to re-set the POST data with the new, processed data
402 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200403
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200404 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
406
407 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 /**
410 * Traverse a multidimensional $_POST array index until the data is found
411 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * @param array
413 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300414 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200416 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100417 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200419 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200421 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 return $array;
425 }
426
427 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 /**
430 * Re-populate the _POST array with our finalized and processed data
431 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300432 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200433 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100434 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
436 foreach ($this->_field_data as $field => $row)
437 {
438 if ( ! is_null($row['postdata']))
439 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200440 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
442 if (isset($_POST[$row['field']]))
443 {
444 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
445 }
446 }
447 else
448 {
Derek Jones63eeae32009-02-10 19:08:56 +0000449 // start with a reference
450 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Jones63eeae32009-02-10 19:08:56 +0000452 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200453 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
Derek Jones63eeae32009-02-10 19:08:56 +0000455 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
457 else
458 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 foreach ($row['keys'] as $val)
460 {
Derek Jones63eeae32009-02-10 19:08:56 +0000461 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 }
463 }
Derek Jones63eeae32009-02-10 19:08:56 +0000464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000466 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 $array = array();
468 foreach ($row['postdata'] as $k => $v)
469 {
470 $array[$k] = $this->prep_for_form($v);
471 }
Derek Jones63eeae32009-02-10 19:08:56 +0000472
473 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
475 else
Derek Jones63eeae32009-02-10 19:08:56 +0000476 {
477 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480 }
481 }
482 }
483
484 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 /**
487 * Executes the Validation routines
488 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 * @param array
490 * @param array
491 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300492 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200494 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100495 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
497 // If the $_POST data is an array we will run a recursive call
498 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200499 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 foreach ($postdata as $key => $val)
501 {
502 $this->_execute($row, $rules, $val, $cycles);
503 $cycles++;
504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 return;
507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 // --------------------------------------------------------------------
510
511 // If the field is blank, but NOT required, no further tests are necessary
512 $callback = FALSE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300513 if ( ! in_array('required', $rules) && is_null($postdata))
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
515 // Before we bail out, does the rule contain a callback?
Andrey Andreev78f55772012-04-03 19:59:08 +0300516 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 $callback = TRUE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300519 $rules = array(1 => $match[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 }
521 else
522 {
523 return;
524 }
525 }
526
527 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev78f55772012-04-03 19:59:08 +0300530 if (is_null($postdata) && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 {
532 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
533 {
534 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300535 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 if ( ! isset($this->_error_messages[$type]))
538 {
539 if (FALSE === ($line = $this->CI->lang->line($type)))
540 {
541 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200542 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 }
544 else
545 {
546 $line = $this->_error_messages[$type];
547 }
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 // Build the error message
550 $message = sprintf($line, $this->_translate_fieldname($row['label']));
551
552 // Save the error message
553 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 if ( ! isset($this->_error_array[$row['field']]))
556 {
557 $this->_error_array[$row['field']] = $message;
558 }
559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 return;
562 }
563
564 // --------------------------------------------------------------------
565
566 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300567 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
569 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 // We set the $postdata variable with the current data in our master array so that
572 // each cycle of the loop is dealing with the processed data from the last cycle
Andrey Andreev78f55772012-04-03 19:59:08 +0300573 if ($row['is_array'] == TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
575 // We shouldn't need this safety, but just in case there isn't an array index
576 // associated with this cycle we'll bail out
577 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
578 {
579 continue;
580 }
Barry Mienydd671972010-10-04 16:33:58 +0200581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
583 $_in_array = TRUE;
584 }
585 else
586 {
587 $postdata = $this->_field_data[$row['field']]['postdata'];
588 }
589
590 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200591
592 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 $callback = FALSE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300594 if (substr($rule, 0, 9) === 'callback_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
596 $rule = substr($rule, 9);
597 $callback = TRUE;
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 // Strip the parameter (if exists) from the rule
601 // Rules can contain a parameter: max_length[5]
602 $param = FALSE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300603 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
605 $rule = $match[1];
606 $param = $match[2];
607 }
Barry Mienydd671972010-10-04 16:33:58 +0200608
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 // Call the function that corresponds to the rule
610 if ($callback === TRUE)
611 {
612 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200613 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 continue;
615 }
Barry Mienydd671972010-10-04 16:33:58 +0200616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 // Run the function and grab the result
618 $result = $this->CI->$rule($postdata, $param);
619
620 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200621 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300623 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 }
625 else
626 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300627 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 }
Barry Mienydd671972010-10-04 16:33:58 +0200629
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev78f55772012-04-03 19:59:08 +0300631 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000633 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 }
635 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300636 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200637 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300638 // If our own wrapper function doesn't exist we see if a native PHP function does.
639 // Users can use any native PHP function call that has one param.
640 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300642 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200643
Andrey Andreev78f55772012-04-03 19:59:08 +0300644 if ($_in_array === TRUE)
645 {
646 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 }
patwork02404a12011-04-08 15:45:46 +0200648 else
649 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300650 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200651 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300652 }
653 else
654 {
655 log_message('debug', 'Unable to find validation rule: '.$rule);
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 }
657
Andrey Andreev78f55772012-04-03 19:59:08 +0300658 continue;
659 }
660 else
661 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 $result = $this->$rule($postdata, $param);
663
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200664 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300666 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 }
668 else
669 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300670 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 }
672 }
Barry Mienydd671972010-10-04 16:33:58 +0200673
Andrey Andreev78f55772012-04-03 19:59:08 +0300674 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200676 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 if ( ! isset($this->_error_messages[$rule]))
678 {
679 if (FALSE === ($line = $this->CI->lang->line($rule)))
680 {
681 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200682 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 }
684 else
685 {
686 $line = $this->_error_messages[$rule];
687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 // Is the parameter we are inserting into the error message the name
Andrey Andreev78f55772012-04-03 19:59:08 +0300690 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200691 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
Pascal Krietec1895832009-10-13 12:56:43 +0000693 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 }
Barry Mienydd671972010-10-04 16:33:58 +0200695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 // Build the error message
697 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
698
699 // Save the error message
700 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 if ( ! isset($this->_error_array[$row['field']]))
703 {
704 $this->_error_array[$row['field']] = $message;
705 }
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 return;
708 }
709 }
710 }
711
712 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 /**
715 * Translate a field name
716 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 * @param string the field name
718 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200719 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100720 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 {
722 // Do we need to translate the field name?
723 // We look for the prefix lang: to determine this
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200724 if (substr($fieldname, 0, 5) === 'lang:')
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 {
726 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200727 $line = substr($fieldname, 5);
728
Derek Jones37f4b9c2011-07-01 17:56:50 -0500729 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
731 {
732 return $line;
733 }
734 }
735
736 return $fieldname;
737 }
738
739 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200740
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 /**
742 * Get the value from a form
743 *
744 * Permits you to repopulate a form field with the value it was submitted
745 * with, or, if that value doesn't exist, with the default
746 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 * @param string the field name
748 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200749 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200750 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100751 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200753 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
755 return $default;
756 }
Barry Mienydd671972010-10-04 16:33:58 +0200757
Phil Sturgeon5c561802011-01-05 16:31:59 +0000758 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600759 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000760 if (is_array($this->_field_data[$field]['postdata']))
761 {
762 return array_shift($this->_field_data[$field]['postdata']);
763 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 return $this->_field_data[$field]['postdata'];
766 }
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200769
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 /**
771 * Set Select
772 *
773 * Enables pull-down lists to be set to the value the user
774 * selected in the event of an error
775 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 * @param string
777 * @param string
778 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200779 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100780 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200781 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200782 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 {
Andrey Andreev0adff1b2012-02-28 14:36:40 +0200784 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 }
Barry Mienydd671972010-10-04 16:33:58 +0200786
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 if (is_array($field))
790 {
791 if ( ! in_array($value, $field))
792 {
793 return '';
794 }
795 }
Andrey Andreev46ac8812012-02-28 14:32:54 +0200796 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200798 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 }
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 return ' selected="selected"';
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 /**
807 * Set Radio
808 *
809 * Enables radio buttons to be set to the value the user
810 * selected in the event of an error
811 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 * @param string
813 * @param string
814 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200815 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100816 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200818 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200820 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 }
Barry Mienydd671972010-10-04 16:33:58 +0200822
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200824
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 if (is_array($field))
826 {
827 if ( ! in_array($value, $field))
828 {
829 return '';
830 }
831 }
832 else
833 {
834 if (($field == '' OR $value == '') OR ($field != $value))
835 {
836 return '';
837 }
838 }
Barry Mienydd671972010-10-04 16:33:58 +0200839
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 return ' checked="checked"';
841 }
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 /**
846 * Set Checkbox
847 *
848 * Enables checkboxes to be set to the value the user
849 * selected in the event of an error
850 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 * @param string
852 * @param string
853 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200854 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100855 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200857 // Logic is exactly the same as for radio fields
858 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 /**
864 * Required
865 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 * @param string
867 * @return bool
868 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100869 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300871 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500877 * Performs a Regular Expression match test.
878 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500879 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300880 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500881 * @return bool
882 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100883 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500884 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200885 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500886 }
887
888 // --------------------------------------------------------------------
889
890 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 * Match one field to another
892 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300894 * @param string field
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 * @return bool
896 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100897 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300899 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000900 if ( ! isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 {
Barry Mienydd671972010-10-04 16:33:58 +0200902 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 }
Barry Mienydd671972010-10-04 16:33:58 +0200904
JonoB099c4782012-03-04 14:37:30 +0000905 return ($str === $validation_array[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 }
Eric Barnescccde962011-12-04 00:01:17 -0500907
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100908 // --------------------------------------------------------------------
909
910 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200911 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100912 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200913 * Check if the input value doesn't already exist
914 * in the specified database field.
915 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100916 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300917 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100918 * @return bool
919 */
920 public function is_unique($str, $field)
921 {
Eric Barnescccde962011-12-04 00:01:17 -0500922 list($table, $field) = explode('.', $field);
923 if (isset($this->CI->db))
924 {
925 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
926 return $query->num_rows() === 0;
927 }
928 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600929 }
Barry Mienydd671972010-10-04 16:33:58 +0200930
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 /**
934 * Minimum Length
935 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300937 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200939 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100940 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200942 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 {
944 return FALSE;
945 }
946
Andrey Andreev78f55772012-04-03 19:59:08 +0300947 return (MB_ENABLED === TRUE)
948 ? ($val <= mb_strlen($str))
949 : ($val <= strlen(str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 }
Barry Mienydd671972010-10-04 16:33:58 +0200951
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200953
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 /**
955 * Max Length
956 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300958 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200960 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100961 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200963 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
965 return FALSE;
966 }
967
Andrey Andreev78f55772012-04-03 19:59:08 +0300968 return (MB_ENABLED === TRUE)
969 ? ($val >= mb_strlen($str))
970 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 }
Barry Mienydd671972010-10-04 16:33:58 +0200972
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 /**
976 * Exact Length
977 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300979 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200981 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100982 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200984 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
986 return FALSE;
987 }
988
Andrey Andreev78f55772012-04-03 19:59:08 +0300989 return (MB_ENABLED === TRUE)
990 ? (mb_strlen($str) == $val)
991 : (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 }
Barry Mienydd671972010-10-04 16:33:58 +0200993
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 /**
997 * Valid Email
998 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 * @param string
1000 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001001 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001002 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001004 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 +00001005 }
1006
1007 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 /**
1010 * Valid Emails
1011 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 * @param string
1013 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001014 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001015 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 {
1017 if (strpos($str, ',') === FALSE)
1018 {
1019 return $this->valid_email(trim($str));
1020 }
Barry Mienydd671972010-10-04 16:33:58 +02001021
Pascal Kriete14287f32011-02-14 13:39:34 -05001022 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001024 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 {
1026 return FALSE;
1027 }
1028 }
Barry Mienydd671972010-10-04 16:33:58 +02001029
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 return TRUE;
1031 }
1032
1033 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001034
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 /**
1036 * Validate IP Address
1037 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001039 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001041 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 {
1043 return $this->CI->input->valid_ip($ip);
1044 }
1045
1046 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001047
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 /**
1049 * Alpha
1050 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 * @param string
1052 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001053 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001054 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001056 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 }
Barry Mienydd671972010-10-04 16:33:58 +02001058
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001060
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 /**
1062 * Alpha-numeric
1063 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 * @param string
1065 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001066 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001067 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001069 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 }
Barry Mienydd671972010-10-04 16:33:58 +02001071
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001073
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 /**
1075 * Alpha-numeric with underscores and dashes
1076 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 * @param string
1078 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001079 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001080 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001082 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 }
Barry Mienydd671972010-10-04 16:33:58 +02001084
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001086
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 /**
1088 * Numeric
1089 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 * @param string
1091 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001092 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001093 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001095 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001096
1097 }
1098
1099 // --------------------------------------------------------------------
1100
Barry Mienydd671972010-10-04 16:33:58 +02001101 /**
1102 * Is Numeric
1103 *
Barry Mienydd671972010-10-04 16:33:58 +02001104 * @param string
1105 * @return bool
1106 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001107 public function is_numeric($str)
Barry Mienydd671972010-10-04 16:33:58 +02001108 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001109 return is_numeric($str);
Barry Mienydd671972010-10-04 16:33:58 +02001110 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001111
1112 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001113
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 /**
1115 * Integer
1116 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 * @param string
1118 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001119 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001120 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001122 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1123 }
1124
1125 // --------------------------------------------------------------------
1126
1127 /**
1128 * Decimal number
1129 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001130 * @param string
1131 * @return bool
1132 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001133 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001134 {
1135 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1136 }
1137
1138 // --------------------------------------------------------------------
1139
1140 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001141 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001142 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001143 * @param string
1144 * @return bool
1145 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001146 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001147 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001148 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001149 }
1150
1151 // --------------------------------------------------------------------
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001152
Nick Busey98c347d2012-02-02 11:07:03 -07001153 /**
1154 * Equal to or Greater than
1155 *
Nick Busey98c347d2012-02-02 11:07:03 -07001156 * @param string
1157 * @return bool
1158 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001159 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001160 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001161 return is_numeric($str) ? ($str >= $min) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001162 }
1163
1164 // --------------------------------------------------------------------
Phil Sturgeonef112c02011-02-07 13:01:47 +00001165
1166 /**
1167 * Less than
1168 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001169 * @param string
1170 * @return bool
1171 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001172 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001173 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001174 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001176
1177 // --------------------------------------------------------------------
1178
Barry Mienydd671972010-10-04 16:33:58 +02001179 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001180 * Equal to or Less than
1181 *
Nick Busey98c347d2012-02-02 11:07:03 -07001182 * @param string
1183 * @return bool
1184 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001185 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001186 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001187 return is_numeric($str) ? ($str <= $max) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001188 }
1189
1190 // --------------------------------------------------------------------
1191
1192 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001193 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001194 *
Barry Mienydd671972010-10-04 16:33:58 +02001195 * @param string
1196 * @return bool
1197 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001198 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001199 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001200 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001201 }
1202
1203 // --------------------------------------------------------------------
1204
1205 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001206 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001207 *
Barry Mienydd671972010-10-04 16:33:58 +02001208 * @param string
1209 * @return bool
1210 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001211 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001212 {
Andrey Andreev46ac8812012-02-28 14:32:54 +02001213 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001214 }
1215
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001217
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 /**
1219 * Valid Base64
1220 *
1221 * Tests a string for characters outside of the Base64 alphabet
1222 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1223 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 * @param string
1225 * @return bool
1226 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001227 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 {
1229 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1230 }
Barry Mienydd671972010-10-04 16:33:58 +02001231
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001233
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 /**
1235 * Prep data for form
1236 *
1237 * This function allows HTML to be safely shown in a form.
1238 * Special characters are converted.
1239 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 * @param string
1241 * @return string
1242 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001243 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 {
1245 if (is_array($data))
1246 {
1247 foreach ($data as $key => $val)
1248 {
1249 $data[$key] = $this->prep_for_form($val);
1250 }
Barry Mienydd671972010-10-04 16:33:58 +02001251
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 return $data;
1253 }
Barry Mienydd671972010-10-04 16:33:58 +02001254
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 if ($this->_safe_form_data == FALSE OR $data === '')
1256 {
1257 return $data;
1258 }
1259
Andrey Andreev46ac8812012-02-28 14:32:54 +02001260 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 }
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 * Prep URL
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 prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 {
1273 if ($str == 'http://' OR $str == '')
1274 {
1275 return '';
1276 }
Barry Mienydd671972010-10-04 16:33:58 +02001277
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001278 if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://')
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001280 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 }
Barry Mienydd671972010-10-04 16:33:58 +02001282
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 return $str;
1284 }
Barry Mienydd671972010-10-04 16:33:58 +02001285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001287
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 /**
1289 * Strip Image Tags
1290 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 * @param string
1292 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001293 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001294 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 {
1296 return $this->CI->input->strip_image_tags($str);
1297 }
Barry Mienydd671972010-10-04 16:33:58 +02001298
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001300
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 /**
1302 * XSS Clean
1303 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 * @param string
1305 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001306 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001307 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 {
Derek Jones5640a712010-04-23 11:22:40 -05001309 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 }
Barry Mienydd671972010-10-04 16:33:58 +02001311
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001313
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 /**
1315 * Convert PHP tags to entities
1316 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 * @param string
1318 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001319 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001320 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001322 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 }
1324
JonoB099c4782012-03-04 14:37:30 +00001325 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001326
1327 /**
1328 * Reset validation vars
1329 *
1330 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001331 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001332 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001333 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001334 */
JonoB883f80f2012-03-05 09:51:27 +00001335 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001336 {
JonoB099c4782012-03-04 14:37:30 +00001337 $this->_field_data = array();
1338 $this->_config_rules = array();
1339 $this->_error_array = array();
1340 $this->_error_messages = array();
1341 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001342 }
1343
Derek Allard2067d1a2008-11-13 22:59:24 +00001344}
Derek Allard2067d1a2008-11-13 22:59:24 +00001345
1346/* End of file Form_validation.php */
Andrey Andreev78f55772012-04-03 19:59:08 +03001347/* Location: ./system/libraries/Form_validation.php */