1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
<?php
require_once 'Horde/Util.php';
require_once 'Horde/Array.php';
/**
* Variables:: class.
*
* $Horde: framework/Util/Variables.php,v 1.8.10.6 2006/03/28 12:31:12 jan Exp $
*
* @author Robert E. Coyle <robertecoyle@hotmail.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @package Horde_Util
*/
class Variables {
var $_vars;
var $_expectedVariables = array();
function Variables($vars = array())
{
if (is_null($vars)) {
$vars = Util::dispelMagicQuotes($_REQUEST);
}
if (isset($vars['_formvars'])) {
$this->_expectedVariables = @unserialize($vars['_formvars']);
unset($vars['_formvars']);
}
$this->_vars = $vars;
}
function &getDefaultVariables()
{
$vars = &new Variables(Util::dispelMagicQuotes($_REQUEST));
return $vars;
}
function exists($varname)
{
if (count($this->_expectedVariables) &&
$this->_exists($this->_expectedVariables, $varname, false)) {
return true;
}
return $this->_exists($this->_vars, $varname, false);
}
function get($varname)
{
$this->_getExists($this->_vars, $varname, $value);
return $value;
}
function getExists($varname, &$exists)
{
$exists = $this->_getExists($this->_vars, $varname, $value);
return $value;
}
function set($varname, $value)
{
$keys = array();
if (!Horde_Array::getArrayParts($varname, $base, $keys)) {
$this->_vars[$varname] = $value;
} else {
array_unshift($keys, $base);
$place = &$this->_vars;
while (count($keys)) {
$key = array_shift($keys);
if (!isset($place[$key])) {
$place[$key] = array();
}
$place = &$place[$key];
}
$place = $value;
}
}
function merge($vars)
{
foreach ($vars as $varname => $value) {
$this->set($varname, $value);
}
}
function add($varname, $value)
{
if ($this->exists($varname)) {
return false;
}
$this->_vars[$varname] = $value;
}
function remove($varname)
{
Horde_Array::getArrayParts($varname, $base, $keys);
if (!is_null($base)) {
$ptr = &$this->_vars[$base];
$end = count($keys) - 1;
foreach ($keys as $key => $val) {
if (!isset($ptr[$val])) {
break;
}
if ($end == $key) {
array_splice($ptr, array_search($val, array_keys($ptr)), 1);
} else {
$ptr = &$ptr[$val];
}
}
} else {
unset($this->_vars[$varname]);
}
}
/**
* Find out whether or not $varname was set in $array.
*
* @access private
*
* @param array $array The array to search in (usually
* either $this->_vars or
* $this->_expectedVariables).
* @param string $varname The name of the variable to look
* for.
* @param boolean $checkExpectedVariables If we don't find $varname,
* should we check
* $this->_expectedVariables to see
* if should have existed (like a
* checkbox or select multiple).
*
* @return Whether or not the variable was set (or, if we've checked
* $this->_expectedVariables, should have been set).
*/
function _exists($array, $varname, $checkExpectedVariables = true)
{
return $this->_getExists($array, $varname, $value, $checkExpectedVariables);
}
/**
* Fetch the requested variable ($varname) into $value, and return
* whether or not the variable was set in $array.
*
* @access private
*
* @param array $array The array to search in (usually
* either $this->_vars or
* $this->_expectedVariables).
* @param string $varname The name of the variable to look
* for.
* @param mixed &$value $varname's value gets assigned
* to.
* this variable.
* @param boolean $checkExpectedVariables If we don't find $varname,
* should we check
* $this->_expectedVariables to see
* if it should have existed (like
* a checkbox or select multiple).
*
* @return Whether or not the variable was set (or, if we've checked
* $this->_expectedVariables, should have been set).
*/
function _getExists($array, $varname, &$value, $checkExpectedVariables = true)
{
if (Horde_Array::getArrayParts($varname, $base, $keys)) {
if (!isset($array[$base])) {
$value = null;
// If we're supposed to check $this->_expectedVariables, do so,
// but make sure not to check it again.
return $checkExpectedVariables ? $this->_exists($this->_expectedVariables, $varname, false) : false;
} else {
$searchspace = &$array[$base];
while (count($keys)) {
$key = array_shift($keys);
if (!isset($searchspace[$key])) {
$value = null;
// If we're supposed to check
// $this->_expectedVariables, do so, but make
// sure not to check it again.
return $checkExpectedVariables ? $this->_exists($this->_expectedVariables, $varname, false) : false;
}
$searchspace = &$searchspace[$key];
}
$value = $searchspace;
return true;
}
} else {
$value = isset($array[$varname]) ? $array[$varname] : null;
if (!is_null($value)) {
return true;
} elseif ($checkExpectedVariables) {
// If we're supposed to check
// $this->_expectedVariables, do so, but make sure not
// to check it again.
return $this->_exists($this->_expectedVariables, $varname, false);
} else {
return false;
}
}
}
}
|