Source for file imap_utf7_local.php

Documentation is available at imap_utf7_local.php

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

Documentation generated on Sat, 07 Oct 2006 16:31:50 +0300 by phpDocumentor 1.3.0RC6