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