1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* elFinder Plugin Normalizer. |
4
|
|
|
* |
5
|
|
|
* UTF-8 Normalizer of file-name and file-path etc. |
6
|
|
|
* nfc(NFC): Canonical Decomposition followed by Canonical Composition |
7
|
|
|
* nfkc(NFKC): Compatibility Decomposition followed by Canonical |
8
|
|
|
* |
9
|
|
|
* This plugin require Class "Normalizer" (PHP 5 >= 5.3.0, PECL intl >= 1.0.0) |
10
|
|
|
* or PEAR package "I18N_UnicodeNormalizer" |
11
|
|
|
* |
12
|
|
|
* ex. binding, configure on connector options |
13
|
|
|
* $opts = array( |
14
|
|
|
* 'bind' => array( |
15
|
|
|
* 'upload.pre mkdir.pre mkfile.pre rename.pre archive.pre ls.pre' => array( |
16
|
|
|
* 'Plugin.Normalizer.cmdPreprocess' |
17
|
|
|
* ), |
18
|
|
|
* 'ls' => array( |
19
|
|
|
* 'Plugin.Normalizer.cmdPostprocess' |
20
|
|
|
* ), |
21
|
|
|
* 'upload.presave' => array( |
22
|
|
|
* 'Plugin.Normalizer.onUpLoadPreSave' |
23
|
|
|
* ) |
24
|
|
|
* ), |
25
|
|
|
* // global configure (optional) |
26
|
|
|
* 'plugin' => array( |
27
|
|
|
* 'Normalizer' => array( |
28
|
|
|
* 'enable' => true, |
29
|
|
|
* 'nfc' => true, |
30
|
|
|
* 'nfkc' => true, |
31
|
|
|
* 'umlauts' => false, |
32
|
|
|
* 'lowercase' => false, |
33
|
|
|
* 'convmap' => array() |
34
|
|
|
* ) |
35
|
|
|
* ), |
36
|
|
|
* // each volume configure (optional) |
37
|
|
|
* 'roots' => array( |
38
|
|
|
* array( |
39
|
|
|
* 'driver' => 'LocalFileSystem', |
40
|
|
|
* 'path' => '/path/to/files/', |
41
|
|
|
* 'URL' => 'http://localhost/to/files/' |
42
|
|
|
* 'plugin' => array( |
43
|
|
|
* 'Normalizer' => array( |
44
|
|
|
* 'enable' => true, |
45
|
|
|
* 'nfc' => true, |
46
|
|
|
* 'nfkc' => true, |
47
|
|
|
* 'umlauts' => false, |
48
|
|
|
* 'lowercase' => false, |
49
|
|
|
* 'convmap' => array() |
50
|
|
|
* ) |
51
|
|
|
* ) |
52
|
|
|
* ) |
53
|
|
|
* ) |
54
|
|
|
* ); |
55
|
|
|
* |
56
|
|
|
* @author Naoki Sawada |
57
|
|
|
* @license New BSD |
58
|
|
|
*/ |
59
|
|
|
class elFinderPluginNormalizer extends elFinderPlugin |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
private $replaced = []; |
62
|
|
|
private $keyMap = [ |
63
|
|
|
'ls' => 'intersect', |
64
|
|
|
'upload' => 'renames', |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
public function __construct($opts) |
68
|
|
|
{ |
69
|
|
|
$defaults = [ |
70
|
|
|
'enable' => true, // For control by volume driver |
71
|
|
|
'nfc' => true, // Canonical Decomposition followed by Canonical Composition |
72
|
|
|
'nfkc' => true, // Compatibility Decomposition followed by Canonical |
73
|
|
|
'umlauts' => false, // Convert umlauts with their closest 7 bit ascii equivalent |
74
|
|
|
'lowercase' => false, // Make chars lowercase |
75
|
|
|
'convmap' => [], // Convert map ('FROM' => 'TO') array |
|
|
|
|
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$this->opts = array_merge($defaults, $opts); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
public function cmdPreprocess($cmd, &$args, $elfinder, $volume) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$opts = $this->getCurrentOpts($volume); |
84
|
|
|
if (! $opts['enable']) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
$this->replaced[$cmd] = []; |
88
|
|
|
$key = (isset($this->keyMap[$cmd])) ? $this->keyMap[$cmd] : 'name'; |
89
|
|
|
|
90
|
|
|
if (isset($args[$key])) { |
91
|
|
|
if (is_array($args[$key])) { |
92
|
|
|
foreach ($args[$key] as $i => $name) { |
93
|
|
|
$this->replaced[$cmd][$name] = $args[$key][$i] = $this->normalize($name, $opts); |
94
|
|
|
} |
95
|
|
|
} else { |
96
|
|
|
$name = $args[$key]; |
97
|
|
|
$this->replaced[$cmd][$name] = $args[$key] = $this->normalize($name, $opts); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function cmdPostprocess($cmd, &$result, $args, $elfinder) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
if ($cmd === 'ls') { |
107
|
|
|
if (! empty($result['list']) && ! empty($this->replaced['ls'])) { |
108
|
|
|
foreach ($result['list'] as $hash => $name) { |
109
|
|
|
if ($keys = array_keys($this->replaced['ls'], $name)) { |
110
|
|
|
if (count($keys) === 1) { |
111
|
|
|
$result['list'][$hash] = $keys[0]; |
112
|
|
|
} else { |
113
|
|
|
$result['list'][$hash] = $keys; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// NOTE: $thash is directory hash so it unneed to process at here |
122
|
|
View Code Duplication |
public function onUpLoadPreSave(&$thash, &$name, $src, $elfinder, $volume) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$opts = $this->getCurrentOpts($volume); |
125
|
|
|
if (! $opts['enable']) { |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$name = $this->normalize($name, $opts); |
130
|
|
|
|
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function normalize($str, $opts) |
135
|
|
|
{ |
136
|
|
|
if ($opts['nfc'] || $opts['nfkc']) { |
137
|
|
|
if (class_exists('Normalizer', false)) { |
138
|
|
View Code Duplication |
if ($opts['nfc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_C)) { |
|
|
|
|
139
|
|
|
$str = Normalizer::normalize($str, Normalizer::FORM_C); |
140
|
|
|
} |
141
|
|
View Code Duplication |
if ($opts['nfkc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_KC)) { |
|
|
|
|
142
|
|
|
$str = Normalizer::normalize($str, Normalizer::FORM_KC); |
143
|
|
|
} |
144
|
|
|
} else { |
145
|
|
|
if (! class_exists('I18N_UnicodeNormalizer', false)) { |
146
|
|
|
include_once 'I18N/UnicodeNormalizer.php'; |
147
|
|
|
} |
148
|
|
|
if (class_exists('I18N_UnicodeNormalizer', false)) { |
149
|
|
|
$normalizer = new I18N_UnicodeNormalizer(); |
150
|
|
|
if ($opts['nfc']) { |
151
|
|
|
$str = $normalizer->normalize($str, 'NFC'); |
152
|
|
|
} |
153
|
|
|
if ($opts['nfkc']) { |
154
|
|
|
$str = $normalizer->normalize($str, 'NFKC'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
if ($opts['umlauts']) { |
160
|
|
|
if (strpos($str = htmlentities($str, ENT_QUOTES, 'UTF-8'), '&') !== false) { |
161
|
|
|
$str = html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~i', '$1', $str), ENT_QUOTES, 'utf-8'); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
if ($opts['convmap'] && is_array($opts['convmap'])) { |
165
|
|
|
$str = strtr($str, $opts['convmap']); |
166
|
|
|
} |
167
|
|
|
if ($opts['lowercase']) { |
168
|
|
|
if (function_exists('mb_strtolower')) { |
169
|
|
|
$str = mb_strtolower($str, 'UTF-8'); |
170
|
|
|
} else { |
171
|
|
|
$str = strtolower($str); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $str; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.