home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / libraries / charset_conversion.lib.php < prev    next >
Encoding:
PHP Script  |  2008-06-23  |  11.5 KB  |  327 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  * Charset conversion functions.
  5.  *
  6.  * @version $Id: charset_conversion.lib.php 11326 2008-06-17 21:32:48Z lem9 $
  7.  */
  8. if (! defined('PHPMYADMIN')) {
  9.     exit;
  10. }
  11.  
  12. /**
  13.  * Loads the recode or iconv extensions if any of it is not loaded yet
  14.  */
  15. if (isset($cfg['AllowAnywhereRecoding'])
  16.     && $cfg['AllowAnywhereRecoding']
  17.     && $allow_recoding) {
  18.  
  19.     if ($cfg['RecodingEngine'] == 'recode') {
  20.         if (!@extension_loaded('recode')) {
  21.             PMA_dl('recode');
  22.             if (!@extension_loaded('recode')) {
  23.                 echo $strCantLoadRecodeIconv;
  24.                 exit;
  25.             }
  26.         }
  27.         $PMA_recoding_engine             = 'recode';
  28.     } elseif ($cfg['RecodingEngine'] == 'iconv') {
  29.         if (!@extension_loaded('iconv')) {
  30.             PMA_dl('iconv');
  31.             if (!@extension_loaded('iconv')) {
  32.                 echo $strCantLoadRecodeIconv;
  33.                 exit;
  34.             }
  35.         }
  36.         $PMA_recoding_engine             = 'iconv';
  37.     } else {
  38.         if (@extension_loaded('iconv')) {
  39.             $PMA_recoding_engine         = 'iconv';
  40.         } elseif (@extension_loaded('recode')) {
  41.             $PMA_recoding_engine         = 'recode';
  42.         } else {
  43.             PMA_dl('iconv');
  44.             if (!@extension_loaded('iconv')) {
  45.                 PMA_dl('recode');
  46.                 if (!@extension_loaded('recode')) {
  47.                     echo $strCantLoadRecodeIconv;
  48.                     exit;
  49.                 } else {
  50.                     $PMA_recoding_engine = 'recode';
  51.                 }
  52.             } else {
  53.                 $PMA_recoding_engine     = 'iconv';
  54.             }
  55.         }
  56.     }
  57. } // end load recode/iconv extension
  58.  
  59. define('PMA_CHARSET_NONE', 0);
  60. define('PMA_CHARSET_ICONV', 1);
  61. define('PMA_CHARSET_LIBICONV', 2);
  62. define('PMA_CHARSET_RECODE', 3);
  63. define('PMA_CHARSET_ICONV_AIX', 4);
  64.  
  65. if (!isset($cfg['IconvExtraParams'])) {
  66.     $cfg['IconvExtraParams'] = '';
  67. }
  68.  
  69. // Finally detects which function will we use:
  70. if (isset($cfg['AllowAnywhereRecoding'])
  71.     && $cfg['AllowAnywhereRecoding']
  72.     && $allow_recoding) {
  73.  
  74.     if (!isset($PMA_recoding_engine)) {
  75.         $PMA_recoding_engine = $cfg['RecodingEngine'];
  76.     }
  77.     if ($PMA_recoding_engine == 'iconv') {
  78.         if (@function_exists('iconv')) {
  79.             if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
  80.                 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
  81.             } else {
  82.                 $PMA_recoding_engine = PMA_CHARSET_ICONV;
  83.             }
  84.         } elseif (@function_exists('libiconv')) {
  85.             $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
  86.         } else {
  87.             $PMA_recoding_engine = PMA_CHARSET_NONE;
  88.  
  89.             if (!isset($GLOBALS['is_header_sent'])) {
  90.                 include './libraries/header.inc.php';
  91.             }
  92.             echo $strCantUseRecodeIconv;
  93.             require_once './libraries/footer.inc.php';
  94.             exit();
  95.         }
  96.     } elseif ($PMA_recoding_engine == 'recode') {
  97.         if (@function_exists('recode_string')) {
  98.             $PMA_recoding_engine = PMA_CHARSET_RECODE;
  99.         } else {
  100.             $PMA_recoding_engine = PMA_CHARSET_NONE;
  101.  
  102.             require_once './libraries/header.inc.php';
  103.             echo $strCantUseRecodeIconv;
  104.             require_once './libraries/footer.inc.php';
  105.             exit;
  106.         }
  107.     } else {
  108.         if (@function_exists('iconv')) {
  109.             if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
  110.                 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
  111.             } else {
  112.                 $PMA_recoding_engine = PMA_CHARSET_ICONV;
  113.             }
  114.         } elseif (@function_exists('libiconv')) {
  115.             $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
  116.         } elseif (@function_exists('recode_string')) {
  117.             $PMA_recoding_engine = PMA_CHARSET_RECODE;
  118.         } else {
  119.             $PMA_recoding_engine = PMA_CHARSET_NONE;
  120.  
  121.             require_once './libraries/header.inc.php';
  122.             echo $strCantUseRecodeIconv;
  123.             require_once './libraries/footer.inc.php';
  124.             exit;
  125.         }
  126.     }
  127. } else {
  128.     $PMA_recoding_engine         = PMA_CHARSET_NONE;
  129. }
  130.  
  131. /* Load AIX iconv wrapper if needed */
  132. if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX) {
  133.     require_once './libraries/iconv_wrapper.lib.php';
  134. }
  135.  
  136. /**
  137.  * Converts encoding according to current settings.
  138.  *
  139.  * @param   mixed    what to convert (string or array of strings or object returned by mysql_fetch_field)
  140.  *
  141.  * @return  string   converted string or array of strings
  142.  *
  143.  * @global  array    the configuration array
  144.  * @global  boolean  whether recoding is allowed or not
  145.  * @global  string   the current charset
  146.  * @global  array    the charset to convert to
  147.  *
  148.  * @access  public
  149.  *
  150.  * @author  nijel
  151.  */
  152. function PMA_convert_display_charset($what) {
  153.     global $cfg, $allow_recoding, $charset, $convcharset;
  154.  
  155.     if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
  156.         || $convcharset == $charset // rabus: if input and output charset are the same, we don't have to do anything...
  157.         // this constant is not defined before the login:
  158.         || (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100)) {  // lem9: even if AllowAnywhereRecoding is TRUE, do not recode for MySQL >= 4.1.x since MySQL does the job
  159.         return $what;
  160.     } elseif (is_array($what)) {
  161.         $result = array();
  162.         foreach ($what AS $key => $val) {
  163.             if (is_string($val) || is_array($val)) {
  164.                 if (is_string($key)) {
  165.                     $result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
  166.                 } else {
  167.                     $result[$key] = PMA_convert_display_charset($val);
  168.                 }
  169.             } else {
  170.                 $result[$key]     = $val;
  171.             }
  172.         } // end while
  173.         return $result;
  174.     } elseif (is_string($what)) {
  175.  
  176.         switch ($GLOBALS['PMA_recoding_engine']) {
  177.             case PMA_CHARSET_RECODE:
  178.                 return recode_string($convcharset . '..'  . $charset, $what);
  179.             case PMA_CHARSET_ICONV:
  180.                 return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
  181.             case PMA_CHARSET_ICONV_AIX:
  182.                 return PMA_aix_iconv_wrapper($convcharset, $charset . $cfg['IconvExtraParams'], $what);
  183.             case PMA_CHARSET_LIBICONV:
  184.                 return libiconv($convcharset, $charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
  185.             default:
  186.                 return $what;
  187.         }
  188.     } elseif (is_object($what)) {
  189.         // isn't it object returned from mysql_fetch_field ?
  190.         if (@is_string($what->name)) {
  191.             $what->name = PMA_convert_display_charset($what->name);
  192.         }
  193.         if (@is_string($what->table)) {
  194.             $what->table = PMA_convert_display_charset($what->table);
  195.         }
  196.         if (@is_string($what->Database)) {
  197.             $what->Database = PMA_convert_display_charset($what->Database);
  198.         }
  199.         return $what;
  200.     } else {
  201.         // when we don't know what it is we don't touch it...
  202.         return $what;
  203.     }
  204. } //  end of the "PMA_convert_display_charset()" function
  205.  
  206.  
  207. /**
  208.  * Converts encoding of text according to current settings.
  209.  *
  210.  * @param   string   what to convert
  211.  *
  212.  * @return  string   converted text
  213.  *
  214.  * @global  array    the configuration array
  215.  * @global  boolean  whether recoding is allowed or not
  216.  * @global  string   the current charset
  217.  * @global  array    the charset to convert to
  218.  *
  219.  * @access  public
  220.  *
  221.  * @author  nijel
  222.  */
  223. function PMA_convert_charset($what) {
  224.     global $cfg, $allow_recoding, $charset, $convcharset;
  225.  
  226.     if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
  227.         || $convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
  228.         return $what;
  229.     } else {
  230.         switch ($GLOBALS['PMA_recoding_engine']) {
  231.             case PMA_CHARSET_RECODE:
  232.                 return recode_string($charset . '..'  . $convcharset, $what);
  233.             case PMA_CHARSET_ICONV:
  234.                 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
  235.             case PMA_CHARSET_ICONV_AIX:
  236.                 return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
  237.             case PMA_CHARSET_LIBICONV:
  238.                 return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
  239.             default:
  240.                 return $what;
  241.         }
  242.     }
  243. } //  end of the "PMA_convert_charset()" function
  244.  
  245. /**
  246.  * Converts encoding of text according to parameters with detected
  247.  * conversion function.
  248.  *
  249.  * @param   string   source charset
  250.  * @param   string   target charset
  251.  * @param   string   what to convert
  252.  *
  253.  * @return  string   converted text
  254.  *
  255.  * @access  public
  256.  *
  257.  * @author  nijel
  258.  */
  259. function PMA_convert_string($src_charset, $dest_charset, $what) {
  260.     if ($src_charset == $dest_charset) {
  261.         return $what;
  262.     }
  263.     switch ($GLOBALS['PMA_recoding_engine']) {
  264.         case PMA_CHARSET_RECODE:
  265.             return recode_string($src_charset . '..'  . $dest_charset, $what);
  266.         case PMA_CHARSET_ICONV:
  267.             return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
  268.         case PMA_CHARSET_ICONV_AIX:
  269.             return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
  270.         case PMA_CHARSET_LIBICONV:
  271.             return libiconv($src_charset, $dest_charset, $what);
  272.         default:
  273.             return $what;
  274.     }
  275. } //  end of the "PMA_convert_string()" function
  276.  
  277.  
  278. /**
  279.  * Converts encoding of file according to parameters with detected
  280.  * conversion function. The old file will be unlinked and new created and
  281.  * its file name is returned.
  282.  *
  283.  * @param   string   source charset
  284.  * @param   string   target charset
  285.  * @param   string   file to convert
  286.  *
  287.  * @return  string   new temporay file
  288.  *
  289.  * @access  public
  290.  *
  291.  * @author  nijel
  292.  */
  293. function PMA_convert_file($src_charset, $dest_charset, $file) {
  294.     switch ($GLOBALS['PMA_recoding_engine']) {
  295.         case PMA_CHARSET_RECODE:
  296.         case PMA_CHARSET_ICONV:
  297.         case PMA_CHARSET_LIBICONV:
  298.             $tmpfname = tempnam('', 'PMA_convert_file');
  299.             $fin      = fopen($file, 'r');
  300.             $fout     = fopen($tmpfname, 'w');
  301.             if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE) {
  302.                 recode_file($src_charset . '..'  . $dest_charset, $fin, $fout);
  303.             } else {
  304.                 while (!feof($fin)) {
  305.                     $line = fgets($fin, 4096);
  306.                     if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV) {
  307.                         $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
  308.                     } elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX) {
  309.                         $dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
  310.                     } else {
  311.                         $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
  312.                     }
  313.                     fputs($fout, $dist);
  314.                 } // end while
  315.             }
  316.             fclose($fin);
  317.             fclose($fout);
  318.             unlink($file);
  319.  
  320.             return $tmpfname;
  321.         default:
  322.             return $file;
  323.     }
  324. } //  end of the "PMA_convert_file()" function
  325.  
  326. ?>
  327.