blob: 38f50fa419287a840379456638fa253bf201b722 [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
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 return $this->_field_data[$field]['postdata'];
740 }
Barry Mienydd671972010-10-04 16:33:58 +0200741
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200743
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 /**
745 * Set Select
746 *
747 * Enables pull-down lists to be set to the value the user
748 * selected in the event of an error
749 *
750 * @access public
751 * @param string
752 * @param string
753 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200754 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200756 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
758 {
759 if ($default === TRUE AND count($this->_field_data) === 0)
760 {
761 return ' selected="selected"';
762 }
763 return '';
764 }
Barry Mienydd671972010-10-04 16:33:58 +0200765
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 if (is_array($field))
769 {
770 if ( ! in_array($value, $field))
771 {
772 return '';
773 }
774 }
775 else
776 {
777 if (($field == '' OR $value == '') OR ($field != $value))
778 {
779 return '';
780 }
781 }
Barry Mienydd671972010-10-04 16:33:58 +0200782
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 return ' selected="selected"';
784 }
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200787
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 /**
789 * Set Radio
790 *
791 * Enables radio buttons to be set to the value the user
792 * selected in the event of an error
793 *
794 * @access public
795 * @param string
796 * @param string
797 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200798 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 function set_radio($field = '', $value = '', $default = FALSE)
800 {
801 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
802 {
803 if ($default === TRUE AND count($this->_field_data) === 0)
804 {
805 return ' checked="checked"';
806 }
807 return '';
808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200811
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 if (is_array($field))
813 {
814 if ( ! in_array($value, $field))
815 {
816 return '';
817 }
818 }
819 else
820 {
821 if (($field == '' OR $value == '') OR ($field != $value))
822 {
823 return '';
824 }
825 }
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 return ' checked="checked"';
828 }
Barry Mienydd671972010-10-04 16:33:58 +0200829
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 /**
833 * Set Checkbox
834 *
835 * Enables checkboxes to be set to the value the user
836 * selected in the event of an error
837 *
838 * @access public
839 * @param string
840 * @param string
841 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200842 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 function set_checkbox($field = '', $value = '', $default = FALSE)
844 {
845 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
846 {
847 if ($default === TRUE AND count($this->_field_data) === 0)
848 {
849 return ' checked="checked"';
850 }
851 return '';
852 }
Barry Mienydd671972010-10-04 16:33:58 +0200853
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 if (is_array($field))
857 {
858 if ( ! in_array($value, $field))
859 {
860 return '';
861 }
862 }
863 else
864 {
865 if (($field == '' OR $value == '') OR ($field != $value))
866 {
867 return '';
868 }
869 }
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 return ' checked="checked"';
872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 /**
877 * Required
878 *
879 * @access public
880 * @param string
881 * @return bool
882 */
883 function required($str)
884 {
885 if ( ! is_array($str))
886 {
887 return (trim($str) == '') ? FALSE : TRUE;
888 }
889 else
890 {
891 return ( ! empty($str));
892 }
893 }
Barry Mienydd671972010-10-04 16:33:58 +0200894
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200896
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500898 * Performs a Regular Expression match test.
899 *
900 * @access public
901 * @param string
902 * @param regex
903 * @return bool
904 */
905 function regex_match($str, $regex)
906 {
907 if ( ! preg_match($regex, $str))
908 {
909 return FALSE;
910 }
911
912 return TRUE;
913 }
914
915 // --------------------------------------------------------------------
916
917 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 * Match one field to another
919 *
920 * @access public
921 * @param string
922 * @param field
923 * @return bool
924 */
925 function matches($str, $field)
926 {
927 if ( ! isset($_POST[$field]))
928 {
Barry Mienydd671972010-10-04 16:33:58 +0200929 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 }
Barry Mienydd671972010-10-04 16:33:58 +0200931
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 $field = $_POST[$field];
933
934 return ($str !== $field) ? FALSE : TRUE;
935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 /**
940 * Minimum Length
941 *
942 * @access public
943 * @param string
944 * @param value
945 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200946 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 function min_length($str, $val)
948 {
949 if (preg_match("/[^0-9]/", $val))
950 {
951 return FALSE;
952 }
953
954 if (function_exists('mb_strlen'))
955 {
Barry Mienydd671972010-10-04 16:33:58 +0200956 return (mb_strlen($str) < $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 return (strlen($str) < $val) ? FALSE : TRUE;
960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200963
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 /**
965 * Max Length
966 *
967 * @access public
968 * @param string
969 * @param value
970 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200971 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 function max_length($str, $val)
973 {
974 if (preg_match("/[^0-9]/", $val))
975 {
976 return FALSE;
977 }
978
979 if (function_exists('mb_strlen'))
980 {
Barry Mienydd671972010-10-04 16:33:58 +0200981 return (mb_strlen($str) > $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
Barry Mienydd671972010-10-04 16:33:58 +0200983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 return (strlen($str) > $val) ? FALSE : TRUE;
985 }
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 /**
990 * Exact Length
991 *
992 * @access public
993 * @param string
994 * @param value
995 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200996 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 function exact_length($str, $val)
998 {
999 if (preg_match("/[^0-9]/", $val))
1000 {
1001 return FALSE;
1002 }
1003
1004 if (function_exists('mb_strlen'))
1005 {
Barry Mienydd671972010-10-04 16:33:58 +02001006 return (mb_strlen($str) != $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 return (strlen($str) != $val) ? FALSE : TRUE;
1010 }
Barry Mienydd671972010-10-04 16:33:58 +02001011
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001013
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 /**
1015 * Valid Email
1016 *
1017 * @access public
1018 * @param string
1019 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001020 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 function valid_email($str)
1022 {
1023 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
1024 }
1025
1026 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001027
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 /**
1029 * Valid Emails
1030 *
1031 * @access public
1032 * @param string
1033 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001034 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 function valid_emails($str)
1036 {
1037 if (strpos($str, ',') === FALSE)
1038 {
1039 return $this->valid_email(trim($str));
1040 }
Barry Mienydd671972010-10-04 16:33:58 +02001041
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 foreach(explode(',', $str) as $email)
1043 {
1044 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
1045 {
1046 return FALSE;
1047 }
1048 }
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 return TRUE;
1051 }
1052
1053 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 /**
1056 * Validate IP Address
1057 *
1058 * @access public
1059 * @param string
1060 * @return string
1061 */
1062 function valid_ip($ip)
1063 {
1064 return $this->CI->input->valid_ip($ip);
1065 }
1066
1067 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001068
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 /**
1070 * Alpha
1071 *
1072 * @access public
1073 * @param string
1074 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001075 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 function alpha($str)
1077 {
1078 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
1079 }
Barry Mienydd671972010-10-04 16:33:58 +02001080
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 /**
1084 * Alpha-numeric
1085 *
1086 * @access public
1087 * @param string
1088 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001089 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 function alpha_numeric($str)
1091 {
1092 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
1093 }
Barry Mienydd671972010-10-04 16:33:58 +02001094
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001096
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 /**
1098 * Alpha-numeric with underscores and dashes
1099 *
1100 * @access public
1101 * @param string
1102 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001103 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 function alpha_dash($str)
1105 {
1106 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
1107 }
Barry Mienydd671972010-10-04 16:33:58 +02001108
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 /**
1112 * Numeric
1113 *
1114 * @access public
1115 * @param string
1116 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001117 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 function numeric($str)
1119 {
1120 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
1121
1122 }
1123
1124 // --------------------------------------------------------------------
1125
Barry Mienydd671972010-10-04 16:33:58 +02001126 /**
1127 * Is Numeric
1128 *
1129 * @access public
1130 * @param string
1131 * @return bool
1132 */
1133 function is_numeric($str)
1134 {
1135 return ( ! is_numeric($str)) ? FALSE : TRUE;
1136 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001137
1138 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001139
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 /**
1141 * Integer
1142 *
1143 * @access public
1144 * @param string
1145 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001146 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 function integer($str)
1148 {
1149 return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
1150 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001151
1152 // --------------------------------------------------------------------
1153
Barry Mienydd671972010-10-04 16:33:58 +02001154 /**
1155 * Is a Natural number (0,1,2,3, etc.)
1156 *
1157 * @access public
1158 * @param string
1159 * @return bool
1160 */
1161 function is_natural($str)
1162 {
1163 return (bool)preg_match( '/^[0-9]+$/', $str);
1164 }
1165
1166 // --------------------------------------------------------------------
1167
1168 /**
1169 * Is a Natural number, but not a zero (1,2,3, etc.)
1170 *
1171 * @access public
1172 * @param string
1173 * @return bool
1174 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001176 {
1177 if ( ! preg_match( '/^[0-9]+$/', $str))
1178 {
1179 return FALSE;
1180 }
1181
1182 if ($str == 0)
1183 {
1184 return FALSE;
1185 }
1186
1187 return TRUE;
1188 }
1189
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001191
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 /**
1193 * Valid Base64
1194 *
1195 * Tests a string for characters outside of the Base64 alphabet
1196 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1197 *
1198 * @access public
1199 * @param string
1200 * @return bool
1201 */
1202 function valid_base64($str)
1203 {
1204 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1205 }
Barry Mienydd671972010-10-04 16:33:58 +02001206
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001208
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 /**
1210 * Prep data for form
1211 *
1212 * This function allows HTML to be safely shown in a form.
1213 * Special characters are converted.
1214 *
1215 * @access public
1216 * @param string
1217 * @return string
1218 */
1219 function prep_for_form($data = '')
1220 {
1221 if (is_array($data))
1222 {
1223 foreach ($data as $key => $val)
1224 {
1225 $data[$key] = $this->prep_for_form($val);
1226 }
Barry Mienydd671972010-10-04 16:33:58 +02001227
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 return $data;
1229 }
Barry Mienydd671972010-10-04 16:33:58 +02001230
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 if ($this->_safe_form_data == FALSE OR $data === '')
1232 {
1233 return $data;
1234 }
1235
1236 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1237 }
Barry Mienydd671972010-10-04 16:33:58 +02001238
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001240
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 /**
1242 * Prep URL
1243 *
1244 * @access public
1245 * @param string
1246 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001247 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 function prep_url($str = '')
1249 {
1250 if ($str == 'http://' OR $str == '')
1251 {
1252 return '';
1253 }
Barry Mienydd671972010-10-04 16:33:58 +02001254
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
1256 {
1257 $str = 'http://'.$str;
1258 }
Barry Mienydd671972010-10-04 16:33:58 +02001259
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 return $str;
1261 }
Barry Mienydd671972010-10-04 16:33:58 +02001262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001264
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 /**
1266 * Strip Image Tags
1267 *
1268 * @access public
1269 * @param string
1270 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001271 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 function strip_image_tags($str)
1273 {
1274 return $this->CI->input->strip_image_tags($str);
1275 }
Barry Mienydd671972010-10-04 16:33:58 +02001276
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001278
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 /**
1280 * XSS Clean
1281 *
1282 * @access public
1283 * @param string
1284 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001285 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 function xss_clean($str)
1287 {
Derek Jones30841672010-04-26 09:09:21 -05001288 if ( ! isset($this->CI->security))
Derek Jones5640a712010-04-23 11:22:40 -05001289 {
Derek Jones30841672010-04-26 09:09:21 -05001290 $this->CI->load->library('security');
Derek Jones5640a712010-04-23 11:22:40 -05001291 }
Derek Jones30841672010-04-26 09:09:21 -05001292
Derek Jones5640a712010-04-23 11:22:40 -05001293 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 }
Barry Mienydd671972010-10-04 16:33:58 +02001295
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001297
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 /**
1299 * Convert PHP tags to entities
1300 *
1301 * @access public
1302 * @param string
1303 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001304 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 function encode_php_tags($str)
1306 {
1307 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
1308 }
1309
1310}
1311// END Form Validation Class
1312
1313/* End of file Form_validation.php */
Rick Ellisec1b70f2008-08-26 19:21:27 +00001314/* Location: ./system/libraries/Form_validation.php */