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