imap_utf7_local.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * imap_general.php
  4. *
  5. * Copyright (c) 1999-2003 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This implements all functions that do general imap functions.
  9. *
  10. * $Id $
  11. */
  12. function imap_utf7_encode_local($s) {
  13. global $languages, $squirrelmail_language;
  14. if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
  15. function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
  16. return $languages[$squirrelmail_language]['XTRA_CODE']('utf7-imap_encode', $s);
  17. }
  18. $b64_s = ''; // buffer for substring to be base64-encoded
  19. $utf7_s = ''; // imap-utf7-encoded string
  20. for ($i = 0; $i < strlen($s); $i++) {
  21. $c = $s[$i];
  22. $ord_c = ord($c);
  23. if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or
  24. (($ord_c >= 0x27) and ($ord_c <= 0x7e))) {
  25. if ($b64_s) {
  26. $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-';
  27. $b64_s = '';
  28. }
  29. $utf7_s = $utf7_s . $c;
  30. } elseif ($ord_c == 0x26) {
  31. if ($b64_s) {
  32. $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
  33. $b64_s = '';
  34. }
  35. $utf7_s = $utf7_s . '&-';
  36. } else {
  37. $b64_s = $b64_s . chr(0) . $c;
  38. }
  39. }
  40. //
  41. // flush buffer
  42. //
  43. if ($b64_s) {
  44. $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
  45. $b64_s = '';
  46. }
  47. return $utf7_s;
  48. }
  49. function imap_utf7_decode_local($s) {
  50. global $languages, $squirrelmail_language;
  51. if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
  52. function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
  53. return $languages[$squirrelmail_language]['XTRA_CODE']('utf7-imap_decode', $s);
  54. }
  55. $b64_s = '';
  56. $iso_8859_1_s = '';
  57. for ($i = 0, $len = strlen($s); $i < $len; $i++) {
  58. $c = $s[$i];
  59. if (strlen($b64_s) > 0) {
  60. if ($c == '-') {
  61. if ($b64_s == '&') {
  62. $iso_8859_1_s = $iso_8859_1_s . '&';
  63. } else {
  64. $iso_8859_1_s = $iso_8859_1_s .
  65. decodeBASE64(substr($b64_s, 1));
  66. }
  67. $b64_s = '';
  68. } else {
  69. $b64_s = $b64_s . $c;
  70. }
  71. } else {
  72. if ($c == '&') {
  73. $b64_s = '&';
  74. } else {
  75. $iso_8859_1_s = $iso_8859_1_s . $c;
  76. }
  77. }
  78. }
  79. return $iso_8859_1_s;
  80. }
  81. function encodeBASE64($s) {
  82. $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,';
  83. $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3...
  84. $e = ''; // base64-encoded string
  85. //foreach($s as $c) {
  86. for ($i = 0; $i < strlen($s); $i++) {
  87. $c = $s[$i];
  88. if ($p == 0) {
  89. $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1);
  90. $t = (ord($c) & 3);
  91. $p = 1;
  92. } elseif ($p == 1) {
  93. $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)];
  94. $t = (ord($c) & 15);
  95. $p = 2;
  96. } elseif ($p == 2) {
  97. $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)];
  98. $e = $e . $B64Chars[ord($c) & 63];
  99. $p = 0;
  100. }
  101. }
  102. //
  103. // flush buffer
  104. //
  105. if ($p == 1) {
  106. $e = $e . $B64Chars[$t << 4];
  107. } elseif ($p == 2) {
  108. $e = $e . $B64Chars[$t << 2];
  109. }
  110. return $e;
  111. }
  112. function decodeBASE64($s) {
  113. $B64Values = array(
  114. 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5,
  115. 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11,
  116. 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17,
  117. 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23,
  118. 'Y' => 24, 'Z' => 25,
  119. 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31,
  120. 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37,
  121. 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43,
  122. 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49,
  123. 'y' => 50, 'z' => 51,
  124. '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57,
  125. '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63
  126. );
  127. $p = 0;
  128. $d = '';
  129. $unicodeNullByteToggle = 0;
  130. for ($i = 0, $len = strlen($s); $i < $len; $i++) {
  131. $c = $s[$i];
  132. if ($p == 0) {
  133. $t = $B64Values[$c];
  134. $p = 1;
  135. } elseif ($p == 1) {
  136. if ($unicodeNullByteToggle) {
  137. $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4));
  138. $unicodeNullByteToggle = 0;
  139. } else {
  140. $unicodeNullByteToggle = 1;
  141. }
  142. $t = ($B64Values[$c] & 15);
  143. $p = 2;
  144. } elseif ($p == 2) {
  145. if ($unicodeNullByteToggle) {
  146. $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2));
  147. $unicodeNullByteToggle = 0;
  148. } else {
  149. $unicodeNullByteToggle = 1;
  150. }
  151. $t = ($B64Values[$c] & 3);
  152. $p = 3;
  153. } elseif ($p == 3) {
  154. if ($unicodeNullByteToggle) {
  155. $d = $d . chr(($t << 6) + $B64Values[$c]);
  156. $unicodeNullByteToggle = 0;
  157. } else {
  158. $unicodeNullByteToggle = 1;
  159. }
  160. $t = ($B64Values[$c] & 3);
  161. $p = 0;
  162. }
  163. }
  164. return $d;
  165. }
  166. ?>