Encoding.php 8.7 KB

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