blob: bf36890587df27fc6a543ea44cc2b0477af19f97 [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
342 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 }
344
345 // Did we end up with any errors?
346 $total_errors = count($this->_error_array);
347
348 if ($total_errors > 0)
349 {
350 $this->_safe_form_data = TRUE;
351 }
352
353 // Now we need to re-set the POST data with the new, processed data
354 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 // No errors, validation passes!
357 if ($total_errors == 0)
358 {
359 return TRUE;
360 }
361
362 // Validation fails
363 return FALSE;
364 }
365
366 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 /**
369 * Traverse a multidimensional $_POST array index until the data is found
370 *
371 * @access private
372 * @param array
373 * @param array
374 * @param integer
375 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200376 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 function _reduce_array($array, $keys, $i = 0)
378 {
379 if (is_array($array))
380 {
381 if (isset($keys[$i]))
382 {
383 if (isset($array[$keys[$i]]))
384 {
385 $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1));
386 }
387 else
388 {
389 return NULL;
390 }
391 }
392 else
393 {
394 return $array;
395 }
396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 return $array;
399 }
400
401 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 /**
404 * Re-populate the _POST array with our finalized and processed data
405 *
406 * @access private
407 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200408 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 function _reset_post_array()
410 {
411 foreach ($this->_field_data as $field => $row)
412 {
413 if ( ! is_null($row['postdata']))
414 {
415 if ($row['is_array'] == FALSE)
416 {
417 if (isset($_POST[$row['field']]))
418 {
419 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
420 }
421 }
422 else
423 {
Derek Jones63eeae32009-02-10 19:08:56 +0000424 // start with a reference
425 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Jones63eeae32009-02-10 19:08:56 +0000427 // before we assign values, make a reference to the right POST key
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 if (count($row['keys']) == 1)
429 {
Derek Jones63eeae32009-02-10 19:08:56 +0000430 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432 else
433 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 foreach ($row['keys'] as $val)
435 {
Derek Jones63eeae32009-02-10 19:08:56 +0000436 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438 }
Derek Jones63eeae32009-02-10 19:08:56 +0000439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000441 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 $array = array();
443 foreach ($row['postdata'] as $k => $v)
444 {
445 $array[$k] = $this->prep_for_form($v);
446 }
Derek Jones63eeae32009-02-10 19:08:56 +0000447
448 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450 else
Derek Jones63eeae32009-02-10 19:08:56 +0000451 {
452 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455 }
456 }
457 }
458
459 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 /**
462 * Executes the Validation routines
463 *
464 * @access private
465 * @param array
466 * @param array
467 * @param mixed
468 * @param integer
469 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200470 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 function _execute($row, $rules, $postdata = NULL, $cycles = 0)
472 {
473 // If the $_POST data is an array we will run a recursive call
474 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200475 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 foreach ($postdata as $key => $val)
477 {
478 $this->_execute($row, $rules, $val, $cycles);
479 $cycles++;
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 return;
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 // --------------------------------------------------------------------
486
487 // If the field is blank, but NOT required, no further tests are necessary
488 $callback = FALSE;
489 if ( ! in_array('required', $rules) AND is_null($postdata))
490 {
491 // Before we bail out, does the rule contain a callback?
492 if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
493 {
494 $callback = TRUE;
495 $rules = (array('1' => $match[1]));
496 }
497 else
498 {
499 return;
500 }
501 }
502
503 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // Isset Test. Typically this rule will only apply to checkboxes.
506 if (is_null($postdata) AND $callback == FALSE)
507 {
508 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
509 {
510 // Set the message type
511 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 if ( ! isset($this->_error_messages[$type]))
514 {
515 if (FALSE === ($line = $this->CI->lang->line($type)))
516 {
517 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200518 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
520 else
521 {
522 $line = $this->_error_messages[$type];
523 }
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 // Build the error message
526 $message = sprintf($line, $this->_translate_fieldname($row['label']));
527
528 // Save the error message
529 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 if ( ! isset($this->_error_array[$row['field']]))
532 {
533 $this->_error_array[$row['field']] = $message;
534 }
535 }
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 return;
538 }
539
540 // --------------------------------------------------------------------
541
542 // Cycle through each rule and run it
543 foreach ($rules As $rule)
544 {
545 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 // We set the $postdata variable with the current data in our master array so that
548 // each cycle of the loop is dealing with the processed data from the last cycle
549 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
550 {
551 // We shouldn't need this safety, but just in case there isn't an array index
552 // associated with this cycle we'll bail out
553 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
554 {
555 continue;
556 }
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
559 $_in_array = TRUE;
560 }
561 else
562 {
563 $postdata = $this->_field_data[$row['field']]['postdata'];
564 }
565
566 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200567
568 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 $callback = FALSE;
570 if (substr($rule, 0, 9) == 'callback_')
571 {
572 $rule = substr($rule, 9);
573 $callback = TRUE;
574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 // Strip the parameter (if exists) from the rule
577 // Rules can contain a parameter: max_length[5]
578 $param = FALSE;
579 if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
580 {
581 $rule = $match[1];
582 $param = $match[2];
583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 // Call the function that corresponds to the rule
586 if ($callback === TRUE)
587 {
588 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200589 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 continue;
591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 // Run the function and grab the result
594 $result = $this->CI->$rule($postdata, $param);
595
596 // Re-assign the result to the master data array
597 if ($_in_array == TRUE)
598 {
599 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
600 }
601 else
602 {
603 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
604 }
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 // If the field isn't required and we just processed a callback we'll move on...
607 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
608 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000609 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 }
611 }
612 else
Barry Mienydd671972010-10-04 16:33:58 +0200613 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 if ( ! method_exists($this, $rule))
615 {
Barry Mienydd671972010-10-04 16:33:58 +0200616 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 // Users can use any native PHP function call that has one param.
618 if (function_exists($rule))
619 {
620 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 if ($_in_array == TRUE)
623 {
624 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
625 }
626 else
627 {
628 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
629 }
630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 continue;
633 }
634
635 $result = $this->$rule($postdata, $param);
636
637 if ($_in_array == TRUE)
638 {
639 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
640 }
641 else
642 {
643 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
644 }
645 }
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 // Did the rule test negatively? If so, grab the error.
648 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200649 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 if ( ! isset($this->_error_messages[$rule]))
651 {
652 if (FALSE === ($line = $this->CI->lang->line($rule)))
653 {
654 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200655 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 }
657 else
658 {
659 $line = $this->_error_messages[$rule];
660 }
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 // Is the parameter we are inserting into the error message the name
663 // of another field? If so we need to grab its "field label"
664 if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
665 {
Pascal Krietec1895832009-10-13 12:56:43 +0000666 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 }
Barry Mienydd671972010-10-04 16:33:58 +0200668
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 // Build the error message
670 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
671
672 // Save the error message
673 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 if ( ! isset($this->_error_array[$row['field']]))
676 {
677 $this->_error_array[$row['field']] = $message;
678 }
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 return;
681 }
682 }
683 }
684
685 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 /**
688 * Translate a field name
689 *
690 * @access private
691 * @param string the field name
692 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200693 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 function _translate_fieldname($fieldname)
695 {
696 // Do we need to translate the field name?
697 // We look for the prefix lang: to determine this
698 if (substr($fieldname, 0, 5) == 'lang:')
699 {
700 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200701 $line = substr($fieldname, 5);
702
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 // Were we able to translate the field name? If not we use $line
704 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
705 {
706 return $line;
707 }
708 }
709
710 return $fieldname;
711 }
712
713 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200714
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 /**
716 * Get the value from a form
717 *
718 * Permits you to repopulate a form field with the value it was submitted
719 * with, or, if that value doesn't exist, with the default
720 *
721 * @access public
722 * @param string the field name
723 * @param string
724 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200725 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 function set_value($field = '', $default = '')
727 {
728 if ( ! isset($this->_field_data[$field]))
729 {
730 return $default;
731 }
Barry Mienydd671972010-10-04 16:33:58 +0200732
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 return $this->_field_data[$field]['postdata'];
734 }
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200737
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 /**
739 * Set Select
740 *
741 * Enables pull-down lists to be set to the value the user
742 * selected in the event of an error
743 *
744 * @access public
745 * @param string
746 * @param string
747 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200748 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200750 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
752 {
753 if ($default === TRUE AND count($this->_field_data) === 0)
754 {
755 return ' selected="selected"';
756 }
757 return '';
758 }
Barry Mienydd671972010-10-04 16:33:58 +0200759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 if (is_array($field))
763 {
764 if ( ! in_array($value, $field))
765 {
766 return '';
767 }
768 }
769 else
770 {
771 if (($field == '' OR $value == '') OR ($field != $value))
772 {
773 return '';
774 }
775 }
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 return ' selected="selected"';
778 }
Barry Mienydd671972010-10-04 16:33:58 +0200779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 /**
783 * Set Radio
784 *
785 * Enables radio buttons to be set to the value the user
786 * selected in the event of an error
787 *
788 * @access public
789 * @param string
790 * @param string
791 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200792 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 function set_radio($field = '', $value = '', $default = FALSE)
794 {
795 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
796 {
797 if ($default === TRUE AND count($this->_field_data) === 0)
798 {
799 return ' checked="checked"';
800 }
801 return '';
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 if (is_array($field))
807 {
808 if ( ! in_array($value, $field))
809 {
810 return '';
811 }
812 }
813 else
814 {
815 if (($field == '' OR $value == '') OR ($field != $value))
816 {
817 return '';
818 }
819 }
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 return ' checked="checked"';
822 }
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 /**
827 * Set Checkbox
828 *
829 * Enables checkboxes to be set to the value the user
830 * selected in the event of an error
831 *
832 * @access public
833 * @param string
834 * @param string
835 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200836 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 function set_checkbox($field = '', $value = '', $default = FALSE)
838 {
839 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
840 {
841 if ($default === TRUE AND count($this->_field_data) === 0)
842 {
843 return ' checked="checked"';
844 }
845 return '';
846 }
Barry Mienydd671972010-10-04 16:33:58 +0200847
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 if (is_array($field))
851 {
852 if ( ! in_array($value, $field))
853 {
854 return '';
855 }
856 }
857 else
858 {
859 if (($field == '' OR $value == '') OR ($field != $value))
860 {
861 return '';
862 }
863 }
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 return ' checked="checked"';
866 }
Barry Mienydd671972010-10-04 16:33:58 +0200867
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 /**
871 * Required
872 *
873 * @access public
874 * @param string
875 * @return bool
876 */
877 function required($str)
878 {
879 if ( ! is_array($str))
880 {
881 return (trim($str) == '') ? FALSE : TRUE;
882 }
883 else
884 {
885 return ( ! empty($str));
886 }
887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200890
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 /**
892 * Match one field to another
893 *
894 * @access public
895 * @param string
896 * @param field
897 * @return bool
898 */
899 function matches($str, $field)
900 {
901 if ( ! isset($_POST[$field]))
902 {
Barry Mienydd671972010-10-04 16:33:58 +0200903 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 }
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 $field = $_POST[$field];
907
908 return ($str !== $field) ? FALSE : TRUE;
909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 /**
914 * Minimum Length
915 *
916 * @access public
917 * @param string
918 * @param value
919 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200920 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 function min_length($str, $val)
922 {
923 if (preg_match("/[^0-9]/", $val))
924 {
925 return FALSE;
926 }
927
928 if (function_exists('mb_strlen'))
929 {
Barry Mienydd671972010-10-04 16:33:58 +0200930 return (mb_strlen($str) < $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 }
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 return (strlen($str) < $val) ? FALSE : TRUE;
934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200937
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 /**
939 * Max Length
940 *
941 * @access public
942 * @param string
943 * @param value
944 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200945 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 function max_length($str, $val)
947 {
948 if (preg_match("/[^0-9]/", $val))
949 {
950 return FALSE;
951 }
952
953 if (function_exists('mb_strlen'))
954 {
Barry Mienydd671972010-10-04 16:33:58 +0200955 return (mb_strlen($str) > $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
Barry Mienydd671972010-10-04 16:33:58 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 return (strlen($str) > $val) ? FALSE : TRUE;
959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 /**
964 * Exact Length
965 *
966 * @access public
967 * @param string
968 * @param value
969 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200970 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 function exact_length($str, $val)
972 {
973 if (preg_match("/[^0-9]/", $val))
974 {
975 return FALSE;
976 }
977
978 if (function_exists('mb_strlen'))
979 {
Barry Mienydd671972010-10-04 16:33:58 +0200980 return (mb_strlen($str) != $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 }
Barry Mienydd671972010-10-04 16:33:58 +0200982
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 return (strlen($str) != $val) ? FALSE : TRUE;
984 }
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 /**
989 * Valid Email
990 *
991 * @access public
992 * @param string
993 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200994 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 function valid_email($str)
996 {
997 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
998 }
999
1000 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 /**
1003 * Valid Emails
1004 *
1005 * @access public
1006 * @param string
1007 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001008 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 function valid_emails($str)
1010 {
1011 if (strpos($str, ',') === FALSE)
1012 {
1013 return $this->valid_email(trim($str));
1014 }
Barry Mienydd671972010-10-04 16:33:58 +02001015
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 foreach(explode(',', $str) as $email)
1017 {
1018 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
1019 {
1020 return FALSE;
1021 }
1022 }
Barry Mienydd671972010-10-04 16:33:58 +02001023
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 return TRUE;
1025 }
1026
1027 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001028
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 /**
1030 * Validate IP Address
1031 *
1032 * @access public
1033 * @param string
1034 * @return string
1035 */
1036 function valid_ip($ip)
1037 {
1038 return $this->CI->input->valid_ip($ip);
1039 }
1040
1041 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001042
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 /**
1044 * Alpha
1045 *
1046 * @access public
1047 * @param string
1048 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001049 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 function alpha($str)
1051 {
1052 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
1053 }
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001056
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 /**
1058 * Alpha-numeric
1059 *
1060 * @access public
1061 * @param string
1062 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001063 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 function alpha_numeric($str)
1065 {
1066 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
1067 }
Barry Mienydd671972010-10-04 16:33:58 +02001068
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001070
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 /**
1072 * Alpha-numeric with underscores and dashes
1073 *
1074 * @access public
1075 * @param string
1076 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001077 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 function alpha_dash($str)
1079 {
1080 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
1081 }
Barry Mienydd671972010-10-04 16:33:58 +02001082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001084
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 /**
1086 * Numeric
1087 *
1088 * @access public
1089 * @param string
1090 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001091 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 function numeric($str)
1093 {
1094 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
1095
1096 }
1097
1098 // --------------------------------------------------------------------
1099
Barry Mienydd671972010-10-04 16:33:58 +02001100 /**
1101 * Is Numeric
1102 *
1103 * @access public
1104 * @param string
1105 * @return bool
1106 */
1107 function is_numeric($str)
1108 {
1109 return ( ! is_numeric($str)) ? FALSE : TRUE;
1110 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001111
1112 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001113
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 /**
1115 * Integer
1116 *
1117 * @access public
1118 * @param string
1119 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001120 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 function integer($str)
1122 {
1123 return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
1124 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001125
1126 // --------------------------------------------------------------------
1127
Barry Mienydd671972010-10-04 16:33:58 +02001128 /**
1129 * Is a Natural number (0,1,2,3, etc.)
1130 *
1131 * @access public
1132 * @param string
1133 * @return bool
1134 */
1135 function is_natural($str)
1136 {
1137 return (bool)preg_match( '/^[0-9]+$/', $str);
1138 }
1139
1140 // --------------------------------------------------------------------
1141
1142 /**
1143 * Is a Natural number, but not a zero (1,2,3, etc.)
1144 *
1145 * @access public
1146 * @param string
1147 * @return bool
1148 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001150 {
1151 if ( ! preg_match( '/^[0-9]+$/', $str))
1152 {
1153 return FALSE;
1154 }
1155
1156 if ($str == 0)
1157 {
1158 return FALSE;
1159 }
1160
1161 return TRUE;
1162 }
1163
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001165
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 /**
1167 * Valid Base64
1168 *
1169 * Tests a string for characters outside of the Base64 alphabet
1170 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1171 *
1172 * @access public
1173 * @param string
1174 * @return bool
1175 */
1176 function valid_base64($str)
1177 {
1178 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1179 }
Barry Mienydd671972010-10-04 16:33:58 +02001180
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001182
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 /**
1184 * Prep data for form
1185 *
1186 * This function allows HTML to be safely shown in a form.
1187 * Special characters are converted.
1188 *
1189 * @access public
1190 * @param string
1191 * @return string
1192 */
1193 function prep_for_form($data = '')
1194 {
1195 if (is_array($data))
1196 {
1197 foreach ($data as $key => $val)
1198 {
1199 $data[$key] = $this->prep_for_form($val);
1200 }
Barry Mienydd671972010-10-04 16:33:58 +02001201
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 return $data;
1203 }
Barry Mienydd671972010-10-04 16:33:58 +02001204
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 if ($this->_safe_form_data == FALSE OR $data === '')
1206 {
1207 return $data;
1208 }
1209
1210 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1211 }
Barry Mienydd671972010-10-04 16:33:58 +02001212
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001214
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 /**
1216 * Prep URL
1217 *
1218 * @access public
1219 * @param string
1220 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001221 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 function prep_url($str = '')
1223 {
1224 if ($str == 'http://' OR $str == '')
1225 {
1226 return '';
1227 }
Barry Mienydd671972010-10-04 16:33:58 +02001228
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
1230 {
1231 $str = 'http://'.$str;
1232 }
Barry Mienydd671972010-10-04 16:33:58 +02001233
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 return $str;
1235 }
Barry Mienydd671972010-10-04 16:33:58 +02001236
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001238
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 /**
1240 * Strip Image Tags
1241 *
1242 * @access public
1243 * @param string
1244 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001245 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 function strip_image_tags($str)
1247 {
1248 return $this->CI->input->strip_image_tags($str);
1249 }
Barry Mienydd671972010-10-04 16:33:58 +02001250
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001252
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 /**
1254 * XSS Clean
1255 *
1256 * @access public
1257 * @param string
1258 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001259 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 function xss_clean($str)
1261 {
Derek Jones30841672010-04-26 09:09:21 -05001262 if ( ! isset($this->CI->security))
Derek Jones5640a712010-04-23 11:22:40 -05001263 {
Derek Jones30841672010-04-26 09:09:21 -05001264 $this->CI->load->library('security');
Derek Jones5640a712010-04-23 11:22:40 -05001265 }
Derek Jones30841672010-04-26 09:09:21 -05001266
Derek Jones5640a712010-04-23 11:22:40 -05001267 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 }
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 * Convert PHP tags to entities
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 encode_php_tags($str)
1280 {
1281 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
1282 }
1283
1284}
1285// END Form Validation Class
1286
1287/* End of file Form_validation.php */
Rick Ellisec1b70f2008-08-26 19:21:27 +00001288/* Location: ./system/libraries/Form_validation.php */