Merge in PHP5 strict changes that are applicable to PHP4.
[htmlpurifier.git] / library / HTMLPurifier / Config.php
blob43c5681d327e521dda24cd9c6b0e5187f029507b
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 * Cached instance of HTMLPurifier_HTMLDefinition
31 var $html_definition;
33 /**
34 * Cached 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 config object based on a mixed var
49 * @static
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);
59 return $ret;
62 /**
63 * Convenience constructor that creates a default configuration object.
64 * @static
65 * @return Default HTMLPurifier_Config object.
67 function createDefault() {
68 $definition =& HTMLPurifier_ConfigSchema::instance();
69 $config = new HTMLPurifier_Config($definition);
70 return $config;
73 /**
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',
81 E_USER_WARNING);
82 return;
84 return $this->conf[$namespace][$key];
87 /**
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',
94 E_USER_WARNING);
95 return;
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',
109 E_USER_WARNING);
110 return;
112 $value = $this->def->validate(
113 $value,
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);
126 return;
130 if ($this->def->isError($value)) {
131 trigger_error('Value is of invalid type', E_USER_WARNING);
132 return;
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) {
168 // condensed form
169 list($namespace, $directive) = explode('.', $key);
170 $this->set($namespace, $directive, $value);
171 } else {
172 $namespace = $key;
173 $namespace_values = $value;
174 foreach ($namespace_values as $directive => $value) {
175 $this->set($namespace, $directive, $value);