blob: 6f79a554ab3442e5b92179d823b21cfefa7b5ad2 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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
Derek Allard2067d1a2008-11-13 22:59:24 +000029 var $CI;
Barry Mienydd671972010-10-04 16:33:58 +020030 var $_field_data = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000031 var $_config_rules = array();
32 var $_error_array = array();
Barry Mienydd671972010-10-04 16:33:58 +020033 var $_error_messages = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000034 var $_error_prefix = '<p>';
35 var $_error_suffix = '</p>';
36 var $error_string = '';
Barry Mienydd671972010-10-04 16:33:58 +020037 var $_safe_form_data = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000038
39
40 /**
41 * Constructor
Barry Mienydd671972010-10-04 16:33:58 +020042 */
Greg Akera9263282010-11-10 15:26:43 -060043 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +020044 {
Derek Allard2067d1a2008-11-13 22:59:24 +000045 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Allard2067d1a2008-11-13 22:59:24 +000047 // Validation rules can be stored in a config file.
48 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020049
Derek Allard2067d1a2008-11-13 22:59:24 +000050 // Automatically load the form helper
51 $this->CI->load->helper('form');
52
53 // Set the character encoding in MB.
54 if (function_exists('mb_internal_encoding'))
55 {
56 mb_internal_encoding($this->CI->config->item('charset'));
57 }
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Allard2067d1a2008-11-13 22:59:24 +000059 log_message('debug', "Form Validation Class Initialized");
60 }
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 /**
65 * Set Rules
66 *
67 * This function takes an array of field names and validation
68 * rules as input, validates the info, and stores it
69 *
70 * @access public
71 * @param mixed
72 * @param string
73 * @return void
74 */
75 function set_rules($field, $label = '', $rules = '')
76 {
77 // No reason to set rules if we have no POST data
78 if (count($_POST) == 0)
79 {
Greg Aker9f9af602010-11-10 15:41:51 -060080 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +000081 }
Barry Mienydd671972010-10-04 16:33:58 +020082
Derek Allard2067d1a2008-11-13 22:59:24 +000083 // If an array was passed via the first parameter instead of indidual string
84 // values we cycle through it and recursively call this function.
85 if (is_array($field))
86 {
87 foreach ($field as $row)
88 {
89 // Houston, we have a problem...
90 if ( ! isset($row['field']) OR ! isset($row['rules']))
91 {
92 continue;
93 }
94
95 // If the field label wasn't passed we use the field name
96 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
97
98 // Here we go!
99 $this->set_rules($row['field'], $label, $row['rules']);
100 }
Greg Aker9f9af602010-11-10 15:41:51 -0600101 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 // No fields? Nothing to do...
105 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
106 {
Greg Aker9f9af602010-11-10 15:41:51 -0600107 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
109
110 // If the field label wasn't passed we use the field name
111 $label = ($label == '') ? $field : $label;
112
113 // Is the field name an array? We test for the existence of a bracket "[" in
114 // the field name to determine this. If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200115 // into its components so that we can fetch the corresponding POST data later
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 if (strpos($field, '[') !== FALSE AND preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200117 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 // Note: Due to a bug in current() that affects some versions
119 // of PHP we can not pass function call directly into it
120 $x = explode('[', $field);
121 $indexes[] = current($x);
122
123 for ($i = 0; $i < count($matches['0']); $i++)
124 {
125 if ($matches['1'][$i] != '')
126 {
127 $indexes[] = $matches['1'][$i];
128 }
129 }
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 $is_array = TRUE;
132 }
133 else
134 {
Barry Mienydd671972010-10-04 16:33:58 +0200135 $indexes = array();
136 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
139 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000141 'field' => $field,
142 'label' => $label,
143 'rules' => $rules,
144 'is_array' => $is_array,
145 'keys' => $indexes,
146 'postdata' => NULL,
147 'error' => ''
148 );
Greg Aker9f9af602010-11-10 15:41:51 -0600149
150 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
153 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 /**
156 * Set Error Message
157 *
158 * Lets users set their own error messages on the fly. Note: The key
159 * name has to match the function name that it corresponds to.
160 *
161 * @access public
162 * @param string
163 * @param string
164 * @return string
165 */
166 function set_message($lang, $val = '')
167 {
168 if ( ! is_array($lang))
169 {
170 $lang = array($lang => $val);
171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 $this->_error_messages = array_merge($this->_error_messages, $lang);
Phil Sturgeonc3828712011-01-19 12:31:47 +0000174
Greg Aker9f9af602010-11-10 15:41:51 -0600175 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 /**
181 * Set The Error Delimiter
182 *
183 * Permits a prefix/suffix to be added to each error message
184 *
185 * @access public
186 * @param string
187 * @param string
188 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200189 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
191 {
192 $this->_error_prefix = $prefix;
193 $this->_error_suffix = $suffix;
Phil Sturgeonc3828712011-01-19 12:31:47 +0000194
Greg Aker9f9af602010-11-10 15:41:51 -0600195 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
197
198 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 /**
201 * Get Error Message
202 *
203 * Gets the error message associated with a particular field
204 *
205 * @access public
206 * @param string the field name
207 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200208 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200210 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
212 {
213 return '';
214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 if ($prefix == '')
217 {
218 $prefix = $this->_error_prefix;
219 }
220
221 if ($suffix == '')
222 {
223 $suffix = $this->_error_suffix;
224 }
225
226 return $prefix.$this->_field_data[$field]['error'].$suffix;
227 }
228
229 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 /**
232 * Error String
233 *
234 * Returns the error messages as a string, wrapped in the error delimiters
235 *
236 * @access public
237 * @param string
238 * @param string
239 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200240 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 function error_string($prefix = '', $suffix = '')
242 {
243 // No errrors, validation passes!
244 if (count($this->_error_array) === 0)
245 {
246 return '';
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 if ($prefix == '')
250 {
251 $prefix = $this->_error_prefix;
252 }
253
254 if ($suffix == '')
255 {
256 $suffix = $this->_error_suffix;
257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 // Generate the error string
260 $str = '';
261 foreach ($this->_error_array as $val)
262 {
263 if ($val != '')
264 {
265 $str .= $prefix.$val.$suffix."\n";
266 }
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 return $str;
270 }
271
272 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 /**
275 * Run the Validator
276 *
277 * This function does all the work.
278 *
279 * @access public
280 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200281 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 function run($group = '')
283 {
284 // Do we even have any data to process? Mm?
285 if (count($_POST) == 0)
286 {
287 return FALSE;
288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 // Does the _field_data array containing the validation rules exist?
291 // If not, we look to see if they were assigned via a config file
292 if (count($this->_field_data) == 0)
293 {
294 // No validation rules? We're done...
295 if (count($this->_config_rules) == 0)
296 {
297 return FALSE;
298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // Is there a validation rule for the particular URI being accessed?
301 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 if ($uri != '' AND isset($this->_config_rules[$uri]))
304 {
305 $this->set_rules($this->_config_rules[$uri]);
306 }
307 else
308 {
309 $this->set_rules($this->_config_rules);
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // We're we able to set the rules correctly?
313 if (count($this->_field_data) == 0)
314 {
315 log_message('debug', "Unable to find validation rules");
316 return FALSE;
317 }
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // Load the language file containing error messages
321 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200322
323 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // corresponding $_POST item and test for errors
325 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200326 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
328 // 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 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 if ($row['is_array'] == TRUE)
331 {
332 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
333 }
334 else
335 {
336 if (isset($_POST[$field]) AND $_POST[$field] != "")
337 {
338 $this->_field_data[$field]['postdata'] = $_POST[$field];
339 }
340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
342 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 }
344
345 // Did we end up with any errors?
346 $total_errors = count($this->_error_array);
347
348 if ($total_errors > 0)
349 {
350 $this->_safe_form_data = TRUE;
351 }
352
353 // Now we need to re-set the POST data with the new, processed data
354 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 // No errors, validation passes!
357 if ($total_errors == 0)
358 {
359 return TRUE;
360 }
361
362 // Validation fails
363 return FALSE;
364 }
365
366 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 /**
369 * Traverse a multidimensional $_POST array index until the data is found
370 *
371 * @access private
372 * @param array
373 * @param array
374 * @param integer
375 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200376 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 function _reduce_array($array, $keys, $i = 0)
378 {
379 if (is_array($array))
380 {
381 if (isset($keys[$i]))
382 {
383 if (isset($array[$keys[$i]]))
384 {
385 $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1));
386 }
387 else
388 {
389 return NULL;
390 }
391 }
392 else
393 {
394 return $array;
395 }
396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 return $array;
399 }
400
401 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 /**
404 * Re-populate the _POST array with our finalized and processed data
405 *
406 * @access private
407 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200408 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 function _reset_post_array()
410 {
411 foreach ($this->_field_data as $field => $row)
412 {
413 if ( ! is_null($row['postdata']))
414 {
415 if ($row['is_array'] == FALSE)
416 {
417 if (isset($_POST[$row['field']]))
418 {
419 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
420 }
421 }
422 else
423 {
Derek Jones63eeae32009-02-10 19:08:56 +0000424 // start with a reference
425 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Jones63eeae32009-02-10 19:08:56 +0000427 // before we assign values, make a reference to the right POST key
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 if (count($row['keys']) == 1)
429 {
Derek Jones63eeae32009-02-10 19:08:56 +0000430 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432 else
433 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 foreach ($row['keys'] as $val)
435 {
Derek Jones63eeae32009-02-10 19:08:56 +0000436 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438 }
Derek Jones63eeae32009-02-10 19:08:56 +0000439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000441 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 $array = array();
443 foreach ($row['postdata'] as $k => $v)
444 {
445 $array[$k] = $this->prep_for_form($v);
446 }
Derek Jones63eeae32009-02-10 19:08:56 +0000447
448 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450 else
Derek Jones63eeae32009-02-10 19:08:56 +0000451 {
452 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455 }
456 }
457 }
458
459 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 /**
462 * Executes the Validation routines
463 *
464 * @access private
465 * @param array
466 * @param array
467 * @param mixed
468 * @param integer
469 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200470 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 function _execute($row, $rules, $postdata = NULL, $cycles = 0)
472 {
473 // If the $_POST data is an array we will run a recursive call
474 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200475 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 foreach ($postdata as $key => $val)
477 {
478 $this->_execute($row, $rules, $val, $cycles);
479 $cycles++;
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 return;
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 // --------------------------------------------------------------------
486
487 // If the field is blank, but NOT required, no further tests are necessary
488 $callback = FALSE;
489 if ( ! in_array('required', $rules) AND is_null($postdata))
490 {
491 // Before we bail out, does the rule contain a callback?
492 if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
493 {
494 $callback = TRUE;
495 $rules = (array('1' => $match[1]));
496 }
497 else
498 {
499 return;
500 }
501 }
502
503 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // Isset Test. Typically this rule will only apply to checkboxes.
506 if (is_null($postdata) AND $callback == FALSE)
507 {
508 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
509 {
510 // Set the message type
511 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 if ( ! isset($this->_error_messages[$type]))
514 {
515 if (FALSE === ($line = $this->CI->lang->line($type)))
516 {
517 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200518 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
520 else
521 {
522 $line = $this->_error_messages[$type];
523 }
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 // Build the error message
526 $message = sprintf($line, $this->_translate_fieldname($row['label']));
527
528 // Save the error message
529 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 if ( ! isset($this->_error_array[$row['field']]))
532 {
533 $this->_error_array[$row['field']] = $message;
534 }
535 }
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 return;
538 }
539
540 // --------------------------------------------------------------------
541
542 // Cycle through each rule and run it
543 foreach ($rules As $rule)
544 {
545 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 // We set the $postdata variable with the current data in our master array so that
548 // each cycle of the loop is dealing with the processed data from the last cycle
549 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
550 {
551 // We shouldn't need this safety, but just in case there isn't an array index
552 // associated with this cycle we'll bail out
553 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
554 {
555 continue;
556 }
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
559 $_in_array = TRUE;
560 }
561 else
562 {
563 $postdata = $this->_field_data[$row['field']]['postdata'];
564 }
565
566 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200567
568 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 $callback = FALSE;
570 if (substr($rule, 0, 9) == 'callback_')
571 {
572 $rule = substr($rule, 9);
573 $callback = TRUE;
574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 // Strip the parameter (if exists) from the rule
577 // Rules can contain a parameter: max_length[5]
578 $param = FALSE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500579 if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
581 $rule = $match[1];
582 $param = $match[2];
583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 // Call the function that corresponds to the rule
586 if ($callback === TRUE)
587 {
588 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200589 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 continue;
591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 // Run the function and grab the result
594 $result = $this->CI->$rule($postdata, $param);
595
596 // Re-assign the result to the master data array
597 if ($_in_array == TRUE)
598 {
599 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
600 }
601 else
602 {
603 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
604 }
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 // If the field isn't required and we just processed a callback we'll move on...
607 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
608 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000609 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 }
611 }
612 else
Barry Mienydd671972010-10-04 16:33:58 +0200613 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 if ( ! method_exists($this, $rule))
615 {
Barry Mienydd671972010-10-04 16:33:58 +0200616 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 // Users can use any native PHP function call that has one param.
618 if (function_exists($rule))
619 {
620 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 if ($_in_array == TRUE)
623 {
624 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
625 }
626 else
627 {
628 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
629 }
630 }
patwork02404a12011-04-08 15:45:46 +0200631 else
632 {
633 log_message('debug', "Unable to find validation rule: ".$rule);
634 }
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 continue;
637 }
638
639 $result = $this->$rule($postdata, $param);
640
641 if ($_in_array == TRUE)
642 {
643 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
644 }
645 else
646 {
647 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
648 }
649 }
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 // Did the rule test negatively? If so, grab the error.
652 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200653 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 if ( ! isset($this->_error_messages[$rule]))
655 {
656 if (FALSE === ($line = $this->CI->lang->line($rule)))
657 {
658 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200659 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
661 else
662 {
663 $line = $this->_error_messages[$rule];
664 }
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 // Is the parameter we are inserting into the error message the name
667 // of another field? If so we need to grab its "field label"
668 if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
669 {
Pascal Krietec1895832009-10-13 12:56:43 +0000670 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 // Build the error message
674 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
675
676 // Save the error message
677 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 if ( ! isset($this->_error_array[$row['field']]))
680 {
681 $this->_error_array[$row['field']] = $message;
682 }
Barry Mienydd671972010-10-04 16:33:58 +0200683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 return;
685 }
686 }
687 }
688
689 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 /**
692 * Translate a field name
693 *
694 * @access private
695 * @param string the field name
696 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200697 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 function _translate_fieldname($fieldname)
699 {
700 // Do we need to translate the field name?
701 // We look for the prefix lang: to determine this
702 if (substr($fieldname, 0, 5) == 'lang:')
703 {
704 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200705 $line = substr($fieldname, 5);
706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 // Were we able to translate the field name? If not we use $line
708 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
709 {
710 return $line;
711 }
712 }
713
714 return $fieldname;
715 }
716
717 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200718
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 /**
720 * Get the value from a form
721 *
722 * Permits you to repopulate a form field with the value it was submitted
723 * with, or, if that value doesn't exist, with the default
724 *
725 * @access public
726 * @param string the field name
727 * @param string
728 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200729 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 function set_value($field = '', $default = '')
731 {
732 if ( ! isset($this->_field_data[$field]))
733 {
734 return $default;
735 }
Barry Mienydd671972010-10-04 16:33:58 +0200736
Phil Sturgeon5c561802011-01-05 16:31:59 +0000737 // If the data is an array output them one at a time.
738 // E.g: form_input('name[]', set_value('name[]');
739 if (is_array($this->_field_data[$field]['postdata']))
740 {
741 return array_shift($this->_field_data[$field]['postdata']);
742 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000743
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 return $this->_field_data[$field]['postdata'];
745 }
Barry Mienydd671972010-10-04 16:33:58 +0200746
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200748
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 /**
750 * Set Select
751 *
752 * Enables pull-down lists to be set to the value the user
753 * selected in the event of an error
754 *
755 * @access public
756 * @param string
757 * @param string
758 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200759 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200761 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
763 {
764 if ($default === TRUE AND count($this->_field_data) === 0)
765 {
766 return ' selected="selected"';
767 }
768 return '';
769 }
Barry Mienydd671972010-10-04 16:33:58 +0200770
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200772
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 if (is_array($field))
774 {
775 if ( ! in_array($value, $field))
776 {
777 return '';
778 }
779 }
780 else
781 {
782 if (($field == '' OR $value == '') OR ($field != $value))
783 {
784 return '';
785 }
786 }
Barry Mienydd671972010-10-04 16:33:58 +0200787
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 return ' selected="selected"';
789 }
Barry Mienydd671972010-10-04 16:33:58 +0200790
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200792
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 /**
794 * Set Radio
795 *
796 * Enables radio buttons to be set to the value the user
797 * selected in the event of an error
798 *
799 * @access public
800 * @param string
801 * @param string
802 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200803 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 function set_radio($field = '', $value = '', $default = FALSE)
805 {
806 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
807 {
808 if ($default === TRUE AND count($this->_field_data) === 0)
809 {
810 return ' checked="checked"';
811 }
812 return '';
813 }
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 if (is_array($field))
818 {
819 if ( ! in_array($value, $field))
820 {
821 return '';
822 }
823 }
824 else
825 {
826 if (($field == '' OR $value == '') OR ($field != $value))
827 {
828 return '';
829 }
830 }
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 return ' checked="checked"';
833 }
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 /**
838 * Set Checkbox
839 *
840 * Enables checkboxes to be set to the value the user
841 * selected in the event of an error
842 *
843 * @access public
844 * @param string
845 * @param string
846 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200847 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 function set_checkbox($field = '', $value = '', $default = FALSE)
849 {
850 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
851 {
852 if ($default === TRUE AND count($this->_field_data) === 0)
853 {
854 return ' checked="checked"';
855 }
856 return '';
857 }
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 if (is_array($field))
862 {
863 if ( ! in_array($value, $field))
864 {
865 return '';
866 }
867 }
868 else
869 {
870 if (($field == '' OR $value == '') OR ($field != $value))
871 {
872 return '';
873 }
874 }
Barry Mienydd671972010-10-04 16:33:58 +0200875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 return ' checked="checked"';
877 }
Barry Mienydd671972010-10-04 16:33:58 +0200878
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 /**
882 * Required
883 *
884 * @access public
885 * @param string
886 * @return bool
887 */
888 function required($str)
889 {
890 if ( ! is_array($str))
891 {
892 return (trim($str) == '') ? FALSE : TRUE;
893 }
894 else
895 {
896 return ( ! empty($str));
897 }
898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200901
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500903 * Performs a Regular Expression match test.
904 *
905 * @access public
906 * @param string
907 * @param regex
908 * @return bool
909 */
910 function regex_match($str, $regex)
911 {
912 if ( ! preg_match($regex, $str))
913 {
914 return FALSE;
915 }
916
917 return TRUE;
918 }
919
920 // --------------------------------------------------------------------
921
922 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 * Match one field to another
924 *
925 * @access public
926 * @param string
927 * @param field
928 * @return bool
929 */
930 function matches($str, $field)
931 {
932 if ( ! isset($_POST[$field]))
933 {
Barry Mienydd671972010-10-04 16:33:58 +0200934 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 $field = $_POST[$field];
938
939 return ($str !== $field) ? FALSE : TRUE;
940 }
Barry Mienydd671972010-10-04 16:33:58 +0200941
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200943
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 /**
945 * Minimum Length
946 *
947 * @access public
948 * @param string
949 * @param value
950 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200951 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 function min_length($str, $val)
953 {
954 if (preg_match("/[^0-9]/", $val))
955 {
956 return FALSE;
957 }
958
959 if (function_exists('mb_strlen'))
960 {
Barry Mienydd671972010-10-04 16:33:58 +0200961 return (mb_strlen($str) < $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 }
Barry Mienydd671972010-10-04 16:33:58 +0200963
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 return (strlen($str) < $val) ? FALSE : TRUE;
965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200968
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 /**
970 * Max Length
971 *
972 * @access public
973 * @param string
974 * @param value
975 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200976 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 function max_length($str, $val)
978 {
979 if (preg_match("/[^0-9]/", $val))
980 {
981 return FALSE;
982 }
983
984 if (function_exists('mb_strlen'))
985 {
Barry Mienydd671972010-10-04 16:33:58 +0200986 return (mb_strlen($str) > $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 }
Barry Mienydd671972010-10-04 16:33:58 +0200988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 return (strlen($str) > $val) ? FALSE : TRUE;
990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200993
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 /**
995 * Exact Length
996 *
997 * @access public
998 * @param string
999 * @param value
1000 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001001 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 function exact_length($str, $val)
1003 {
1004 if (preg_match("/[^0-9]/", $val))
1005 {
1006 return FALSE;
1007 }
1008
1009 if (function_exists('mb_strlen'))
1010 {
Barry Mienydd671972010-10-04 16:33:58 +02001011 return (mb_strlen($str) != $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 }
Barry Mienydd671972010-10-04 16:33:58 +02001013
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 return (strlen($str) != $val) ? FALSE : TRUE;
1015 }
Barry Mienydd671972010-10-04 16:33:58 +02001016
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 /**
1020 * Valid Email
1021 *
1022 * @access public
1023 * @param string
1024 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001025 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 function valid_email($str)
1027 {
1028 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
1029 }
1030
1031 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001032
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 /**
1034 * Valid Emails
1035 *
1036 * @access public
1037 * @param string
1038 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001039 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 function valid_emails($str)
1041 {
1042 if (strpos($str, ',') === FALSE)
1043 {
1044 return $this->valid_email(trim($str));
1045 }
Barry Mienydd671972010-10-04 16:33:58 +02001046
Pascal Kriete14287f32011-02-14 13:39:34 -05001047 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
1049 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
1050 {
1051 return FALSE;
1052 }
1053 }
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 return TRUE;
1056 }
1057
1058 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001059
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 /**
1061 * Validate IP Address
1062 *
1063 * @access public
1064 * @param string
1065 * @return string
1066 */
1067 function valid_ip($ip)
1068 {
1069 return $this->CI->input->valid_ip($ip);
1070 }
1071
1072 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001073
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 /**
1075 * Alpha
1076 *
1077 * @access public
1078 * @param string
1079 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001080 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 function alpha($str)
1082 {
1083 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
1084 }
Barry Mienydd671972010-10-04 16:33:58 +02001085
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001087
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 /**
1089 * Alpha-numeric
1090 *
1091 * @access public
1092 * @param string
1093 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001094 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 function alpha_numeric($str)
1096 {
1097 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
1098 }
Barry Mienydd671972010-10-04 16:33:58 +02001099
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001101
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 /**
1103 * Alpha-numeric with underscores and dashes
1104 *
1105 * @access public
1106 * @param string
1107 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001108 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 function alpha_dash($str)
1110 {
1111 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
1112 }
Barry Mienydd671972010-10-04 16:33:58 +02001113
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001115
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 /**
1117 * Numeric
1118 *
1119 * @access public
1120 * @param string
1121 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001122 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 function numeric($str)
1124 {
1125 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
1126
1127 }
1128
1129 // --------------------------------------------------------------------
1130
Barry Mienydd671972010-10-04 16:33:58 +02001131 /**
1132 * Is Numeric
1133 *
1134 * @access public
1135 * @param string
1136 * @return bool
1137 */
1138 function is_numeric($str)
1139 {
1140 return ( ! is_numeric($str)) ? FALSE : TRUE;
1141 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001142
1143 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 /**
1146 * Integer
1147 *
1148 * @access public
1149 * @param string
1150 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001151 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 function integer($str)
1153 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001154 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1155 }
1156
1157 // --------------------------------------------------------------------
1158
1159 /**
1160 * Decimal number
1161 *
1162 * @access public
1163 * @param string
1164 * @return bool
1165 */
1166 function decimal($str)
1167 {
1168 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1169 }
1170
1171 // --------------------------------------------------------------------
1172
1173 /**
1174 * Greather than
1175 *
1176 * @access public
1177 * @param string
1178 * @return bool
1179 */
1180 function greater_than($str, $min)
1181 {
1182 if ( ! is_numeric($str))
1183 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001184 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001185 }
1186 return $str > $min;
1187 }
1188
1189 // --------------------------------------------------------------------
1190
1191 /**
1192 * Less than
1193 *
1194 * @access public
1195 * @param string
1196 * @return bool
1197 */
1198 function less_than($str, $max)
1199 {
1200 if ( ! is_numeric($str))
1201 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001202 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001203 }
1204 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001206
1207 // --------------------------------------------------------------------
1208
Barry Mienydd671972010-10-04 16:33:58 +02001209 /**
1210 * Is a Natural number (0,1,2,3, etc.)
1211 *
1212 * @access public
1213 * @param string
1214 * @return bool
1215 */
1216 function is_natural($str)
1217 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001218 return (bool) preg_match( '/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001219 }
1220
1221 // --------------------------------------------------------------------
1222
1223 /**
1224 * Is a Natural number, but not a zero (1,2,3, etc.)
1225 *
1226 * @access public
1227 * @param string
1228 * @return bool
1229 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001231 {
1232 if ( ! preg_match( '/^[0-9]+$/', $str))
1233 {
1234 return FALSE;
1235 }
1236
1237 if ($str == 0)
1238 {
1239 return FALSE;
1240 }
1241
1242 return TRUE;
1243 }
1244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001246
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 /**
1248 * Valid Base64
1249 *
1250 * Tests a string for characters outside of the Base64 alphabet
1251 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1252 *
1253 * @access public
1254 * @param string
1255 * @return bool
1256 */
1257 function valid_base64($str)
1258 {
1259 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1260 }
Barry Mienydd671972010-10-04 16:33:58 +02001261
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 * Prep data for form
1266 *
1267 * This function allows HTML to be safely shown in a form.
1268 * Special characters are converted.
1269 *
1270 * @access public
1271 * @param string
1272 * @return string
1273 */
1274 function prep_for_form($data = '')
1275 {
1276 if (is_array($data))
1277 {
1278 foreach ($data as $key => $val)
1279 {
1280 $data[$key] = $this->prep_for_form($val);
1281 }
Barry Mienydd671972010-10-04 16:33:58 +02001282
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 return $data;
1284 }
Barry Mienydd671972010-10-04 16:33:58 +02001285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 if ($this->_safe_form_data == FALSE OR $data === '')
1287 {
1288 return $data;
1289 }
1290
1291 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1292 }
Barry Mienydd671972010-10-04 16:33:58 +02001293
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001295
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 /**
1297 * Prep URL
1298 *
1299 * @access public
1300 * @param string
1301 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001302 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 function prep_url($str = '')
1304 {
1305 if ($str == 'http://' OR $str == '')
1306 {
1307 return '';
1308 }
Barry Mienydd671972010-10-04 16:33:58 +02001309
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
1311 {
1312 $str = 'http://'.$str;
1313 }
Barry Mienydd671972010-10-04 16:33:58 +02001314
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 return $str;
1316 }
Barry Mienydd671972010-10-04 16:33:58 +02001317
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001319
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 /**
1321 * Strip Image Tags
1322 *
1323 * @access public
1324 * @param string
1325 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001326 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 function strip_image_tags($str)
1328 {
1329 return $this->CI->input->strip_image_tags($str);
1330 }
Barry Mienydd671972010-10-04 16:33:58 +02001331
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001333
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 /**
1335 * XSS Clean
1336 *
1337 * @access public
1338 * @param string
1339 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001340 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 function xss_clean($str)
1342 {
Derek Jones5640a712010-04-23 11:22:40 -05001343 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 }
Barry Mienydd671972010-10-04 16:33:58 +02001345
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001347
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 /**
1349 * Convert PHP tags to entities
1350 *
1351 * @access public
1352 * @param string
1353 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001354 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 function encode_php_tags($str)
1356 {
1357 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
1358 }
1359
1360}
1361// END Form Validation Class
1362
1363/* End of file Form_validation.php */
patwork02404a12011-04-08 15:45:46 +02001364/* Location: ./system/libraries/Form_validation.php */