Changeset 2729 in subversion


Ignore:
Timestamp:
Jul 9, 2009 5:02:34 PM (4 years ago)
Author:
alec
Message:
  • simplify 'utf8' class use, make rcube_charset_convert() 5x faster on systems without mbstring and iconv installed
Location:
trunk/roundcubemail/program
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/roundcubemail/program/include/main.inc

    r2720 r2729  
    184184  static $mbstring_list = null; 
    185185  static $convert_warning = false; 
    186  
     186  static $conv = null; 
     187   
    187188  $error = false; 
    188   $conv = null; 
    189189 
    190190  $to = empty($to) ? $to = strtoupper(RCMAIL_CHARSET) : rcube_parse_charset($to); 
     
    224224  } 
    225225 
    226   # try to convert with custom classes 
    227   if (class_exists('utf8')) 
    228     $conv = new utf8(); 
    229  
    230   // convert string to UTF-8 
     226  // convert charset using bundled classes/functions 
    231227  if ($to == 'UTF-8') { 
    232228    if ($from == 'UTF7-IMAP') { 
    233229      if ($_str = utf7_to_utf8($str)) 
    234         $str = $_str; 
    235       else 
    236         $error = true; 
     230        return $_str; 
    237231    } 
    238232    else if ($from == 'UTF-7') { 
    239233      if ($_str = rcube_utf7_to_utf8($str)) 
    240         $str = $_str; 
     234        return $_str; 
     235    } 
     236    else if (($from == 'ISO-8859-1') && function_exists('utf8_encode')) { 
     237      return utf8_encode($str); 
     238    } 
     239    else if (class_exists('utf8')) { 
     240      if (!$conv) 
     241        $conv = new utf8($from); 
    241242      else 
    242         $error = true; 
    243     } 
    244     else if (($from == 'ISO-8859-1') && function_exists('utf8_encode')) { 
    245       $str = utf8_encode($str); 
    246     } 
    247     else if ($from != 'UTF-8' && $conv) { 
    248       $from = preg_replace(array('/^WINDOWS-*125([0-8])$/', '/^CP-/'), array('CP125\\1', 'CP'), $from); 
    249       $conv->loadCharset($from); 
    250       $str = $conv->strToUtf8($str); 
    251     } 
    252     else if ($from != 'UTF-8') 
    253       $error = true; 
     243        $conv->loadCharset($from); 
     244 
     245      if($_str = $conv->strToUtf8($str)) 
     246        return $_str; 
     247    } 
     248    $error = true; 
    254249  } 
    255250   
     
    259254    if ($to == 'UTF7-IMAP' || $to == 'UTF-7') { 
    260255      if ($_str = utf8_to_utf7($str)) 
    261         $str = $_str; 
    262       else 
    263         $error = true; 
     256        return $_str; 
    264257    } 
    265258    else if ($to == 'ISO-8859-1' && function_exists('utf8_decode')) { 
    266259      return utf8_decode($str); 
    267260    } 
    268     else if ($to != 'UTF-8' && $conv) { 
    269       $to = preg_replace(array('/^WINDOWS-*125([0-8])$/', '/^CP-/'), array('CP125\\1', 'CP'), $to); 
    270       $conv->loadCharset($to); 
    271       return $conv->utf8ToStr($str); 
    272     } 
    273     else if ($to != 'UTF-8') { 
    274       $error = true; 
    275     } 
     261    else if (class_exists('utf8')) { 
     262      if (!$conv) 
     263        $conv = new utf8($to); 
     264      else 
     265        $conv->loadCharset($from); 
     266 
     267      if ($_str = $conv->strToUtf8($str)) 
     268        return $_str; 
     269    } 
     270    $error = true; 
    276271  } 
    277272   
    278273  // report error 
    279   if ($error && !$convert_warning){ 
     274  if ($error && !$convert_warning) { 
    280275    raise_error(array( 
    281276      'code' => 500, 
    282277      'type' => 'php', 
    283278      'file' => __FILE__, 
     279      'line' => __LINE__, 
    284280      'message' => "Could not convert string from $from to $to. Make sure iconv/mbstring is installed or lib/utf8.class is available." 
    285281      ), true, false); 
     
    288284  } 
    289285   
    290   // return UTF-8 string 
     286  // return UTF-8 or original string 
    291287  return $str; 
    292288  } 
  • trunk/roundcubemail/program/lib/utf8.class.php

    r2213 r2729  
    3838// Adapted to fit RoundCube 
    3939define("UTF8_MAP_DIR", "program/lib/encoding"); 
    40 $utf8_maps = array( 
    41   "CP1250" => UTF8_MAP_DIR . "/CP1250.map", 
    42   "CP1251" => UTF8_MAP_DIR . "/CP1251.map", 
    43   "CP1252" => UTF8_MAP_DIR . "/CP1252.map", 
    44   "CP1253" => UTF8_MAP_DIR . "/CP1253.map", 
    45   "CP1254" => UTF8_MAP_DIR . "/CP1254.map", 
    46   "CP1255" => UTF8_MAP_DIR . "/CP1255.map", 
    47   "CP1256" => UTF8_MAP_DIR . "/CP1256.map", 
    48   "CP1257" => UTF8_MAP_DIR . "/CP1257.map", 
    49   "CP1258" => UTF8_MAP_DIR . "/CP1258.map", 
    50   "ISO-8859-1" => UTF8_MAP_DIR . "/ISO-8859-1.map", 
    51   "ISO-8859-2" => UTF8_MAP_DIR . "/ISO-8859-2.map", 
    52   "ISO-8859-3" => UTF8_MAP_DIR . "/ISO-8859-3.map", 
    53   "ISO-8859-4" => UTF8_MAP_DIR . "/ISO-8859-4.map", 
    54   "ISO-8859-5" => UTF8_MAP_DIR . "/ISO-8859-5.map", 
    55   "ISO-8859-6" => UTF8_MAP_DIR . "/ISO-8859-6.map", 
    56   "ISO-8859-7" => UTF8_MAP_DIR . "/ISO-8859-7.map", 
    57   "ISO-8859-8" => UTF8_MAP_DIR . "/ISO-8859-8.map", 
    58   "ISO-8859-9" => UTF8_MAP_DIR . "/ISO-8859-9.map", 
    59   "KOI8-R" => UTF8_MAP_DIR . "/KOI8R.map", 
    60   "KOI8R" => UTF8_MAP_DIR . "/KOI8R.map" 
    61   ); 
    6240 
    6341//Error constants 
    64 define("ERR_OPEN_MAP_FILE","ERR_OPEN_MAP_FILE"); 
     42define("ERR_OPEN_MAP_FILE", "ERR_OPEN_MAP_FILE"); 
    6543 
    6644//Class definition 
    67 Class utf8{ 
     45Class utf8 { 
    6846 
    6947  var $charset = "ISO-8859-1"; 
    7048  var $ascMap = array(); 
    7149  var $utfMap = array(); 
     50  var $aliases = array( 
     51    'KOI8-R' => 'KOI8R' 
     52  ); 
     53  var $error = null; 
    7254 
    73   function __construct($charset="ISO-8859-1"){ 
     55  function __construct($charset="ISO-8859-1") { 
    7456    $this->loadCharset($charset); 
    7557  } 
    7658   
    7759  //Load charset 
    78   function loadCharset($charset){ 
    79     global $utf8_maps; 
     60  function loadCharset($charset) { 
     61     
     62    $charset = preg_replace(array('/^WINDOWS-*125([0-8])$/', '/^CP-/'), array('CP125\\1', 'CP'), $charset); 
     63    if (isset($aliases[$charset])) 
     64      $charset = $aliases[$charset]; 
     65     
     66    $this->charset = $charset; 
    8067 
    81     if (!is_file($utf8_maps[$charset])) 
     68    if (empty($this->ascMap[$charset])) 
    8269      { 
    83       $this->onError(ERR_OPEN_MAP_FILE, "Failed to open map file for $charset"); 
    84       return; 
     70      $file = UTF8_MAP_DIR.'/'.$charset.'.map'; 
     71     
     72      if (!is_file($file)) { 
     73        $this->onError(ERR_OPEN_MAP_FILE, "Failed to open map file for $charset"); 
     74        return; 
    8575      } 
    8676     
    87     if (empty($this->ascMap[$charset])) 
    88       { 
    89       $lines = file_get_contents($utf8_maps[$charset]); 
     77      $lines = file_get_contents($file); 
    9078      $lines = preg_replace("/#.*$/m","",$lines); 
    9179      $lines = preg_replace("/\n\n/","",$lines); 
    9280      $lines = explode("\n",$lines); 
     81 
    9382      foreach($lines as $line){ 
    9483        $parts = explode('0x',$line); 
     
    9988        } 
    10089      } 
     90       
     91      $this->utfMap = array_flip($this->ascMap[$charset]); 
    10192    } 
    102      
    103     $this->charset = $charset; 
    104     $this->utfMap = array_flip($this->ascMap[$charset]); 
    10593  } 
    10694 
    10795  //Error handler 
    10896  function onError($err_code,$err_text){ 
    109     //print($err_code . " : " . $err_text . "<hr>\n"); 
    110     raise_error(array('code' => 500, 
    111                       'type' => 'php', 
    112                       'file' => __FILE__, 
    113                       'message' => $err_text), TRUE, FALSE); 
     97    $this->error = $err_text; 
     98    return null; 
    11499  } 
    115100 
    116101  //Translate string ($str) to UTF-8 from given charset 
    117102  function strToUtf8($str){ 
     103    if (empty($this->ascMap[$this->charset])) 
     104      return null; 
     105 
    118106    $chars = unpack('C*', $str); 
    119107    $cnt = count($chars); 
    120     for($i=1;$i<=$cnt;$i++) $this->_charToUtf8($chars[$i]); 
     108    for($i=1; $i<=$cnt; $i++) 
     109      $this->_charToUtf8($chars[$i]); 
     110 
    121111    return implode("",$chars); 
    122112  } 
     
    124114  //Translate UTF-8 string to single byte string in the given charset 
    125115  function utf8ToStr($utf){ 
     116    if (empty($this->ascMap[$this->charset])) 
     117      return null; 
     118 
    126119    $chars = unpack('C*', $utf); 
    127120    $cnt = count($chars); 
    128121    $res = ""; //No simple way to do it in place... concatenate char by char 
    129     for ($i=1;$i<=$cnt;$i++){ 
     122 
     123    for ($i=1; $i<=$cnt; $i++) 
    130124      $res .= $this->_utf8ToChar($chars, $i); 
    131     } 
     125 
    132126    return $res; 
    133127  } 
Note: See TracChangeset for help on using the changeset viewer.