PageRenderTime 169ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/pma/libraries/charset_conversion.lib.php

https://bitbucket.org/StasPiv/playzone
PHP | 238 lines | 164 code | 17 blank | 57 comment | 59 complexity | f1f61fb28620bb9042f8c85afd38d56d MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0, LGPL-2.1
  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 11982 2008-11-24 10:32:56Z nijel $
  7. * @package phpMyAdmin
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  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. if ($cfg['RecodingEngine'] == 'recode') {
  18. if (!@extension_loaded('recode')) {
  19. echo $strCantLoadRecodeIconv;
  20. exit;
  21. }
  22. $PMA_recoding_engine = 'recode';
  23. } elseif ($cfg['RecodingEngine'] == 'iconv') {
  24. if (!@extension_loaded('iconv')) {
  25. echo $strCantLoadRecodeIconv;
  26. exit;
  27. }
  28. $PMA_recoding_engine = 'iconv';
  29. } else {
  30. if (@extension_loaded('iconv')) {
  31. $PMA_recoding_engine = 'iconv';
  32. } elseif (@extension_loaded('recode')) {
  33. $PMA_recoding_engine = 'recode';
  34. } else {
  35. echo $strCantLoadRecodeIconv;
  36. exit;
  37. }
  38. }
  39. } // end load recode/iconv extension
  40. define('PMA_CHARSET_NONE', 0);
  41. define('PMA_CHARSET_ICONV', 1);
  42. define('PMA_CHARSET_LIBICONV', 2);
  43. define('PMA_CHARSET_RECODE', 3);
  44. define('PMA_CHARSET_ICONV_AIX', 4);
  45. if (!isset($cfg['IconvExtraParams'])) {
  46. $cfg['IconvExtraParams'] = '';
  47. }
  48. // Finally detect which function we will use:
  49. if (isset($cfg['AllowAnywhereRecoding'])
  50. && $cfg['AllowAnywhereRecoding']) {
  51. if (!isset($PMA_recoding_engine)) {
  52. $PMA_recoding_engine = $cfg['RecodingEngine'];
  53. }
  54. if ($PMA_recoding_engine == 'iconv') {
  55. if (@function_exists('iconv')) {
  56. if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
  57. $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
  58. } else {
  59. $PMA_recoding_engine = PMA_CHARSET_ICONV;
  60. }
  61. } elseif (@function_exists('libiconv')) {
  62. $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
  63. } else {
  64. $PMA_recoding_engine = PMA_CHARSET_NONE;
  65. if (!isset($GLOBALS['is_header_sent'])) {
  66. include './libraries/header.inc.php';
  67. }
  68. echo $strCantUseRecodeIconv;
  69. require_once './libraries/footer.inc.php';
  70. exit();
  71. }
  72. } elseif ($PMA_recoding_engine == 'recode') {
  73. if (@function_exists('recode_string')) {
  74. $PMA_recoding_engine = PMA_CHARSET_RECODE;
  75. } else {
  76. $PMA_recoding_engine = PMA_CHARSET_NONE;
  77. require_once './libraries/header.inc.php';
  78. echo $strCantUseRecodeIconv;
  79. require_once './libraries/footer.inc.php';
  80. exit;
  81. }
  82. } else {
  83. if (@function_exists('iconv')) {
  84. if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
  85. $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
  86. } else {
  87. $PMA_recoding_engine = PMA_CHARSET_ICONV;
  88. }
  89. } elseif (@function_exists('libiconv')) {
  90. $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
  91. } elseif (@function_exists('recode_string')) {
  92. $PMA_recoding_engine = PMA_CHARSET_RECODE;
  93. } else {
  94. $PMA_recoding_engine = PMA_CHARSET_NONE;
  95. require_once './libraries/header.inc.php';
  96. echo $strCantUseRecodeIconv;
  97. require_once './libraries/footer.inc.php';
  98. exit;
  99. }
  100. }
  101. } else {
  102. $PMA_recoding_engine = PMA_CHARSET_NONE;
  103. }
  104. /* Load AIX iconv wrapper if needed */
  105. if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX) {
  106. require_once './libraries/iconv_wrapper.lib.php';
  107. }
  108. /**
  109. * Converts encoding of text according to current settings.
  110. *
  111. * @param string what to convert
  112. *
  113. * @return string converted text
  114. *
  115. * @global array the configuration array
  116. * @global boolean whether recoding is allowed or not
  117. * @global string the current charset
  118. * @global array the charset to convert to
  119. *
  120. * @access public
  121. *
  122. * @author nijel
  123. */
  124. function PMA_convert_charset($what) {
  125. global $cfg, $charset, $convcharset;
  126. if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] )
  127. || $convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
  128. return $what;
  129. } else {
  130. switch ($GLOBALS['PMA_recoding_engine']) {
  131. case PMA_CHARSET_RECODE:
  132. return recode_string($charset . '..' . $convcharset, $what);
  133. case PMA_CHARSET_ICONV:
  134. return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
  135. case PMA_CHARSET_ICONV_AIX:
  136. return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
  137. case PMA_CHARSET_LIBICONV:
  138. return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
  139. default:
  140. return $what;
  141. }
  142. }
  143. } // end of the "PMA_convert_charset()" function
  144. /**
  145. * Converts encoding of text according to parameters with detected
  146. * conversion function.
  147. *
  148. * @param string source charset
  149. * @param string target charset
  150. * @param string what to convert
  151. *
  152. * @return string converted text
  153. *
  154. * @access public
  155. *
  156. * @author nijel
  157. */
  158. function PMA_convert_string($src_charset, $dest_charset, $what) {
  159. if ($src_charset == $dest_charset) {
  160. return $what;
  161. }
  162. switch ($GLOBALS['PMA_recoding_engine']) {
  163. case PMA_CHARSET_RECODE:
  164. return recode_string($src_charset . '..' . $dest_charset, $what);
  165. case PMA_CHARSET_ICONV:
  166. return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
  167. case PMA_CHARSET_ICONV_AIX:
  168. return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
  169. case PMA_CHARSET_LIBICONV:
  170. return libiconv($src_charset, $dest_charset, $what);
  171. default:
  172. return $what;
  173. }
  174. } // end of the "PMA_convert_string()" function
  175. /**
  176. * Converts encoding of file according to parameters with detected
  177. * conversion function. The old file will be unlinked and new created and
  178. * its file name is returned.
  179. *
  180. * @param string source charset
  181. * @param string target charset
  182. * @param string file to convert
  183. *
  184. * @return string new temporay file
  185. *
  186. * @access public
  187. *
  188. * @author nijel
  189. */
  190. function PMA_convert_file($src_charset, $dest_charset, $file) {
  191. switch ($GLOBALS['PMA_recoding_engine']) {
  192. case PMA_CHARSET_RECODE:
  193. case PMA_CHARSET_ICONV:
  194. case PMA_CHARSET_LIBICONV:
  195. $tmpfname = tempnam('', 'PMA_convert_file');
  196. $fin = fopen($file, 'r');
  197. $fout = fopen($tmpfname, 'w');
  198. if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE) {
  199. recode_file($src_charset . '..' . $dest_charset, $fin, $fout);
  200. } else {
  201. while (!feof($fin)) {
  202. $line = fgets($fin, 4096);
  203. if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV) {
  204. $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
  205. } elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX) {
  206. $dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
  207. } else {
  208. $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
  209. }
  210. fputs($fout, $dist);
  211. } // end while
  212. }
  213. fclose($fin);
  214. fclose($fout);
  215. unlink($file);
  216. return $tmpfname;
  217. default:
  218. return $file;
  219. }
  220. } // end of the "PMA_convert_file()" function
  221. ?>