Documentation TYPO3 par Ameos

charset_conversion.lib.php

00001 <?php
00002 /* $Id: charset_conversion.lib.php,v 2.3 2004/01/06 21:49:42 rabus 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         return $what;
00144     }
00145     else if (is_array($what)) {
00146         $result = array();
00147         foreach($what AS $key => $val) {
00148             if (is_string($val) || is_array($val)) {
00149                 if (is_string($key)) {
00150                     $result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
00151                 } else {
00152                     $result[$key] = PMA_convert_display_charset($val);
00153                 }
00154             } else {
00155                 $result[$key]     = $val;
00156             }
00157         } // end while
00158         return $result;
00159     }
00160     else if (is_string($what)) {
00161         switch ($GLOBALS['PMA_recoding_engine']) {
00162             case PMA_CHARSET_RECODE:
00163                 return recode_string($convcharset . '..'  . $charset, $what);
00164             case PMA_CHARSET_ICONV:
00165                 return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
00166             case PMA_CHARSET_LIBICONV:
00167                 return libiconv($convcharset, $charset, $what);
00168             default:
00169                 return $what;
00170         }
00171     }
00172     else if (is_object($what)) {
00173         // isn't it object returned from mysql_fetch_field ?
00174         if (@is_string($what->name)) {
00175             $what->name = PMA_convert_display_charset($what->name);
00176         }
00177         if (@is_string($what->table)) {
00178             $what->table = PMA_convert_display_charset($what->table);
00179         }
00180         if (@is_string($what->Database)) {
00181             $what->Database = PMA_convert_display_charset($what->Database);
00182         }
00183         return $what;
00184     }
00185     else {
00186         // when we don't know what it is we don't touch it...
00187         return $what;
00188     }
00189 } //  end of the "PMA_convert_display_charset()" function
00190 
00191 
00208 function PMA_convert_charset($what) {
00209     global $cfg, $allow_recoding, $charset, $convcharset;
00210 
00211     if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
00212         || $convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
00213         return $what;
00214     } else {
00215         switch ($GLOBALS['PMA_recoding_engine']) {
00216             case PMA_CHARSET_RECODE:
00217                 return recode_string($charset . '..'  . $convcharset, $what);
00218             case PMA_CHARSET_ICONV:
00219                 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
00220             case PMA_CHARSET_LIBICONV:
00221                 return libiconv($charset, $convcharset, $what);
00222             default:
00223                 return $what;
00224         }
00225     }
00226 } //  end of the "PMA_convert_charset()" function
00227 
00242 function PMA_convert_string($src_charset, $dest_charset, $what) {
00243     switch ($GLOBALS['PMA_recoding_engine']) {
00244         case PMA_CHARSET_RECODE:
00245             return recode_string($src_charset . '..'  . $dest_charset, $what);
00246         case PMA_CHARSET_ICONV:
00247             return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
00248         case PMA_CHARSET_LIBICONV:
00249             return libiconv($src_charset, $dest_charset, $what);
00250         default:
00251             return $what;
00252     }
00253 } //  end of the "PMA_convert_string()" function
00254 
00255 
00271 function PMA_convert_file($src_charset, $dest_charset, $file) {
00272     switch ($GLOBALS['PMA_recoding_engine']) {
00273         case PMA_CHARSET_RECODE:
00274         case PMA_CHARSET_ICONV:
00275         case PMA_CHARSET_LIBICONV:
00276             $tmpfname = tempnam('', 'PMA_convert_file');
00277             $fin      = fopen($file, 'r');
00278             $fout     = fopen($tmpfname, 'w');
00279             if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE) {
00280                 recode_file($src_charset . '..'  . $dest_charset, $fin, $fout);
00281             } else {
00282                 while (!feof($fin)) {
00283                     $line = fgets($fin, 4096);
00284                     if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV) {
00285                         $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
00286                     } else {
00287                         $dist = libiconv($src_charset, $dest_charset, $line);
00288                     }
00289                     fputs($fout, $dist);
00290                 } // end while
00291             }
00292             fclose($fin);
00293             fclose($fout);
00294             unlink($file);
00295 
00296             return $tmpfname;
00297         default:
00298             return $file;
00299     }
00300 } //  end of the "PMA_convert_file()" function
00301 
00302 ?>


Généré par Les experts TYPO3 avec  doxygen 1.4.6