- speed up plain text messages parsing (up to 60%)

release-0.6
alecpl 15 years ago
parent f042be35ae
commit 88ed237f7c

@ -28,10 +28,20 @@
class rcube_string_replacer class rcube_string_replacer
{ {
public static $pattern = '/##str_replacement\[([0-9]+)\]##/'; public static $pattern = '/##str_replacement\[([0-9]+)\]##/';
public $mailto_pattern;
public $link_pattern;
private $values = array(); private $values = array();
function __construct()
{
$url_chars = 'a-z0-9_\-\+\*\$\/&%=@#:;';
$url_chars_within = '\?\.~,!';
$this->link_pattern = "/([\w]+:\/\/|\Wwww\.)([a-z0-9\-\.]+[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/i";
$this->mailto_pattern = "/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/i";
}
/** /**
* Add a string to the internal list * Add a string to the internal list
* *
@ -64,13 +74,13 @@ class rcube_string_replacer
$i = -1; $i = -1;
$scheme = strtolower($matches[1]); $scheme = strtolower($matches[1]);
if ($scheme == 'http' || $scheme == 'https' || $scheme == 'ftp') { if ($scheme == 'http://' || $scheme == 'https://' || $scheme == 'ftp://') {
$url = $matches[1] . '://' . $matches[2]; $url = $matches[1] . $matches[2];
$i = $this->add(html::a(array('href' => $url, 'target' => "_blank"), Q($url))); $i = $this->add(html::a(array('href' => $url, 'target' => '_blank'), Q($url)));
} }
else if ($matches[2] == 'www.') { else if (preg_match('/^(\W)www\.$/', $matches[1], $m)) {
$url = $matches[2] . $matches[3]; $url = 'www.' . $matches[2];
$i = $this->add($matches[1] . html::a(array('href' => 'http://' . $url, 'target' => "_blank"), Q($url))); $i = $this->add($m[1] . html::a(array('href' => 'http://' . $url, 'target' => '_blank'), Q($url)));
} }
return $i >= 0 ? $this->get_replacement($i) : ''; return $i >= 0 ? $this->get_replacement($i) : '';

@ -776,50 +776,54 @@ function rcmail_print_body($part, $p = array())
// free some memory (hopefully) // free some memory (hopefully)
unset($data['body']); unset($data['body']);
// plaintext postprocessing // plaintext postprocessing
if ($part->ctype_secondary == 'plain') { if ($part->ctype_secondary == 'plain') {
// make links and email-addresses clickable // make links and email-addresses clickable
$replacements = new rcube_string_replacer; $replacements = new rcube_string_replacer;
$url_chars = 'a-z0-9_\-\+\*\$\/&%=@#:;';
$url_chars_within = '\?\.~,!';
// search for patterns like links and e-mail addresses // search for patterns like links and e-mail addresses
$body = preg_replace_callback("/([\w]+):\/\/([a-z0-9\-\.]+[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/i", array($replacements, 'link_callback'), $body); $body = preg_replace_callback($replacements->link_pattern, array($replacements, 'link_callback'), $body);
$body = preg_replace_callback("/([^\/:]|\s)(www\.)([a-z0-9\-]{2,}[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/i", array($replacements, 'link_callback'), $body); $body = preg_replace_callback($replacements->mailto_pattern, array($replacements, 'mailto_callback'), $body);
$body = preg_replace_callback('/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/i', array($replacements, 'mailto_callback'), $body);
// split body into single lines // split body into single lines
$a_lines = preg_split('/\r?\n/', $body); $a_lines = preg_split('/\r?\n/', $body);
$q_lines = array();
$quote_level = 0; $quote_level = 0;
// colorize quoted parts // find/mark quoted lines...
for ($n=0; $n < count($a_lines); $n++) { for ($n=0, $cnt=count($a_lines); $n < $cnt; $n++) {
$line = $a_lines[$n];
$quotation = '';
$q = 0; $q = 0;
if (preg_match('/^(>+\s*)+/', $line, $regs)) { if ($a_lines[$n][0] == '>' && preg_match('/^(>+\s*)+/', $a_lines[$n], $regs)) {
$q = strlen(preg_replace('/\s/', '', $regs[0])); $q = strlen(preg_replace('/\s/', '', $regs[0]));
$line = substr($line, strlen($regs[0])); $a_lines[$n] = substr($a_lines[$n], strlen($regs[0]));
if ($q > $quote_level) if ($q > $quote_level)
$quotation = str_repeat('<blockquote>', $q - $quote_level); $q_lines[$n]['quote'] = $q - $quote_level;
else if ($q < $quote_level) else if ($q < $quote_level)
$quotation = str_repeat("</blockquote>", $quote_level - $q); $eq_lines[$n]['endquote'] = $quote_level - $q;
} }
else if ($quote_level > 0) else if ($quote_level > 0)
$quotation = str_repeat("</blockquote>", $quote_level); $q_lines[$n]['endquote'] = $quote_level;
$quote_level = $q; $quote_level = $q;
$a_lines[$n] = $quotation . Q($line, 'replace', false); // htmlquote plaintext
} }
// quote plain text
$body = Q(join("\n", $a_lines), 'replace', false);
// ... colorize quoted lines
$a_lines = preg_split('/\n/', $body);
foreach ($q_lines as $i => $q)
if ($q['quote'])
$a_lines[$i] = str_repeat('<blockquote>', $q['quote']) . $a_lines[$i];
else if ($q['endquote'])
$a_lines[$i] = str_repeat('</blockquote>', $q['endquote']) . $a_lines[$i];
// insert the links for urls and mailtos // insert the links for urls and mailtos
$body = $replacements->resolve(join("\n", $a_lines)); $body = $replacements->resolve(join("\n", $a_lines));
} }
// allow post-processing of the message body // allow post-processing of the message body
$data = $RCMAIL->plugins->exec_hook('message_part_after', array('type' => $part->ctype_secondary, 'body' => $body) + $data); $data = $RCMAIL->plugins->exec_hook('message_part_after', array('type' => $part->ctype_secondary, 'body' => $body) + $data);
@ -990,7 +994,6 @@ function rcmail_message_body($attrib)
else else
$out .= html::div('message-part', html::tag('pre', array(), Q($MESSAGE->body))); $out .= html::div('message-part', html::tag('pre', array(), Q($MESSAGE->body)));
$ctype_primary = strtolower($MESSAGE->structure->ctype_primary); $ctype_primary = strtolower($MESSAGE->structure->ctype_primary);
$ctype_secondary = strtolower($MESSAGE->structure->ctype_secondary); $ctype_secondary = strtolower($MESSAGE->structure->ctype_secondary);

Loading…
Cancel
Save