Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract HTML signature image processing code
Needed because drag/drop images is allowed in compose preview window
- Loading branch information
Showing
4 changed files
with
122 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/** | ||
* Copyright 2014 Horde LLC (http://www.horde.org/) | ||
* | ||
* See the enclosed file COPYING for license information (GPL). If you | ||
* did not receive this file, see http://www.horde.org/licenses/gpl. | ||
* | ||
* @category Horde | ||
* @copyright 2014 Horde LLC | ||
* @license http://www.horde.org/licenses/gpl GPL | ||
* @package IMP | ||
*/ | ||
|
||
/** | ||
* Parse HTML signature data. | ||
* | ||
* @author Michael Slusarz <slusarz@horde.org> | ||
* @category Horde | ||
* @copyright 2014 Horde LLC | ||
* @license http://www.horde.org/licenses/gpl GPL | ||
* @package IMP | ||
*/ | ||
class IMP_Compose_HtmlSignature | ||
{ | ||
/** Signature data attribute name. */ | ||
const HTMLSIG_ATTR = 'imp_htmlsig'; | ||
|
||
/** | ||
* DOM object containing HTML signature data. | ||
* | ||
* @var Horde_Domhtml | ||
*/ | ||
public $dom; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $sig HTML signature data. | ||
* | ||
* @throws IMP_Exception | ||
*/ | ||
public function __construct($sig) | ||
{ | ||
global $conf, $injector; | ||
|
||
/* Scrub HTML. */ | ||
$this->dom = $injector->getInstance('Horde_Core_Factory_TextFilter')->filter( | ||
$sig, | ||
'Xss', | ||
array( | ||
'charset' => 'UTF-8', | ||
'return_dom' => true, | ||
'strip_style_attributes' => false | ||
) | ||
); | ||
|
||
$img_limit = intval($conf['compose']['htmlsig_img_size']); | ||
|
||
$xpath = new DOMXPath($this->dom->dom); | ||
foreach ($xpath->query('//*[@src]') as $node) { | ||
$src = $node->getAttribute('src'); | ||
|
||
if (Horde_Url_Data::isData($src)) { | ||
if (strcasecmp($node->tagName, 'IMG') === 0) { | ||
$data_url = new Horde_Url_Data($src); | ||
if ($img_limit && | ||
($img_limit -= strlen($data_url->data)) < 0) { | ||
throw new IMP_Exception(_("The total size of your HTML signature image data has exceeded the maximum allowed.")); | ||
} | ||
|
||
$node->setAttribute(self::HTMLSIG_ATTR, 1); | ||
} else { | ||
/* Don't allow any other non-image data URLs. */ | ||
$node->removeAttribute('src'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Determine if node contains HTML signature image data. | ||
* | ||
* @param DOMNode $node The node to check. | ||
* @param boolean $strip Strip attribute from the node? | ||
* | ||
* @return boolean True if node contains image data. | ||
*/ | ||
static public function isSigImage(DOMNode $node, $strip = false) | ||
{ | ||
if ((strcasecmp($node->tagName, 'IMG') === 0) && | ||
$node->hasAttribute(self::HTMLSIG_ATTR)) { | ||
if ($strip) { | ||
$node->removeAttribute(self::HTMLSIG_ATTR); | ||
} | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters