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