2006-10-01 18:14:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registry object that contains information about the current context.
|
2007-06-24 04:22:28 +00:00
|
|
|
* @warning Is a bit buggy when variables are set to null: it thinks
|
|
|
|
* they don't exist! So use false instead, please.
|
2008-04-23 02:40:17 +00:00
|
|
|
* @note Since the variables Context deals with may not be objects,
|
|
|
|
* references are very important here! Do not remove!
|
2006-10-01 18:14:08 +00:00
|
|
|
*/
|
|
|
|
class HTMLPurifier_Context
|
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-10-01 18:14:08 +00:00
|
|
|
/**
|
|
|
|
* Private array that stores the references.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @type array
|
2006-10-01 18:14:08 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
private $_storage = array();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-10-01 18:14:08 +00:00
|
|
|
/**
|
|
|
|
* Registers a variable into the context.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param string $name String name
|
|
|
|
* @param mixed $ref Reference to variable to be registered
|
2006-10-01 18:14:08 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function register($name, &$ref)
|
|
|
|
{
|
2013-10-13 20:05:21 +00:00
|
|
|
if (array_key_exists($name, $this->_storage)) {
|
2013-07-16 11:56:14 +00:00
|
|
|
trigger_error(
|
|
|
|
"Name $name produces collision, cannot re-register",
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
2006-10-01 18:14:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->_storage[$name] =& $ref;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-10-01 18:14:08 +00:00
|
|
|
/**
|
|
|
|
* Retrieves a variable reference from the context.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param string $name String name
|
|
|
|
* @param bool $ignore_error Boolean whether or not to ignore error
|
|
|
|
* @return mixed
|
2006-10-01 18:14:08 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function &get($name, $ignore_error = false)
|
|
|
|
{
|
2013-10-13 20:05:21 +00:00
|
|
|
if (!array_key_exists($name, $this->_storage)) {
|
2007-06-25 01:08:57 +00:00
|
|
|
if (!$ignore_error) {
|
2013-07-16 11:56:14 +00:00
|
|
|
trigger_error(
|
|
|
|
"Attempted to retrieve non-existent variable $name",
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
2007-06-25 01:08:57 +00:00
|
|
|
}
|
2006-11-16 23:58:33 +00:00
|
|
|
$var = null; // so we can return by reference
|
|
|
|
return $var;
|
2006-10-01 18:14:08 +00:00
|
|
|
}
|
|
|
|
return $this->_storage[$name];
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-10-01 18:14:08 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Destroys a variable in the context.
|
|
|
|
* @param string $name String name
|
2006-10-01 18:14:08 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function destroy($name)
|
|
|
|
{
|
2013-10-13 20:05:21 +00:00
|
|
|
if (!array_key_exists($name, $this->_storage)) {
|
2013-07-16 11:56:14 +00:00
|
|
|
trigger_error(
|
|
|
|
"Attempted to destroy non-existent variable $name",
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
2006-10-01 18:14:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
unset($this->_storage[$name]);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-10-01 18:39:48 +00:00
|
|
|
/**
|
|
|
|
* Checks whether or not the variable exists.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param string $name String name
|
|
|
|
* @return bool
|
2006-10-01 18:39:48 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function exists($name)
|
|
|
|
{
|
2013-10-13 20:05:21 +00:00
|
|
|
return array_key_exists($name, $this->_storage);
|
2006-10-01 18:39:48 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-10-21 18:18:36 +00:00
|
|
|
/**
|
|
|
|
* Loads a series of variables from an associative array
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param array $context_array Assoc array of variables to load
|
2006-10-21 18:18:36 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function loadArray($context_array)
|
|
|
|
{
|
2006-10-21 18:18:36 +00:00
|
|
|
foreach ($context_array as $key => $discard) {
|
|
|
|
$this->register($key, $context_array[$key]);
|
|
|
|
}
|
|
|
|
}
|
2006-10-01 18:14:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|