2007-02-15 14:00:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/HTMLModule.php';
|
2007-02-17 17:43:44 +00:00
|
|
|
require_once 'HTMLPurifier/ElementDef.php';
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/ContentSets.php';
|
|
|
|
require_once 'HTMLPurifier/AttrTypes.php';
|
|
|
|
require_once 'HTMLPurifier/AttrCollections.php';
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/AttrDef.php';
|
|
|
|
require_once 'HTMLPurifier/AttrDef/Enum.php';
|
2007-02-15 14:00:18 +00:00
|
|
|
|
|
|
|
// W3C modules
|
2007-02-16 03:07:47 +00:00
|
|
|
require_once 'HTMLPurifier/HTMLModule/CommonAttributes.php';
|
2007-02-15 14:00:18 +00:00
|
|
|
require_once 'HTMLPurifier/HTMLModule/Text.php';
|
|
|
|
require_once 'HTMLPurifier/HTMLModule/Hypertext.php';
|
|
|
|
require_once 'HTMLPurifier/HTMLModule/List.php';
|
|
|
|
require_once 'HTMLPurifier/HTMLModule/Presentation.php';
|
|
|
|
require_once 'HTMLPurifier/HTMLModule/Edit.php';
|
|
|
|
require_once 'HTMLPurifier/HTMLModule/Bdo.php';
|
|
|
|
require_once 'HTMLPurifier/HTMLModule/Tables.php';
|
|
|
|
require_once 'HTMLPurifier/HTMLModule/Image.php';
|
|
|
|
require_once 'HTMLPurifier/HTMLModule/StyleAttribute.php';
|
|
|
|
require_once 'HTMLPurifier/HTMLModule/Legacy.php';
|
2007-04-30 21:19:15 +00:00
|
|
|
require_once 'HTMLPurifier/HTMLModule/Target.php';
|
2007-02-15 14:00:18 +00:00
|
|
|
|
|
|
|
// proprietary modules
|
|
|
|
require_once 'HTMLPurifier/HTMLModule/TransformToStrict.php';
|
|
|
|
require_once 'HTMLPurifier/HTMLModule/TransformToXHTML11.php';
|
|
|
|
|
2007-02-18 05:29:19 +00:00
|
|
|
HTMLPurifier_ConfigSchema::define(
|
|
|
|
'HTML', 'Doctype', null, 'string/null',
|
|
|
|
'Doctype to use, valid values are HTML 4.01 Transitional, HTML 4.01 '.
|
|
|
|
'Strict, XHTML 1.0 Transitional, XHTML 1.0 Strict, XHTML 1.1. '.
|
|
|
|
'Technically speaking this is not actually a doctype (as it does '.
|
|
|
|
'not identify a corresponding DTD), but we are using this name '.
|
2007-02-20 03:05:03 +00:00
|
|
|
'for sake of simplicity. This will override any older directives '.
|
2007-02-18 05:29:19 +00:00
|
|
|
'like %Core.XHTML or %HTML.Strict.'
|
|
|
|
);
|
|
|
|
|
2007-02-15 14:00:18 +00:00
|
|
|
class HTMLPurifier_HTMLModuleManager
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of HTMLPurifier_Module instances, indexed by module's class name.
|
|
|
|
* All known modules, regardless of use, are in this array.
|
|
|
|
*/
|
|
|
|
var $modules = array();
|
|
|
|
|
2007-02-17 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* String doctype we will validate against. See $validModules for use.
|
2007-02-18 05:29:19 +00:00
|
|
|
*
|
|
|
|
* @note
|
|
|
|
* There is a special doctype '*' that acts both as the "default"
|
|
|
|
* doctype if a customized system only defines one doctype and
|
|
|
|
* also a catch-all doctype that gets merged into all the other
|
|
|
|
* module collections. When possible, use a private collection to
|
|
|
|
* share modules between doctypes: this special doctype is to
|
|
|
|
* make life more convenient for users.
|
2007-02-17 22:17:14 +00:00
|
|
|
*/
|
|
|
|
var $doctype;
|
|
|
|
var $doctypeAliases = array(); /**< Lookup array of strings to real doctypes */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Associative array: $collections[$type][$doctype] = list of modules.
|
|
|
|
* This is used to logically separate types of functionality so that
|
|
|
|
* based on the doctype and other configuration settings they may
|
|
|
|
* be easily switched and on and off. Custom setups may not need
|
|
|
|
* to use this abstraction, opting to have only one big collection
|
|
|
|
* with one valid doctype.
|
|
|
|
*/
|
|
|
|
var $collections = array();
|
|
|
|
|
2007-02-16 03:16:17 +00:00
|
|
|
/**
|
2007-02-16 03:33:29 +00:00
|
|
|
* Modules that may be used in a valid doctype of this kind.
|
2007-02-17 17:10:28 +00:00
|
|
|
* Correctional and leniency modules should not be placed in this
|
|
|
|
* array unless the user said so: don't stuff every possible lenient
|
|
|
|
* module for this doctype in here.
|
2007-02-16 03:16:17 +00:00
|
|
|
*/
|
2007-02-16 03:33:29 +00:00
|
|
|
var $validModules = array();
|
2007-02-17 22:17:14 +00:00
|
|
|
var $validCollections = array(); /**< Collections to merge into $validModules */
|
2007-02-16 03:33:29 +00:00
|
|
|
|
|
|
|
/**
|
2007-02-17 22:17:14 +00:00
|
|
|
* Modules that we will allow in input, subset of $validModules. Single
|
2007-02-16 03:33:29 +00:00
|
|
|
* element definitions may result in us consulting validModules.
|
|
|
|
*/
|
|
|
|
var $activeModules = array();
|
2007-02-17 22:17:14 +00:00
|
|
|
var $activeCollections = array(); /**< Collections to merge into $activeModules */
|
2007-02-16 03:16:17 +00:00
|
|
|
|
2007-02-18 05:29:19 +00:00
|
|
|
var $counter = 0; /**< Designates next available integer order for modules. */
|
|
|
|
var $initialized = false; /**< Says whether initialize() was called */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specifies what doctype to siphon new modules from addModule() to,
|
|
|
|
* or false to disable the functionality. Must be used in conjunction
|
|
|
|
* with $autoCollection.
|
|
|
|
*/
|
|
|
|
var $autoDoctype = false;
|
2007-02-15 14:00:18 +00:00
|
|
|
/**
|
2007-02-18 05:29:19 +00:00
|
|
|
* Specifies what collection to siphon new modules from addModule() to,
|
|
|
|
* or false to disable the functionality. Must be used in conjunction
|
|
|
|
* with $autoCollection.
|
2007-02-15 14:00:18 +00:00
|
|
|
*/
|
2007-02-18 05:29:19 +00:00
|
|
|
var $autoCollection = false;
|
2007-02-15 14:00:18 +00:00
|
|
|
|
|
|
|
/** Associative array of element name to defining modules (always array) */
|
2007-02-17 22:17:14 +00:00
|
|
|
var $elementLookup = array();
|
2007-02-15 14:00:18 +00:00
|
|
|
|
|
|
|
/** List of prefixes we should use for resolving small names */
|
|
|
|
var $prefixes = array('HTMLPurifier_HTMLModule_');
|
|
|
|
|
2007-02-17 19:37:48 +00:00
|
|
|
var $contentSets; /**< Instance of HTMLPurifier_ContentSets */
|
2007-02-15 14:00:18 +00:00
|
|
|
var $attrTypes; /**< Instance of HTMLPurifier_AttrTypes */
|
|
|
|
var $attrCollections; /**< Instance of HTMLPurifier_AttrCollections */
|
|
|
|
|
2007-02-17 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* @param $blank If true, don't do any initializing
|
|
|
|
*/
|
|
|
|
function HTMLPurifier_HTMLModuleManager($blank = false) {
|
2007-02-15 14:00:18 +00:00
|
|
|
|
2007-02-17 22:17:14 +00:00
|
|
|
// the only editable internal object. The rest need to
|
|
|
|
// be manipulated through modules
|
|
|
|
$this->attrTypes = new HTMLPurifier_AttrTypes();
|
|
|
|
|
|
|
|
if (!$blank) $this->initialize();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function initialize() {
|
2007-02-18 05:29:19 +00:00
|
|
|
$this->initialized = true;
|
|
|
|
|
2007-02-17 22:17:14 +00:00
|
|
|
// load default modules to the recognized modules list (not active)
|
2007-02-15 14:00:18 +00:00
|
|
|
$modules = array(
|
2007-02-16 03:01:23 +00:00
|
|
|
// define
|
2007-02-16 03:07:47 +00:00
|
|
|
'CommonAttributes',
|
2007-02-16 03:01:23 +00:00
|
|
|
'Text', 'Hypertext', 'List', 'Presentation',
|
|
|
|
'Edit', 'Bdo', 'Tables', 'Image', 'StyleAttribute',
|
2007-04-30 21:19:15 +00:00
|
|
|
'Target',
|
2007-02-16 03:01:23 +00:00
|
|
|
// define-redefine
|
|
|
|
'Legacy',
|
|
|
|
// redefine
|
|
|
|
'TransformToStrict', 'TransformToXHTML11'
|
2007-02-15 14:00:18 +00:00
|
|
|
);
|
2007-02-16 03:01:23 +00:00
|
|
|
foreach ($modules as $module) {
|
|
|
|
$this->addModule($module);
|
2007-02-15 14:00:18 +00:00
|
|
|
}
|
|
|
|
|
2007-02-17 22:17:14 +00:00
|
|
|
// Safe modules for supported doctypes. These are included
|
|
|
|
// in the valid and active module lists by default
|
|
|
|
$this->collections['Safe'] = array(
|
|
|
|
'_Common' => array( // leading _ indicates private
|
|
|
|
'CommonAttributes', 'Text', 'Hypertext', 'List',
|
|
|
|
'Presentation', 'Edit', 'Bdo', 'Tables', 'Image',
|
|
|
|
'StyleAttribute'
|
|
|
|
),
|
|
|
|
// HTML definitions, defer to XHTML definitions
|
|
|
|
'HTML 4.01 Transitional' => array(array('XHTML 1.0 Transitional')),
|
|
|
|
'HTML 4.01 Strict' => array(array('XHTML 1.0 Strict')),
|
|
|
|
// XHTML definitions
|
2007-04-30 21:19:15 +00:00
|
|
|
'XHTML 1.0 Transitional' => array( array('XHTML 1.0 Strict'), 'Legacy', 'Target' ),
|
2007-02-17 22:17:14 +00:00
|
|
|
'XHTML 1.0 Strict' => array(array('_Common')),
|
|
|
|
'XHTML 1.1' => array(array('_Common')),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Modules that specify elements that are unsafe from untrusted
|
|
|
|
// third-parties. These should be registered in $validModules but
|
|
|
|
// almost never $activeModules unless you really know what you're
|
|
|
|
// doing.
|
|
|
|
$this->collections['Unsafe'] = array();
|
|
|
|
|
|
|
|
// Modules to import if lenient mode (attempt to convert everything
|
2007-02-18 05:29:19 +00:00
|
|
|
// to a valid representation) is on. These must not be in $validModules
|
2007-02-17 22:17:14 +00:00
|
|
|
// unless specified so.
|
|
|
|
$this->collections['Lenient'] = array(
|
2007-02-18 05:29:19 +00:00
|
|
|
'HTML 4.01 Strict' => array(array('XHTML 1.0 Strict')),
|
2007-02-17 22:17:14 +00:00
|
|
|
'XHTML 1.0 Strict' => array('TransformToStrict'),
|
|
|
|
'XHTML 1.1' => array(array('XHTML 1.0 Strict'), 'TransformToXHTML11')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Modules to import if correctional mode (correct everything that
|
2007-02-18 05:29:19 +00:00
|
|
|
// is feasible to strict mode) is on. These must not be in $validModules
|
2007-02-17 22:17:14 +00:00
|
|
|
// unless specified so.
|
|
|
|
$this->collections['Correctional'] = array(
|
2007-02-18 05:29:19 +00:00
|
|
|
'HTML 4.01 Transitional' => array(array('XHTML 1.0 Transitional')),
|
2007-02-17 22:17:14 +00:00
|
|
|
'XHTML 1.0 Transitional' => array('TransformToStrict'), // probably want a different one
|
|
|
|
);
|
2007-02-15 14:00:18 +00:00
|
|
|
|
2007-02-18 05:29:19 +00:00
|
|
|
// User-space modules, custom code or whatever
|
|
|
|
$this->collections['Extension'] = array();
|
|
|
|
|
2007-02-17 22:17:14 +00:00
|
|
|
// setup active versus valid modules. ORDER IS IMPORTANT!
|
|
|
|
// definition modules
|
|
|
|
$this->makeCollectionActive('Safe');
|
|
|
|
$this->makeCollectionValid('Unsafe');
|
|
|
|
// redefinition modules
|
|
|
|
$this->makeCollectionActive('Lenient');
|
|
|
|
$this->makeCollectionActive('Correctional');
|
2007-02-18 05:29:19 +00:00
|
|
|
|
|
|
|
$this->autoDoctype = '*';
|
|
|
|
$this->autoCollection = 'Extension';
|
|
|
|
|
2007-02-15 14:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-02-17 22:17:14 +00:00
|
|
|
* Adds a module to the recognized module list. This does not
|
|
|
|
* do anything else: the module must be added to a corresponding
|
|
|
|
* collection to be "activated".
|
2007-02-15 14:00:18 +00:00
|
|
|
* @param $module Mixed: string module name, with or without
|
|
|
|
* HTMLPurifier_HTMLModule prefix, or instance of
|
|
|
|
* subclass of HTMLPurifier_HTMLModule.
|
2007-04-21 02:19:18 +00:00
|
|
|
* @note This function will not call autoload, you must instantiate
|
|
|
|
* (and thus invoke) autoload outside the method.
|
2007-04-21 02:31:38 +00:00
|
|
|
* @note If a string is passed as a module name, different variants
|
|
|
|
* will be tested in this order:
|
|
|
|
* - Check for HTMLPurifier_HTMLModule_$name
|
|
|
|
* - Check all prefixes with $name in order they were added
|
|
|
|
* - Check for literal object name
|
|
|
|
* - Throw fatal error
|
|
|
|
* If your object name collides with an internal class, specify
|
|
|
|
* your module manually.
|
2007-02-15 14:00:18 +00:00
|
|
|
*/
|
2007-02-16 03:01:23 +00:00
|
|
|
function addModule($module) {
|
2007-02-15 14:00:18 +00:00
|
|
|
if (is_string($module)) {
|
|
|
|
$original_module = $module;
|
2007-04-21 02:31:38 +00:00
|
|
|
$ok = false;
|
|
|
|
foreach ($this->prefixes as $prefix) {
|
|
|
|
$module = $prefix . $original_module;
|
|
|
|
if (class_exists($module)) {
|
|
|
|
$ok = true;
|
|
|
|
break;
|
2007-02-15 14:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-21 02:31:38 +00:00
|
|
|
if (!$ok) {
|
|
|
|
$module = $original_module;
|
|
|
|
if (!class_exists($module, false)) {
|
|
|
|
trigger_error($original_module . ' module does not exist',
|
|
|
|
E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2007-02-15 14:00:18 +00:00
|
|
|
}
|
|
|
|
$module = new $module();
|
|
|
|
}
|
2007-02-17 22:17:14 +00:00
|
|
|
$module->order = $this->counter++; // assign then increment
|
2007-02-15 14:00:18 +00:00
|
|
|
$this->modules[$module->name] = $module;
|
2007-02-18 05:29:19 +00:00
|
|
|
if ($this->autoDoctype !== false && $this->autoCollection !== false) {
|
|
|
|
$this->collections[$this->autoCollection][$this->autoDoctype][] = $module->name;
|
|
|
|
}
|
2007-02-15 14:00:18 +00:00
|
|
|
}
|
|
|
|
|
2007-02-17 22:17:14 +00:00
|
|
|
/**
|
|
|
|
* Makes a collection active, while also making it valid if not
|
|
|
|
* already done so. See $activeModules for the semantics of "active".
|
|
|
|
* @param $collection_name Name of collection to activate
|
|
|
|
*/
|
|
|
|
function makeCollectionActive($collection_name) {
|
|
|
|
if (!in_array($collection_name, $this->validCollections)) {
|
|
|
|
$this->makeCollectionValid($collection_name);
|
|
|
|
}
|
|
|
|
$this->activeCollections[] = $collection_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a collection valid. See $validModules for the semantics of "valid"
|
|
|
|
*/
|
|
|
|
function makeCollectionValid($collection_name) {
|
|
|
|
$this->validCollections[] = $collection_name;
|
|
|
|
}
|
|
|
|
|
2007-02-17 19:37:48 +00:00
|
|
|
/**
|
|
|
|
* Adds a class prefix that addModule() will use to resolve a
|
|
|
|
* string name to a concrete class
|
|
|
|
*/
|
|
|
|
function addPrefix($prefix) {
|
|
|
|
$this->prefixes[] = (string) $prefix;
|
|
|
|
}
|
|
|
|
|
2007-02-15 14:00:18 +00:00
|
|
|
function setup($config) {
|
2007-02-18 05:29:19 +00:00
|
|
|
|
|
|
|
// load up the autocollection
|
|
|
|
if ($this->autoCollection !== false) {
|
|
|
|
$this->makeCollectionActive($this->autoCollection);
|
|
|
|
}
|
|
|
|
|
2007-02-16 03:33:29 +00:00
|
|
|
// retrieve the doctype
|
|
|
|
$this->doctype = $this->getDoctype($config);
|
2007-02-17 22:17:14 +00:00
|
|
|
if (isset($this->doctypeAliases[$this->doctype])) {
|
|
|
|
$this->doctype = $this->doctypeAliases[$this->doctype];
|
|
|
|
}
|
2007-02-16 03:33:29 +00:00
|
|
|
|
2007-02-17 17:10:28 +00:00
|
|
|
// process module collections to module name => module instance form
|
2007-02-17 22:17:14 +00:00
|
|
|
foreach ($this->collections as $col_i => $x) {
|
|
|
|
$this->processCollections($this->collections[$col_i]);
|
2007-02-15 14:00:18 +00:00
|
|
|
}
|
|
|
|
|
2007-02-17 22:17:14 +00:00
|
|
|
$this->validModules = $this->assembleModules($this->validCollections);
|
|
|
|
$this->activeModules = $this->assembleModules($this->activeCollections);
|
2007-02-16 03:33:29 +00:00
|
|
|
|
|
|
|
// setup lookup table based on all valid modules
|
|
|
|
foreach ($this->validModules as $module) {
|
2007-02-18 05:29:19 +00:00
|
|
|
foreach ($module->info as $name => $def) {
|
2007-02-17 22:17:14 +00:00
|
|
|
if (!isset($this->elementLookup[$name])) {
|
|
|
|
$this->elementLookup[$name] = array();
|
2007-02-15 14:00:18 +00:00
|
|
|
}
|
2007-02-17 22:17:14 +00:00
|
|
|
$this->elementLookup[$name][] = $module->name;
|
2007-02-15 14:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-16 03:33:29 +00:00
|
|
|
// note the different choice
|
2007-02-15 14:00:18 +00:00
|
|
|
$this->contentSets = new HTMLPurifier_ContentSets(
|
2007-02-17 17:10:28 +00:00
|
|
|
// content models that contain non-allowed elements are
|
|
|
|
// harmless because RemoveForeignElements will ensure
|
|
|
|
// they never get in anyway, and there is usually no
|
|
|
|
// reason why you should want to restrict a content
|
|
|
|
// model beyond what is mandated by the doctype.
|
|
|
|
// Note, however, that this means redefinitions of
|
|
|
|
// content models can't be tossed in validModels willy-nilly:
|
|
|
|
// that stuff still is regulated by configuration.
|
2007-02-16 03:33:29 +00:00
|
|
|
$this->validModules
|
|
|
|
);
|
|
|
|
$this->attrCollections = new HTMLPurifier_AttrCollections(
|
|
|
|
$this->attrTypes,
|
2007-02-17 17:10:28 +00:00
|
|
|
// only explicitly allowed modules are allowed to affect
|
|
|
|
// the global attribute collections. This mean's there's
|
|
|
|
// a distinction between loading the Bdo module, and the
|
|
|
|
// bdo element: Bdo will enable the dir attribute on all
|
|
|
|
// elements, while bdo will only define the bdo element,
|
|
|
|
// which will not have an editable directionality. This might
|
|
|
|
// catch people who are loading only elements by surprise, so
|
|
|
|
// we should consider loading an entire module if all the
|
|
|
|
// elements it defines are requested by the user, especially
|
|
|
|
// if it affects the global attribute collections.
|
2007-02-16 03:33:29 +00:00
|
|
|
$this->activeModules
|
2007-02-15 14:00:18 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-02-17 17:10:28 +00:00
|
|
|
/**
|
|
|
|
* Takes a list of collections and merges together all the defined
|
|
|
|
* modules for the current doctype from those collections.
|
|
|
|
* @param $collections List of collection suffixes we should grab
|
|
|
|
* modules from (like 'Safe' or 'Lenient')
|
|
|
|
*/
|
|
|
|
function assembleModules($collections) {
|
|
|
|
$modules = array();
|
2007-02-17 22:17:14 +00:00
|
|
|
$numOfCollectionsUsed = 0;
|
|
|
|
foreach ($collections as $name) {
|
2007-02-18 05:29:19 +00:00
|
|
|
$disable_global = false;
|
|
|
|
if (!isset($this->collections[$name])) {
|
|
|
|
trigger_error("$name collection is undefined", E_USER_ERROR);
|
|
|
|
continue;
|
|
|
|
}
|
2007-02-17 22:17:14 +00:00
|
|
|
$cols = $this->collections[$name];
|
|
|
|
if (isset($cols[$this->doctype])) {
|
2007-02-18 05:29:19 +00:00
|
|
|
if (isset($cols[$this->doctype]['*'])) {
|
|
|
|
unset($cols[$this->doctype]['*']);
|
|
|
|
$disable_global = true;
|
|
|
|
}
|
2007-02-17 17:10:28 +00:00
|
|
|
$modules += $cols[$this->doctype];
|
2007-02-17 22:17:14 +00:00
|
|
|
$numOfCollectionsUsed++;
|
2007-02-17 17:10:28 +00:00
|
|
|
}
|
2007-02-18 05:29:19 +00:00
|
|
|
// accept catch-all doctype
|
|
|
|
if (
|
|
|
|
$this->doctype !== '*' &&
|
|
|
|
isset($cols['*']) &&
|
|
|
|
!$disable_global
|
|
|
|
) {
|
|
|
|
$modules += $cols['*'];
|
|
|
|
}
|
2007-02-17 17:10:28 +00:00
|
|
|
}
|
2007-02-18 05:29:19 +00:00
|
|
|
|
2007-02-17 22:17:14 +00:00
|
|
|
if ($numOfCollectionsUsed < 1) {
|
|
|
|
// possible XSS injection if user-specified doctypes
|
|
|
|
// are allowed
|
|
|
|
trigger_error("Doctype {$this->doctype} does not exist, ".
|
|
|
|
"check for typos (if you desire a doctype that allows ".
|
|
|
|
"no elements, use an empty array collection)", E_USER_ERROR);
|
|
|
|
}
|
2007-02-17 17:10:28 +00:00
|
|
|
return $modules;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a collection and performs inclusions and substitutions for it.
|
|
|
|
* @param $cols Reference to collections class member variable
|
|
|
|
*/
|
2007-02-15 14:00:18 +00:00
|
|
|
function processCollections(&$cols) {
|
|
|
|
|
|
|
|
// $cols is the set of collections
|
|
|
|
// $col_i is the name (index) of a collection
|
|
|
|
// $col is a collection/list of modules
|
|
|
|
|
|
|
|
// perform inclusions
|
|
|
|
foreach ($cols as $col_i => $col) {
|
2007-02-18 05:29:19 +00:00
|
|
|
$seen = array();
|
|
|
|
if (!empty($col[0]) && is_array($col[0])) {
|
|
|
|
$seen[$col_i] = true; // recursion reporting
|
|
|
|
$includes = $col[0];
|
|
|
|
unset($cols[$col_i][0]); // remove inclusions value, recursion guard
|
|
|
|
} else {
|
|
|
|
$includes = array();
|
|
|
|
}
|
|
|
|
if (empty($includes)) continue;
|
2007-02-15 14:00:18 +00:00
|
|
|
for ($i = 0; isset($includes[$i]); $i++) {
|
|
|
|
$inc = $includes[$i];
|
2007-02-17 19:37:48 +00:00
|
|
|
if (isset($seen[$inc])) {
|
|
|
|
trigger_error(
|
|
|
|
"Circular inclusion detected in $col_i collection",
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
$seen[$inc] = true;
|
|
|
|
}
|
|
|
|
if (!isset($cols[$inc])) {
|
|
|
|
trigger_error(
|
|
|
|
"Collection $col_i tried to include undefined ".
|
|
|
|
"collection $inc", E_USER_ERROR);
|
|
|
|
continue;
|
|
|
|
}
|
2007-02-15 14:00:18 +00:00
|
|
|
foreach ($cols[$inc] as $module) {
|
|
|
|
if (is_array($module)) { // another inclusion!
|
|
|
|
foreach ($module as $inc2) $includes[] = $inc2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$cols[$col_i][] = $module; // merge in the other modules
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-16 03:16:17 +00:00
|
|
|
// replace with real modules, invert module from list to
|
|
|
|
// assoc array of module name to module instance
|
2007-02-15 14:00:18 +00:00
|
|
|
foreach ($cols as $col_i => $col) {
|
2007-02-18 05:29:19 +00:00
|
|
|
$ignore_global = false;
|
2007-02-17 17:10:28 +00:00
|
|
|
$order = array();
|
2007-02-15 14:00:18 +00:00
|
|
|
foreach ($col as $module_i => $module) {
|
2007-02-16 03:16:17 +00:00
|
|
|
unset($cols[$col_i][$module_i]);
|
2007-02-17 19:37:48 +00:00
|
|
|
if (is_array($module)) {
|
|
|
|
trigger_error("Illegal inclusion array at index".
|
|
|
|
" $module_i found collection $col_i, inclusion".
|
|
|
|
" arrays must be at start of collection (index 0)",
|
|
|
|
E_USER_ERROR);
|
|
|
|
continue;
|
|
|
|
}
|
2007-02-18 05:29:19 +00:00
|
|
|
if ($module_i === '*' && $module === false) {
|
|
|
|
$ignore_global = true;
|
|
|
|
continue;
|
|
|
|
}
|
2007-02-17 19:37:48 +00:00
|
|
|
if (!isset($this->modules[$module])) {
|
|
|
|
trigger_error(
|
|
|
|
"Collection $col_i references undefined ".
|
|
|
|
"module $module",
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
2007-02-16 03:16:17 +00:00
|
|
|
$module = $this->modules[$module];
|
|
|
|
$cols[$col_i][$module->name] = $module;
|
2007-02-17 17:10:28 +00:00
|
|
|
$order[$module->name] = $module->order;
|
2007-02-15 14:00:18 +00:00
|
|
|
}
|
2007-02-17 17:10:28 +00:00
|
|
|
array_multisort(
|
|
|
|
$order, SORT_ASC, SORT_NUMERIC, $cols[$col_i]
|
|
|
|
);
|
2007-02-18 05:29:19 +00:00
|
|
|
if ($ignore_global) $cols[$col_i]['*'] = false;
|
2007-02-15 14:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete pseudo-collections
|
|
|
|
foreach ($cols as $col_i => $col) {
|
|
|
|
if ($col_i[0] == '_') unset($cols[$col_i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-02-17 17:10:28 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the doctype from the configuration object
|
|
|
|
*/
|
2007-02-15 14:00:18 +00:00
|
|
|
function getDoctype($config) {
|
2007-02-18 05:29:19 +00:00
|
|
|
$doctype = $config->get('HTML', 'Doctype');
|
|
|
|
if ($doctype !== null) {
|
|
|
|
return $doctype;
|
|
|
|
}
|
|
|
|
if (!$this->initialized) {
|
|
|
|
// don't do HTML-oriented backwards compatibility stuff
|
|
|
|
// use either the auto-doctype, or the catch-all doctype
|
|
|
|
return $this->autoDoctype ? $this->autoDoctype : '*';
|
|
|
|
}
|
|
|
|
// this is backwards-compatibility stuff
|
2007-02-17 17:10:28 +00:00
|
|
|
if ($config->get('Core', 'XHTML')) {
|
|
|
|
$doctype = 'XHTML 1.0';
|
|
|
|
} else {
|
|
|
|
$doctype = 'HTML 4.01';
|
|
|
|
}
|
2007-02-15 14:00:18 +00:00
|
|
|
if ($config->get('HTML', 'Strict')) {
|
2007-02-17 17:10:28 +00:00
|
|
|
$doctype .= ' Strict';
|
2007-02-15 14:00:18 +00:00
|
|
|
} else {
|
2007-02-17 17:10:28 +00:00
|
|
|
$doctype .= ' Transitional';
|
2007-02-15 14:00:18 +00:00
|
|
|
}
|
|
|
|
return $doctype;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-02-17 17:10:28 +00:00
|
|
|
* Retrieves merged element definitions for all active elements.
|
|
|
|
* @note We may want to generate an elements array during setup
|
|
|
|
* and pass that on, because a specific combination of
|
|
|
|
* elements may trigger the loading of a module.
|
|
|
|
* @param $config Instance of HTMLPurifier_Config, for determining
|
|
|
|
* stray elements.
|
2007-02-15 14:00:18 +00:00
|
|
|
*/
|
|
|
|
function getElements($config) {
|
|
|
|
|
|
|
|
$elements = array();
|
2007-02-16 03:33:29 +00:00
|
|
|
foreach ($this->activeModules as $module) {
|
2007-02-15 14:00:18 +00:00
|
|
|
foreach ($module->elements as $name) {
|
|
|
|
$elements[$name] = $this->getElement($name, $config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-17 17:10:28 +00:00
|
|
|
// standalone elements now loaded
|
|
|
|
|
2007-02-15 14:00:18 +00:00
|
|
|
return $elements;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-02-17 17:10:28 +00:00
|
|
|
/**
|
|
|
|
* Retrieves a single merged element definition
|
|
|
|
* @param $name Name of element
|
|
|
|
* @param $config Instance of HTMLPurifier_Config, may not be necessary.
|
|
|
|
*/
|
2007-02-15 14:00:18 +00:00
|
|
|
function getElement($name, $config) {
|
|
|
|
|
|
|
|
$def = false;
|
|
|
|
|
2007-02-16 03:33:29 +00:00
|
|
|
$modules = $this->validModules;
|
2007-02-15 14:00:18 +00:00
|
|
|
|
2007-02-17 22:17:14 +00:00
|
|
|
if (!isset($this->elementLookup[$name])) {
|
2007-02-15 14:00:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-17 22:17:14 +00:00
|
|
|
foreach($this->elementLookup[$name] as $module_name) {
|
2007-02-15 14:00:18 +00:00
|
|
|
|
|
|
|
$module = $modules[$module_name];
|
|
|
|
$new_def = $module->info[$name];
|
|
|
|
|
|
|
|
if (!$def && $new_def->standalone) {
|
|
|
|
$def = $new_def;
|
|
|
|
} elseif ($def) {
|
|
|
|
$def->mergeIn($new_def);
|
|
|
|
} else {
|
2007-02-17 22:17:14 +00:00
|
|
|
// could "save it for another day":
|
2007-02-17 17:10:28 +00:00
|
|
|
// non-standalone definitions that don't have a standalone
|
|
|
|
// to merge into could be deferred to the end
|
2007-02-15 14:00:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// attribute value expansions
|
|
|
|
$this->attrCollections->performInclusions($def->attr);
|
|
|
|
$this->attrCollections->expandIdentifiers($def->attr, $this->attrTypes);
|
|
|
|
|
|
|
|
// descendants_are_inline, for ChildDef_Chameleon
|
|
|
|
if (is_string($def->content_model) &&
|
|
|
|
strpos($def->content_model, 'Inline') !== false) {
|
|
|
|
if ($name != 'del' && $name != 'ins') {
|
|
|
|
// this is for you, ins/del
|
|
|
|
$def->descendants_are_inline = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->contentSets->generateChildDef($def, $module);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $def;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-04-30 21:19:15 +00:00
|
|
|
?>
|