SafeFN.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Class to safely store UTF-8 in a Filename
  4. *
  5. * Encodes a utf8 string using only the following characters 0-9a-z_.-%
  6. * characters 0-9a-z in the original string are preserved, "plain".
  7. * all other characters are represented in a substring that starts
  8. * with '%' are "converted".
  9. * The transition from converted substrings to plain characters is
  10. * marked with a '.'
  11. *
  12. * @author Christopher Smith <chris@jalakai.co.uk>
  13. * @date 2010-04-02
  14. */
  15. class SafeFN {
  16. // 'safe' characters are a superset of $plain, $pre_indicator and $post_indicator
  17. private static $plain = '-./[_0123456789abcdefghijklmnopqrstuvwxyz'; // these characters aren't converted
  18. private static $pre_indicator = '%';
  19. private static $post_indicator = ']';
  20. /**
  21. * Convert an UTF-8 string to a safe ASCII String
  22. *
  23. * conversion process
  24. * - if codepoint is a plain or post_indicator character,
  25. * - if previous character was "converted", append post_indicator to output, clear "converted" flag
  26. * - append ascii byte for character to output
  27. * (continue to next character)
  28. *
  29. * - if codepoint is a pre_indicator character,
  30. * - append ascii byte for character to output, set "converted" flag
  31. * (continue to next character)
  32. *
  33. * (all remaining characters)
  34. * - reduce codepoint value for non-printable ASCII characters (0x00 - 0x1f). Space becomes our zero.
  35. * - convert reduced value to base36 (0-9a-z)
  36. * - append $pre_indicator characater followed by base36 string to output, set converted flag
  37. * (continue to next character)
  38. *
  39. * @param string $filename a utf8 string, should only include printable characters - not 0x00-0x1f
  40. * @return string an encoded representation of $filename using only 'safe' ASCII characters
  41. *
  42. * @author Christopher Smith <chris@jalakai.co.uk>
  43. */
  44. public static function encode($filename) {
  45. return self::unicodeToSafe(\dokuwiki\Utf8\Unicode::fromUtf8($filename));
  46. }
  47. /**
  48. * decoding process
  49. * - split the string into substrings at any occurrence of pre or post indicator characters
  50. * - check the first character of the substring
  51. * - if its not a pre_indicator character
  52. * - if previous character was converted, skip over post_indicator character
  53. * - copy codepoint values of remaining characters to the output array
  54. * - clear any converted flag
  55. * (continue to next substring)
  56. *
  57. * _ else (its a pre_indicator character)
  58. * - if string length is 1, copy the post_indicator character to the output array
  59. * (continue to next substring)
  60. *
  61. * - else (string length > 1)
  62. * - skip the pre-indicator character and convert remaining string from base36 to base10
  63. * - increase codepoint value for non-printable ASCII characters (add 0x20)
  64. * - append codepoint to output array
  65. * (continue to next substring)
  66. *
  67. * @param string $filename a 'safe' encoded ASCII string,
  68. * @return string decoded utf8 representation of $filename
  69. *
  70. * @author Christopher Smith <chris@jalakai.co.uk>
  71. */
  72. public static function decode($filename) {
  73. return \dokuwiki\Utf8\Unicode::toUtf8(self::safeToUnicode(strtolower($filename)));
  74. }
  75. public static function validatePrintableUtf8($printable_utf8) {
  76. return !preg_match('#[\x01-\x1f]#',$printable_utf8);
  77. }
  78. public static function validateSafe($safe) {
  79. return !preg_match('#[^'.self::$plain.self::$post_indicator.self::$pre_indicator.']#',$safe);
  80. }
  81. /**
  82. * convert an array of unicode codepoints into 'safe_filename' format
  83. *
  84. * @param array int $unicode an array of unicode codepoints
  85. * @return string the unicode represented in 'safe_filename' format
  86. *
  87. * @author Christopher Smith <chris@jalakai.co.uk>
  88. */
  89. private static function unicodeToSafe($unicode) {
  90. $safe = '';
  91. $converted = false;
  92. foreach ($unicode as $codepoint) {
  93. if ($codepoint < 127 && (strpos(self::$plain.self::$post_indicator,chr($codepoint))!==false)) {
  94. if ($converted) {
  95. $safe .= self::$post_indicator;
  96. $converted = false;
  97. }
  98. $safe .= chr($codepoint);
  99. } else if ($codepoint == ord(self::$pre_indicator)) {
  100. $safe .= self::$pre_indicator;
  101. $converted = true;
  102. } else {
  103. $safe .= self::$pre_indicator.base_convert((string)($codepoint-32),10,36);
  104. $converted = true;
  105. }
  106. }
  107. if($converted) $safe .= self::$post_indicator;
  108. return $safe;
  109. }
  110. /**
  111. * convert a 'safe_filename' string into an array of unicode codepoints
  112. *
  113. * @param string $safe a filename in 'safe_filename' format
  114. * @return array int an array of unicode codepoints
  115. *
  116. * @author Christopher Smith <chris@jalakai.co.uk>
  117. */
  118. private static function safeToUnicode($safe) {
  119. $unicode = array();
  120. $split = preg_split('#(?=['.self::$post_indicator.self::$pre_indicator.'])#',$safe,-1,PREG_SPLIT_NO_EMPTY);
  121. $converted = false;
  122. foreach ($split as $sub) {
  123. $len = strlen($sub);
  124. if ($sub[0] != self::$pre_indicator) {
  125. // plain (unconverted) characters, optionally starting with a post_indicator
  126. // set initial value to skip any post_indicator
  127. for ($i=($converted?1:0); $i < $len; $i++) {
  128. $unicode[] = ord($sub[$i]);
  129. }
  130. $converted = false;
  131. } else if ($len==1) {
  132. // a pre_indicator character in the real data
  133. $unicode[] = ord($sub);
  134. $converted = true;
  135. } else {
  136. // a single codepoint in base36, adjusted for initial 32 non-printable chars
  137. $unicode[] = 32 + (int)base_convert(substr($sub,1),36,10);
  138. $converted = true;
  139. }
  140. }
  141. return $unicode;
  142. }
  143. }