]> git.donarmstrong.com Git - roundcube.git/blob - program/lib/icl_commons.inc
Imported Upstream version 0.1~beta2.2~dfsg
[roundcube.git] / program / lib / icl_commons.inc
1 <?php
2 function mod_b64_decode($data){
3         return base64_decode(str_replace(",","/",$data));
4 }
5
6 function mod_b64_encode($data){
7         return str_replace("/",",",str_replace("=","",base64_encode($data)));
8 }
9
10
11 function utf8_to_html($str){
12         $len = strlen($str);
13         $out = "";
14         for($i=0;$i<$len;$i+=2){
15                 $val = ord($str[$i]);
16                 $next_val = ord($str[$i+1]);
17                 if ($val<255){
18                         $out.="&#".($val*256+$next_val).";";
19                 }else{
20                         $out.=$str[$i].$str[$i+1];
21                 }
22         }
23         return $out;
24 }
25
26 function iil_utf7_decode($str, $raw=false){
27         if (strpos($str, '&')===false) return $str;
28         
29         $len = strlen($str);
30         $in_b64 = false;
31         $b64_data = "";
32         $out = "";
33         for ($i=0;$i<$len;$i++){
34                 $char = $str[$i];
35                 if ($char=='&') $in_b64 = true;
36                 else if ($in_b64 && $char=='-'){
37                         $in_b64 = false;
38                         if ($b64_data=="") $out.="&";
39                         else{
40                                 $dec=mod_b64_decode($b64_data);
41                                 $out.=($raw?$dec:utf8_to_html($dec));
42                                 $b64_data = "";
43                         }
44                 }else if ($in_b64) $b64_data.=$char;
45                 else $out.=$char;
46         }
47         return $out;
48 }
49
50 function iil_utf7_encode($str){
51         if (!ereg("[\200-\237]",$str) && !ereg("[\241-\377]",$str))
52         return $str;
53
54         $len = strlen($str);
55
56         for ($i=0;$i<$len;$i++){
57                 $val = ord($str[$i]);
58                 if ($val>=224 && $val<=239){
59                         $unicode = ($val-224) * 4096 + (ord($str[$i+1])-128) * 64 + (ord($str[$i+2])-128);
60                         $i+=2;
61                         $utf_code.=chr((int)($unicode/256)).chr($unicode%256);
62                 }else if ($val>=192 && $val<=223){
63                         $unicode = ($val-192) * 64 + (ord($str[$i+1])-128);
64                         $i++;
65                         $utf_code.=chr((int)($unicode/256)).chr($unicode%256);
66                 }else{
67                         if ($utf_code){
68                                 $out.='&'.mod_b64_encode($utf_code).'-';
69                                 $utf_code="";
70                         }
71                         if ($str[$i]=="-") $out.="&";
72                         $out.=$str[$i];
73                 }
74         }
75         if ($utf_code)
76                 $out.='&'.mod_b64_encode($utf_code).'-';
77         return $out;
78 }
79
80
81 ?>