From 299524b0175d2e8e37e3b9aa3a21be0654be8af7 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Fri, 9 Jul 2010 15:51:18 -0600 Subject: [PATCH] Remove horde/Core dependency in Emails filter. --- .../Core/lib/Horde/Core/Text/Filter/Emails.php | 88 ++++++++++ framework/Core/package.xml | 2 + .../Text_Filter/lib/Horde/Text/Filter/Emails.php | 181 ++++++++------------- 3 files changed, 154 insertions(+), 117 deletions(-) create mode 100644 framework/Core/lib/Horde/Core/Text/Filter/Emails.php diff --git a/framework/Core/lib/Horde/Core/Text/Filter/Emails.php b/framework/Core/lib/Horde/Core/Text/Filter/Emails.php new file mode 100644 index 000000000..2e0d978db --- /dev/null +++ b/framework/Core/lib/Horde/Core/Text/Filter/Emails.php @@ -0,0 +1,88 @@ + + * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @package Core + */ +class Horde_Core_Text_Filter_Emails extends Horde_Text_Filter_Emails +{ + /** + * Constructor. + * + * @param array $params Parameters (in addition to base + * Horde_Text_Filter_Emails parameters): + *
+     * always_mailto - (boolean) If true, a mailto: link is generated always.
+     *                 Only if no mail/compose registry API method exists
+     *                 otherwise.
+     *                 DEFAULT: false
+     * 
+ */ + public function __construct(array $params = array()) + { + $this->_params = array_merge(array( + 'always_mailto' => false + ), $this->_params, $params); + + parent::__construct($params); + } + + /** + * Regular expression callback. + * + * @param array $matches preg_replace_callback() matches. + * + * @return string Replacement string. + */ + protected function _regexCallback($matches) + { + if ($this->_params['always_mailto'] || + !$GLOBALS['registry']->hasMethod('mail/compose')) { + return parent::_regexCallback($matches); + } + + if (empty($email2)) { + $args = $matches[7]; + $email = $matches[3]; + $args_long = $matches[5]; + } else { + $args = $matches[13]; + $email = $matches[10]; + $args_long = $matches[11]; + } + + parse_str($args, $extra); + try { + $url = strval($GLOBALS['registry']->call('mail/compose', array(array('to' => $email), $extra))); + } catch (Horde_Exception $e) { + return parent::_regexCallback($matches); + } + + if (substr($url, 0, 11) == 'javascript:') { + $href = '#'; + $onclick = ' onclick="' . substr($url, 11) . ';return false;"'; + } else { + $href = $url; + $onclick = ''; + } + + $class = empty($this->_params['class']) + ? '' + : ' class="' . $this->_params['class'] . '"'; + + return $matches[1] . $matches[2] . $matches[9] . '' . + htmlspecialchars($email) . $args_long + . '' . $matches[8] . $matches[4] . ($matches[14] ? '>' : ''); + } + +} diff --git a/framework/Core/package.xml b/framework/Core/package.xml index 5f8d0deca..8c0ff3a6d 100644 --- a/framework/Core/package.xml +++ b/framework/Core/package.xml @@ -175,6 +175,7 @@ Application Framework. + @@ -415,6 +416,7 @@ Application Framework. + diff --git a/framework/Text_Filter/lib/Horde/Text/Filter/Emails.php b/framework/Text_Filter/lib/Horde/Text/Filter/Emails.php index 1a3ee73bf..18cb5aa5f 100644 --- a/framework/Text_Filter/lib/Horde/Text/Filter/Emails.php +++ b/framework/Text_Filter/lib/Horde/Text/Filter/Emails.php @@ -5,14 +5,13 @@ * * Parameters: *
- * always_mailto - (boolean) If true, a mailto: link is generated always.
- *                 Only if no mail/compose registry API method exists
- *                 otherwise.
- * class - (string) CSS class of the generated  tag.  Defaults to none.
+ * class - (string) CSS class of the generated  tag.
+ *         DEFAULT: ''
  * encode - (boolean) Whether to escape special HTML characters in the URLs
  *          and finally "encode" the complete tag so that it can be decoded
  *          later with the decode() method. This is useful if you want to run
  *          htmlspecialchars() or similar *after* using this filter.
+ *          DEFAULT: false
  * 
* * Copyright 2003-2010 The Horde Project (http://www.horde.org/) @@ -34,138 +33,86 @@ class Horde_Text_Filter_Emails extends Horde_Text_Filter_Base * @var array */ protected $_params = array( - 'always_mailto' => false, 'class' => '', 'encode' => false ); /** + * Email regular expression. + * + * @var string + */ + protected $_regexp = <<)? + /ix +EOR; + + /** * Returns a hash with replace patterns. * * @return array Patterns hash. */ public function getPatterns() { - $class = empty($this->_params['class']) - ? '' - : ' class="' . $this->_params['class'] . '"'; - - $regexp = <<)? - /eix -EOR; - - if (isset($GLOBALS['registry']) && - $GLOBALS['registry']->hasMethod('mail/compose') && - !$this->_params['always_mailto']) { - /* If we have a mail/compose registry method, use it. */ - $replacement = 'Horde_Text_Filter_Emails::callback(\'registry\', \'' - . $this->_params['encode'] . '\', \'' . $class - . '\', \'$1\', \'$2\', \'$3\', \'$4\', \'$5\', \'$7\', \'$8\', \'$9\', \'$10\', \'$11\', \'$13\', \'$14\')'; - } else { - /* Otherwise, generate a standard mailto: and let the browser - * handle it. */ - if ($this->_params['encode']) { - $replacement = <<' . htmlspecialchars('$3$5') . '' - . htmlspecialchars('$4$8') - : htmlspecialchars('$9') . '' . htmlspecialchars('$10$11') . '' - . htmlspecialchars('$14') -EOP; - $replacement = 'chr(1).chr(1).chr(1).base64_encode(' . $replacement . ').chr(1).chr(1).chr(1)'; - } else { - $replacement = 'Horde_Text_Filter_Emails::callback(\'link\', \'' - . $this->_params['encode'] . '\', \'' . $class - . '\', \'$1\', \'$2\', \'$3\', \'$4\', \'$5\', \'$7\', \'$8\', \'$9\', \'$10\', \'$11\', \'$13\', \'$14\')'; - } - } - - return array('regexp' => array($regexp => $replacement)); + return array('regexp_callback' => array( + $this->_regexp => array($this, 'regexCallback') + )); } /** - * TODO + * Regular expression callback. + * + * @param array $matches preg_replace_callback() matches. See regex above + * for description of matching data. + * + * @return string Replacement string. */ - static public function callback($mode, $encode, $class, $bracket1, - $protocol, $email, $closing, $args_long, - $args, $bracket2, $prefix, $email2, - $args_long2, $args2, $closebracket) + public function regexCallback($matches) { - if ($mode == 'link') { - if ($email2 === '') { - return $bracket1 . $protocol . '' . $email . $args_long . '' . $closing . $bracket2; - } - - return (($prefix == '<') ? '<' : $prefix) . '' . $email2 . $args_long2 . '' . ($closebracket ? '>' : ''); - } - - if (!empty($email2)) { - $args = $args2; - $email = $email2; - $args_long = $args_long2; - } - - parse_str($args, $extra); - try { - $url = $GLOBALS['registry']->call('mail/compose', array(array('to' => $email), $extra)); - } catch (Horde_Exception $e) { - $url = 'mailto:' . urlencode($email); - } + $data = $this->_regexCallback($matches); - $url = str_replace('&', '&', $url); - if (substr($url, 0, 11) == 'javascript:') { - $href = '#'; - $onclick = ' onclick="' . substr($url, 11) . ';return false;"'; - } else { - $href = $url; - $onclick = ''; - } + return $this->_params['encode'] + ? "\01\01\01" . base64_encode($data) . "\01\01\01" + : $data; + } - if ($encode) { - return chr(1).chr(1).chr(1) - . base64_encode( - htmlspecialchars($bracket1 . $protocol . $prefix) - . '' - . htmlspecialchars($email . $args_long) . '' - . htmlspecialchars($bracket2)) - . chr(1).chr(1).chr(1) . $closing . ($closebracket ? '>' : ''); - } + /** + * Regular expression callback. + * + * @param array $matches preg_replace_callback() matches. See regex above + * for description of matching data. + * + * @return string Replacement string. + */ + protected function _regexCallback($matches) + { + $class = empty($this->_params['class']) + ? '' + : ' class="' . $this->_params['class'] . '"'; - return $bracket1 . $protocol . $prefix . '' . htmlspecialchars($email) . $args_long - . '' . $bracket2 . $closing . ($closebracket ? '>' : ''); + return ($matches[10] === '') + ? $matches[1] . $matches[2] . '' . $matches[3] . $matches[5] . '' . $matches[4] . $matches[8] + : (($matches[9] == '<') ? '<' : $matches[9]) . '' . $matches[10] . $matches[11] . '' . ($matches[14] ? '>' : ''); } /** -- 2.11.0