[ Index ]
 

Code source de phpMyAdmin 2.10.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/libraries/ -> charset_conversion.lib.php (source)

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


Généré le : Mon Nov 26 15:18:20 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics