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' => array( |
16
|
|
|
* 'Plugin.Normalizer.cmdPreprocess' |
17
|
|
|
* ), |
18
|
|
|
* 'upload.presave' => array( |
19
|
|
|
* 'Plugin.Normalizer.onUpLoadPreSave' |
20
|
|
|
* ) |
21
|
|
|
* ), |
22
|
|
|
* // global configure (optional) |
23
|
|
|
* 'plugin' => array( |
24
|
|
|
* 'Normalizer' => array( |
25
|
|
|
* 'enable' => true, |
26
|
|
|
* 'nfc' => true, |
27
|
|
|
* 'nfkc' => true, |
28
|
|
|
* 'lowercase' => false, |
29
|
|
|
* 'convmap' => array() |
30
|
|
|
* ) |
31
|
|
|
* ), |
32
|
|
|
* // each volume configure (optional) |
33
|
|
|
* 'roots' => array( |
34
|
|
|
* array( |
35
|
|
|
* 'driver' => 'LocalFileSystem', |
36
|
|
|
* 'path' => '/path/to/files/', |
37
|
|
|
* 'URL' => 'http://localhost/to/files/' |
38
|
|
|
* 'plugin' => array( |
39
|
|
|
* 'Normalizer' => array( |
40
|
|
|
* 'enable' => true, |
41
|
|
|
* 'nfc' => true, |
42
|
|
|
* 'nfkc' => true, |
43
|
|
|
* 'lowercase' => false, |
44
|
|
|
* 'convmap' => array() |
45
|
|
|
* ) |
46
|
|
|
* ) |
47
|
|
|
* ) |
48
|
|
|
* ) |
49
|
|
|
* ); |
50
|
|
|
* |
51
|
|
|
* @package elfinder |
52
|
|
|
* @author Naoki Sawada |
53
|
|
|
* @license New BSD |
54
|
|
|
*/ |
55
|
|
|
class elFinderPluginNormalizer |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
private $opts = array(); |
58
|
|
|
|
59
|
|
|
public function __construct($opts) { |
60
|
|
|
$defaults = array( |
61
|
|
|
'enable' => true, // For control by volume driver |
62
|
|
|
'nfc' => true, // Canonical Decomposition followed by Canonical Composition |
63
|
|
|
'nfkc' => true, // Compatibility Decomposition followed by Canonical |
64
|
|
|
'lowercase' => false, // Make chars lowercase |
65
|
|
|
'convmap' => array()// Convert map ('FROM' => 'TO') array |
|
|
|
|
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$this->opts = array_merge($defaults, $opts); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function cmdPreprocess($cmd, &$args, $elfinder, $volume) { |
|
|
|
|
72
|
|
|
$opts = $this->getOpts($volume); |
73
|
|
|
if (! $opts['enable']) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (isset($args['name'])) { |
78
|
|
|
if (is_array($args['name'])) { |
79
|
|
|
foreach($args['name'] as $i => $name) { |
80
|
|
|
$args['name'][$i] = $this->normalize($name, $opts); |
81
|
|
|
} |
82
|
|
|
} else { |
83
|
|
|
$args['name'] = $this->normalize($args['name'], $opts); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function onUpLoadPreSave(&$path, &$name, $src, $elfinder, $volume) { |
|
|
|
|
90
|
|
|
$opts = $this->getOpts($volume); |
91
|
|
|
if (! $opts['enable']) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($path) { |
96
|
|
|
$path = $this->normalize($path, $opts); |
97
|
|
|
} |
98
|
|
|
$name = $this->normalize($name, $opts); |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
private function getOpts($volume) { |
|
|
|
|
103
|
|
|
$opts = $this->opts; |
104
|
|
|
if (is_object($volume)) { |
105
|
|
|
$volOpts = $volume->getOptionsPlugin('Normalizer'); |
106
|
|
|
if (is_array($volOpts)) { |
107
|
|
|
$opts = array_merge($this->opts, $volOpts); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
return $opts; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function normalize($str, $opts) { |
114
|
|
|
if ($opts['nfc'] || $opts['nfkc']) { |
115
|
|
|
if (class_exists('Normalizer', false)) { |
116
|
|
View Code Duplication |
if ($opts['nfc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_C)) |
|
|
|
|
117
|
|
|
$str = Normalizer::normalize($str, Normalizer::FORM_C); |
118
|
|
View Code Duplication |
if ($opts['nfkc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_KC)) |
|
|
|
|
119
|
|
|
$str = Normalizer::normalize($str, Normalizer::FORM_KC); |
120
|
|
|
} else { |
121
|
|
|
if (! class_exists('I18N_UnicodeNormalizer', false)) { |
122
|
|
|
@ include_once 'I18N/UnicodeNormalizer.php'; |
|
|
|
|
123
|
|
|
} |
124
|
|
|
if (class_exists('I18N_UnicodeNormalizer', false)) { |
125
|
|
|
$normalizer = new I18N_UnicodeNormalizer(); |
126
|
|
|
if ($opts['nfc']) |
127
|
|
|
$str = $normalizer->normalize($str, 'NFC'); |
128
|
|
|
if ($opts['nfkc']) |
129
|
|
|
$str = $normalizer->normalize($str, 'NFKC'); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
if ($opts['lowercase']) { |
134
|
|
|
$str = strtolower($str); |
135
|
|
|
} |
136
|
|
|
if ($opts['convmap'] && is_array($opts['convmap'])) { |
137
|
|
|
$str = strtr($str, $opts['convmap']); |
138
|
|
|
} |
139
|
|
|
return $str; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.