108 $parent = $parent ? $parent : $definition->defaultPlist;
110 $this->def = $definition;
123 public static function create($config, $schema = null)
132 $ret =
new HTMLPurifier_Config($schema);
134 if (is_string($config)) {
135 $ret->loadIni($config);
136 } elseif (is_array($config))
$ret->loadArray($config);
169 public function get($key, $a = null)
173 "Using deprecated API: use \$config->get('$key.$a') instead",
178 if (!$this->finalized) {
181 if (!isset($this->def->info[$key])) {
184 'Cannot retrieve value of undefined directive ' . htmlspecialchars($key),
189 if (isset($this->def->info[$key]->isAlias)) {
190 $d = $this->def->info[$key];
192 'Cannot get value from aliased directive, use real name ' .
$d->key,
198 list($ns) = explode(
'.', $key);
199 if ($ns !== $this->lock) {
201 'Cannot get value of namespace ' . $ns .
' when lock for ' .
203 ' is active, this probably indicates a Definition setup method ' .
204 'is accessing directives that are not within its namespace',
210 return $this->plist->get($key);
222 if (!$this->finalized) {
228 'Cannot retrieve undefined namespace ' .
229 htmlspecialchars($namespace),
250 $batch = $this->
getBatch($namespace);
251 unset($batch[
'DefinitionRev']);
265 if (empty($this->serial)) {
278 if (!$this->finalized) {
282 foreach ($this->plist->squash() as $name => $value) {
283 list($ns, $key) = explode(
'.', $name, 2);
284 $ret[$ns][$key] = $value;
296 public function set($key, $value, $a = null)
298 if (strpos($key,
'.') ===
false) {
302 $key =
"$key.$directive";
303 $this->
triggerError(
"Using deprecated API: use \$config->set('$key', ...) instead", E_USER_NOTICE);
307 if ($this->
isFinalized(
'Cannot set directive after finalization')) {
310 if (!isset($this->def->info[$key])) {
312 'Cannot set undefined directive ' . htmlspecialchars($key) .
' to value',
317 $def = $this->def->info[$key];
319 if (isset(
$def->isAlias)) {
320 if ($this->aliasMode) {
322 'Double-aliases not allowed, please fix '.
323 'ConfigSchema bug with' . $key,
328 $this->aliasMode =
true;
329 $this->
set(
$def->key, $value);
330 $this->aliasMode =
false;
331 $this->
triggerError(
"$key is an alias, preferred directive name is {$def->key}", E_USER_NOTICE);
343 $allow_null = isset(
$def->allow_null);
347 $value = $this->parser->parse($value, $type, $allow_null);
350 'Value for ' . $key .
' is of invalid type, should be ' .
356 if (is_string($value) && is_object(
$def)) {
358 if (isset(
$def->aliases[$value])) {
359 $value =
$def->aliases[$value];
362 if (isset(
$def->allowed) && !isset(
$def->allowed[$value])) {
364 'Value not supported, valid values are: ' .
371 $this->plist->set($key, $value);
393 foreach ($lookup as $name => $b) {
396 return implode(
', ', $list);
475 if ($optimized && !$raw) {
478 if (!$this->finalized) {
485 $cache = $factory->create($type, $this);
491 if (!empty($this->definitions[$type])) {
492 $def = $this->definitions[$type];
498 if (
$def->optimized) {
499 $cache->add(
$def, $this);
505 $def = $cache->get($this);
508 $this->definitions[$type] =
$def;
518 $cache->add(
$def, $this);
527 if (is_null($this->
get($type .
'.DefinitionID'))) {
530 "Cannot retrieve raw version without specifying %$type.DefinitionID"
534 if (!empty($this->definitions[$type])) {
535 $def = $this->definitions[$type];
536 if (
$def->setup && !$optimized) {
537 $extra = $this->chatty ?
538 " (try moving this code block earlier in your initialization)" :
541 "Cannot retrieve raw definition after it has already been setup" .
545 if (
$def->optimized === null) {
546 $extra = $this->chatty ?
" (try flushing your cache)" :
"";
548 "Optimization status of definition is unknown" . $extra
551 if (
$def->optimized !== $optimized) {
552 $msg = $optimized ?
"optimized" :
"unoptimized";
553 $extra = $this->chatty ?
554 " (this backtrace is for the first inconsistent call, which was for a $msg raw definition)"
557 "Inconsistent use of optimized and unoptimized raw definition retrievals" . $extra
579 $def = $cache->get($this);
583 $this->definitions[$type] =
$def;
589 if (!is_null($this->
get($type .
'.DefinitionID'))) {
592 'Due to a documentation error in previous version of HTML Purifier, your ' .
593 'definitions are not being cached. If this is OK, you can remove the ' .
594 '%$type.DefinitionRev and %$type.DefinitionID declaration. Otherwise, ' .
595 'modify your code to use maybeGetRawDefinition, and test if the returned ' .
596 'value is null before making any edits (if it is null, that means that a ' .
597 'cached version is available, and no raw operations are necessary). See ' .
598 '<a href="http://htmlpurifier.org/docs/enduser-customize.html#optimized">' .
599 'Customize</a> for more details',
604 "Useless DefinitionID declaration",
612 $def->optimized = $optimized;
629 if ($type ==
'HTML') {
631 } elseif ($type ==
'CSS') {
633 } elseif ($type ==
'URI') {
637 "Definition of $type type not supported"
640 $this->definitions[$type] =
$def;
672 if ($this->
isFinalized(
'Cannot load directives after finalization')) {
675 foreach ($config_array as $key => $value) {
676 $key = str_replace(
'_',
'.', $key);
677 if (strpos($key,
'.') !==
false) {
678 $this->
set($key, $value);
681 $namespace_values = $value;
682 foreach ($namespace_values as $directive => $value2) {
683 $this->
set(
$namespace .
'.'. $directive, $value2);
704 if ($allowed !==
true) {
705 if (is_string($allowed)) {
706 $allowed = array($allowed);
708 $allowed_ns = array();
709 $allowed_directives = array();
710 $blacklisted_directives = array();
711 foreach ($allowed as $ns_or_directive) {
712 if (strpos($ns_or_directive,
'.') !==
false) {
714 if ($ns_or_directive[0] ==
'-') {
715 $blacklisted_directives[substr($ns_or_directive, 1)] =
true;
717 $allowed_directives[$ns_or_directive] =
true;
721 $allowed_ns[$ns_or_directive] =
true;
726 foreach ($schema->info as $key =>
$def) {
727 list($ns, $directive) = explode(
'.', $key, 2);
728 if ($allowed !==
true) {
729 if (isset($blacklisted_directives[
"$ns.$directive"])) {
732 if (!isset($allowed_directives[
"$ns.$directive"]) && !isset($allowed_ns[$ns])) {
736 if (isset(
$def->isAlias)) {
739 if ($directive ==
'DefinitionID' || $directive ==
'DefinitionRev') {
742 $ret[] = array($ns, $directive);
759 public static function loadArrayFromForm($array, $index =
false, $allowed =
true, $mq_fix =
true, $schema = null)
792 public static function prepareArrayFromForm($array, $index =
false, $allowed =
true, $mq_fix =
true, $schema = null)
794 if ($index !==
false) {
795 $array = (isset($array[$index]) && is_array($array[$index])) ? $array[$index] : array();
797 $mq = $mq_fix && function_exists(
'get_magic_quotes_gpc') && get_magic_quotes_gpc();
801 foreach ($allowed as $key) {
802 list($ns, $directive) = $key;
803 $skey =
"$ns.$directive";
804 if (!empty($array[
"Null_$skey"])) {
805 $ret[$ns][$directive] = null;
808 if (!isset($array[$skey])) {
811 $value = $mq ? stripslashes($array[$skey]) : $array[$skey];
812 $ret[$ns][$directive] = $value;
824 if ($this->
isFinalized(
'Cannot load directives after finalization')) {
827 $array = parse_ini_file(
$filename,
true);
840 if ($this->finalized && $error) {
855 $this->plist->squash(
true);
864 $this->finalized =
true;
865 $this->parser = null;
880 $trace = debug_backtrace();
882 for ($i = 0, $c = count($trace); $i < $c - 1; $i++) {
884 if ($trace[$i + 1][
'class'] ===
'HTMLPurifier_Config') {
888 $extra =
" invoked on line {$frame['line']} in file {$frame['file']}";
892 trigger_error($msg . $extra, $no);