blob: f45760024d42ef22c0e57c0f1bbca5655d8b917b [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
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, 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(
Barry Mienydd671972010-10-04 16:33:58 +0200141 'field' => $field,
142 'label' => $label,
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 '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);
Greg Aker9f9af602010-11-10 15:41:51 -0600174
175 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;
Greg Aker9f9af602010-11-10 15:41:51 -0600194
195 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
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500342 preg_match_all('/([a-zA-Z_-]*(\[.*\])?)\|?/i', $row['rules'], $matches);
343
344 $rules = $matches[1];
345 array_pop($rules);
346 unset($matches);
347
348 $this->_execute($row, $rules, $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
350
351 // Did we end up with any errors?
352 $total_errors = count($this->_error_array);
353
354 if ($total_errors > 0)
355 {
356 $this->_safe_form_data = TRUE;
357 }
358
359 // Now we need to re-set the POST data with the new, processed data
360 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // No errors, validation passes!
363 if ($total_errors == 0)
364 {
365 return TRUE;
366 }
367
368 // Validation fails
369 return FALSE;
370 }
371
372 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 /**
375 * Traverse a multidimensional $_POST array index until the data is found
376 *
377 * @access private
378 * @param array
379 * @param array
380 * @param integer
381 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200382 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 function _reduce_array($array, $keys, $i = 0)
384 {
385 if (is_array($array))
386 {
387 if (isset($keys[$i]))
388 {
389 if (isset($array[$keys[$i]]))
390 {
391 $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1));
392 }
393 else
394 {
395 return NULL;
396 }
397 }
398 else
399 {
400 return $array;
401 }
402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 return $array;
405 }
406
407 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 /**
410 * Re-populate the _POST array with our finalized and processed data
411 *
412 * @access private
413 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200414 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 function _reset_post_array()
416 {
417 foreach ($this->_field_data as $field => $row)
418 {
419 if ( ! is_null($row['postdata']))
420 {
421 if ($row['is_array'] == FALSE)
422 {
423 if (isset($_POST[$row['field']]))
424 {
425 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
426 }
427 }
428 else
429 {
Derek Jones63eeae32009-02-10 19:08:56 +0000430 // start with a reference
431 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Jones63eeae32009-02-10 19:08:56 +0000433 // before we assign values, make a reference to the right POST key
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 if (count($row['keys']) == 1)
435 {
Derek Jones63eeae32009-02-10 19:08:56 +0000436 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438 else
439 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 foreach ($row['keys'] as $val)
441 {
Derek Jones63eeae32009-02-10 19:08:56 +0000442 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
444 }
Derek Jones63eeae32009-02-10 19:08:56 +0000445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000447 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 $array = array();
449 foreach ($row['postdata'] as $k => $v)
450 {
451 $array[$k] = $this->prep_for_form($v);
452 }
Derek Jones63eeae32009-02-10 19:08:56 +0000453
454 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
456 else
Derek Jones63eeae32009-02-10 19:08:56 +0000457 {
458 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
461 }
462 }
463 }
464
465 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 /**
468 * Executes the Validation routines
469 *
470 * @access private
471 * @param array
472 * @param array
473 * @param mixed
474 * @param integer
475 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200476 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 function _execute($row, $rules, $postdata = NULL, $cycles = 0)
478 {
479 // If the $_POST data is an array we will run a recursive call
480 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200481 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 foreach ($postdata as $key => $val)
483 {
484 $this->_execute($row, $rules, $val, $cycles);
485 $cycles++;
486 }
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 return;
489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 // --------------------------------------------------------------------
492
493 // If the field is blank, but NOT required, no further tests are necessary
494 $callback = FALSE;
495 if ( ! in_array('required', $rules) AND is_null($postdata))
496 {
497 // Before we bail out, does the rule contain a callback?
498 if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
499 {
500 $callback = TRUE;
501 $rules = (array('1' => $match[1]));
502 }
503 else
504 {
505 return;
506 }
507 }
508
509 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 // Isset Test. Typically this rule will only apply to checkboxes.
512 if (is_null($postdata) AND $callback == FALSE)
513 {
514 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
515 {
516 // Set the message type
517 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 if ( ! isset($this->_error_messages[$type]))
520 {
521 if (FALSE === ($line = $this->CI->lang->line($type)))
522 {
523 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200524 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 }
526 else
527 {
528 $line = $this->_error_messages[$type];
529 }
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 // Build the error message
532 $message = sprintf($line, $this->_translate_fieldname($row['label']));
533
534 // Save the error message
535 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 if ( ! isset($this->_error_array[$row['field']]))
538 {
539 $this->_error_array[$row['field']] = $message;
540 }
541 }
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 return;
544 }
545
546 // --------------------------------------------------------------------
547
548 // Cycle through each rule and run it
549 foreach ($rules As $rule)
550 {
551 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 // We set the $postdata variable with the current data in our master array so that
554 // each cycle of the loop is dealing with the processed data from the last cycle
555 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
556 {
557 // We shouldn't need this safety, but just in case there isn't an array index
558 // associated with this cycle we'll bail out
559 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
560 {
561 continue;
562 }
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
565 $_in_array = TRUE;
566 }
567 else
568 {
569 $postdata = $this->_field_data[$row['field']]['postdata'];
570 }
571
572 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200573
574 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 $callback = FALSE;
576 if (substr($rule, 0, 9) == 'callback_')
577 {
578 $rule = substr($rule, 9);
579 $callback = TRUE;
580 }
Barry Mienydd671972010-10-04 16:33:58 +0200581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 // Strip the parameter (if exists) from the rule
583 // Rules can contain a parameter: max_length[5]
584 $param = FALSE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500585 if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 $rule = $match[1];
588 $param = $match[2];
589 }
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 // Call the function that corresponds to the rule
592 if ($callback === TRUE)
593 {
594 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200595 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 continue;
597 }
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 // Run the function and grab the result
600 $result = $this->CI->$rule($postdata, $param);
601
602 // Re-assign the result to the master data array
603 if ($_in_array == TRUE)
604 {
605 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
606 }
607 else
608 {
609 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
610 }
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 // If the field isn't required and we just processed a callback we'll move on...
613 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
614 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000615 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 }
617 }
618 else
Barry Mienydd671972010-10-04 16:33:58 +0200619 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 if ( ! method_exists($this, $rule))
621 {
Barry Mienydd671972010-10-04 16:33:58 +0200622 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 // Users can use any native PHP function call that has one param.
624 if (function_exists($rule))
625 {
626 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 if ($_in_array == TRUE)
629 {
630 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
631 }
632 else
633 {
634 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
635 }
636 }
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 continue;
639 }
640
641 $result = $this->$rule($postdata, $param);
642
643 if ($_in_array == TRUE)
644 {
645 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
646 }
647 else
648 {
649 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
650 }
651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // Did the rule test negatively? If so, grab the error.
654 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200655 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 if ( ! isset($this->_error_messages[$rule]))
657 {
658 if (FALSE === ($line = $this->CI->lang->line($rule)))
659 {
660 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200661 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 }
663 else
664 {
665 $line = $this->_error_messages[$rule];
666 }
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 // Is the parameter we are inserting into the error message the name
669 // of another field? If so we need to grab its "field label"
670 if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
671 {
Pascal Krietec1895832009-10-13 12:56:43 +0000672 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 // Build the error message
676 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
677
678 // Save the error message
679 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 if ( ! isset($this->_error_array[$row['field']]))
682 {
683 $this->_error_array[$row['field']] = $message;
684 }
Barry Mienydd671972010-10-04 16:33:58 +0200685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 return;
687 }
688 }
689 }
690
691 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 /**
694 * Translate a field name
695 *
696 * @access private
697 * @param string the field name
698 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200699 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 function _translate_fieldname($fieldname)
701 {
702 // Do we need to translate the field name?
703 // We look for the prefix lang: to determine this
704 if (substr($fieldname, 0, 5) == 'lang:')
705 {
706 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200707 $line = substr($fieldname, 5);
708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 // Were we able to translate the field name? If not we use $line
710 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
711 {
712 return $line;
713 }
714 }
715
716 return $fieldname;
717 }
718
719 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200720
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 /**
722 * Get the value from a form
723 *
724 * Permits you to repopulate a form field with the value it was submitted
725 * with, or, if that value doesn't exist, with the default
726 *
727 * @access public
728 * @param string the field name
729 * @param string
730 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200731 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 function set_value($field = '', $default = '')
733 {
734 if ( ! isset($this->_field_data[$field]))
735 {
736 return $default;
737 }
Barry Mienydd671972010-10-04 16:33:58 +0200738
Phil Sturgeon5c561802011-01-05 16:31:59 +0000739 // If the data is an array output them one at a time.
740 // E.g: form_input('name[]', set_value('name[]');
741 if (is_array($this->_field_data[$field]['postdata']))
742 {
743 return array_shift($this->_field_data[$field]['postdata']);
744 }
745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 return $this->_field_data[$field]['postdata'];
747 }
Barry Mienydd671972010-10-04 16:33:58 +0200748
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200750
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 /**
752 * Set Select
753 *
754 * Enables pull-down lists to be set to the value the user
755 * selected in the event of an error
756 *
757 * @access public
758 * @param string
759 * @param string
760 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200761 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200763 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
765 {
766 if ($default === TRUE AND count($this->_field_data) === 0)
767 {
768 return ' selected="selected"';
769 }
770 return '';
771 }
Barry Mienydd671972010-10-04 16:33:58 +0200772
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200774
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 if (is_array($field))
776 {
777 if ( ! in_array($value, $field))
778 {
779 return '';
780 }
781 }
782 else
783 {
784 if (($field == '' OR $value == '') OR ($field != $value))
785 {
786 return '';
787 }
788 }
Barry Mienydd671972010-10-04 16:33:58 +0200789
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 return ' selected="selected"';
791 }
Barry Mienydd671972010-10-04 16:33:58 +0200792
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 /**
796 * Set Radio
797 *
798 * Enables radio buttons to be set to the value the user
799 * selected in the event of an error
800 *
801 * @access public
802 * @param string
803 * @param string
804 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200805 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 function set_radio($field = '', $value = '', $default = FALSE)
807 {
808 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
809 {
810 if ($default === TRUE AND count($this->_field_data) === 0)
811 {
812 return ' checked="checked"';
813 }
814 return '';
815 }
Barry Mienydd671972010-10-04 16:33:58 +0200816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200818
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 if (is_array($field))
820 {
821 if ( ! in_array($value, $field))
822 {
823 return '';
824 }
825 }
826 else
827 {
828 if (($field == '' OR $value == '') OR ($field != $value))
829 {
830 return '';
831 }
832 }
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 return ' checked="checked"';
835 }
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200838
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 /**
840 * Set Checkbox
841 *
842 * Enables checkboxes to be set to the value the user
843 * selected in the event of an error
844 *
845 * @access public
846 * @param string
847 * @param string
848 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200849 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 function set_checkbox($field = '', $value = '', $default = FALSE)
851 {
852 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
853 {
854 if ($default === TRUE AND count($this->_field_data) === 0)
855 {
856 return ' checked="checked"';
857 }
858 return '';
859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 if (is_array($field))
864 {
865 if ( ! in_array($value, $field))
866 {
867 return '';
868 }
869 }
870 else
871 {
872 if (($field == '' OR $value == '') OR ($field != $value))
873 {
874 return '';
875 }
876 }
Barry Mienydd671972010-10-04 16:33:58 +0200877
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 return ' checked="checked"';
879 }
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 /**
884 * Required
885 *
886 * @access public
887 * @param string
888 * @return bool
889 */
890 function required($str)
891 {
892 if ( ! is_array($str))
893 {
894 return (trim($str) == '') ? FALSE : TRUE;
895 }
896 else
897 {
898 return ( ! empty($str));
899 }
900 }
Barry Mienydd671972010-10-04 16:33:58 +0200901
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200903
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500905 * Performs a Regular Expression match test.
906 *
907 * @access public
908 * @param string
909 * @param regex
910 * @return bool
911 */
912 function regex_match($str, $regex)
913 {
914 if ( ! preg_match($regex, $str))
915 {
916 return FALSE;
917 }
918
919 return TRUE;
920 }
921
922 // --------------------------------------------------------------------
923
924 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 * Match one field to another
926 *
927 * @access public
928 * @param string
929 * @param field
930 * @return bool
931 */
932 function matches($str, $field)
933 {
934 if ( ! isset($_POST[$field]))
935 {
Barry Mienydd671972010-10-04 16:33:58 +0200936 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 }
Barry Mienydd671972010-10-04 16:33:58 +0200938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 $field = $_POST[$field];
940
941 return ($str !== $field) ? FALSE : TRUE;
942 }
Barry Mienydd671972010-10-04 16:33:58 +0200943
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200945
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 /**
947 * Minimum Length
948 *
949 * @access public
950 * @param string
951 * @param value
952 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200953 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 function min_length($str, $val)
955 {
956 if (preg_match("/[^0-9]/", $val))
957 {
958 return FALSE;
959 }
960
961 if (function_exists('mb_strlen'))
962 {
Barry Mienydd671972010-10-04 16:33:58 +0200963 return (mb_strlen($str) < $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 }
Barry Mienydd671972010-10-04 16:33:58 +0200965
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 return (strlen($str) < $val) ? FALSE : TRUE;
967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 /**
972 * Max Length
973 *
974 * @access public
975 * @param string
976 * @param value
977 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200978 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 function max_length($str, $val)
980 {
981 if (preg_match("/[^0-9]/", $val))
982 {
983 return FALSE;
984 }
985
986 if (function_exists('mb_strlen'))
987 {
Barry Mienydd671972010-10-04 16:33:58 +0200988 return (mb_strlen($str) > $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 }
Barry Mienydd671972010-10-04 16:33:58 +0200990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 return (strlen($str) > $val) ? FALSE : TRUE;
992 }
Barry Mienydd671972010-10-04 16:33:58 +0200993
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 /**
997 * Exact Length
998 *
999 * @access public
1000 * @param string
1001 * @param value
1002 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001003 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 function exact_length($str, $val)
1005 {
1006 if (preg_match("/[^0-9]/", $val))
1007 {
1008 return FALSE;
1009 }
1010
1011 if (function_exists('mb_strlen'))
1012 {
Barry Mienydd671972010-10-04 16:33:58 +02001013 return (mb_strlen($str) != $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 }
Barry Mienydd671972010-10-04 16:33:58 +02001015
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 return (strlen($str) != $val) ? FALSE : TRUE;
1017 }
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001020
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 /**
1022 * Valid Email
1023 *
1024 * @access public
1025 * @param string
1026 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001027 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 function valid_email($str)
1029 {
1030 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
1031 }
1032
1033 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001034
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 /**
1036 * Valid Emails
1037 *
1038 * @access public
1039 * @param string
1040 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001041 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 function valid_emails($str)
1043 {
1044 if (strpos($str, ',') === FALSE)
1045 {
1046 return $this->valid_email(trim($str));
1047 }
Barry Mienydd671972010-10-04 16:33:58 +02001048
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 foreach(explode(',', $str) as $email)
1050 {
1051 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
1052 {
1053 return FALSE;
1054 }
1055 }
Barry Mienydd671972010-10-04 16:33:58 +02001056
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 return TRUE;
1058 }
1059
1060 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001061
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 /**
1063 * Validate IP Address
1064 *
1065 * @access public
1066 * @param string
1067 * @return string
1068 */
1069 function valid_ip($ip)
1070 {
1071 return $this->CI->input->valid_ip($ip);
1072 }
1073
1074 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001075
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 /**
1077 * Alpha
1078 *
1079 * @access public
1080 * @param string
1081 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001082 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 function alpha($str)
1084 {
1085 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
1086 }
Barry Mienydd671972010-10-04 16:33:58 +02001087
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001089
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 /**
1091 * Alpha-numeric
1092 *
1093 * @access public
1094 * @param string
1095 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001096 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 function alpha_numeric($str)
1098 {
1099 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
1100 }
Barry Mienydd671972010-10-04 16:33:58 +02001101
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001103
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 /**
1105 * Alpha-numeric with underscores and dashes
1106 *
1107 * @access public
1108 * @param string
1109 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001110 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 function alpha_dash($str)
1112 {
1113 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
1114 }
Barry Mienydd671972010-10-04 16:33:58 +02001115
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 /**
1119 * Numeric
1120 *
1121 * @access public
1122 * @param string
1123 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001124 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 function numeric($str)
1126 {
1127 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
1128
1129 }
1130
1131 // --------------------------------------------------------------------
1132
Barry Mienydd671972010-10-04 16:33:58 +02001133 /**
1134 * Is Numeric
1135 *
1136 * @access public
1137 * @param string
1138 * @return bool
1139 */
1140 function is_numeric($str)
1141 {
1142 return ( ! is_numeric($str)) ? FALSE : TRUE;
1143 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001144
1145 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 /**
1148 * Integer
1149 *
1150 * @access public
1151 * @param string
1152 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001153 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 function integer($str)
1155 {
1156 return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
1157 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001158
1159 // --------------------------------------------------------------------
1160
Barry Mienydd671972010-10-04 16:33:58 +02001161 /**
1162 * Is a Natural number (0,1,2,3, etc.)
1163 *
1164 * @access public
1165 * @param string
1166 * @return bool
1167 */
1168 function is_natural($str)
1169 {
1170 return (bool)preg_match( '/^[0-9]+$/', $str);
1171 }
1172
1173 // --------------------------------------------------------------------
1174
1175 /**
1176 * Is a Natural number, but not a zero (1,2,3, etc.)
1177 *
1178 * @access public
1179 * @param string
1180 * @return bool
1181 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001183 {
1184 if ( ! preg_match( '/^[0-9]+$/', $str))
1185 {
1186 return FALSE;
1187 }
1188
1189 if ($str == 0)
1190 {
1191 return FALSE;
1192 }
1193
1194 return TRUE;
1195 }
1196
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001198
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 /**
1200 * Valid Base64
1201 *
1202 * Tests a string for characters outside of the Base64 alphabet
1203 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1204 *
1205 * @access public
1206 * @param string
1207 * @return bool
1208 */
1209 function valid_base64($str)
1210 {
1211 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1212 }
Barry Mienydd671972010-10-04 16:33:58 +02001213
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001215
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 /**
1217 * Prep data for form
1218 *
1219 * This function allows HTML to be safely shown in a form.
1220 * Special characters are converted.
1221 *
1222 * @access public
1223 * @param string
1224 * @return string
1225 */
1226 function prep_for_form($data = '')
1227 {
1228 if (is_array($data))
1229 {
1230 foreach ($data as $key => $val)
1231 {
1232 $data[$key] = $this->prep_for_form($val);
1233 }
Barry Mienydd671972010-10-04 16:33:58 +02001234
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 return $data;
1236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 if ($this->_safe_form_data == FALSE OR $data === '')
1239 {
1240 return $data;
1241 }
1242
1243 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1244 }
Barry Mienydd671972010-10-04 16:33:58 +02001245
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001247
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 /**
1249 * Prep URL
1250 *
1251 * @access public
1252 * @param string
1253 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001254 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 function prep_url($str = '')
1256 {
1257 if ($str == 'http://' OR $str == '')
1258 {
1259 return '';
1260 }
Barry Mienydd671972010-10-04 16:33:58 +02001261
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
1263 {
1264 $str = 'http://'.$str;
1265 }
Barry Mienydd671972010-10-04 16:33:58 +02001266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 return $str;
1268 }
Barry Mienydd671972010-10-04 16:33:58 +02001269
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001271
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 /**
1273 * Strip Image Tags
1274 *
1275 * @access public
1276 * @param string
1277 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001278 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 function strip_image_tags($str)
1280 {
1281 return $this->CI->input->strip_image_tags($str);
1282 }
Barry Mienydd671972010-10-04 16:33:58 +02001283
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 /**
1287 * XSS Clean
1288 *
1289 * @access public
1290 * @param string
1291 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001292 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 function xss_clean($str)
1294 {
Derek Jones30841672010-04-26 09:09:21 -05001295 if ( ! isset($this->CI->security))
Derek Jones5640a712010-04-23 11:22:40 -05001296 {
Derek Jones30841672010-04-26 09:09:21 -05001297 $this->CI->load->library('security');
Derek Jones5640a712010-04-23 11:22:40 -05001298 }
Derek Jones30841672010-04-26 09:09:21 -05001299
Derek Jones5640a712010-04-23 11:22:40 -05001300 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 }
Barry Mienydd671972010-10-04 16:33:58 +02001302
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001304
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 /**
1306 * Convert PHP tags to entities
1307 *
1308 * @access public
1309 * @param string
1310 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001311 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 function encode_php_tags($str)
1313 {
1314 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
1315 }
1316
1317}
1318// END Form Validation Class
1319
1320/* End of file Form_validation.php */
Rick Ellisec1b70f2008-08-26 19:21:27 +00001321/* Location: ./system/libraries/Form_validation.php */