2006-08-11 20:23:41 +00:00
|
|
|
<?php
|
|
|
|
|
2008-01-27 01:54:41 +00:00
|
|
|
if (!defined('HTMLPURIFIER_SCHEMA_STRICT')) define('HTMLPURIFIER_SCHEMA_STRICT', false);
|
|
|
|
|
2006-08-19 23:06:59 +00:00
|
|
|
/**
|
|
|
|
* Configuration definition, defines directives and their defaults.
|
|
|
|
*/
|
2006-09-16 22:36:58 +00:00
|
|
|
class HTMLPurifier_ConfigSchema {
|
2006-08-11 20:23:41 +00:00
|
|
|
|
2006-08-19 23:06:59 +00:00
|
|
|
/**
|
2006-08-27 18:49:16 +00:00
|
|
|
* Defaults of the directives and namespaces.
|
2006-08-19 23:06:59 +00:00
|
|
|
* @note This shares the exact same structure as HTMLPurifier_Config::$conf
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $defaults = array();
|
2006-08-27 18:49:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Definition of the directives.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $info = array();
|
2006-08-11 20:23:41 +00:00
|
|
|
|
2006-08-27 18:49:16 +00:00
|
|
|
/**
|
|
|
|
* Definition of namespaces.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $info_namespace = array();
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2008-01-27 19:41:40 +00:00
|
|
|
/**
|
|
|
|
* Application-wide singleton
|
|
|
|
*/
|
|
|
|
static protected $singleton;
|
|
|
|
|
2008-03-04 15:06:00 +00:00
|
|
|
/**
|
|
|
|
* Variable parser.
|
|
|
|
*/
|
|
|
|
protected $parser;
|
|
|
|
|
2006-08-27 18:49:16 +00:00
|
|
|
/**
|
|
|
|
* Lookup table of allowed types.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $types = array(
|
2006-09-10 22:17:22 +00:00
|
|
|
'string' => 'String',
|
|
|
|
'istring' => 'Case-insensitive string',
|
2007-06-29 01:54:48 +00:00
|
|
|
'text' => 'Text',
|
|
|
|
'itext' => 'Case-insensitive text',
|
2006-09-10 22:17:22 +00:00
|
|
|
'int' => 'Integer',
|
|
|
|
'float' => 'Float',
|
|
|
|
'bool' => 'Boolean',
|
|
|
|
'lookup' => 'Lookup array',
|
|
|
|
'list' => 'Array list',
|
|
|
|
'hash' => 'Associative array',
|
|
|
|
'mixed' => 'Mixed'
|
2006-08-27 18:49:16 +00:00
|
|
|
);
|
|
|
|
|
2008-03-04 15:06:00 +00:00
|
|
|
public function __construct() {
|
2008-03-05 03:51:09 +00:00
|
|
|
$this->parser = new HTMLPurifier_VarParser_Flexible();
|
2008-03-04 15:06:00 +00:00
|
|
|
}
|
|
|
|
|
2006-08-19 23:06:59 +00:00
|
|
|
/**
|
2008-02-10 20:34:39 +00:00
|
|
|
* Unserializes the default ConfigSchema.
|
2006-08-19 23:06:59 +00:00
|
|
|
*/
|
2008-02-10 20:34:39 +00:00
|
|
|
public static function makeFromSerial() {
|
|
|
|
return unserialize(file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser'));
|
2006-08-11 20:23:41 +00:00
|
|
|
}
|
|
|
|
|
2006-08-19 23:06:59 +00:00
|
|
|
/**
|
|
|
|
* Retrieves an instance of the application-wide configuration definition.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public static function &instance($prototype = null) {
|
2006-08-11 20:23:41 +00:00
|
|
|
if ($prototype !== null) {
|
2008-01-27 19:41:40 +00:00
|
|
|
HTMLPurifier_ConfigSchema::$singleton = $prototype;
|
|
|
|
} elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) {
|
2008-02-10 20:34:39 +00:00
|
|
|
HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial();
|
2006-08-11 20:23:41 +00:00
|
|
|
}
|
2008-01-27 19:41:40 +00:00
|
|
|
return HTMLPurifier_ConfigSchema::$singleton;
|
|
|
|
}
|
|
|
|
|
2008-02-20 00:37:08 +00:00
|
|
|
/**
|
|
|
|
* Throws an E_USER_NOTICE stating that a method is deprecated.
|
|
|
|
*/
|
|
|
|
private static function deprecated($method) {
|
|
|
|
trigger_error("Static HTMLPurifier_ConfigSchema::$method deprecated, use add*() method instead", E_USER_NOTICE);
|
|
|
|
}
|
|
|
|
|
2008-01-27 19:41:40 +00:00
|
|
|
/** @see HTMLPurifier_ConfigSchema->set() */
|
|
|
|
public static function define($namespace, $name, $default, $type, $description) {
|
2008-02-20 00:37:08 +00:00
|
|
|
HTMLPurifier_ConfigSchema::deprecated(__METHOD__);
|
2008-01-27 19:41:40 +00:00
|
|
|
$def =& HTMLPurifier_ConfigSchema::instance();
|
|
|
|
$def->add($namespace, $name, $default, $type, $description);
|
2006-08-11 20:23:41 +00:00
|
|
|
}
|
|
|
|
|
2006-08-19 23:06:59 +00:00
|
|
|
/**
|
|
|
|
* Defines a directive for configuration
|
|
|
|
* @warning Will fail of directive's namespace is defined
|
|
|
|
* @param $namespace Namespace the directive is in
|
|
|
|
* @param $name Key of directive
|
|
|
|
* @param $default Default value of directive
|
2006-08-27 18:49:16 +00:00
|
|
|
* @param $type Allowed type of the directive. See
|
|
|
|
* HTMLPurifier_DirectiveDef::$type for allowed values
|
2006-08-19 23:06:59 +00:00
|
|
|
* @param $description Description of directive for documentation
|
|
|
|
*/
|
2008-01-27 19:41:40 +00:00
|
|
|
public function add($namespace, $name, $default, $type, $description) {
|
2007-07-31 01:04:38 +00:00
|
|
|
// basic sanity checks
|
|
|
|
if (HTMLPURIFIER_SCHEMA_STRICT) {
|
2008-01-27 19:41:40 +00:00
|
|
|
if (!isset($this->info[$namespace])) {
|
2007-07-31 01:04:38 +00:00
|
|
|
trigger_error('Cannot define directive for undefined namespace',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!ctype_alnum($name)) {
|
|
|
|
trigger_error('Directive name must be alphanumeric',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (empty($description)) {
|
|
|
|
trigger_error('Description must be non-empty',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2007-01-20 22:59:20 +00:00
|
|
|
}
|
2007-07-31 01:04:38 +00:00
|
|
|
|
2008-01-27 19:41:40 +00:00
|
|
|
if (isset($this->info[$namespace][$name])) {
|
2007-07-31 01:04:38 +00:00
|
|
|
// already defined
|
2008-01-27 20:21:31 +00:00
|
|
|
trigger_error('Cannot redefine directive');
|
|
|
|
return;
|
2006-08-27 18:49:16 +00:00
|
|
|
} else {
|
2007-07-31 01:04:38 +00:00
|
|
|
// needs defining
|
|
|
|
|
|
|
|
// process modifiers (OPTIMIZE!)
|
2006-11-12 02:59:36 +00:00
|
|
|
$type_values = explode('/', $type, 2);
|
|
|
|
$type = $type_values[0];
|
|
|
|
$modifier = isset($type_values[1]) ? $type_values[1] : false;
|
|
|
|
$allow_null = ($modifier === 'null');
|
|
|
|
|
2007-07-31 01:04:38 +00:00
|
|
|
if (HTMLPURIFIER_SCHEMA_STRICT) {
|
2008-01-27 19:41:40 +00:00
|
|
|
if (!isset($this->types[$type])) {
|
2007-07-31 01:04:38 +00:00
|
|
|
trigger_error('Invalid type for configuration directive',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2008-03-04 15:06:00 +00:00
|
|
|
try {
|
|
|
|
$default = $this->parser->parse($default, $type, $allow_null);
|
|
|
|
} catch (HTMLPurifier_VarParserException $e) {
|
2007-07-31 01:04:38 +00:00
|
|
|
trigger_error('Default value does not match directive type',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2006-08-27 18:49:16 +00:00
|
|
|
}
|
2007-07-31 01:04:38 +00:00
|
|
|
|
2008-01-27 19:41:40 +00:00
|
|
|
$this->info[$namespace][$name] =
|
2007-02-05 03:22:32 +00:00
|
|
|
new HTMLPurifier_ConfigDef_Directive();
|
2008-01-27 19:41:40 +00:00
|
|
|
$this->info[$namespace][$name]->type = $type;
|
|
|
|
$this->info[$namespace][$name]->allow_null = $allow_null;
|
|
|
|
$this->defaults[$namespace][$name] = $default;
|
2006-08-11 20:23:41 +00:00
|
|
|
}
|
2007-07-31 01:04:38 +00:00
|
|
|
if (!HTMLPURIFIER_SCHEMA_STRICT) return;
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->info[$namespace][$name]->description = $description;
|
2008-01-27 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @see HTMLPurifier_ConfigSchema->addNamespace() */
|
|
|
|
public static function defineNamespace($namespace, $description) {
|
2008-02-20 00:37:08 +00:00
|
|
|
HTMLPurifier_ConfigSchema::deprecated(__METHOD__);
|
2008-01-27 19:41:40 +00:00
|
|
|
$def =& HTMLPurifier_ConfigSchema::instance();
|
|
|
|
$def->addNamespace($namespace, $description);
|
2006-08-11 20:23:41 +00:00
|
|
|
}
|
|
|
|
|
2006-08-19 23:06:59 +00:00
|
|
|
/**
|
|
|
|
* Defines a namespace for directives to be put into.
|
|
|
|
* @param $namespace Namespace's name
|
|
|
|
* @param $description Description of the namespace
|
|
|
|
*/
|
2008-01-27 19:41:40 +00:00
|
|
|
public function addNamespace($namespace, $description) {
|
|
|
|
$this->info[$namespace] = array();
|
|
|
|
$this->info_namespace[$namespace] = new HTMLPurifier_ConfigDef_Namespace();
|
|
|
|
$this->info_namespace[$namespace]->description = $description;
|
|
|
|
$this->defaults[$namespace] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @see HTMLPurifier_ConfigSchema->addValueAliases() */
|
|
|
|
public static function defineValueAliases($namespace, $name, $aliases) {
|
2008-02-20 00:37:08 +00:00
|
|
|
HTMLPurifier_ConfigSchema::deprecated(__METHOD__);
|
2008-01-27 19:41:40 +00:00
|
|
|
$def =& HTMLPurifier_ConfigSchema::instance();
|
|
|
|
$def->addValueAliases($namespace, $name, $aliases);
|
2006-08-11 20:23:41 +00:00
|
|
|
}
|
|
|
|
|
2006-08-27 18:49:16 +00:00
|
|
|
/**
|
|
|
|
* Defines a directive value alias.
|
|
|
|
*
|
|
|
|
* Directive value aliases are convenient for developers because it lets
|
|
|
|
* them set a directive to several values and get the same result.
|
|
|
|
* @param $namespace Directive's namespace
|
|
|
|
* @param $name Name of Directive
|
|
|
|
* @param $alias Name of aliased value
|
|
|
|
* @param $real Value aliased value will be converted into
|
|
|
|
*/
|
2008-01-27 19:41:40 +00:00
|
|
|
public function addValueAliases($namespace, $name, $aliases) {
|
|
|
|
if (HTMLPURIFIER_SCHEMA_STRICT && !isset($this->info[$namespace][$name])) {
|
2006-08-27 18:49:16 +00:00
|
|
|
trigger_error('Cannot set value alias for non-existant directive',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach ($aliases as $alias => $real) {
|
2007-07-31 01:04:38 +00:00
|
|
|
if (HTMLPURIFIER_SCHEMA_STRICT) {
|
2008-01-27 19:41:40 +00:00
|
|
|
if (!$this->info[$namespace][$name] !== true &&
|
|
|
|
!isset($this->info[$namespace][$name]->allowed[$real])
|
2007-07-31 01:04:38 +00:00
|
|
|
) {
|
|
|
|
trigger_error('Cannot define alias to value that is not allowed',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2008-01-27 19:41:40 +00:00
|
|
|
if (isset($this->info[$namespace][$name]->allowed[$alias])) {
|
2007-07-31 01:04:38 +00:00
|
|
|
trigger_error('Cannot define alias over allowed value',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2006-08-27 18:49:16 +00:00
|
|
|
}
|
2008-01-27 19:41:40 +00:00
|
|
|
$this->info[$namespace][$name]->aliases[$alias] = $real;
|
2006-08-27 18:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-27 19:41:40 +00:00
|
|
|
/** @see HTMLPurifier_ConfigSchema->addAllowedValues() */
|
|
|
|
public static function defineAllowedValues($namespace, $name, $allowed_values) {
|
2008-02-20 00:37:08 +00:00
|
|
|
HTMLPurifier_ConfigSchema::deprecated(__METHOD__);
|
2008-01-27 19:41:40 +00:00
|
|
|
$def =& HTMLPurifier_ConfigSchema::instance();
|
|
|
|
$def->addAllowedValues($namespace, $name, $allowed_values);
|
|
|
|
}
|
|
|
|
|
2006-08-27 18:49:16 +00:00
|
|
|
/**
|
|
|
|
* Defines a set of allowed values for a directive.
|
|
|
|
* @param $namespace Namespace of directive
|
|
|
|
* @param $name Name of directive
|
|
|
|
* @param $allowed_values Arraylist of allowed values
|
|
|
|
*/
|
2008-01-27 19:41:40 +00:00
|
|
|
public function addAllowedValues($namespace, $name, $allowed_values) {
|
|
|
|
if (HTMLPURIFIER_SCHEMA_STRICT && !isset($this->info[$namespace][$name])) {
|
2006-08-27 18:49:16 +00:00
|
|
|
trigger_error('Cannot define allowed values for undefined directive',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2008-01-27 19:41:40 +00:00
|
|
|
$directive =& $this->info[$namespace][$name];
|
2007-01-20 22:59:20 +00:00
|
|
|
$type = $directive->type;
|
2007-07-31 01:04:38 +00:00
|
|
|
if (HTMLPURIFIER_SCHEMA_STRICT && $type != 'string' && $type != 'istring') {
|
2007-01-20 22:59:20 +00:00
|
|
|
trigger_error('Cannot define allowed values for directive whose type is not string',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($directive->allowed === true) {
|
|
|
|
$directive->allowed = array();
|
2006-08-27 18:49:16 +00:00
|
|
|
}
|
|
|
|
foreach ($allowed_values as $value) {
|
2007-01-20 22:59:20 +00:00
|
|
|
$directive->allowed[$value] = true;
|
|
|
|
}
|
2007-07-31 01:04:38 +00:00
|
|
|
if (
|
|
|
|
HTMLPURIFIER_SCHEMA_STRICT &&
|
2008-01-27 19:41:40 +00:00
|
|
|
$this->defaults[$namespace][$name] !== null &&
|
|
|
|
!isset($directive->allowed[$this->defaults[$namespace][$name]])
|
2007-07-31 01:04:38 +00:00
|
|
|
) {
|
2007-01-20 22:59:20 +00:00
|
|
|
trigger_error('Default value must be in allowed range of variables',
|
|
|
|
E_USER_ERROR);
|
|
|
|
$directive->allowed = true; // undo undo!
|
|
|
|
return;
|
2006-08-27 18:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-27 19:41:40 +00:00
|
|
|
/** @see HTMLPurifier_ConfigSchema->addAlias() */
|
|
|
|
public static function defineAlias($namespace, $name, $new_namespace, $new_name) {
|
2008-02-20 00:37:08 +00:00
|
|
|
HTMLPurifier_ConfigSchema::deprecated(__METHOD__);
|
2008-01-27 19:41:40 +00:00
|
|
|
$def =& HTMLPurifier_ConfigSchema::instance();
|
|
|
|
$def->addAlias($namespace, $name, $new_namespace, $new_name);
|
|
|
|
}
|
|
|
|
|
2007-01-20 18:43:58 +00:00
|
|
|
/**
|
|
|
|
* Defines a directive alias for backwards compatibility
|
|
|
|
* @param $namespace
|
|
|
|
* @param $name Directive that will be aliased
|
|
|
|
* @param $new_namespace
|
|
|
|
* @param $new_name Directive that the alias will be to
|
|
|
|
*/
|
2008-01-27 19:41:40 +00:00
|
|
|
public function addAlias($namespace, $name, $new_namespace, $new_name) {
|
2007-07-31 01:04:38 +00:00
|
|
|
if (HTMLPURIFIER_SCHEMA_STRICT) {
|
2008-01-27 19:41:40 +00:00
|
|
|
if (!isset($this->info[$namespace])) {
|
2007-07-31 01:04:38 +00:00
|
|
|
trigger_error('Cannot define directive alias in undefined namespace',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!ctype_alnum($name)) {
|
|
|
|
trigger_error('Directive name must be alphanumeric',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2008-01-27 19:41:40 +00:00
|
|
|
if (isset($this->info[$namespace][$name])) {
|
2007-07-31 01:04:38 +00:00
|
|
|
trigger_error('Cannot define alias over directive',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2008-01-27 19:41:40 +00:00
|
|
|
if (!isset($this->info[$new_namespace][$new_name])) {
|
2007-07-31 01:04:38 +00:00
|
|
|
trigger_error('Cannot define alias to undefined directive',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2008-01-27 19:41:40 +00:00
|
|
|
if ($this->info[$new_namespace][$new_name]->class == 'alias') {
|
2007-07-31 01:04:38 +00:00
|
|
|
trigger_error('Cannot define alias to alias',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2007-01-20 18:43:58 +00:00
|
|
|
}
|
2008-01-27 19:41:40 +00:00
|
|
|
$this->info[$namespace][$name] =
|
2007-02-05 03:22:32 +00:00
|
|
|
new HTMLPurifier_ConfigDef_DirectiveAlias(
|
2007-01-20 22:59:20 +00:00
|
|
|
$new_namespace, $new_name);
|
2008-01-27 19:41:40 +00:00
|
|
|
$this->info[$new_namespace][$new_name]->directiveAliases[] = "$namespace.$name";
|
2007-01-20 18:43:58 +00:00
|
|
|
}
|
|
|
|
|
2006-11-12 02:59:36 +00:00
|
|
|
/**
|
|
|
|
* Takes an absolute path and munges it into a more manageable relative path
|
2007-11-25 02:24:39 +00:00
|
|
|
* @todo Consider making protected
|
|
|
|
* @param $filename Filename to check
|
|
|
|
* @return string munged filename
|
2006-11-12 02:59:36 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function mungeFilename($filename) {
|
2007-07-31 01:04:38 +00:00
|
|
|
if (!HTMLPURIFIER_SCHEMA_STRICT) return $filename;
|
2006-08-27 18:49:16 +00:00
|
|
|
$offset = strrpos($filename, 'HTMLPurifier');
|
|
|
|
$filename = substr($filename, $offset);
|
|
|
|
$filename = str_replace('\\', '/', $filename);
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
2006-11-12 02:59:36 +00:00
|
|
|
/**
|
|
|
|
* Checks if var is an HTMLPurifier_Error object
|
2007-11-25 02:24:39 +00:00
|
|
|
* @todo Consider making protected
|
2006-11-12 02:59:36 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function isError($var) {
|
2006-11-12 02:59:36 +00:00
|
|
|
if (!is_object($var)) return false;
|
2007-11-25 02:24:39 +00:00
|
|
|
if (!($var instanceof HTMLPurifier_Error)) return false;
|
2006-11-12 02:59:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-03-04 15:06:00 +00:00
|
|
|
|
|
|
|
/** @deprecated, use HTMLPurifier_VarParser->parse() */
|
|
|
|
public function validate($a, $b, $c = false) {
|
|
|
|
trigger_error("HTMLPurifier_ConfigSchema->validate deprecated, use HTMLPurifier_VarParser->parse instead", E_USER_NOTICE);
|
|
|
|
return $this->parser->parse($a, $b, $c);
|
|
|
|
}
|
|
|
|
|
2006-08-27 18:49:16 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 13:58:32 +00:00
|
|
|
|