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); |
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 | |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame^] | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 631 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | continue; |
| 633 | } |
| 634 | |
| 635 | $result = $this->$rule($postdata, $param); |
| 636 | |
| 637 | if ($_in_array == TRUE) |
| 638 | { |
| 639 | $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; |
| 640 | } |
| 641 | else |
| 642 | { |
| 643 | $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result; |
| 644 | } |
| 645 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 646 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 647 | // Did the rule test negatively? If so, grab the error. |
| 648 | if ($result === FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 649 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 650 | if ( ! isset($this->_error_messages[$rule])) |
| 651 | { |
| 652 | if (FALSE === ($line = $this->CI->lang->line($rule))) |
| 653 | { |
| 654 | $line = 'Unable to access an error message corresponding to your field name.'; |
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 | } |
| 657 | else |
| 658 | { |
| 659 | $line = $this->_error_messages[$rule]; |
| 660 | } |
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 | // Is the parameter we are inserting into the error message the name |
| 663 | // of another field? If so we need to grab its "field label" |
| 664 | if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label'])) |
| 665 | { |
Pascal Kriete | c189583 | 2009-10-13 12:56:43 +0000 | [diff] [blame] | 666 | $param = $this->_translate_fieldname($this->_field_data[$param]['label']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 667 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 668 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 669 | // Build the error message |
| 670 | $message = sprintf($line, $this->_translate_fieldname($row['label']), $param); |
| 671 | |
| 672 | // Save the error message |
| 673 | $this->_field_data[$row['field']]['error'] = $message; |
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 | if ( ! isset($this->_error_array[$row['field']])) |
| 676 | { |
| 677 | $this->_error_array[$row['field']] = $message; |
| 678 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 679 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 680 | return; |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 686 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 687 | /** |
| 688 | * Translate a field name |
| 689 | * |
| 690 | * @access private |
| 691 | * @param string the field name |
| 692 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 693 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 694 | function _translate_fieldname($fieldname) |
| 695 | { |
| 696 | // Do we need to translate the field name? |
| 697 | // We look for the prefix lang: to determine this |
| 698 | if (substr($fieldname, 0, 5) == 'lang:') |
| 699 | { |
| 700 | // Grab the variable |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 701 | $line = substr($fieldname, 5); |
| 702 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 703 | // Were we able to translate the field name? If not we use $line |
| 704 | if (FALSE === ($fieldname = $this->CI->lang->line($line))) |
| 705 | { |
| 706 | return $line; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | return $fieldname; |
| 711 | } |
| 712 | |
| 713 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 714 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 715 | /** |
| 716 | * Get the value from a form |
| 717 | * |
| 718 | * Permits you to repopulate a form field with the value it was submitted |
| 719 | * with, or, if that value doesn't exist, with the default |
| 720 | * |
| 721 | * @access public |
| 722 | * @param string the field name |
| 723 | * @param string |
| 724 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 725 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 726 | function set_value($field = '', $default = '') |
| 727 | { |
| 728 | if ( ! isset($this->_field_data[$field])) |
| 729 | { |
| 730 | return $default; |
| 731 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 732 | |
Phil Sturgeon | 5c56180 | 2011-01-05 16:31:59 +0000 | [diff] [blame] | 733 | // If the data is an array output them one at a time. |
| 734 | // E.g: form_input('name[]', set_value('name[]'); |
| 735 | if (is_array($this->_field_data[$field]['postdata'])) |
| 736 | { |
| 737 | return array_shift($this->_field_data[$field]['postdata']); |
| 738 | } |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame^] | 739 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 740 | return $this->_field_data[$field]['postdata']; |
| 741 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 742 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 743 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 744 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 745 | /** |
| 746 | * Set Select |
| 747 | * |
| 748 | * Enables pull-down lists to be set to the value the user |
| 749 | * selected in the event of an error |
| 750 | * |
| 751 | * @access public |
| 752 | * @param string |
| 753 | * @param string |
| 754 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 755 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 756 | function set_select($field = '', $value = '', $default = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 757 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 758 | if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) |
| 759 | { |
| 760 | if ($default === TRUE AND count($this->_field_data) === 0) |
| 761 | { |
| 762 | return ' selected="selected"'; |
| 763 | } |
| 764 | return ''; |
| 765 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 766 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 767 | $field = $this->_field_data[$field]['postdata']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 768 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 769 | if (is_array($field)) |
| 770 | { |
| 771 | if ( ! in_array($value, $field)) |
| 772 | { |
| 773 | return ''; |
| 774 | } |
| 775 | } |
| 776 | else |
| 777 | { |
| 778 | if (($field == '' OR $value == '') OR ($field != $value)) |
| 779 | { |
| 780 | return ''; |
| 781 | } |
| 782 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 783 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 784 | return ' selected="selected"'; |
| 785 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 786 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 787 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 788 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 789 | /** |
| 790 | * Set Radio |
| 791 | * |
| 792 | * Enables radio buttons to be set to the value the user |
| 793 | * selected in the event of an error |
| 794 | * |
| 795 | * @access public |
| 796 | * @param string |
| 797 | * @param string |
| 798 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 799 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 800 | function set_radio($field = '', $value = '', $default = FALSE) |
| 801 | { |
| 802 | if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) |
| 803 | { |
| 804 | if ($default === TRUE AND count($this->_field_data) === 0) |
| 805 | { |
| 806 | return ' checked="checked"'; |
| 807 | } |
| 808 | return ''; |
| 809 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 810 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 811 | $field = $this->_field_data[$field]['postdata']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 812 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 813 | if (is_array($field)) |
| 814 | { |
| 815 | if ( ! in_array($value, $field)) |
| 816 | { |
| 817 | return ''; |
| 818 | } |
| 819 | } |
| 820 | else |
| 821 | { |
| 822 | if (($field == '' OR $value == '') OR ($field != $value)) |
| 823 | { |
| 824 | return ''; |
| 825 | } |
| 826 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 827 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 828 | return ' checked="checked"'; |
| 829 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 830 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 831 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 832 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 833 | /** |
| 834 | * Set Checkbox |
| 835 | * |
| 836 | * Enables checkboxes to be set to the value the user |
| 837 | * selected in the event of an error |
| 838 | * |
| 839 | * @access public |
| 840 | * @param string |
| 841 | * @param string |
| 842 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 843 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 844 | function set_checkbox($field = '', $value = '', $default = FALSE) |
| 845 | { |
| 846 | if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) |
| 847 | { |
| 848 | if ($default === TRUE AND count($this->_field_data) === 0) |
| 849 | { |
| 850 | return ' checked="checked"'; |
| 851 | } |
| 852 | return ''; |
| 853 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 854 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 855 | $field = $this->_field_data[$field]['postdata']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 856 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 857 | if (is_array($field)) |
| 858 | { |
| 859 | if ( ! in_array($value, $field)) |
| 860 | { |
| 861 | return ''; |
| 862 | } |
| 863 | } |
| 864 | else |
| 865 | { |
| 866 | if (($field == '' OR $value == '') OR ($field != $value)) |
| 867 | { |
| 868 | return ''; |
| 869 | } |
| 870 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 871 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 872 | return ' checked="checked"'; |
| 873 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 874 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 875 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 876 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 877 | /** |
| 878 | * Required |
| 879 | * |
| 880 | * @access public |
| 881 | * @param string |
| 882 | * @return bool |
| 883 | */ |
| 884 | function required($str) |
| 885 | { |
| 886 | if ( ! is_array($str)) |
| 887 | { |
| 888 | return (trim($str) == '') ? FALSE : TRUE; |
| 889 | } |
| 890 | else |
| 891 | { |
| 892 | return ( ! empty($str)); |
| 893 | } |
| 894 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 895 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 896 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 897 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 898 | /** |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 899 | * Performs a Regular Expression match test. |
| 900 | * |
| 901 | * @access public |
| 902 | * @param string |
| 903 | * @param regex |
| 904 | * @return bool |
| 905 | */ |
| 906 | function regex_match($str, $regex) |
| 907 | { |
| 908 | if ( ! preg_match($regex, $str)) |
| 909 | { |
| 910 | return FALSE; |
| 911 | } |
| 912 | |
| 913 | return TRUE; |
| 914 | } |
| 915 | |
| 916 | // -------------------------------------------------------------------- |
| 917 | |
| 918 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 919 | * Match one field to another |
| 920 | * |
| 921 | * @access public |
| 922 | * @param string |
| 923 | * @param field |
| 924 | * @return bool |
| 925 | */ |
| 926 | function matches($str, $field) |
| 927 | { |
| 928 | if ( ! isset($_POST[$field])) |
| 929 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 930 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 931 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 932 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 933 | $field = $_POST[$field]; |
| 934 | |
| 935 | return ($str !== $field) ? FALSE : TRUE; |
| 936 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 937 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 938 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 939 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 940 | /** |
| 941 | * Minimum Length |
| 942 | * |
| 943 | * @access public |
| 944 | * @param string |
| 945 | * @param value |
| 946 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 947 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 948 | function min_length($str, $val) |
| 949 | { |
| 950 | if (preg_match("/[^0-9]/", $val)) |
| 951 | { |
| 952 | return FALSE; |
| 953 | } |
| 954 | |
| 955 | if (function_exists('mb_strlen')) |
| 956 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 957 | return (mb_strlen($str) < $val) ? FALSE : TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 958 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 959 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 960 | return (strlen($str) < $val) ? FALSE : TRUE; |
| 961 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 962 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 963 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 964 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 965 | /** |
| 966 | * Max Length |
| 967 | * |
| 968 | * @access public |
| 969 | * @param string |
| 970 | * @param value |
| 971 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 972 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 973 | function max_length($str, $val) |
| 974 | { |
| 975 | if (preg_match("/[^0-9]/", $val)) |
| 976 | { |
| 977 | return FALSE; |
| 978 | } |
| 979 | |
| 980 | if (function_exists('mb_strlen')) |
| 981 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 982 | return (mb_strlen($str) > $val) ? FALSE : TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 983 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 984 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 985 | return (strlen($str) > $val) ? FALSE : TRUE; |
| 986 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 987 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 988 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 989 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 990 | /** |
| 991 | * Exact Length |
| 992 | * |
| 993 | * @access public |
| 994 | * @param string |
| 995 | * @param value |
| 996 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 997 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 998 | function exact_length($str, $val) |
| 999 | { |
| 1000 | if (preg_match("/[^0-9]/", $val)) |
| 1001 | { |
| 1002 | return FALSE; |
| 1003 | } |
| 1004 | |
| 1005 | if (function_exists('mb_strlen')) |
| 1006 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1007 | return (mb_strlen($str) != $val) ? FALSE : TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1008 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1009 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1010 | return (strlen($str) != $val) ? FALSE : TRUE; |
| 1011 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1012 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1013 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1014 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1015 | /** |
| 1016 | * Valid Email |
| 1017 | * |
| 1018 | * @access public |
| 1019 | * @param string |
| 1020 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1021 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1022 | function valid_email($str) |
| 1023 | { |
| 1024 | return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE; |
| 1025 | } |
| 1026 | |
| 1027 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1028 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1029 | /** |
| 1030 | * Valid Emails |
| 1031 | * |
| 1032 | * @access public |
| 1033 | * @param string |
| 1034 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1035 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1036 | function valid_emails($str) |
| 1037 | { |
| 1038 | if (strpos($str, ',') === FALSE) |
| 1039 | { |
| 1040 | return $this->valid_email(trim($str)); |
| 1041 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1042 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1043 | foreach(explode(',', $str) as $email) |
| 1044 | { |
| 1045 | if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE) |
| 1046 | { |
| 1047 | return FALSE; |
| 1048 | } |
| 1049 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1050 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1051 | return TRUE; |
| 1052 | } |
| 1053 | |
| 1054 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1055 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1056 | /** |
| 1057 | * Validate IP Address |
| 1058 | * |
| 1059 | * @access public |
| 1060 | * @param string |
| 1061 | * @return string |
| 1062 | */ |
| 1063 | function valid_ip($ip) |
| 1064 | { |
| 1065 | return $this->CI->input->valid_ip($ip); |
| 1066 | } |
| 1067 | |
| 1068 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1069 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1070 | /** |
| 1071 | * Alpha |
| 1072 | * |
| 1073 | * @access public |
| 1074 | * @param string |
| 1075 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1076 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1077 | function alpha($str) |
| 1078 | { |
| 1079 | return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE; |
| 1080 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1081 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1082 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1083 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1084 | /** |
| 1085 | * Alpha-numeric |
| 1086 | * |
| 1087 | * @access public |
| 1088 | * @param string |
| 1089 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1090 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1091 | function alpha_numeric($str) |
| 1092 | { |
| 1093 | return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE; |
| 1094 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1095 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1096 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1097 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1098 | /** |
| 1099 | * Alpha-numeric with underscores and dashes |
| 1100 | * |
| 1101 | * @access public |
| 1102 | * @param string |
| 1103 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1104 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1105 | function alpha_dash($str) |
| 1106 | { |
| 1107 | return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE; |
| 1108 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1109 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1110 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1111 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1112 | /** |
| 1113 | * Numeric |
| 1114 | * |
| 1115 | * @access public |
| 1116 | * @param string |
| 1117 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1118 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1119 | function numeric($str) |
| 1120 | { |
| 1121 | return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str); |
| 1122 | |
| 1123 | } |
| 1124 | |
| 1125 | // -------------------------------------------------------------------- |
| 1126 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1127 | /** |
| 1128 | * Is Numeric |
| 1129 | * |
| 1130 | * @access public |
| 1131 | * @param string |
| 1132 | * @return bool |
| 1133 | */ |
| 1134 | function is_numeric($str) |
| 1135 | { |
| 1136 | return ( ! is_numeric($str)) ? FALSE : TRUE; |
| 1137 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1138 | |
| 1139 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1140 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1141 | /** |
| 1142 | * Integer |
| 1143 | * |
| 1144 | * @access public |
| 1145 | * @param string |
| 1146 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1147 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1148 | function integer($str) |
| 1149 | { |
| 1150 | return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str); |
| 1151 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1152 | |
| 1153 | // -------------------------------------------------------------------- |
| 1154 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1155 | /** |
| 1156 | * Is a Natural number (0,1,2,3, etc.) |
| 1157 | * |
| 1158 | * @access public |
| 1159 | * @param string |
| 1160 | * @return bool |
| 1161 | */ |
| 1162 | function is_natural($str) |
| 1163 | { |
| 1164 | return (bool)preg_match( '/^[0-9]+$/', $str); |
| 1165 | } |
| 1166 | |
| 1167 | // -------------------------------------------------------------------- |
| 1168 | |
| 1169 | /** |
| 1170 | * Is a Natural number, but not a zero (1,2,3, etc.) |
| 1171 | * |
| 1172 | * @access public |
| 1173 | * @param string |
| 1174 | * @return bool |
| 1175 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1176 | function is_natural_no_zero($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1177 | { |
| 1178 | if ( ! preg_match( '/^[0-9]+$/', $str)) |
| 1179 | { |
| 1180 | return FALSE; |
| 1181 | } |
| 1182 | |
| 1183 | if ($str == 0) |
| 1184 | { |
| 1185 | return FALSE; |
| 1186 | } |
| 1187 | |
| 1188 | return TRUE; |
| 1189 | } |
| 1190 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1191 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1192 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1193 | /** |
| 1194 | * Valid Base64 |
| 1195 | * |
| 1196 | * Tests a string for characters outside of the Base64 alphabet |
| 1197 | * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045 |
| 1198 | * |
| 1199 | * @access public |
| 1200 | * @param string |
| 1201 | * @return bool |
| 1202 | */ |
| 1203 | function valid_base64($str) |
| 1204 | { |
| 1205 | return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str); |
| 1206 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1207 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1208 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1209 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1210 | /** |
| 1211 | * Prep data for form |
| 1212 | * |
| 1213 | * This function allows HTML to be safely shown in a form. |
| 1214 | * Special characters are converted. |
| 1215 | * |
| 1216 | * @access public |
| 1217 | * @param string |
| 1218 | * @return string |
| 1219 | */ |
| 1220 | function prep_for_form($data = '') |
| 1221 | { |
| 1222 | if (is_array($data)) |
| 1223 | { |
| 1224 | foreach ($data as $key => $val) |
| 1225 | { |
| 1226 | $data[$key] = $this->prep_for_form($val); |
| 1227 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1228 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1229 | return $data; |
| 1230 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1231 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1232 | if ($this->_safe_form_data == FALSE OR $data === '') |
| 1233 | { |
| 1234 | return $data; |
| 1235 | } |
| 1236 | |
| 1237 | return str_replace(array("'", '"', '<', '>'), array("'", """, '<', '>'), stripslashes($data)); |
| 1238 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1239 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1240 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1241 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1242 | /** |
| 1243 | * Prep URL |
| 1244 | * |
| 1245 | * @access public |
| 1246 | * @param string |
| 1247 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1248 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1249 | function prep_url($str = '') |
| 1250 | { |
| 1251 | if ($str == 'http://' OR $str == '') |
| 1252 | { |
| 1253 | return ''; |
| 1254 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1255 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1256 | if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://') |
| 1257 | { |
| 1258 | $str = 'http://'.$str; |
| 1259 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1260 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1261 | return $str; |
| 1262 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1263 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1264 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1265 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1266 | /** |
| 1267 | * Strip Image Tags |
| 1268 | * |
| 1269 | * @access public |
| 1270 | * @param string |
| 1271 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1272 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1273 | function strip_image_tags($str) |
| 1274 | { |
| 1275 | return $this->CI->input->strip_image_tags($str); |
| 1276 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1277 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1278 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1279 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1280 | /** |
| 1281 | * XSS Clean |
| 1282 | * |
| 1283 | * @access public |
| 1284 | * @param string |
| 1285 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1286 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1287 | function xss_clean($str) |
| 1288 | { |
Derek Jones | 3084167 | 2010-04-26 09:09:21 -0500 | [diff] [blame] | 1289 | if ( ! isset($this->CI->security)) |
Derek Jones | 5640a71 | 2010-04-23 11:22:40 -0500 | [diff] [blame] | 1290 | { |
Derek Jones | 3084167 | 2010-04-26 09:09:21 -0500 | [diff] [blame] | 1291 | $this->CI->load->library('security'); |
Derek Jones | 5640a71 | 2010-04-23 11:22:40 -0500 | [diff] [blame] | 1292 | } |
Derek Jones | 3084167 | 2010-04-26 09:09:21 -0500 | [diff] [blame] | 1293 | |
Derek Jones | 5640a71 | 2010-04-23 11:22:40 -0500 | [diff] [blame] | 1294 | return $this->CI->security->xss_clean($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1295 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1296 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1297 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1298 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1299 | /** |
| 1300 | * Convert PHP tags to entities |
| 1301 | * |
| 1302 | * @access public |
| 1303 | * @param string |
| 1304 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1305 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1306 | function encode_php_tags($str) |
| 1307 | { |
| 1308 | return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('<?php', '<?PHP', '<?', '?>'), $str); |
| 1309 | } |
| 1310 | |
| 1311 | } |
| 1312 | // END Form Validation Class |
| 1313 | |
| 1314 | /* End of file Form_validation.php */ |
Rick Ellis | ec1b70f | 2008-08-26 19:21:27 +0000 | [diff] [blame] | 1315 | /* Location: ./system/libraries/Form_validation.php */ |