| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * imap_general.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This implements all functions that do general imap functions. |
| 10 | * |
| 11 | * $Id $ |
| 12 | */ |
| 13 | |
| 14 | function imap_utf7_encode_local($s) { |
| 15 | global $languages, $squirrelmail_language; |
| 16 | |
| 17 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
| 18 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
| 19 | return $languages[$squirrelmail_language]['XTRA_CODE']('utf7-imap_encode', $s); |
| 20 | } |
| 21 | |
| 22 | global $default_charset; |
| 23 | set_my_charset(); |
| 24 | |
| 25 | if ( ! $default_charset == "iso-8859-1" ) { |
| 26 | // Allows mbstring functions only with iso-8859-*, utf-8 and |
| 27 | // iso-2022-jp (Japanese) |
| 28 | // koi8-r and gb2312 can be added only in php 4.3+ |
| 29 | if ( preg_match("/iso-8859-/i",$default_charset) || |
| 30 | preg_match("/utf-8/i",$default_charset) || |
| 31 | preg_match("/iso-2022-jp/i",$default_charset) ) { |
| 32 | if(function_exists("mb_convert_encoding")) { |
| 33 | return mb_convert_encoding($s, "UTF7-IMAP", $default_charset); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Later code works only for ISO-8859-1 |
| 39 | |
| 40 | $b64_s = ''; // buffer for substring to be base64-encoded |
| 41 | $utf7_s = ''; // imap-utf7-encoded string |
| 42 | for ($i = 0; $i < strlen($s); $i++) { |
| 43 | $c = $s[$i]; |
| 44 | $ord_c = ord($c); |
| 45 | if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or |
| 46 | (($ord_c >= 0x27) and ($ord_c <= 0x7e))) { |
| 47 | if ($b64_s) { |
| 48 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-'; |
| 49 | $b64_s = ''; |
| 50 | } |
| 51 | $utf7_s = $utf7_s . $c; |
| 52 | } elseif ($ord_c == 0x26) { |
| 53 | if ($b64_s) { |
| 54 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-'; |
| 55 | $b64_s = ''; |
| 56 | } |
| 57 | $utf7_s = $utf7_s . '&-'; |
| 58 | } else { |
| 59 | $b64_s = $b64_s . chr(0) . $c; |
| 60 | } |
| 61 | } |
| 62 | // |
| 63 | // flush buffer |
| 64 | // |
| 65 | if ($b64_s) { |
| 66 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-'; |
| 67 | $b64_s = ''; |
| 68 | } |
| 69 | return $utf7_s; |
| 70 | } |
| 71 | |
| 72 | function imap_utf7_decode_local($s) { |
| 73 | global $languages, $squirrelmail_language; |
| 74 | |
| 75 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
| 76 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
| 77 | return $languages[$squirrelmail_language]['XTRA_CODE']('utf7-imap_decode', $s); |
| 78 | } |
| 79 | |
| 80 | global $default_charset; |
| 81 | set_my_charset(); |
| 82 | |
| 83 | // FIXME: Something wrong. if I enable this test - decoding works incorrectly |
| 84 | // if ( ! $default_charset == "iso-8859-1" ) { |
| 85 | // Allows mbstring functions only with iso-8859-*, utf-8 and |
| 86 | // iso-2022-jp (Japanese) |
| 87 | // koi8-r and gb2312 can be added only in php 4.3+ |
| 88 | if ( preg_match("/iso-8859-/i",$default_charset) || |
| 89 | preg_match("/utf-8/i",$default_charset) || |
| 90 | preg_match("/iso-2022-jp/i",$default_charset) ) { |
| 91 | if(function_exists("mb_convert_encoding")) { |
| 92 | return mb_convert_encoding($s, $default_charset, "UTF7-IMAP"); |
| 93 | } |
| 94 | } |
| 95 | // } |
| 96 | |
| 97 | // Later code works only for ISO-8859-1 |
| 98 | |
| 99 | $b64_s = ''; |
| 100 | $iso_8859_1_s = ''; |
| 101 | for ($i = 0, $len = strlen($s); $i < $len; $i++) { |
| 102 | $c = $s[$i]; |
| 103 | if (strlen($b64_s) > 0) { |
| 104 | if ($c == '-') { |
| 105 | if ($b64_s == '&') { |
| 106 | $iso_8859_1_s = $iso_8859_1_s . '&'; |
| 107 | } else { |
| 108 | $iso_8859_1_s = $iso_8859_1_s . |
| 109 | decodeBASE64(substr($b64_s, 1)); |
| 110 | } |
| 111 | $b64_s = ''; |
| 112 | } else { |
| 113 | $b64_s = $b64_s . $c; |
| 114 | } |
| 115 | } else { |
| 116 | if ($c == '&') { |
| 117 | $b64_s = '&'; |
| 118 | } else { |
| 119 | $iso_8859_1_s = $iso_8859_1_s . $c; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | return $iso_8859_1_s; |
| 124 | } |
| 125 | |
| 126 | function encodeBASE64($s) { |
| 127 | $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,'; |
| 128 | $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3... |
| 129 | $e = ''; // base64-encoded string |
| 130 | //foreach($s as $c) { |
| 131 | for ($i = 0; $i < strlen($s); $i++) { |
| 132 | $c = $s[$i]; |
| 133 | if ($p == 0) { |
| 134 | $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1); |
| 135 | $t = (ord($c) & 3); |
| 136 | $p = 1; |
| 137 | } elseif ($p == 1) { |
| 138 | $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)]; |
| 139 | $t = (ord($c) & 15); |
| 140 | $p = 2; |
| 141 | } elseif ($p == 2) { |
| 142 | $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)]; |
| 143 | $e = $e . $B64Chars[ord($c) & 63]; |
| 144 | $p = 0; |
| 145 | } |
| 146 | } |
| 147 | // |
| 148 | // flush buffer |
| 149 | // |
| 150 | if ($p == 1) { |
| 151 | $e = $e . $B64Chars[$t << 4]; |
| 152 | } elseif ($p == 2) { |
| 153 | $e = $e . $B64Chars[$t << 2]; |
| 154 | } |
| 155 | return $e; |
| 156 | } |
| 157 | |
| 158 | function decodeBASE64($s) { |
| 159 | $B64Values = array( |
| 160 | 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, |
| 161 | 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, |
| 162 | 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, |
| 163 | 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, |
| 164 | 'Y' => 24, 'Z' => 25, |
| 165 | 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31, |
| 166 | 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37, |
| 167 | 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43, |
| 168 | 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49, |
| 169 | 'y' => 50, 'z' => 51, |
| 170 | '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57, |
| 171 | '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63 |
| 172 | ); |
| 173 | $p = 0; |
| 174 | $d = ''; |
| 175 | $unicodeNullByteToggle = 0; |
| 176 | for ($i = 0, $len = strlen($s); $i < $len; $i++) { |
| 177 | $c = $s[$i]; |
| 178 | if ($p == 0) { |
| 179 | $t = $B64Values[$c]; |
| 180 | $p = 1; |
| 181 | } elseif ($p == 1) { |
| 182 | if ($unicodeNullByteToggle) { |
| 183 | $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4)); |
| 184 | $unicodeNullByteToggle = 0; |
| 185 | } else { |
| 186 | $unicodeNullByteToggle = 1; |
| 187 | } |
| 188 | $t = ($B64Values[$c] & 15); |
| 189 | $p = 2; |
| 190 | } elseif ($p == 2) { |
| 191 | if ($unicodeNullByteToggle) { |
| 192 | $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2)); |
| 193 | $unicodeNullByteToggle = 0; |
| 194 | } else { |
| 195 | $unicodeNullByteToggle = 1; |
| 196 | } |
| 197 | $t = ($B64Values[$c] & 3); |
| 198 | $p = 3; |
| 199 | } elseif ($p == 3) { |
| 200 | if ($unicodeNullByteToggle) { |
| 201 | $d = $d . chr(($t << 6) + $B64Values[$c]); |
| 202 | $unicodeNullByteToggle = 0; |
| 203 | } else { |
| 204 | $unicodeNullByteToggle = 1; |
| 205 | } |
| 206 | $t = ($B64Values[$c] & 3); |
| 207 | $p = 0; |
| 208 | } |
| 209 | } |
| 210 | return $d; |
| 211 | } |
| 212 | |
| 213 | ?> |