/ASTRA_Demo_Server/udrive/home/admin/www/phpMyAdmin/libraries/charset_conversion.lib.php

https://github.com/shafiqissani/ASTRA-College-Website · PHP · 325 lines · 227 code · 24 blank · 74 comment · 84 complexity · 9f0032fe60b619140c4b9e261f266510 MD5 · raw file

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