Documentation TYPO3 par Ameos

charset_conversion.lib.php

00001 <?php
00002 /* $Id: charset_conversion.lib.php,v 2.6 2005/05/22 11:37:12 lem9 Exp $ */
00003 // vim: expandtab sw=4 ts=4 sts=4:
00004 
00005 
00014 if (isset($cfg['AllowAnywhereRecoding'])
00015     && $cfg['AllowAnywhereRecoding']
00016     && $allow_recoding) {
00017 
00018     if ($cfg['RecodingEngine'] == 'recode') {
00019         if (!@extension_loaded('recode')) {
00020             PMA_dl('recode');
00021             if (!@extension_loaded('recode')) {
00022                 echo $strCantLoadRecodeIconv;
00023                 exit;
00024             }
00025         }
00026         $PMA_recoding_engine             = 'recode';
00027     } else if ($cfg['RecodingEngine'] == 'iconv') {
00028         if (!@extension_loaded('iconv')) {
00029             PMA_dl('iconv');
00030             if (!@extension_loaded('iconv')) {
00031                 echo $strCantLoadRecodeIconv;
00032                 exit;
00033             }
00034         }
00035         $PMA_recoding_engine             = 'iconv';
00036     } else {
00037         if (@extension_loaded('iconv')) {
00038             $PMA_recoding_engine         = 'iconv';
00039         } else if (@extension_loaded('recode')) {
00040             $PMA_recoding_engine         = 'recode';
00041         } else {
00042             PMA_dl('iconv');
00043             if (!@extension_loaded('iconv')) {
00044                 PMA_dl('recode');
00045                 if (!@extension_loaded('recode')) {
00046                     echo $strCantLoadRecodeIconv;
00047                     exit;
00048                 } else {
00049                     $PMA_recoding_engine = 'recode';
00050                 }
00051             } else {
00052                 $PMA_recoding_engine     = 'iconv';
00053             }
00054         }
00055     }
00056 } // end load recode/iconv extension
00057 
00058 define('PMA_CHARSET_NONE', 0);
00059 define('PMA_CHARSET_ICONV', 1);
00060 define('PMA_CHARSET_LIBICONV', 2);
00061 define('PMA_CHARSET_RECODE', 3);
00062 
00063 if (!isset($cfg['IconvExtraParams'])) {
00064     $cfg['IconvExtraParams'] = '';
00065 }
00066 
00067 // Finally detects which function will we use:
00068 if (isset($cfg['AllowAnywhereRecoding'])
00069     && $cfg['AllowAnywhereRecoding']
00070     && $allow_recoding) {
00071 
00072     if (!isset($PMA_recoding_engine)) {
00073         $PMA_recoding_engine = $cfg['RecodingEngine'];
00074     }
00075     if ($PMA_recoding_engine == 'iconv') {
00076         if (@function_exists('iconv')) {
00077             $PMA_recoding_engine = PMA_CHARSET_ICONV;
00078         } else if (@function_exists('libiconv')) {
00079             $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
00080         } else {
00081             $PMA_recoding_engine = PMA_CHARSET_NONE;
00082 
00083             if (!isset($GLOBALS['is_header_sent'])) {
00084                 include('./header.inc.php');
00085             }
00086             echo $strCantUseRecodeIconv;
00087             require_once('./footer.inc.php');
00088             exit();
00089         }
00090     } else if ($PMA_recoding_engine == 'recode') {
00091         if (@function_exists('recode_string')) {
00092             $PMA_recoding_engine = PMA_CHARSET_RECODE;
00093         } else {
00094             $PMA_recoding_engine = PMA_CHARSET_NONE;
00095 
00096             require_once('./header.inc.php');
00097             echo $strCantUseRecodeIconv;
00098             require_once('./footer.inc.php');
00099             exit;
00100         }
00101     } else {
00102         if (@function_exists('iconv')) {
00103             $PMA_recoding_engine = PMA_CHARSET_ICONV;
00104         } else if (@function_exists('libiconv')) {
00105             $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
00106         } elseif (@function_exists('recode_string')) {
00107             $PMA_recoding_engine = PMA_CHARSET_RECODE;
00108         } else {
00109             $PMA_recoding_engine = PMA_CHARSET_NONE;
00110 
00111             require_once('./header.inc.php');
00112             echo $strCantUseRecodeIconv;
00113             require_once('./footer.inc.php');
00114             exit;
00115         }
00116     }
00117 } else {
00118     $PMA_recoding_engine         = PMA_CHARSET_NONE;
00119 }
00120 
00121 
00138 function PMA_convert_display_charset($what) {
00139     global $cfg, $allow_recoding, $charset, $convcharset;
00140 
00141     if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
00142         || $convcharset == $charset // rabus: if input and output charset are the same, we don't have to do anything...
00143         || PMA_MYSQL_INT_VERSION >= 40100 ) {  // lem9: even if AllowAnywhereRecoding is TRUE, do not recode for MySQL >= 4.1.x since MySQL does the job
00144         return $what;
00145     }
00146     else if (is_array($what)) {
00147         $result = array();
00148         foreach ($what AS $key => $val) {
00149             if (is_string($val) || is_array($val)) {
00150                 if (is_string($key)) {
00151                     $result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
00152                 } else {
00153                     $result[$key] = PMA_convert_display_charset($val);
00154                 }
00155             } else {
00156                 $result[$key]     = $val;
00157             }
00158         } // end while
00159         return $result;
00160     }
00161     else if (is_string($what)) {
00162 
00163         switch ($GLOBALS['PMA_recoding_engine']) {
00164             case PMA_CHARSET_RECODE:
00165                 return recode_string($convcharset . '..'  . $charset, $what);
00166             case PMA_CHARSET_ICONV:
00167                 return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
00168             case PMA_CHARSET_LIBICONV:
00169                 return libiconv($convcharset, $charset, $what);
00170             default:
00171                 return $what;
00172         }
00173     }
00174     else if (is_object($what)) {
00175         // isn't it object returned from mysql_fetch_field ?
00176         if (@is_string($what->name)) {
00177             $what->name = PMA_convert_display_charset($what->name);
00178         }
00179         if (@is_string($what->table)) {
00180             $what->table = PMA_convert_display_charset($what->table);
00181         }
00182         if (@is_string($what->Database)) {
00183             $what->Database = PMA_convert_display_charset($what->Database);
00184         }
00185         return $what;
00186     }
00187     else {
00188         // when we don't know what it is we don't touch it...
00189         return $what;
00190     }
00191 } //  end of the "PMA_convert_display_charset()" function
00192 
00193 
00210 function PMA_convert_charset($what) {
00211     global $cfg, $allow_recoding, $charset, $convcharset;
00212 
00213     if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
00214         || $convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
00215         return $what;
00216     } else {
00217         switch ($GLOBALS['PMA_recoding_engine']) {
00218             case PMA_CHARSET_RECODE:
00219                 return recode_string($charset . '..'  . $convcharset, $what);
00220             case PMA_CHARSET_ICONV:
00221                 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
00222             case PMA_CHARSET_LIBICONV:
00223                 return libiconv($charset, $convcharset, $what);
00224             default:
00225                 return $what;
00226         }
00227     }
00228 } //  end of the "PMA_convert_charset()" function
00229 
00244 function PMA_convert_string($src_charset, $dest_charset, $what) {
00245     if ($src_charset == $dest_charset) return $what;
00246     switch ($GLOBALS['PMA_recoding_engine']) {
00247         case PMA_CHARSET_RECODE:
00248             return recode_string($src_charset . '..'  . $dest_charset, $what);
00249         case PMA_CHARSET_ICONV:
00250             return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
00251         case PMA_CHARSET_LIBICONV:
00252             return libiconv($src_charset, $dest_charset, $what);
00253         default:
00254             return $what;
00255     }
00256 } //  end of the "PMA_convert_string()" function
00257 
00258 
00274 function PMA_convert_file($src_charset, $dest_charset, $file) {
00275     switch ($GLOBALS['PMA_recoding_engine']) {
00276         case PMA_CHARSET_RECODE:
00277         case PMA_CHARSET_ICONV:
00278         case PMA_CHARSET_LIBICONV:
00279             $tmpfname = tempnam('', 'PMA_convert_file');
00280             $fin      = fopen($file, 'r');
00281             $fout     = fopen($tmpfname, 'w');
00282             if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE) {
00283                 recode_file($src_charset . '..'  . $dest_charset, $fin, $fout);
00284             } else {
00285                 while (!feof($fin)) {
00286                     $line = fgets($fin, 4096);
00287                     if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV) {
00288                         $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
00289                     } else {
00290                         $dist = libiconv($src_charset, $dest_charset, $line);
00291                     }
00292                     fputs($fout, $dist);
00293                 } // end while
00294             }
00295             fclose($fin);
00296             fclose($fout);
00297             unlink($file);
00298 
00299             return $tmpfname;
00300         default:
00301             return $file;
00302     }
00303 } //  end of the "PMA_convert_file()" function
00304 
00305 ?>


Généré par Le spécialiste TYPO3 avec  doxygen 1.4.6