1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* elFinder Plugin Sanitizer. |
4
|
|
|
* |
5
|
|
|
* Sanitizer of file-name and file-path etc. |
6
|
|
|
* |
7
|
|
|
* ex. binding, configure on connector options |
8
|
|
|
* $opts = array( |
9
|
|
|
* 'bind' => array( |
10
|
|
|
* 'upload.pre mkdir.pre mkfile.pre rename.pre archive.pre ls.pre' => array( |
11
|
|
|
* 'Plugin.Sanitizer.cmdPreprocess' |
12
|
|
|
* ), |
13
|
|
|
* 'ls' => array( |
14
|
|
|
* 'Plugin.Sanitizer.cmdPostprocess' |
15
|
|
|
* ), |
16
|
|
|
* 'upload.presave' => array( |
17
|
|
|
* 'Plugin.Sanitizer.onUpLoadPreSave' |
18
|
|
|
* ) |
19
|
|
|
* ), |
20
|
|
|
* // global configure (optional) |
21
|
|
|
* 'plugin' => array( |
22
|
|
|
* 'Sanitizer' => array( |
23
|
|
|
* 'enable' => true, |
24
|
|
|
* 'targets' => array('\\','/',':','*','?','"','<','>','|'), // target chars |
25
|
|
|
* 'replace' => '_' // replace to this |
26
|
|
|
* ) |
27
|
|
|
* ), |
28
|
|
|
* // each volume configure (optional) |
29
|
|
|
* 'roots' => array( |
30
|
|
|
* array( |
31
|
|
|
* 'driver' => 'LocalFileSystem', |
32
|
|
|
* 'path' => '/path/to/files/', |
33
|
|
|
* 'URL' => 'http://localhost/to/files/' |
34
|
|
|
* 'plugin' => array( |
35
|
|
|
* 'Sanitizer' => array( |
36
|
|
|
* 'enable' => true, |
37
|
|
|
* 'targets' => array('\\','/',':','*','?','"','<','>','|'), // target chars |
38
|
|
|
* 'replace' => '_' // replace to this |
39
|
|
|
* ) |
40
|
|
|
* ) |
41
|
|
|
* ) |
42
|
|
|
* ) |
43
|
|
|
* ); |
44
|
|
|
* |
45
|
|
|
* @author Naoki Sawada |
46
|
|
|
* @license New BSD |
47
|
|
|
*/ |
48
|
|
|
class elFinderPluginSanitizer extends elFinderPlugin |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
private $replaced = []; |
51
|
|
|
private $keyMap = [ |
52
|
|
|
'ls' => 'intersect', |
53
|
|
|
'upload' => 'renames', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
public function __construct($opts) |
57
|
|
|
{ |
58
|
|
|
$defaults = [ |
59
|
|
|
'enable' => true, // For control by volume driver |
60
|
|
|
'targets' => ['\\', '/', ':', '*', '?', '"', '<', '>', '|'], // target chars |
61
|
|
|
'replace' => '_', // replace to this |
62
|
|
|
'pathAllows' => ['/'], // Characters allowed in path name of characters in `targets` array |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
$this->opts = array_merge($defaults, $opts); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function cmdPreprocess($cmd, &$args, $elfinder, $volume) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$opts = $this->getCurrentOpts($volume); |
71
|
|
|
if (! $opts['enable']) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
$this->replaced[$cmd] = []; |
75
|
|
|
$key = (isset($this->keyMap[$cmd])) ? $this->keyMap[$cmd] : 'name'; |
76
|
|
|
|
77
|
|
|
if (isset($args[$key])) { |
78
|
|
|
if (is_array($args[$key])) { |
79
|
|
|
foreach ($args[$key] as $i => $name) { |
80
|
|
|
$this->replaced[$cmd][$name] = $args[$key][$i] = $this->sanitizeFileName($name, $opts); |
81
|
|
|
} |
82
|
|
|
} else { |
83
|
|
|
$name = $args[$key]; |
84
|
|
|
$this->replaced[$cmd][$name] = $args[$key] = $this->sanitizeFileName($name, $opts); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
public function cmdPostprocess($cmd, &$result, $args, $elfinder) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
if ($cmd === 'ls') { |
94
|
|
|
if (! empty($result['list']) && ! empty($this->replaced['ls'])) { |
95
|
|
|
foreach ($result['list'] as $hash => $name) { |
96
|
|
|
if ($keys = array_keys($this->replaced['ls'], $name)) { |
97
|
|
|
if (count($keys) === 1) { |
98
|
|
|
$result['list'][$hash] = $keys[0]; |
99
|
|
|
} else { |
100
|
|
|
$result['list'][$hash] = $keys; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// NOTE: $thash is directory hash so it unneed to process at here |
109
|
|
View Code Duplication |
public function onUpLoadPreSave(&$thash, &$name, $src, $elfinder, $volume) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$opts = $this->getCurrentOpts($volume); |
112
|
|
|
if (! $opts['enable']) { |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$name = $this->sanitizeFileName($name, $opts); |
117
|
|
|
|
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function sanitizeFileName($filename, $opts, $allows = []) |
122
|
|
|
{ |
123
|
|
|
$targets = $allows ? array_diff($opts['targets'], $allows) : $opts['targets']; |
124
|
|
|
|
125
|
|
|
return str_replace($targets, $opts['replace'], $filename); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.