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