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 | { |
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( |
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 | ); |
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); |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 174 | |
| 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; |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 194 | |
| 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 | |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame^] | 342 | preg_match_all('/([a-zA-Z_-]*(\[.*\])?)\|?/i', $row['rules'], $matches); |
| 343 | |
| 344 | $rules = $matches[1]; |
| 345 | array_pop($rules); |
| 346 | unset($matches); |
| 347 | |
| 348 | $this->_execute($row, $rules, $this->_field_data[$field]['postdata']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // Did we end up with any errors? |
| 352 | $total_errors = count($this->_error_array); |
| 353 | |
| 354 | if ($total_errors > 0) |
| 355 | { |
| 356 | $this->_safe_form_data = TRUE; |
| 357 | } |
| 358 | |
| 359 | // Now we need to re-set the POST data with the new, processed data |
| 360 | $this->_reset_post_array(); |
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 | // No errors, validation passes! |
| 363 | if ($total_errors == 0) |
| 364 | { |
| 365 | return TRUE; |
| 366 | } |
| 367 | |
| 368 | // Validation fails |
| 369 | return FALSE; |
| 370 | } |
| 371 | |
| 372 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 373 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | /** |
| 375 | * Traverse a multidimensional $_POST array index until the data is found |
| 376 | * |
| 377 | * @access private |
| 378 | * @param array |
| 379 | * @param array |
| 380 | * @param integer |
| 381 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 382 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | function _reduce_array($array, $keys, $i = 0) |
| 384 | { |
| 385 | if (is_array($array)) |
| 386 | { |
| 387 | if (isset($keys[$i])) |
| 388 | { |
| 389 | if (isset($array[$keys[$i]])) |
| 390 | { |
| 391 | $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)); |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | return NULL; |
| 396 | } |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | return $array; |
| 401 | } |
| 402 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 403 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | return $array; |
| 405 | } |
| 406 | |
| 407 | // -------------------------------------------------------------------- |
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 | /** |
| 410 | * Re-populate the _POST array with our finalized and processed data |
| 411 | * |
| 412 | * @access private |
| 413 | * @return null |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 414 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | function _reset_post_array() |
| 416 | { |
| 417 | foreach ($this->_field_data as $field => $row) |
| 418 | { |
| 419 | if ( ! is_null($row['postdata'])) |
| 420 | { |
| 421 | if ($row['is_array'] == FALSE) |
| 422 | { |
| 423 | if (isset($_POST[$row['field']])) |
| 424 | { |
| 425 | $_POST[$row['field']] = $this->prep_for_form($row['postdata']); |
| 426 | } |
| 427 | } |
| 428 | else |
| 429 | { |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 430 | // start with a reference |
| 431 | $post_ref =& $_POST; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 432 | |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 433 | // before we assign values, make a reference to the right POST key |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | if (count($row['keys']) == 1) |
| 435 | { |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 436 | $post_ref =& $post_ref[current($row['keys'])]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | } |
| 438 | else |
| 439 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | foreach ($row['keys'] as $val) |
| 441 | { |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 442 | $post_ref =& $post_ref[$val]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 445 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | if (is_array($row['postdata'])) |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 447 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | $array = array(); |
| 449 | foreach ($row['postdata'] as $k => $v) |
| 450 | { |
| 451 | $array[$k] = $this->prep_for_form($v); |
| 452 | } |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 453 | |
| 454 | $post_ref = $array; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | } |
| 456 | else |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 457 | { |
| 458 | $post_ref = $this->prep_for_form($row['postdata']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 459 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 466 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | /** |
| 468 | * Executes the Validation routines |
| 469 | * |
| 470 | * @access private |
| 471 | * @param array |
| 472 | * @param array |
| 473 | * @param mixed |
| 474 | * @param integer |
| 475 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 476 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | function _execute($row, $rules, $postdata = NULL, $cycles = 0) |
| 478 | { |
| 479 | // If the $_POST data is an array we will run a recursive call |
| 480 | if (is_array($postdata)) |
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 | foreach ($postdata as $key => $val) |
| 483 | { |
| 484 | $this->_execute($row, $rules, $val, $cycles); |
| 485 | $cycles++; |
| 486 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 487 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | return; |
| 489 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 490 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | // -------------------------------------------------------------------- |
| 492 | |
| 493 | // If the field is blank, but NOT required, no further tests are necessary |
| 494 | $callback = FALSE; |
| 495 | if ( ! in_array('required', $rules) AND is_null($postdata)) |
| 496 | { |
| 497 | // Before we bail out, does the rule contain a callback? |
| 498 | if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match)) |
| 499 | { |
| 500 | $callback = TRUE; |
| 501 | $rules = (array('1' => $match[1])); |
| 502 | } |
| 503 | else |
| 504 | { |
| 505 | return; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 510 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | // Isset Test. Typically this rule will only apply to checkboxes. |
| 512 | if (is_null($postdata) AND $callback == FALSE) |
| 513 | { |
| 514 | if (in_array('isset', $rules, TRUE) OR in_array('required', $rules)) |
| 515 | { |
| 516 | // Set the message type |
| 517 | $type = (in_array('required', $rules)) ? 'required' : 'isset'; |
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 | if ( ! isset($this->_error_messages[$type])) |
| 520 | { |
| 521 | if (FALSE === ($line = $this->CI->lang->line($type))) |
| 522 | { |
| 523 | $line = 'The field was not set'; |
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 | } |
| 526 | else |
| 527 | { |
| 528 | $line = $this->_error_messages[$type]; |
| 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 | // Build the error message |
| 532 | $message = sprintf($line, $this->_translate_fieldname($row['label'])); |
| 533 | |
| 534 | // Save the error message |
| 535 | $this->_field_data[$row['field']]['error'] = $message; |
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 | if ( ! isset($this->_error_array[$row['field']])) |
| 538 | { |
| 539 | $this->_error_array[$row['field']] = $message; |
| 540 | } |
| 541 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 542 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | |
| 546 | // -------------------------------------------------------------------- |
| 547 | |
| 548 | // Cycle through each rule and run it |
| 549 | foreach ($rules As $rule) |
| 550 | { |
| 551 | $_in_array = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 552 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 553 | // We set the $postdata variable with the current data in our master array so that |
| 554 | // each cycle of the loop is dealing with the processed data from the last cycle |
| 555 | if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata'])) |
| 556 | { |
| 557 | // We shouldn't need this safety, but just in case there isn't an array index |
| 558 | // associated with this cycle we'll bail out |
| 559 | if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles])) |
| 560 | { |
| 561 | continue; |
| 562 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 563 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 564 | $postdata = $this->_field_data[$row['field']]['postdata'][$cycles]; |
| 565 | $_in_array = TRUE; |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | $postdata = $this->_field_data[$row['field']]['postdata']; |
| 570 | } |
| 571 | |
| 572 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 573 | |
| 574 | // Is the rule a callback? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 575 | $callback = FALSE; |
| 576 | if (substr($rule, 0, 9) == 'callback_') |
| 577 | { |
| 578 | $rule = substr($rule, 9); |
| 579 | $callback = TRUE; |
| 580 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 581 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | // Strip the parameter (if exists) from the rule |
| 583 | // Rules can contain a parameter: max_length[5] |
| 584 | $param = FALSE; |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame^] | 585 | if (preg_match("/(.*?)\[(.*)\]/", $rule, $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 586 | { |
| 587 | $rule = $match[1]; |
| 588 | $param = $match[2]; |
| 589 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 590 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | // Call the function that corresponds to the rule |
| 592 | if ($callback === TRUE) |
| 593 | { |
| 594 | if ( ! method_exists($this->CI, $rule)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 595 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 596 | continue; |
| 597 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 598 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 599 | // Run the function and grab the result |
| 600 | $result = $this->CI->$rule($postdata, $param); |
| 601 | |
| 602 | // Re-assign the result to the master data array |
| 603 | if ($_in_array == TRUE) |
| 604 | { |
| 605 | $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; |
| 606 | } |
| 607 | else |
| 608 | { |
| 609 | $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result; |
| 610 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 611 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 612 | // If the field isn't required and we just processed a callback we'll move on... |
| 613 | if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE) |
| 614 | { |
Derek Allard | 4e5cf1c | 2009-07-06 20:53:41 +0000 | [diff] [blame] | 615 | continue; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 619 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 620 | if ( ! method_exists($this, $rule)) |
| 621 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 622 | // 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] | 623 | // Users can use any native PHP function call that has one param. |
| 624 | if (function_exists($rule)) |
| 625 | { |
| 626 | $result = $rule($postdata); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 627 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 628 | if ($_in_array == TRUE) |
| 629 | { |
| 630 | $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; |
| 631 | } |
| 632 | else |
| 633 | { |
| 634 | $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result; |
| 635 | } |
| 636 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 637 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 638 | continue; |
| 639 | } |
| 640 | |
| 641 | $result = $this->$rule($postdata, $param); |
| 642 | |
| 643 | if ($_in_array == TRUE) |
| 644 | { |
| 645 | $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; |
| 646 | } |
| 647 | else |
| 648 | { |
| 649 | $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result; |
| 650 | } |
| 651 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 652 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 653 | // Did the rule test negatively? If so, grab the error. |
| 654 | if ($result === FALSE) |
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 | if ( ! isset($this->_error_messages[$rule])) |
| 657 | { |
| 658 | if (FALSE === ($line = $this->CI->lang->line($rule))) |
| 659 | { |
| 660 | $line = 'Unable to access an error message corresponding to your field name.'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 661 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 662 | } |
| 663 | else |
| 664 | { |
| 665 | $line = $this->_error_messages[$rule]; |
| 666 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 667 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 668 | // Is the parameter we are inserting into the error message the name |
| 669 | // of another field? If so we need to grab its "field label" |
| 670 | if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label'])) |
| 671 | { |
Pascal Kriete | c189583 | 2009-10-13 12:56:43 +0000 | [diff] [blame] | 672 | $param = $this->_translate_fieldname($this->_field_data[$param]['label']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 673 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 674 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 675 | // Build the error message |
| 676 | $message = sprintf($line, $this->_translate_fieldname($row['label']), $param); |
| 677 | |
| 678 | // Save the error message |
| 679 | $this->_field_data[$row['field']]['error'] = $message; |
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 | if ( ! isset($this->_error_array[$row['field']])) |
| 682 | { |
| 683 | $this->_error_array[$row['field']] = $message; |
| 684 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 685 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 686 | return; |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 692 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 693 | /** |
| 694 | * Translate a field name |
| 695 | * |
| 696 | * @access private |
| 697 | * @param string the field name |
| 698 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 699 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 700 | function _translate_fieldname($fieldname) |
| 701 | { |
| 702 | // Do we need to translate the field name? |
| 703 | // We look for the prefix lang: to determine this |
| 704 | if (substr($fieldname, 0, 5) == 'lang:') |
| 705 | { |
| 706 | // Grab the variable |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 707 | $line = substr($fieldname, 5); |
| 708 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 709 | // Were we able to translate the field name? If not we use $line |
| 710 | if (FALSE === ($fieldname = $this->CI->lang->line($line))) |
| 711 | { |
| 712 | return $line; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | return $fieldname; |
| 717 | } |
| 718 | |
| 719 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 720 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 721 | /** |
| 722 | * Get the value from a form |
| 723 | * |
| 724 | * Permits you to repopulate a form field with the value it was submitted |
| 725 | * with, or, if that value doesn't exist, with the default |
| 726 | * |
| 727 | * @access public |
| 728 | * @param string the field name |
| 729 | * @param string |
| 730 | * @return void |
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 | function set_value($field = '', $default = '') |
| 733 | { |
| 734 | if ( ! isset($this->_field_data[$field])) |
| 735 | { |
| 736 | return $default; |
| 737 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 738 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 739 | return $this->_field_data[$field]['postdata']; |
| 740 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 741 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 742 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 743 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 744 | /** |
| 745 | * Set Select |
| 746 | * |
| 747 | * Enables pull-down lists to be set to the value the user |
| 748 | * selected in the event of an error |
| 749 | * |
| 750 | * @access public |
| 751 | * @param string |
| 752 | * @param string |
| 753 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 754 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 755 | function set_select($field = '', $value = '', $default = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 756 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 757 | if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) |
| 758 | { |
| 759 | if ($default === TRUE AND count($this->_field_data) === 0) |
| 760 | { |
| 761 | return ' selected="selected"'; |
| 762 | } |
| 763 | return ''; |
| 764 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 765 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 766 | $field = $this->_field_data[$field]['postdata']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 767 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 768 | if (is_array($field)) |
| 769 | { |
| 770 | if ( ! in_array($value, $field)) |
| 771 | { |
| 772 | return ''; |
| 773 | } |
| 774 | } |
| 775 | else |
| 776 | { |
| 777 | if (($field == '' OR $value == '') OR ($field != $value)) |
| 778 | { |
| 779 | return ''; |
| 780 | } |
| 781 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 782 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 783 | return ' selected="selected"'; |
| 784 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 785 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | /** |
| 789 | * Set Radio |
| 790 | * |
| 791 | * Enables radio buttons to be set to the value the user |
| 792 | * selected in the event of an error |
| 793 | * |
| 794 | * @access public |
| 795 | * @param string |
| 796 | * @param string |
| 797 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 798 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 799 | function set_radio($field = '', $value = '', $default = FALSE) |
| 800 | { |
| 801 | if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) |
| 802 | { |
| 803 | if ($default === TRUE AND count($this->_field_data) === 0) |
| 804 | { |
| 805 | return ' checked="checked"'; |
| 806 | } |
| 807 | return ''; |
| 808 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 809 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 810 | $field = $this->_field_data[$field]['postdata']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 811 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 812 | if (is_array($field)) |
| 813 | { |
| 814 | if ( ! in_array($value, $field)) |
| 815 | { |
| 816 | return ''; |
| 817 | } |
| 818 | } |
| 819 | else |
| 820 | { |
| 821 | if (($field == '' OR $value == '') OR ($field != $value)) |
| 822 | { |
| 823 | return ''; |
| 824 | } |
| 825 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 826 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 827 | return ' checked="checked"'; |
| 828 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 829 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | /** |
| 833 | * Set Checkbox |
| 834 | * |
| 835 | * Enables checkboxes to be set to the value the user |
| 836 | * selected in the event of an error |
| 837 | * |
| 838 | * @access public |
| 839 | * @param string |
| 840 | * @param string |
| 841 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 842 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 843 | function set_checkbox($field = '', $value = '', $default = FALSE) |
| 844 | { |
| 845 | if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) |
| 846 | { |
| 847 | if ($default === TRUE AND count($this->_field_data) === 0) |
| 848 | { |
| 849 | return ' checked="checked"'; |
| 850 | } |
| 851 | return ''; |
| 852 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 853 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 854 | $field = $this->_field_data[$field]['postdata']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 855 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 856 | if (is_array($field)) |
| 857 | { |
| 858 | if ( ! in_array($value, $field)) |
| 859 | { |
| 860 | return ''; |
| 861 | } |
| 862 | } |
| 863 | else |
| 864 | { |
| 865 | if (($field == '' OR $value == '') OR ($field != $value)) |
| 866 | { |
| 867 | return ''; |
| 868 | } |
| 869 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 870 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 871 | return ' checked="checked"'; |
| 872 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 873 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | /** |
| 877 | * Required |
| 878 | * |
| 879 | * @access public |
| 880 | * @param string |
| 881 | * @return bool |
| 882 | */ |
| 883 | function required($str) |
| 884 | { |
| 885 | if ( ! is_array($str)) |
| 886 | { |
| 887 | return (trim($str) == '') ? FALSE : TRUE; |
| 888 | } |
| 889 | else |
| 890 | { |
| 891 | return ( ! empty($str)); |
| 892 | } |
| 893 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 894 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 895 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 896 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 897 | /** |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame^] | 898 | * Performs a Regular Expression match test. |
| 899 | * |
| 900 | * @access public |
| 901 | * @param string |
| 902 | * @param regex |
| 903 | * @return bool |
| 904 | */ |
| 905 | function regex_match($str, $regex) |
| 906 | { |
| 907 | if ( ! preg_match($regex, $str)) |
| 908 | { |
| 909 | return FALSE; |
| 910 | } |
| 911 | |
| 912 | return TRUE; |
| 913 | } |
| 914 | |
| 915 | // -------------------------------------------------------------------- |
| 916 | |
| 917 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 918 | * Match one field to another |
| 919 | * |
| 920 | * @access public |
| 921 | * @param string |
| 922 | * @param field |
| 923 | * @return bool |
| 924 | */ |
| 925 | function matches($str, $field) |
| 926 | { |
| 927 | if ( ! isset($_POST[$field])) |
| 928 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 929 | return FALSE; |
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 | $field = $_POST[$field]; |
| 933 | |
| 934 | return ($str !== $field) ? FALSE : TRUE; |
| 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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 938 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 939 | /** |
| 940 | * Minimum Length |
| 941 | * |
| 942 | * @access public |
| 943 | * @param string |
| 944 | * @param value |
| 945 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 946 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 947 | function min_length($str, $val) |
| 948 | { |
| 949 | if (preg_match("/[^0-9]/", $val)) |
| 950 | { |
| 951 | return FALSE; |
| 952 | } |
| 953 | |
| 954 | if (function_exists('mb_strlen')) |
| 955 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 956 | return (mb_strlen($str) < $val) ? FALSE : TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 957 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 958 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 959 | return (strlen($str) < $val) ? FALSE : TRUE; |
| 960 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 961 | |
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 | /** |
| 965 | * Max Length |
| 966 | * |
| 967 | * @access public |
| 968 | * @param string |
| 969 | * @param value |
| 970 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 971 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 972 | function max_length($str, $val) |
| 973 | { |
| 974 | if (preg_match("/[^0-9]/", $val)) |
| 975 | { |
| 976 | return FALSE; |
| 977 | } |
| 978 | |
| 979 | if (function_exists('mb_strlen')) |
| 980 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 981 | return (mb_strlen($str) > $val) ? FALSE : TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 982 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 983 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 984 | return (strlen($str) > $val) ? FALSE : TRUE; |
| 985 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 986 | |
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 | /** |
| 990 | * Exact Length |
| 991 | * |
| 992 | * @access public |
| 993 | * @param string |
| 994 | * @param value |
| 995 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 996 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 997 | function exact_length($str, $val) |
| 998 | { |
| 999 | if (preg_match("/[^0-9]/", $val)) |
| 1000 | { |
| 1001 | return FALSE; |
| 1002 | } |
| 1003 | |
| 1004 | if (function_exists('mb_strlen')) |
| 1005 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1006 | return (mb_strlen($str) != $val) ? FALSE : TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1007 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1008 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1009 | return (strlen($str) != $val) ? FALSE : TRUE; |
| 1010 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1011 | |
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 | /** |
| 1015 | * Valid Email |
| 1016 | * |
| 1017 | * @access public |
| 1018 | * @param string |
| 1019 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1020 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1021 | function valid_email($str) |
| 1022 | { |
| 1023 | return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE; |
| 1024 | } |
| 1025 | |
| 1026 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1027 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1028 | /** |
| 1029 | * Valid Emails |
| 1030 | * |
| 1031 | * @access public |
| 1032 | * @param string |
| 1033 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1034 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1035 | function valid_emails($str) |
| 1036 | { |
| 1037 | if (strpos($str, ',') === FALSE) |
| 1038 | { |
| 1039 | return $this->valid_email(trim($str)); |
| 1040 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1041 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1042 | foreach(explode(',', $str) as $email) |
| 1043 | { |
| 1044 | if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE) |
| 1045 | { |
| 1046 | return FALSE; |
| 1047 | } |
| 1048 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1049 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1050 | return TRUE; |
| 1051 | } |
| 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 | /** |
| 1056 | * Validate IP Address |
| 1057 | * |
| 1058 | * @access public |
| 1059 | * @param string |
| 1060 | * @return string |
| 1061 | */ |
| 1062 | function valid_ip($ip) |
| 1063 | { |
| 1064 | return $this->CI->input->valid_ip($ip); |
| 1065 | } |
| 1066 | |
| 1067 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1068 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1069 | /** |
| 1070 | * Alpha |
| 1071 | * |
| 1072 | * @access public |
| 1073 | * @param string |
| 1074 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1075 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1076 | function alpha($str) |
| 1077 | { |
| 1078 | return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE; |
| 1079 | } |
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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1082 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1083 | /** |
| 1084 | * Alpha-numeric |
| 1085 | * |
| 1086 | * @access public |
| 1087 | * @param string |
| 1088 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1089 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1090 | function alpha_numeric($str) |
| 1091 | { |
| 1092 | return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE; |
| 1093 | } |
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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1096 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1097 | /** |
| 1098 | * Alpha-numeric with underscores and dashes |
| 1099 | * |
| 1100 | * @access public |
| 1101 | * @param string |
| 1102 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1103 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1104 | function alpha_dash($str) |
| 1105 | { |
| 1106 | return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE; |
| 1107 | } |
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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1110 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1111 | /** |
| 1112 | * Numeric |
| 1113 | * |
| 1114 | * @access public |
| 1115 | * @param string |
| 1116 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1117 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1118 | function numeric($str) |
| 1119 | { |
| 1120 | return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str); |
| 1121 | |
| 1122 | } |
| 1123 | |
| 1124 | // -------------------------------------------------------------------- |
| 1125 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1126 | /** |
| 1127 | * Is Numeric |
| 1128 | * |
| 1129 | * @access public |
| 1130 | * @param string |
| 1131 | * @return bool |
| 1132 | */ |
| 1133 | function is_numeric($str) |
| 1134 | { |
| 1135 | return ( ! is_numeric($str)) ? FALSE : TRUE; |
| 1136 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1137 | |
| 1138 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1140 | /** |
| 1141 | * Integer |
| 1142 | * |
| 1143 | * @access public |
| 1144 | * @param string |
| 1145 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1146 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1147 | function integer($str) |
| 1148 | { |
| 1149 | return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str); |
| 1150 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1151 | |
| 1152 | // -------------------------------------------------------------------- |
| 1153 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1154 | /** |
| 1155 | * Is a Natural number (0,1,2,3, etc.) |
| 1156 | * |
| 1157 | * @access public |
| 1158 | * @param string |
| 1159 | * @return bool |
| 1160 | */ |
| 1161 | function is_natural($str) |
| 1162 | { |
| 1163 | return (bool)preg_match( '/^[0-9]+$/', $str); |
| 1164 | } |
| 1165 | |
| 1166 | // -------------------------------------------------------------------- |
| 1167 | |
| 1168 | /** |
| 1169 | * Is a Natural number, but not a zero (1,2,3, etc.) |
| 1170 | * |
| 1171 | * @access public |
| 1172 | * @param string |
| 1173 | * @return bool |
| 1174 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1175 | function is_natural_no_zero($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1176 | { |
| 1177 | if ( ! preg_match( '/^[0-9]+$/', $str)) |
| 1178 | { |
| 1179 | return FALSE; |
| 1180 | } |
| 1181 | |
| 1182 | if ($str == 0) |
| 1183 | { |
| 1184 | return FALSE; |
| 1185 | } |
| 1186 | |
| 1187 | return TRUE; |
| 1188 | } |
| 1189 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1190 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1191 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1192 | /** |
| 1193 | * Valid Base64 |
| 1194 | * |
| 1195 | * Tests a string for characters outside of the Base64 alphabet |
| 1196 | * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045 |
| 1197 | * |
| 1198 | * @access public |
| 1199 | * @param string |
| 1200 | * @return bool |
| 1201 | */ |
| 1202 | function valid_base64($str) |
| 1203 | { |
| 1204 | return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str); |
| 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 data for form |
| 1211 | * |
| 1212 | * This function allows HTML to be safely shown in a form. |
| 1213 | * Special characters are converted. |
| 1214 | * |
| 1215 | * @access public |
| 1216 | * @param string |
| 1217 | * @return string |
| 1218 | */ |
| 1219 | function prep_for_form($data = '') |
| 1220 | { |
| 1221 | if (is_array($data)) |
| 1222 | { |
| 1223 | foreach ($data as $key => $val) |
| 1224 | { |
| 1225 | $data[$key] = $this->prep_for_form($val); |
| 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 $data; |
| 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 | if ($this->_safe_form_data == FALSE OR $data === '') |
| 1232 | { |
| 1233 | return $data; |
| 1234 | } |
| 1235 | |
| 1236 | return str_replace(array("'", '"', '<', '>'), array("'", """, '<', '>'), stripslashes($data)); |
| 1237 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1238 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1239 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1240 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1241 | /** |
| 1242 | * Prep URL |
| 1243 | * |
| 1244 | * @access public |
| 1245 | * @param string |
| 1246 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1247 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1248 | function prep_url($str = '') |
| 1249 | { |
| 1250 | if ($str == 'http://' OR $str == '') |
| 1251 | { |
| 1252 | return ''; |
| 1253 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1254 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1255 | if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://') |
| 1256 | { |
| 1257 | $str = 'http://'.$str; |
| 1258 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1259 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1260 | return $str; |
| 1261 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1262 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1263 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1264 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1265 | /** |
| 1266 | * Strip Image Tags |
| 1267 | * |
| 1268 | * @access public |
| 1269 | * @param string |
| 1270 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1271 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1272 | function strip_image_tags($str) |
| 1273 | { |
| 1274 | return $this->CI->input->strip_image_tags($str); |
| 1275 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1276 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1277 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1278 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1279 | /** |
| 1280 | * XSS Clean |
| 1281 | * |
| 1282 | * @access public |
| 1283 | * @param string |
| 1284 | * @return string |
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 | function xss_clean($str) |
| 1287 | { |
Derek Jones | 3084167 | 2010-04-26 09:09:21 -0500 | [diff] [blame] | 1288 | if ( ! isset($this->CI->security)) |
Derek Jones | 5640a71 | 2010-04-23 11:22:40 -0500 | [diff] [blame] | 1289 | { |
Derek Jones | 3084167 | 2010-04-26 09:09:21 -0500 | [diff] [blame] | 1290 | $this->CI->load->library('security'); |
Derek Jones | 5640a71 | 2010-04-23 11:22:40 -0500 | [diff] [blame] | 1291 | } |
Derek Jones | 3084167 | 2010-04-26 09:09:21 -0500 | [diff] [blame] | 1292 | |
Derek Jones | 5640a71 | 2010-04-23 11:22:40 -0500 | [diff] [blame] | 1293 | return $this->CI->security->xss_clean($str); |
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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1297 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1298 | /** |
| 1299 | * Convert PHP tags to entities |
| 1300 | * |
| 1301 | * @access public |
| 1302 | * @param string |
| 1303 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1304 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1305 | function encode_php_tags($str) |
| 1306 | { |
| 1307 | return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('<?php', '<?PHP', '<?', '?>'), $str); |
| 1308 | } |
| 1309 | |
| 1310 | } |
| 1311 | // END Form Validation Class |
| 1312 | |
| 1313 | /* End of file Form_validation.php */ |
Rick Ellis | ec1b70f | 2008-08-26 19:21:27 +0000 | [diff] [blame] | 1314 | /* Location: ./system/libraries/Form_validation.php */ |