blob: 5663da981090ff3942b3612bf18e8e49627ca804 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
89 if (count($_POST) == 0)
90 {
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...
Derek Jones37f4b9c2011-07-01 17:56:50 -0500116 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
Derek Jones37f4b9c2011-07-01 17:56:50 -0500124 // Is the field name an array? We test for the existence of a bracket "[" in
125 // the field name to determine this. If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200126 // into its components so that we can fetch the corresponding POST data later
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 if (strpos($field, '[') !== FALSE AND preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200128 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 // Note: Due to a bug in current() that affects some versions
130 // of PHP we can not pass function call directly into it
131 $x = explode('[', $field);
132 $indexes[] = current($x);
133
134 for ($i = 0; $i < count($matches['0']); $i++)
135 {
136 if ($matches['1'][$i] != '')
137 {
138 $indexes[] = $matches['1'][$i];
139 }
140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 $is_array = TRUE;
143 }
144 else
145 {
Barry Mienydd671972010-10-04 16:33:58 +0200146 $indexes = array();
147 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
150 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000152 'field' => $field,
153 'label' => $label,
154 'rules' => $rules,
155 'is_array' => $is_array,
156 'keys' => $indexes,
157 'postdata' => NULL,
158 'error' => ''
159 );
Greg Aker9f9af602010-11-10 15:41:51 -0600160
161 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
163
164 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 /**
167 * Set Error Message
168 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500169 * Lets users set their own error messages on the fly. Note: The key
170 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 *
172 * @access public
173 * @param string
174 * @param string
175 * @return string
176 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100177 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
179 if ( ! is_array($lang))
180 {
181 $lang = array($lang => $val);
182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 $this->_error_messages = array_merge($this->_error_messages, $lang);
Phil Sturgeonc3828712011-01-19 12:31:47 +0000185
Greg Aker9f9af602010-11-10 15:41:51 -0600186 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 /**
192 * Set The Error Delimiter
193 *
194 * Permits a prefix/suffix to be added to each error message
195 *
196 * @access public
197 * @param string
198 * @param string
199 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200200 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100201 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
203 $this->_error_prefix = $prefix;
204 $this->_error_suffix = $suffix;
Phil Sturgeonc3828712011-01-19 12:31:47 +0000205
Greg Aker9f9af602010-11-10 15:41:51 -0600206 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
208
209 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 /**
212 * Get Error Message
213 *
214 * Gets the error message associated with a particular field
215 *
216 * @access public
217 * @param string the field name
218 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200219 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100220 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200221 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
223 {
224 return '';
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 if ($prefix == '')
228 {
229 $prefix = $this->_error_prefix;
230 }
231
232 if ($suffix == '')
233 {
234 $suffix = $this->_error_suffix;
235 }
236
237 return $prefix.$this->_field_data[$field]['error'].$suffix;
238 }
239
240 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 /**
243 * Error String
244 *
245 * Returns the error messages as a string, wrapped in the error delimiters
246 *
247 * @access public
248 * @param string
249 * @param string
250 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200251 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100252 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
254 // No errrors, validation passes!
255 if (count($this->_error_array) === 0)
256 {
257 return '';
258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 if ($prefix == '')
261 {
262 $prefix = $this->_error_prefix;
263 }
264
265 if ($suffix == '')
266 {
267 $suffix = $this->_error_suffix;
268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // Generate the error string
271 $str = '';
272 foreach ($this->_error_array as $val)
273 {
274 if ($val != '')
275 {
276 $str .= $prefix.$val.$suffix."\n";
277 }
278 }
Barry Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 return $str;
281 }
282
283 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 /**
286 * Run the Validator
287 *
288 * This function does all the work.
289 *
290 * @access public
291 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200292 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100293 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500295 // Do we even have any data to process? Mm?
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 if (count($_POST) == 0)
297 {
298 return FALSE;
299 }
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // Does the _field_data array containing the validation rules exist?
302 // If not, we look to see if they were assigned via a config file
303 if (count($this->_field_data) == 0)
304 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500305 // No validation rules? We're done...
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 if (count($this->_config_rules) == 0)
307 {
308 return FALSE;
309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // Is there a validation rule for the particular URI being accessed?
312 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 if ($uri != '' AND isset($this->_config_rules[$uri]))
315 {
316 $this->set_rules($this->_config_rules[$uri]);
317 }
318 else
319 {
320 $this->set_rules($this->_config_rules);
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // We're we able to set the rules correctly?
324 if (count($this->_field_data) == 0)
325 {
326 log_message('debug', "Unable to find validation rules");
327 return FALSE;
328 }
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // Load the language file containing error messages
332 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200333
334 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // corresponding $_POST item and test for errors
336 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200337 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
339 // 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 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 if ($row['is_array'] == TRUE)
342 {
343 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
344 }
345 else
346 {
347 if (isset($_POST[$field]) AND $_POST[$field] != "")
348 {
349 $this->_field_data[$field]['postdata'] = $_POST[$field];
350 }
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
353 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
355
356 // Did we end up with any errors?
357 $total_errors = count($this->_error_array);
358
359 if ($total_errors > 0)
360 {
361 $this->_safe_form_data = TRUE;
362 }
363
364 // Now we need to re-set the POST data with the new, processed data
365 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // No errors, validation passes!
368 if ($total_errors == 0)
369 {
370 return TRUE;
371 }
372
373 // Validation fails
374 return FALSE;
375 }
376
377 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 /**
380 * Traverse a multidimensional $_POST array index until the data is found
381 *
382 * @access private
383 * @param array
384 * @param array
385 * @param integer
386 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200387 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100388 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 if (is_array($array))
391 {
392 if (isset($keys[$i]))
393 {
394 if (isset($array[$keys[$i]]))
395 {
396 $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1));
397 }
398 else
399 {
400 return NULL;
401 }
402 }
403 else
404 {
405 return $array;
406 }
407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 return $array;
410 }
411
412 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 /**
415 * Re-populate the _POST array with our finalized and processed data
416 *
417 * @access private
418 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200419 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100420 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 foreach ($this->_field_data as $field => $row)
423 {
424 if ( ! is_null($row['postdata']))
425 {
426 if ($row['is_array'] == FALSE)
427 {
428 if (isset($_POST[$row['field']]))
429 {
430 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
431 }
432 }
433 else
434 {
Derek Jones63eeae32009-02-10 19:08:56 +0000435 // start with a reference
436 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Jones63eeae32009-02-10 19:08:56 +0000438 // before we assign values, make a reference to the right POST key
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 if (count($row['keys']) == 1)
440 {
Derek Jones63eeae32009-02-10 19:08:56 +0000441 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
443 else
444 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 foreach ($row['keys'] as $val)
446 {
Derek Jones63eeae32009-02-10 19:08:56 +0000447 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449 }
Derek Jones63eeae32009-02-10 19:08:56 +0000450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000452 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 $array = array();
454 foreach ($row['postdata'] as $k => $v)
455 {
456 $array[$k] = $this->prep_for_form($v);
457 }
Derek Jones63eeae32009-02-10 19:08:56 +0000458
459 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
461 else
Derek Jones63eeae32009-02-10 19:08:56 +0000462 {
463 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
466 }
467 }
468 }
469
470 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 /**
473 * Executes the Validation routines
474 *
475 * @access private
476 * @param array
477 * @param array
478 * @param mixed
479 * @param integer
480 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200481 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100482 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
484 // If the $_POST data is an array we will run a recursive call
485 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200486 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 foreach ($postdata as $key => $val)
488 {
489 $this->_execute($row, $rules, $val, $cycles);
490 $cycles++;
491 }
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 return;
494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 // --------------------------------------------------------------------
497
498 // If the field is blank, but NOT required, no further tests are necessary
499 $callback = FALSE;
500 if ( ! in_array('required', $rules) AND is_null($postdata))
501 {
502 // Before we bail out, does the rule contain a callback?
Marcos Coelhoc28b2852011-07-05 12:59:41 -0700503 if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
505 $callback = TRUE;
506 $rules = (array('1' => $match[1]));
507 }
508 else
509 {
510 return;
511 }
512 }
513
514 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 // Isset Test. Typically this rule will only apply to checkboxes.
517 if (is_null($postdata) AND $callback == FALSE)
518 {
519 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
520 {
521 // Set the message type
522 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 if ( ! isset($this->_error_messages[$type]))
525 {
526 if (FALSE === ($line = $this->CI->lang->line($type)))
527 {
528 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200529 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 }
531 else
532 {
533 $line = $this->_error_messages[$type];
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 // Build the error message
537 $message = sprintf($line, $this->_translate_fieldname($row['label']));
538
539 // Save the error message
540 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 if ( ! isset($this->_error_array[$row['field']]))
543 {
544 $this->_error_array[$row['field']] = $message;
545 }
546 }
Barry Mienydd671972010-10-04 16:33:58 +0200547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 return;
549 }
550
551 // --------------------------------------------------------------------
552
553 // Cycle through each rule and run it
554 foreach ($rules As $rule)
555 {
556 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 // We set the $postdata variable with the current data in our master array so that
559 // each cycle of the loop is dealing with the processed data from the last cycle
560 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
561 {
562 // We shouldn't need this safety, but just in case there isn't an array index
563 // associated with this cycle we'll bail out
564 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
565 {
566 continue;
567 }
Barry Mienydd671972010-10-04 16:33:58 +0200568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
570 $_in_array = TRUE;
571 }
572 else
573 {
574 $postdata = $this->_field_data[$row['field']]['postdata'];
575 }
576
577 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200578
579 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 $callback = FALSE;
581 if (substr($rule, 0, 9) == 'callback_')
582 {
583 $rule = substr($rule, 9);
584 $callback = TRUE;
585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 // Strip the parameter (if exists) from the rule
588 // Rules can contain a parameter: max_length[5]
589 $param = FALSE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500590 if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
592 $rule = $match[1];
593 $param = $match[2];
594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 // Call the function that corresponds to the rule
597 if ($callback === TRUE)
598 {
599 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200600 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 continue;
602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 // Run the function and grab the result
605 $result = $this->CI->$rule($postdata, $param);
606
607 // Re-assign the result to the master data array
608 if ($_in_array == TRUE)
609 {
610 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
611 }
612 else
613 {
614 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
615 }
Barry Mienydd671972010-10-04 16:33:58 +0200616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 // If the field isn't required and we just processed a callback we'll move on...
618 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
619 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000620 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 }
622 }
623 else
Barry Mienydd671972010-10-04 16:33:58 +0200624 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 if ( ! method_exists($this, $rule))
626 {
Barry Mienydd671972010-10-04 16:33:58 +0200627 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 // Users can use any native PHP function call that has one param.
629 if (function_exists($rule))
630 {
631 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 if ($_in_array == TRUE)
634 {
635 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
636 }
637 else
638 {
639 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
640 }
641 }
patwork02404a12011-04-08 15:45:46 +0200642 else
643 {
644 log_message('debug', "Unable to find validation rule: ".$rule);
645 }
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 continue;
648 }
649
650 $result = $this->$rule($postdata, $param);
651
652 if ($_in_array == TRUE)
653 {
654 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
655 }
656 else
657 {
658 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
659 }
660 }
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Jones37f4b9c2011-07-01 17:56:50 -0500662 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200664 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 if ( ! isset($this->_error_messages[$rule]))
666 {
667 if (FALSE === ($line = $this->CI->lang->line($rule)))
668 {
669 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200670 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 }
672 else
673 {
674 $line = $this->_error_messages[$rule];
675 }
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 // Is the parameter we are inserting into the error message the name
Derek Jones37f4b9c2011-07-01 17:56:50 -0500678 // of another field? If so we need to grab its "field label"
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
680 {
Pascal Krietec1895832009-10-13 12:56:43 +0000681 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 }
Barry Mienydd671972010-10-04 16:33:58 +0200683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 // Build the error message
685 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
686
687 // Save the error message
688 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 if ( ! isset($this->_error_array[$row['field']]))
691 {
692 $this->_error_array[$row['field']] = $message;
693 }
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 return;
696 }
697 }
698 }
699
700 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 /**
703 * Translate a field name
704 *
705 * @access private
706 * @param string the field name
707 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200708 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100709 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
711 // Do we need to translate the field name?
712 // We look for the prefix lang: to determine this
713 if (substr($fieldname, 0, 5) == 'lang:')
714 {
715 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200716 $line = substr($fieldname, 5);
717
Derek Jones37f4b9c2011-07-01 17:56:50 -0500718 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
720 {
721 return $line;
722 }
723 }
724
725 return $fieldname;
726 }
727
728 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200729
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 /**
731 * Get the value from a form
732 *
733 * Permits you to repopulate a form field with the value it was submitted
734 * with, or, if that value doesn't exist, with the default
735 *
736 * @access public
737 * @param string the field name
738 * @param string
739 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200740 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100741 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 {
743 if ( ! isset($this->_field_data[$field]))
744 {
745 return $default;
746 }
Barry Mienydd671972010-10-04 16:33:58 +0200747
Phil Sturgeon5c561802011-01-05 16:31:59 +0000748 // If the data is an array output them one at a time.
Derek Jones37f4b9c2011-07-01 17:56:50 -0500749 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000750 if (is_array($this->_field_data[$field]['postdata']))
751 {
752 return array_shift($this->_field_data[$field]['postdata']);
753 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000754
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 return $this->_field_data[$field]['postdata'];
756 }
Barry Mienydd671972010-10-04 16:33:58 +0200757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 /**
761 * Set Select
762 *
763 * Enables pull-down lists to be set to the value the user
764 * selected in the event of an error
765 *
766 * @access public
767 * @param string
768 * @param string
769 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200770 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100771 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200772 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
774 {
775 if ($default === TRUE AND count($this->_field_data) === 0)
776 {
777 return ' selected="selected"';
778 }
779 return '';
780 }
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200783
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 if (is_array($field))
785 {
786 if ( ! in_array($value, $field))
787 {
788 return '';
789 }
790 }
791 else
792 {
793 if (($field == '' OR $value == '') OR ($field != $value))
794 {
795 return '';
796 }
797 }
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 return ' selected="selected"';
800 }
Barry Mienydd671972010-10-04 16:33:58 +0200801
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 /**
805 * Set Radio
806 *
807 * Enables radio buttons to be set to the value the user
808 * selected in the event of an error
809 *
810 * @access public
811 * @param string
812 * @param string
813 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200814 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100815 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
817 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
818 {
819 if ($default === TRUE AND count($this->_field_data) === 0)
820 {
821 return ' checked="checked"';
822 }
823 return '';
824 }
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 if (is_array($field))
829 {
830 if ( ! in_array($value, $field))
831 {
832 return '';
833 }
834 }
835 else
836 {
837 if (($field == '' OR $value == '') OR ($field != $value))
838 {
839 return '';
840 }
841 }
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 return ' checked="checked"';
844 }
Barry Mienydd671972010-10-04 16:33:58 +0200845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200847
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 /**
849 * Set Checkbox
850 *
851 * Enables checkboxes to be set to the value the user
852 * selected in the event of an error
853 *
854 * @access public
855 * @param string
856 * @param string
857 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200858 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100859 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 {
861 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
862 {
863 if ($default === TRUE AND count($this->_field_data) === 0)
864 {
865 return ' checked="checked"';
866 }
867 return '';
868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200871
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 if (is_array($field))
873 {
874 if ( ! in_array($value, $field))
875 {
876 return '';
877 }
878 }
879 else
880 {
881 if (($field == '' OR $value == '') OR ($field != $value))
882 {
883 return '';
884 }
885 }
Barry Mienydd671972010-10-04 16:33:58 +0200886
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 return ' checked="checked"';
888 }
Barry Mienydd671972010-10-04 16:33:58 +0200889
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200891
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 /**
893 * Required
894 *
895 * @access public
896 * @param string
897 * @return bool
898 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100899 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 {
901 if ( ! is_array($str))
902 {
903 return (trim($str) == '') ? FALSE : TRUE;
904 }
905 else
906 {
907 return ( ! empty($str));
908 }
909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500914 * Performs a Regular Expression match test.
915 *
916 * @access public
917 * @param string
918 * @param regex
919 * @return bool
920 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100921 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500922 {
923 if ( ! preg_match($regex, $str))
924 {
925 return FALSE;
926 }
927
Derek Jones37f4b9c2011-07-01 17:56:50 -0500928 return TRUE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500929 }
930
931 // --------------------------------------------------------------------
932
933 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 * Match one field to another
935 *
936 * @access public
937 * @param string
938 * @param field
939 * @return bool
940 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100941 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 {
943 if ( ! isset($_POST[$field]))
944 {
Barry Mienydd671972010-10-04 16:33:58 +0200945 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 $field = $_POST[$field];
949
950 return ($str !== $field) ? FALSE : TRUE;
951 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100952
953 // --------------------------------------------------------------------
954
955 /**
956 * Match one field to another
957 *
958 * @access public
959 * @param string
960 * @param field
961 * @return bool
962 */
963 public function is_unique($str, $field)
964 {
965 list($table, $field)=explode('.', $field);
966 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
967
968 return $query->num_rows() === 0;
969 }
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200972
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 /**
974 * Minimum Length
975 *
976 * @access public
977 * @param string
978 * @param value
979 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200980 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100981 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 {
983 if (preg_match("/[^0-9]/", $val))
984 {
985 return FALSE;
986 }
987
988 if (function_exists('mb_strlen'))
989 {
Barry Mienydd671972010-10-04 16:33:58 +0200990 return (mb_strlen($str) < $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 }
Barry Mienydd671972010-10-04 16:33:58 +0200992
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 return (strlen($str) < $val) ? FALSE : TRUE;
994 }
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 /**
999 * Max Length
1000 *
1001 * @access public
1002 * @param string
1003 * @param value
1004 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001005 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001006 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 {
1008 if (preg_match("/[^0-9]/", $val))
1009 {
1010 return FALSE;
1011 }
1012
1013 if (function_exists('mb_strlen'))
1014 {
Barry Mienydd671972010-10-04 16:33:58 +02001015 return (mb_strlen($str) > $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 return (strlen($str) > $val) ? FALSE : TRUE;
1019 }
Barry Mienydd671972010-10-04 16:33:58 +02001020
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001022
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 /**
1024 * Exact Length
1025 *
1026 * @access public
1027 * @param string
1028 * @param value
1029 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001030 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001031 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 {
1033 if (preg_match("/[^0-9]/", $val))
1034 {
1035 return FALSE;
1036 }
1037
1038 if (function_exists('mb_strlen'))
1039 {
Barry Mienydd671972010-10-04 16:33:58 +02001040 return (mb_strlen($str) != $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 }
Barry Mienydd671972010-10-04 16:33:58 +02001042
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 return (strlen($str) != $val) ? FALSE : TRUE;
1044 }
Barry Mienydd671972010-10-04 16:33:58 +02001045
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001047
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 /**
1049 * Valid Email
1050 *
1051 * @access public
1052 * @param string
1053 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001054 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001055 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 {
1057 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
1058 }
1059
1060 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001061
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 /**
1063 * Valid Emails
1064 *
1065 * @access public
1066 * @param string
1067 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001068 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001069 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 {
1071 if (strpos($str, ',') === FALSE)
1072 {
1073 return $this->valid_email(trim($str));
1074 }
Barry Mienydd671972010-10-04 16:33:58 +02001075
Pascal Kriete14287f32011-02-14 13:39:34 -05001076 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 {
1078 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
1079 {
1080 return FALSE;
1081 }
1082 }
Barry Mienydd671972010-10-04 16:33:58 +02001083
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 return TRUE;
1085 }
1086
1087 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 /**
1090 * Validate IP Address
1091 *
1092 * @access public
1093 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001094 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001096 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 {
1098 return $this->CI->input->valid_ip($ip);
1099 }
1100
1101 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001102
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 /**
1104 * Alpha
1105 *
1106 * @access public
1107 * @param string
1108 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001109 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001110 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 {
1112 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
1113 }
Barry Mienydd671972010-10-04 16:33:58 +02001114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001116
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 /**
1118 * Alpha-numeric
1119 *
1120 * @access public
1121 * @param string
1122 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001123 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001124 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 {
1126 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
1127 }
Barry Mienydd671972010-10-04 16:33:58 +02001128
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001130
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 /**
1132 * Alpha-numeric with underscores and dashes
1133 *
1134 * @access public
1135 * @param string
1136 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001137 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001138 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 {
1140 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
1141 }
Barry Mienydd671972010-10-04 16:33:58 +02001142
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 /**
1146 * Numeric
1147 *
1148 * @access public
1149 * @param string
1150 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001151 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001152 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 {
1154 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
1155
1156 }
1157
1158 // --------------------------------------------------------------------
1159
Barry Mienydd671972010-10-04 16:33:58 +02001160 /**
1161 * Is Numeric
1162 *
1163 * @access public
1164 * @param string
1165 * @return bool
1166 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001167 public function is_numeric($str)
Barry Mienydd671972010-10-04 16:33:58 +02001168 {
1169 return ( ! is_numeric($str)) ? FALSE : TRUE;
1170 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001171
1172 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001173
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 /**
1175 * Integer
1176 *
1177 * @access public
1178 * @param string
1179 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001180 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001181 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001183 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1184 }
1185
1186 // --------------------------------------------------------------------
1187
1188 /**
1189 * Decimal number
1190 *
1191 * @access public
1192 * @param string
1193 * @return bool
1194 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001195 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001196 {
1197 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1198 }
1199
1200 // --------------------------------------------------------------------
1201
1202 /**
1203 * Greather than
1204 *
1205 * @access public
1206 * @param string
1207 * @return bool
1208 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001209 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001210 {
1211 if ( ! is_numeric($str))
1212 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001213 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001214 }
1215 return $str > $min;
1216 }
1217
1218 // --------------------------------------------------------------------
1219
1220 /**
1221 * Less than
1222 *
1223 * @access public
1224 * @param string
1225 * @return bool
1226 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001227 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001228 {
1229 if ( ! is_numeric($str))
1230 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001231 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001232 }
1233 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001235
1236 // --------------------------------------------------------------------
1237
Barry Mienydd671972010-10-04 16:33:58 +02001238 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001239 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001240 *
1241 * @access public
1242 * @param string
1243 * @return bool
1244 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001245 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001246 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001247 return (bool) preg_match( '/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001248 }
1249
1250 // --------------------------------------------------------------------
1251
1252 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001253 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001254 *
1255 * @access public
1256 * @param string
1257 * @return bool
1258 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001259 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001260 {
1261 if ( ! preg_match( '/^[0-9]+$/', $str))
1262 {
1263 return FALSE;
1264 }
1265
1266 if ($str == 0)
1267 {
1268 return FALSE;
1269 }
1270
1271 return TRUE;
1272 }
1273
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001275
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 /**
1277 * Valid Base64
1278 *
1279 * Tests a string for characters outside of the Base64 alphabet
1280 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1281 *
1282 * @access public
1283 * @param string
1284 * @return bool
1285 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001286 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 {
1288 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1289 }
Barry Mienydd671972010-10-04 16:33:58 +02001290
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001292
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 /**
1294 * Prep data for form
1295 *
1296 * This function allows HTML to be safely shown in a form.
1297 * Special characters are converted.
1298 *
1299 * @access public
1300 * @param string
1301 * @return string
1302 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001303 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 {
1305 if (is_array($data))
1306 {
1307 foreach ($data as $key => $val)
1308 {
1309 $data[$key] = $this->prep_for_form($val);
1310 }
Barry Mienydd671972010-10-04 16:33:58 +02001311
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 return $data;
1313 }
Barry Mienydd671972010-10-04 16:33:58 +02001314
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 if ($this->_safe_form_data == FALSE OR $data === '')
1316 {
1317 return $data;
1318 }
1319
1320 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1321 }
Barry Mienydd671972010-10-04 16:33:58 +02001322
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001324
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 /**
1326 * Prep URL
1327 *
1328 * @access public
1329 * @param string
1330 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001331 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001332 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 {
1334 if ($str == 'http://' OR $str == '')
1335 {
1336 return '';
1337 }
Barry Mienydd671972010-10-04 16:33:58 +02001338
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
1340 {
1341 $str = 'http://'.$str;
1342 }
Barry Mienydd671972010-10-04 16:33:58 +02001343
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 return $str;
1345 }
Barry Mienydd671972010-10-04 16:33:58 +02001346
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001348
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 /**
1350 * Strip Image Tags
1351 *
1352 * @access public
1353 * @param string
1354 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001355 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001356 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 {
1358 return $this->CI->input->strip_image_tags($str);
1359 }
Barry Mienydd671972010-10-04 16:33:58 +02001360
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001362
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 /**
1364 * XSS Clean
1365 *
1366 * @access public
1367 * @param string
1368 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001369 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001370 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 {
Derek Jones5640a712010-04-23 11:22:40 -05001372 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 }
Barry Mienydd671972010-10-04 16:33:58 +02001374
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001376
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 /**
1378 * Convert PHP tags to entities
1379 *
1380 * @access public
1381 * @param string
1382 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001383 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001384 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001386 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 }
1388
1389}
1390// END Form Validation Class
1391
1392/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001393/* Location: ./system/libraries/Form_validation.php */