blob: 4f3ffd0d3b67e197aab0ec2e717a8acee4478e8f [file] [log] [blame]
Rick Ellisec1b70f2008-08-26 19:21:27 +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
Rick Ellisd02b5bf2008-09-12 23:35:31 +00009 * @copyright Copyright (c) 2008, EllisLab, Inc.
Rick Ellisec1b70f2008-08-26 19:21:27 +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 {
28
29 var $CI;
30 var $_field_data = array();
31 var $_config_rules = array();
32 var $_error_array = array();
33 var $_error_messages = array();
34 var $_error_prefix = '<p>';
35 var $_error_suffix = '</p>';
36 var $error_string = '';
37 var $_safe_form_data = FALSE;
38
39
40 /**
41 * Constructor
42 *
43 */
44 function CI_Form_validation($rules = array())
45 {
46 $this->CI =& get_instance();
47
48 // Validation rules can be stored in a config file.
49 $this->_config_rules = $rules;
50
51 // Automatically load the form helper
52 $this->CI->load->helper('form');
Rick Ellis29828ac2008-10-17 06:55:01 +000053
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 }
59
Rick Ellisec1b70f2008-08-26 19:21:27 +000060 log_message('debug', "Validation Class Initialized");
61 }
62
63 // --------------------------------------------------------------------
64
65 /**
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 }
83
84 // 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 }
104
Rick Ellis9056b562008-09-09 20:42:33 +0000105 // No fields? Nothing to do...
106 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Rick Ellisec1b70f2008-08-26 19:21:27 +0000107 {
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
116 // into its components so that we can fetch the corresponding POST data later
117 if (strpos($field, '[') !== FALSE AND preg_match_all('/\[(.*?)\]/', $field, $matches))
118 {
119 // 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 }
131
132 $is_array = TRUE;
133 }
134 else
135 {
136 $indexes = array();
137 $is_array = FALSE;
138 }
139
140 // Build our master array
141 $this->_field_data[$field] = array(
142 'field' => $field,
143 'label' => $label,
144 'rules' => $rules,
145 'is_array' => $is_array,
146 'keys' => $indexes,
147 'postdata' => NULL,
148 'error' => ''
149 );
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
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 }
171
172 $this->_error_messages = array_merge($this->_error_messages, $lang);
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
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
186 */
187 function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
188 {
189 $this->_error_prefix = $prefix;
190 $this->_error_suffix = $suffix;
191 }
192
193 // --------------------------------------------------------------------
194
195 /**
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
203 */
204 function error($field = '', $prefix = '', $suffix = '')
205 {
Rick Ellis9056b562008-09-09 20:42:33 +0000206 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
Rick Ellisec1b70f2008-08-26 19:21:27 +0000207 {
208 return '';
209 }
210
211 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 // --------------------------------------------------------------------
225
226 /**
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
235 */
236 function error_string($prefix = '', $suffix = '')
237 {
238 // No errrors, validation passes!
239 if (count($this->_error_array) === 0)
240 {
241 return '';
242 }
243
244 if ($prefix == '')
245 {
246 $prefix = $this->_error_prefix;
247 }
248
249 if ($suffix == '')
250 {
251 $suffix = $this->_error_suffix;
252 }
253
254 // Generate the error string
255 $str = '';
256 foreach ($this->_error_array as $val)
257 {
Rick Ellis9056b562008-09-09 20:42:33 +0000258 if ($val != '')
259 {
260 $str .= $prefix.$val.$suffix."\n";
261 }
Rick Ellisec1b70f2008-08-26 19:21:27 +0000262 }
263
264 return $str;
265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * Run the Validator
271 *
272 * This function does all the work.
273 *
274 * @access public
275 * @return bool
276 */
277 function run($group = '')
278 {
279 // Do we even have any data to process? Mm?
280 if (count($_POST) == 0)
281 {
282 return FALSE;
283 }
284
285 // 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 }
294
295 // Is there a validation rule for the particular URI being accessed?
296 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
297
298 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 }
306
307 // 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 }
314
315 // Load the language file containing error messages
316 $this->CI->lang->load('form_validation');
317
318 // Cycle through the rules for each field, match the
319 // corresponding $_POST item and test for errors
320 foreach ($this->_field_data as $field => $row)
321 {
322 // 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.
324
325 if ($row['is_array'] == TRUE)
326 {
327 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
328 }
329 else
330 {
Rick Ellisa5acb732008-08-27 21:43:13 +0000331 if (isset($_POST[$field]) AND $_POST[$field] != "")
Rick Ellisec1b70f2008-08-26 19:21:27 +0000332 {
333 $this->_field_data[$field]['postdata'] = $_POST[$field];
334 }
335 }
336
337 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
338 }
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();
350
351 // No errors, validation passes!
352 if ($total_errors == 0)
353 {
354 return TRUE;
355 }
356
357 // Validation fails
358 return FALSE;
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
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
371 */
372 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 }
392
393 return $array;
394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Re-populate the _POST array with our finalized and processed data
400 *
401 * @access private
402 * @return null
403 */
404 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 {
419 $post = '$_POST["';
420
421 if (count($row['keys']) == 1)
422 {
423 $post .= current($row['keys']);
424 $post .= '"]';
425 }
426 else
427 {
428 $i = 0;
429 foreach ($row['keys'] as $val)
430 {
431 if ($i == 0)
432 {
433 $post .= $val.'"]';
434 $i++;
435 continue;
436 }
437
438 $post .= '["'.$val.'"]';
439 }
440 }
441
442 if (is_array($row['postdata']))
443 {
444 $array = array();
445 foreach ($row['postdata'] as $k => $v)
446 {
447 $array[$k] = $this->prep_for_form($v);
448 }
449
450 $post .= ' = $array;';
451 }
452 else
453 {
454 $post .= ' = "'.$this->prep_for_form($row['postdata']).'";';
455 }
456
457 eval($post);
458 }
459 }
460 }
461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Executes the Validation routines
467 *
468 * @access private
469 * @param array
470 * @param array
471 * @param mixed
472 * @param integer
473 * @return mixed
474 */
475 function _execute($row, $rules, $postdata = NULL, $cycles = 0)
476 {
477 // If the $_POST data is an array we will run a recursive call
478 if (is_array($postdata))
479 {
480 foreach ($postdata as $key => $val)
481 {
482 $this->_execute($row, $rules, $val, $cycles);
483 $cycles++;
484 }
485
486 return;
487 }
Rick Ellis53b70e12008-09-20 04:48:14 +0000488
Rick Ellisec1b70f2008-08-26 19:21:27 +0000489 // --------------------------------------------------------------------
490
491 // If the field is blank, but NOT required, no further tests are necessary
Rick Ellis53b70e12008-09-20 04:48:14 +0000492 $callback = FALSE;
493 if ( ! in_array('required', $rules) AND is_null($postdata))
Rick Ellisec1b70f2008-08-26 19:21:27 +0000494 {
Rick Ellis53b70e12008-09-20 04:48:14 +0000495 // Before we bail out, does the rule contain a callback?
496 if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
497 {
498 $callback = TRUE;
499 $rules = (array('1' => $match[1]));
500 }
501 else
502 {
503 return;
504 }
Rick Ellisec1b70f2008-08-26 19:21:27 +0000505 }
506
507 // --------------------------------------------------------------------
508
509 // Isset Test. Typically this rule will only apply to checkboxes.
Rick Ellis53b70e12008-09-20 04:48:14 +0000510 if (is_null($postdata) AND $callback == FALSE)
Rick Ellisec1b70f2008-08-26 19:21:27 +0000511 {
512 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
513 {
514 if ( ! isset($this->_error_messages['isset']))
515 {
516 if (FALSE === ($line = $this->CI->lang->line('isset')))
517 {
518 $line = 'The field was not set';
519 }
520 }
521 else
522 {
Rick Ellis29828ac2008-10-17 06:55:01 +0000523 $line = $this->_error_messages['isset'];
Rick Ellisec1b70f2008-08-26 19:21:27 +0000524 }
525
526 // Build the error message
Rick Ellis277451a2008-09-20 03:42:20 +0000527 $message = sprintf($line, $this->_translate_fieldname($row['label']));
Rick Ellisec1b70f2008-08-26 19:21:27 +0000528
529 // Save the error message
530 $this->_field_data[$row['field']]['error'] = $message;
531
532 if ( ! isset($this->_error_array[$row['field']]))
533 {
534 $this->_error_array[$row['field']] = $message;
535 }
536 }
537
538 return;
539 }
540
541 // --------------------------------------------------------------------
542
543 // Cycle through each rule and run it
544 foreach ($rules As $rule)
545 {
546 $_in_array = FALSE;
547
548 // We set the $postdata variable with the current data in our master array so that
549 // each cycle of the loop is dealing with the processed data from the last cycle
550 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
551 {
552 // We shouldn't need this safety, but just in case there isn't an array index
553 // associated with this cycle we'll bail out
554 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
555 {
556 continue;
557 }
558
559 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
560 $_in_array = TRUE;
561 }
562 else
563 {
564 $postdata = $this->_field_data[$row['field']]['postdata'];
565 }
566
567 // --------------------------------------------------------------------
568
569 // Is the rule a callback?
570 $callback = FALSE;
571 if (substr($rule, 0, 9) == 'callback_')
572 {
573 $rule = substr($rule, 9);
574 $callback = TRUE;
575 }
576
577 // Strip the parameter (if exists) from the rule
578 // Rules can contain a parameter: max_length[5]
579 $param = FALSE;
580 if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
581 {
582 $rule = $match[1];
583 $param = $match[2];
584 }
585
586 // Call the function that corresponds to the rule
587 if ($callback === TRUE)
588 {
589 if ( ! method_exists($this->CI, $rule))
590 {
591 continue;
592 }
593
594 // Run the function and grab the result
595 $result = $this->CI->$rule($postdata, $param);
596
597 // Re-assign the result to the master data array
598 if ($_in_array == TRUE)
599 {
600 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
601 }
602 else
603 {
604 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
605 }
606
607 // If the field isn't required and we just processed a callback we'll move on...
608 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
609 {
610 return;
611 }
612 }
613 else
614 {
615 if ( ! method_exists($this, $rule))
616 {
Rick Ellisfaa6d092008-09-23 20:01:33 +0000617 // If our own wrapper function doesn't exist we see if a native PHP function does.
618 // Users can use any native PHP function call that has one param.
Rick Ellisec1b70f2008-08-26 19:21:27 +0000619 if (function_exists($rule))
620 {
621 $result = $rule($postdata);
622
623 if ($_in_array == TRUE)
624 {
625 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
626 }
627 else
628 {
629 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
630 }
631 }
632
633 continue;
634 }
635
636 $result = $this->$rule($postdata, $param);
637
638 if ($_in_array == TRUE)
639 {
640 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
641 }
642 else
643 {
644 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
645 }
646 }
647
648 // Did the rule test negatively? If so, grab the error.
649 if ($result === FALSE)
Rick Ellis96fabe52008-09-23 01:18:36 +0000650 {
Rick Ellisec1b70f2008-08-26 19:21:27 +0000651 if ( ! isset($this->_error_messages[$rule]))
652 {
653 if (FALSE === ($line = $this->CI->lang->line($rule)))
654 {
655 $line = 'Unable to access an error message corresponding to your field name.';
656 }
657 }
658 else
659 {
Rick Ellis96fabe52008-09-23 01:18:36 +0000660 $line = $this->_error_messages[$rule];
661 }
Rick Ellisec1b70f2008-08-26 19:21:27 +0000662
663 // Build the error message
Rick Ellis277451a2008-09-20 03:42:20 +0000664 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
Rick Ellisec1b70f2008-08-26 19:21:27 +0000665
666 // Save the error message
667 $this->_field_data[$row['field']]['error'] = $message;
668
669 if ( ! isset($this->_error_array[$row['field']]))
670 {
671 $this->_error_array[$row['field']] = $message;
672 }
673
674 return;
675 }
676 }
Rick Ellis277451a2008-09-20 03:42:20 +0000677 }
678
679 // --------------------------------------------------------------------
680
681 /**
682 * Translate a field name
683 *
684 * @access private
685 * @param string the field name
686 * @return string
687 */
688 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 {
Rick Ellis06695402008-09-22 21:58:10 +0000694 // Grab the variable
Rick Ellis9b8bc352008-09-23 01:21:23 +0000695 $line = substr($fieldname, 5);
Rick Ellis277451a2008-09-20 03:42:20 +0000696
Rick Ellis9b8bc352008-09-23 01:21:23 +0000697 // Were we able to translate the field name? If not we use $line
698 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
Rick Ellisfe61d632008-09-22 21:55:12 +0000699 {
Rick Ellis06695402008-09-22 21:58:10 +0000700 return $line;
Rick Ellisfe61d632008-09-22 21:55:12 +0000701 }
Rick Ellis277451a2008-09-20 03:42:20 +0000702 }
703
Rick Ellis96fabe52008-09-23 01:18:36 +0000704 return $fieldname;
Rick Ellis277451a2008-09-20 03:42:20 +0000705 }
Rick Ellisec1b70f2008-08-26 19:21:27 +0000706
707 // --------------------------------------------------------------------
708
709 /**
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
719 */
720 function set_value($field = '', $default = '')
721 {
722 if ( ! isset($this->_field_data[$field]))
723 {
724 return $default;
725 }
726
727 return $this->_field_data[$field]['postdata'];
728 }
729
730 // --------------------------------------------------------------------
731
732 /**
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
742 */
743 function set_select($field = '', $value = '', $default = FALSE)
744 {
745 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 }
753
754 $field = $this->_field_data[$field]['postdata'];
755
756 if (is_array($field))
757 {
Rick Ellis32f84f12008-09-20 04:21:27 +0000758 if ( ! in_array($value, $field))
Rick Ellisec1b70f2008-08-26 19:21:27 +0000759 {
760 return '';
761 }
762 }
763 else
764 {
765 if (($field == '' OR $value == '') OR ($field != $value))
766 {
767 return '';
768 }
769 }
770
771 return ' selected="selected"';
772 }
773
774 // --------------------------------------------------------------------
775
776 /**
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
786 */
787 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 }
797
798 $field = $this->_field_data[$field]['postdata'];
799
800 if (is_array($field))
801 {
Rick Ellis32f84f12008-09-20 04:21:27 +0000802 if ( ! in_array($value, $field))
Rick Ellisec1b70f2008-08-26 19:21:27 +0000803 {
804 return '';
805 }
806 }
807 else
808 {
809 if (($field == '' OR $value == '') OR ($field != $value))
810 {
811 return '';
812 }
813 }
814
815 return ' checked="checked"';
816 }
817
818 // --------------------------------------------------------------------
819
820 /**
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
830 */
831 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 }
841
842 $field = $this->_field_data[$field]['postdata'];
843
844 if (is_array($field))
845 {
Rick Ellis32f84f12008-09-20 04:21:27 +0000846 if ( ! in_array($value, $field))
Rick Ellisec1b70f2008-08-26 19:21:27 +0000847 {
848 return '';
849 }
850 }
851 else
852 {
853 if (($field == '' OR $value == '') OR ($field != $value))
854 {
855 return '';
856 }
857 }
858
859 return ' checked="checked"';
860 }
861
862 // --------------------------------------------------------------------
863
864 /**
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 }
882
883 // --------------------------------------------------------------------
884
885 /**
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 {
897 return FALSE;
898 }
899
900 $field = $_POST[$field];
901
902 return ($str !== $field) ? FALSE : TRUE;
903 }
904
905 // --------------------------------------------------------------------
906
907 /**
908 * Minimum Length
909 *
910 * @access public
911 * @param string
912 * @param value
913 * @return bool
914 */
915 function min_length($str, $val)
916 {
917 if (preg_match("/[^0-9]/", $val))
918 {
919 return FALSE;
920 }
Rick Ellis29828ac2008-10-17 06:55:01 +0000921
922 if (function_exists('mb_strlen'))
923 {
924 return (mb_strlen($str) < $val) ? FALSE : TRUE;
925 }
Rick Ellisec1b70f2008-08-26 19:21:27 +0000926
927 return (strlen($str) < $val) ? FALSE : TRUE;
928 }
929
930 // --------------------------------------------------------------------
931
932 /**
933 * Max Length
934 *
935 * @access public
936 * @param string
937 * @param value
938 * @return bool
939 */
940 function max_length($str, $val)
941 {
942 if (preg_match("/[^0-9]/", $val))
943 {
944 return FALSE;
945 }
Rick Ellis29828ac2008-10-17 06:55:01 +0000946
947 if (function_exists('mb_strlen'))
948 {
949 return (mb_strlen($str) > $val) ? FALSE : TRUE;
950 }
Rick Ellisec1b70f2008-08-26 19:21:27 +0000951
952 return (strlen($str) > $val) ? FALSE : TRUE;
953 }
954
955 // --------------------------------------------------------------------
956
957 /**
958 * Exact Length
959 *
960 * @access public
961 * @param string
962 * @param value
963 * @return bool
964 */
965 function exact_length($str, $val)
966 {
967 if (preg_match("/[^0-9]/", $val))
968 {
969 return FALSE;
970 }
Rick Ellis29828ac2008-10-17 06:55:01 +0000971
972 if (function_exists('mb_strlen'))
973 {
974 return (mb_strlen($str) != $val) ? FALSE : TRUE;
975 }
Rick Ellisec1b70f2008-08-26 19:21:27 +0000976
977 return (strlen($str) != $val) ? FALSE : TRUE;
978 }
979
980 // --------------------------------------------------------------------
981
982 /**
983 * Valid Email
984 *
985 * @access public
986 * @param string
987 * @return bool
988 */
989 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 // --------------------------------------------------------------------
995
996 /**
997 * Valid Emails
998 *
999 * @access public
1000 * @param string
1001 * @return bool
1002 */
1003 function valid_emails($str)
1004 {
1005 if (strpos($str, ',') === FALSE)
1006 {
1007 return $this->valid_email(trim($str));
1008 }
1009
1010 foreach(explode(',', $str) as $email)
1011 {
1012 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
1013 {
1014 return FALSE;
1015 }
1016 }
1017
1018 return TRUE;
1019 }
1020
1021 // --------------------------------------------------------------------
1022
1023 /**
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 // --------------------------------------------------------------------
1036
1037 /**
1038 * Alpha
1039 *
1040 * @access public
1041 * @param string
1042 * @return bool
1043 */
1044 function alpha($str)
1045 {
1046 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
1047 }
1048
1049 // --------------------------------------------------------------------
1050
1051 /**
1052 * Alpha-numeric
1053 *
1054 * @access public
1055 * @param string
1056 * @return bool
1057 */
1058 function alpha_numeric($str)
1059 {
1060 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
1061 }
1062
1063 // --------------------------------------------------------------------
1064
1065 /**
1066 * Alpha-numeric with underscores and dashes
1067 *
1068 * @access public
1069 * @param string
1070 * @return bool
1071 */
1072 function alpha_dash($str)
1073 {
1074 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
1075 }
1076
1077 // --------------------------------------------------------------------
1078
1079 /**
1080 * Numeric
1081 *
1082 * @access public
1083 * @param string
1084 * @return bool
1085 */
1086 function numeric($str)
1087 {
1088 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
1089
1090 }
1091
1092 // --------------------------------------------------------------------
1093
1094 /**
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 }
1105
1106 // --------------------------------------------------------------------
1107
1108 /**
1109 * Integer
1110 *
1111 * @access public
1112 * @param string
1113 * @return bool
1114 */
1115 function integer($str)
1116 {
1117 return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
1118 }
1119
1120 // --------------------------------------------------------------------
1121
1122 /**
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 */
1143 function is_natural_no_zero($str)
1144 {
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
1158 // --------------------------------------------------------------------
1159
1160 /**
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 }
1174
1175 // --------------------------------------------------------------------
1176
1177 /**
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 }
1195
1196 return $data;
1197 }
1198
1199 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 }
1206
1207 // --------------------------------------------------------------------
1208
1209 /**
1210 * Prep URL
1211 *
1212 * @access public
1213 * @param string
1214 * @return string
1215 */
1216 function prep_url($str = '')
1217 {
1218 if ($str == 'http://' OR $str == '')
1219 {
1220 return '';
1221 }
1222
1223 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
1224 {
1225 $str = 'http://'.$str;
1226 }
1227
1228 return $str;
1229 }
1230
1231 // --------------------------------------------------------------------
1232
1233 /**
1234 * Strip Image Tags
1235 *
1236 * @access public
1237 * @param string
1238 * @return string
1239 */
1240 function strip_image_tags($str)
1241 {
1242 return $this->CI->input->strip_image_tags($str);
1243 }
1244
1245 // --------------------------------------------------------------------
1246
1247 /**
1248 * XSS Clean
1249 *
1250 * @access public
1251 * @param string
1252 * @return string
1253 */
1254 function xss_clean($str)
1255 {
1256 return $this->CI->input->xss_clean($str);
1257 }
1258
1259 // --------------------------------------------------------------------
1260
1261 /**
1262 * Convert PHP tags to entities
1263 *
1264 * @access public
1265 * @param string
1266 * @return string
1267 */
1268 function encode_php_tags($str)
1269 {
1270 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
1271 }
1272
1273}
1274// END Form Validation Class
1275
1276/* End of file Form_validation.php */
1277/* Location: ./system/libraries/Form_validation.php */