friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/HTMLModule/Tidy.php

231 lines
7.2 KiB
PHP
Raw Normal View History

2010-09-08 23:14:17 -04:00
<?php
/**
* Abstract class for a set of proprietary modules that clean up (tidy)
* poorly written HTML.
* @todo Figure out how to protect some of these methods/properties
*/
class HTMLPurifier_HTMLModule_Tidy extends HTMLPurifier_HTMLModule
{
/**
2016-02-09 05:06:17 -05:00
* List of supported levels.
* Index zero is a special case "no fixes" level.
* @type array
2010-09-08 23:14:17 -04:00
*/
public $levels = array(0 => 'none', 'light', 'medium', 'heavy');
/**
2016-02-09 05:06:17 -05:00
* Default level to place all fixes in.
* Disabled by default.
* @type string
2010-09-08 23:14:17 -04:00
*/
public $defaultLevel = null;
/**
2016-02-09 05:06:17 -05:00
* Lists of fixes used by getFixesForLevel().
* Format is:
2010-09-08 23:14:17 -04:00
* HTMLModule_Tidy->fixesForLevel[$level] = array('fix-1', 'fix-2');
2016-02-09 05:06:17 -05:00
* @type array
2010-09-08 23:14:17 -04:00
*/
public $fixesForLevel = array(
2016-02-09 05:06:17 -05:00
'light' => array(),
2010-09-08 23:14:17 -04:00
'medium' => array(),
2016-02-09 05:06:17 -05:00
'heavy' => array()
2010-09-08 23:14:17 -04:00
);
/**
* Lazy load constructs the module by determining the necessary
* fixes to create and then delegating to the populate() function.
2016-02-09 05:06:17 -05:00
* @param HTMLPurifier_Config $config
2010-09-08 23:14:17 -04:00
* @todo Wildcard matching and error reporting when an added or
* subtracted fix has no effect.
*/
2016-02-09 05:06:17 -05:00
public function setup($config)
{
2010-09-08 23:14:17 -04:00
// create fixes, initialize fixesForLevel
$fixes = $this->makeFixes();
$this->makeFixesForLevel($fixes);
// figure out which fixes to use
$level = $config->get('HTML.TidyLevel');
$fixes_lookup = $this->getFixesForLevel($level);
// get custom fix declarations: these need namespace processing
2016-02-09 05:06:17 -05:00
$add_fixes = $config->get('HTML.TidyAdd');
2010-09-08 23:14:17 -04:00
$remove_fixes = $config->get('HTML.TidyRemove');
foreach ($fixes as $name => $fix) {
// needs to be refactored a little to implement globbing
2016-02-09 05:06:17 -05:00
if (isset($remove_fixes[$name]) ||
(!isset($add_fixes[$name]) && !isset($fixes_lookup[$name]))) {
2010-09-08 23:14:17 -04:00
unset($fixes[$name]);
}
}
// populate this module with necessary fixes
$this->populate($fixes);
}
/**
* Retrieves all fixes per a level, returning fixes for that specific
* level as well as all levels below it.
2016-02-09 05:06:17 -05:00
* @param string $level level identifier, see $levels for valid values
* @return array Lookup up table of fixes
2010-09-08 23:14:17 -04:00
*/
2016-02-09 05:06:17 -05:00
public function getFixesForLevel($level)
{
2010-09-08 23:14:17 -04:00
if ($level == $this->levels[0]) {
return array();
}
$activated_levels = array();
for ($i = 1, $c = count($this->levels); $i < $c; $i++) {
$activated_levels[] = $this->levels[$i];
2016-02-09 05:06:17 -05:00
if ($this->levels[$i] == $level) {
break;
}
2010-09-08 23:14:17 -04:00
}
if ($i == $c) {
trigger_error(
'Tidy level ' . htmlspecialchars($level) . ' not recognized',
E_USER_WARNING
);
return array();
}
$ret = array();
foreach ($activated_levels as $level) {
foreach ($this->fixesForLevel[$level] as $fix) {
$ret[$fix] = true;
}
}
return $ret;
}
/**
* Dynamically populates the $fixesForLevel member variable using
* the fixes array. It may be custom overloaded, used in conjunction
* with $defaultLevel, or not used at all.
2016-02-09 05:06:17 -05:00
* @param array $fixes
2010-09-08 23:14:17 -04:00
*/
2016-02-09 05:06:17 -05:00
public function makeFixesForLevel($fixes)
{
if (!isset($this->defaultLevel)) {
return;
}
2010-09-08 23:14:17 -04:00
if (!isset($this->fixesForLevel[$this->defaultLevel])) {
trigger_error(
'Default level ' . $this->defaultLevel . ' does not exist',
E_USER_ERROR
);
return;
}
$this->fixesForLevel[$this->defaultLevel] = array_keys($fixes);
}
/**
* Populates the module with transforms and other special-case code
* based on a list of fixes passed to it
2016-02-09 05:06:17 -05:00
* @param array $fixes Lookup table of fixes to activate
2010-09-08 23:14:17 -04:00
*/
2016-02-09 05:06:17 -05:00
public function populate($fixes)
{
2010-09-08 23:14:17 -04:00
foreach ($fixes as $name => $fix) {
// determine what the fix is for
list($type, $params) = $this->getFixType($name);
switch ($type) {
case 'attr_transform_pre':
case 'attr_transform_post':
$attr = $params['attr'];
if (isset($params['element'])) {
$element = $params['element'];
if (empty($this->info[$element])) {
$e = $this->addBlankElement($element);
} else {
$e = $this->info[$element];
}
} else {
$type = "info_$type";
$e = $this;
}
// PHP does some weird parsing when I do
// $e->$type[$attr], so I have to assign a ref.
$f =& $e->$type;
$f[$attr] = $fix;
break;
case 'tag_transform':
$this->info_tag_transform[$params['element']] = $fix;
break;
case 'child':
case 'content_model_type':
$element = $params['element'];
if (empty($this->info[$element])) {
$e = $this->addBlankElement($element);
} else {
$e = $this->info[$element];
}
$e->$type = $fix;
break;
default:
trigger_error("Fix type $type not supported", E_USER_ERROR);
break;
}
}
}
/**
* Parses a fix name and determines what kind of fix it is, as well
* as other information defined by the fix
* @param $name String name of fix
* @return array(string $fix_type, array $fix_parameters)
* @note $fix_parameters is type dependant, see populate() for usage
* of these parameters
*/
2016-02-09 05:06:17 -05:00
public function getFixType($name)
{
2010-09-08 23:14:17 -04:00
// parse it
$property = $attr = null;
2016-02-09 05:06:17 -05:00
if (strpos($name, '#') !== false) {
list($name, $property) = explode('#', $name);
}
if (strpos($name, '@') !== false) {
list($name, $attr) = explode('@', $name);
}
2010-09-08 23:14:17 -04:00
// figure out the parameters
$params = array();
2016-02-09 05:06:17 -05:00
if ($name !== '') {
$params['element'] = $name;
}
if (!is_null($attr)) {
$params['attr'] = $attr;
}
2010-09-08 23:14:17 -04:00
// special case: attribute transform
if (!is_null($attr)) {
2016-02-09 05:06:17 -05:00
if (is_null($property)) {
$property = 'pre';
}
2010-09-08 23:14:17 -04:00
$type = 'attr_transform_' . $property;
return array($type, $params);
}
// special case: tag transform
if (is_null($property)) {
return array('tag_transform', $params);
}
return array($property, $params);
}
/**
* Defines all fixes the module will perform in a compact
* associative array of fix name to fix implementation.
2016-02-09 05:06:17 -05:00
* @return array
2010-09-08 23:14:17 -04:00
*/
2016-02-09 05:06:17 -05:00
public function makeFixes()
{
}
2010-09-08 23:14:17 -04:00
}
// vim: et sw=4 sts=4