blob: 1bd55499a6d42fa64e93db861f31b9d04b90a6d9 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Eric Barnescccde962011-12-04 00:01:17 -05008 *
Instructor, BCIT0e59db62019-01-01 08:34:36 -08009 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
Eric Barnescccde962011-12-04 00:01:17 -050010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Instructor, BCIT0e59db62019-01-01 08:34:36 -080032 * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33 * @license https://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Form Validation Class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/libraries/form_validation.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020050
Timothy Warren0688ac92012-04-20 10:25:04 -040051 /**
52 * Reference to the CodeIgniter instance
53 *
54 * @var object
55 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010056 protected $CI;
Derek Allard2067d1a2008-11-13 22:59:24 +000057
Timothy Warren0688ac92012-04-20 10:25:04 -040058 /**
59 * Validation data for the current form submission
60 *
61 * @var array
62 */
Andrey Andreev56454792012-05-17 14:32:19 +030063 protected $_field_data = array();
64
Timothy Warren0688ac92012-04-20 10:25:04 -040065 /**
66 * Validation rules for the current form
67 *
68 * @var array
69 */
Andrey Andreev56454792012-05-17 14:32:19 +030070 protected $_config_rules = array();
71
Timothy Warren0688ac92012-04-20 10:25:04 -040072 /**
73 * Array of validation errors
74 *
75 * @var array
76 */
Andrey Andreev78f55772012-04-03 19:59:08 +030077 protected $_error_array = array();
Andrey Andreev56454792012-05-17 14:32:19 +030078
Timothy Warren0688ac92012-04-20 10:25:04 -040079 /**
80 * Array of custom error messages
81 *
82 * @var array
83 */
Andrey Andreev78f55772012-04-03 19:59:08 +030084 protected $_error_messages = array();
Andrey Andreev56454792012-05-17 14:32:19 +030085
Timothy Warren0688ac92012-04-20 10:25:04 -040086 /**
87 * Start tag for error wrapping
88 *
89 * @var string
90 */
Andrey Andreev78f55772012-04-03 19:59:08 +030091 protected $_error_prefix = '<p>';
Andrey Andreev56454792012-05-17 14:32:19 +030092
Timothy Warren0688ac92012-04-20 10:25:04 -040093 /**
94 * End tag for error wrapping
Andrey Andreev56454792012-05-17 14:32:19 +030095 *
Timothy Warren0688ac92012-04-20 10:25:04 -040096 * @var string
97 */
Andrey Andreev78f55772012-04-03 19:59:08 +030098 protected $_error_suffix = '</p>';
Andrey Andreev56454792012-05-17 14:32:19 +030099
Timothy Warren0688ac92012-04-20 10:25:04 -0400100 /**
101 * Custom error message
102 *
103 * @var string
104 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300105 protected $error_string = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300106
Timothy Warren0688ac92012-04-20 10:25:04 -0400107 /**
108 * Whether the form data has been validated as safe
109 *
110 * @var bool
111 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300112 protected $_safe_form_data = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300113
Timothy Warren0688ac92012-04-20 10:25:04 -0400114 /**
115 * Custom data to validate
116 *
117 * @var array
118 */
Andrey Andreevcff35802012-11-26 15:49:52 +0200119 public $validation_data = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000120
Timothy Warren0688ac92012-04-20 10:25:04 -0400121 /**
122 * Initialize Form_Validation class
123 *
Andrey Andreev56454792012-05-17 14:32:19 +0300124 * @param array $rules
125 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -0400126 */
Greg Akera9263282010-11-10 15:26:43 -0600127 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +0200128 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200130
Mike Funk326a5e72012-02-24 10:06:28 -0500131 // applies delimiters set in config file.
Mike Funk7f42d062012-03-08 09:00:57 -0500132 if (isset($rules['error_prefix']))
133 {
134 $this->_error_prefix = $rules['error_prefix'];
135 unset($rules['error_prefix']);
136 }
137 if (isset($rules['error_suffix']))
138 {
139 $this->_error_suffix = $rules['error_suffix'];
140 unset($rules['error_suffix']);
141 }
Andrey Andreev31cf46e2012-03-20 15:48:00 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 // Validation rules can be stored in a config file.
144 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // Automatically load the form helper
147 $this->CI->load->helper('form');
148
Andrey Andreev90726b82015-01-20 12:39:22 +0200149 log_message('info', 'Form Validation Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 /**
155 * Set Rules
156 *
157 * This function takes an array of field names and validation
Andrey Andreev4b90a372014-03-10 10:24:24 +0200158 * rules as input, any custom error messages, validates the info,
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600159 * and stores it
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400161 * @param mixed $field
162 * @param string $label
163 * @param mixed $rules
Ahmedul Haque Abid0742fad2014-01-09 07:51:10 +0600164 * @param array $errors
Andrew Podner4296a652012-12-17 07:51:15 -0500165 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 */
Andrey Andreev4b90a372014-03-10 10:24:24 +0200167 public function set_rules($field, $label = '', $rules = array(), $errors = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +0000170 // or a validation array has not been specified
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200171 if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Greg Aker9f9af602010-11-10 15:41:51 -0600173 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
tiyowanc2acb232012-03-14 21:24:00 +0400176 // If an array was passed via the first parameter instead of individual string
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 // values we cycle through it and recursively call this function.
178 if (is_array($field))
179 {
180 foreach ($field as $row)
181 {
182 // Houston, we have a problem...
Andrey Andreev78f55772012-04-03 19:59:08 +0300183 if ( ! isset($row['field'], $row['rules']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
185 continue;
186 }
187
188 // If the field label wasn't passed we use the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300189 $label = isset($row['label']) ? $row['label'] : $row['field'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000190
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600191 // Add the custom error message array
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600192 $errors = (isset($row['errors']) && is_array($row['errors'])) ? $row['errors'] : array();
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 // Here we go!
Ahmedul Haque Abidbc1cbad2014-01-09 07:53:34 +0600195 $this->set_rules($row['field'], $label, $row['rules'], $errors);
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300197
Greg Aker9f9af602010-11-10 15:41:51 -0600198 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Andrey Andreev475dfac2015-04-07 00:07:04 +0300201 // No fields or no rules? Nothing to do...
202 if ( ! is_string($field) OR $field === '' OR empty($rules))
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Greg Aker9f9af602010-11-10 15:41:51 -0600204 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
Andrey Andreev4b90a372014-03-10 10:24:24 +0200206 elseif ( ! is_array($rules))
207 {
208 // BC: Convert pipe-separated rules string to an array
Andrey Andreev475dfac2015-04-07 00:07:04 +0300209 if ( ! is_string($rules))
Andrey Andreev4b90a372014-03-10 10:24:24 +0200210 {
211 return $this;
212 }
Andrey Andreev475dfac2015-04-07 00:07:04 +0300213
richcc936352015-09-09 15:52:26 -0400214 $rules = preg_split('/\|(?![^\[]*\])/', $rules);
Andrey Andreev4b90a372014-03-10 10:24:24 +0200215 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000216
217 // If the field label wasn't passed we use the field name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100218 $label = ($label === '') ? $field : $label;
Derek Allard2067d1a2008-11-13 22:59:24 +0000219
Andrey Andreevfde170c2014-03-10 19:55:11 +0200220 $indexes = array();
221
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200222 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200223 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev4b90a372014-03-10 10:24:24 +0200224 if (($is_array = (bool) preg_match_all('/\[(.*?)\]/', $field, $matches)) === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200225 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200226 sscanf($field, '%[^[][', $indexes[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000227
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200228 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100230 if ($matches[1][$i] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200232 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
234 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
237 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300239 'field' => $field,
240 'label' => $label,
241 'rules' => $rules,
Ahmedul Haque Abid7945d302014-01-09 16:50:23 +0600242 'errors' => $errors,
Andrey Andreev56454792012-05-17 14:32:19 +0300243 'is_array' => $is_array,
244 'keys' => $indexes,
245 'postdata' => NULL,
246 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000247 );
Greg Aker9f9af602010-11-10 15:41:51 -0600248
249 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
251
252 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 /**
JonoB099c4782012-03-04 14:37:30 +0000255 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200256 *
JonoB099c4782012-03-04 14:37:30 +0000257 * If an array is set through this method, then this array will
258 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200259 *
260 * Note that if you are validating multiple arrays, then the
261 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000262 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200263 *
264 * @param array $data
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200265 * @return CI_Form_validation
JonoB099c4782012-03-04 14:37:30 +0000266 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200267 public function set_data(array $data)
JonoB099c4782012-03-04 14:37:30 +0000268 {
Andrey Andreeva4712f52014-01-06 11:25:46 +0200269 if ( ! empty($data))
JonoB099c4782012-03-04 14:37:30 +0000270 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200271 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000272 }
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200273
274 return $this;
JonoB099c4782012-03-04 14:37:30 +0000275 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200276
JonoB099c4782012-03-04 14:37:30 +0000277 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000278
279 /**
280 * Set Error Message
281 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300282 * Lets users set their own error messages on the fly. Note:
283 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300285 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500287 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100289 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 if ( ! is_array($lang))
292 {
293 $lang = array($lang => $val);
294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600297 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 /**
303 * Set The Error Delimiter
304 *
305 * Permits a prefix/suffix to be added to each error message
306 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * @param string
308 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500309 * @return CI_Form_validation
Barry Mienydd671972010-10-04 16:33:58 +0200310 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100311 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
313 $this->_error_prefix = $prefix;
314 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600315 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
317
318 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 /**
321 * Get Error Message
322 *
323 * Gets the error message associated with a particular field
324 *
Andrey Andreeva4712f52014-01-06 11:25:46 +0200325 * @param string $field Field name
326 * @param string $prefix HTML start tag
Andrey Andreev868301a2014-01-06 12:29:50 +0200327 * @param string $suffix HTML end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300328 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200329 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200330 public function error($field, $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200331 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300332 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 return '';
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Alex Bilbied261b1e2012-06-02 11:12:16 +0100337 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 $prefix = $this->_error_prefix;
340 }
341
Alex Bilbied261b1e2012-06-02 11:12:16 +0100342 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
344 $suffix = $this->_error_suffix;
345 }
346
347 return $prefix.$this->_field_data[$field]['error'].$suffix;
348 }
349
350 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100353 * Get Array of Error Messages
354 *
355 * Returns the error messages as an array
356 *
357 * @return array
358 */
359 public function error_array()
360 {
361 return $this->_error_array;
362 }
363
364 // --------------------------------------------------------------------
365
366 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 * Error String
368 *
369 * Returns the error messages as a string, wrapped in the error delimiters
370 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * @param string
372 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300373 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200374 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100375 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
vlakoff35672462013-02-15 01:36:04 +0100377 // No errors, validation passes!
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 if (count($this->_error_array) === 0)
379 {
380 return '';
381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
Alex Bilbied261b1e2012-06-02 11:12:16 +0100383 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
385 $prefix = $this->_error_prefix;
386 }
387
Alex Bilbied261b1e2012-06-02 11:12:16 +0100388 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 $suffix = $this->_error_suffix;
391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // Generate the error string
394 $str = '';
395 foreach ($this->_error_array as $val)
396 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100397 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
399 $str .= $prefix.$val.$suffix."\n";
400 }
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 return $str;
404 }
405
406 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 /**
409 * Run the Validator
410 *
411 * This function does all the work.
412 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400413 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200415 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100416 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Andrey Andreevce8fa5a2015-12-14 12:57:09 +0200418 $validation_array = empty($this->validation_data)
419 ? $_POST
420 : $this->validation_data;
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // Does the _field_data array containing the validation rules exist?
423 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200424 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500426 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200427 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
429 return FALSE;
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200432 if (empty($group))
433 {
434 // Is there a validation rule for the particular URI being accessed?
435 $group = trim($this->CI->uri->ruri_string(), '/');
436 isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method;
437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200439 $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules);
Barry Mienydd671972010-10-04 16:33:58 +0200440
Andrey Andreev901573c2012-01-11 01:40:48 +0200441 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200442 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200444 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 return FALSE;
446 }
447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 // Load the language file containing error messages
450 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200451
Andrey Andreev751f2472012-11-03 18:26:27 +0200452 // Cycle through the rules for each field and match the corresponding $validation_data item
Andrey Andreev55a40b72016-03-12 15:43:22 +0200453 foreach ($this->_field_data as $field => &$row)
Barry Mienydd671972010-10-04 16:33:58 +0200454 {
Andrey Andreev751f2472012-11-03 18:26:27 +0200455 // Fetch the data from the validation_data array item and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 // Depending on whether the field name is an array or a string will determine where we get it from.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200457 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
JonoB099c4782012-03-04 14:37:30 +0000459 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
Andrey Andreevb137d232015-04-29 11:44:38 +0300461 elseif (isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300463 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Andrey Andreev751f2472012-11-03 18:26:27 +0200465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Andrey Andreev751f2472012-11-03 18:26:27 +0200467 // Execute validation rules
468 // Note: A second foreach (for now) is required in order to avoid false-positives
469 // for rules like 'matches', which correlate to other validation fields.
Andrey Andreev55a40b72016-03-12 15:43:22 +0200470 foreach ($this->_field_data as $field => &$row)
Andrey Andreev751f2472012-11-03 18:26:27 +0200471 {
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300472 // Don't try to validate if we have no rules set
473 if (empty($row['rules']))
474 {
475 continue;
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Andrey Andreev55a40b72016-03-12 15:43:22 +0200478 $this->_execute($row, $row['rules'], $row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480
481 // Did we end up with any errors?
482 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 if ($total_errors > 0)
484 {
485 $this->_safe_form_data = TRUE;
486 }
487
488 // Now we need to re-set the POST data with the new, processed data
Andrey Andreev02ac1872016-03-16 12:19:34 +0200489 empty($this->validation_data) && $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200490
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200491 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
493
494 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 /**
Andrey Andreev0fae6252016-05-17 13:46:55 +0300497 * Prepare rules
498 *
499 * Re-orders the provided rules in order of importance, so that
500 * they can easily be executed later without weird checks ...
501 *
502 * "Callbacks" are given the highest priority (always called),
503 * followed by 'required' (called if callbacks didn't fail),
504 * and then every next rule depends on the previous one passing.
505 *
506 * @param array $rules
507 * @return array
508 */
509 protected function _prepare_rules($rules)
510 {
511 $new_rules = array();
512 $callbacks = array();
513
514 foreach ($rules as &$rule)
515 {
516 // Let 'required' always be the first (non-callback) rule
517 if ($rule === 'required')
518 {
519 array_unshift($new_rules, 'required');
520 }
521 // 'isset' is a kind of a weird alias for 'required' ...
522 elseif ($rule === 'isset' && (empty($new_rules) OR $new_rules[0] !== 'required'))
523 {
524 array_unshift($new_rules, 'isset');
525 }
526 // The old/classic 'callback_'-prefixed rules
527 elseif (is_string($rule) && strncmp('callback_', $rule, 9) === 0)
528 {
529 $callbacks[] = $rule;
530 }
531 // Proper callables
532 elseif (is_callable($rule))
533 {
534 $callbacks[] = $rule;
535 }
536 // "Named" callables; i.e. array('name' => $callable)
537 elseif (is_array($rule) && isset($rule[0], $rule[1]) && is_callable($rule[1]))
538 {
539 $callbacks[] = $rule;
540 }
541 // Everything else goes at the end of the queue
542 else
543 {
544 $new_rules[] = $rule;
545 }
546 }
547
548 return array_merge($callbacks, $new_rules);
549 }
550
551 // --------------------------------------------------------------------
552
553 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * Traverse a multidimensional $_POST array index until the data is found
555 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 * @param array
557 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300558 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200560 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100561 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200563 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200565 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 }
Barry Mienydd671972010-10-04 16:33:58 +0200567
Andrey Andreev2d48b4f2012-11-23 17:33:21 +0200568 // NULL must be returned for empty fields
Andrey Andreev44c34632012-11-23 18:46:34 +0200569 return ($array === '') ? NULL : $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 }
571
572 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 /**
575 * Re-populate the _POST array with our finalized and processed data
576 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200577 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200578 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100579 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
581 foreach ($this->_field_data as $field => $row)
582 {
vlakoff1228fe22013-01-14 01:30:09 +0100583 if ($row['postdata'] !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200585 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
Andrey Andreev063f5d82019-01-04 14:41:47 +0200587 isset($_POST[$field]) && $_POST[$field] = is_array($row['postdata']) ? NULL : $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 }
589 else
590 {
Derek Jones63eeae32009-02-10 19:08:56 +0000591 // start with a reference
592 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Jones63eeae32009-02-10 19:08:56 +0000594 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200595 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
Derek Jones63eeae32009-02-10 19:08:56 +0000597 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 }
599 else
600 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 foreach ($row['keys'] as $val)
602 {
Derek Jones63eeae32009-02-10 19:08:56 +0000603 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 }
605 }
Derek Jones63eeae32009-02-10 19:08:56 +0000606
Andrey Andreev5576d2c2016-03-16 12:16:45 +0200607 $post_ref = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 }
609 }
610 }
611 }
612
613 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 /**
616 * Executes the Validation routines
617 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 * @param array
619 * @param array
620 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300621 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200623 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100624 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
626 // If the $_POST data is an array we will run a recursive call
Andrey Andreev7243d0b2016-03-12 11:40:34 +0200627 //
628 // Note: We MUST check if the array is empty or not!
629 // Otherwise empty arrays will always pass validation.
630 if (is_array($postdata) && ! empty($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200631 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 foreach ($postdata as $key => $val)
633 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300634 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 }
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 return;
638 }
Barry Mienydd671972010-10-04 16:33:58 +0200639
Andrey Andreev0fae6252016-05-17 13:46:55 +0300640 $rules = $this->_prepare_rules($rules);
Andrey Andreev78f55772012-04-03 19:59:08 +0300641 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
643 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200644
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 // We set the $postdata variable with the current data in our master array so that
646 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100647 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
649 // We shouldn't need this safety, but just in case there isn't an array index
650 // associated with this cycle we'll bail out
651 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
652 {
653 continue;
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
657 $_in_array = TRUE;
658 }
659 else
660 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300661 // If we get an array field, but it's not expected - then it is most likely
662 // somebody messing with the form on the client side, so we'll just consider
663 // it an empty field
664 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
Andrey Andreev4b90a372014-03-10 10:24:24 +0200665 ? NULL
666 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 }
668
Barry Mienydd671972010-10-04 16:33:58 +0200669 // Is the rule a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200670 $callback = $callable = FALSE;
671 if (is_string($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200673 if (strpos($rule, 'callback_') === 0)
674 {
675 $rule = substr($rule, 9);
676 $callback = TRUE;
677 }
678 }
679 elseif (is_callable($rule))
680 {
681 $callable = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 }
Andrey Andreev60726ef2014-09-08 11:31:48 +0300683 elseif (is_array($rule) && isset($rule[0], $rule[1]) && is_callable($rule[1]))
684 {
685 // We have a "named" callable, so save the name
686 $callable = $rule[0];
687 $rule = $rule[1];
688 }
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 // Strip the parameter (if exists) from the rule
691 // Rules can contain a parameter: max_length[5]
692 $param = FALSE;
Andrey Andreev4b90a372014-03-10 10:24:24 +0200693 if ( ! $callable && preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 {
Andrey Andreevef758bd2012-11-15 12:24:52 +0200695 $rule = $match[1];
696 $param = $match[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 }
Barry Mienydd671972010-10-04 16:33:58 +0200698
Andrey Andreev9e78be02016-05-25 10:54:36 +0300699 // Ignore empty, non-required inputs with a few exceptions ...
700 if (
701 ($postdata === NULL OR $postdata === '')
702 && $callback === FALSE
703 && $callable === FALSE
704 && ! in_array($rule, array('required', 'isset', 'matches'), TRUE)
705 )
706 {
707 continue;
708 }
709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 // Call the function that corresponds to the rule
Andrey Andreev60726ef2014-09-08 11:31:48 +0300711 if ($callback OR $callable !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200713 if ($callback)
Barry Mienydd671972010-10-04 16:33:58 +0200714 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200715 if ( ! method_exists($this->CI, $rule))
716 {
717 log_message('debug', 'Unable to find callback validation rule: '.$rule);
718 $result = FALSE;
719 }
720 else
721 {
722 // Run the function and grab the result
723 $result = $this->CI->$rule($postdata, $param);
724 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200726 else
727 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200728 $result = is_array($rule)
Andrey Andreev60726ef2014-09-08 11:31:48 +0300729 ? $rule[0]->{$rule[1]}($postdata)
730 : $rule($postdata);
731
732 // Is $callable set to a rule name?
733 if ($callable !== FALSE)
734 {
735 $rule = $callable;
736 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200737 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000738
739 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200740 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300742 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 }
744 else
745 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300746 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300749 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200750 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300751 // If our own wrapper function doesn't exist we see if a native PHP function does.
752 // Users can use any native PHP function call that has one param.
753 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200755 // Native PHP functions issue warnings if you pass them more parameters than they use
Andrey Andreev320d37c2012-04-03 20:21:39 +0300756 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200757
Andrey Andreev78f55772012-04-03 19:59:08 +0300758 if ($_in_array === TRUE)
759 {
760 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 }
patwork02404a12011-04-08 15:45:46 +0200762 else
763 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300764 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200765 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200767 else
768 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300769 log_message('debug', 'Unable to find validation rule: '.$rule);
770 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200771 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300772 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 else
774 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 $result = $this->$rule($postdata, $param);
776
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200777 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300779 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 }
781 else
782 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300783 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
785 }
Barry Mienydd671972010-10-04 16:33:58 +0200786
Andrey Andreev901573c2012-01-11 01:40:48 +0200787 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200789 {
Andrey Andreev60726ef2014-09-08 11:31:48 +0300790 // Callable rules might not have named error messages
Andrey Andreev57f10052014-06-07 12:22:37 +0300791 if ( ! is_string($rule))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600792 {
Andrey Andreev38b5eb02015-06-10 17:38:26 +0300793 $line = $this->CI->lang->line('form_validation_error_message_not_set').'(Anonymous function)';
Andrey Andreev57f10052014-06-07 12:22:37 +0300794 }
Andrey Andreev57f10052014-06-07 12:22:37 +0300795 else
796 {
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200797 $line = $this->_get_error_message($rule, $row['field']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 }
Barry Mienydd671972010-10-04 16:33:58 +0200799
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200801 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200802 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 {
Pascal Krietec1895832009-10-13 12:56:43 +0000804 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600808 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
Derek Allard2067d1a2008-11-13 22:59:24 +0000809
810 // Save the error message
811 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 if ( ! isset($this->_error_array[$row['field']]))
814 {
815 $this->_error_array[$row['field']] = $message;
816 }
Barry Mienydd671972010-10-04 16:33:58 +0200817
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 return;
819 }
820 }
821 }
822
823 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200824
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 /**
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200826 * Get the error message for the rule
827 *
828 * @param string $rule The rule name
829 * @param string $field The field name
830 * @return string
831 */
832 protected function _get_error_message($rule, $field)
833 {
834 // check if a custom message is defined through validation config row.
835 if (isset($this->_field_data[$field]['errors'][$rule]))
836 {
837 return $this->_field_data[$field]['errors'][$rule];
838 }
839 // check if a custom message has been set using the set_message() function
840 elseif (isset($this->_error_messages[$rule]))
841 {
842 return $this->_error_messages[$rule];
843 }
Andrey Andreevb30a64a2016-02-09 23:46:25 +0200844 elseif (FALSE !== ($line = $this->CI->lang->line('form_validation_'.$rule)))
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200845 {
Andrey Andreevb30a64a2016-02-09 23:46:25 +0200846 return $line;
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200847 }
848 // DEPRECATED support for non-prefixed keys, lang file again
Andrey Andreevb30a64a2016-02-09 23:46:25 +0200849 elseif (FALSE !== ($line = $this->CI->lang->line($rule, FALSE)))
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200850 {
Andrey Andreevb30a64a2016-02-09 23:46:25 +0200851 return $line;
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200852 }
853
854 return $this->CI->lang->line('form_validation_error_message_not_set').'('.$rule.')';
855 }
856
857 // --------------------------------------------------------------------
858
859 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 * Translate a field name
861 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 * @param string the field name
863 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200864 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100865 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 {
Andrey Andreev5d8e2a62015-04-14 16:56:28 +0300867 // Do we need to translate the field name? We look for the prefix 'lang:' to determine this
868 // If we find one, but there's no translation for the string - just return it
869 if (sscanf($fieldname, 'lang:%s', $line) === 1 && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 {
Andrey Andreev5d8e2a62015-04-14 16:56:28 +0300871 return $line;
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 }
873
874 return $fieldname;
875 }
876
877 // --------------------------------------------------------------------
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200878
Eric Roberts41cc0902012-01-24 00:59:44 -0600879 /**
880 * Build an error message using the field and param.
881 *
882 * @param string The error message line
883 * @param string A field's human name
884 * @param mixed A rule's optional parameter
885 * @return string
886 */
887 protected function _build_error_msg($line, $field = '', $param = '')
888 {
889 // Check for %s in the string for legacy support.
Eric Roberts24a13f52012-12-12 07:09:42 -0600890 if (strpos($line, '%s') !== FALSE)
Eric Roberts41cc0902012-01-24 00:59:44 -0600891 {
892 return sprintf($line, $field, $param);
893 }
Andrew Podner4296a652012-12-17 07:51:15 -0500894
Eric Roberts41cc0902012-01-24 00:59:44 -0600895 return str_replace(array('{field}', '{param}'), array($field, $param), $line);
896 }
897
898 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 /**
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530901 * Checks if the rule is present within the validator
902 *
903 * Permits you to check if a rule is present within the validator
904 *
905 * @param string the field name
906 * @return bool
907 */
908 public function has_rule($field)
909 {
910 return isset($this->_field_data[$field]);
911 }
912
913 // --------------------------------------------------------------------
914
915 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 * Get the value from a form
917 *
918 * Permits you to repopulate a form field with the value it was submitted
919 * with, or, if that value doesn't exist, with the default
920 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 * @param string the field name
922 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200923 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200924 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100925 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200927 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 {
929 return $default;
930 }
Barry Mienydd671972010-10-04 16:33:58 +0200931
Phil Sturgeon5c561802011-01-05 16:31:59 +0000932 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600933 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000934 if (is_array($this->_field_data[$field]['postdata']))
935 {
936 return array_shift($this->_field_data[$field]['postdata']);
937 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 return $this->_field_data[$field]['postdata'];
940 }
Barry Mienydd671972010-10-04 16:33:58 +0200941
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200943
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 /**
945 * Set Select
946 *
947 * Enables pull-down lists to be set to the value the user
948 * selected in the event of an error
949 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 * @param string
951 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400952 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200954 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100955 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200956 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200957 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200959 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300963 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 if (is_array($field))
965 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300966 // Note: in_array('', array(0)) returns TRUE, do not use it
967 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300969 if ($value === $v)
970 {
971 return ' selected="selected"';
972 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300974
975 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100977 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200979 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
Barry Mienydd671972010-10-04 16:33:58 +0200981
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 return ' selected="selected"';
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 /**
988 * Set Radio
989 *
990 * Enables radio buttons to be set to the value the user
991 * selected in the event of an error
992 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 * @param string
994 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400995 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200997 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100998 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 {
Andrey Andreev46ac8812012-02-28 14:32:54 +02001000 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 {
Andrey Andreev6de924c2012-01-20 13:18:18 +02001002 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
Barry Mienydd671972010-10-04 16:33:58 +02001004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +03001006 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 if (is_array($field))
1008 {
Andrey Andreeva587a932013-10-23 19:57:46 +03001009 // Note: in_array('', array(0)) returns TRUE, do not use it
1010 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 {
Andrey Andreeva587a932013-10-23 19:57:46 +03001012 if ($value === $v)
1013 {
1014 return ' checked="checked"';
1015 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 }
Andrey Andreeva587a932013-10-23 19:57:46 +03001017
1018 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 }
Alex Bilbied261b1e2012-06-02 11:12:16 +01001020 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001022 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 }
Barry Mienydd671972010-10-04 16:33:58 +02001024
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 return ' checked="checked"';
1026 }
Barry Mienydd671972010-10-04 16:33:58 +02001027
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001029
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 /**
1031 * Set Checkbox
1032 *
1033 * Enables checkboxes to be set to the value the user
1034 * selected in the event of an error
1035 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 * @param string
1037 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001038 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001040 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001041 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001043 // Logic is exactly the same as for radio fields
1044 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 }
Barry Mienydd671972010-10-04 16:33:58 +02001046
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001048
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 /**
1050 * Required
1051 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 * @param string
1053 * @return bool
1054 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001055 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 {
Andrey Andreev0fae6252016-05-17 13:46:55 +03001057 return is_array($str)
1058 ? (empty($str) === FALSE)
1059 : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 }
Barry Mienydd671972010-10-04 16:33:58 +02001061
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001063
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001065 * Performs a Regular Expression match test.
1066 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001067 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001068 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001069 * @return bool
1070 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001071 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001072 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001073 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001074 }
1075
1076 // --------------------------------------------------------------------
1077
1078 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 * Match one field to another
1080 *
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001081 * @param string $str string to compare against
1082 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 * @return bool
1084 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001085 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 {
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001087 return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
1088 ? ($str === $this->_field_data[$field]['postdata'])
1089 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001091
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001092 // --------------------------------------------------------------------
1093
1094 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -03001095 * Differs from another field
1096 *
1097 * @param string
1098 * @param string field
1099 * @return bool
1100 */
1101 public function differs($str, $field)
1102 {
1103 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001105
1106 // --------------------------------------------------------------------
1107
1108 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +02001109 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001110 *
Andrey Andreevd09d6502012-01-03 06:38:33 +02001111 * Check if the input value doesn't already exist
1112 * in the specified database field.
1113 *
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001114 * @param string $str
1115 * @param string $field
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001116 * @return bool
1117 */
1118 public function is_unique($str, $field)
1119 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001120 sscanf($field, '%[^.].%[^.]', $table, $field);
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001121 return isset($this->CI->db)
1122 ? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
1123 : FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001124 }
Barry Mienydd671972010-10-04 16:33:58 +02001125
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001127
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 /**
1129 * Minimum Length
1130 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001132 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001134 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001135 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001137 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 {
1139 return FALSE;
1140 }
1141
bjjay8d6c8fe2015-03-09 13:46:06 +08001142 return ($val <= mb_strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 }
Barry Mienydd671972010-10-04 16:33:58 +02001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 /**
1148 * Max Length
1149 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001151 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001153 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001154 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001156 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 {
1158 return FALSE;
1159 }
1160
bjjay8d6c8fe2015-03-09 13:46:06 +08001161 return ($val >= mb_strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 }
Barry Mienydd671972010-10-04 16:33:58 +02001163
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001165
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 /**
1167 * Exact Length
1168 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +02001170 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001172 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001173 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001175 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 {
1177 return FALSE;
1178 }
1179
bjjay8d6c8fe2015-03-09 13:46:06 +08001180 return (mb_strlen($str) === (int) $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 }
Barry Mienydd671972010-10-04 16:33:58 +02001182
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001184
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 /**
Andrey Andreevdaaca882012-11-26 22:16:12 +02001186 * Valid URL
1187 *
1188 * @param string $str
1189 * @return bool
1190 */
1191 public function valid_url($str)
1192 {
1193 if (empty($str))
1194 {
1195 return FALSE;
1196 }
1197 elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches))
1198 {
1199 if (empty($matches[2]))
1200 {
1201 return FALSE;
1202 }
Andrey Andreeve33c82d2016-08-10 15:18:31 +03001203 elseif ( ! in_array(strtolower($matches[1]), array('http', 'https'), TRUE))
Andrey Andreevdaaca882012-11-26 22:16:12 +02001204 {
1205 return FALSE;
1206 }
1207
1208 $str = $matches[2];
1209 }
1210
Andrey Andreev391d3392016-01-30 22:43:41 +02001211 // PHP 7 accepts IPv6 addresses within square brackets as hostnames,
1212 // but it appears that the PR that came in with https://bugs.php.net/bug.php?id=68039
1213 // was never merged into a PHP 5 branch ... https://3v4l.org/8PsSN
1214 if (preg_match('/^\[([^\]]+)\]/', $str, $matches) && ! is_php('7') && filter_var($matches[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== FALSE)
1215 {
1216 $str = 'ipv6.host'.substr($str, strlen($matches[1]) + 2);
1217 }
1218
Andrey Andreeva8382792016-07-28 16:40:12 +03001219 return (filter_var('http://'.$str, FILTER_VALIDATE_URL) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 }
1221
1222 // --------------------------------------------------------------------
1223
1224 /**
1225 * Valid Email
1226 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 * @param string
1228 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001229 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001230 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 {
Andrey Andreeved1a0452017-06-19 08:25:23 +03001232 if (function_exists('idn_to_ascii') && preg_match('#\A([^@]+)@(.+)\z#', $str, $matches))
Andrey Andreev95496662014-06-01 00:00:13 +03001233 {
Andrey Andreev221c0952018-01-22 10:29:19 +02001234 $domain = defined('INTL_IDNA_VARIANT_UTS46')
Andrey Andreev4fd2d492017-11-09 20:38:33 +02001235 ? idn_to_ascii($matches[2], 0, INTL_IDNA_VARIANT_UTS46)
1236 : idn_to_ascii($matches[2]);
Andrey Andreev32f8c932018-06-12 16:25:44 +03001237
1238 if ($domain !== FALSE)
1239 {
1240 $str = $matches[1].'@'.$domain;
1241 }
Andrey Andreev95496662014-06-01 00:00:13 +03001242 }
1243
Andrey Andreev580388b2012-06-27 15:43:46 +03001244 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 }
1246
1247 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001248
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 /**
1250 * Valid Emails
1251 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 * @param string
1253 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001254 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001255 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 {
1257 if (strpos($str, ',') === FALSE)
1258 {
1259 return $this->valid_email(trim($str));
1260 }
Barry Mienydd671972010-10-04 16:33:58 +02001261
Pascal Kriete14287f32011-02-14 13:39:34 -05001262 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001264 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 {
1266 return FALSE;
1267 }
1268 }
Barry Mienydd671972010-10-04 16:33:58 +02001269
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 return TRUE;
1271 }
1272
1273 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001274
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 /**
1276 * Validate IP Address
1277 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001279 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001280 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001282 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001284 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 }
1286
1287 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001288
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 /**
1290 * Alpha
1291 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 * @param string
1293 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001294 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001295 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001297 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 }
Barry Mienydd671972010-10-04 16:33:58 +02001299
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001301
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 /**
1303 * Alpha-numeric
1304 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 * @param string
1306 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001307 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001308 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001310 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 }
Barry Mienydd671972010-10-04 16:33:58 +02001312
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 // --------------------------------------------------------------------
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001314
1315 /**
1316 * Alpha-numeric w/ spaces
1317 *
1318 * @param string
1319 * @return bool
1320 */
1321 public function alpha_numeric_spaces($str)
1322 {
Sajan Parikhdf3bfed2013-02-04 12:25:49 -06001323 return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001324 }
1325
1326 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001327
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 /**
1329 * Alpha-numeric with underscores and dashes
1330 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 * @param string
1332 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001333 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001334 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001336 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 }
Barry Mienydd671972010-10-04 16:33:58 +02001338
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001340
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 /**
1342 * Numeric
1343 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 * @param string
1345 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001346 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001347 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001349 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001350
1351 }
1352
1353 // --------------------------------------------------------------------
1354
Barry Mienydd671972010-10-04 16:33:58 +02001355 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 * Integer
1357 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 * @param string
1359 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001360 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001361 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001363 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1364 }
1365
1366 // --------------------------------------------------------------------
1367
1368 /**
1369 * Decimal number
1370 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001371 * @param string
1372 * @return bool
1373 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001374 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001375 {
1376 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1377 }
1378
1379 // --------------------------------------------------------------------
1380
1381 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001382 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001383 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001384 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001385 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001386 * @return bool
1387 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001388 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001389 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001390 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001391 }
1392
1393 // --------------------------------------------------------------------
1394
1395 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001396 * Equal to or Greater than
1397 *
Nick Busey98c347d2012-02-02 11:07:03 -07001398 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001399 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001400 * @return bool
1401 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001402 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001403 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001404 return is_numeric($str) ? ($str >= $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001405 }
1406
1407 // --------------------------------------------------------------------
1408
1409 /**
1410 * Less than
1411 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001412 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001413 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001414 * @return bool
1415 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001416 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001417 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001418 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001420
1421 // --------------------------------------------------------------------
1422
Barry Mienydd671972010-10-04 16:33:58 +02001423 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001424 * Equal to or Less than
1425 *
Nick Busey98c347d2012-02-02 11:07:03 -07001426 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001427 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001428 * @return bool
1429 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001430 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001431 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001432 return is_numeric($str) ? ($str <= $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001433 }
1434
1435 // --------------------------------------------------------------------
1436
Barry Mienydd671972010-10-04 16:33:58 +02001437 /**
Lance Vincenteca885d2015-01-28 17:43:23 +08001438 * Value should be within an array of values
1439 *
1440 * @param string
Andrey Andreev5b662e42015-01-29 00:13:54 +02001441 * @param string
Lance Vincenteca885d2015-01-28 17:43:23 +08001442 * @return bool
1443 */
Lance Vincent49f483d2015-01-28 22:46:19 +08001444 public function in_list($value, $list)
Lance Vincenteca885d2015-01-28 17:43:23 +08001445 {
Lance Vincent0b0117c2015-01-28 20:31:42 +08001446 return in_array($value, explode(',', $list), TRUE);
Lance Vincenteca885d2015-01-28 17:43:23 +08001447 }
1448
1449 // --------------------------------------------------------------------
1450
1451 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001452 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001453 *
Barry Mienydd671972010-10-04 16:33:58 +02001454 * @param string
1455 * @return bool
1456 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001457 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001458 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001459 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001460 }
1461
1462 // --------------------------------------------------------------------
1463
1464 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001465 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001466 *
Barry Mienydd671972010-10-04 16:33:58 +02001467 * @param string
1468 * @return bool
1469 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001470 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001471 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001472 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001473 }
1474
Derek Allard2067d1a2008-11-13 22:59:24 +00001475 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001476
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 /**
1478 * Valid Base64
1479 *
1480 * Tests a string for characters outside of the Base64 alphabet
1481 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1482 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001483 * @param string
1484 * @return bool
1485 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001486 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001487 {
Andrey Andreevcd9797a2013-06-28 14:03:48 +03001488 return (base64_encode(base64_decode($str)) === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 }
Barry Mienydd671972010-10-04 16:33:58 +02001490
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001492
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 /**
1494 * Prep data for form
1495 *
1496 * This function allows HTML to be safely shown in a form.
1497 * Special characters are converted.
1498 *
Andrey Andreev4f555072016-03-12 17:21:55 +02001499 * @deprecated 3.0.6 Not used anywhere within the framework and pretty much useless
1500 * @param mixed $data Input data
1501 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 */
Andrey Andreev4f555072016-03-12 17:21:55 +02001503 public function prep_for_form($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +02001505 if ($this->_safe_form_data === FALSE OR empty($data))
1506 {
1507 return $data;
1508 }
1509
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 if (is_array($data))
1511 {
1512 foreach ($data as $key => $val)
1513 {
1514 $data[$key] = $this->prep_for_form($val);
1515 }
Barry Mienydd671972010-10-04 16:33:58 +02001516
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 return $data;
1518 }
Barry Mienydd671972010-10-04 16:33:58 +02001519
Andrey Andreev901573c2012-01-11 01:40:48 +02001520 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 }
Barry Mienydd671972010-10-04 16:33:58 +02001522
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001524
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 /**
1526 * Prep URL
1527 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001528 * @param string
1529 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001530 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001531 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001532 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001533 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 {
1535 return '';
1536 }
Barry Mienydd671972010-10-04 16:33:58 +02001537
Andrey Andreev901573c2012-01-11 01:40:48 +02001538 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001539 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001540 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001541 }
Barry Mienydd671972010-10-04 16:33:58 +02001542
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 return $str;
1544 }
Barry Mienydd671972010-10-04 16:33:58 +02001545
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001547
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 /**
1549 * Strip Image Tags
1550 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 * @param string
1552 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001553 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001554 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001555 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001556 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001557 }
Barry Mienydd671972010-10-04 16:33:58 +02001558
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001560
Derek Allard2067d1a2008-11-13 22:59:24 +00001561 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 * Convert PHP tags to entities
1563 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001564 * @param string
1565 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001566 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001567 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001568 {
Andrey Andreev838a9d62012-12-03 14:37:47 +02001569 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001570 }
1571
JonoB099c4782012-03-04 14:37:30 +00001572 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001573
1574 /**
1575 * Reset validation vars
1576 *
1577 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001578 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001579 *
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001580 * @return CI_Form_validation
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001581 */
JonoB883f80f2012-03-05 09:51:27 +00001582 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001583 {
JonoB099c4782012-03-04 14:37:30 +00001584 $this->_field_data = array();
JonoB099c4782012-03-04 14:37:30 +00001585 $this->_error_array = array();
1586 $this->_error_messages = array();
1587 $this->error_string = '';
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001588 return $this;
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001589 }
1590
Derek Allard2067d1a2008-11-13 22:59:24 +00001591}