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