imap_utf7_local.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * functions/imap_utf7_local.php - utf7-imap functions
  4. *
  5. * This implements all functions that do imap UTF7 conversions.
  6. * Before 1.3.2 functions were stored in imap_utf7_decode_local.php and
  7. * imap_utf7_encode_local.php files.
  8. *
  9. * @copyright 1999-2025 The SquirrelMail Project Team
  10. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11. * @version $Id$
  12. * @package squirrelmail
  13. * @subpackage imap
  14. * @since 1.3.2
  15. */
  16. /**
  17. * Function that uses php mbstring functions to convert from and to utf7-imap charset
  18. *
  19. * Since 1.5.1 list of supported charsets depends sq_mb_list_encoding function.
  20. * Before that it was hardcoded to iso-8859-x, utf-8 and iso-2022-jp.
  21. * @param string $str folder name
  22. * @param string $to_encoding name of resulting charset
  23. * @param string $from_encoding name of original charset
  24. * @param string $default_charset default charset used by translation.
  25. * @return string encoded folder name or ''
  26. * @since 1.4.2
  27. */
  28. function sqimap_mb_convert_encoding($str, $to_encoding, $from_encoding, $default_charset) {
  29. $supported_encodings=sq_mb_list_encodings();
  30. if ( in_array(strtolower($default_charset),$supported_encodings) &&
  31. function_exists('mb_convert_encoding')) {
  32. return mb_convert_encoding($str, $to_encoding, $from_encoding);
  33. }
  34. return '';
  35. }
  36. /**
  37. * encode folder name to utf7-imap
  38. *
  39. * If mbstring functions do not support charset used by translation, falls back to iso-8859-1
  40. * @param string $s folder name
  41. * @return string utf7-imap encoded folder name
  42. * @since 1.2.7
  43. */
  44. function imap_utf7_encode_local($s) {
  45. global $languages, $squirrelmail_language;
  46. if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
  47. function_exists($languages[$squirrelmail_language]['XTRA_CODE'].'_utf7_imap_encode')) {
  48. return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_encode', $s);
  49. }
  50. if ($s == '') //If empty, don't bother
  51. return '';
  52. global $default_charset;
  53. set_my_charset(); //must be called before using $default_charset
  54. if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) {
  55. $utf7_s = sqimap_mb_convert_encoding($s, 'UTF7-IMAP', $default_charset, $default_charset);
  56. if ($utf7_s != '')
  57. return $utf7_s;
  58. }
  59. // Later code works only for ISO-8859-1
  60. $b64_s = ''; // buffer for substring to be base64-encoded
  61. $utf7_s = ''; // imap-utf7-encoded string
  62. for ($i = 0; $i < strlen($s); $i++) {
  63. $c = $s[$i];
  64. $ord_c = ord($c);
  65. if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or
  66. (($ord_c >= 0x27) and ($ord_c <= 0x7e))) {
  67. if ($b64_s) {
  68. $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-';
  69. $b64_s = '';
  70. }
  71. $utf7_s = $utf7_s . $c;
  72. } elseif ($ord_c == 0x26) {
  73. if ($b64_s) {
  74. $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
  75. $b64_s = '';
  76. }
  77. $utf7_s = $utf7_s . '&-';
  78. } else {
  79. $b64_s = $b64_s . chr(0) . $c;
  80. }
  81. }
  82. //
  83. // flush buffer
  84. //
  85. if ($b64_s) {
  86. $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
  87. $b64_s = '';
  88. }
  89. return $utf7_s;
  90. }
  91. /**
  92. * converts folder name from utf7-imap to charset used by translation
  93. *
  94. * If mbstring functions do not support charset used by translation, falls back to iso-8859-1
  95. * @param string $s folder name in utf7-imap
  96. * @return string folder name in charset used by translation
  97. * @since 1.2.7
  98. */
  99. function imap_utf7_decode_local($s) {
  100. global $languages, $squirrelmail_language;
  101. if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
  102. function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_decode')) {
  103. return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_decode', $s);
  104. }
  105. if ($s == '') //If empty, don't bother
  106. return '';
  107. global $default_charset;
  108. set_my_charset(); //must be called before using $default_charset
  109. if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) {
  110. $utf7_s = sqimap_mb_convert_encoding($s, $default_charset, 'UTF7-IMAP', $default_charset);
  111. if ($utf7_s != '')
  112. return $utf7_s;
  113. }
  114. // Later code works only for ISO-8859-1
  115. $b64_s = '';
  116. $iso_8859_1_s = '';
  117. for ($i = 0, $len = strlen($s); $i < $len; $i++) {
  118. $c = $s[$i];
  119. if (strlen($b64_s) > 0) {
  120. if ($c == '-') {
  121. if ($b64_s == '&') {
  122. $iso_8859_1_s = $iso_8859_1_s . '&';
  123. } else {
  124. $iso_8859_1_s = $iso_8859_1_s .
  125. decodeBASE64(substr($b64_s, 1));
  126. }
  127. $b64_s = '';
  128. } else {
  129. $b64_s = $b64_s . $c;
  130. }
  131. } else {
  132. if ($c == '&') {
  133. $b64_s = '&';
  134. } else {
  135. $iso_8859_1_s = $iso_8859_1_s . $c;
  136. }
  137. }
  138. }
  139. return $iso_8859_1_s;
  140. }
  141. /**
  142. * Converts string to base64
  143. * @param string $s string
  144. * @return string base64 encoded string
  145. * @since 1.2.7
  146. */
  147. function encodeBASE64($s) {
  148. $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,';
  149. $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3...
  150. $e = ''; // base64-encoded string
  151. //foreach($s as $c) {
  152. for ($i = 0; $i < strlen($s); $i++) {
  153. $c = $s[$i];
  154. if ($p == 0) {
  155. $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1);
  156. $t = (ord($c) & 3);
  157. $p = 1;
  158. } elseif ($p == 1) {
  159. $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)];
  160. $t = (ord($c) & 15);
  161. $p = 2;
  162. } elseif ($p == 2) {
  163. $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)];
  164. $e = $e . $B64Chars[ord($c) & 63];
  165. $p = 0;
  166. }
  167. }
  168. //
  169. // flush buffer
  170. //
  171. if ($p == 1) {
  172. $e = $e . $B64Chars[$t << 4];
  173. } elseif ($p == 2) {
  174. $e = $e . $B64Chars[$t << 2];
  175. }
  176. return $e;
  177. }
  178. /**
  179. * Converts string from base64
  180. * @param string $s base64 encoded string
  181. * @return string decoded string
  182. * @since 1.2.7
  183. */
  184. function decodeBASE64($s) {
  185. $B64Values = array(
  186. 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5,
  187. 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11,
  188. 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17,
  189. 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23,
  190. 'Y' => 24, 'Z' => 25,
  191. 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31,
  192. 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37,
  193. 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43,
  194. 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49,
  195. 'y' => 50, 'z' => 51,
  196. '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57,
  197. '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63
  198. );
  199. $p = 0;
  200. $d = '';
  201. $unicodeNullByteToggle = 0;
  202. for ($i = 0, $len = strlen($s); $i < $len; $i++) {
  203. $c = $s[$i];
  204. if ($p == 0) {
  205. $t = $B64Values[$c];
  206. $p = 1;
  207. } elseif ($p == 1) {
  208. if ($unicodeNullByteToggle) {
  209. $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4));
  210. $unicodeNullByteToggle = 0;
  211. } else {
  212. $unicodeNullByteToggle = 1;
  213. }
  214. $t = ($B64Values[$c] & 15);
  215. $p = 2;
  216. } elseif ($p == 2) {
  217. if ($unicodeNullByteToggle) {
  218. $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2));
  219. $unicodeNullByteToggle = 0;
  220. } else {
  221. $unicodeNullByteToggle = 1;
  222. }
  223. $t = ($B64Values[$c] & 3);
  224. $p = 3;
  225. } elseif ($p == 3) {
  226. if ($unicodeNullByteToggle) {
  227. $d = $d . chr(($t << 6) + $B64Values[$c]);
  228. $unicodeNullByteToggle = 0;
  229. } else {
  230. $unicodeNullByteToggle = 1;
  231. }
  232. $t = ($B64Values[$c] & 3);
  233. $p = 0;
  234. }
  235. }
  236. return $d;
  237. }