Encoding.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Hold the PhpMyAdmin\Encoding class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use PhpMyAdmin\Config\ConfigFile;
  10. use PhpMyAdmin\Core;
  11. use PhpMyAdmin\Template;
  12. /**
  13. * Encoding conversion helper class
  14. *
  15. * @package PhpMyAdmin
  16. */
  17. class Encoding
  18. {
  19. /**
  20. * None encoding conversion engine
  21. *
  22. * @var int
  23. */
  24. const ENGINE_NONE = 0;
  25. /**
  26. * iconv encoding conversion engine
  27. *
  28. * @var int
  29. */
  30. const ENGINE_ICONV = 1;
  31. /**
  32. * recode encoding conversion engine
  33. *
  34. * @var int
  35. */
  36. const ENGINE_RECODE = 2;
  37. /**
  38. * mbstring encoding conversion engine
  39. *
  40. * @var int
  41. */
  42. const ENGINE_MB = 3;
  43. /**
  44. * Chosen encoding engine
  45. *
  46. * @var int
  47. */
  48. private static $_engine = null;
  49. /**
  50. * Map of conversion engine configurations
  51. *
  52. * Each entry contains:
  53. *
  54. * - function to detect
  55. * - engine contant
  56. * - extension name to warn when missing
  57. *
  58. * @var array
  59. */
  60. private static $_enginemap = array(
  61. 'iconv' => array('iconv', self::ENGINE_ICONV, 'iconv'),
  62. 'recode' => array('recode_string', self::ENGINE_RECODE, 'recode'),
  63. 'mb' => array('mb_convert_encoding', self::ENGINE_MB, 'mbstring'),
  64. 'none' => array('isset', self::ENGINE_NONE, ''),
  65. );
  66. /**
  67. * Order of automatic detection of engines
  68. *
  69. * @var array
  70. */
  71. private static $_engineorder = array(
  72. 'iconv', 'mb', 'recode',
  73. );
  74. /**
  75. * Kanji encodings list
  76. *
  77. * @var string
  78. */
  79. private static $_kanji_encodings = 'ASCII,SJIS,EUC-JP,JIS';
  80. /**
  81. * Initializes encoding engine detecting available backends.
  82. *
  83. * @return void
  84. */
  85. public static function initEngine()
  86. {
  87. $engine = 'auto';
  88. if (isset($GLOBALS['cfg']['RecodingEngine'])) {
  89. $engine = $GLOBALS['cfg']['RecodingEngine'];
  90. }
  91. /* Use user configuration */
  92. if (isset(self::$_enginemap[$engine])) {
  93. if (function_exists(self::$_enginemap[$engine][0])) {
  94. self::$_engine = self::$_enginemap[$engine][1];
  95. return;
  96. } else {
  97. Core::warnMissingExtension(self::$_enginemap[$engine][2]);
  98. }
  99. }
  100. /* Autodetection */
  101. foreach (self::$_engineorder as $engine) {
  102. if (function_exists(self::$_enginemap[$engine][0])) {
  103. self::$_engine = self::$_enginemap[$engine][1];
  104. return;
  105. }
  106. }
  107. /* Fallback to none conversion */
  108. self::$_engine = self::ENGINE_NONE;
  109. }
  110. /**
  111. * Setter for engine. Use with caution, mostly useful for testing.
  112. *
  113. * @param int $engine Engine enconding
  114. *
  115. * @return void
  116. */
  117. public static function setEngine($engine)
  118. {
  119. self::$_engine = $engine;
  120. }
  121. /**
  122. * Checks whether there is any charset conversion supported
  123. *
  124. * @return bool
  125. */
  126. public static function isSupported()
  127. {
  128. if (is_null(self::$_engine)) {
  129. self::initEngine();
  130. }
  131. return self::$_engine != self::ENGINE_NONE;
  132. }
  133. /**
  134. * Converts encoding of text according to parameters with detected
  135. * conversion function.
  136. *
  137. * @param string $src_charset source charset
  138. * @param string $dest_charset target charset
  139. * @param string $what what to convert
  140. *
  141. * @return string converted text
  142. *
  143. * @access public
  144. */
  145. public static function convertString($src_charset, $dest_charset, $what)
  146. {
  147. if ($src_charset == $dest_charset) {
  148. return $what;
  149. }
  150. if (is_null(self::$_engine)) {
  151. self::initEngine();
  152. }
  153. switch (self::$_engine) {
  154. case self::ENGINE_RECODE:
  155. return recode_string(
  156. $src_charset . '..' . $dest_charset,
  157. $what
  158. );
  159. case self::ENGINE_ICONV:
  160. return iconv(
  161. $src_charset,
  162. $dest_charset .
  163. (isset($GLOBALS['cfg']['IconvExtraParams']) ? $GLOBALS['cfg']['IconvExtraParams'] : ''),
  164. $what
  165. );
  166. case self::ENGINE_MB:
  167. return mb_convert_encoding(
  168. $what,
  169. $dest_charset,
  170. $src_charset
  171. );
  172. default:
  173. return $what;
  174. }
  175. }
  176. /**
  177. * Detects whether Kanji encoding is available
  178. *
  179. * @return bool
  180. */
  181. public static function canConvertKanji()
  182. {
  183. return $GLOBALS['lang'] == 'ja';
  184. }
  185. /**
  186. * Setter for Kanji encodings. Use with caution, mostly useful for testing.
  187. *
  188. * @return string
  189. */
  190. public static function getKanjiEncodings()
  191. {
  192. return self::$_kanji_encodings;
  193. }
  194. /**
  195. * Setter for Kanji encodings. Use with caution, mostly useful for testing.
  196. *
  197. * @param string $value Kanji encodings list
  198. *
  199. * @return void
  200. */
  201. public static function setKanjiEncodings($value)
  202. {
  203. self::$_kanji_encodings = $value;
  204. }
  205. /**
  206. * Reverses SJIS & EUC-JP position in the encoding codes list
  207. *
  208. * @return void
  209. */
  210. public static function kanjiChangeOrder()
  211. {
  212. $parts = explode(',', self::$_kanji_encodings);
  213. if ($parts[1] == 'EUC-JP') {
  214. self::$_kanji_encodings = 'ASCII,SJIS,EUC-JP,JIS';
  215. } else {
  216. self::$_kanji_encodings = 'ASCII,EUC-JP,SJIS,JIS';
  217. }
  218. }
  219. /**
  220. * Kanji string encoding convert
  221. *
  222. * @param string $str the string to convert
  223. * @param string $enc the destination encoding code
  224. * @param string $kana set 'kana' convert to JIS-X208-kana
  225. *
  226. * @return string the converted string
  227. */
  228. public static function kanjiStrConv($str, $enc, $kana)
  229. {
  230. if ($enc == '' && $kana == '') {
  231. return $str;
  232. }
  233. $string_encoding = mb_detect_encoding($str, self::$_kanji_encodings);
  234. if ($string_encoding === false) {
  235. $string_encoding = 'utf-8';
  236. }
  237. if ($kana == 'kana') {
  238. $dist = mb_convert_kana($str, 'KV', $string_encoding);
  239. $str = $dist;
  240. }
  241. if ($string_encoding != $enc && $enc != '') {
  242. $dist = mb_convert_encoding($str, $enc, $string_encoding);
  243. } else {
  244. $dist = $str;
  245. }
  246. return $dist;
  247. }
  248. /**
  249. * Kanji file encoding convert
  250. *
  251. * @param string $file the name of the file to convert
  252. * @param string $enc the destination encoding code
  253. * @param string $kana set 'kana' convert to JIS-X208-kana
  254. *
  255. * @return string the name of the converted file
  256. */
  257. public static function kanjiFileConv($file, $enc, $kana)
  258. {
  259. if ($enc == '' && $kana == '') {
  260. return $file;
  261. }
  262. $tmpfname = tempnam($GLOBALS['PMA_Config']->getUploadTempDir(), $enc);
  263. $fpd = fopen($tmpfname, 'wb');
  264. $fps = fopen($file, 'r');
  265. self::kanjiChangeOrder();
  266. while (!feof($fps)) {
  267. $line = fgets($fps, 4096);
  268. $dist = self::kanjiStrConv($line, $enc, $kana);
  269. fputs($fpd, $dist);
  270. } // end while
  271. self::kanjiChangeOrder();
  272. fclose($fps);
  273. fclose($fpd);
  274. unlink($file);
  275. return $tmpfname;
  276. }
  277. /**
  278. * Defines radio form fields to switch between encoding modes
  279. *
  280. * @return string xhtml code for the radio controls
  281. */
  282. public static function kanjiEncodingForm()
  283. {
  284. return Template::get('encoding/kanji_encoding_form')->render();
  285. }
  286. /**
  287. * Lists available encodings.
  288. *
  289. * @return array
  290. */
  291. public static function listEncodings()
  292. {
  293. if (is_null(self::$_engine)) {
  294. self::initEngine();
  295. }
  296. /* Most engines do not support listing */
  297. if (self::$_engine != self::ENGINE_MB) {
  298. return $GLOBALS['cfg']['AvailableCharsets'];
  299. }
  300. return array_intersect(
  301. array_map('strtolower', mb_list_encodings()),
  302. $GLOBALS['cfg']['AvailableCharsets']
  303. );
  304. }
  305. }