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