blob: 20bb6bdb78357ca9bb5bc92dea204da35bde5093 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Rick Ellisd02b5bf2008-09-12 23:35:31 +00009 * @copyright Copyright (c) 2008, EllisLab, Inc.
Derek Allardcdd2ab22008-01-23 00:05:38 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Validation Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Validation
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Allardcdd2ab22008-01-23 00:05:38 +000025 * @link http://codeigniter.com/user_guide/libraries/validation.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000026 */
27class CI_Validation {
28
29 var $CI;
30 var $error_string = '';
31 var $_error_array = array();
32 var $_rules = array();
33 var $_fields = array();
34 var $_error_messages = array();
35 var $_current_field = '';
36 var $_safe_form_data = FALSE;
37 var $_error_prefix = '<p>';
38 var $_error_suffix = '</p>';
39
40
41
42 /**
43 * Constructor
44 *
45 */
46 function CI_Validation()
47 {
48 $this->CI =& get_instance();
49 log_message('debug', "Validation Class Initialized");
50 }
51
52 // --------------------------------------------------------------------
53
54 /**
55 * Set Fields
56 *
57 * This function takes an array of field names as input
58 * and generates class variables with the same name, which will
59 * either be blank or contain the $_POST value corresponding to it
60 *
61 * @access public
62 * @param string
63 * @param string
64 * @return void
65 */
66 function set_fields($data = '', $field = '')
67 {
68 if ($data == '')
69 {
70 if (count($this->_fields) == 0)
71 {
72 return FALSE;
73 }
74 }
75 else
76 {
Derek Jones0b59f272008-05-13 04:22:33 +000077 if ( ! is_array($data))
Derek Allardd2df9bc2007-04-15 17:41:17 +000078 {
79 $data = array($data => $field);
80 }
81
82 if (count($data) > 0)
83 {
84 $this->_fields = $data;
85 }
86 }
87
88 foreach($this->_fields as $key => $val)
Derek Jones07edd4b2008-01-18 19:50:49 +000089 {
Derek Jones0b59f272008-05-13 04:22:33 +000090 $this->$key = ( ! isset($_POST[$key])) ? '' : $this->prep_for_form($_POST[$key]);
Derek Allardd2df9bc2007-04-15 17:41:17 +000091
92 $error = $key.'_error';
Derek Jones0b59f272008-05-13 04:22:33 +000093 if ( ! isset($this->$error))
Derek Allardd2df9bc2007-04-15 17:41:17 +000094 {
95 $this->$error = '';
96 }
97 }
98 }
99
100 // --------------------------------------------------------------------
101
102 /**
103 * Set Rules
104 *
105 * This function takes an array of field names and validation
106 * rules as input ad simply stores is for use later.
107 *
108 * @access public
109 * @param mixed
110 * @param string
111 * @return void
112 */
113 function set_rules($data, $rules = '')
114 {
Derek Jones0b59f272008-05-13 04:22:33 +0000115 if ( ! is_array($data))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000116 {
117 if ($rules == '')
118 return;
119
Derek Jones40306b52008-05-07 22:52:45 +0000120 $data = array($data => $rules);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000121 }
122
123 foreach ($data as $key => $val)
124 {
125 $this->_rules[$key] = $val;
126 }
127 }
128
129 // --------------------------------------------------------------------
130
131 /**
132 * Set Error Message
133 *
134 * Lets users set their own error messages on the fly. Note: The key
135 * name has to match the function name that it corresponds to.
136 *
137 * @access public
138 * @param string
139 * @param string
140 * @return string
141 */
142 function set_message($lang, $val = '')
143 {
Derek Jones0b59f272008-05-13 04:22:33 +0000144 if ( ! is_array($lang))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000145 {
146 $lang = array($lang => $val);
147 }
148
149 $this->_error_messages = array_merge($this->_error_messages, $lang);
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Set The Error Delimiter
156 *
157 * Permits a prefix/suffix to be added to each error message
158 *
159 * @access public
160 * @param string
161 * @param string
162 * @return void
163 */
164 function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
165 {
166 $this->_error_prefix = $prefix;
167 $this->_error_suffix = $suffix;
168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Run the Validator
174 *
175 * This function does all the work.
176 *
177 * @access public
178 * @return bool
179 */
180 function run()
181 {
182 // Do we even have any data to process? Mm?
183 if (count($_POST) == 0 OR count($this->_rules) == 0)
184 {
185 return FALSE;
186 }
187
188 // Load the language file containing error messages
189 $this->CI->lang->load('validation');
190
191 // Cycle through the rules and test for errors
192 foreach ($this->_rules as $field => $rules)
193 {
194 //Explode out the rules!
195 $ex = explode('|', $rules);
196
Rick Ellisa1b05a12007-04-27 21:20:38 +0000197 // Is the field required? If not, if the field is blank we'll move on to the next test
Derek Jones0b59f272008-05-13 04:22:33 +0000198 if ( ! in_array('required', $ex, TRUE))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000199 {
Derek Jones0b59f272008-05-13 04:22:33 +0000200 if ( ! isset($_POST[$field]) OR $_POST[$field] == '')
Derek Allardd2df9bc2007-04-15 17:41:17 +0000201 {
202 continue;
203 }
204 }
205
206 /*
207 * Are we dealing with an "isset" rule?
208 *
209 * Before going further, we'll see if one of the rules
210 * is to check whether the item is set (typically this
211 * applies only to checkboxes). If so, we'll
212 * test for it here since there's not reason to go
213 * further
214 */
Derek Jones0b59f272008-05-13 04:22:33 +0000215 if ( ! isset($_POST[$field]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000216 {
217 if (in_array('isset', $ex, TRUE) OR in_array('required', $ex))
218 {
Derek Jones0b59f272008-05-13 04:22:33 +0000219 if ( ! isset($this->_error_messages['isset']))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000220 {
221 if (FALSE === ($line = $this->CI->lang->line('isset')))
222 {
223 $line = 'The field was not set';
224 }
225 }
226 else
227 {
228 $line = $this->_error_messages['isset'];
229 }
230
Derek Allard9805f612007-12-31 16:02:32 +0000231 // Build the error message
Derek Jones0b59f272008-05-13 04:22:33 +0000232 $mfield = ( ! isset($this->_fields[$field])) ? $field : $this->_fields[$field];
Derek Allard9805f612007-12-31 16:02:32 +0000233 $message = sprintf($line, $mfield);
234
235 // Set the error variable. Example: $this->username_error
236 $error = $field.'_error';
237 $this->$error = $this->_error_prefix.$message.$this->_error_suffix;
238 $this->_error_array[] = $message;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000239 }
240
241 continue;
242 }
243
244 /*
245 * Set the current field
246 *
247 * The various prepping functions need to know the
248 * current field name so they can do this:
249 *
250 * $_POST[$this->_current_field] == 'bla bla';
251 */
252 $this->_current_field = $field;
253
254 // Cycle through the rules!
255 foreach ($ex As $rule)
256 {
257 // Is the rule a callback?
258 $callback = FALSE;
259 if (substr($rule, 0, 9) == 'callback_')
260 {
261 $rule = substr($rule, 9);
262 $callback = TRUE;
263 }
264
265 // Strip the parameter (if exists) from the rule
266 // Rules can contain a parameter: max_length[5]
267 $param = FALSE;
268 if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
269 {
270 $rule = $match[1];
271 $param = $match[2];
272 }
273
274 // Call the function that corresponds to the rule
275 if ($callback === TRUE)
276 {
Derek Jones0b59f272008-05-13 04:22:33 +0000277 if ( ! method_exists($this->CI, $rule))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000278 {
279 continue;
280 }
281
282 $result = $this->CI->$rule($_POST[$field], $param);
283
284 // If the field isn't required and we just processed a callback we'll move on...
Derek Jones0b59f272008-05-13 04:22:33 +0000285 if ( ! in_array('required', $ex, TRUE) AND $result !== FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000286 {
287 continue 2;
288 }
289
290 }
291 else
292 {
Derek Jones0b59f272008-05-13 04:22:33 +0000293 if ( ! method_exists($this, $rule))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000294 {
295 /*
296 * Run the native PHP function if called for
297 *
298 * If our own wrapper function doesn't exist we see
299 * if a native PHP function does. Users can use
300 * any native PHP function call that has one param.
301 */
302 if (function_exists($rule))
303 {
304 $_POST[$field] = $rule($_POST[$field]);
305 $this->$field = $_POST[$field];
306 }
307
308 continue;
309 }
310
311 $result = $this->$rule($_POST[$field], $param);
312 }
313
314 // Did the rule test negatively? If so, grab the error.
315 if ($result === FALSE)
316 {
Derek Jones0b59f272008-05-13 04:22:33 +0000317 if ( ! isset($this->_error_messages[$rule]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000318 {
319 if (FALSE === ($line = $this->CI->lang->line($rule)))
320 {
321 $line = 'Unable to access an error message corresponding to your field name.';
322 }
323 }
324 else
325 {
Derek Allard89bf50f2007-08-14 02:39:04 +0000326 $line = $this->_error_messages[$rule];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000327 }
328
329 // Build the error message
Derek Jones0b59f272008-05-13 04:22:33 +0000330 $mfield = ( ! isset($this->_fields[$field])) ? $field : $this->_fields[$field];
331 $mparam = ( ! isset($this->_fields[$param])) ? $param : $this->_fields[$param];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000332 $message = sprintf($line, $mfield, $mparam);
333
334 // Set the error variable. Example: $this->username_error
335 $error = $field.'_error';
336 $this->$error = $this->_error_prefix.$message.$this->_error_suffix;
337
338 // Add the error to the error array
339 $this->_error_array[] = $message;
340 continue 2;
341 }
342 }
343
344 }
345
346 $total_errors = count($this->_error_array);
347
348 /*
349 * Recompile the class variables
350 *
351 * If any prepping functions were called the $_POST data
352 * might now be different then the corresponding class
353 * variables so we'll set them anew.
354 */
355 if ($total_errors > 0)
356 {
357 $this->_safe_form_data = TRUE;
358 }
359
360 $this->set_fields();
361
362 // Did we end up with any errors?
363 if ($total_errors == 0)
364 {
365 return TRUE;
366 }
367
368 // Generate the error string
369 foreach ($this->_error_array as $val)
370 {
371 $this->error_string .= $this->_error_prefix.$val.$this->_error_suffix."\n";
372 }
373
374 return FALSE;
375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Required
381 *
382 * @access public
383 * @param string
384 * @return bool
385 */
386 function required($str)
387 {
Derek Jones0b59f272008-05-13 04:22:33 +0000388 if ( ! is_array($str))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000389 {
390 return (trim($str) == '') ? FALSE : TRUE;
391 }
392 else
393 {
Derek Jones0b59f272008-05-13 04:22:33 +0000394 return ( ! empty($str));
Derek Allardd2df9bc2007-04-15 17:41:17 +0000395 }
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * Match one field to another
402 *
403 * @access public
404 * @param string
Derek Allard9736d3f2008-06-16 21:36:01 +0000405 * @param field
Derek Allardd2df9bc2007-04-15 17:41:17 +0000406 * @return bool
407 */
408 function matches($str, $field)
409 {
Derek Jones0b59f272008-05-13 04:22:33 +0000410 if ( ! isset($_POST[$field]))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000411 {
412 return FALSE;
413 }
414
415 return ($str !== $_POST[$field]) ? FALSE : TRUE;
416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Minimum Length
422 *
423 * @access public
424 * @param string
Derek Allard9736d3f2008-06-16 21:36:01 +0000425 * @param value
Derek Allardd2df9bc2007-04-15 17:41:17 +0000426 * @return bool
427 */
428 function min_length($str, $val)
429 {
Rick Ellis001e2562007-06-13 22:01:30 +0000430 if (preg_match("/[^0-9]/", $val))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000431 {
432 return FALSE;
433 }
434
435 return (strlen($str) < $val) ? FALSE : TRUE;
436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Max Length
442 *
443 * @access public
444 * @param string
Derek Allard9736d3f2008-06-16 21:36:01 +0000445 * @param value
Derek Allardd2df9bc2007-04-15 17:41:17 +0000446 * @return bool
447 */
448 function max_length($str, $val)
449 {
Rick Ellis001e2562007-06-13 22:01:30 +0000450 if (preg_match("/[^0-9]/", $val))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000451 {
452 return FALSE;
453 }
454
455 return (strlen($str) > $val) ? FALSE : TRUE;
456 }
457
458 // --------------------------------------------------------------------
459
460 /**
461 * Exact Length
462 *
463 * @access public
464 * @param string
Derek Allard9736d3f2008-06-16 21:36:01 +0000465 * @param value
Derek Allardd2df9bc2007-04-15 17:41:17 +0000466 * @return bool
467 */
468 function exact_length($str, $val)
469 {
Rick Ellis001e2562007-06-13 22:01:30 +0000470 if (preg_match("/[^0-9]/", $val))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000471 {
472 return FALSE;
473 }
474
475 return (strlen($str) != $val) ? FALSE : TRUE;
476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Valid Email
482 *
483 * @access public
484 * @param string
485 * @return bool
486 */
487 function valid_email($str)
488 {
Derek Jones0b59f272008-05-13 04:22:33 +0000489 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000490 }
491
492 // --------------------------------------------------------------------
493
494 /**
Derek Allardb94b89c2008-04-28 23:18:00 +0000495 * Valid Emails
496 *
497 * @access public
498 * @param string
499 * @return bool
500 */
501 function valid_emails($str)
502 {
503 if (strpos($str, ',') === FALSE)
504 {
505 return $this->valid_email(trim($str));
506 }
507
508 foreach(explode(',', $str) as $email)
509 {
510 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
511 {
512 return FALSE;
513 }
514 }
515
516 return TRUE;
517 }
518
519 // --------------------------------------------------------------------
520
521 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000522 * Validate IP Address
523 *
524 * @access public
525 * @param string
526 * @return string
527 */
528 function valid_ip($ip)
529 {
Derek Allard53a9c3f2007-09-18 19:18:53 +0000530 return $this->CI->input->valid_ip($ip);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * Alpha
537 *
538 * @access public
539 * @param string
540 * @return bool
541 */
542 function alpha($str)
543 {
Derek Jones0b59f272008-05-13 04:22:33 +0000544 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000545 }
546
547 // --------------------------------------------------------------------
548
549 /**
550 * Alpha-numeric
551 *
552 * @access public
553 * @param string
554 * @return bool
555 */
556 function alpha_numeric($str)
557 {
Derek Jones0b59f272008-05-13 04:22:33 +0000558 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000559 }
560
561 // --------------------------------------------------------------------
562
563 /**
564 * Alpha-numeric with underscores and dashes
565 *
566 * @access public
567 * @param string
568 * @return bool
569 */
570 function alpha_dash($str)
571 {
Derek Jones0b59f272008-05-13 04:22:33 +0000572 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000573 }
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Numeric
579 *
580 * @access public
Derek Allard22cd38b2008-01-22 19:45:03 +0000581 * @param string
Derek Allardd2df9bc2007-04-15 17:41:17 +0000582 * @return bool
583 */
584 function numeric($str)
585 {
Derek Allard20460a62008-01-22 19:40:05 +0000586 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
587
Derek Allardd2df9bc2007-04-15 17:41:17 +0000588 }
589
590 // --------------------------------------------------------------------
Derek Allardeb002ff2008-02-10 20:27:40 +0000591
Rick Ellisc2abf1f2008-08-20 22:18:48 +0000592 /**
593 * Is Numeric
594 *
595 * @access public
596 * @param string
597 * @return bool
598 */
599 function is_numeric($str)
600 {
601 return ( ! is_numeric($str)) ? FALSE : TRUE;
602 }
Derek Allardeb002ff2008-02-10 20:27:40 +0000603
604 // --------------------------------------------------------------------
Derek Allardd2df9bc2007-04-15 17:41:17 +0000605
606 /**
Derek Allard12f9cc82008-01-22 07:21:32 +0000607 * Integer
Derek Allardd2df9bc2007-04-15 17:41:17 +0000608 *
609 * @access public
610 * @param string
611 * @return bool
612 */
Derek Allard12f9cc82008-01-22 07:21:32 +0000613 function integer($str)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000614 {
Derek Allard20460a62008-01-22 19:40:05 +0000615 return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000616 }
Rick Ellisd6b06492008-08-20 22:07:30 +0000617
618 // --------------------------------------------------------------------
619
Rick Ellisc2abf1f2008-08-20 22:18:48 +0000620 /**
621 * Is a Natural number (0,1,2,3, etc.)
622 *
623 * @access public
624 * @param string
625 * @return bool
626 */
627 function is_natural($str)
628 {
Rick Ellisd6b06492008-08-20 22:07:30 +0000629 return (bool)preg_match( '/^[0-9]+$/', $str);
Rick Ellisc2abf1f2008-08-20 22:18:48 +0000630 }
Rick Ellisd6b06492008-08-20 22:07:30 +0000631
632 // --------------------------------------------------------------------
633
Rick Ellisc2abf1f2008-08-20 22:18:48 +0000634 /**
635 * Is a Natural number, but not a zero (1,2,3, etc.)
636 *
637 * @access public
638 * @param string
639 * @return bool
640 */
Rick Ellisd6b06492008-08-20 22:07:30 +0000641 function is_natural_no_zero($str)
Rick Ellisc2abf1f2008-08-20 22:18:48 +0000642 {
Derek Allard993925b2008-08-21 12:43:31 +0000643 if ( ! preg_match( '/^[0-9]+$/', $str))
644 {
645 return FALSE;
646 }
647
648 if ($str == 0)
649 {
650 return FALSE;
651 }
652
653 return TRUE;
Rick Ellisc2abf1f2008-08-20 22:18:48 +0000654 }
Rick Ellisd6b06492008-08-20 22:07:30 +0000655
Derek Allardd2df9bc2007-04-15 17:41:17 +0000656 // --------------------------------------------------------------------
657
658 /**
Derek Jones15130ca2008-01-28 15:54:45 +0000659 * Valid Base64
660 *
661 * Tests a string for characters outside of the Base64 alphabet
662 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
663 *
664 * @access public
665 * @param string
666 * @return bool
667 */
668 function valid_base64($str)
669 {
670 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
671 }
672
673 // --------------------------------------------------------------------
674
675 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000676 * Set Select
677 *
678 * Enables pull-down lists to be set to the value the user
679 * selected in the event of an error
680 *
681 * @access public
682 * @param string
683 * @param string
684 * @return string
685 */
686 function set_select($field = '', $value = '')
687 {
688 if ($field == '' OR $value == '' OR ! isset($_POST[$field]))
689 {
690 return '';
691 }
692
693 if ($_POST[$field] == $value)
694 {
695 return ' selected="selected"';
696 }
697 }
698
699 // --------------------------------------------------------------------
700
701 /**
702 * Set Radio
703 *
704 * Enables radio buttons to be set to the value the user
705 * selected in the event of an error
706 *
707 * @access public
708 * @param string
709 * @param string
710 * @return string
711 */
712 function set_radio($field = '', $value = '')
713 {
714 if ($field == '' OR $value == '' OR ! isset($_POST[$field]))
715 {
716 return '';
717 }
718
719 if ($_POST[$field] == $value)
720 {
721 return ' checked="checked"';
722 }
723 }
724
725 // --------------------------------------------------------------------
726
727 /**
728 * Set Checkbox
729 *
730 * Enables checkboxes to be set to the value the user
731 * selected in the event of an error
732 *
733 * @access public
734 * @param string
735 * @param string
736 * @return string
737 */
738 function set_checkbox($field = '', $value = '')
739 {
740 if ($field == '' OR $value == '' OR ! isset($_POST[$field]))
741 {
742 return '';
743 }
744
745 if ($_POST[$field] == $value)
746 {
747 return ' checked="checked"';
748 }
749 }
750
751 // --------------------------------------------------------------------
752
753 /**
754 * Prep data for form
755 *
756 * This function allows HTML to be safely shown in a form.
757 * Special characters are converted.
758 *
759 * @access public
760 * @param string
761 * @return string
762 */
Derek Jones07edd4b2008-01-18 19:50:49 +0000763 function prep_for_form($data = '')
Derek Allardd2df9bc2007-04-15 17:41:17 +0000764 {
Derek Jones07edd4b2008-01-18 19:50:49 +0000765 if (is_array($data))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000766 {
Derek Jones07edd4b2008-01-18 19:50:49 +0000767 foreach ($data as $key => $val)
768 {
769 $data[$key] = $this->prep_for_form($val);
770 }
Derek Jonesd56743b2008-05-12 16:17:58 +0000771
772 return $data;
Derek Jones07edd4b2008-01-18 19:50:49 +0000773 }
774
775 if ($this->_safe_form_data == FALSE OR $data == '')
776 {
777 return $data;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000778 }
779
Derek Jones07edd4b2008-01-18 19:50:49 +0000780 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
Derek Allardd2df9bc2007-04-15 17:41:17 +0000781 }
782
783 // --------------------------------------------------------------------
784
785 /**
786 * Prep URL
787 *
788 * @access public
789 * @param string
790 * @return string
791 */
792 function prep_url($str = '')
793 {
794 if ($str == 'http://' OR $str == '')
795 {
796 $_POST[$this->_current_field] = '';
797 return;
798 }
799
800 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
801 {
802 $str = 'http://'.$str;
803 }
804
805 $_POST[$this->_current_field] = $str;
806 }
807
808 // --------------------------------------------------------------------
809
810 /**
811 * Strip Image Tags
812 *
813 * @access public
814 * @param string
815 * @return string
816 */
817 function strip_image_tags($str)
818 {
Derek Allard15a34772008-01-29 21:09:26 +0000819 $_POST[$this->_current_field] = $this->CI->input->strip_image_tags($str);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000820 }
821
822 // --------------------------------------------------------------------
823
824 /**
825 * XSS Clean
826 *
827 * @access public
828 * @param string
829 * @return string
830 */
831 function xss_clean($str)
832 {
833 $_POST[$this->_current_field] = $this->CI->input->xss_clean($str);
834 }
835
836 // --------------------------------------------------------------------
837
838 /**
839 * Convert PHP tags to entities
840 *
841 * @access public
842 * @param string
843 * @return string
844 */
845 function encode_php_tags($str)
846 {
847 $_POST[$this->_current_field] = str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
848 }
849
850}
851// END Validation Class
Derek Jonesd56743b2008-05-12 16:17:58 +0000852
853/* End of file Validation.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000854/* Location: ./system/libraries/Validation.php */