blob: 566655b1216647298e83ad9f42b12205e25ff006 [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 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
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
42 *
Barry Mienydd671972010-10-04 16:33:58 +020043 */
Derek Allard2067d1a2008-11-13 22:59:24 +000044 function CI_Form_validation($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +020045 {
Derek Allard2067d1a2008-11-13 22:59:24 +000046 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020047
Derek Allard2067d1a2008-11-13 22:59:24 +000048 // Validation rules can be stored in a config file.
49 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 // Automatically load the form helper
52 $this->CI->load->helper('form');
53
54 // Set the character encoding in MB.
55 if (function_exists('mb_internal_encoding'))
56 {
57 mb_internal_encoding($this->CI->config->item('charset'));
58 }
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 log_message('debug', "Form Validation Class Initialized");
61 }
Barry Mienydd671972010-10-04 16:33:58 +020062
Derek Allard2067d1a2008-11-13 22:59:24 +000063 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 /**
66 * Set Rules
67 *
68 * This function takes an array of field names and validation
69 * rules as input, validates the info, and stores it
70 *
71 * @access public
72 * @param mixed
73 * @param string
74 * @return void
75 */
76 function set_rules($field, $label = '', $rules = '')
77 {
78 // No reason to set rules if we have no POST data
79 if (count($_POST) == 0)
80 {
81 return;
82 }
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Allard2067d1a2008-11-13 22:59:24 +000084 // If an array was passed via the first parameter instead of indidual string
85 // values we cycle through it and recursively call this function.
86 if (is_array($field))
87 {
88 foreach ($field as $row)
89 {
90 // Houston, we have a problem...
91 if ( ! isset($row['field']) OR ! isset($row['rules']))
92 {
93 continue;
94 }
95
96 // If the field label wasn't passed we use the field name
97 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
98
99 // Here we go!
100 $this->set_rules($row['field'], $label, $row['rules']);
101 }
102 return;
103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 // No fields? Nothing to do...
106 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
107 {
108 return;
109 }
110
111 // If the field label wasn't passed we use the field name
112 $label = ($label == '') ? $field : $label;
113
114 // Is the field name an array? We test for the existence of a bracket "[" in
115 // the field name to determine this. If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200116 // into its components so that we can fetch the corresponding POST data later
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 if (strpos($field, '[') !== FALSE AND preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200118 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 // Note: Due to a bug in current() that affects some versions
120 // of PHP we can not pass function call directly into it
121 $x = explode('[', $field);
122 $indexes[] = current($x);
123
124 for ($i = 0; $i < count($matches['0']); $i++)
125 {
126 if ($matches['1'][$i] != '')
127 {
128 $indexes[] = $matches['1'][$i];
129 }
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 $is_array = TRUE;
133 }
134 else
135 {
Barry Mienydd671972010-10-04 16:33:58 +0200136 $indexes = array();
137 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
140 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 $this->_field_data[$field] = array(
Barry Mienydd671972010-10-04 16:33:58 +0200142 'field' => $field,
143 'label' => $label,
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 'rules' => $rules,
145 'is_array' => $is_array,
146 'keys' => $indexes,
147 'postdata' => NULL,
148 'error' => ''
149 );
150 }
151
152 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 /**
155 * Set Error Message
156 *
157 * Lets users set their own error messages on the fly. Note: The key
158 * name has to match the function name that it corresponds to.
159 *
160 * @access public
161 * @param string
162 * @param string
163 * @return string
164 */
165 function set_message($lang, $val = '')
166 {
167 if ( ! is_array($lang))
168 {
169 $lang = array($lang => $val);
170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 $this->_error_messages = array_merge($this->_error_messages, $lang);
173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 /**
178 * Set The Error Delimiter
179 *
180 * Permits a prefix/suffix to be added to each error message
181 *
182 * @access public
183 * @param string
184 * @param string
185 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200186 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
188 {
189 $this->_error_prefix = $prefix;
190 $this->_error_suffix = $suffix;
191 }
192
193 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 /**
196 * Get Error Message
197 *
198 * Gets the error message associated with a particular field
199 *
200 * @access public
201 * @param string the field name
202 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200203 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200205 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
207 {
208 return '';
209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 if ($prefix == '')
212 {
213 $prefix = $this->_error_prefix;
214 }
215
216 if ($suffix == '')
217 {
218 $suffix = $this->_error_suffix;
219 }
220
221 return $prefix.$this->_field_data[$field]['error'].$suffix;
222 }
223
224 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 /**
227 * Error String
228 *
229 * Returns the error messages as a string, wrapped in the error delimiters
230 *
231 * @access public
232 * @param string
233 * @param string
234 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200235 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 function error_string($prefix = '', $suffix = '')
237 {
238 // No errrors, validation passes!
239 if (count($this->_error_array) === 0)
240 {
241 return '';
242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 if ($prefix == '')
245 {
246 $prefix = $this->_error_prefix;
247 }
248
249 if ($suffix == '')
250 {
251 $suffix = $this->_error_suffix;
252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // Generate the error string
255 $str = '';
256 foreach ($this->_error_array as $val)
257 {
258 if ($val != '')
259 {
260 $str .= $prefix.$val.$suffix."\n";
261 }
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 return $str;
265 }
266
267 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 /**
270 * Run the Validator
271 *
272 * This function does all the work.
273 *
274 * @access public
275 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200276 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 function run($group = '')
278 {
279 // Do we even have any data to process? Mm?
280 if (count($_POST) == 0)
281 {
282 return FALSE;
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // Does the _field_data array containing the validation rules exist?
286 // If not, we look to see if they were assigned via a config file
287 if (count($this->_field_data) == 0)
288 {
289 // No validation rules? We're done...
290 if (count($this->_config_rules) == 0)
291 {
292 return FALSE;
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 // Is there a validation rule for the particular URI being accessed?
296 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 if ($uri != '' AND isset($this->_config_rules[$uri]))
299 {
300 $this->set_rules($this->_config_rules[$uri]);
301 }
302 else
303 {
304 $this->set_rules($this->_config_rules);
305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 // We're we able to set the rules correctly?
308 if (count($this->_field_data) == 0)
309 {
310 log_message('debug', "Unable to find validation rules");
311 return FALSE;
312 }
313 }
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 // Load the language file containing error messages
316 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200317
318 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // corresponding $_POST item and test for errors
320 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200321 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
323 // 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 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 if ($row['is_array'] == TRUE)
326 {
327 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
328 }
329 else
330 {
331 if (isset($_POST[$field]) AND $_POST[$field] != "")
332 {
333 $this->_field_data[$field]['postdata'] = $_POST[$field];
334 }
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
337 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339
340 // Did we end up with any errors?
341 $total_errors = count($this->_error_array);
342
343 if ($total_errors > 0)
344 {
345 $this->_safe_form_data = TRUE;
346 }
347
348 // Now we need to re-set the POST data with the new, processed data
349 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // No errors, validation passes!
352 if ($total_errors == 0)
353 {
354 return TRUE;
355 }
356
357 // Validation fails
358 return FALSE;
359 }
360
361 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 /**
364 * Traverse a multidimensional $_POST array index until the data is found
365 *
366 * @access private
367 * @param array
368 * @param array
369 * @param integer
370 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200371 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 function _reduce_array($array, $keys, $i = 0)
373 {
374 if (is_array($array))
375 {
376 if (isset($keys[$i]))
377 {
378 if (isset($array[$keys[$i]]))
379 {
380 $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1));
381 }
382 else
383 {
384 return NULL;
385 }
386 }
387 else
388 {
389 return $array;
390 }
391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 return $array;
394 }
395
396 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 /**
399 * Re-populate the _POST array with our finalized and processed data
400 *
401 * @access private
402 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200403 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 function _reset_post_array()
405 {
406 foreach ($this->_field_data as $field => $row)
407 {
408 if ( ! is_null($row['postdata']))
409 {
410 if ($row['is_array'] == FALSE)
411 {
412 if (isset($_POST[$row['field']]))
413 {
414 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
415 }
416 }
417 else
418 {
Derek Jones63eeae32009-02-10 19:08:56 +0000419 // start with a reference
420 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Jones63eeae32009-02-10 19:08:56 +0000422 // before we assign values, make a reference to the right POST key
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 if (count($row['keys']) == 1)
424 {
Derek Jones63eeae32009-02-10 19:08:56 +0000425 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
427 else
428 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 foreach ($row['keys'] as $val)
430 {
Derek Jones63eeae32009-02-10 19:08:56 +0000431 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 }
433 }
Derek Jones63eeae32009-02-10 19:08:56 +0000434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000436 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 $array = array();
438 foreach ($row['postdata'] as $k => $v)
439 {
440 $array[$k] = $this->prep_for_form($v);
441 }
Derek Jones63eeae32009-02-10 19:08:56 +0000442
443 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
445 else
Derek Jones63eeae32009-02-10 19:08:56 +0000446 {
447 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450 }
451 }
452 }
453
454 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 /**
457 * Executes the Validation routines
458 *
459 * @access private
460 * @param array
461 * @param array
462 * @param mixed
463 * @param integer
464 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200465 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 function _execute($row, $rules, $postdata = NULL, $cycles = 0)
467 {
468 // If the $_POST data is an array we will run a recursive call
469 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200470 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 foreach ($postdata as $key => $val)
472 {
473 $this->_execute($row, $rules, $val, $cycles);
474 $cycles++;
475 }
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 return;
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 // --------------------------------------------------------------------
481
482 // If the field is blank, but NOT required, no further tests are necessary
483 $callback = FALSE;
484 if ( ! in_array('required', $rules) AND is_null($postdata))
485 {
486 // Before we bail out, does the rule contain a callback?
487 if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
488 {
489 $callback = TRUE;
490 $rules = (array('1' => $match[1]));
491 }
492 else
493 {
494 return;
495 }
496 }
497
498 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 // Isset Test. Typically this rule will only apply to checkboxes.
501 if (is_null($postdata) AND $callback == FALSE)
502 {
503 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
504 {
505 // Set the message type
506 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 if ( ! isset($this->_error_messages[$type]))
509 {
510 if (FALSE === ($line = $this->CI->lang->line($type)))
511 {
512 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200513 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 }
515 else
516 {
517 $line = $this->_error_messages[$type];
518 }
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // Build the error message
521 $message = sprintf($line, $this->_translate_fieldname($row['label']));
522
523 // Save the error message
524 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 if ( ! isset($this->_error_array[$row['field']]))
527 {
528 $this->_error_array[$row['field']] = $message;
529 }
530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 return;
533 }
534
535 // --------------------------------------------------------------------
536
537 // Cycle through each rule and run it
538 foreach ($rules As $rule)
539 {
540 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 // We set the $postdata variable with the current data in our master array so that
543 // each cycle of the loop is dealing with the processed data from the last cycle
544 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
545 {
546 // We shouldn't need this safety, but just in case there isn't an array index
547 // associated with this cycle we'll bail out
548 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
549 {
550 continue;
551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
554 $_in_array = TRUE;
555 }
556 else
557 {
558 $postdata = $this->_field_data[$row['field']]['postdata'];
559 }
560
561 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200562
563 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 $callback = FALSE;
565 if (substr($rule, 0, 9) == 'callback_')
566 {
567 $rule = substr($rule, 9);
568 $callback = TRUE;
569 }
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 // Strip the parameter (if exists) from the rule
572 // Rules can contain a parameter: max_length[5]
573 $param = FALSE;
574 if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
575 {
576 $rule = $match[1];
577 $param = $match[2];
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // Call the function that corresponds to the rule
581 if ($callback === TRUE)
582 {
583 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200584 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 continue;
586 }
Barry Mienydd671972010-10-04 16:33:58 +0200587
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 // Run the function and grab the result
589 $result = $this->CI->$rule($postdata, $param);
590
591 // Re-assign the result to the master data array
592 if ($_in_array == TRUE)
593 {
594 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
595 }
596 else
597 {
598 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 // If the field isn't required and we just processed a callback we'll move on...
602 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
603 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000604 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 }
606 }
607 else
Barry Mienydd671972010-10-04 16:33:58 +0200608 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 if ( ! method_exists($this, $rule))
610 {
Barry Mienydd671972010-10-04 16:33:58 +0200611 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 // Users can use any native PHP function call that has one param.
613 if (function_exists($rule))
614 {
615 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 if ($_in_array == TRUE)
618 {
619 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
620 }
621 else
622 {
623 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
624 }
625 }
Barry Mienydd671972010-10-04 16:33:58 +0200626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 continue;
628 }
629
630 $result = $this->$rule($postdata, $param);
631
632 if ($_in_array == TRUE)
633 {
634 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
635 }
636 else
637 {
638 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
639 }
640 }
Barry Mienydd671972010-10-04 16:33:58 +0200641
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 // Did the rule test negatively? If so, grab the error.
643 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200644 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 if ( ! isset($this->_error_messages[$rule]))
646 {
647 if (FALSE === ($line = $this->CI->lang->line($rule)))
648 {
649 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200650 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 }
652 else
653 {
654 $line = $this->_error_messages[$rule];
655 }
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 // Is the parameter we are inserting into the error message the name
658 // of another field? If so we need to grab its "field label"
659 if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
660 {
Pascal Krietec1895832009-10-13 12:56:43 +0000661 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 }
Barry Mienydd671972010-10-04 16:33:58 +0200663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 // Build the error message
665 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
666
667 // Save the error message
668 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200669
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 if ( ! isset($this->_error_array[$row['field']]))
671 {
672 $this->_error_array[$row['field']] = $message;
673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 return;
676 }
677 }
678 }
679
680 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 /**
683 * Translate a field name
684 *
685 * @access private
686 * @param string the field name
687 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200688 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 function _translate_fieldname($fieldname)
690 {
691 // Do we need to translate the field name?
692 // We look for the prefix lang: to determine this
693 if (substr($fieldname, 0, 5) == 'lang:')
694 {
695 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200696 $line = substr($fieldname, 5);
697
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 // Were we able to translate the field name? If not we use $line
699 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
700 {
701 return $line;
702 }
703 }
704
705 return $fieldname;
706 }
707
708 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 /**
711 * Get the value from a form
712 *
713 * Permits you to repopulate a form field with the value it was submitted
714 * with, or, if that value doesn't exist, with the default
715 *
716 * @access public
717 * @param string the field name
718 * @param string
719 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200720 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 function set_value($field = '', $default = '')
722 {
723 if ( ! isset($this->_field_data[$field]))
724 {
725 return $default;
726 }
Barry Mienydd671972010-10-04 16:33:58 +0200727
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 return $this->_field_data[$field]['postdata'];
729 }
Barry Mienydd671972010-10-04 16:33:58 +0200730
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200732
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 /**
734 * Set Select
735 *
736 * Enables pull-down lists to be set to the value the user
737 * selected in the event of an error
738 *
739 * @access public
740 * @param string
741 * @param string
742 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200743 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200745 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
747 {
748 if ($default === TRUE AND count($this->_field_data) === 0)
749 {
750 return ' selected="selected"';
751 }
752 return '';
753 }
Barry Mienydd671972010-10-04 16:33:58 +0200754
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200756
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 if (is_array($field))
758 {
759 if ( ! in_array($value, $field))
760 {
761 return '';
762 }
763 }
764 else
765 {
766 if (($field == '' OR $value == '') OR ($field != $value))
767 {
768 return '';
769 }
770 }
Barry Mienydd671972010-10-04 16:33:58 +0200771
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 return ' selected="selected"';
773 }
Barry Mienydd671972010-10-04 16:33:58 +0200774
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 /**
778 * Set Radio
779 *
780 * Enables radio buttons to be set to the value the user
781 * selected in the event of an error
782 *
783 * @access public
784 * @param string
785 * @param string
786 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200787 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 function set_radio($field = '', $value = '', $default = FALSE)
789 {
790 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
791 {
792 if ($default === TRUE AND count($this->_field_data) === 0)
793 {
794 return ' checked="checked"';
795 }
796 return '';
797 }
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 if (is_array($field))
802 {
803 if ( ! in_array($value, $field))
804 {
805 return '';
806 }
807 }
808 else
809 {
810 if (($field == '' OR $value == '') OR ($field != $value))
811 {
812 return '';
813 }
814 }
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 return ' checked="checked"';
817 }
Barry Mienydd671972010-10-04 16:33:58 +0200818
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 /**
822 * Set Checkbox
823 *
824 * Enables checkboxes to be set to the value the user
825 * selected in the event of an error
826 *
827 * @access public
828 * @param string
829 * @param string
830 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200831 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 function set_checkbox($field = '', $value = '', $default = FALSE)
833 {
834 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
835 {
836 if ($default === TRUE AND count($this->_field_data) === 0)
837 {
838 return ' checked="checked"';
839 }
840 return '';
841 }
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 if (is_array($field))
846 {
847 if ( ! in_array($value, $field))
848 {
849 return '';
850 }
851 }
852 else
853 {
854 if (($field == '' OR $value == '') OR ($field != $value))
855 {
856 return '';
857 }
858 }
Barry Mienydd671972010-10-04 16:33:58 +0200859
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 return ' checked="checked"';
861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 /**
866 * Required
867 *
868 * @access public
869 * @param string
870 * @return bool
871 */
872 function required($str)
873 {
874 if ( ! is_array($str))
875 {
876 return (trim($str) == '') ? FALSE : TRUE;
877 }
878 else
879 {
880 return ( ! empty($str));
881 }
882 }
Barry Mienydd671972010-10-04 16:33:58 +0200883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 /**
887 * Match one field to another
888 *
889 * @access public
890 * @param string
891 * @param field
892 * @return bool
893 */
894 function matches($str, $field)
895 {
896 if ( ! isset($_POST[$field]))
897 {
Barry Mienydd671972010-10-04 16:33:58 +0200898 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 }
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 $field = $_POST[$field];
902
903 return ($str !== $field) ? FALSE : TRUE;
904 }
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200907
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 /**
909 * Minimum Length
910 *
911 * @access public
912 * @param string
913 * @param value
914 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200915 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 function min_length($str, $val)
917 {
918 if (preg_match("/[^0-9]/", $val))
919 {
920 return FALSE;
921 }
922
923 if (function_exists('mb_strlen'))
924 {
Barry Mienydd671972010-10-04 16:33:58 +0200925 return (mb_strlen($str) < $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 }
Barry Mienydd671972010-10-04 16:33:58 +0200927
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 return (strlen($str) < $val) ? FALSE : TRUE;
929 }
Barry Mienydd671972010-10-04 16:33:58 +0200930
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 /**
934 * Max Length
935 *
936 * @access public
937 * @param string
938 * @param value
939 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200940 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 function max_length($str, $val)
942 {
943 if (preg_match("/[^0-9]/", $val))
944 {
945 return FALSE;
946 }
947
948 if (function_exists('mb_strlen'))
949 {
Barry Mienydd671972010-10-04 16:33:58 +0200950 return (mb_strlen($str) > $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
Barry Mienydd671972010-10-04 16:33:58 +0200952
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 return (strlen($str) > $val) ? FALSE : TRUE;
954 }
Barry Mienydd671972010-10-04 16:33:58 +0200955
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 /**
959 * Exact Length
960 *
961 * @access public
962 * @param string
963 * @param value
964 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200965 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 function exact_length($str, $val)
967 {
968 if (preg_match("/[^0-9]/", $val))
969 {
970 return FALSE;
971 }
972
973 if (function_exists('mb_strlen'))
974 {
Barry Mienydd671972010-10-04 16:33:58 +0200975 return (mb_strlen($str) != $val) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 }
Barry Mienydd671972010-10-04 16:33:58 +0200977
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 return (strlen($str) != $val) ? FALSE : TRUE;
979 }
Barry Mienydd671972010-10-04 16:33:58 +0200980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200982
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 /**
984 * Valid Email
985 *
986 * @access public
987 * @param string
988 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200989 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 function valid_email($str)
991 {
992 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
993 }
994
995 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 /**
998 * Valid Emails
999 *
1000 * @access public
1001 * @param string
1002 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001003 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 function valid_emails($str)
1005 {
1006 if (strpos($str, ',') === FALSE)
1007 {
1008 return $this->valid_email(trim($str));
1009 }
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 foreach(explode(',', $str) as $email)
1012 {
1013 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
1014 {
1015 return FALSE;
1016 }
1017 }
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 return TRUE;
1020 }
1021
1022 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001023
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 /**
1025 * Validate IP Address
1026 *
1027 * @access public
1028 * @param string
1029 * @return string
1030 */
1031 function valid_ip($ip)
1032 {
1033 return $this->CI->input->valid_ip($ip);
1034 }
1035
1036 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001037
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 /**
1039 * Alpha
1040 *
1041 * @access public
1042 * @param string
1043 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001044 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 function alpha($str)
1046 {
1047 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
1048 }
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001051
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 /**
1053 * Alpha-numeric
1054 *
1055 * @access public
1056 * @param string
1057 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001058 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 function alpha_numeric($str)
1060 {
1061 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
1062 }
Barry Mienydd671972010-10-04 16:33:58 +02001063
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001065
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 /**
1067 * Alpha-numeric with underscores and dashes
1068 *
1069 * @access public
1070 * @param string
1071 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001072 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 function alpha_dash($str)
1074 {
1075 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
1076 }
Barry Mienydd671972010-10-04 16:33:58 +02001077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001079
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 /**
1081 * Numeric
1082 *
1083 * @access public
1084 * @param string
1085 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001086 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 function numeric($str)
1088 {
1089 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
1090
1091 }
1092
1093 // --------------------------------------------------------------------
1094
Barry Mienydd671972010-10-04 16:33:58 +02001095 /**
1096 * Is Numeric
1097 *
1098 * @access public
1099 * @param string
1100 * @return bool
1101 */
1102 function is_numeric($str)
1103 {
1104 return ( ! is_numeric($str)) ? FALSE : TRUE;
1105 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001106
1107 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001108
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 /**
1110 * Integer
1111 *
1112 * @access public
1113 * @param string
1114 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001115 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 function integer($str)
1117 {
1118 return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
1119 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001120
1121 // --------------------------------------------------------------------
1122
Barry Mienydd671972010-10-04 16:33:58 +02001123 /**
1124 * Is a Natural number (0,1,2,3, etc.)
1125 *
1126 * @access public
1127 * @param string
1128 * @return bool
1129 */
1130 function is_natural($str)
1131 {
1132 return (bool)preg_match( '/^[0-9]+$/', $str);
1133 }
1134
1135 // --------------------------------------------------------------------
1136
1137 /**
1138 * Is a Natural number, but not a zero (1,2,3, etc.)
1139 *
1140 * @access public
1141 * @param string
1142 * @return bool
1143 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001145 {
1146 if ( ! preg_match( '/^[0-9]+$/', $str))
1147 {
1148 return FALSE;
1149 }
1150
1151 if ($str == 0)
1152 {
1153 return FALSE;
1154 }
1155
1156 return TRUE;
1157 }
1158
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001160
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 /**
1162 * Valid Base64
1163 *
1164 * Tests a string for characters outside of the Base64 alphabet
1165 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1166 *
1167 * @access public
1168 * @param string
1169 * @return bool
1170 */
1171 function valid_base64($str)
1172 {
1173 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1174 }
Barry Mienydd671972010-10-04 16:33:58 +02001175
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001177
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 /**
1179 * Prep data for form
1180 *
1181 * This function allows HTML to be safely shown in a form.
1182 * Special characters are converted.
1183 *
1184 * @access public
1185 * @param string
1186 * @return string
1187 */
1188 function prep_for_form($data = '')
1189 {
1190 if (is_array($data))
1191 {
1192 foreach ($data as $key => $val)
1193 {
1194 $data[$key] = $this->prep_for_form($val);
1195 }
Barry Mienydd671972010-10-04 16:33:58 +02001196
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 return $data;
1198 }
Barry Mienydd671972010-10-04 16:33:58 +02001199
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 if ($this->_safe_form_data == FALSE OR $data === '')
1201 {
1202 return $data;
1203 }
1204
1205 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1206 }
Barry Mienydd671972010-10-04 16:33:58 +02001207
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001209
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 /**
1211 * Prep URL
1212 *
1213 * @access public
1214 * @param string
1215 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001216 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 function prep_url($str = '')
1218 {
1219 if ($str == 'http://' OR $str == '')
1220 {
1221 return '';
1222 }
Barry Mienydd671972010-10-04 16:33:58 +02001223
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
1225 {
1226 $str = 'http://'.$str;
1227 }
Barry Mienydd671972010-10-04 16:33:58 +02001228
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 return $str;
1230 }
Barry Mienydd671972010-10-04 16:33:58 +02001231
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001233
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 /**
1235 * Strip Image Tags
1236 *
1237 * @access public
1238 * @param string
1239 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001240 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 function strip_image_tags($str)
1242 {
1243 return $this->CI->input->strip_image_tags($str);
1244 }
Barry Mienydd671972010-10-04 16:33:58 +02001245
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001247
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 /**
1249 * XSS Clean
1250 *
1251 * @access public
1252 * @param string
1253 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001254 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 function xss_clean($str)
1256 {
Derek Jones30841672010-04-26 09:09:21 -05001257 if ( ! isset($this->CI->security))
Derek Jones5640a712010-04-23 11:22:40 -05001258 {
Derek Jones30841672010-04-26 09:09:21 -05001259 $this->CI->load->library('security');
Derek Jones5640a712010-04-23 11:22:40 -05001260 }
Derek Jones30841672010-04-26 09:09:21 -05001261
Derek Jones5640a712010-04-23 11:22:40 -05001262 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 }
Barry Mienydd671972010-10-04 16:33:58 +02001264
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 /**
1268 * Convert PHP tags to entities
1269 *
1270 * @access public
1271 * @param string
1272 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001273 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 function encode_php_tags($str)
1275 {
1276 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
1277 }
1278
1279}
1280// END Form Validation Class
1281
1282/* End of file Form_validation.php */
Rick Ellisec1b70f2008-08-26 19:21:27 +00001283/* Location: ./system/libraries/Form_validation.php */