xref: /dokuwiki/inc/SafeFN.class.php (revision dcee16d694a9c39beaca3c59d970652f528a999c)
170e083ceSChristopher Smith<?php
270e083ceSChristopher Smith
370e083ceSChristopher Smith/**
470e083ceSChristopher Smith *  Class to safely store UTF-8 in a Filename
570e083ceSChristopher Smith *
670e083ceSChristopher Smith *  Encodes a utf8 string using only the following characters 0-9a-z_.-%
770e083ceSChristopher Smith *  characters 0-9a-z in the original string are preserved, "plain".
870e083ceSChristopher Smith *  all other characters are represented in a substring that starts
970e083ceSChristopher Smith *  with '%' are "converted".
1070e083ceSChristopher Smith *  The transition from converted substrings to plain characters is
1170e083ceSChristopher Smith *  marked with a '.'
1270e083ceSChristopher Smith *
1370e083ceSChristopher Smith *  @author   Christopher Smith
1470e083ceSChristopher Smith *  @date     2010-04-02
1570e083ceSChristopher Smith */
1670e083ceSChristopher Smithclass SafeFN {
1770e083ceSChristopher Smith
18*dcee16d6SChristopher Smith    // 'safe' characters are a superset of $plain, $pre_indicator and $post_indicator
19*dcee16d6SChristopher Smith    private static $plain = '-/_0123456789abcdefghijklmnopqrstuvwxyz'; // these characters aren't converted
2070e083ceSChristopher Smith    private static $pre_indicator = '%';
21*dcee16d6SChristopher Smith    private static $post_indicator = '.';
2270e083ceSChristopher Smith
2370e083ceSChristopher Smith    /**
2470e083ceSChristopher Smith     * Convert an UTF-8 string to a safe ASCII String
2570e083ceSChristopher Smith     *
2670e083ceSChristopher Smith     *  conversion process
27*dcee16d6SChristopher Smith     *    - if codepoint is a plain or post_indicator character,
28*dcee16d6SChristopher Smith     *      - if previous character was "converted", append post_indicator to output, clear "converted" flag
29*dcee16d6SChristopher Smith     *      - append ascii byte for character to output
30*dcee16d6SChristopher Smith     *      (continue to next character)
3170e083ceSChristopher Smith     *
32*dcee16d6SChristopher Smith     *    - if codepoint is a pre_indicator character,
33*dcee16d6SChristopher Smith     *      - append ascii byte for character to output, set "converted" flag
34*dcee16d6SChristopher Smith     *      (continue to next character)
35*dcee16d6SChristopher Smith     *
36*dcee16d6SChristopher Smith     *    (all remaining characters)
37*dcee16d6SChristopher Smith     *    - reduce codepoint value for non-printable ASCII characters (0x00 - 0x1f).  Space becomes our zero.
38*dcee16d6SChristopher Smith     *    - convert reduced value to base36 (0-9a-z)
39*dcee16d6SChristopher Smith     *    - append $pre_indicator characater followed by base36 string to output, set converted flag
40*dcee16d6SChristopher Smith     *      continue to next character)
41*dcee16d6SChristopher Smith     *
42*dcee16d6SChristopher Smith     * @param    string    $filename     a utf8 string, should only include printable characters - not 0x00-0x1f
43*dcee16d6SChristopher Smith     * @return   string    an encoded representation of $filename using only 'safe' ASCII characters
44*dcee16d6SChristopher Smith     *
45*dcee16d6SChristopher Smith     * @author   Christopher Smith <chris@jalakai.co.uk>
4670e083ceSChristopher Smith     */
47*dcee16d6SChristopher Smith    public function encode($filename) {
48*dcee16d6SChristopher Smith        return self::unicode_to_safe(utf8_to_unicode($filename));
4970e083ceSChristopher Smith    }
5070e083ceSChristopher Smith
5170e083ceSChristopher Smith    /**
5270e083ceSChristopher Smith     *  decoding process
53*dcee16d6SChristopher Smith     *    - split the string into substrings at any occurrence of pre or post indicator characters
5470e083ceSChristopher Smith     *    - check the first character of the substring
55*dcee16d6SChristopher Smith     *      - if its not a pre_indicator character
56*dcee16d6SChristopher Smith     *        - if previous character was converted, skip over post_indicator character
57*dcee16d6SChristopher Smith     *        - copy codepoint values of remaining characters to the output array
58*dcee16d6SChristopher Smith     *        - clear any converted flag
59*dcee16d6SChristopher Smith     *      (continue to next substring)
60*dcee16d6SChristopher Smith     *
61*dcee16d6SChristopher Smith     *     _ else (its a pre_indicator character)
62*dcee16d6SChristopher Smith     *       - if string length is 1, copy the post_indicator character to the output array
63*dcee16d6SChristopher Smith     *       (continue to next substring)
64*dcee16d6SChristopher Smith     *
65*dcee16d6SChristopher Smith     *       - else (string length > 1)
66*dcee16d6SChristopher Smith     *         - skip the pre-indicator character and convert remaining string from base36 to base10
67*dcee16d6SChristopher Smith     *         - increase codepoint value for non-printable ASCII characters (add 0x20)
68*dcee16d6SChristopher Smith     *         - append codepoint to output array
69*dcee16d6SChristopher Smith     *       (continue to next substring)
70*dcee16d6SChristopher Smith     *
71*dcee16d6SChristopher Smith     * @param    string    $filename     a 'safe' encoded ASCII string,
72*dcee16d6SChristopher Smith     * @return   string    decoded utf8 representation of $filename
73*dcee16d6SChristopher Smith     *
74*dcee16d6SChristopher Smith     * @author   Christopher Smith <chris@jalakai.co.uk>
7570e083ceSChristopher Smith     */
76*dcee16d6SChristopher Smith    public function decode($filename) {
77*dcee16d6SChristopher Smith        return unicode_to_utf8(self::safe_to_unicode(strtolower($filename)));
7870e083ceSChristopher Smith    }
7970e083ceSChristopher Smith
8070e083ceSChristopher Smith    public function validate_printable_utf8($printable_utf8) {
81*dcee16d6SChristopher Smith        return !preg_match('#[\x01-\x1f]#',$printable_utf8);
8270e083ceSChristopher Smith    }
8370e083ceSChristopher Smith
8470e083ceSChristopher Smith    public function validate_safe($safe) {
85*dcee16d6SChristopher Smith        return !preg_match('#[^'.self::$plain.self::$post_indicator.self::$pre_indicator.']#',$safe);
8670e083ceSChristopher Smith    }
8770e083ceSChristopher Smith
88*dcee16d6SChristopher Smith    /**
89*dcee16d6SChristopher Smith     * convert an array of unicode codepoints into 'safe_filename' format
90*dcee16d6SChristopher Smith     *
91*dcee16d6SChristopher Smith     * @param    array  int    $unicode    an array of unicode codepoints
92*dcee16d6SChristopher Smith     * @return   string        the unicode represented in 'safe_filename' format
93*dcee16d6SChristopher Smith     *
94*dcee16d6SChristopher Smith     * @author   Christopher Smith <chris@jalakai.co.uk>
95*dcee16d6SChristopher Smith     */
96*dcee16d6SChristopher Smith    private function unicode_to_safe($unicode) {
9770e083ceSChristopher Smith
9870e083ceSChristopher Smith        $safe = '';
9970e083ceSChristopher Smith        $converted = false;
10070e083ceSChristopher Smith
10170e083ceSChristopher Smith        foreach ($unicode as $codepoint) {
102*dcee16d6SChristopher Smith            if ($codepoint < 127 && (strpos(self::$plain.self::$post_indicator,chr($codepoint))!==false)) {
10370e083ceSChristopher Smith                if ($converted) {
10470e083ceSChristopher Smith                    $safe .= self::$post_indicator;
10570e083ceSChristopher Smith                    $converted = false;
10670e083ceSChristopher Smith                }
10770e083ceSChristopher Smith                $safe .= chr($codepoint);
10870e083ceSChristopher Smith
109*dcee16d6SChristopher Smith            } else if ($codepoint == ord(self::$pre_indicator)) {
110*dcee16d6SChristopher Smith                $safe .= self::$pre_indicator;
11170e083ceSChristopher Smith                $converted = true;
11270e083ceSChristopher Smith            } else {
113*dcee16d6SChristopher Smith                $safe .= self::$pre_indicator.base_convert((string)($codepoint-32),10,36);
11470e083ceSChristopher Smith                $converted = true;
11570e083ceSChristopher Smith            }
11670e083ceSChristopher Smith        }
11770e083ceSChristopher Smith        return $safe;
11870e083ceSChristopher Smith    }
11970e083ceSChristopher Smith
120*dcee16d6SChristopher Smith    /**
121*dcee16d6SChristopher Smith     * convert a 'safe_filename' string into an array of unicode codepoints
122*dcee16d6SChristopher Smith     *
123*dcee16d6SChristopher Smith     * @param   string         $safe     a filename in 'safe_filename' format
124*dcee16d6SChristopher Smith     * @return  array   int    an array of unicode codepoints
125*dcee16d6SChristopher Smith     *
126*dcee16d6SChristopher Smith     * @author   Christopher Smith <chris@jalakai.co.uk>
127*dcee16d6SChristopher Smith     */
128*dcee16d6SChristopher Smith    private function safe_to_unicode($safe) {
129*dcee16d6SChristopher Smith
13070e083ceSChristopher Smith        $unicode = array();
131*dcee16d6SChristopher Smith        $split = preg_split('#(?=['.self::$post_indicator.self::$pre_indicator.'])#',$safe,-1,PREG_SPLIT_NO_EMPTY);
13270e083ceSChristopher Smith
13370e083ceSChristopher Smith        $converted = false;
13470e083ceSChristopher Smith        foreach ($split as $sub) {
135*dcee16d6SChristopher Smith            if ($sub[0] != self::$pre_indicator) {
136*dcee16d6SChristopher Smith                // plain (unconverted) characters, optionally starting with a post_indicator
137*dcee16d6SChristopher Smith                // set initial value to skip any post_indicator
138*dcee16d6SChristopher Smith                for ($i=($converted?1:0); $i < strlen($sub); $i++) {
13970e083ceSChristopher Smith                    $unicode[] = ord($sub[$i]);
14070e083ceSChristopher Smith                }
141*dcee16d6SChristopher Smith                $converted = false;
14270e083ceSChristopher Smith            } else if (strlen($sub)==1) {
143*dcee16d6SChristopher Smith                // a pre_indicator character in the real data
14470e083ceSChristopher Smith                $unicode[] = ord($sub);
14570e083ceSChristopher Smith                $converted = true;
146*dcee16d6SChristopher Smith            } else {
147*dcee16d6SChristopher Smith                // a single codepoint in base36, adjusted for initial 32 non-printable chars
148*dcee16d6SChristopher Smith                $unicode[] = 32 + (int)base_convert(substr($sub,1),36,10);
149*dcee16d6SChristopher Smith                $converted = true;
15070e083ceSChristopher Smith            }
15170e083ceSChristopher Smith        }
15270e083ceSChristopher Smith
15370e083ceSChristopher Smith        return $unicode;
15470e083ceSChristopher Smith    }
15570e083ceSChristopher Smith
15670e083ceSChristopher Smith}
157