Skip to content

Commit

Permalink
Convert all to new configuration get/set format.
Browse files Browse the repository at this point in the history
Signed-off-by: Edward Z. Yang <[email protected]>
  • Loading branch information
Edward Z. Yang committed Feb 21, 2009
1 parent b107eec commit 86ca784
Show file tree
Hide file tree
Showing 106 changed files with 504 additions and 815 deletions.
402 changes: 1 addition & 401 deletions configdoc/usage.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/dev-config-bcbreaks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ look something like %Filter.YouTube.Blacklist. While you could technically
set it with ('HTML', 'YouTube.Blacklist'), the logical extension
('HTML', 'YouTube', 'Blacklist') does not work.

[more changes coming]
The old API will still work, but will emit E_USER_NOTICEs.



Expand Down
4 changes: 2 additions & 2 deletions docs/examples/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
$config = HTMLPurifier_Config::createDefault();

// configuration goes here:
$config->set('Core', 'Encoding', 'UTF-8'); // replace with your encoding
$config->set('HTML', 'Doctype', 'XHTML 1.0 Transitional'); // replace with your doctype
$config->set('Core.Encoding', 'UTF-8'); // replace with your encoding
$config->set('HTML.Doctype', 'XHTML 1.0 Transitional'); // replace with your doctype

$purifier = new HTMLPurifier($config);

Expand Down
6 changes: 3 additions & 3 deletions library/HTMLPurifier.kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ function kses($string, $allowed_html, $allowed_protocols = null) {
$allowed_attributes["$element.$attribute"] = true;
}
}
$config->set('HTML', 'AllowedElements', $allowed_elements);
$config->set('HTML', 'AllowedAttributes', $allowed_attributes);
$config->set('HTML.AllowedElements', $allowed_elements);
$config->set('HTML.AllowedAttributes', $allowed_attributes);
$allowed_schemes = array();
if ($allowed_protocols !== null) {
$config->set('URI', 'AllowedSchemes', $allowed_protocols);
$config->set('URI.AllowedSchemes', $allowed_protocols);
}
$purifier = new HTMLPurifier($config);
return $purifier->purify($string);
Expand Down
2 changes: 1 addition & 1 deletion library/HTMLPurifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function purify($html, $config = null) {
$context->register('Generator', $this->generator);

// set up global context variables
if ($config->get('Core', 'CollectErrors')) {
if ($config->get('Core.CollectErrors')) {
// may get moved out if other facilities use it
$language_factory = HTMLPurifier_LanguageFactory::instance();
$language = $language_factory->create($config, $context);
Expand Down
2 changes: 1 addition & 1 deletion library/HTMLPurifier/AttrDef/CSS/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class HTMLPurifier_AttrDef_CSS_Color extends HTMLPurifier_AttrDef
public function validate($color, $config, $context) {

static $colors = null;
if ($colors === null) $colors = $config->get('Core', 'ColorKeywords');
if ($colors === null) $colors = $config->get('Core.ColorKeywords');

$color = trim($color);
if ($color === '') return false;
Expand Down
2 changes: 1 addition & 1 deletion library/HTMLPurifier/AttrDef/HTML/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class HTMLPurifier_AttrDef_HTML_Color extends HTMLPurifier_AttrDef
public function validate($string, $config, $context) {

static $colors = null;
if ($colors === null) $colors = $config->get('Core', 'ColorKeywords');
if ($colors === null) $colors = $config->get('Core.ColorKeywords');

$string = trim($string);

Expand Down
2 changes: 1 addition & 1 deletion library/HTMLPurifier/AttrDef/HTML/FrameTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HTMLPurifier_AttrDef_HTML_FrameTarget extends HTMLPurifier_AttrDef_Enum
public function __construct() {}

public function validate($string, $config, $context) {
if ($this->valid_values === false) $this->valid_values = $config->get('Attr', 'AllowedFrameTargets');
if ($this->valid_values === false) $this->valid_values = $config->get('Attr.AllowedFrameTargets');
return parent::validate($string, $config, $context);
}

Expand Down
10 changes: 5 additions & 5 deletions library/HTMLPurifier/AttrDef/HTML/ID.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ class HTMLPurifier_AttrDef_HTML_ID extends HTMLPurifier_AttrDef

public function validate($id, $config, $context) {

if (!$config->get('Attr', 'EnableID')) return false;
if (!$config->get('Attr.EnableID')) return false;

$id = trim($id); // trim it first

if ($id === '') return false;

$prefix = $config->get('Attr', 'IDPrefix');
$prefix = $config->get('Attr.IDPrefix');
if ($prefix !== '') {
$prefix .= $config->get('Attr', 'IDPrefixLocal');
$prefix .= $config->get('Attr.IDPrefixLocal');
// prevent re-appending the prefix
if (strpos($id, $prefix) !== 0) $id = $prefix . $id;
} elseif ($config->get('Attr', 'IDPrefixLocal') !== '') {
} elseif ($config->get('Attr.IDPrefixLocal') !== '') {
trigger_error('%Attr.IDPrefixLocal cannot be used unless '.
'%Attr.IDPrefix is set', E_USER_WARNING);
}
Expand All @@ -51,7 +51,7 @@ public function validate($id, $config, $context) {
$result = ($trim === '');
}

$regexp = $config->get('Attr', 'IDBlacklistRegexp');
$regexp = $config->get('Attr.IDBlacklistRegexp');
if ($regexp && preg_match($regexp, $id)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion library/HTMLPurifier/AttrDef/HTML/LinkTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct($name) {

public function validate($string, $config, $context) {

$allowed = $config->get('Attr', $this->name);
$allowed = $config->get('Attr.' . $this->name);
if (empty($allowed)) return false;

$string = $this->parseCDATA($string);
Expand Down
2 changes: 1 addition & 1 deletion library/HTMLPurifier/AttrDef/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function make($string) {

public function validate($uri, $config, $context) {

if ($config->get('URI', 'Disable')) return false;
if ($config->get('URI.Disable')) return false;

$uri = $this->parseCDATA($uri);

Expand Down
2 changes: 1 addition & 1 deletion library/HTMLPurifier/AttrTransform/BdoDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class HTMLPurifier_AttrTransform_BdoDir extends HTMLPurifier_AttrTransform

public function transform($attr, $config, $context) {
if (isset($attr['dir'])) return $attr;
$attr['dir'] = $config->get('Attr', 'DefaultTextDir');
$attr['dir'] = $config->get('Attr.DefaultTextDir');
return $attr;
}

Expand Down
8 changes: 4 additions & 4 deletions library/HTMLPurifier/AttrTransform/ImgRequired.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ public function transform($attr, $config, $context) {

$src = true;
if (!isset($attr['src'])) {
if ($config->get('Core', 'RemoveInvalidImg')) return $attr;
$attr['src'] = $config->get('Attr', 'DefaultInvalidImage');
if ($config->get('Core.RemoveInvalidImg')) return $attr;
$attr['src'] = $config->get('Attr.DefaultInvalidImage');
$src = false;
}

if (!isset($attr['alt'])) {
if ($src) {
$alt = $config->get('Attr', 'DefaultImageAlt');
$alt = $config->get('Attr.DefaultImageAlt');
if ($alt === null) {
$attr['alt'] = basename($attr['src']);
} else {
$attr['alt'] = $alt;
}
} else {
$attr['alt'] = $config->get('Attr', 'DefaultInvalidImageAlt');
$attr['alt'] = $config->get('Attr.DefaultInvalidImageAlt');
}
}

Expand Down
10 changes: 5 additions & 5 deletions library/HTMLPurifier/CSSDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ protected function doSetup($config) {
new HTMLPurifier_AttrDef_CSS_Percentage(true),
new HTMLPurifier_AttrDef_Enum(array('auto'))
));
$max = $config->get('CSS', 'MaxImgLength');
$max = $config->get('CSS.MaxImgLength');

$this->info['width'] =
$this->info['height'] =
Expand Down Expand Up @@ -211,15 +211,15 @@ protected function doSetup($config) {
// partial support
$this->info['white-space'] = new HTMLPurifier_AttrDef_Enum(array('nowrap'));

if ($config->get('CSS', 'Proprietary')) {
if ($config->get('CSS.Proprietary')) {
$this->doSetupProprietary($config);
}

if ($config->get('CSS', 'AllowTricky')) {
if ($config->get('CSS.AllowTricky')) {
$this->doSetupTricky($config);
}

$allow_important = $config->get('CSS', 'AllowImportant');
$allow_important = $config->get('CSS.AllowImportant');
// wrap all attr-defs with decorator that handles !important
foreach ($this->info as $k => $v) {
$this->info[$k] = new HTMLPurifier_AttrDef_CSS_ImportantDecorator($v, $allow_important);
Expand Down Expand Up @@ -272,7 +272,7 @@ protected function setupConfigStuff($config) {
// setup allowed elements
$support = "(for information on implementing this, see the ".
"support forums) ";
$allowed_attributes = $config->get('CSS', 'AllowedProperties');
$allowed_attributes = $config->get('CSS.AllowedProperties');
if ($allowed_attributes !== null) {
foreach ($this->info as $name => $d) {
if(!isset($allowed_attributes[$name])) unset($this->info[$name]);
Expand Down
2 changes: 1 addition & 1 deletion library/HTMLPurifier/ChildDef/Required.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function validateChildren($tokens_of_children, $config, $context) {
$all_whitespace = true;

// some configuration
$escape_invalid_children = $config->get('Core', 'EscapeInvalidChildren');
$escape_invalid_children = $config->get('Core.EscapeInvalidChildren');

// generator
$gen = new HTMLPurifier_Generator($config, $context);
Expand Down
80 changes: 60 additions & 20 deletions library/HTMLPurifier/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ class HTMLPurifier_Config
*/
protected $plist;

/**
* Whether or not a set is taking place due to an
* alias lookup.
*/
private $aliasMode;

/**
* Set to false if you do not want line and file numbers in errors
* (useful when unit testing)
*/
public $chatty = true;

/**
* @param $definition HTMLPurifier_ConfigSchema that defines what directives
* are allowed.
Expand Down Expand Up @@ -117,18 +129,21 @@ public static function createDefault() {
* @param $namespace String namespace
* @param $key String key
*/
public function get($namespace, $directive) {
$key = "$namespace.$directive";
public function get($key, $a = null) {
if ($a !== null) {
$this->triggerError("Using deprecated API: use \$config->get('$key.$a') instead", E_USER_WARNING);
$key = "$key.$a";
}
if (!$this->finalized) $this->autoFinalize ? $this->finalize() : $this->plist->squash(true);
if (!isset($this->def->info[$key])) {
// can't add % due to SimpleTest bug
trigger_error('Cannot retrieve value of undefined directive ' . htmlspecialchars($key),
$this->triggerError('Cannot retrieve value of undefined directive ' . htmlspecialchars($key),
E_USER_WARNING);
return;
}
if (isset($this->def->info[$key]->isAlias)) {
$d = $this->def->info[$key];
trigger_error('Cannot get value from aliased directive, use real name ' . $d->key,
$this->triggerError('Cannot get value from aliased directive, use real name ' . $d->key,
E_USER_ERROR);
return;
}
Expand All @@ -143,7 +158,7 @@ public function getBatch($namespace) {
if (!$this->finalized) $this->autoFinalize ? $this->finalize() : $this->plist->squash(true);
$full = $this->getAll();
if (!isset($full[$namespace])) {
trigger_error('Cannot retrieve undefined namespace ' . htmlspecialchars($namespace),
$this->triggerError('Cannot retrieve undefined namespace ' . htmlspecialchars($namespace),
E_USER_WARNING);
return;
}
Expand Down Expand Up @@ -196,26 +211,34 @@ public function getAll() {
* @param $key String key
* @param $value Mixed value
*/
public function set($namespace, $directive, $value, $from_alias = false) {
$key = "$namespace.$directive";
public function set($key, $value, $a = null) {
if (strpos($key, '.') === false) {
$namespace = $key;
$directive = $value;
$value = $a;
$key = "$key.$directive";
$this->triggerError("Using deprecated API: use \$config->set('$key', ...) instead", E_USER_NOTICE);
} else {
list($namespace) = explode('.', $key);
}
if ($this->isFinalized('Cannot set directive after finalization')) return;
if (!isset($this->def->info[$key])) {
trigger_error('Cannot set undefined directive ' . htmlspecialchars($key) . ' to value',
$this->triggerError('Cannot set undefined directive ' . htmlspecialchars($key) . ' to value',
E_USER_WARNING);
return;
}
$def = $this->def->info[$key];

if (isset($def->isAlias)) {
if ($from_alias) {
trigger_error('Double-aliases not allowed, please fix '.
if ($this->aliasMode) {
$this->triggerError('Double-aliases not allowed, please fix '.
'ConfigSchema bug with' . $key, E_USER_ERROR);
return;
}
list($alias_namespace, $alias_directive) = explode('.', $def->key, 2);
$this->set($alias_namespace, $alias_directive,
$value, true);
trigger_error("$key is an alias, preferred directive name is {$def->key}", E_USER_NOTICE);
$this->aliasMode = true;
$this->set($def->key, $value);
$this->aliasMode = false;
$this->triggerError("$key is an alias, preferred directive name is {$def->key}", E_USER_NOTICE);
return;
}

Expand All @@ -233,7 +256,7 @@ public function set($namespace, $directive, $value, $from_alias = false) {
try {
$value = $this->parser->parse($value, $type, $allow_null);
} catch (HTMLPurifier_VarParserException $e) {
trigger_error('Value for ' . $key . ' is of invalid type, should be ' . HTMLPurifier_VarParser::getTypeName($type), E_USER_WARNING);
$this->triggerError('Value for ' . $key . ' is of invalid type, should be ' . HTMLPurifier_VarParser::getTypeName($type), E_USER_WARNING);
return;
}
if (is_string($value) && is_object($def)) {
Expand All @@ -243,7 +266,7 @@ public function set($namespace, $directive, $value, $from_alias = false) {
}
// check to see if the value is allowed
if (isset($def->allowed) && !isset($def->allowed[$value])) {
trigger_error('Value not supported, valid values are: ' .
$this->triggerError('Value not supported, valid values are: ' .
$this->_listify($def->allowed), E_USER_WARNING);
return;
}
Expand Down Expand Up @@ -330,7 +353,7 @@ public function getDefinition($type, $raw = false) {
}
// quick abort if raw
if ($raw) {
if (is_null($this->get($type, 'DefinitionID'))) {
if (is_null($this->get($type . '.DefinitionID'))) {
// fatally error out if definition ID not set
throw new HTMLPurifier_Exception("Cannot retrieve raw version without specifying %$type.DefinitionID");
}
Expand All @@ -354,12 +377,12 @@ public function loadArray($config_array) {
$key = str_replace('_', '.', $key);
if (strpos($key, '.') !== false) {
list($namespace, $directive) = explode(".", $key, 2);
$this->set($namespace, $directive, $value);
$this->set($key, $value);
} else {
$namespace = $key;
$namespace_values = $value;
foreach ($namespace_values as $directive => $value) {
$this->set($namespace, $directive, $value);
$this->set($namespace .'.'. $directive, $value);
}
}
}
Expand Down Expand Up @@ -472,7 +495,7 @@ public function loadIni($filename) {
*/
public function isFinalized($error = false) {
if ($this->finalized && $error) {
trigger_error($error, E_USER_ERROR);
$this->triggerError($error, E_USER_ERROR);
}
return $this->finalized;
}
Expand All @@ -492,6 +515,23 @@ public function finalize() {
$this->finalized = true;
}

/**
* Produces a nicely formatted error message by supplying the
* stack frame information from two levels up and OUTSIDE of
* HTMLPurifier_Config.
*/
protected function triggerError($msg, $no) {
// determine previous stack frame
$backtrace = debug_backtrace();
if ($this->chatty && isset($backtrace[1])) {
$frame = $backtrace[1];
$extra = " on line {$frame['line']} in file {$frame['file']}";
} else {
$extra = '';
}
trigger_error($msg . $extra, $no);
}

}

// vim: et sw=4 sts=4
4 changes: 2 additions & 2 deletions library/HTMLPurifier/DefinitionCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct($type) {
public function generateKey($config) {
return $config->version . ',' . // possibly replace with function calls
$config->getBatchSerial($this->type) . ',' .
$config->get($this->type, 'DefinitionRev');
$config->get($this->type . '.DefinitionRev');
}

/**
Expand All @@ -46,7 +46,7 @@ public function isOld($key, $config) {
// versions match, ids match, check revision number
if (
$hash == $config->getBatchSerial($this->type) &&
$revision < $config->get($this->type, 'DefinitionRev')
$revision < $config->get($this->type . '.DefinitionRev')
) return true;
return false;
}
Expand Down
Loading

0 comments on commit 86ca784

Please sign in to comment.