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