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