PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/classes/Encoding.php

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