[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Copyright 2009-2014 Horde LLC (http://www.horde.org/) 4 * 5 * See the enclosed file COPYING for license information (LGPL). If you 6 * did not receive this file, see http://www.horde.org/licenses/lgpl21. 7 * 8 * @category Horde 9 * @copyright 2009-2014 Horde LLC 10 * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 11 * @package Util 12 */ 13 14 /** 15 * An OO-way to access form variables. 16 * 17 * @author Robert E. Coyle <robertecoyle@hotmail.com> 18 * @author Chuck Hagenbuch <chuck@horde.org> 19 * @author Michael Slusarz <slusarz@horde.org> 20 * @category Horde 21 * @copyright 2009-2014 Horde LLC 22 * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 23 * @package Util 24 */ 25 class Horde_Variables implements ArrayAccess, Countable, IteratorAggregate 26 { 27 /** 28 * The list of expected variables. 29 * 30 * @var array 31 */ 32 protected $_expected = array(); 33 34 /** 35 * Has the input been sanitized? 36 * 37 * @var boolean 38 */ 39 protected $_sanitized = false; 40 41 /** 42 * Array of form variables. 43 * 44 * @var array 45 */ 46 protected $_vars; 47 48 /** 49 * Returns a Horde_Variables object populated with the form input. 50 * 51 * @param string $sanitize Sanitize the input variables? 52 * 53 * @return Horde_Variables Variables object. 54 */ 55 static public function getDefaultVariables($sanitize = false) 56 { 57 return new self(null, $sanitize); 58 } 59 60 /** 61 * Constructor. 62 * 63 * @param array $vars The list of form variables (if null, defaults 64 * to PHP's $_REQUEST value). If '_formvars' 65 * exists, it must be a JSON encoded array that 66 * contains the list of allowed form variables. 67 * @param string $sanitize Sanitize the input variables? 68 */ 69 public function __construct($vars = array(), $sanitize = false) 70 { 71 if (is_null($vars)) { 72 $request_copy = $_REQUEST; 73 $vars = Horde_Util::dispelMagicQuotes($request_copy); 74 } 75 76 if (isset($vars['_formvars'])) { 77 $this->_expected = @json_decode($vars['_formvars'], true); 78 unset($vars['_formvars']); 79 } 80 81 $this->_vars = $vars; 82 83 if ($sanitize) { 84 $this->sanitize(); 85 } 86 } 87 88 /** 89 * Sanitize the form input. 90 */ 91 public function sanitize() 92 { 93 if (!$this->_sanitized) { 94 foreach (array_keys($this->_vars) as $key) { 95 $this->$key = $this->filter($key); 96 } 97 $this->_sanitized = true; 98 } 99 } 100 101 /** 102 * Alias of isset(). 103 * 104 * @see __isset() 105 */ 106 public function exists($varname) 107 { 108 return isset($this->$varname); 109 } 110 111 /** 112 * isset() implementation. 113 * 114 * @param string $varname The form variable name. 115 * 116 * @return boolean Does $varname form variable exist? 117 */ 118 public function __isset($varname) 119 { 120 return count($this->_expected) 121 ? $this->_getExists($this->_expected, $varname, $value) 122 : $this->_getExists($this->_vars, $varname, $value); 123 } 124 125 /** 126 * Implements isset() for ArrayAccess interface. 127 * 128 * @see __isset() 129 */ 130 public function offsetExists($field) 131 { 132 return $this->__isset($field); 133 } 134 135 /** 136 * Returns the value of a given form variable. 137 * 138 * @param string $varname The form variable name. 139 * @param string $default The default form variable value. 140 * 141 * @return mixed The form variable, or $default if it doesn't exist. 142 */ 143 public function get($varname, $default = null) 144 { 145 return $this->_getExists($this->_vars, $varname, $value) 146 ? $value 147 : $default; 148 } 149 150 /** 151 * Returns the value of a given form variable. 152 * 153 * @param string $varname The form variable name. 154 * 155 * @return mixed The form variable, or null if it doesn't exist. 156 */ 157 public function __get($varname) 158 { 159 $this->_getExists($this->_vars, $varname, $value); 160 return $value; 161 } 162 163 /** 164 * Implements getter for ArrayAccess interface. 165 * 166 * @see __get() 167 */ 168 public function offsetGet($field) 169 { 170 return $this->__get($field); 171 } 172 173 /** 174 * Given a variable name, returns the value and sets a variable indicating 175 * whether the value exists in the form data. 176 * 177 * @param string $varname The form variable name. 178 * @param boolean &$exists Reference to variable that will indicate 179 * whether $varname existed in form data. 180 * 181 * @return mixed The form variable, or null if it doesn't exist. 182 */ 183 public function getExists($varname, &$exists) 184 { 185 $exists = $this->_getExists($this->_vars, $varname, $value); 186 return $value; 187 } 188 189 /** 190 * Sets the value of a given form variable. 191 * 192 * @see __set() 193 */ 194 public function set($varname, $value) 195 { 196 $this->$varname = $value; 197 } 198 199 /** 200 * Sets the value of a given form variable. 201 * 202 * @param string $varname The form variable name. 203 * @param mixed $value The value to set. 204 */ 205 public function __set($varname, $value) 206 { 207 $keys = array(); 208 209 if (Horde_Array::getArrayParts($varname, $base, $keys)) { 210 array_unshift($keys, $base); 211 $place = &$this->_vars; 212 $i = count($keys); 213 214 while ($i--) { 215 $key = array_shift($keys); 216 if (!isset($place[$key])) { 217 $place[$key] = array(); 218 } 219 $place = &$place[$key]; 220 } 221 222 $place = $value; 223 } else { 224 $this->_vars[$varname] = $value; 225 } 226 } 227 228 /** 229 * Implements setter for ArrayAccess interface. 230 * 231 * @see __set() 232 */ 233 public function offsetSet($field, $value) 234 { 235 $this->__set($field, $value); 236 } 237 238 /** 239 * Deletes a given form variable. 240 * 241 * @see __unset() 242 */ 243 public function remove($varname) 244 { 245 unset($this->$varname); 246 } 247 248 /** 249 * Deletes a given form variable. 250 * 251 * @param string $varname The form variable name. 252 */ 253 public function __unset($varname) 254 { 255 Horde_Array::getArrayParts($varname, $base, $keys); 256 257 if (is_null($base)) { 258 unset($this->_vars[$varname]); 259 } else { 260 $ptr = &$this->_vars[$base]; 261 $end = count($keys) - 1; 262 foreach ($keys as $key => $val) { 263 if (!isset($ptr[$val])) { 264 break; 265 } 266 if ($end == $key) { 267 array_splice($ptr, array_search($val, array_keys($ptr)), 1); 268 } else { 269 $ptr = &$ptr[$val]; 270 } 271 } 272 } 273 } 274 275 /** 276 * Implements unset() for ArrayAccess interface. 277 * 278 * @see __unset() 279 */ 280 public function offsetUnset($field) 281 { 282 $this->__unset($field); 283 } 284 285 /** 286 * Merges a list of variables into the current form variable list. 287 * 288 * @param array $vars Form variables. 289 */ 290 public function merge($vars) 291 { 292 foreach ($vars as $varname => $value) { 293 $this->$varname = $value; 294 } 295 } 296 297 /** 298 * Set $varname to $value ONLY if it's not already present. 299 * 300 * @param string $varname The form variable name. 301 * @param mixed $value The value to set. 302 * 303 * @return boolean True if the value was altered. 304 */ 305 public function add($varname, $value) 306 { 307 if ($this->exists($varname)) { 308 return false; 309 } 310 311 $this->_vars[$varname] = $value; 312 return true; 313 } 314 315 /** 316 * Filters a form value so that it can be used in HTML output. 317 * 318 * @param string $varname The form variable name. 319 * 320 * @return mixed The filtered variable, or null if it doesn't exist. 321 */ 322 public function filter($varname) 323 { 324 $val = $this->$varname; 325 326 if (is_null($val) || $this->_sanitized) { 327 return $val; 328 } 329 330 return is_array($val) 331 ? filter_var_array($val, FILTER_SANITIZE_STRING) 332 : filter_var($val, FILTER_SANITIZE_STRING); 333 } 334 335 /* Protected methods. */ 336 337 /** 338 * Fetch the requested variable ($varname) into $value, and return 339 * whether or not the variable was set in $array. 340 * 341 * @param array $array The array to search in (usually either 342 * $this->_vars or $this->_expected). 343 * @param string $varname The name of the variable to look for. 344 * @param mixed &$value $varname's value gets assigned to this variable. 345 * 346 * @return boolean Whether or not the variable was set (or, if we've 347 * checked $this->_expected, should have been set). 348 */ 349 protected function _getExists($array, $varname, &$value) 350 { 351 if (Horde_Array::getArrayParts($varname, $base, $keys)) { 352 if (!isset($array[$base])) { 353 $value = null; 354 return false; 355 } 356 357 $searchspace = &$array[$base]; 358 $i = count($keys); 359 360 while ($i--) { 361 $key = array_shift($keys); 362 if (!isset($searchspace[$key])) { 363 $value = null; 364 return false; 365 } 366 $searchspace = &$searchspace[$key]; 367 } 368 $value = $searchspace; 369 370 return true; 371 } 372 373 $value = isset($array[$varname]) 374 ? $array[$varname] 375 : null; 376 377 return !is_null($value); 378 } 379 380 /* Countable methods. */ 381 382 /** 383 */ 384 public function count() 385 { 386 return count($this->_vars); 387 } 388 389 /* IteratorAggregate method. */ 390 391 public function getIterator() 392 { 393 return new ArrayIterator($this->_vars); 394 } 395 396 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |