From 74be739c40eb3146b7a809f2277f07fc0ef65ab8 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Thu, 28 Jan 2010 09:46:26 -0500
Subject: [PATCH] - speed up templates parsing
---
program/lib/washtml.php | 53 ++++++++++++++++++++++++++++++++++-------------------
1 files changed, 34 insertions(+), 19 deletions(-)
diff --git a/program/lib/washtml.php b/program/lib/washtml.php
index 340dc93..fb2533b 100644
--- a/program/lib/washtml.php
+++ b/program/lib/washtml.php
@@ -69,18 +69,25 @@
* Dont be a fool:
* - Dont alter data on a GET: '<img src="http://yourhost/mail?action=delete&uid=3267" />'
* - ...
+ *
+ * Roundcube Changes:
+ * - added $block_elements
+ * - changed $ignore_elements behaviour
*/
class washtml
{
/* Allowed HTML elements (default) */
- static $html_elements = array('a', 'abbr', 'acronym', 'address', 'area', 'b', 'basefont', 'bdo', 'big', 'blockquote', 'br', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'dir', 'div', 'dl', 'dt', 'em', 'fieldset', 'font', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'ins', 'label', 'legend', 'li', 'map', 'menu', 'ol', 'p', 'pre', 'q', 's', 'samp', 'small', 'span', 'strike', 'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'tt', 'u', 'ul', 'var', 'img');
+ static $html_elements = array('a', 'abbr', 'acronym', 'address', 'area', 'b', 'basefont', 'bdo', 'big', 'blockquote', 'br', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'dir', 'div', 'dl', 'dt', 'em', 'fieldset', 'font', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'ins', 'label', 'legend', 'li', 'map', 'menu', 'nobr', 'ol', 'p', 'pre', 'q', 's', 'samp', 'small', 'span', 'strike', 'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'tt', 'u', 'ul', 'var', 'wbr', 'img');
- /* Ignore these HTML tags but process their content */
- static $ignore_elements = array('html', 'body');
+ /* Ignore these HTML tags and their content */
+ static $ignore_elements = array('script', 'applet', 'embed', 'object', 'style');
/* Allowed HTML attributes */
- static $html_attribs = array('name', 'class', 'title', 'alt', 'width', 'height', 'align', 'nowrap', 'col', 'row', 'id', 'rowspan', 'colspan', 'cellspacing', 'cellpadding', 'valign', 'bgcolor', 'color', 'border', 'bordercolorlight', 'bordercolordark', 'face', 'marginwidth', 'marginheight', 'axis', 'border', 'abbr', 'char', 'charoff', 'clear', 'compact', 'coords', 'vspace', 'hspace', 'cellborder', 'size', 'lang', 'dir', 'background');
+ static $html_attribs = array('name', 'class', 'title', 'alt', 'width', 'height', 'align', 'nowrap', 'col', 'row', 'id', 'rowspan', 'colspan', 'cellspacing', 'cellpadding', 'valign', 'bgcolor', 'color', 'border', 'bordercolorlight', 'bordercolordark', 'face', 'marginwidth', 'marginheight', 'axis', 'border', 'abbr', 'char', 'charoff', 'clear', 'compact', 'coords', 'vspace', 'hspace', 'cellborder', 'size', 'lang', 'dir');
+
+ /* Block elements which could be empty but cannot be returned in short form (<tag />) */
+ static $block_elements = array('div', 'p', 'pre', 'blockquote', 'a');
/* State for linked objects in HTML */
public $extlinks = false;
@@ -97,6 +104,9 @@
/* Ignore these HTML tags but process their content */
private $_ignore_elements = array();
+ /* Block elements which could be empty but cannot be returned in short form (<tag />) */
+ private $_block_elements = array();
+
/* Allowed HTML attributes */
private $_html_attribs = array();
@@ -106,7 +116,8 @@
$this->_html_elements = array_flip((array)$p['html_elements']) + array_flip(self::$html_elements) ;
$this->_html_attribs = array_flip((array)$p['html_attribs']) + array_flip(self::$html_attribs);
$this->_ignore_elements = array_flip((array)$p['ignore_elements']) + array_flip(self::$ignore_elements);
- unset($p['html_elements'], $p['html_attribs'], $p['ignore_elements']);
+ $this->_block_elements = array_flip((array)$p['block_elements']) + array_flip(self::$block_elements);
+ unset($p['html_elements'], $p['html_attribs'], $p['ignore_elements'], $p['block_elements']);
$this->config = $p + array('show_washed'=>true, 'allow_remote'=>false, 'cid_map'=>array());
}
@@ -132,13 +143,14 @@
'|#[0-9a-f]{3,6}|[a-z0-9\-]+'.
')\s*/i', $str, $match)) {
if($match[2]) {
- if(preg_match('/^(http|https|ftp):.*$/i', $match[2], $url)) {
+ if($src = $this->config['cid_map'][$match[2]])
+ $value .= ' url(\''.htmlspecialchars($src, ENT_QUOTES) . '\')';
+ else if(preg_match('/^(http|https|ftp):.*$/i', $match[2], $url)) {
if($this->config['allow_remote'])
$value .= ' url(\''.htmlspecialchars($url[0], ENT_QUOTES).'\')';
else
$this->extlinks = true;
- } else if(preg_match('/^cid:(.*)$/i', $match[2], $cid))
- $value .= ' url(\''.htmlspecialchars($this->config['cid_map']['cid:'.$cid[1]], ENT_QUOTES) . '\')';
+ }
} else if($match[0] != 'url' && $match[0] != 'rbg')//whitelist ?
$value .= ' ' . $match[0];
$str = substr($str, strlen($match[0]));
@@ -159,21 +171,23 @@
$key = strtolower($key);
$value = $node->getAttribute($key);
if(isset($this->_html_attribs[$key]) ||
- ($key == 'href' && preg_match('/^(http|https|ftp|mailto):.*/i', $value)))
+ ($key == 'href' && preg_match('/^(http:|https:|ftp:|mailto:|#).+/i', $value)))
$t .= ' ' . $key . '="' . htmlspecialchars($value, ENT_QUOTES) . '"';
else if($key == 'style' && ($style = $this->wash_style($value)))
$t .= ' style="' . $style . '"';
- else if($key == 'src' && strtolower($node->tagName) == 'img') { //check tagName anyway
- if(preg_match('/^(http|https|ftp):.*/i', $value)) {
+ else if($key == 'background' || ($key == 'src' && strtolower($node->tagName) == 'img')) { //check tagName anyway
+ if($src = $this->config['cid_map'][$value]) {
+ $t .= ' ' . $key . '="' . htmlspecialchars($src, ENT_QUOTES) . '"';
+ }
+ else if(preg_match('/^(http|https|ftp):.+/i', $value)) {
if($this->config['allow_remote'])
$t .= ' ' . $key . '="' . htmlspecialchars($value, ENT_QUOTES) . '"';
else {
$this->extlinks = true;
if ($this->config['blocked_src'])
- $t .= ' src="' . htmlspecialchars($this->config['blocked_src'], ENT_QUOTES) . '"';
+ $t .= ' ' . $key . '="' . htmlspecialchars($this->config['blocked_src'], ENT_QUOTES) . '"';
}
- } else if(preg_match('/^cid:(.*)$/i', $value, $cid))
- $t .= ' ' . $key . '="' . htmlspecialchars($this->config['cid_map']['cid:'.$cid[1]], ENT_QUOTES) . '"';
+ }
} else
$washed .= ($washed?' ':'') . $key;
}
@@ -199,12 +213,13 @@
} else if(isset($this->_html_elements[$tagName])) {
$content = $this->dumpHtml($node);
$dump .= '<' . $tagName . $this->wash_attribs($node) .
- ($content?">$content</$tagName>":' />');
+ ($content || isset($this->_block_elements[$tagName]) ? ">$content</$tagName>" : ' />');
} else if(isset($this->_ignore_elements[$tagName])) {
- $dump .= '<!-- ' . htmlspecialchars($tagName, ENT_QUOTES) . ' ignored -->';
- $dump .= $this->dumpHtml($node); //Just ignored
- } else
$dump .= '<!-- ' . htmlspecialchars($tagName, ENT_QUOTES) . ' not allowed -->';
+ } else {
+ $dump .= '<!-- ' . htmlspecialchars($tagName, ENT_QUOTES) . ' ignored -->';
+ $dump .= $this->dumpHtml($node); // ignore tags not its content
+ }
break;
case XML_CDATA_SECTION_NODE:
$dump .= $node->nodeValue;
@@ -237,4 +252,4 @@
}
-?>
\ No newline at end of file
+?>
--
Gitblit v1.9.1