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

/phpmyadmin/libraries/kanji-encoding.lib.php

https://github.com/drbowen/openemr
PHP | 161 lines | 79 code | 20 blank | 62 comment | 23 complexity | 2ce4f011886f9450065deb582996f355 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions for kanji-encoding convert (available only with japanese
  5. * language)
  6. *
  7. * PHP4 configure requirements:
  8. * --enable-mbstring --enable-mbstr-enc-trans --enable-mbregex
  9. *
  10. * 2002/2/22 - by Yukihiro Kawada <kawada@den.fujifilm.co.jp>
  11. *
  12. * @package PhpMyAdmin
  13. */
  14. if (! defined('PHPMYADMIN')) {
  15. exit;
  16. }
  17. /**
  18. * Gets the php internal encoding codes and sets the available encoding
  19. * codes list
  20. * 2002/1/4 by Y.Kawada
  21. *
  22. * @global string the current encoding code
  23. * @global string the available encoding codes list
  24. *
  25. * @return boolean always true
  26. */
  27. function PMA_internal_enc_check()
  28. {
  29. global $internal_enc, $enc_list;
  30. $internal_enc = mb_internal_encoding();
  31. if ($internal_enc == 'EUC-JP') {
  32. $enc_list = 'ASCII,EUC-JP,SJIS,JIS';
  33. } else {
  34. $enc_list = 'ASCII,SJIS,EUC-JP,JIS';
  35. }
  36. return true;
  37. } // end of the 'PMA_internal_enc_check' function
  38. /**
  39. * Reverses SJIS & EUC-JP position in the encoding codes list
  40. * 2002/1/4 by Y.Kawada
  41. *
  42. * @global string the available encoding codes list
  43. *
  44. * @return boolean always true
  45. */
  46. function PMA_change_enc_order()
  47. {
  48. global $enc_list;
  49. $p = explode(',', $enc_list);
  50. if ($p[1] == 'EUC-JP') {
  51. $enc_list = 'ASCII,SJIS,EUC-JP,JIS';
  52. } else {
  53. $enc_list = 'ASCII,EUC-JP,SJIS,JIS';
  54. }
  55. return true;
  56. } // end of the 'PMA_change_enc_order' function
  57. /**
  58. * Kanji string encoding convert
  59. * 2002/1/4 by Y.Kawada
  60. *
  61. * @param string $str the string to convert
  62. * @param string $enc the destination encoding code
  63. * @param string $kana set 'kana' convert to JIS-X208-kana
  64. *
  65. * @global string the available encoding codes list
  66. *
  67. * @return string the converted string
  68. */
  69. function PMA_kanji_str_conv($str, $enc, $kana)
  70. {
  71. global $enc_list;
  72. if ($enc == '' && $kana == '') {
  73. return $str;
  74. }
  75. $nw = mb_detect_encoding($str, $enc_list);
  76. if ($kana == 'kana') {
  77. $dist = mb_convert_kana($str, 'KV', $nw);
  78. $str = $dist;
  79. }
  80. if ($nw != $enc && $enc != '') {
  81. $dist = mb_convert_encoding($str, $enc, $nw);
  82. } else {
  83. $dist = $str;
  84. }
  85. return $dist;
  86. } // end of the 'PMA_kanji_str_conv' function
  87. /**
  88. * Kanji file encoding convert
  89. * 2002/1/4 by Y.Kawada
  90. *
  91. * @param string $file the name of the file to convert
  92. * @param string $enc the destination encoding code
  93. * @param string $kana set 'kana' convert to JIS-X208-kana
  94. *
  95. * @return string the name of the converted file
  96. */
  97. function PMA_kanji_file_conv($file, $enc, $kana)
  98. {
  99. if ($enc == '' && $kana == '') {
  100. return $file;
  101. }
  102. $tmpfname = tempnam('', $enc);
  103. $fpd = fopen($tmpfname, 'wb');
  104. $fps = fopen($file, 'r');
  105. PMA_change_enc_order();
  106. while (!feof($fps)) {
  107. $line = fgets($fps, 4096);
  108. $dist = PMA_kanji_str_conv($line, $enc, $kana);
  109. fputs($fpd, $dist);
  110. } // end while
  111. PMA_change_enc_order();
  112. fclose($fps);
  113. fclose($fpd);
  114. unlink($file);
  115. return $tmpfname;
  116. } // end of the 'PMA_kanji_file_conv' function
  117. /**
  118. * Defines radio form fields to switch between encoding modes
  119. * 2002/1/4 by Y.Kawada
  120. *
  121. * @param string $spaces spaces character to prepend the output with
  122. *
  123. * @return string xhtml code for the radio controls
  124. */
  125. function PMA_set_enc_form($spaces)
  126. {
  127. return "\n"
  128. /* l10n: This is currently used only in Japanese locales */
  129. . $spaces . '<ul>' . "\n" . '<li>'
  130. . $spaces . '<input type="radio" name="knjenc" value="" checked="checked" id="kj-none" /><label for="kj-none">' . _pgettext('None encoding conversion', 'None') . "</label>\n"
  131. . $spaces . '<input type="radio" name="knjenc" value="EUC-JP" id="kj-euc" /><label for="kj-euc">EUC</label>' . "\n"
  132. . $spaces . '<input type="radio" name="knjenc" value="SJIS" id="kj-sjis" /><label for="kj-sjis">SJIS</label>' . "\n"
  133. . $spaces . '</li>' . "\n" . '<li>'
  134. . $spaces . '<input type="checkbox" name="xkana" value="kana" id="kj-kana" />' . "\n"
  135. /* l10n: This is currently used only in Japanese locales */
  136. . $spaces . '<label for="kj-kana">' . __('Convert to Kana') . '</label><br />' . "\n"
  137. . $spaces . '</li>' . "\n" . '</ul>'
  138. ;
  139. } // end of the 'PMA_set_enc_form' function
  140. PMA_internal_enc_check();
  141. ?>