4 * Configuration object that triggers customizable behavior.
6 * @warning This class is strongly defined: that means that the class
7 * will fail if an undefined directive is retrieved or set.
9 * @note Many classes that could (although many times don't) use the
10 * configuration object make it a mandatory parameter. This is
11 * because a configuration object should always be forwarded,
12 * otherwise, you run the risk of missing a parameter and then
13 * being stumped when a configuration directive doesn't work.
15 class HTMLPurifier_Config
19 * Two-level associative array of configuration directives
24 * Reference HTMLPurifier_ConfigSchema for value checking
29 * Cached instance of HTMLPurifier_HTMLDefinition
34 * Cached instance of HTMLPurifier_CSSDefinition
39 * @param $definition HTMLPurifier_ConfigSchema that defines what directives
42 function HTMLPurifier_Config(&$definition) {
43 $this->conf
= $definition->defaults
; // set up, copy in defaults
44 $this->def
= $definition; // keep a copy around for checking
48 * Convenience constructor that creates a config object based on a mixed var
50 * @param mixed $config Variable that defines the state of the config
51 * object. Can be: a HTMLPurifier_Config() object or
52 * an array of directives based on loadArray().
53 * @return Configured HTMLPurifier_Config object
55 function create($config) {
56 if (is_a($config, 'HTMLPurifier_Config')) return $config;
57 $ret = HTMLPurifier_Config
::createDefault();
58 if (is_array($config)) $ret->loadArray($config);
63 * Convenience constructor that creates a default configuration object.
65 * @return Default HTMLPurifier_Config object.
67 function createDefault() {
68 $definition =& HTMLPurifier_ConfigSchema
::instance();
69 $config = new HTMLPurifier_Config($definition);
74 * Retreives a value from the configuration.
75 * @param $namespace String namespace
76 * @param $key String key
78 function get($namespace, $key) {
79 if (!isset($this->def
->info
[$namespace][$key])) {
80 trigger_error('Cannot retrieve value of undefined directive',
84 return $this->conf
[$namespace][$key];
88 * Retreives an array of directives to values from a given namespace
89 * @param $namespace String namespace
91 function getBatch($namespace) {
92 if (!isset($this->def
->info
[$namespace])) {
93 trigger_error('Cannot retrieve undefined namespace',
97 return $this->conf
[$namespace];
101 * Sets a value to configuration.
102 * @param $namespace String namespace
103 * @param $key String key
104 * @param $value Mixed value
106 function set($namespace, $key, $value) {
107 if (!isset($this->def
->info
[$namespace][$key])) {
108 trigger_error('Cannot set undefined directive to value',
112 $value = $this->def
->validate(
114 $this->def
->info
[$namespace][$key]->type
,
115 $this->def
->info
[$namespace][$key]->allow_null
117 if (is_string($value)) {
118 // resolve value alias if defined
119 if (isset($this->def
->info
[$namespace][$key]->aliases
[$value])) {
120 $value = $this->def
->info
[$namespace][$key]->aliases
[$value];
122 if ($this->def
->info
[$namespace][$key]->allowed
!== true) {
123 // check to see if the value is allowed
124 if (!isset($this->def
->info
[$namespace][$key]->allowed
[$value])) {
125 trigger_error('Value not supported', E_USER_WARNING
);
130 if ($this->def
->isError($value)) {
131 trigger_error('Value is of invalid type', E_USER_WARNING
);
134 $this->conf
[$namespace][$key] = $value;
138 * Retrieves a copy of the HTML definition.
140 function getHTMLDefinition() {
141 if ($this->html_definition
=== null) {
142 $this->html_definition
= new HTMLPurifier_HTMLDefinition();
143 $this->html_definition
->setup($this);
145 return $this->html_definition
;
149 * Retrieves a copy of the CSS definition
151 function getCSSDefinition() {
152 if ($this->css_definition
=== null) {
153 $this->css_definition
= new HTMLPurifier_CSSDefinition();
154 $this->css_definition
->setup($this);
156 return $this->css_definition
;
160 * Loads configuration values from an array with the following structure:
161 * Namespace.Directive => Value
162 * @param $config_array Configuration associative array
164 function loadArray($config_array) {
165 foreach ($config_array as $key => $value) {
166 $key = str_replace('_', '.', $key);
167 if (strpos($key, '.') !== false) {
169 list($namespace, $directive) = explode('.', $key);
170 $this->set($namespace, $directive, $value);
173 $namespace_values = $value;
174 foreach ($namespace_values as $directive => $value) {
175 $this->set($namespace, $directive, $value);