1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Validation
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/libraries/validation.html
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>';
46 function CI_Validation()
48 $this->CI =& get_instance();
50 if (function_exists('mb_internal_encoding'))
52 mb_internal_encoding($this->CI->config->item('charset'));
55 log_message('debug', "Validation Class Initialized");
58 // --------------------------------------------------------------------
63 * This function takes an array of field names as input
64 * and generates class variables with the same name, which will
65 * either be blank or contain the $_POST value corresponding to it
72 function set_fields($data = '', $field = '')
76 if (count($this->_fields) == 0)
83 if ( ! is_array($data))
85 $data = array($data => $field);
90 $this->_fields = $data;
94 foreach($this->_fields as $key => $val)
96 $this->$key = ( ! isset($_POST[$key])) ? '' : $this->prep_for_form($_POST[$key]);
98 $error = $key.'_error';
99 if ( ! isset($this->$error))
106 // --------------------------------------------------------------------
111 * This function takes an array of field names and validation
112 * rules as input ad simply stores is for use later.
119 function set_rules($data, $rules = '')
121 if ( ! is_array($data))
126 $data = array($data => $rules);
129 foreach ($data as $key => $val)
131 $this->_rules[$key] = $val;
135 // --------------------------------------------------------------------
140 * Lets users set their own error messages on the fly. Note: The key
141 * name has to match the function name that it corresponds to.
148 function set_message($lang, $val = '')
150 if ( ! is_array($lang))
152 $lang = array($lang => $val);
155 $this->_error_messages = array_merge($this->_error_messages, $lang);
158 // --------------------------------------------------------------------
161 * Set The Error Delimiter
163 * Permits a prefix/suffix to be added to each error message
170 function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
172 $this->_error_prefix = $prefix;
173 $this->_error_suffix = $suffix;
176 // --------------------------------------------------------------------
181 * This function does all the work.
188 // Do we even have any data to process? Mm?
189 if (count($_POST) == 0 OR count($this->_rules) == 0)
194 // Load the language file containing error messages
195 $this->CI->lang->load('validation');
197 // Cycle through the rules and test for errors
198 foreach ($this->_rules as $field => $rules)
200 //Explode out the rules!
201 $ex = explode('|', $rules);
203 // Is the field required? If not, if the field is blank we'll move on to the next test
204 if ( ! in_array('required', $ex, TRUE))
206 if ( ! isset($_POST[$field]) OR $_POST[$field] == '')
213 * Are we dealing with an "isset" rule?
215 * Before going further, we'll see if one of the rules
216 * is to check whether the item is set (typically this
217 * applies only to checkboxes). If so, we'll
218 * test for it here since there's not reason to go
221 if ( ! isset($_POST[$field]))
223 if (in_array('isset', $ex, TRUE) OR in_array('required', $ex))
225 if ( ! isset($this->_error_messages['isset']))
227 if (FALSE === ($line = $this->CI->lang->line('isset')))
229 $line = 'The field was not set';
234 $line = $this->_error_messages['isset'];
237 // Build the error message
238 $mfield = ( ! isset($this->_fields[$field])) ? $field : $this->_fields[$field];
239 $message = sprintf($line, $mfield);
241 // Set the error variable. Example: $this->username_error
242 $error = $field.'_error';
243 $this->$error = $this->_error_prefix.$message.$this->_error_suffix;
244 $this->_error_array[] = $message;
251 * Set the current field
253 * The various prepping functions need to know the
254 * current field name so they can do this:
256 * $_POST[$this->_current_field] == 'bla bla';
258 $this->_current_field = $field;
260 // Cycle through the rules!
261 foreach ($ex As $rule)
263 // Is the rule a callback?
265 if (substr($rule, 0, 9) == 'callback_')
267 $rule = substr($rule, 9);
271 // Strip the parameter (if exists) from the rule
272 // Rules can contain a parameter: max_length[5]
274 if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
280 // Call the function that corresponds to the rule
281 if ($callback === TRUE)
283 if ( ! method_exists($this->CI, $rule))
288 $result = $this->CI->$rule($_POST[$field], $param);
290 // If the field isn't required and we just processed a callback we'll move on...
291 if ( ! in_array('required', $ex, TRUE) AND $result !== FALSE)
299 if ( ! method_exists($this, $rule))
302 * Run the native PHP function if called for
304 * If our own wrapper function doesn't exist we see
305 * if a native PHP function does. Users can use
306 * any native PHP function call that has one param.
308 if (function_exists($rule))
310 $_POST[$field] = $rule($_POST[$field]);
311 $this->$field = $_POST[$field];
317 $result = $this->$rule($_POST[$field], $param);
320 // Did the rule test negatively? If so, grab the error.
321 if ($result === FALSE)
323 if ( ! isset($this->_error_messages[$rule]))
325 if (FALSE === ($line = $this->CI->lang->line($rule)))
327 $line = 'Unable to access an error message corresponding to your field name.';
332 $line = $this->_error_messages[$rule];
335 // Build the error message
336 $mfield = ( ! isset($this->_fields[$field])) ? $field : $this->_fields[$field];
337 $mparam = ( ! isset($this->_fields[$param])) ? $param : $this->_fields[$param];
338 $message = sprintf($line, $mfield, $mparam);
340 // Set the error variable. Example: $this->username_error
341 $error = $field.'_error';
342 $this->$error = $this->_error_prefix.$message.$this->_error_suffix;
344 // Add the error to the error array
345 $this->_error_array[] = $message;
352 $total_errors = count($this->_error_array);
355 * Recompile the class variables
357 * If any prepping functions were called the $_POST data
358 * might now be different then the corresponding class
359 * variables so we'll set them anew.
361 if ($total_errors > 0)
363 $this->_safe_form_data = TRUE;
368 // Did we end up with any errors?
369 if ($total_errors == 0)
374 // Generate the error string
375 foreach ($this->_error_array as $val)
377 $this->error_string .= $this->_error_prefix.$val.$this->_error_suffix."\n";
383 // --------------------------------------------------------------------
392 function required($str)
394 if ( ! is_array($str))
396 return (trim($str) == '') ? FALSE : TRUE;
400 return ( ! empty($str));
404 // --------------------------------------------------------------------
407 * Match one field to another
414 function matches($str, $field)
416 if ( ! isset($_POST[$field]))
421 return ($str !== $_POST[$field]) ? FALSE : TRUE;
424 // --------------------------------------------------------------------
434 function min_length($str, $val)
436 if (preg_match("/[^0-9]/", $val))
441 if (function_exists('mb_strlen'))
443 return (mb_strlen($str) < $val) ? FALSE : TRUE;
446 return (strlen($str) < $val) ? FALSE : TRUE;
449 // --------------------------------------------------------------------
459 function max_length($str, $val)
461 if (preg_match("/[^0-9]/", $val))
466 if (function_exists('mb_strlen'))
468 return (mb_strlen($str) > $val) ? FALSE : TRUE;
471 return (strlen($str) > $val) ? FALSE : TRUE;
474 // --------------------------------------------------------------------
484 function exact_length($str, $val)
486 if (preg_match("/[^0-9]/", $val))
491 if (function_exists('mb_strlen'))
493 return (mb_strlen($str) != $val) ? FALSE : TRUE;
496 return (strlen($str) != $val) ? FALSE : TRUE;
499 // --------------------------------------------------------------------
508 function valid_email($str)
510 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
513 // --------------------------------------------------------------------
522 function valid_emails($str)
524 if (strpos($str, ',') === FALSE)
526 return $this->valid_email(trim($str));
529 foreach(explode(',', $str) as $email)
531 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
540 // --------------------------------------------------------------------
543 * Validate IP Address
549 function valid_ip($ip)
551 return $this->CI->input->valid_ip($ip);
554 // --------------------------------------------------------------------
565 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
568 // --------------------------------------------------------------------
577 function alpha_numeric($str)
579 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
582 // --------------------------------------------------------------------
585 * Alpha-numeric with underscores and dashes
591 function alpha_dash($str)
593 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
596 // --------------------------------------------------------------------
605 function numeric($str)
607 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
611 // --------------------------------------------------------------------
620 function is_numeric($str)
622 return ( ! is_numeric($str)) ? FALSE : TRUE;
625 // --------------------------------------------------------------------
634 function integer($str)
636 return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
639 // --------------------------------------------------------------------
642 * Is a Natural number (0,1,2,3, etc.)
648 function is_natural($str)
650 return (bool)preg_match( '/^[0-9]+$/', $str);
653 // --------------------------------------------------------------------
656 * Is a Natural number, but not a zero (1,2,3, etc.)
662 function is_natural_no_zero($str)
664 if ( ! preg_match( '/^[0-9]+$/', $str))
677 // --------------------------------------------------------------------
682 * Tests a string for characters outside of the Base64 alphabet
683 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
689 function valid_base64($str)
691 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
694 // --------------------------------------------------------------------
699 * Enables pull-down lists to be set to the value the user
700 * selected in the event of an error
707 function set_select($field = '', $value = '')
709 if ($field == '' OR $value == '' OR ! isset($_POST[$field]))
714 if ($_POST[$field] == $value)
716 return ' selected="selected"';
720 // --------------------------------------------------------------------
725 * Enables radio buttons to be set to the value the user
726 * selected in the event of an error
733 function set_radio($field = '', $value = '')
735 if ($field == '' OR $value == '' OR ! isset($_POST[$field]))
740 if ($_POST[$field] == $value)
742 return ' checked="checked"';
746 // --------------------------------------------------------------------
751 * Enables checkboxes to be set to the value the user
752 * selected in the event of an error
759 function set_checkbox($field = '', $value = '')
761 if ($field == '' OR $value == '' OR ! isset($_POST[$field]))
766 if ($_POST[$field] == $value)
768 return ' checked="checked"';
772 // --------------------------------------------------------------------
777 * This function allows HTML to be safely shown in a form.
778 * Special characters are converted.
784 function prep_for_form($data = '')
788 foreach ($data as $key => $val)
790 $data[$key] = $this->prep_for_form($val);
796 if ($this->_safe_form_data == FALSE OR $data == '')
801 return str_replace(array("'", '"', '<', '>'), array("'", """, '<', '>'), stripslashes($data));
804 // --------------------------------------------------------------------
813 function prep_url($str = '')
815 if ($str == 'http://' OR $str == '')
817 $_POST[$this->_current_field] = '';
821 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
823 $str = 'http://'.$str;
826 $_POST[$this->_current_field] = $str;
829 // --------------------------------------------------------------------
838 function strip_image_tags($str)
840 $_POST[$this->_current_field] = $this->CI->input->strip_image_tags($str);
843 // --------------------------------------------------------------------
852 function xss_clean($str)
854 $_POST[$this->_current_field] = $this->CI->input->xss_clean($str);
857 // --------------------------------------------------------------------
860 * Convert PHP tags to entities
866 function encode_php_tags($str)
868 $_POST[$this->_current_field] = str_replace(array('<?php', '<?PHP', '<?', '?>'), array('<?php', '<?PHP', '<?', '?>'), $str);
872 // END Validation Class
874 /* End of file Validation.php */
875 /* Location: ./system/libraries/Validation.php */