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,
52 * an array of directives based on loadArray(),
53 * or a string filename of an ini file.
54 * @return Configured HTMLPurifier_Config object
56 function create($config) {
57 if (is_a($config, 'HTMLPurifier_Config')) return $config;
58 $ret = HTMLPurifier_Config
::createDefault();
59 if (is_string($config)) $ret->loadIni($config);
60 elseif (is_array($config)) $ret->loadArray($config);
65 * Convenience constructor that creates a default configuration object.
67 * @return Default HTMLPurifier_Config object.
69 function createDefault() {
70 $definition =& HTMLPurifier_ConfigSchema
::instance();
71 $config = new HTMLPurifier_Config($definition);
76 * Retreives a value from the configuration.
77 * @param $namespace String namespace
78 * @param $key String key
80 function get($namespace, $key, $from_alias = false) {
81 if (!isset($this->def
->info
[$namespace][$key])) {
82 trigger_error('Cannot retrieve value of undefined directive',
86 if ($this->def
->info
[$namespace][$key]->class == 'alias') {
87 trigger_error('Cannot get value from aliased directive, use real name',
91 return $this->conf
[$namespace][$key];
95 * Retreives an array of directives to values from a given namespace
96 * @param $namespace String namespace
98 function getBatch($namespace) {
99 if (!isset($this->def
->info
[$namespace])) {
100 trigger_error('Cannot retrieve undefined namespace',
104 return $this->conf
[$namespace];
108 * Sets a value to configuration.
109 * @param $namespace String namespace
110 * @param $key String key
111 * @param $value Mixed value
113 function set($namespace, $key, $value, $from_alias = false) {
114 if (!isset($this->def
->info
[$namespace][$key])) {
115 trigger_error('Cannot set undefined directive to value',
119 if ($this->def
->info
[$namespace][$key]->class == 'alias') {
121 trigger_error('Double-aliases not allowed, please fix '.
124 $this->set($this->def
->info
[$namespace][$key]->namespace,
125 $this->def
->info
[$namespace][$key]->name
,
129 $value = $this->def
->validate(
131 $this->def
->info
[$namespace][$key]->type
,
132 $this->def
->info
[$namespace][$key]->allow_null
134 if (is_string($value)) {
135 // resolve value alias if defined
136 if (isset($this->def
->info
[$namespace][$key]->aliases
[$value])) {
137 $value = $this->def
->info
[$namespace][$key]->aliases
[$value];
139 if ($this->def
->info
[$namespace][$key]->allowed
!== true) {
140 // check to see if the value is allowed
141 if (!isset($this->def
->info
[$namespace][$key]->allowed
[$value])) {
142 trigger_error('Value not supported', E_USER_WARNING
);
147 if ($this->def
->isError($value)) {
148 trigger_error('Value is of invalid type', E_USER_WARNING
);
151 $this->conf
[$namespace][$key] = $value;
152 if ($namespace == 'HTML' ||
$namespace == 'Attr') {
153 // reset HTML definition if relevant attributes changed
154 $this->html_definition
= null;
156 if ($namespace == 'CSS') {
157 $this->css_definition
= null;
162 * Retrieves reference to the HTML definition.
163 * @param $raw Return a copy that has not been setup yet. Must be
164 * called before it's been setup, otherwise won't work.
166 function &getHTMLDefinition($raw = false) {
168 empty($this->html_definition
) ||
// hasn't ever been setup
169 ($raw && $this->html_definition
->setup
) // requesting new one
171 $this->html_definition
= new HTMLPurifier_HTMLDefinition($this);
172 if ($raw) return $this->html_definition
; // no setup!
174 if (!$this->html_definition
->setup
) $this->html_definition
->setup();
175 return $this->html_definition
;
179 * Retrieves reference to the CSS definition
181 function &getCSSDefinition() {
182 if ($this->css_definition
=== null) {
183 $this->css_definition
= new HTMLPurifier_CSSDefinition();
184 $this->css_definition
->setup($this);
186 return $this->css_definition
;
190 * Loads configuration values from an array with the following structure:
191 * Namespace.Directive => Value
192 * @param $config_array Configuration associative array
194 function loadArray($config_array) {
195 foreach ($config_array as $key => $value) {
196 $key = str_replace('_', '.', $key);
197 if (strpos($key, '.') !== false) {
199 list($namespace, $directive) = explode('.', $key);
200 $this->set($namespace, $directive, $value);
203 $namespace_values = $value;
204 foreach ($namespace_values as $directive => $value) {
205 $this->set($namespace, $directive, $value);
212 * Loads configuration values from an ini file
213 * @param $filename Name of ini file
215 function loadIni($filename) {
216 $array = parse_ini_file($filename, true);
217 $this->loadArray($array);