Renamed ConfigDef to ConfigSchema. (Required major internal restructuring but should...
[htmlpurifier.git] / library / HTMLPurifier / Config.php
blob706dffcedc593ff5cf9bf67a2942bb29bf4532ce
1 <?php
3 /**
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.
8 *
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
18 /**
19 * Two-level associative array of configuration directives
21 var $conf;
23 /**
24 * Reference HTMLPurifier_ConfigSchema for value checking
26 var $def;
28 /**
29 * Instance of HTMLPurifier_HTMLDefinition
31 var $html_definition;
33 /**
34 * Instance of HTMLPurifier_CSSDefinition
36 var $css_definition;
38 /**
39 * @param $definition HTMLPurifier_ConfigSchema that defines what directives
40 * are allowed.
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
47 /**
48 * Convenience constructor that creates a default configuration object.
49 * @return Default HTMLPurifier_Config object.
51 function createDefault() {
52 $definition =& HTMLPurifier_ConfigSchema::instance();
53 $config = new HTMLPurifier_Config($definition);
54 return $config;
57 /**
58 * Retreives a value from the configuration.
59 * @param $namespace String namespace
60 * @param $key String key
62 function get($namespace, $key) {
63 if (!isset($this->conf[$namespace][$key])) {
64 trigger_error('Cannot retrieve value of undefined directive',
65 E_USER_WARNING);
66 return;
68 return $this->conf[$namespace][$key];
71 /**
72 * Sets a value to configuration.
73 * @param $namespace String namespace
74 * @param $key String key
75 * @param $value Mixed value
77 function set($namespace, $key, $value) {
78 if (!isset($this->conf[$namespace][$key])) {
79 trigger_error('Cannot set undefined directive to value',
80 E_USER_WARNING);
81 return;
83 $value = $this->def->validate($value,
84 $this->def->info[$namespace][$key]->type);
85 if (is_string($value)) {
86 // resolve value alias if defined
87 if (isset($this->def->info[$namespace][$key]->aliases[$value])) {
88 $value = $this->def->info[$namespace][$key]->aliases[$value];
90 if ($this->def->info[$namespace][$key]->allowed !== true) {
91 // check to see if the value is allowed
92 if (!isset($this->def->info[$namespace][$key]->allowed[$value])) {
93 trigger_error('Value not supported', E_USER_WARNING);
94 return;
98 if ($value === null) {
99 trigger_error('Value is of invalid type', E_USER_WARNING);
100 return;
102 $this->conf[$namespace][$key] = $value;
106 * Retrieves a copy of the HTML definition.
108 function getHTMLDefinition() {
109 if ($this->html_definition === null) {
110 $this->html_definition = new HTMLPurifier_HTMLDefinition();
111 $this->html_definition->setup($this);
113 return $this->html_definition;
117 * Retrieves a copy of the CSS definition
119 function getCSSDefinition() {
120 if ($this->css_definition === null) {
121 $this->css_definition = new HTMLPurifier_CSSDefinition();
122 $this->css_definition->setup($this);
124 return $this->css_definition;