xref: /dokuwiki/inc/SafeFN.class.php (revision d868eb89f182718a31113373a6272670bd7f8012)
170e083ceSChristopher Smith<?php
270e083ceSChristopher Smith
324870174SAndreas Gohruse dokuwiki\Utf8\Unicode;
470e083ceSChristopher Smith/**
570e083ceSChristopher Smith * Class to safely store UTF-8 in a Filename
670e083ceSChristopher Smith *
770e083ceSChristopher Smith * Encodes a utf8 string using only the following characters 0-9a-z_.-%
870e083ceSChristopher Smith * characters 0-9a-z in the original string are preserved, "plain".
970e083ceSChristopher Smith * all other characters are represented in a substring that starts
1070e083ceSChristopher Smith * with '%' are "converted".
1170e083ceSChristopher Smith * The transition from converted substrings to plain characters is
1270e083ceSChristopher Smith * marked with a '.'
1370e083ceSChristopher Smith *
1463703ba5SAndreas Gohr * @author   Christopher Smith <chris@jalakai.co.uk>
1570e083ceSChristopher Smith * @date     2010-04-02
1670e083ceSChristopher Smith */
178c7c53b0SAndreas Gohrclass SafeFN
188c7c53b0SAndreas Gohr{
1970e083ceSChristopher Smith
20dcee16d6SChristopher Smith    // 'safe' characters are a superset of $plain, $pre_indicator and $post_indicator
21344763adSChristopher Smith    private static $plain = '-./[_0123456789abcdefghijklmnopqrstuvwxyz'; // these characters aren't converted
2270e083ceSChristopher Smith    private static $pre_indicator = '%';
23344763adSChristopher Smith    private static $post_indicator = ']';
2470e083ceSChristopher Smith
2570e083ceSChristopher Smith    /**
2670e083ceSChristopher Smith     * Convert an UTF-8 string to a safe ASCII String
2770e083ceSChristopher Smith     *
2870e083ceSChristopher Smith     *  conversion process
29dcee16d6SChristopher Smith     *    - if codepoint is a plain or post_indicator character,
30dcee16d6SChristopher Smith     *      - if previous character was "converted", append post_indicator to output, clear "converted" flag
31dcee16d6SChristopher Smith     *      - append ascii byte for character to output
32dcee16d6SChristopher Smith     *      (continue to next character)
3370e083ceSChristopher Smith     *
34dcee16d6SChristopher Smith     *    - if codepoint is a pre_indicator character,
35dcee16d6SChristopher Smith     *      - append ascii byte for character to output, set "converted" flag
36dcee16d6SChristopher Smith     *      (continue to next character)
37dcee16d6SChristopher Smith     *
38dcee16d6SChristopher Smith     *    (all remaining characters)
39dcee16d6SChristopher Smith     *    - reduce codepoint value for non-printable ASCII characters (0x00 - 0x1f).  Space becomes our zero.
40dcee16d6SChristopher Smith     *    - convert reduced value to base36 (0-9a-z)
41dcee16d6SChristopher Smith     *    - append $pre_indicator characater followed by base36 string to output, set converted flag
42344763adSChristopher Smith     *    (continue to next character)
43dcee16d6SChristopher Smith     *
44dcee16d6SChristopher Smith     * @param    string    $filename     a utf8 string, should only include printable characters - not 0x00-0x1f
45dcee16d6SChristopher Smith     * @return   string    an encoded representation of $filename using only 'safe' ASCII characters
46dcee16d6SChristopher Smith     *
47dcee16d6SChristopher Smith     * @author   Christopher Smith <chris@jalakai.co.uk>
4870e083ceSChristopher Smith     */
49*d868eb89SAndreas Gohr    public static function encode($filename)
50*d868eb89SAndreas Gohr    {
5124870174SAndreas Gohr        return self::unicodeToSafe(Unicode::fromUtf8($filename));
5270e083ceSChristopher Smith    }
5370e083ceSChristopher Smith
5470e083ceSChristopher Smith    /**
5570e083ceSChristopher Smith     *  decoding process
56dcee16d6SChristopher Smith     *    - split the string into substrings at any occurrence of pre or post indicator characters
5770e083ceSChristopher Smith     *    - check the first character of the substring
58dcee16d6SChristopher Smith     *      - if its not a pre_indicator character
59dcee16d6SChristopher Smith     *        - if previous character was converted, skip over post_indicator character
60dcee16d6SChristopher Smith     *        - copy codepoint values of remaining characters to the output array
61dcee16d6SChristopher Smith     *        - clear any converted flag
62dcee16d6SChristopher Smith     *      (continue to next substring)
63dcee16d6SChristopher Smith     *
64dcee16d6SChristopher Smith     *     _ else (its a pre_indicator character)
65dcee16d6SChristopher Smith     *       - if string length is 1, copy the post_indicator character to the output array
66dcee16d6SChristopher Smith     *       (continue to next substring)
67dcee16d6SChristopher Smith     *
68dcee16d6SChristopher Smith     *       - else (string length > 1)
69dcee16d6SChristopher Smith     *         - skip the pre-indicator character and convert remaining string from base36 to base10
70dcee16d6SChristopher Smith     *         - increase codepoint value for non-printable ASCII characters (add 0x20)
71dcee16d6SChristopher Smith     *         - append codepoint to output array
72dcee16d6SChristopher Smith     *       (continue to next substring)
73dcee16d6SChristopher Smith     *
74dcee16d6SChristopher Smith     * @param    string    $filename     a 'safe' encoded ASCII string,
75dcee16d6SChristopher Smith     * @return   string    decoded utf8 representation of $filename
76dcee16d6SChristopher Smith     *
77dcee16d6SChristopher Smith     * @author   Christopher Smith <chris@jalakai.co.uk>
7870e083ceSChristopher Smith     */
79*d868eb89SAndreas Gohr    public static function decode($filename)
80*d868eb89SAndreas Gohr    {
8124870174SAndreas Gohr        return Unicode::toUtf8(self::safeToUnicode(strtolower($filename)));
8270e083ceSChristopher Smith    }
8370e083ceSChristopher Smith
84*d868eb89SAndreas Gohr    public static function validatePrintableUtf8($printable_utf8)
85*d868eb89SAndreas Gohr    {
86dcee16d6SChristopher Smith        return !preg_match('#[\x01-\x1f]#', $printable_utf8);
8770e083ceSChristopher Smith    }
8870e083ceSChristopher Smith
89*d868eb89SAndreas Gohr    public static function validateSafe($safe)
90*d868eb89SAndreas Gohr    {
91dcee16d6SChristopher Smith        return !preg_match('#[^'.self::$plain.self::$post_indicator.self::$pre_indicator.']#', $safe);
9270e083ceSChristopher Smith    }
9370e083ceSChristopher Smith
94dcee16d6SChristopher Smith    /**
95dcee16d6SChristopher Smith     * convert an array of unicode codepoints into 'safe_filename' format
96dcee16d6SChristopher Smith     *
9708aad3a3STakamura     * @param    array  int    $unicode    an array of unicode codepoints
98dcee16d6SChristopher Smith     * @return   string        the unicode represented in 'safe_filename' format
99dcee16d6SChristopher Smith     *
100dcee16d6SChristopher Smith     * @author   Christopher Smith <chris@jalakai.co.uk>
101dcee16d6SChristopher Smith     */
102*d868eb89SAndreas Gohr    private static function unicodeToSafe($unicode)
103*d868eb89SAndreas Gohr    {
10470e083ceSChristopher Smith
10570e083ceSChristopher Smith        $safe = '';
10670e083ceSChristopher Smith        $converted = false;
10770e083ceSChristopher Smith
10870e083ceSChristopher Smith        foreach ($unicode as $codepoint) {
109dcee16d6SChristopher Smith            if ($codepoint < 127 && (strpos(self::$plain.self::$post_indicator, chr($codepoint))!==false)) {
11070e083ceSChristopher Smith                if ($converted) {
11170e083ceSChristopher Smith                    $safe .= self::$post_indicator;
11270e083ceSChristopher Smith                    $converted = false;
11370e083ceSChristopher Smith                }
11470e083ceSChristopher Smith                $safe .= chr($codepoint);
115dcee16d6SChristopher Smith            } elseif ($codepoint == ord(self::$pre_indicator)) {
116dcee16d6SChristopher Smith                $safe .= self::$pre_indicator;
11770e083ceSChristopher Smith                $converted = true;
11870e083ceSChristopher Smith            } else {
119dcee16d6SChristopher Smith                $safe .= self::$pre_indicator.base_convert((string)($codepoint-32), 10, 36);
12070e083ceSChristopher Smith                $converted = true;
12170e083ceSChristopher Smith            }
12270e083ceSChristopher Smith        }
1238bddd94eSAdrian Lang        if($converted) $safe .= self::$post_indicator;
12470e083ceSChristopher Smith        return $safe;
12570e083ceSChristopher Smith    }
12670e083ceSChristopher Smith
127dcee16d6SChristopher Smith    /**
128dcee16d6SChristopher Smith     * convert a 'safe_filename' string into an array of unicode codepoints
129dcee16d6SChristopher Smith     *
130dcee16d6SChristopher Smith     * @param   string         $safe     a filename in 'safe_filename' format
131dcee16d6SChristopher Smith     * @return  array   int    an array of unicode codepoints
132dcee16d6SChristopher Smith     *
133dcee16d6SChristopher Smith     * @author   Christopher Smith <chris@jalakai.co.uk>
134dcee16d6SChristopher Smith     */
135*d868eb89SAndreas Gohr    private static function safeToUnicode($safe)
136*d868eb89SAndreas Gohr    {
137dcee16d6SChristopher Smith
13824870174SAndreas Gohr        $unicode = [];
139dcee16d6SChristopher Smith        $split = preg_split('#(?=['.self::$post_indicator.self::$pre_indicator.'])#', $safe, -1, PREG_SPLIT_NO_EMPTY);
14070e083ceSChristopher Smith
14170e083ceSChristopher Smith        $converted = false;
14270e083ceSChristopher Smith        foreach ($split as $sub) {
14363703ba5SAndreas Gohr            $len = strlen($sub);
144dcee16d6SChristopher Smith            if ($sub[0] != self::$pre_indicator) {
145dcee16d6SChristopher Smith                // plain (unconverted) characters, optionally starting with a post_indicator
146dcee16d6SChristopher Smith                // set initial value to skip any post_indicator
14763703ba5SAndreas Gohr                for ($i=($converted?1:0); $i < $len; $i++) {
14870e083ceSChristopher Smith                    $unicode[] = ord($sub[$i]);
14970e083ceSChristopher Smith                }
150dcee16d6SChristopher Smith                $converted = false;
15163703ba5SAndreas Gohr            } elseif ($len==1) {
152dcee16d6SChristopher Smith                // a pre_indicator character in the real data
15370e083ceSChristopher Smith                $unicode[] = ord($sub);
15470e083ceSChristopher Smith                $converted = true;
155dcee16d6SChristopher Smith            } else {
156dcee16d6SChristopher Smith                // a single codepoint in base36, adjusted for initial 32 non-printable chars
157dcee16d6SChristopher Smith                $unicode[] = 32 + (int)base_convert(substr($sub, 1), 36, 10);
158dcee16d6SChristopher Smith                $converted = true;
15970e083ceSChristopher Smith            }
16070e083ceSChristopher Smith        }
16170e083ceSChristopher Smith
16270e083ceSChristopher Smith        return $unicode;
16370e083ceSChristopher Smith    }
16470e083ceSChristopher Smith}
165