blob: f0c1500dfd9071ddbf2322e6eb9cdaebd4b5b42d [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');
53
54 log_message('debug', "Validation Class Initialized");
55 }
56
57 // --------------------------------------------------------------------
58
59 /**
60 * Set Rules
61 *
62 * This function takes an array of field names and validation
63 * rules as input, validates the info, and stores it
64 *
65 * @access public
66 * @param mixed
67 * @param string
68 * @return void
69 */
70 function set_rules($field, $label = '', $rules = '')
71 {
72 // No reason to set rules if we have no POST data
73 if (count($_POST) == 0)
74 {
75 return;
76 }
77
78 // If an array was passed via the first parameter instead of indidual string
79 // values we cycle through it and recursively call this function.
80 if (is_array($field))
81 {
82 foreach ($field as $row)
83 {
84 // Houston, we have a problem...
85 if ( ! isset($row['field']) OR ! isset($row['rules']))
86 {
87 continue;
88 }
89
90 // If the field label wasn't passed we use the field name
91 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
92
93 // Here we go!
94 $this->set_rules($row['field'], $label, $row['rules']);
95 }
96 return;
97 }
98
Rick Ellis9056b562008-09-09 20:42:33 +000099 // No fields? Nothing to do...
100 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Rick Ellisec1b70f2008-08-26 19:21:27 +0000101 {
102 return;
103 }
104
105 // If the field label wasn't passed we use the field name
106 $label = ($label == '') ? $field : $label;
107
108 // Is the field name an array? We test for the existence of a bracket "[" in
109 // the field name to determine this. If it is an array, we break it apart
110 // into its components so that we can fetch the corresponding POST data later
111 if (strpos($field, '[') !== FALSE AND preg_match_all('/\[(.*?)\]/', $field, $matches))
112 {
113 // Note: Due to a bug in current() that affects some versions
114 // of PHP we can not pass function call directly into it
115 $x = explode('[', $field);
116 $indexes[] = current($x);
117
118 for ($i = 0; $i < count($matches['0']); $i++)
119 {
120 if ($matches['1'][$i] != '')
121 {
122 $indexes[] = $matches['1'][$i];
123 }
124 }
125
126 $is_array = TRUE;
127 }
128 else
129 {
130 $indexes = array();
131 $is_array = FALSE;
132 }
133
134 // Build our master array
135 $this->_field_data[$field] = array(
136 'field' => $field,
137 'label' => $label,
138 'rules' => $rules,
139 'is_array' => $is_array,
140 'keys' => $indexes,
141 'postdata' => NULL,
142 'error' => ''
143 );
144 }
145
146 // --------------------------------------------------------------------
147
148 /**
149 * Set Error Message
150 *
151 * Lets users set their own error messages on the fly. Note: The key
152 * name has to match the function name that it corresponds to.
153 *
154 * @access public
155 * @param string
156 * @param string
157 * @return string
158 */
159 function set_message($lang, $val = '')
160 {
161 if ( ! is_array($lang))
162 {
163 $lang = array($lang => $val);
164 }
165
166 $this->_error_messages = array_merge($this->_error_messages, $lang);
167 }
168
169 // --------------------------------------------------------------------
170
171 /**
172 * Set The Error Delimiter
173 *
174 * Permits a prefix/suffix to be added to each error message
175 *
176 * @access public
177 * @param string
178 * @param string
179 * @return void
180 */
181 function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
182 {
183 $this->_error_prefix = $prefix;
184 $this->_error_suffix = $suffix;
185 }
186
187 // --------------------------------------------------------------------
188
189 /**
190 * Get Error Message
191 *
192 * Gets the error message associated with a particular field
193 *
194 * @access public
195 * @param string the field name
196 * @return void
197 */
198 function error($field = '', $prefix = '', $suffix = '')
199 {
Rick Ellis9056b562008-09-09 20:42:33 +0000200 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
Rick Ellisec1b70f2008-08-26 19:21:27 +0000201 {
202 return '';
203 }
204
205 if ($prefix == '')
206 {
207 $prefix = $this->_error_prefix;
208 }
209
210 if ($suffix == '')
211 {
212 $suffix = $this->_error_suffix;
213 }
214
215 return $prefix.$this->_field_data[$field]['error'].$suffix;
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Error String
222 *
223 * Returns the error messages as a string, wrapped in the error delimiters
224 *
225 * @access public
226 * @param string
227 * @param string
228 * @return str
229 */
230 function error_string($prefix = '', $suffix = '')
231 {
232 // No errrors, validation passes!
233 if (count($this->_error_array) === 0)
234 {
235 return '';
236 }
237
238 if ($prefix == '')
239 {
240 $prefix = $this->_error_prefix;
241 }
242
243 if ($suffix == '')
244 {
245 $suffix = $this->_error_suffix;
246 }
247
248 // Generate the error string
249 $str = '';
250 foreach ($this->_error_array as $val)
251 {
Rick Ellis9056b562008-09-09 20:42:33 +0000252 if ($val != '')
253 {
254 $str .= $prefix.$val.$suffix."\n";
255 }
Rick Ellisec1b70f2008-08-26 19:21:27 +0000256 }
257
258 return $str;
259 }
260
261 // --------------------------------------------------------------------
262
263 /**
264 * Run the Validator
265 *
266 * This function does all the work.
267 *
268 * @access public
269 * @return bool
270 */
271 function run($group = '')
272 {
273 // Do we even have any data to process? Mm?
274 if (count($_POST) == 0)
275 {
276 return FALSE;
277 }
278
279 // Does the _field_data array containing the validation rules exist?
280 // If not, we look to see if they were assigned via a config file
281 if (count($this->_field_data) == 0)
282 {
283 // No validation rules? We're done...
284 if (count($this->_config_rules) == 0)
285 {
286 return FALSE;
287 }
288
289 // Is there a validation rule for the particular URI being accessed?
290 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
291
292 if ($uri != '' AND isset($this->_config_rules[$uri]))
293 {
294 $this->set_rules($this->_config_rules[$uri]);
295 }
296 else
297 {
298 $this->set_rules($this->_config_rules);
299 }
300
301 // We're we able to set the rules correctly?
302 if (count($this->_field_data) == 0)
303 {
304 log_message('debug', "Unable to find validation rules");
305 return FALSE;
306 }
307 }
308
309 // Load the language file containing error messages
310 $this->CI->lang->load('form_validation');
311
312 // Cycle through the rules for each field, match the
313 // corresponding $_POST item and test for errors
314 foreach ($this->_field_data as $field => $row)
315 {
316 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
317 // Depending on whether the field name is an array or a string will determine where we get it from.
318
319 if ($row['is_array'] == TRUE)
320 {
321 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
322 }
323 else
324 {
Rick Ellisa5acb732008-08-27 21:43:13 +0000325 if (isset($_POST[$field]) AND $_POST[$field] != "")
Rick Ellisec1b70f2008-08-26 19:21:27 +0000326 {
327 $this->_field_data[$field]['postdata'] = $_POST[$field];
328 }
329 }
330
331 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
332 }
333
334 // Did we end up with any errors?
335 $total_errors = count($this->_error_array);
336
337 if ($total_errors > 0)
338 {
339 $this->_safe_form_data = TRUE;
340 }
341
342 // Now we need to re-set the POST data with the new, processed data
343 $this->_reset_post_array();
344
345 // No errors, validation passes!
346 if ($total_errors == 0)
347 {
348 return TRUE;
349 }
350
351 // Validation fails
352 return FALSE;
353 }
354
355 // --------------------------------------------------------------------
356
357 /**
358 * Traverse a multidimensional $_POST array index until the data is found
359 *
360 * @access private
361 * @param array
362 * @param array
363 * @param integer
364 * @return mixed
365 */
366 function _reduce_array($array, $keys, $i = 0)
367 {
368 if (is_array($array))
369 {
370 if (isset($keys[$i]))
371 {
372 if (isset($array[$keys[$i]]))
373 {
374 $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1));
375 }
376 else
377 {
378 return NULL;
379 }
380 }
381 else
382 {
383 return $array;
384 }
385 }
386
387 return $array;
388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * Re-populate the _POST array with our finalized and processed data
394 *
395 * @access private
396 * @return null
397 */
398 function _reset_post_array()
399 {
400 foreach ($this->_field_data as $field => $row)
401 {
402 if ( ! is_null($row['postdata']))
403 {
404 if ($row['is_array'] == FALSE)
405 {
406 if (isset($_POST[$row['field']]))
407 {
408 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
409 }
410 }
411 else
412 {
413 $post = '$_POST["';
414
415 if (count($row['keys']) == 1)
416 {
417 $post .= current($row['keys']);
418 $post .= '"]';
419 }
420 else
421 {
422 $i = 0;
423 foreach ($row['keys'] as $val)
424 {
425 if ($i == 0)
426 {
427 $post .= $val.'"]';
428 $i++;
429 continue;
430 }
431
432 $post .= '["'.$val.'"]';
433 }
434 }
435
436 if (is_array($row['postdata']))
437 {
438 $array = array();
439 foreach ($row['postdata'] as $k => $v)
440 {
441 $array[$k] = $this->prep_for_form($v);
442 }
443
444 $post .= ' = $array;';
445 }
446 else
447 {
448 $post .= ' = "'.$this->prep_for_form($row['postdata']).'";';
449 }
450
451 eval($post);
452 }
453 }
454 }
455 }
456
457 // --------------------------------------------------------------------
458
459 /**
460 * Executes the Validation routines
461 *
462 * @access private
463 * @param array
464 * @param array
465 * @param mixed
466 * @param integer
467 * @return mixed
468 */
469 function _execute($row, $rules, $postdata = NULL, $cycles = 0)
470 {
471 // If the $_POST data is an array we will run a recursive call
472 if (is_array($postdata))
473 {
474 foreach ($postdata as $key => $val)
475 {
476 $this->_execute($row, $rules, $val, $cycles);
477 $cycles++;
478 }
479
480 return;
481 }
Rick Ellis53b70e12008-09-20 04:48:14 +0000482
Rick Ellisec1b70f2008-08-26 19:21:27 +0000483 // --------------------------------------------------------------------
484
485 // If the field is blank, but NOT required, no further tests are necessary
Rick Ellis53b70e12008-09-20 04:48:14 +0000486 $callback = FALSE;
487 if ( ! in_array('required', $rules) AND is_null($postdata))
Rick Ellisec1b70f2008-08-26 19:21:27 +0000488 {
Rick Ellis53b70e12008-09-20 04:48:14 +0000489 // Before we bail out, does the rule contain a callback?
490 if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
491 {
492 $callback = TRUE;
493 $rules = (array('1' => $match[1]));
494 }
495 else
496 {
497 return;
498 }
Rick Ellisec1b70f2008-08-26 19:21:27 +0000499 }
500
501 // --------------------------------------------------------------------
502
503 // Isset Test. Typically this rule will only apply to checkboxes.
Rick Ellis53b70e12008-09-20 04:48:14 +0000504 if (is_null($postdata) AND $callback == FALSE)
Rick Ellisec1b70f2008-08-26 19:21:27 +0000505 {
506 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
507 {
508 if ( ! isset($this->_error_messages['isset']))
509 {
510 if (FALSE === ($line = $this->CI->lang->line('isset')))
511 {
512 $line = 'The field was not set';
513 }
514 }
515 else
516 {
Rick Ellis96fabe52008-09-23 01:18:36 +0000517 $line = $this->_error_messages[$rule];
Rick Ellisec1b70f2008-08-26 19:21:27 +0000518 }
519
520 // Build the error message
Rick Ellis277451a2008-09-20 03:42:20 +0000521 $message = sprintf($line, $this->_translate_fieldname($row['label']));
Rick Ellisec1b70f2008-08-26 19:21:27 +0000522
523 // Save the error message
524 $this->_field_data[$row['field']]['error'] = $message;
525
526 if ( ! isset($this->_error_array[$row['field']]))
527 {
528 $this->_error_array[$row['field']] = $message;
529 }
530 }
531
532 return;
533 }
534
535 // --------------------------------------------------------------------
536
537 // Cycle through each rule and run it
538 foreach ($rules As $rule)
539 {
540 $_in_array = FALSE;
541
542 // 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 }
552
553 $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 // --------------------------------------------------------------------
562
563 // Is the rule a callback?
564 $callback = FALSE;
565 if (substr($rule, 0, 9) == 'callback_')
566 {
567 $rule = substr($rule, 9);
568 $callback = TRUE;
569 }
570
571 // 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 }
579
580 // Call the function that corresponds to the rule
581 if ($callback === TRUE)
582 {
583 if ( ! method_exists($this->CI, $rule))
584 {
585 continue;
586 }
587
588 // 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 }
600
601 // 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 {
604 return;
605 }
606 }
607 else
608 {
609 if ( ! method_exists($this, $rule))
610 {
611 /*
612 * Run the native PHP function if called for
613 *
614 * If our own wrapper function doesn't exist we see
615 * if a native PHP function does. Users can use
616 * any native PHP function call that has one param.
617 */
618 if (function_exists($rule))
619 {
620 $result = $rule($postdata);
621
622 if ($_in_array == TRUE)
623 {
624 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
625 }
626 else
627 {
628 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
629 }
630 }
631
632 continue;
633 }
634
635 $result = $this->$rule($postdata, $param);
636
637 if ($_in_array == TRUE)
638 {
639 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
640 }
641 else
642 {
643 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
644 }
645 }
646
647 // Did the rule test negatively? If so, grab the error.
648 if ($result === FALSE)
Rick Ellis96fabe52008-09-23 01:18:36 +0000649 {
Rick Ellisec1b70f2008-08-26 19:21:27 +0000650 if ( ! isset($this->_error_messages[$rule]))
651 {
652 if (FALSE === ($line = $this->CI->lang->line($rule)))
653 {
654 $line = 'Unable to access an error message corresponding to your field name.';
655 }
656 }
657 else
658 {
Rick Ellis96fabe52008-09-23 01:18:36 +0000659 $line = $this->_error_messages[$rule];
660 }
Rick Ellisec1b70f2008-08-26 19:21:27 +0000661
662 // Build the error message
Rick Ellis277451a2008-09-20 03:42:20 +0000663 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
Rick Ellisec1b70f2008-08-26 19:21:27 +0000664
665 // Save the error message
666 $this->_field_data[$row['field']]['error'] = $message;
667
668 if ( ! isset($this->_error_array[$row['field']]))
669 {
670 $this->_error_array[$row['field']] = $message;
671 }
672
673 return;
674 }
675 }
Rick Ellis277451a2008-09-20 03:42:20 +0000676 }
677
678 // --------------------------------------------------------------------
679
680 /**
681 * Translate a field name
682 *
683 * @access private
684 * @param string the field name
685 * @return string
686 */
687 function _translate_fieldname($fieldname)
688 {
689 // Do we need to translate the field name?
690 // We look for the prefix lang: to determine this
691 if (substr($fieldname, 0, 5) == 'lang:')
692 {
Rick Ellis06695402008-09-22 21:58:10 +0000693 // Grab the variable
Rick Ellisfe61d632008-09-22 21:55:12 +0000694 $line = substr($fieldname, 5);
Rick Ellis06695402008-09-22 21:58:10 +0000695
696 // Translate it
Rick Ellisfe61d632008-09-22 21:55:12 +0000697 $fieldname = $this->CI->lang->line($line);
Rick Ellis277451a2008-09-20 03:42:20 +0000698
699 // Were we able to translate the field name?
Rick Ellisfe61d632008-09-22 21:55:12 +0000700 if ($fieldname === FALSE)
701 {
Rick Ellis06695402008-09-22 21:58:10 +0000702 return $line;
Rick Ellisfe61d632008-09-22 21:55:12 +0000703 }
704 else
705 {
Rick Ellis193743a2008-09-22 21:55:41 +0000706 return $fieldname;
Rick Ellisfe61d632008-09-22 21:55:12 +0000707 }
Rick Ellis277451a2008-09-20 03:42:20 +0000708 }
709
Rick Ellis96fabe52008-09-23 01:18:36 +0000710 return $fieldname;
Rick Ellis277451a2008-09-20 03:42:20 +0000711 }
Rick Ellisec1b70f2008-08-26 19:21:27 +0000712
713 // --------------------------------------------------------------------
714
715 /**
716 * Get the value from a form
717 *
718 * Permits you to repopulate a form field with the value it was submitted
719 * with, or, if that value doesn't exist, with the default
720 *
721 * @access public
722 * @param string the field name
723 * @param string
724 * @return void
725 */
726 function set_value($field = '', $default = '')
727 {
728 if ( ! isset($this->_field_data[$field]))
729 {
730 return $default;
731 }
732
733 return $this->_field_data[$field]['postdata'];
734 }
735
736 // --------------------------------------------------------------------
737
738 /**
739 * Set Select
740 *
741 * Enables pull-down lists to be set to the value the user
742 * selected in the event of an error
743 *
744 * @access public
745 * @param string
746 * @param string
747 * @return string
748 */
749 function set_select($field = '', $value = '', $default = FALSE)
750 {
751 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
752 {
753 if ($default === TRUE AND count($this->_field_data) === 0)
754 {
755 return ' selected="selected"';
756 }
757 return '';
758 }
759
760 $field = $this->_field_data[$field]['postdata'];
761
762 if (is_array($field))
763 {
Rick Ellis32f84f12008-09-20 04:21:27 +0000764 if ( ! in_array($value, $field))
Rick Ellisec1b70f2008-08-26 19:21:27 +0000765 {
766 return '';
767 }
768 }
769 else
770 {
771 if (($field == '' OR $value == '') OR ($field != $value))
772 {
773 return '';
774 }
775 }
776
777 return ' selected="selected"';
778 }
779
780 // --------------------------------------------------------------------
781
782 /**
783 * Set Radio
784 *
785 * Enables radio buttons to be set to the value the user
786 * selected in the event of an error
787 *
788 * @access public
789 * @param string
790 * @param string
791 * @return string
792 */
793 function set_radio($field = '', $value = '', $default = FALSE)
794 {
795 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
796 {
797 if ($default === TRUE AND count($this->_field_data) === 0)
798 {
799 return ' checked="checked"';
800 }
801 return '';
802 }
803
804 $field = $this->_field_data[$field]['postdata'];
805
806 if (is_array($field))
807 {
Rick Ellis32f84f12008-09-20 04:21:27 +0000808 if ( ! in_array($value, $field))
Rick Ellisec1b70f2008-08-26 19:21:27 +0000809 {
810 return '';
811 }
812 }
813 else
814 {
815 if (($field == '' OR $value == '') OR ($field != $value))
816 {
817 return '';
818 }
819 }
820
821 return ' checked="checked"';
822 }
823
824 // --------------------------------------------------------------------
825
826 /**
827 * Set Checkbox
828 *
829 * Enables checkboxes to be set to the value the user
830 * selected in the event of an error
831 *
832 * @access public
833 * @param string
834 * @param string
835 * @return string
836 */
837 function set_checkbox($field = '', $value = '', $default = FALSE)
838 {
839 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
840 {
841 if ($default === TRUE AND count($this->_field_data) === 0)
842 {
843 return ' checked="checked"';
844 }
845 return '';
846 }
847
848 $field = $this->_field_data[$field]['postdata'];
849
850 if (is_array($field))
851 {
Rick Ellis32f84f12008-09-20 04:21:27 +0000852 if ( ! in_array($value, $field))
Rick Ellisec1b70f2008-08-26 19:21:27 +0000853 {
854 return '';
855 }
856 }
857 else
858 {
859 if (($field == '' OR $value == '') OR ($field != $value))
860 {
861 return '';
862 }
863 }
864
865 return ' checked="checked"';
866 }
867
868 // --------------------------------------------------------------------
869
870 /**
871 * Required
872 *
873 * @access public
874 * @param string
875 * @return bool
876 */
877 function required($str)
878 {
879 if ( ! is_array($str))
880 {
881 return (trim($str) == '') ? FALSE : TRUE;
882 }
883 else
884 {
885 return ( ! empty($str));
886 }
887 }
888
889 // --------------------------------------------------------------------
890
891 /**
892 * Match one field to another
893 *
894 * @access public
895 * @param string
896 * @param field
897 * @return bool
898 */
899 function matches($str, $field)
900 {
901 if ( ! isset($_POST[$field]))
902 {
903 return FALSE;
904 }
905
906 $field = $_POST[$field];
907
908 return ($str !== $field) ? FALSE : TRUE;
909 }
910
911 // --------------------------------------------------------------------
912
913 /**
914 * Minimum Length
915 *
916 * @access public
917 * @param string
918 * @param value
919 * @return bool
920 */
921 function min_length($str, $val)
922 {
923 if (preg_match("/[^0-9]/", $val))
924 {
925 return FALSE;
926 }
927
928 return (strlen($str) < $val) ? FALSE : TRUE;
929 }
930
931 // --------------------------------------------------------------------
932
933 /**
934 * Max Length
935 *
936 * @access public
937 * @param string
938 * @param value
939 * @return bool
940 */
941 function max_length($str, $val)
942 {
943 if (preg_match("/[^0-9]/", $val))
944 {
945 return FALSE;
946 }
947
948 return (strlen($str) > $val) ? FALSE : TRUE;
949 }
950
951 // --------------------------------------------------------------------
952
953 /**
954 * Exact Length
955 *
956 * @access public
957 * @param string
958 * @param value
959 * @return bool
960 */
961 function exact_length($str, $val)
962 {
963 if (preg_match("/[^0-9]/", $val))
964 {
965 return FALSE;
966 }
967
968 return (strlen($str) != $val) ? FALSE : TRUE;
969 }
970
971 // --------------------------------------------------------------------
972
973 /**
974 * Valid Email
975 *
976 * @access public
977 * @param string
978 * @return bool
979 */
980 function valid_email($str)
981 {
982 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
983 }
984
985 // --------------------------------------------------------------------
986
987 /**
988 * Valid Emails
989 *
990 * @access public
991 * @param string
992 * @return bool
993 */
994 function valid_emails($str)
995 {
996 if (strpos($str, ',') === FALSE)
997 {
998 return $this->valid_email(trim($str));
999 }
1000
1001 foreach(explode(',', $str) as $email)
1002 {
1003 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
1004 {
1005 return FALSE;
1006 }
1007 }
1008
1009 return TRUE;
1010 }
1011
1012 // --------------------------------------------------------------------
1013
1014 /**
1015 * Validate IP Address
1016 *
1017 * @access public
1018 * @param string
1019 * @return string
1020 */
1021 function valid_ip($ip)
1022 {
1023 return $this->CI->input->valid_ip($ip);
1024 }
1025
1026 // --------------------------------------------------------------------
1027
1028 /**
1029 * Alpha
1030 *
1031 * @access public
1032 * @param string
1033 * @return bool
1034 */
1035 function alpha($str)
1036 {
1037 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
1038 }
1039
1040 // --------------------------------------------------------------------
1041
1042 /**
1043 * Alpha-numeric
1044 *
1045 * @access public
1046 * @param string
1047 * @return bool
1048 */
1049 function alpha_numeric($str)
1050 {
1051 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
1052 }
1053
1054 // --------------------------------------------------------------------
1055
1056 /**
1057 * Alpha-numeric with underscores and dashes
1058 *
1059 * @access public
1060 * @param string
1061 * @return bool
1062 */
1063 function alpha_dash($str)
1064 {
1065 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
1066 }
1067
1068 // --------------------------------------------------------------------
1069
1070 /**
1071 * Numeric
1072 *
1073 * @access public
1074 * @param string
1075 * @return bool
1076 */
1077 function numeric($str)
1078 {
1079 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
1080
1081 }
1082
1083 // --------------------------------------------------------------------
1084
1085 /**
1086 * Is Numeric
1087 *
1088 * @access public
1089 * @param string
1090 * @return bool
1091 */
1092 function is_numeric($str)
1093 {
1094 return ( ! is_numeric($str)) ? FALSE : TRUE;
1095 }
1096
1097 // --------------------------------------------------------------------
1098
1099 /**
1100 * Integer
1101 *
1102 * @access public
1103 * @param string
1104 * @return bool
1105 */
1106 function integer($str)
1107 {
1108 return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
1109 }
1110
1111 // --------------------------------------------------------------------
1112
1113 /**
1114 * Is a Natural number (0,1,2,3, etc.)
1115 *
1116 * @access public
1117 * @param string
1118 * @return bool
1119 */
1120 function is_natural($str)
1121 {
1122 return (bool)preg_match( '/^[0-9]+$/', $str);
1123 }
1124
1125 // --------------------------------------------------------------------
1126
1127 /**
1128 * Is a Natural number, but not a zero (1,2,3, etc.)
1129 *
1130 * @access public
1131 * @param string
1132 * @return bool
1133 */
1134 function is_natural_no_zero($str)
1135 {
1136 if ( ! preg_match( '/^[0-9]+$/', $str))
1137 {
1138 return FALSE;
1139 }
1140
1141 if ($str == 0)
1142 {
1143 return FALSE;
1144 }
1145
1146 return TRUE;
1147 }
1148
1149 // --------------------------------------------------------------------
1150
1151 /**
1152 * Valid Base64
1153 *
1154 * Tests a string for characters outside of the Base64 alphabet
1155 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1156 *
1157 * @access public
1158 * @param string
1159 * @return bool
1160 */
1161 function valid_base64($str)
1162 {
1163 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1164 }
1165
1166 // --------------------------------------------------------------------
1167
1168 /**
1169 * Prep data for form
1170 *
1171 * This function allows HTML to be safely shown in a form.
1172 * Special characters are converted.
1173 *
1174 * @access public
1175 * @param string
1176 * @return string
1177 */
1178 function prep_for_form($data = '')
1179 {
1180 if (is_array($data))
1181 {
1182 foreach ($data as $key => $val)
1183 {
1184 $data[$key] = $this->prep_for_form($val);
1185 }
1186
1187 return $data;
1188 }
1189
1190 if ($this->_safe_form_data == FALSE OR $data === '')
1191 {
1192 return $data;
1193 }
1194
1195 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1196 }
1197
1198 // --------------------------------------------------------------------
1199
1200 /**
1201 * Prep URL
1202 *
1203 * @access public
1204 * @param string
1205 * @return string
1206 */
1207 function prep_url($str = '')
1208 {
1209 if ($str == 'http://' OR $str == '')
1210 {
1211 return '';
1212 }
1213
1214 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
1215 {
1216 $str = 'http://'.$str;
1217 }
1218
1219 return $str;
1220 }
1221
1222 // --------------------------------------------------------------------
1223
1224 /**
1225 * Strip Image Tags
1226 *
1227 * @access public
1228 * @param string
1229 * @return string
1230 */
1231 function strip_image_tags($str)
1232 {
1233 return $this->CI->input->strip_image_tags($str);
1234 }
1235
1236 // --------------------------------------------------------------------
1237
1238 /**
1239 * XSS Clean
1240 *
1241 * @access public
1242 * @param string
1243 * @return string
1244 */
1245 function xss_clean($str)
1246 {
1247 return $this->CI->input->xss_clean($str);
1248 }
1249
1250 // --------------------------------------------------------------------
1251
1252 /**
1253 * Convert PHP tags to entities
1254 *
1255 * @access public
1256 * @param string
1257 * @return string
1258 */
1259 function encode_php_tags($str)
1260 {
1261 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
1262 }
1263
1264}
1265// END Form Validation Class
1266
1267/* End of file Form_validation.php */
1268/* Location: ./system/libraries/Form_validation.php */