blob: d62071b79ad03170d08c9f4308dfe68d84ddea6a [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 {
80 return;
81 }
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 }
101 return;
102 }
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 {
107 return;
108 }
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 );
149 }
150
151 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 /**
154 * Set Error Message
155 *
156 * Lets users set their own error messages on the fly. Note: The key
157 * name has to match the function name that it corresponds to.
158 *
159 * @access public
160 * @param string
161 * @param string
162 * @return string
163 */
164 function set_message($lang, $val = '')
165 {
166 if ( ! is_array($lang))
167 {
168 $lang = array($lang => $val);
169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 $this->_error_messages = array_merge($this->_error_messages, $lang);
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 /**
177 * Set The Error Delimiter
178 *
179 * Permits a prefix/suffix to be added to each error message
180 *
181 * @access public
182 * @param string
183 * @param string
184 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200185 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
187 {
188 $this->_error_prefix = $prefix;
189 $this->_error_suffix = $suffix;
190 }
191
192 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 /**
195 * Get Error Message
196 *
197 * Gets the error message associated with a particular field
198 *
199 * @access public
200 * @param string the field name
201 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200202 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200204 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
206 {
207 return '';
208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 if ($prefix == '')
211 {
212 $prefix = $this->_error_prefix;
213 }
214
215 if ($suffix == '')
216 {
217 $suffix = $this->_error_suffix;
218 }
219
220 return $prefix.$this->_field_data[$field]['error'].$suffix;
221 }
222
223 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 /**
226 * Error String
227 *
228 * Returns the error messages as a string, wrapped in the error delimiters
229 *
230 * @access public
231 * @param string
232 * @param string
233 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200234 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 function error_string($prefix = '', $suffix = '')
236 {
237 // No errrors, validation passes!
238 if (count($this->_error_array) === 0)
239 {
240 return '';
241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 if ($prefix == '')
244 {
245 $prefix = $this->_error_prefix;
246 }
247
248 if ($suffix == '')
249 {
250 $suffix = $this->_error_suffix;
251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 // Generate the error string
254 $str = '';
255 foreach ($this->_error_array as $val)
256 {
257 if ($val != '')
258 {
259 $str .= $prefix.$val.$suffix."\n";
260 }
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 return $str;
264 }
265
266 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 /**
269 * Run the Validator
270 *
271 * This function does all the work.
272 *
273 * @access public
274 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200275 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 function run($group = '')
277 {
278 // Do we even have any data to process? Mm?
279 if (count($_POST) == 0)
280 {
281 return FALSE;
282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 // Does the _field_data array containing the validation rules exist?
285 // If not, we look to see if they were assigned via a config file
286 if (count($this->_field_data) == 0)
287 {
288 // No validation rules? We're done...
289 if (count($this->_config_rules) == 0)
290 {
291 return FALSE;
292 }
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 // Is there a validation rule for the particular URI being accessed?
295 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 if ($uri != '' AND isset($this->_config_rules[$uri]))
298 {
299 $this->set_rules($this->_config_rules[$uri]);
300 }
301 else
302 {
303 $this->set_rules($this->_config_rules);
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // We're we able to set the rules correctly?
307 if (count($this->_field_data) == 0)
308 {
309 log_message('debug', "Unable to find validation rules");
310 return FALSE;
311 }
312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // Load the language file containing error messages
315 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200316
317 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // corresponding $_POST item and test for errors
319 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200320 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
322 // 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 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 if ($row['is_array'] == TRUE)
325 {
326 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
327 }
328 else
329 {
330 if (isset($_POST[$field]) AND $_POST[$field] != "")
331 {
332 $this->_field_data[$field]['postdata'] = $_POST[$field];
333 }
334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
336 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
338
339 // Did we end up with any errors?
340 $total_errors = count($this->_error_array);
341
342 if ($total_errors > 0)
343 {
344 $this->_safe_form_data = TRUE;
345 }
346
347 // Now we need to re-set the POST data with the new, processed data
348 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // No errors, validation passes!
351 if ($total_errors == 0)
352 {
353 return TRUE;
354 }
355
356 // Validation fails
357 return FALSE;
358 }
359
360 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 /**
363 * Traverse a multidimensional $_POST array index until the data is found
364 *
365 * @access private
366 * @param array
367 * @param array
368 * @param integer
369 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200370 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 function _reduce_array($array, $keys, $i = 0)
372 {
373 if (is_array($array))
374 {
375 if (isset($keys[$i]))
376 {
377 if (isset($array[$keys[$i]]))
378 {
379 $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1));
380 }
381 else
382 {
383 return NULL;
384 }
385 }
386 else
387 {
388 return $array;
389 }
390 }
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 return $array;
393 }
394
395 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 /**
398 * Re-populate the _POST array with our finalized and processed data
399 *
400 * @access private
401 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200402 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 function _reset_post_array()
404 {
405 foreach ($this->_field_data as $field => $row)
406 {
407 if ( ! is_null($row['postdata']))
408 {
409 if ($row['is_array'] == FALSE)
410 {
411 if (isset($_POST[$row['field']]))
412 {
413 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
414 }
415 }
416 else
417 {
Derek Jones63eeae32009-02-10 19:08:56 +0000418 // start with a reference
419 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Jones63eeae32009-02-10 19:08:56 +0000421 // before we assign values, make a reference to the right POST key
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 if (count($row['keys']) == 1)
423 {
Derek Jones63eeae32009-02-10 19:08:56 +0000424 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
426 else
427 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 foreach ($row['keys'] as $val)
429 {
Derek Jones63eeae32009-02-10 19:08:56 +0000430 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432 }
Derek Jones63eeae32009-02-10 19:08:56 +0000433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000435 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 $array = array();
437 foreach ($row['postdata'] as $k => $v)
438 {
439 $array[$k] = $this->prep_for_form($v);
440 }
Derek Jones63eeae32009-02-10 19:08:56 +0000441
442 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
444 else
Derek Jones63eeae32009-02-10 19:08:56 +0000445 {
446 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449 }
450 }
451 }
452
453 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 /**
456 * Executes the Validation routines
457 *
458 * @access private
459 * @param array
460 * @param array
461 * @param mixed
462 * @param integer
463 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200464 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 function _execute($row, $rules, $postdata = NULL, $cycles = 0)
466 {
467 // If the $_POST data is an array we will run a recursive call
468 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200469 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 foreach ($postdata as $key => $val)
471 {
472 $this->_execute($row, $rules, $val, $cycles);
473 $cycles++;
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 return;
477 }
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 // --------------------------------------------------------------------
480
481 // If the field is blank, but NOT required, no further tests are necessary
482 $callback = FALSE;
483 if ( ! in_array('required', $rules) AND is_null($postdata))
484 {
485 // Before we bail out, does the rule contain a callback?
486 if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
487 {
488 $callback = TRUE;
489 $rules = (array('1' => $match[1]));
490 }
491 else
492 {
493 return;
494 }
495 }
496
497 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 // Isset Test. Typically this rule will only apply to checkboxes.
500 if (is_null($postdata) AND $callback == FALSE)
501 {
502 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
503 {
504 // Set the message type
505 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 if ( ! isset($this->_error_messages[$type]))
508 {
509 if (FALSE === ($line = $this->CI->lang->line($type)))
510 {
511 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200512 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
514 else
515 {
516 $line = $this->_error_messages[$type];
517 }
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 // Build the error message
520 $message = sprintf($line, $this->_translate_fieldname($row['label']));
521
522 // Save the error message
523 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 if ( ! isset($this->_error_array[$row['field']]))
526 {
527 $this->_error_array[$row['field']] = $message;
528 }
529 }
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 return;
532 }
533
534 // --------------------------------------------------------------------
535
536 // Cycle through each rule and run it
537 foreach ($rules As $rule)
538 {
539 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 // We set the $postdata variable with the current data in our master array so that
542 // each cycle of the loop is dealing with the processed data from the last cycle
543 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
544 {
545 // We shouldn't need this safety, but just in case there isn't an array index
546 // associated with this cycle we'll bail out
547 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
548 {
549 continue;
550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
553 $_in_array = TRUE;
554 }
555 else
556 {
557 $postdata = $this->_field_data[$row['field']]['postdata'];
558 }
559
560 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200561
562 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 $callback = FALSE;
564 if (substr($rule, 0, 9) == 'callback_')
565 {
566 $rule = substr($rule, 9);
567 $callback = TRUE;
568 }
Barry Mienydd671972010-10-04 16:33:58 +0200569
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 // Strip the parameter (if exists) from the rule
571 // Rules can contain a parameter: max_length[5]
572 $param = FALSE;
573 if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
574 {
575 $rule = $match[1];
576 $param = $match[2];
577 }
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 // Call the function that corresponds to the rule
580 if ($callback === TRUE)
581 {
582 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200583 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 continue;
585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 // Run the function and grab the result
588 $result = $this->CI->$rule($postdata, $param);
589
590 // Re-assign the result to the master data array
591 if ($_in_array == TRUE)
592 {
593 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
594 }
595 else
596 {
597 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 // If the field isn't required and we just processed a callback we'll move on...
601 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
602 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000603 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 }
605 }
606 else
Barry Mienydd671972010-10-04 16:33:58 +0200607 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 if ( ! method_exists($this, $rule))
609 {
Barry Mienydd671972010-10-04 16:33:58 +0200610 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 // Users can use any native PHP function call that has one param.
612 if (function_exists($rule))
613 {
614 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 if ($_in_array == TRUE)
617 {
618 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
619 }
620 else
621 {
622 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
623 }
624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 continue;
627 }
628
629 $result = $this->$rule($postdata, $param);
630
631 if ($_in_array == TRUE)
632 {
633 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
634 }
635 else
636 {
637 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
638 }
639 }
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 // Did the rule test negatively? If so, grab the error.
642 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200643 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 if ( ! isset($this->_error_messages[$rule]))
645 {
646 if (FALSE === ($line = $this->CI->lang->line($rule)))
647 {
648 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200649 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 }
651 else
652 {
653 $line = $this->_error_messages[$rule];
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 // Is the parameter we are inserting into the error message the name
657 // of another field? If so we need to grab its "field label"
658 if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
659 {
Pascal Krietec1895832009-10-13 12:56:43 +0000660 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 }
Barry Mienydd671972010-10-04 16:33:58 +0200662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 // Build the error message
664 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
665
666 // Save the error message
667 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200668
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 if ( ! isset($this->_error_array[$row['field']]))
670 {
671 $this->_error_array[$row['field']] = $message;
672 }
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 return;
675 }
676 }
677 }
678
679 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 /**
682 * Translate a field name
683 *
684 * @access private
685 * @param string the field name
686 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200687 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 function _translate_fieldname($fieldname)
689 {
690 // Do we need to translate the field name?
691 // We look for the prefix lang: to determine this
692 if (substr($fieldname, 0, 5) == 'lang:')
693 {
694 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200695 $line = substr($fieldname, 5);
696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 // Were we able to translate the field name? If not we use $line
698 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
699 {
700 return $line;
701 }
702 }
703
704 return $fieldname;
705 }
706
707 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 /**
710 * Get the value from a form
711 *
712 * Permits you to repopulate a form field with the value it was submitted
713 * with, or, if that value doesn't exist, with the default
714 *
715 * @access public
716 * @param string the field name
717 * @param string
718 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200719 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 function set_value($field = '', $default = '')
721 {
722 if ( ! isset($this->_field_data[$field]))
723 {
724 return $default;
725 }
Barry Mienydd671972010-10-04 16:33:58 +0200726
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 return $this->_field_data[$field]['postdata'];
728 }
Barry Mienydd671972010-10-04 16:33:58 +0200729
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 /**
733 * Set Select
734 *
735 * Enables pull-down lists to be set to the value the user
736 * selected in the event of an error
737 *
738 * @access public
739 * @param string
740 * @param string
741 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200742 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200744 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
746 {
747 if ($default === TRUE AND count($this->_field_data) === 0)
748 {
749 return ' selected="selected"';
750 }
751 return '';
752 }
Barry Mienydd671972010-10-04 16:33:58 +0200753
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200755
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 if (is_array($field))
757 {
758 if ( ! in_array($value, $field))
759 {
760 return '';
761 }
762 }
763 else
764 {
765 if (($field == '' OR $value == '') OR ($field != $value))
766 {
767 return '';
768 }
769 }
Barry Mienydd671972010-10-04 16:33:58 +0200770
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 return ' selected="selected"';
772 }
Barry Mienydd671972010-10-04 16:33:58 +0200773
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 /**
777 * Set Radio
778 *
779 * Enables radio buttons to be set to the value the user
780 * selected in the event of an error
781 *
782 * @access public
783 * @param string
784 * @param string
785 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200786 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 function set_radio($field = '', $value = '', $default = FALSE)
788 {
789 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
790 {
791 if ($default === TRUE AND count($this->_field_data) === 0)
792 {
793 return ' checked="checked"';
794 }
795 return '';
796 }
Barry Mienydd671972010-10-04 16:33:58 +0200797
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200799
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 if (is_array($field))
801 {
802 if ( ! in_array($value, $field))
803 {
804 return '';
805 }
806 }
807 else
808 {
809 if (($field == '' OR $value == '') OR ($field != $value))
810 {
811 return '';
812 }
813 }
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 return ' checked="checked"';
816 }
Barry Mienydd671972010-10-04 16:33:58 +0200817
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 /**
821 * Set Checkbox
822 *
823 * Enables checkboxes to be set to the value the user
824 * selected in the event of an error
825 *
826 * @access public
827 * @param string
828 * @param string
829 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200830 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 function set_checkbox($field = '', $value = '', $default = FALSE)
832 {
833 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
834 {
835 if ($default === TRUE AND count($this->_field_data) === 0)
836 {
837 return ' checked="checked"';
838 }
839 return '';
840 }
Barry Mienydd671972010-10-04 16:33:58 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200843
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 if (is_array($field))
845 {
846 if ( ! in_array($value, $field))
847 {
848 return '';
849 }
850 }
851 else
852 {
853 if (($field == '' OR $value == '') OR ($field != $value))
854 {
855 return '';
856 }
857 }
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 return ' checked="checked"';
860 }
Barry Mienydd671972010-10-04 16:33:58 +0200861
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 /**
865 * Required
866 *
867 * @access public
868 * @param string
869 * @return bool
870 */
871 function required($str)
872 {
873 if ( ! is_array($str))
874 {
875 return (trim($str) == '') ? FALSE : TRUE;
876 }
877 else
878 {
879 return ( ! empty($str));
880 }
881 }
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 /**
886 * Match one field to another
887 *
888 * @access public
889 * @param string
890 * @param field
891 * @return bool
892 */
893 function matches($str, $field)
894 {
895 if ( ! isset($_POST[$field]))
896 {
Barry Mienydd671972010-10-04 16:33:58 +0200897 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 $field = $_POST[$field];
901
902 return ($str !== $field) ? FALSE : TRUE;
903 }
Barry Mienydd671972010-10-04 16:33:58 +0200904
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200906
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 /**
908 * Minimum Length
909 *
910 * @access public
911 * @param string
912 * @param value
913 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200914 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 function min_length($str, $val)
916 {
917 if (preg_match("/[^0-9]/", $val))
918 {
919 return FALSE;
920 }
921
922 if (function_exists('mb_strlen'))
923 {
Barry Mienydd671972010-10-04 16:33:58 +0200924 return (mb_strlen($str) < $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 }
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 return (strlen($str) < $val) ? FALSE : TRUE;
928 }
Barry Mienydd671972010-10-04 16:33:58 +0200929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200931
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 /**
933 * Max Length
934 *
935 * @access public
936 * @param string
937 * @param value
938 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200939 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 function max_length($str, $val)
941 {
942 if (preg_match("/[^0-9]/", $val))
943 {
944 return FALSE;
945 }
946
947 if (function_exists('mb_strlen'))
948 {
Barry Mienydd671972010-10-04 16:33:58 +0200949 return (mb_strlen($str) > $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 }
Barry Mienydd671972010-10-04 16:33:58 +0200951
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 return (strlen($str) > $val) ? FALSE : TRUE;
953 }
Barry Mienydd671972010-10-04 16:33:58 +0200954
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 /**
958 * Exact Length
959 *
960 * @access public
961 * @param string
962 * @param value
963 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200964 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 function exact_length($str, $val)
966 {
967 if (preg_match("/[^0-9]/", $val))
968 {
969 return FALSE;
970 }
971
972 if (function_exists('mb_strlen'))
973 {
Barry Mienydd671972010-10-04 16:33:58 +0200974 return (mb_strlen($str) != $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 }
Barry Mienydd671972010-10-04 16:33:58 +0200976
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 return (strlen($str) != $val) ? FALSE : TRUE;
978 }
Barry Mienydd671972010-10-04 16:33:58 +0200979
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200981
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 /**
983 * Valid Email
984 *
985 * @access public
986 * @param string
987 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200988 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 function valid_email($str)
990 {
991 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
992 }
993
994 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 /**
997 * Valid Emails
998 *
999 * @access public
1000 * @param string
1001 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001002 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 function valid_emails($str)
1004 {
1005 if (strpos($str, ',') === FALSE)
1006 {
1007 return $this->valid_email(trim($str));
1008 }
Barry Mienydd671972010-10-04 16:33:58 +02001009
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 foreach(explode(',', $str) as $email)
1011 {
1012 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
1013 {
1014 return FALSE;
1015 }
1016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 return TRUE;
1019 }
1020
1021 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001022
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 /**
1024 * Validate IP Address
1025 *
1026 * @access public
1027 * @param string
1028 * @return string
1029 */
1030 function valid_ip($ip)
1031 {
1032 return $this->CI->input->valid_ip($ip);
1033 }
1034
1035 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001036
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 /**
1038 * Alpha
1039 *
1040 * @access public
1041 * @param string
1042 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001043 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 function alpha($str)
1045 {
1046 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
1047 }
Barry Mienydd671972010-10-04 16:33:58 +02001048
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 /**
1052 * Alpha-numeric
1053 *
1054 * @access public
1055 * @param string
1056 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001057 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 function alpha_numeric($str)
1059 {
1060 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
1061 }
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001064
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 /**
1066 * Alpha-numeric with underscores and dashes
1067 *
1068 * @access public
1069 * @param string
1070 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001071 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 function alpha_dash($str)
1073 {
1074 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
1075 }
Barry Mienydd671972010-10-04 16:33:58 +02001076
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001078
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 /**
1080 * Numeric
1081 *
1082 * @access public
1083 * @param string
1084 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001085 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 function numeric($str)
1087 {
1088 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
1089
1090 }
1091
1092 // --------------------------------------------------------------------
1093
Barry Mienydd671972010-10-04 16:33:58 +02001094 /**
1095 * Is Numeric
1096 *
1097 * @access public
1098 * @param string
1099 * @return bool
1100 */
1101 function is_numeric($str)
1102 {
1103 return ( ! is_numeric($str)) ? FALSE : TRUE;
1104 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001105
1106 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 /**
1109 * Integer
1110 *
1111 * @access public
1112 * @param string
1113 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001114 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 function integer($str)
1116 {
1117 return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
1118 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001119
1120 // --------------------------------------------------------------------
1121
Barry Mienydd671972010-10-04 16:33:58 +02001122 /**
1123 * Is a Natural number (0,1,2,3, etc.)
1124 *
1125 * @access public
1126 * @param string
1127 * @return bool
1128 */
1129 function is_natural($str)
1130 {
1131 return (bool)preg_match( '/^[0-9]+$/', $str);
1132 }
1133
1134 // --------------------------------------------------------------------
1135
1136 /**
1137 * Is a Natural number, but not a zero (1,2,3, etc.)
1138 *
1139 * @access public
1140 * @param string
1141 * @return bool
1142 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001144 {
1145 if ( ! preg_match( '/^[0-9]+$/', $str))
1146 {
1147 return FALSE;
1148 }
1149
1150 if ($str == 0)
1151 {
1152 return FALSE;
1153 }
1154
1155 return TRUE;
1156 }
1157
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001159
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 /**
1161 * Valid Base64
1162 *
1163 * Tests a string for characters outside of the Base64 alphabet
1164 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1165 *
1166 * @access public
1167 * @param string
1168 * @return bool
1169 */
1170 function valid_base64($str)
1171 {
1172 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1173 }
Barry Mienydd671972010-10-04 16:33:58 +02001174
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001176
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 /**
1178 * Prep data for form
1179 *
1180 * This function allows HTML to be safely shown in a form.
1181 * Special characters are converted.
1182 *
1183 * @access public
1184 * @param string
1185 * @return string
1186 */
1187 function prep_for_form($data = '')
1188 {
1189 if (is_array($data))
1190 {
1191 foreach ($data as $key => $val)
1192 {
1193 $data[$key] = $this->prep_for_form($val);
1194 }
Barry Mienydd671972010-10-04 16:33:58 +02001195
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 return $data;
1197 }
Barry Mienydd671972010-10-04 16:33:58 +02001198
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 if ($this->_safe_form_data == FALSE OR $data === '')
1200 {
1201 return $data;
1202 }
1203
1204 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
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 URL
1211 *
1212 * @access public
1213 * @param string
1214 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001215 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 function prep_url($str = '')
1217 {
1218 if ($str == 'http://' OR $str == '')
1219 {
1220 return '';
1221 }
Barry Mienydd671972010-10-04 16:33:58 +02001222
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
1224 {
1225 $str = 'http://'.$str;
1226 }
Barry Mienydd671972010-10-04 16:33:58 +02001227
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 return $str;
1229 }
Barry Mienydd671972010-10-04 16:33:58 +02001230
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001232
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 /**
1234 * Strip Image Tags
1235 *
1236 * @access public
1237 * @param string
1238 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001239 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 function strip_image_tags($str)
1241 {
1242 return $this->CI->input->strip_image_tags($str);
1243 }
Barry Mienydd671972010-10-04 16:33:58 +02001244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001246
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 /**
1248 * XSS Clean
1249 *
1250 * @access public
1251 * @param string
1252 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001253 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 function xss_clean($str)
1255 {
Derek Jones30841672010-04-26 09:09:21 -05001256 if ( ! isset($this->CI->security))
Derek Jones5640a712010-04-23 11:22:40 -05001257 {
Derek Jones30841672010-04-26 09:09:21 -05001258 $this->CI->load->library('security');
Derek Jones5640a712010-04-23 11:22:40 -05001259 }
Derek Jones30841672010-04-26 09:09:21 -05001260
Derek Jones5640a712010-04-23 11:22:40 -05001261 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 }
Barry Mienydd671972010-10-04 16:33:58 +02001263
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001265
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 /**
1267 * Convert PHP tags to entities
1268 *
1269 * @access public
1270 * @param string
1271 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001272 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 function encode_php_tags($str)
1274 {
1275 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
1276 }
1277
1278}
1279// END Form Validation Class
1280
1281/* End of file Form_validation.php */
Rick Ellisec1b70f2008-08-26 19:21:27 +00001282/* Location: ./system/libraries/Form_validation.php */