Displaying and fixing some XHTML
[squirrelmail.git] / functions / i18n.php
index 8937fbffbd7e6215ce9713ed9f1f184df57f5fdc..1ce3c5b3340caf2aa3cb368d1e97a39535363c0e 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 
 /**
- * functions/i18n.php
+ * SquirrelMail internationalization functions
  *
  * Copyright (c) 1999-2004 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
@@ -12,8 +12,9 @@
  * Internally the output character set is used. Other characters are
  * encoded using Unicode entities according to HTML 4.0.
  *
- * $Id$
+ * @version $Id$
  * @package squirrelmail
+ * @subpackage i18n
  */
 
 /** Everything uses global.php... */
@@ -48,7 +49,7 @@ function charset_decode ($charset, $string) {
 
     // Variables that allow to use functions without function_exist() calls
     if (! isset($use_php_recode) || $use_php_recode=="" ) {
-            $use_php_recode=false; }
+      $use_php_recode=false; }
     if (! isset($use_php_iconv) || $use_php_iconv=="" ) {
          $use_php_iconv=false; }
 
@@ -67,15 +68,15 @@ function charset_decode ($charset, $string) {
      */
     if ( $use_php_recode ) {
       if ( $default_charset == "utf-8" ) {
-       // other charsets can be converted to utf-8 without loss.
-       // and output string is smaller
-       $string = recode_string($charset . "..utf-8",$string);
-       return htmlspecialchars($string);
+        // other charsets can be converted to utf-8 without loss.
+        // and output string is smaller
+        $string = recode_string($charset . "..utf-8",$string);
+        return htmlspecialchars($string);
       } else {
-       $string = recode_string($charset . "..html",$string);
-       // recode does not convert single quote, htmlspecialchars does.
-       $string = str_replace("'", '&#039;', $string);
-       return $string;
+        $string = recode_string($charset . "..html",$string);
+        // recode does not convert single quote, htmlspecialchars does.
+        $string = str_replace("'", '&#039;', $string);
+        return $string;
       }
     }
 
@@ -98,14 +99,57 @@ function charset_decode ($charset, $string) {
     $decode=fixcharset($charset);
     $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php';
     if (file_exists($decodefile)) {
-       include_once($decodefile);
-       $ret = call_user_func('charset_decode_'.$decode, $string);
+      include_once($decodefile);
+      $ret = call_user_func('charset_decode_'.$decode, $string);
     } else {
-       $ret = $string;
+      $ret = $string;
     }
     return( $ret );
 }
-                                                                                                                                          
+
+/**
+ * Converts html string to given charset
+ * @param string $string
+ * @param string $charset
+ * @param boolean $htmlencode keep htmlspecialchars encoding
+ * @param string 
+ */
+function charset_encode($string,$charset,$htmlencode=true) {
+  global $default_charset;
+
+  // Undo html special chars
+  if (! $htmlencode ) {
+     $string = str_replace(array('&amp;','&gt;','&lt;','&quot;'),array('&','>','<','"'),$string);
+  }
+
+  $encode=fixcharset($charset);
+  $encodefile=SM_PATH . 'functions/encode/' . $encode . '.php';
+  if (file_exists($encodefile)) {
+    include_once($encodefile);
+    $ret = call_user_func('charset_encode_'.$encode, $string);
+  } else {
+    $ret = $string;
+  }
+  return( $ret );
+}
+
+/**
+ * Combined decoding and encoding functions
+ *
+ * If conversion is done to charset different that utf-8, unsupported symbols
+ * will be replaced with question marks.
+ * @param string $in_charset initial charset
+ * @param string $string string that has to be converted
+ * @param string $out_charset final charset
+ * @param boolean $htmlencode keep htmlspecialchars encoding
+ * @return string converted string
+ */
+function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) {
+  $string=charset_decode($in_charset,$string);
+  $string=charset_encode($string,$out_charset,$htmlencode);
+  return $string;
+}
+
 /**
  * Makes charset name suitable for decoding cycles
  *
@@ -188,51 +232,63 @@ function set_up_language($sm_language, $do_search = false, $default = false) {
          isset($languages[$sm_notAlias]['CHARSET']) ) {
         bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
         textdomain( 'squirrelmail' );
-       if (function_exists('bind_textdomain_codeset')) {
-            if ($sm_notAlias == 'ja_JP') {
-               bind_textdomain_codeset ("squirrelmail", 'EUC-JP');
+        if (function_exists('bind_textdomain_codeset')) {
+          if ($sm_notAlias == 'ja_JP') {
+            bind_textdomain_codeset ("squirrelmail", 'EUC-JP');
             } else {
-               bind_textdomain_codeset ("squirrelmail", $languages[$sm_notAlias]['CHARSET'] );
-           }
-       }
-       if (isset($languages[$sm_notAlias]['LOCALE'])){
-         $longlocale=$languages[$sm_notAlias]['LOCALE'];
-       } else {
-         $longlocale=$sm_notAlias;
-       }
+              bind_textdomain_codeset ("squirrelmail", $languages[$sm_notAlias]['CHARSET'] );
+            }
+        }
+        if (isset($languages[$sm_notAlias]['LOCALE'])){
+          $longlocale=$languages[$sm_notAlias]['LOCALE'];
+        } else {
+          $longlocale=$sm_notAlias;
+        }
         if ( !ini_get('safe_mode') &&
              getenv( 'LC_ALL' ) != $longlocale ) {
             putenv( "LC_ALL=$longlocale" );
             putenv( "LANG=$longlocale" );
             putenv( "LANGUAGE=$longlocale" );
         }
-       setlocale(LC_ALL, $longlocale);
-
-       // Set text direction/alignment variables
-       if (isset($languages[$sm_notAlias]['DIR']) && 
-               $languages[$sm_notAlias]['DIR'] == 'rtl') {
-           $text_direction='rtl';
-           $left_align='right';
-           $right_align='left';
-       } else {
-           $text_direction='ltr';
-           $left_align='left';
-           $right_align='right';
-       }
-
-       $squirrelmail_language = $sm_notAlias;
+        setlocale(LC_ALL, $longlocale);
+
+        // Set text direction/alignment variables
+        if (isset($languages[$sm_notAlias]['DIR']) && 
+            $languages[$sm_notAlias]['DIR'] == 'rtl') {
+          /**
+           * Text direction
+           * @global string $text_direction
+           */
+          $text_direction='rtl';
+          /**
+           * Left alignment
+           * @global string $left_align
+           */
+          $left_align='right';
+          /**
+           * Right alignment
+           * @global string $right_align
+           */
+          $right_align='left';
+        } else {
+          $text_direction='ltr';
+          $left_align='left';
+          $right_align='right';
+        }
+
+        $squirrelmail_language = $sm_notAlias;
         if ($squirrelmail_language == 'ja_JP') {
             header ('Content-Type: text/html; charset=EUC-JP');
             if (!function_exists('mb_internal_encoding')) {
-               // Error messages can't be displayed here
-               $error = 1;
-               // Revert to English if possible.
-               if (function_exists('setPref')  && $username!='' && $data_dir!="") {
-                   setPref($data_dir, $username, 'language', "en_US");
-                   $error = 2;         
-               }
-               // stop further execution in order not to get php errors on mb_internal_encoding().
-               return $error;
+              // Error messages can't be displayed here
+              $error = 1;
+              // Revert to English if possible.
+              if (function_exists('setPref')  && $username!='' && $data_dir!="") {
+                setPref($data_dir, $username, 'language', "en_US");
+                $error = 2;
+              }
+              // stop further execution in order not to get php errors on mb_internal_encoding().
+              return $error;
             }
             if (function_exists('mb_language')) {
                 mb_language('Japanese');
@@ -307,7 +363,7 @@ if (! isset($squirrelmail_language)) {
  * Each 'language' definition requires NAME+CHARSET or ALIAS variables.
  *
  * @name $languages
- * @global $languages
+ * @global array $languages
  */
 $languages['bg_BG']['NAME']    = 'Bulgarian';
 $languages['bg_BG']['ALTNAME'] = '&#1041;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080;';
@@ -371,6 +427,11 @@ $languages['et_EE']['CHARSET'] = 'iso-8859-15';
 $languages['et_EE']['LOCALE']  = 'et_EE.ISO8859-15';
 $languages['et']['ALIAS'] = 'et_EE';
 
+$languages['eu_ES']['NAME']    = 'Basque';
+$languages['eu_ES']['CHARSET'] = 'iso-8859-1';
+$languages['eu_ES']['LOCALE']  = 'eu_ES.ISO8859-1';
+$languages['eu']['ALIAS'] = 'eu_ES';
+
 $languages['fo_FO']['NAME']    = 'Faroese';
 $languages['fo_FO']['CHARSET'] = 'iso-8859-1';
 $languages['fo_FO']['LOCALE']  = 'fo_FO.ISO8859-1';
@@ -426,7 +487,8 @@ $languages['ja']['ALIAS'] = 'ja_JP';
 $languages['ko_KR']['NAME']    = 'Korean';
 $languages['ko_KR']['CHARSET'] = 'euc-KR';
 $languages['ko_KR']['LOCALE']  = 'ko_KR.EUC-KR';
-$languages['ko_KR']['XTRA_CODE'] = 'korean_charset_xtra';
+// Function does not provide all needed options
+// $languages['ko_KR']['XTRA_CODE'] = 'korean_charset_xtra';
 $languages['ko']['ALIAS'] = 'ko_KR';
 
 $languages['lt_LT']['NAME']    = 'Lithuanian';
@@ -589,16 +651,28 @@ elseif ($gettext_flags == 0) {
 } else {
     /* Uh-ho.  A weird install */
     if (! $gettext_flags & 1) {
+      /**
+       * Function is used as replacement in broken installs
+       * @ignore
+       */
         function _($str) {
             return $str;
         }
     }
     if (! $gettext_flags & 2) {
+      /**
+       * Function is used as replacement in broken installs
+       * @ignore
+       */
         function bindtextdomain() {
             return;
         }
     }
     if (! $gettext_flags & 4) {
+      /**
+       * Function is used as replacemet in broken installs
+       * @ignore
+       */
         function textdomain() {
             return;
         }
@@ -613,18 +687,18 @@ elseif ($gettext_flags == 0) {
  * Default return value is defined by second argument.
  * Use of third argument depends on action.
  *
- * @param string action performed by this function. 
+ * @param string $action action performed by this function.
  *    possible values:
- *     decode - convert returned string to euc-jp. third argument unused
- *     encode - convert returned string to jis. third argument unused
- *     strimwidth - third argument=$width. trims string to $width symbols.
- *     encodeheader - create base64 encoded header in iso-2022-jp. third argument unused
- *     decodeheader - return human readable string from mime header. string is returned in euc-jp. third argument unused
- *     downloadfilename - third argument $useragent. Arguments provide browser info. Returns shift-jis or euc-jp encoded file name
- *     wordwrap - third argument=$wrap. wraps text at $wrap symbols
- *     utf7-imap_encode - returns string converted from euc-jp to utf7-imap. third argument unused
- *     utf7-imap_decode - returns string converted from utf7-imap to euc-jp. third argument unused
- * @param string default return value
+ *  decode - convert returned string to euc-jp. third argument unused
+ *  encode - convert returned string to jis. third argument unused
+ *  strimwidth - third argument=$width. trims string to $width symbols.
+ *  encodeheader - create base64 encoded header in iso-2022-jp. third argument unused
+ *  decodeheader - return human readable string from mime header. string is returned in euc-jp. third argument unused
+ *  downloadfilename - third argument $useragent. Arguments provide browser info. Returns shift-jis or euc-jp encoded file name
+ *  wordwrap - third argument=$wrap. wraps text at $wrap symbols
+ *  utf7-imap_encode - returns string converted from euc-jp to utf7-imap. third argument unused
+ *  utf7-imap_decode - returns string converted from utf7-imap to euc-jp. third argument unused
+ * @param string $ret default return value
  */
 function japanese_charset_xtra() {
     $ret = func_get_arg(1);  /* default return value */
@@ -761,7 +835,7 @@ function japanese_charset_xtra() {
  *
  * @param string action performed by this function. 
  *    possible values:
- *     downloadfilename - Hangul(Korean Character) Attached File Name Fix.
+ * downloadfilename - Hangul(Korean Character) Attached File Name Fix.
  * @param string default return value
  */
 function korean_charset_xtra() {
@@ -859,30 +933,30 @@ function is_conversion_safe($input_charset) {
 switch ($default_charset):
 case "windows-1251":
       if ( $input_charset == "iso-8859-5" || 
-          $input_charset == "koi8-r" ||
-          $input_charset == "koi8-u" ) {
+           $input_charset == "koi8-r" ||
+           $input_charset == "koi8-u" ) {
         return true;
      } else {
         return false;
      }
 case "windows-1257":
   if ( $input_charset == "iso-8859-13" || 
-        $input_charset == "iso-8859-4" ) {
+       $input_charset == "iso-8859-4" ) {
     return true;
   } else {
     return false;
   }
 case "iso-8859-4":
   if ( $input_charset == "iso-8859-13" || 
-        $input_charset == "windows-1257" ) {
+       $input_charset == "windows-1257" ) {
      return true;
   } else {
      return false;
   }
 case "iso-8859-5":
   if ( $input_charset == "windows-1251" || 
-        $input_charset == "koi8-r" || 
-        $input_charset == "koi8-u" ) {
+       $input_charset == "koi8-r" || 
+       $input_charset == "koi8-u" ) {
      return true;
   } else {
      return false;
@@ -896,16 +970,16 @@ case "iso-8859-13":
   }
 case "koi8-r":
   if ( $input_charset == "windows-1251" ||
-        $input_charset == "iso-8859-5" || 
-        $input_charset == "koi8-u" ) {
+       $input_charset == "iso-8859-5" || 
+       $input_charset == "koi8-u" ) {
      return true;
   } else {
      return false;
   }
 case "koi8-u":
   if ( $input_charset == "windows-1251" ||
-       $input_charset == "iso-8859-5" ||
-       $input_charset == "koi8-r" ) {
+       $input_charset == "iso-8859-5" ||
+       $input_charset == "koi8-r" ) {
      return true;
   } else {
      return false;