friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/ChildDef/Custom.php

103 lines
2.7 KiB
PHP
Raw Normal View History

2010-09-08 23:14:17 -04:00
<?php
/**
* Custom validation class, accepts DTD child definitions
*
* @warning Currently this class is an all or nothing proposition, that is,
* it will only give a bool return value.
*/
class HTMLPurifier_ChildDef_Custom extends HTMLPurifier_ChildDef
{
2016-02-09 05:06:17 -05:00
/**
* @type string
*/
2010-09-08 23:14:17 -04:00
public $type = 'custom';
2016-02-09 05:06:17 -05:00
/**
* @type bool
*/
2010-09-08 23:14:17 -04:00
public $allow_empty = false;
2016-02-09 05:06:17 -05:00
2010-09-08 23:14:17 -04:00
/**
2016-02-09 05:06:17 -05:00
* Allowed child pattern as defined by the DTD.
* @type string
2010-09-08 23:14:17 -04:00
*/
public $dtd_regex;
2016-02-09 05:06:17 -05:00
2010-09-08 23:14:17 -04:00
/**
2016-02-09 05:06:17 -05:00
* PCRE regex derived from $dtd_regex.
* @type string
2010-09-08 23:14:17 -04:00
*/
private $_pcre_regex;
2016-02-09 05:06:17 -05:00
2010-09-08 23:14:17 -04:00
/**
* @param $dtd_regex Allowed child pattern from the DTD
*/
2016-02-09 05:06:17 -05:00
public function __construct($dtd_regex)
{
2010-09-08 23:14:17 -04:00
$this->dtd_regex = $dtd_regex;
$this->_compileRegex();
}
2016-02-09 05:06:17 -05:00
2010-09-08 23:14:17 -04:00
/**
* Compiles the PCRE regex from a DTD regex ($dtd_regex to $_pcre_regex)
*/
2016-02-09 05:06:17 -05:00
protected function _compileRegex()
{
2010-09-08 23:14:17 -04:00
$raw = str_replace(' ', '', $this->dtd_regex);
if ($raw{0} != '(') {
$raw = "($raw)";
}
$el = '[#a-zA-Z0-9_.-]+';
$reg = $raw;
// COMPLICATED! AND MIGHT BE BUGGY! I HAVE NO CLUE WHAT I'M
// DOING! Seriously: if there's problems, please report them.
// collect all elements into the $elements array
preg_match_all("/$el/", $reg, $matches);
foreach ($matches[0] as $match) {
$this->elements[$match] = true;
}
// setup all elements as parentheticals with leading commas
$reg = preg_replace("/$el/", '(,\\0)', $reg);
// remove commas when they were not solicited
$reg = preg_replace("/([^,(|]\(+),/", '\\1', $reg);
// remove all non-paranthetical commas: they are handled by first regex
$reg = preg_replace("/,\(/", '(', $reg);
$this->_pcre_regex = $reg;
}
2016-02-09 05:06:17 -05:00
/**
* @param HTMLPurifier_Node[] $children
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool
*/
public function validateChildren($children, $config, $context)
{
2010-09-08 23:14:17 -04:00
$list_of_children = '';
$nesting = 0; // depth into the nest
2016-02-09 05:06:17 -05:00
foreach ($children as $node) {
if (!empty($node->is_whitespace)) {
continue;
2010-09-08 23:14:17 -04:00
}
2016-02-09 05:06:17 -05:00
$list_of_children .= $node->name . ',';
2010-09-08 23:14:17 -04:00
}
// add leading comma to deal with stray comma declarations
$list_of_children = ',' . rtrim($list_of_children, ',');
$okay =
preg_match(
2016-02-09 05:06:17 -05:00
'/^,?' . $this->_pcre_regex . '$/',
2010-09-08 23:14:17 -04:00
$list_of_children
);
2016-02-09 05:06:17 -05:00
return (bool)$okay;
2010-09-08 23:14:17 -04:00
}
}
// vim: et sw=4 sts=4