blob: c71c8b59a59ae834521d5de99ab454ba708d2102 [file] [log] [blame]
Razican114ab092011-04-25 17:26:45 +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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Form Validation Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Validation
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
26 */
27class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020028
Phil Sturgeon3837ae72011-05-09 21:12:26 +010029 protected $CI;
30 protected $_field_data = array();
31 protected $_config_rules = array();
32 protected $_error_array = array();
33 protected $_error_messages = array();
34 protected $_error_prefix = '<p>';
35 protected $_error_suffix = '</p>';
36 protected $error_string = '';
37 protected $_safe_form_data = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000038
39 /**
40 * Constructor
Barry Mienydd671972010-10-04 16:33:58 +020041 */
Greg Akera9263282010-11-10 15:26:43 -060042 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +020043 {
Derek Allard2067d1a2008-11-13 22:59:24 +000044 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020045
Derek Allard2067d1a2008-11-13 22:59:24 +000046 // Validation rules can be stored in a config file.
47 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020048
Derek Allard2067d1a2008-11-13 22:59:24 +000049 // Automatically load the form helper
50 $this->CI->load->helper('form');
51
52 // Set the character encoding in MB.
53 if (function_exists('mb_internal_encoding'))
54 {
55 mb_internal_encoding($this->CI->config->item('charset'));
56 }
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 log_message('debug', "Form Validation Class Initialized");
59 }
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020062
Derek Allard2067d1a2008-11-13 22:59:24 +000063 /**
64 * Set Rules
65 *
66 * This function takes an array of field names and validation
67 * rules as input, validates the info, and stores it
68 *
69 * @access public
70 * @param mixed
71 * @param string
72 * @return void
73 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010074 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000075 {
76 // No reason to set rules if we have no POST data
77 if (count($_POST) == 0)
78 {
Greg Aker9f9af602010-11-10 15:41:51 -060079 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +000080 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 // If an array was passed via the first parameter instead of indidual string
83 // values we cycle through it and recursively call this function.
84 if (is_array($field))
85 {
86 foreach ($field as $row)
87 {
88 // Houston, we have a problem...
89 if ( ! isset($row['field']) OR ! isset($row['rules']))
90 {
91 continue;
92 }
93
94 // If the field label wasn't passed we use the field name
95 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
96
97 // Here we go!
98 $this->set_rules($row['field'], $label, $row['rules']);
99 }
Greg Aker9f9af602010-11-10 15:41:51 -0600100 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 // No fields? Nothing to do...
Razican114ab092011-04-25 17:26:45 +0200104 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Greg Aker9f9af602010-11-10 15:41:51 -0600106 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
108
109 // If the field label wasn't passed we use the field name
110 $label = ($label == '') ? $field : $label;
111
Razican114ab092011-04-25 17:26:45 +0200112 // Is the field name an array? We test for the existence of a bracket "[" in
113 // the field name to determine this. If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200114 // into its components so that we can fetch the corresponding POST data later
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 if (strpos($field, '[') !== FALSE AND preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200116 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 // Note: Due to a bug in current() that affects some versions
118 // of PHP we can not pass function call directly into it
119 $x = explode('[', $field);
120 $indexes[] = current($x);
121
122 for ($i = 0; $i < count($matches['0']); $i++)
123 {
124 if ($matches['1'][$i] != '')
125 {
126 $indexes[] = $matches['1'][$i];
127 }
128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 $is_array = TRUE;
131 }
132 else
133 {
Barry Mienydd671972010-10-04 16:33:58 +0200134 $indexes = array();
135 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
138 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000140 'field' => $field,
141 'label' => $label,
142 'rules' => $rules,
143 'is_array' => $is_array,
144 'keys' => $indexes,
145 'postdata' => NULL,
146 'error' => ''
147 );
Greg Aker9f9af602010-11-10 15:41:51 -0600148
149 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
151
152 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 /**
155 * Set Error Message
156 *
Razican114ab092011-04-25 17:26:45 +0200157 * Lets users set their own error messages on the fly. Note: The key
158 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 *
160 * @access public
161 * @param string
162 * @param string
163 * @return string
164 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100165 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
167 if ( ! is_array($lang))
168 {
169 $lang = array($lang => $val);
170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 $this->_error_messages = array_merge($this->_error_messages, $lang);
Phil Sturgeonc3828712011-01-19 12:31:47 +0000173
Greg Aker9f9af602010-11-10 15:41:51 -0600174 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 /**
180 * Set The Error Delimiter
181 *
182 * Permits a prefix/suffix to be added to each error message
183 *
184 * @access public
185 * @param string
186 * @param string
187 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200188 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100189 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
191 $this->_error_prefix = $prefix;
192 $this->_error_suffix = $suffix;
Phil Sturgeonc3828712011-01-19 12:31:47 +0000193
Greg Aker9f9af602010-11-10 15:41:51 -0600194 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
196
197 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 /**
200 * Get Error Message
201 *
202 * Gets the error message associated with a particular field
203 *
204 * @access public
205 * @param string the field name
206 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200207 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100208 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200209 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
211 {
212 return '';
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 if ($prefix == '')
216 {
217 $prefix = $this->_error_prefix;
218 }
219
220 if ($suffix == '')
221 {
222 $suffix = $this->_error_suffix;
223 }
224
225 return $prefix.$this->_field_data[$field]['error'].$suffix;
226 }
227
228 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 /**
231 * Error String
232 *
233 * Returns the error messages as a string, wrapped in the error delimiters
234 *
235 * @access public
236 * @param string
237 * @param string
238 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200239 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100240 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
242 // No errrors, validation passes!
243 if (count($this->_error_array) === 0)
244 {
245 return '';
246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 if ($prefix == '')
249 {
250 $prefix = $this->_error_prefix;
251 }
252
253 if ($suffix == '')
254 {
255 $suffix = $this->_error_suffix;
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // Generate the error string
259 $str = '';
260 foreach ($this->_error_array as $val)
261 {
262 if ($val != '')
263 {
264 $str .= $prefix.$val.$suffix."\n";
265 }
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 return $str;
269 }
270
271 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 /**
274 * Run the Validator
275 *
276 * This function does all the work.
277 *
278 * @access public
279 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200280 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100281 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Razican114ab092011-04-25 17:26:45 +0200283 // Do we even have any data to process? Mm?
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 if (count($_POST) == 0)
285 {
286 return FALSE;
287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // Does the _field_data array containing the validation rules exist?
290 // If not, we look to see if they were assigned via a config file
291 if (count($this->_field_data) == 0)
292 {
Razican114ab092011-04-25 17:26:45 +0200293 // No validation rules? We're done...
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 if (count($this->_config_rules) == 0)
295 {
296 return FALSE;
297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 // Is there a validation rule for the particular URI being accessed?
300 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 if ($uri != '' AND isset($this->_config_rules[$uri]))
303 {
304 $this->set_rules($this->_config_rules[$uri]);
305 }
306 else
307 {
308 $this->set_rules($this->_config_rules);
309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // We're we able to set the rules correctly?
312 if (count($this->_field_data) == 0)
313 {
314 log_message('debug', "Unable to find validation rules");
315 return FALSE;
316 }
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // Load the language file containing error messages
320 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200321
322 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // corresponding $_POST item and test for errors
324 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200325 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
327 // 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 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 if ($row['is_array'] == TRUE)
330 {
331 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
332 }
333 else
334 {
335 if (isset($_POST[$field]) AND $_POST[$field] != "")
336 {
337 $this->_field_data[$field]['postdata'] = $_POST[$field];
338 }
339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
341 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 }
343
344 // Did we end up with any errors?
345 $total_errors = count($this->_error_array);
346
347 if ($total_errors > 0)
348 {
349 $this->_safe_form_data = TRUE;
350 }
351
352 // Now we need to re-set the POST data with the new, processed data
353 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // No errors, validation passes!
356 if ($total_errors == 0)
357 {
358 return TRUE;
359 }
360
361 // Validation fails
362 return FALSE;
363 }
364
365 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 /**
368 * Traverse a multidimensional $_POST array index until the data is found
369 *
370 * @access private
371 * @param array
372 * @param array
373 * @param integer
374 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200375 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100376 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 if (is_array($array))
379 {
380 if (isset($keys[$i]))
381 {
382 if (isset($array[$keys[$i]]))
383 {
384 $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1));
385 }
386 else
387 {
388 return NULL;
389 }
390 }
391 else
392 {
393 return $array;
394 }
395 }
Barry Mienydd671972010-10-04 16:33:58 +0200396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 return $array;
398 }
399
400 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 /**
403 * Re-populate the _POST array with our finalized and processed data
404 *
405 * @access private
406 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200407 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100408 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
410 foreach ($this->_field_data as $field => $row)
411 {
412 if ( ! is_null($row['postdata']))
413 {
414 if ($row['is_array'] == FALSE)
415 {
416 if (isset($_POST[$row['field']]))
417 {
418 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
419 }
420 }
421 else
422 {
Derek Jones63eeae32009-02-10 19:08:56 +0000423 // start with a reference
424 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Jones63eeae32009-02-10 19:08:56 +0000426 // before we assign values, make a reference to the right POST key
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 if (count($row['keys']) == 1)
428 {
Derek Jones63eeae32009-02-10 19:08:56 +0000429 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
431 else
432 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 foreach ($row['keys'] as $val)
434 {
Derek Jones63eeae32009-02-10 19:08:56 +0000435 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
437 }
Derek Jones63eeae32009-02-10 19:08:56 +0000438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000440 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 $array = array();
442 foreach ($row['postdata'] as $k => $v)
443 {
444 $array[$k] = $this->prep_for_form($v);
445 }
Derek Jones63eeae32009-02-10 19:08:56 +0000446
447 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449 else
Derek Jones63eeae32009-02-10 19:08:56 +0000450 {
451 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
454 }
455 }
456 }
457
458 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 /**
461 * Executes the Validation routines
462 *
463 * @access private
464 * @param array
465 * @param array
466 * @param mixed
467 * @param integer
468 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200469 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100470 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
472 // If the $_POST data is an array we will run a recursive call
473 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200474 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 foreach ($postdata as $key => $val)
476 {
477 $this->_execute($row, $rules, $val, $cycles);
478 $cycles++;
479 }
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 return;
482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 // --------------------------------------------------------------------
485
486 // If the field is blank, but NOT required, no further tests are necessary
487 $callback = FALSE;
488 if ( ! in_array('required', $rules) AND is_null($postdata))
489 {
490 // Before we bail out, does the rule contain a callback?
491 if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
492 {
493 $callback = TRUE;
494 $rules = (array('1' => $match[1]));
495 }
496 else
497 {
498 return;
499 }
500 }
501
502 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 // Isset Test. Typically this rule will only apply to checkboxes.
505 if (is_null($postdata) AND $callback == FALSE)
506 {
507 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
508 {
509 // Set the message type
510 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 if ( ! isset($this->_error_messages[$type]))
513 {
514 if (FALSE === ($line = $this->CI->lang->line($type)))
515 {
516 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200517 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 }
519 else
520 {
521 $line = $this->_error_messages[$type];
522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 // Build the error message
525 $message = sprintf($line, $this->_translate_fieldname($row['label']));
526
527 // Save the error message
528 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 if ( ! isset($this->_error_array[$row['field']]))
531 {
532 $this->_error_array[$row['field']] = $message;
533 }
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 return;
537 }
538
539 // --------------------------------------------------------------------
540
541 // Cycle through each rule and run it
542 foreach ($rules As $rule)
543 {
544 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 // We set the $postdata variable with the current data in our master array so that
547 // each cycle of the loop is dealing with the processed data from the last cycle
548 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
549 {
550 // We shouldn't need this safety, but just in case there isn't an array index
551 // associated with this cycle we'll bail out
552 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
553 {
554 continue;
555 }
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
558 $_in_array = TRUE;
559 }
560 else
561 {
562 $postdata = $this->_field_data[$row['field']]['postdata'];
563 }
564
565 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200566
567 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 $callback = FALSE;
569 if (substr($rule, 0, 9) == 'callback_')
570 {
571 $rule = substr($rule, 9);
572 $callback = TRUE;
573 }
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 // Strip the parameter (if exists) from the rule
576 // Rules can contain a parameter: max_length[5]
577 $param = FALSE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500578 if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
580 $rule = $match[1];
581 $param = $match[2];
582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 // Call the function that corresponds to the rule
585 if ($callback === TRUE)
586 {
587 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200588 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 continue;
590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 // Run the function and grab the result
593 $result = $this->CI->$rule($postdata, $param);
594
595 // Re-assign the result to the master data array
596 if ($_in_array == TRUE)
597 {
598 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
599 }
600 else
601 {
602 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
603 }
Barry Mienydd671972010-10-04 16:33:58 +0200604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 // If the field isn't required and we just processed a callback we'll move on...
606 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
607 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000608 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 }
610 }
611 else
Barry Mienydd671972010-10-04 16:33:58 +0200612 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 if ( ! method_exists($this, $rule))
614 {
Barry Mienydd671972010-10-04 16:33:58 +0200615 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 // Users can use any native PHP function call that has one param.
617 if (function_exists($rule))
618 {
619 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 if ($_in_array == TRUE)
622 {
623 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
624 }
625 else
626 {
627 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
628 }
629 }
patwork02404a12011-04-08 15:45:46 +0200630 else
631 {
632 log_message('debug', "Unable to find validation rule: ".$rule);
633 }
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 continue;
636 }
637
638 $result = $this->$rule($postdata, $param);
639
640 if ($_in_array == TRUE)
641 {
642 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
643 }
644 else
645 {
646 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
647 }
648 }
Barry Mienydd671972010-10-04 16:33:58 +0200649
Razican114ab092011-04-25 17:26:45 +0200650 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200652 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 if ( ! isset($this->_error_messages[$rule]))
654 {
655 if (FALSE === ($line = $this->CI->lang->line($rule)))
656 {
657 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200658 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
660 else
661 {
662 $line = $this->_error_messages[$rule];
663 }
Barry Mienydd671972010-10-04 16:33:58 +0200664
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 // Is the parameter we are inserting into the error message the name
Razican114ab092011-04-25 17:26:45 +0200666 // of another field? If so we need to grab its "field label"
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
668 {
Pascal Krietec1895832009-10-13 12:56:43 +0000669 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 // Build the error message
673 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
674
675 // Save the error message
676 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200677
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 if ( ! isset($this->_error_array[$row['field']]))
679 {
680 $this->_error_array[$row['field']] = $message;
681 }
Barry Mienydd671972010-10-04 16:33:58 +0200682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 return;
684 }
685 }
686 }
687
688 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 /**
691 * Translate a field name
692 *
693 * @access private
694 * @param string the field name
695 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200696 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100697 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
699 // Do we need to translate the field name?
700 // We look for the prefix lang: to determine this
701 if (substr($fieldname, 0, 5) == 'lang:')
702 {
703 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200704 $line = substr($fieldname, 5);
705
Razican114ab092011-04-25 17:26:45 +0200706 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
708 {
709 return $line;
710 }
711 }
712
713 return $fieldname;
714 }
715
716 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200717
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 /**
719 * Get the value from a form
720 *
721 * Permits you to repopulate a form field with the value it was submitted
722 * with, or, if that value doesn't exist, with the default
723 *
724 * @access public
725 * @param string the field name
726 * @param string
727 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200728 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100729 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 {
731 if ( ! isset($this->_field_data[$field]))
732 {
733 return $default;
734 }
Barry Mienydd671972010-10-04 16:33:58 +0200735
Phil Sturgeon5c561802011-01-05 16:31:59 +0000736 // If the data is an array output them one at a time.
Razican114ab092011-04-25 17:26:45 +0200737 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000738 if (is_array($this->_field_data[$field]['postdata']))
739 {
740 return array_shift($this->_field_data[$field]['postdata']);
741 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 return $this->_field_data[$field]['postdata'];
744 }
Barry Mienydd671972010-10-04 16:33:58 +0200745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 /**
749 * Set Select
750 *
751 * Enables pull-down lists to be set to the value the user
752 * selected in the event of an error
753 *
754 * @access public
755 * @param string
756 * @param string
757 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200758 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100759 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200760 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
762 {
763 if ($default === TRUE AND count($this->_field_data) === 0)
764 {
765 return ' selected="selected"';
766 }
767 return '';
768 }
Barry Mienydd671972010-10-04 16:33:58 +0200769
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200771
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 if (is_array($field))
773 {
774 if ( ! in_array($value, $field))
775 {
776 return '';
777 }
778 }
779 else
780 {
781 if (($field == '' OR $value == '') OR ($field != $value))
782 {
783 return '';
784 }
785 }
Barry Mienydd671972010-10-04 16:33:58 +0200786
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 return ' selected="selected"';
788 }
Barry Mienydd671972010-10-04 16:33:58 +0200789
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 /**
793 * Set Radio
794 *
795 * Enables radio buttons to be set to the value the user
796 * selected in the event of an error
797 *
798 * @access public
799 * @param string
800 * @param string
801 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200802 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100803 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 {
805 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
806 {
807 if ($default === TRUE AND count($this->_field_data) === 0)
808 {
809 return ' checked="checked"';
810 }
811 return '';
812 }
Barry Mienydd671972010-10-04 16:33:58 +0200813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 if (is_array($field))
817 {
818 if ( ! in_array($value, $field))
819 {
820 return '';
821 }
822 }
823 else
824 {
825 if (($field == '' OR $value == '') OR ($field != $value))
826 {
827 return '';
828 }
829 }
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 return ' checked="checked"';
832 }
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 /**
837 * Set Checkbox
838 *
839 * Enables checkboxes to be set to the value the user
840 * selected in the event of an error
841 *
842 * @access public
843 * @param string
844 * @param string
845 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200846 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100847 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 {
849 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
850 {
851 if ($default === TRUE AND count($this->_field_data) === 0)
852 {
853 return ' checked="checked"';
854 }
855 return '';
856 }
Barry Mienydd671972010-10-04 16:33:58 +0200857
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200859
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 if (is_array($field))
861 {
862 if ( ! in_array($value, $field))
863 {
864 return '';
865 }
866 }
867 else
868 {
869 if (($field == '' OR $value == '') OR ($field != $value))
870 {
871 return '';
872 }
873 }
Barry Mienydd671972010-10-04 16:33:58 +0200874
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 return ' checked="checked"';
876 }
Barry Mienydd671972010-10-04 16:33:58 +0200877
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200879
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 /**
881 * Required
882 *
883 * @access public
884 * @param string
885 * @return bool
886 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100887 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
889 if ( ! is_array($str))
890 {
891 return (trim($str) == '') ? FALSE : TRUE;
892 }
893 else
894 {
895 return ( ! empty($str));
896 }
897 }
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500902 * Performs a Regular Expression match test.
903 *
904 * @access public
905 * @param string
906 * @param regex
907 * @return bool
908 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100909 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500910 {
911 if ( ! preg_match($regex, $str))
912 {
913 return FALSE;
914 }
915
Razican114ab092011-04-25 17:26:45 +0200916 return TRUE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500917 }
918
919 // --------------------------------------------------------------------
920
921 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 * Match one field to another
923 *
924 * @access public
925 * @param string
926 * @param field
927 * @return bool
928 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100929 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 {
931 if ( ! isset($_POST[$field]))
932 {
Barry Mienydd671972010-10-04 16:33:58 +0200933 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 $field = $_POST[$field];
937
938 return ($str !== $field) ? FALSE : TRUE;
939 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100940
941 // --------------------------------------------------------------------
942
943 /**
944 * Match one field to another
945 *
946 * @access public
947 * @param string
948 * @param field
949 * @return bool
950 */
951 public function is_unique($str, $field)
952 {
953 list($table, $field)=explode('.', $field);
954 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
955
956 return $query->num_rows() === 0;
957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 /**
962 * Minimum Length
963 *
964 * @access public
965 * @param string
966 * @param value
967 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200968 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100969 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 {
971 if (preg_match("/[^0-9]/", $val))
972 {
973 return FALSE;
974 }
975
976 if (function_exists('mb_strlen'))
977 {
Barry Mienydd671972010-10-04 16:33:58 +0200978 return (mb_strlen($str) < $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 }
Barry Mienydd671972010-10-04 16:33:58 +0200980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 return (strlen($str) < $val) ? FALSE : TRUE;
982 }
Barry Mienydd671972010-10-04 16:33:58 +0200983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 /**
987 * Max Length
988 *
989 * @access public
990 * @param string
991 * @param value
992 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200993 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100994 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
996 if (preg_match("/[^0-9]/", $val))
997 {
998 return FALSE;
999 }
1000
1001 if (function_exists('mb_strlen'))
1002 {
Barry Mienydd671972010-10-04 16:33:58 +02001003 return (mb_strlen($str) > $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 return (strlen($str) > $val) ? FALSE : TRUE;
1007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 /**
1012 * Exact Length
1013 *
1014 * @access public
1015 * @param string
1016 * @param value
1017 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001018 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001019 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 {
1021 if (preg_match("/[^0-9]/", $val))
1022 {
1023 return FALSE;
1024 }
1025
1026 if (function_exists('mb_strlen'))
1027 {
Barry Mienydd671972010-10-04 16:33:58 +02001028 return (mb_strlen($str) != $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 }
Barry Mienydd671972010-10-04 16:33:58 +02001030
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 return (strlen($str) != $val) ? FALSE : TRUE;
1032 }
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001035
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 /**
1037 * Valid Email
1038 *
1039 * @access public
1040 * @param string
1041 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001042 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001043 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 {
1045 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
1046 }
1047
1048 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 /**
1051 * Valid Emails
1052 *
1053 * @access public
1054 * @param string
1055 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001056 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001057 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 {
1059 if (strpos($str, ',') === FALSE)
1060 {
1061 return $this->valid_email(trim($str));
1062 }
Barry Mienydd671972010-10-04 16:33:58 +02001063
Pascal Kriete14287f32011-02-14 13:39:34 -05001064 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 {
1066 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
1067 {
1068 return FALSE;
1069 }
1070 }
Barry Mienydd671972010-10-04 16:33:58 +02001071
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 return TRUE;
1073 }
1074
1075 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001076
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 /**
1078 * Validate IP Address
1079 *
1080 * @access public
1081 * @param string
1082 * @return string
1083 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001084 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 {
1086 return $this->CI->input->valid_ip($ip);
1087 }
1088
1089 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001090
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 /**
1092 * Alpha
1093 *
1094 * @access public
1095 * @param string
1096 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001097 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001098 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
1100 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
1101 }
Barry Mienydd671972010-10-04 16:33:58 +02001102
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001104
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 /**
1106 * Alpha-numeric
1107 *
1108 * @access public
1109 * @param string
1110 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001111 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001112 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 {
1114 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
1115 }
Barry Mienydd671972010-10-04 16:33:58 +02001116
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001118
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 /**
1120 * Alpha-numeric with underscores and dashes
1121 *
1122 * @access public
1123 * @param string
1124 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001125 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001126 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 {
1128 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
1129 }
Barry Mienydd671972010-10-04 16:33:58 +02001130
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001132
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 /**
1134 * Numeric
1135 *
1136 * @access public
1137 * @param string
1138 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001139 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001140 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 {
1142 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
1143
1144 }
1145
1146 // --------------------------------------------------------------------
1147
Barry Mienydd671972010-10-04 16:33:58 +02001148 /**
1149 * Is Numeric
1150 *
1151 * @access public
1152 * @param string
1153 * @return bool
1154 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001155 public function is_numeric($str)
Barry Mienydd671972010-10-04 16:33:58 +02001156 {
1157 return ( ! is_numeric($str)) ? FALSE : TRUE;
1158 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001159
1160 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001161
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 /**
1163 * Integer
1164 *
1165 * @access public
1166 * @param string
1167 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001168 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001169 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001171 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1172 }
1173
1174 // --------------------------------------------------------------------
1175
1176 /**
1177 * Decimal number
1178 *
1179 * @access public
1180 * @param string
1181 * @return bool
1182 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001183 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001184 {
1185 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1186 }
1187
1188 // --------------------------------------------------------------------
1189
1190 /**
1191 * Greather than
1192 *
1193 * @access public
1194 * @param string
1195 * @return bool
1196 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001197 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001198 {
1199 if ( ! is_numeric($str))
1200 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001201 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001202 }
1203 return $str > $min;
1204 }
1205
1206 // --------------------------------------------------------------------
1207
1208 /**
1209 * Less than
1210 *
1211 * @access public
1212 * @param string
1213 * @return bool
1214 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001215 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001216 {
1217 if ( ! is_numeric($str))
1218 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001219 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001220 }
1221 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001223
1224 // --------------------------------------------------------------------
1225
Barry Mienydd671972010-10-04 16:33:58 +02001226 /**
Razican114ab092011-04-25 17:26:45 +02001227 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001228 *
1229 * @access public
1230 * @param string
1231 * @return bool
1232 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001233 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001234 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001235 return (bool) preg_match( '/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001236 }
1237
1238 // --------------------------------------------------------------------
1239
1240 /**
Razican114ab092011-04-25 17:26:45 +02001241 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001242 *
1243 * @access public
1244 * @param string
1245 * @return bool
1246 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001247 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001248 {
1249 if ( ! preg_match( '/^[0-9]+$/', $str))
1250 {
1251 return FALSE;
1252 }
1253
1254 if ($str == 0)
1255 {
1256 return FALSE;
1257 }
1258
1259 return TRUE;
1260 }
1261
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001263
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 /**
1265 * Valid Base64
1266 *
1267 * Tests a string for characters outside of the Base64 alphabet
1268 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1269 *
1270 * @access public
1271 * @param string
1272 * @return bool
1273 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001274 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 {
1276 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1277 }
Barry Mienydd671972010-10-04 16:33:58 +02001278
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 /**
1282 * Prep data for form
1283 *
1284 * This function allows HTML to be safely shown in a form.
1285 * Special characters are converted.
1286 *
1287 * @access public
1288 * @param string
1289 * @return string
1290 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001291 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 {
1293 if (is_array($data))
1294 {
1295 foreach ($data as $key => $val)
1296 {
1297 $data[$key] = $this->prep_for_form($val);
1298 }
Barry Mienydd671972010-10-04 16:33:58 +02001299
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 return $data;
1301 }
Barry Mienydd671972010-10-04 16:33:58 +02001302
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 if ($this->_safe_form_data == FALSE OR $data === '')
1304 {
1305 return $data;
1306 }
1307
1308 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1309 }
Barry Mienydd671972010-10-04 16:33:58 +02001310
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001312
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 /**
1314 * Prep URL
1315 *
1316 * @access public
1317 * @param string
1318 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001319 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001320 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 {
1322 if ($str == 'http://' OR $str == '')
1323 {
1324 return '';
1325 }
Barry Mienydd671972010-10-04 16:33:58 +02001326
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
1328 {
1329 $str = 'http://'.$str;
1330 }
Barry Mienydd671972010-10-04 16:33:58 +02001331
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 return $str;
1333 }
Barry Mienydd671972010-10-04 16:33:58 +02001334
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001336
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 /**
1338 * Strip Image Tags
1339 *
1340 * @access public
1341 * @param string
1342 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001343 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001344 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 {
1346 return $this->CI->input->strip_image_tags($str);
1347 }
Barry Mienydd671972010-10-04 16:33:58 +02001348
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001350
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 /**
1352 * XSS Clean
1353 *
1354 * @access public
1355 * @param string
1356 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001357 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001358 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 {
Derek Jones5640a712010-04-23 11:22:40 -05001360 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 }
Barry Mienydd671972010-10-04 16:33:58 +02001362
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001364
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 /**
1366 * Convert PHP tags to entities
1367 *
1368 * @access public
1369 * @param string
1370 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001371 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001372 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 {
Razican114ab092011-04-25 17:26:45 +02001374 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 }
1376
1377}
1378// END Form Validation Class
1379
1380/* End of file Form_validation.php */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001381/* Location: ./system/libraries/Form_validation.php */