blob: 8173c6edfdbc0d87cdcb52433d138467d7998797 [file] [log] [blame]
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnescccde962011-12-04 00:01:17 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnescccde962011-12-04 00:01:17 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
28// ------------------------------------------------------------------------
29
30/**
31 * Form Validation Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
38 */
39class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020040
Phil Sturgeon3837ae72011-05-09 21:12:26 +010041 protected $CI;
42 protected $_field_data = array();
43 protected $_config_rules = array();
44 protected $_error_array = array();
45 protected $_error_messages = array();
46 protected $_error_prefix = '<p>';
47 protected $_error_suffix = '</p>';
48 protected $error_string = '';
49 protected $_safe_form_data = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000050
51 /**
52 * Constructor
Barry Mienydd671972010-10-04 16:33:58 +020053 */
Greg Akera9263282010-11-10 15:26:43 -060054 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +020055 {
Derek Allard2067d1a2008-11-13 22:59:24 +000056 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 // Validation rules can be stored in a config file.
59 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 // Automatically load the form helper
62 $this->CI->load->helper('form');
63
64 // Set the character encoding in MB.
65 if (function_exists('mb_internal_encoding'))
66 {
67 mb_internal_encoding($this->CI->config->item('charset'));
68 }
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 log_message('debug', "Form Validation Class Initialized");
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075 /**
76 * Set Rules
77 *
78 * This function takes an array of field names and validation
79 * rules as input, validates the info, and stores it
80 *
81 * @access public
82 * @param mixed
83 * @param string
84 * @return void
85 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010086 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
88 // No reason to set rules if we have no POST data
Andrey Andreev8dc532d2011-12-24 17:57:54 +020089 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
Greg Aker9f9af602010-11-10 15:41:51 -060091 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +000092 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 // If an array was passed via the first parameter instead of indidual string
95 // values we cycle through it and recursively call this function.
96 if (is_array($field))
97 {
98 foreach ($field as $row)
99 {
100 // Houston, we have a problem...
101 if ( ! isset($row['field']) OR ! isset($row['rules']))
102 {
103 continue;
104 }
105
106 // If the field label wasn't passed we use the field name
107 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
108
109 // Here we go!
110 $this->set_rules($row['field'], $label, $row['rules']);
111 }
Greg Aker9f9af602010-11-10 15:41:51 -0600112 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200116 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
Greg Aker9f9af602010-11-10 15:41:51 -0600118 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
120
121 // If the field label wasn't passed we use the field name
122 $label = ($label == '') ? $field : $label;
123
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200124 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200125 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200126 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200127 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 // Note: Due to a bug in current() that affects some versions
129 // of PHP we can not pass function call directly into it
130 $x = explode('[', $field);
131 $indexes[] = current($x);
132
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200133 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200135 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200137 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 $is_array = TRUE;
142 }
143 else
144 {
Barry Mienydd671972010-10-04 16:33:58 +0200145 $indexes = array();
146 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
149 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000151 'field' => $field,
152 'label' => $label,
153 'rules' => $rules,
154 'is_array' => $is_array,
155 'keys' => $indexes,
156 'postdata' => NULL,
157 'error' => ''
158 );
Greg Aker9f9af602010-11-10 15:41:51 -0600159
160 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
162
163 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 /**
166 * Set Error Message
167 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500168 * Lets users set their own error messages on the fly. Note: The key
169 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 *
171 * @access public
172 * @param string
173 * @param string
174 * @return string
175 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100176 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
178 if ( ! is_array($lang))
179 {
180 $lang = array($lang => $val);
181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 $this->_error_messages = array_merge($this->_error_messages, $lang);
Phil Sturgeonc3828712011-01-19 12:31:47 +0000184
Greg Aker9f9af602010-11-10 15:41:51 -0600185 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 /**
191 * Set The Error Delimiter
192 *
193 * Permits a prefix/suffix to be added to each error message
194 *
195 * @access public
196 * @param string
197 * @param string
198 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200199 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100200 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
202 $this->_error_prefix = $prefix;
203 $this->_error_suffix = $suffix;
Phil Sturgeonc3828712011-01-19 12:31:47 +0000204
Greg Aker9f9af602010-11-10 15:41:51 -0600205 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
207
208 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 /**
211 * Get Error Message
212 *
213 * Gets the error message associated with a particular field
214 *
215 * @access public
216 * @param string the field name
217 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200218 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100219 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200220 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
222 {
223 return '';
224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 if ($prefix == '')
227 {
228 $prefix = $this->_error_prefix;
229 }
230
231 if ($suffix == '')
232 {
233 $suffix = $this->_error_suffix;
234 }
235
236 return $prefix.$this->_field_data[$field]['error'].$suffix;
237 }
238
239 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 /**
242 * Error String
243 *
244 * Returns the error messages as a string, wrapped in the error delimiters
245 *
246 * @access public
247 * @param string
248 * @param string
249 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200250 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100251 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
253 // No errrors, validation passes!
254 if (count($this->_error_array) === 0)
255 {
256 return '';
257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 if ($prefix == '')
260 {
261 $prefix = $this->_error_prefix;
262 }
263
264 if ($suffix == '')
265 {
266 $suffix = $this->_error_suffix;
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // Generate the error string
270 $str = '';
271 foreach ($this->_error_array as $val)
272 {
273 if ($val != '')
274 {
275 $str .= $prefix.$val.$suffix."\n";
276 }
277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 return $str;
280 }
281
282 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 /**
285 * Run the Validator
286 *
287 * This function does all the work.
288 *
289 * @access public
290 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200291 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100292 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500294 // Do we even have any data to process? Mm?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200295 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
297 return FALSE;
298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // Does the _field_data array containing the validation rules exist?
301 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200302 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500304 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200305 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
307 return FALSE;
308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // Is there a validation rule for the particular URI being accessed?
311 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 if ($uri != '' AND isset($this->_config_rules[$uri]))
314 {
315 $this->set_rules($this->_config_rules[$uri]);
316 }
317 else
318 {
319 $this->set_rules($this->_config_rules);
320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // We're we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200323 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
325 log_message('debug', "Unable to find validation rules");
326 return FALSE;
327 }
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // Load the language file containing error messages
331 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200332
333 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // corresponding $_POST item and test for errors
335 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200336 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
338 // 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 +0200339
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200340 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
343 }
344 else
345 {
346 if (isset($_POST[$field]) AND $_POST[$field] != "")
347 {
348 $this->_field_data[$field]['postdata'] = $_POST[$field];
349 }
350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
352 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
354
355 // Did we end up with any errors?
356 $total_errors = count($this->_error_array);
357
358 if ($total_errors > 0)
359 {
360 $this->_safe_form_data = TRUE;
361 }
362
363 // Now we need to re-set the POST data with the new, processed data
364 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200365
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200366 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
368
369 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 /**
372 * Traverse a multidimensional $_POST array index until the data is found
373 *
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200374 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 * @param array
376 * @param array
377 * @param integer
378 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200379 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100380 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200382 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200384 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 return $array;
388 }
389
390 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 /**
393 * Re-populate the _POST array with our finalized and processed data
394 *
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200395 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200397 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100398 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 foreach ($this->_field_data as $field => $row)
401 {
402 if ( ! is_null($row['postdata']))
403 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200404 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
406 if (isset($_POST[$row['field']]))
407 {
408 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
409 }
410 }
411 else
412 {
Derek Jones63eeae32009-02-10 19:08:56 +0000413 // start with a reference
414 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Jones63eeae32009-02-10 19:08:56 +0000416 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200417 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
Derek Jones63eeae32009-02-10 19:08:56 +0000419 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
421 else
422 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 foreach ($row['keys'] as $val)
424 {
Derek Jones63eeae32009-02-10 19:08:56 +0000425 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
427 }
Derek Jones63eeae32009-02-10 19:08:56 +0000428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000430 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 $array = array();
432 foreach ($row['postdata'] as $k => $v)
433 {
434 $array[$k] = $this->prep_for_form($v);
435 }
Derek Jones63eeae32009-02-10 19:08:56 +0000436
437 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439 else
Derek Jones63eeae32009-02-10 19:08:56 +0000440 {
441 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
444 }
445 }
446 }
447
448 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 /**
451 * Executes the Validation routines
452 *
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200453 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 * @param array
455 * @param array
456 * @param mixed
457 * @param integer
458 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200459 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100460 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 // If the $_POST data is an array we will run a recursive call
463 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200464 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 foreach ($postdata as $key => $val)
466 {
467 $this->_execute($row, $rules, $val, $cycles);
468 $cycles++;
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 return;
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // --------------------------------------------------------------------
475
476 // If the field is blank, but NOT required, no further tests are necessary
477 $callback = FALSE;
478 if ( ! in_array('required', $rules) AND is_null($postdata))
479 {
480 // Before we bail out, does the rule contain a callback?
Marcos Coelhoc28b2852011-07-05 12:59:41 -0700481 if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
483 $callback = TRUE;
484 $rules = (array('1' => $match[1]));
485 }
486 else
487 {
488 return;
489 }
490 }
491
492 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200495 if (is_null($postdata) AND $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
497 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
498 {
499 // Set the message type
500 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 if ( ! isset($this->_error_messages[$type]))
503 {
504 if (FALSE === ($line = $this->CI->lang->line($type)))
505 {
506 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200507 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
509 else
510 {
511 $line = $this->_error_messages[$type];
512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 // Build the error message
515 $message = sprintf($line, $this->_translate_fieldname($row['label']));
516
517 // Save the error message
518 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 if ( ! isset($this->_error_array[$row['field']]))
521 {
522 $this->_error_array[$row['field']] = $message;
523 }
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 return;
527 }
528
529 // --------------------------------------------------------------------
530
531 // Cycle through each rule and run it
532 foreach ($rules As $rule)
533 {
534 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 // We set the $postdata variable with the current data in our master array so that
537 // each cycle of the loop is dealing with the processed data from the last cycle
538 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
539 {
540 // We shouldn't need this safety, but just in case there isn't an array index
541 // associated with this cycle we'll bail out
542 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
543 {
544 continue;
545 }
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
548 $_in_array = TRUE;
549 }
550 else
551 {
552 $postdata = $this->_field_data[$row['field']]['postdata'];
553 }
554
555 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200556
557 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 $callback = FALSE;
559 if (substr($rule, 0, 9) == 'callback_')
560 {
561 $rule = substr($rule, 9);
562 $callback = TRUE;
563 }
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 // Strip the parameter (if exists) from the rule
566 // Rules can contain a parameter: max_length[5]
567 $param = FALSE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500568 if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
570 $rule = $match[1];
571 $param = $match[2];
572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 // Call the function that corresponds to the rule
575 if ($callback === TRUE)
576 {
577 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200578 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 continue;
580 }
Barry Mienydd671972010-10-04 16:33:58 +0200581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 // Run the function and grab the result
583 $result = $this->CI->$rule($postdata, $param);
584
585 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200586 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
589 }
590 else
591 {
592 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 // If the field isn't required and we just processed a callback we'll move on...
596 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
597 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000598 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 }
600 }
601 else
Barry Mienydd671972010-10-04 16:33:58 +0200602 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 if ( ! method_exists($this, $rule))
604 {
Barry Mienydd671972010-10-04 16:33:58 +0200605 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 // Users can use any native PHP function call that has one param.
607 if (function_exists($rule))
608 {
609 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200610
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200611 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
613 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
614 }
615 else
616 {
617 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
618 }
619 }
patwork02404a12011-04-08 15:45:46 +0200620 else
621 {
622 log_message('debug', "Unable to find validation rule: ".$rule);
623 }
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 continue;
626 }
627
628 $result = $this->$rule($postdata, $param);
629
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200630 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
632 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
633 }
634 else
635 {
636 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
637 }
638 }
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Jones37f4b9c2011-07-01 17:56:50 -0500640 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200642 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 if ( ! isset($this->_error_messages[$rule]))
644 {
645 if (FALSE === ($line = $this->CI->lang->line($rule)))
646 {
647 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200648 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 }
650 else
651 {
652 $line = $this->_error_messages[$rule];
653 }
Barry Mienydd671972010-10-04 16:33:58 +0200654
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 // Is the parameter we are inserting into the error message the name
Derek Jones37f4b9c2011-07-01 17:56:50 -0500656 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200657 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
Pascal Krietec1895832009-10-13 12:56:43 +0000659 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 // Build the error message
663 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
664
665 // Save the error message
666 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 if ( ! isset($this->_error_array[$row['field']]))
669 {
670 $this->_error_array[$row['field']] = $message;
671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 return;
674 }
675 }
676 }
677
678 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 /**
681 * Translate a field name
682 *
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200683 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 * @param string the field name
685 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200686 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100687 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
689 // Do we need to translate the field name?
690 // We look for the prefix lang: to determine this
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200691 if (substr($fieldname, 0, 5) === 'lang:')
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
693 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200694 $line = substr($fieldname, 5);
695
Derek Jones37f4b9c2011-07-01 17:56:50 -0500696 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
698 {
699 return $line;
700 }
701 }
702
703 return $fieldname;
704 }
705
706 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 /**
709 * Get the value from a form
710 *
711 * Permits you to repopulate a form field with the value it was submitted
712 * with, or, if that value doesn't exist, with the default
713 *
714 * @access public
715 * @param string the field name
716 * @param string
717 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200718 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100719 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
721 if ( ! isset($this->_field_data[$field]))
722 {
723 return $default;
724 }
Barry Mienydd671972010-10-04 16:33:58 +0200725
Phil Sturgeon5c561802011-01-05 16:31:59 +0000726 // If the data is an array output them one at a time.
Derek Jones37f4b9c2011-07-01 17:56:50 -0500727 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000728 if (is_array($this->_field_data[$field]['postdata']))
729 {
730 return array_shift($this->_field_data[$field]['postdata']);
731 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000732
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 return $this->_field_data[$field]['postdata'];
734 }
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200737
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 /**
739 * Set Select
740 *
741 * Enables pull-down lists to be set to the value the user
742 * selected in the event of an error
743 *
744 * @access public
745 * @param string
746 * @param string
747 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200748 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100749 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200750 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
752 {
753 if ($default === TRUE AND count($this->_field_data) === 0)
754 {
755 return ' selected="selected"';
756 }
757 return '';
758 }
Barry Mienydd671972010-10-04 16:33:58 +0200759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 if (is_array($field))
763 {
764 if ( ! in_array($value, $field))
765 {
766 return '';
767 }
768 }
769 else
770 {
771 if (($field == '' OR $value == '') OR ($field != $value))
772 {
773 return '';
774 }
775 }
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 return ' selected="selected"';
778 }
Barry Mienydd671972010-10-04 16:33:58 +0200779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 /**
783 * Set Radio
784 *
785 * Enables radio buttons to be set to the value the user
786 * selected in the event of an error
787 *
788 * @access public
789 * @param string
790 * @param string
791 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200792 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100793 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
795 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
796 {
797 if ($default === TRUE AND count($this->_field_data) === 0)
798 {
799 return ' checked="checked"';
800 }
801 return '';
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 if (is_array($field))
807 {
808 if ( ! in_array($value, $field))
809 {
810 return '';
811 }
812 }
813 else
814 {
815 if (($field == '' OR $value == '') OR ($field != $value))
816 {
817 return '';
818 }
819 }
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 return ' checked="checked"';
822 }
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 /**
827 * Set Checkbox
828 *
829 * Enables checkboxes to be set to the value the user
830 * selected in the event of an error
831 *
832 * @access public
833 * @param string
834 * @param string
835 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200836 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100837 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200839 // Logic is exactly the same as for radio fields
840 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 }
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 * Required
847 *
848 * @access public
849 * @param string
850 * @return bool
851 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100852 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200854 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 }
Barry Mienydd671972010-10-04 16:33:58 +0200856
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500860 * Performs a Regular Expression match test.
861 *
862 * @access public
863 * @param string
864 * @param regex
865 * @return bool
866 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100867 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500868 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200869 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500870 }
871
872 // --------------------------------------------------------------------
873
874 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 * Match one field to another
876 *
877 * @access public
878 * @param string
879 * @param field
880 * @return bool
881 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100882 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 {
884 if ( ! isset($_POST[$field]))
885 {
Barry Mienydd671972010-10-04 16:33:58 +0200886 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 $field = $_POST[$field];
890
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200891 return ($str === $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 }
Eric Barnescccde962011-12-04 00:01:17 -0500893
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100894 // --------------------------------------------------------------------
895
896 /**
897 * Match one field to another
898 *
899 * @access public
900 * @param string
901 * @param field
902 * @return bool
903 */
904 public function is_unique($str, $field)
905 {
Eric Barnescccde962011-12-04 00:01:17 -0500906 list($table, $field) = explode('.', $field);
907 if (isset($this->CI->db))
908 {
909 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
910 return $query->num_rows() === 0;
911 }
912 return FALSE;
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200913 }
Barry Mienydd671972010-10-04 16:33:58 +0200914
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200916
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 /**
918 * Minimum Length
919 *
920 * @access public
921 * @param string
922 * @param value
923 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200924 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100925 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 {
927 if (preg_match("/[^0-9]/", $val))
928 {
929 return FALSE;
930 }
931
932 if (function_exists('mb_strlen'))
933 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200934 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200937 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 }
Barry Mienydd671972010-10-04 16:33:58 +0200939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200941
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 /**
943 * Max Length
944 *
945 * @access public
946 * @param string
947 * @param value
948 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200949 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100950 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 {
952 if (preg_match("/[^0-9]/", $val))
953 {
954 return FALSE;
955 }
956
957 if (function_exists('mb_strlen'))
958 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200959 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200962 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 }
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 /**
968 * Exact Length
969 *
970 * @access public
971 * @param string
972 * @param value
973 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200974 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100975 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
977 if (preg_match("/[^0-9]/", $val))
978 {
979 return FALSE;
980 }
981
982 if (function_exists('mb_strlen'))
983 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200984 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 }
Barry Mienydd671972010-10-04 16:33:58 +0200986
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200987 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 /**
993 * Valid Email
994 *
995 * @access public
996 * @param string
997 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200998 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100999 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001001 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 +00001002 }
1003
1004 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 /**
1007 * Valid Emails
1008 *
1009 * @access public
1010 * @param string
1011 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001012 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001013 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
1015 if (strpos($str, ',') === FALSE)
1016 {
1017 return $this->valid_email(trim($str));
1018 }
Barry Mienydd671972010-10-04 16:33:58 +02001019
Pascal Kriete14287f32011-02-14 13:39:34 -05001020 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001022 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
1024 return FALSE;
1025 }
1026 }
Barry Mienydd671972010-10-04 16:33:58 +02001027
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 return TRUE;
1029 }
1030
1031 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001032
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 /**
1034 * Validate IP Address
1035 *
1036 * @access public
1037 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001038 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001040 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 {
1042 return $this->CI->input->valid_ip($ip);
1043 }
1044
1045 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001046
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 /**
1048 * Alpha
1049 *
1050 * @access public
1051 * @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 *
1064 * @access public
1065 * @param string
1066 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001067 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001068 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001070 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 }
Barry Mienydd671972010-10-04 16:33:58 +02001072
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001074
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 /**
1076 * Alpha-numeric with underscores and dashes
1077 *
1078 * @access public
1079 * @param string
1080 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001081 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001082 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001084 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 }
Barry Mienydd671972010-10-04 16:33:58 +02001086
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 /**
1090 * Numeric
1091 *
1092 * @access public
1093 * @param string
1094 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001095 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001096 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001098 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001099
1100 }
1101
1102 // --------------------------------------------------------------------
1103
Barry Mienydd671972010-10-04 16:33:58 +02001104 /**
1105 * Is Numeric
1106 *
1107 * @access public
1108 * @param string
1109 * @return bool
1110 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001111 public function is_numeric($str)
Barry Mienydd671972010-10-04 16:33:58 +02001112 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001113 return is_numeric($str);
Barry Mienydd671972010-10-04 16:33:58 +02001114 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001115
1116 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 /**
1119 * Integer
1120 *
1121 * @access public
1122 * @param string
1123 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001124 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001125 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001127 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1128 }
1129
1130 // --------------------------------------------------------------------
1131
1132 /**
1133 * Decimal number
1134 *
1135 * @access public
1136 * @param string
1137 * @return bool
1138 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001139 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001140 {
1141 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1142 }
1143
1144 // --------------------------------------------------------------------
1145
1146 /**
1147 * Greather than
1148 *
1149 * @access public
1150 * @param string
1151 * @return bool
1152 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001153 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001154 {
1155 if ( ! is_numeric($str))
1156 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001157 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001158 }
1159 return $str > $min;
1160 }
1161
1162 // --------------------------------------------------------------------
1163
1164 /**
1165 * Less than
1166 *
1167 * @access public
1168 * @param string
1169 * @return bool
1170 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001171 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001172 {
1173 if ( ! is_numeric($str))
1174 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001175 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001176 }
1177 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001179
1180 // --------------------------------------------------------------------
1181
Barry Mienydd671972010-10-04 16:33:58 +02001182 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001183 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001184 *
1185 * @access public
1186 * @param string
1187 * @return bool
1188 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001189 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001190 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001191 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001192 }
1193
1194 // --------------------------------------------------------------------
1195
1196 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001197 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001198 *
1199 * @access public
1200 * @param string
1201 * @return bool
1202 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001203 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001204 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001205 return ($str != 0 AND preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001206 }
1207
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001209
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 /**
1211 * Valid Base64
1212 *
1213 * Tests a string for characters outside of the Base64 alphabet
1214 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1215 *
1216 * @access public
1217 * @param string
1218 * @return bool
1219 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001220 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 {
1222 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1223 }
Barry Mienydd671972010-10-04 16:33:58 +02001224
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001226
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 /**
1228 * Prep data for form
1229 *
1230 * This function allows HTML to be safely shown in a form.
1231 * Special characters are converted.
1232 *
1233 * @access public
1234 * @param string
1235 * @return string
1236 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001237 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 {
1239 if (is_array($data))
1240 {
1241 foreach ($data as $key => $val)
1242 {
1243 $data[$key] = $this->prep_for_form($val);
1244 }
Barry Mienydd671972010-10-04 16:33:58 +02001245
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 return $data;
1247 }
Barry Mienydd671972010-10-04 16:33:58 +02001248
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 if ($this->_safe_form_data == FALSE OR $data === '')
1250 {
1251 return $data;
1252 }
1253
1254 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1255 }
Barry Mienydd671972010-10-04 16:33:58 +02001256
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001258
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 /**
1260 * Prep URL
1261 *
1262 * @access public
1263 * @param string
1264 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001265 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001266 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 {
1268 if ($str == 'http://' OR $str == '')
1269 {
1270 return '';
1271 }
Barry Mienydd671972010-10-04 16:33:58 +02001272
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001273 if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://')
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 {
1275 $str = 'http://'.$str;
1276 }
Barry Mienydd671972010-10-04 16:33:58 +02001277
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 return $str;
1279 }
Barry Mienydd671972010-10-04 16:33:58 +02001280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001282
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 /**
1284 * Strip Image Tags
1285 *
1286 * @access public
1287 * @param string
1288 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001289 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001290 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 {
1292 return $this->CI->input->strip_image_tags($str);
1293 }
Barry Mienydd671972010-10-04 16:33:58 +02001294
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001296
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 /**
1298 * XSS Clean
1299 *
1300 * @access public
1301 * @param string
1302 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001303 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001304 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 {
Derek Jones5640a712010-04-23 11:22:40 -05001306 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 }
Barry Mienydd671972010-10-04 16:33:58 +02001308
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001310
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 /**
1312 * Convert PHP tags to entities
1313 *
1314 * @access public
1315 * @param string
1316 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001317 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001318 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001320 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 }
1322
1323}
1324// END Form Validation Class
1325
1326/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001327/* Location: ./system/libraries/Form_validation.php */