charset_conversion.lib.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Charset conversion functions.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. define('PMA_CHARSET_NONE', 0);
  12. define('PMA_CHARSET_ICONV', 1);
  13. define('PMA_CHARSET_RECODE', 2);
  14. define('PMA_CHARSET_ICONV_AIX', 3);
  15. define('PMA_CHARSET_MB', 4);
  16. if (! isset($GLOBALS['cfg']['RecodingEngine'])) {
  17. $GLOBALS['cfg']['RecodingEngine'] = '';
  18. }
  19. // Finally detect which function we will use:
  20. if ($GLOBALS['cfg']['RecodingEngine'] == 'iconv') {
  21. if (@function_exists('iconv')) {
  22. if ((@stristr(PHP_OS, 'AIX'))
  23. && (@strcasecmp(ICONV_IMPL, 'unknown') == 0)
  24. && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)
  25. ) {
  26. $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
  27. } else {
  28. $PMA_recoding_engine = PMA_CHARSET_ICONV;
  29. }
  30. } else {
  31. $PMA_recoding_engine = PMA_CHARSET_NONE;
  32. PMA_warnMissingExtension('iconv');
  33. }
  34. } elseif ($GLOBALS['cfg']['RecodingEngine'] == 'recode') {
  35. if (@function_exists('recode_string')) {
  36. $PMA_recoding_engine = PMA_CHARSET_RECODE;
  37. } else {
  38. $PMA_recoding_engine = PMA_CHARSET_NONE;
  39. PMA_warnMissingExtension('recode');
  40. }
  41. } elseif ($GLOBALS['cfg']['RecodingEngine'] == 'mb') {
  42. if (@function_exists('mb_convert_encoding')) {
  43. $PMA_recoding_engine = PMA_CHARSET_MB;
  44. } else {
  45. $PMA_recoding_engine = PMA_CHARSET_NONE;
  46. PMA_warnMissingExtension('mbstring');
  47. }
  48. } elseif ($GLOBALS['cfg']['RecodingEngine'] == 'auto') {
  49. if (@function_exists('iconv')) {
  50. if ((@stristr(PHP_OS, 'AIX'))
  51. && (@strcasecmp(ICONV_IMPL, 'unknown') == 0)
  52. && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)
  53. ) {
  54. $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
  55. } else {
  56. $PMA_recoding_engine = PMA_CHARSET_ICONV;
  57. }
  58. } elseif (@function_exists('recode_string')) {
  59. $PMA_recoding_engine = PMA_CHARSET_RECODE;
  60. } elseif (@function_exists('mb_convert_encoding')) {
  61. $PMA_recoding_engine = PMA_CHARSET_MB;
  62. } else {
  63. $PMA_recoding_engine = PMA_CHARSET_NONE;
  64. }
  65. } else {
  66. $PMA_recoding_engine = PMA_CHARSET_NONE;
  67. }
  68. /* Load AIX iconv wrapper if needed */
  69. if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX) {
  70. include_once './libraries/iconv_wrapper.lib.php';
  71. }
  72. /**
  73. * Converts encoding of text according to parameters with detected
  74. * conversion function.
  75. *
  76. * @param string $src_charset source charset
  77. * @param string $dest_charset target charset
  78. * @param string $what what to convert
  79. *
  80. * @return string converted text
  81. *
  82. * @access public
  83. *
  84. */
  85. function PMA_convertString($src_charset, $dest_charset, $what)
  86. {
  87. if ($src_charset == $dest_charset) {
  88. return $what;
  89. }
  90. switch ($GLOBALS['PMA_recoding_engine']) {
  91. case PMA_CHARSET_RECODE:
  92. return recode_string($src_charset . '..' . $dest_charset, $what);
  93. case PMA_CHARSET_ICONV:
  94. return iconv(
  95. $src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what
  96. );
  97. case PMA_CHARSET_ICONV_AIX:
  98. return PMA_convertAIXIconv(
  99. $src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what
  100. );
  101. case PMA_CHARSET_MB:
  102. return mb_convert_encoding(
  103. $what, $dest_charset, $src_charset
  104. );
  105. default:
  106. return $what;
  107. }
  108. } // end of the "PMA_convertString()" function
  109. ?>