/phpmyadmin/libraries/charset_conversion.lib.php

https://github.com/md-tech/openemr · PHP · 326 lines · 230 code · 21 blank · 75 comment · 85 complexity · 3d0a9c9dda42def061b3a312a51c5edf MD5 · raw file

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