1 | <?php |
||
13 | class ODS implements EscaperInterface |
||
14 | { |
||
15 | use Singleton; |
||
16 | |||
17 | /** |
||
18 | * Escapes the given string to make it compatible with XLSX |
||
19 | * |
||
20 | * @param string $string The string to escape |
||
21 | * @return string The escaped string |
||
22 | */ |
||
23 | 41 | public function escape($string) |
|
24 | { |
||
25 | 41 | if (defined('ENT_DISALLOWED')) { |
|
26 | // 'ENT_DISALLOWED' ensures that invalid characters in the given document type are replaced. |
||
27 | // Otherwise control characters like a vertical tab "\v" will make the XML document unreadable by the XML processor |
||
28 | // @link https://github.com/box/spout/issues/329 |
||
29 | 41 | $replacedString = htmlspecialchars($string, ENT_NOQUOTES | ENT_DISALLOWED); |
|
30 | } else { |
||
31 | // We are on hhvm or any other engine that does not support ENT_DISALLOWED. |
||
32 | // |
||
33 | // @NOTE: Using ENT_NOQUOTES as only XML entities ('<', '>', '&') need to be encoded. |
||
34 | // Single and double quotes can be left as is. |
||
35 | $escapedString = htmlspecialchars($string, ENT_NOQUOTES); |
||
36 | |||
37 | // control characters values are from 0 to 1F (hex values) in the ASCII table |
||
38 | // some characters should not be escaped though: "\t", "\r" and "\n". |
||
39 | $regexPattern = '[\x00-\x08' . |
||
40 | // skipping "\t" (0x9) and "\n" (0xA) |
||
41 | '\x0B-\x0C' . |
||
42 | // skipping "\r" (0xD) |
||
43 | '\x0E-\x1F]'; |
||
44 | $replacedString = preg_replace("/$regexPattern/", '�', $escapedString); |
||
45 | } |
||
46 | |||
47 | 41 | return $replacedString; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * Unescapes the given string to make it compatible with XLSX |
||
52 | * |
||
53 | * @param string $string The string to unescape |
||
54 | * @return string The unescaped string |
||
55 | */ |
||
56 | 28 | public function unescape($string) |
|
66 | } |
||
67 |