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