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