Changeset 88ed237 in github


Ignore:
Timestamp:
Jun 4, 2009 6:27:19 AM (4 years ago)
Author:
alecpl <alec@…>
Branches:
master, HEAD, courier-fix, dev-browser-capabilities, pdo, release-0.6, release-0.7, release-0.8
Children:
0061e7b
Parents:
f042be3
Message:
  • speed up plain text messages parsing (up to 60%)
Location:
program
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • program/include/rcube_string_replacer.php

    raa055c9 r88ed237  
    2929{ 
    3030  public static $pattern = '/##str_replacement\[([0-9]+)\]##/'; 
    31  
     31  public $mailto_pattern; 
     32  public $link_pattern; 
    3233  private $values = array(); 
    3334 
     35 
     36  function __construct() 
     37  { 
     38    $url_chars = 'a-z0-9_\-\+\*\$\/&%=@#:;'; 
     39    $url_chars_within = '\?\.~,!'; 
     40 
     41    $this->link_pattern = "/([\w]+:\/\/|\Wwww\.)([a-z0-9\-\.]+[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/i"; 
     42    $this->mailto_pattern = "/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/i"; 
     43  } 
    3444 
    3545  /** 
     
    6575    $scheme = strtolower($matches[1]); 
    6676 
    67     if ($scheme == 'http' || $scheme == 'https' || $scheme == 'ftp') { 
    68       $url = $matches[1] . '://' . $matches[2]; 
    69       $i = $this->add(html::a(array('href' => $url, 'target' => "_blank"), Q($url))); 
     77    if ($scheme == 'http://' || $scheme == 'https://' || $scheme == 'ftp://') { 
     78      $url = $matches[1] . $matches[2]; 
     79      $i = $this->add(html::a(array('href' => $url, 'target' => '_blank'), Q($url))); 
    7080    } 
    71     else if ($matches[2] == 'www.') { 
    72       $url = $matches[2] . $matches[3]; 
    73       $i = $this->add($matches[1] . html::a(array('href' => 'http://' . $url, 'target' => "_blank"), Q($url))); 
     81    else if (preg_match('/^(\W)www\.$/', $matches[1], $m)) { 
     82      $url = 'www.' . $matches[2]; 
     83      $i = $this->add($m[1] . html::a(array('href' => 'http://' . $url, 'target' => '_blank'), Q($url))); 
    7484    } 
    7585 
  • program/steps/mail/func.inc

    re54f6c7a r88ed237  
    777777  unset($data['body']); 
    778778 
    779  
    780779  // plaintext postprocessing 
    781780  if ($part->ctype_secondary == 'plain') { 
     
    783782    $replacements = new rcube_string_replacer; 
    784783     
    785     $url_chars = 'a-z0-9_\-\+\*\$\/&%=@#:;'; 
    786     $url_chars_within = '\?\.~,!'; 
    787  
    788784    // search for patterns like links and e-mail addresses 
    789     $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); 
    790     $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); 
    791     $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); 
     785    $body = preg_replace_callback($replacements->link_pattern, array($replacements, 'link_callback'), $body); 
     786    $body = preg_replace_callback($replacements->mailto_pattern, array($replacements, 'mailto_callback'), $body); 
    792787 
    793788    // split body into single lines 
    794789    $a_lines = preg_split('/\r?\n/', $body); 
     790    $q_lines = array(); 
    795791    $quote_level = 0; 
    796792 
    797     // colorize quoted parts 
    798     for ($n=0; $n < count($a_lines); $n++) { 
    799       $line = $a_lines[$n]; 
    800       $quotation = ''; 
     793    // find/mark quoted lines... 
     794    for ($n=0, $cnt=count($a_lines); $n < $cnt; $n++) { 
    801795      $q = 0; 
    802796     
    803       if (preg_match('/^(>+\s*)+/', $line, $regs)) { 
    804         $q    = strlen(preg_replace('/\s/', '', $regs[0])); 
    805         $line = substr($line, strlen($regs[0])); 
     797      if ($a_lines[$n][0] == '>' && preg_match('/^(>+\s*)+/', $a_lines[$n], $regs)) { 
     798        $q = strlen(preg_replace('/\s/', '', $regs[0])); 
     799        $a_lines[$n] = substr($a_lines[$n], strlen($regs[0])); 
    806800 
    807801        if ($q > $quote_level) 
    808           $quotation = str_repeat('<blockquote>', $q - $quote_level); 
     802          $q_lines[$n]['quote'] = $q - $quote_level; 
    809803        else if ($q < $quote_level) 
    810           $quotation = str_repeat("</blockquote>", $quote_level - $q); 
     804          $eq_lines[$n]['endquote'] = $quote_level - $q; 
    811805      } 
    812806      else if ($quote_level > 0) 
    813         $quotation = str_repeat("</blockquote>", $quote_level); 
     807        $q_lines[$n]['endquote'] = $quote_level; 
    814808 
    815809      $quote_level = $q; 
    816       $a_lines[$n] = $quotation . Q($line, 'replace', false);  // htmlquote plaintext 
    817     } 
     810    } 
     811 
     812    // quote plain text 
     813    $body = Q(join("\n", $a_lines), 'replace', false); 
     814 
     815    // ... colorize quoted lines 
     816    $a_lines = preg_split('/\n/', $body); 
     817    foreach ($q_lines as $i => $q) 
     818      if ($q['quote']) 
     819        $a_lines[$i] = str_repeat('<blockquote>', $q['quote']) . $a_lines[$i]; 
     820      else if ($q['endquote']) 
     821        $a_lines[$i] = str_repeat('</blockquote>', $q['endquote']) . $a_lines[$i]; 
    818822 
    819823    // insert the links for urls and mailtos 
    820824    $body = $replacements->resolve(join("\n", $a_lines)); 
    821825  } 
    822    
     826 
    823827  // allow post-processing of the message body 
    824828  $data = $RCMAIL->plugins->exec_hook('message_part_after', array('type' => $part->ctype_secondary, 'body' => $body) + $data); 
     
    991995    $out .= html::div('message-part', html::tag('pre', array(), Q($MESSAGE->body))); 
    992996 
    993  
    994997  $ctype_primary = strtolower($MESSAGE->structure->ctype_primary); 
    995998  $ctype_secondary = strtolower($MESSAGE->structure->ctype_secondary); 
Note: See TracChangeset for help on using the changeset viewer.