Changeset 2729 in subversion
- Timestamp:
- Jul 9, 2009 5:02:34 PM (4 years ago)
- Location:
- trunk/roundcubemail/program
- Files:
-
- 2 edited
-
include/main.inc (modified) (4 diffs)
-
lib/utf8.class.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/roundcubemail/program/include/main.inc
r2720 r2729 184 184 static $mbstring_list = null; 185 185 static $convert_warning = false; 186 186 static $conv = null; 187 187 188 $error = false; 188 $conv = null;189 189 190 190 $to = empty($to) ? $to = strtoupper(RCMAIL_CHARSET) : rcube_parse_charset($to); … … 224 224 } 225 225 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 231 227 if ($to == 'UTF-8') { 232 228 if ($from == 'UTF7-IMAP') { 233 229 if ($_str = utf7_to_utf8($str)) 234 $str = $_str; 235 else 236 $error = true; 230 return $_str; 237 231 } 238 232 else if ($from == 'UTF-7') { 239 233 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); 241 242 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; 254 249 } 255 250 … … 259 254 if ($to == 'UTF7-IMAP' || $to == 'UTF-7') { 260 255 if ($_str = utf8_to_utf7($str)) 261 $str = $_str; 262 else 263 $error = true; 256 return $_str; 264 257 } 265 258 else if ($to == 'ISO-8859-1' && function_exists('utf8_decode')) { 266 259 return utf8_decode($str); 267 260 } 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; 276 271 } 277 272 278 273 // report error 279 if ($error && !$convert_warning) {274 if ($error && !$convert_warning) { 280 275 raise_error(array( 281 276 'code' => 500, 282 277 'type' => 'php', 283 278 'file' => __FILE__, 279 'line' => __LINE__, 284 280 'message' => "Could not convert string from $from to $to. Make sure iconv/mbstring is installed or lib/utf8.class is available." 285 281 ), true, false); … … 288 284 } 289 285 290 // return UTF-8 string286 // return UTF-8 or original string 291 287 return $str; 292 288 } -
trunk/roundcubemail/program/lib/utf8.class.php
r2213 r2729 38 38 // Adapted to fit RoundCube 39 39 define("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 );62 40 63 41 //Error constants 64 define("ERR_OPEN_MAP_FILE", "ERR_OPEN_MAP_FILE");42 define("ERR_OPEN_MAP_FILE", "ERR_OPEN_MAP_FILE"); 65 43 66 44 //Class definition 67 Class utf8 {45 Class utf8 { 68 46 69 47 var $charset = "ISO-8859-1"; 70 48 var $ascMap = array(); 71 49 var $utfMap = array(); 50 var $aliases = array( 51 'KOI8-R' => 'KOI8R' 52 ); 53 var $error = null; 72 54 73 function __construct($charset="ISO-8859-1") {55 function __construct($charset="ISO-8859-1") { 74 56 $this->loadCharset($charset); 75 57 } 76 58 77 59 //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; 80 67 81 if ( !is_file($utf8_maps[$charset]))68 if (empty($this->ascMap[$charset])) 82 69 { 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; 85 75 } 86 76 87 if (empty($this->ascMap[$charset])) 88 { 89 $lines = file_get_contents($utf8_maps[$charset]); 77 $lines = file_get_contents($file); 90 78 $lines = preg_replace("/#.*$/m","",$lines); 91 79 $lines = preg_replace("/\n\n/","",$lines); 92 80 $lines = explode("\n",$lines); 81 93 82 foreach($lines as $line){ 94 83 $parts = explode('0x',$line); … … 99 88 } 100 89 } 90 91 $this->utfMap = array_flip($this->ascMap[$charset]); 101 92 } 102 103 $this->charset = $charset;104 $this->utfMap = array_flip($this->ascMap[$charset]);105 93 } 106 94 107 95 //Error handler 108 96 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; 114 99 } 115 100 116 101 //Translate string ($str) to UTF-8 from given charset 117 102 function strToUtf8($str){ 103 if (empty($this->ascMap[$this->charset])) 104 return null; 105 118 106 $chars = unpack('C*', $str); 119 107 $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 121 111 return implode("",$chars); 122 112 } … … 124 114 //Translate UTF-8 string to single byte string in the given charset 125 115 function utf8ToStr($utf){ 116 if (empty($this->ascMap[$this->charset])) 117 return null; 118 126 119 $chars = unpack('C*', $utf); 127 120 $cnt = count($chars); 128 121 $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++) 130 124 $res .= $this->_utf8ToChar($chars, $i); 131 } 125 132 126 return $res; 133 127 }
Note: See TracChangeset
for help on using the changeset viewer.
