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' => array( |
11
|
|
|
* 'Plugin.Sanitizer.cmdPreprocess' |
12
|
|
|
* ), |
13
|
|
|
* 'upload.presave' => array( |
14
|
|
|
* 'Plugin.Sanitizer.onUpLoadPreSave' |
15
|
|
|
* ) |
16
|
|
|
* ), |
17
|
|
|
* // global configure (optional) |
18
|
|
|
* 'plugin' => array( |
19
|
|
|
* 'Sanitizer' => array( |
20
|
|
|
* 'enable' => true, |
21
|
|
|
* 'targets' => array('\\','/',':','*','?','"','<','>','|'), // target chars |
22
|
|
|
* 'replace' => '_' // replace to this |
23
|
|
|
* ) |
24
|
|
|
* ), |
25
|
|
|
* // each volume configure (optional) |
26
|
|
|
* 'roots' => array( |
27
|
|
|
* array( |
28
|
|
|
* 'driver' => 'LocalFileSystem', |
29
|
|
|
* 'path' => '/path/to/files/', |
30
|
|
|
* 'URL' => 'http://localhost/to/files/' |
31
|
|
|
* 'plugin' => array( |
32
|
|
|
* 'Sanitizer' => array( |
33
|
|
|
* 'enable' => true, |
34
|
|
|
* 'targets' => array('\\','/',':','*','?','"','<','>','|'), // target chars |
35
|
|
|
* 'replace' => '_' // replace to this |
36
|
|
|
* ) |
37
|
|
|
* ) |
38
|
|
|
* ) |
39
|
|
|
* ) |
40
|
|
|
* ); |
41
|
|
|
* |
42
|
|
|
* @package elfinder |
43
|
|
|
* @author Naoki Sawada |
44
|
|
|
* @license New BSD |
45
|
|
|
*/ |
46
|
|
|
class elFinderPluginSanitizer |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
private $opts = array(); |
49
|
|
|
|
50
|
|
|
public function __construct($opts) { |
51
|
|
|
$defaults = array( |
52
|
|
|
'enable' => true, // For control by volume driver |
53
|
|
|
'targets' => array('\\','/',':','*','?','"','<','>','|'), // target chars |
54
|
|
|
'replace' => '_' // replace to this |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$this->opts = array_merge($defaults, $opts); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function cmdPreprocess($cmd, &$args, $elfinder, $volume) { |
|
|
|
|
61
|
|
|
$opts = $this->getOpts($volume); |
62
|
|
|
if (! $opts['enable']) { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (isset($args['name'])) { |
67
|
|
|
if (is_array($args['name'])) { |
68
|
|
|
foreach($args['name'] as $i => $name) { |
69
|
|
|
$args['name'][$i] = $this->sanitizeFileName($name, $opts); |
70
|
|
|
} |
71
|
|
|
} else { |
72
|
|
|
$args['name'] = $this->sanitizeFileName($args['name'], $opts); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function onUpLoadPreSave(&$path, &$name, $src, $elfinder, $volume) { |
|
|
|
|
79
|
|
|
$opts = $this->getOpts($volume); |
80
|
|
|
if (! $opts['enable']) { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($path) { |
85
|
|
|
$path = $this->sanitizeFileName($path, $opts, array('/')); |
86
|
|
|
} |
87
|
|
|
$name = $this->sanitizeFileName($name, $opts); |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
private function getOpts($volume) { |
|
|
|
|
92
|
|
|
$opts = $this->opts; |
93
|
|
|
if (is_object($volume)) { |
94
|
|
|
$volOpts = $volume->getOptionsPlugin('Sanitizer'); |
95
|
|
|
if (is_array($volOpts)) { |
96
|
|
|
$opts = array_merge($this->opts, $volOpts); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return $opts; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function sanitizeFileName($filename, $opts, $allows = array()) { |
103
|
|
|
$targets = $allows? array_diff($opts['targets'], $allows) : $opts['targets']; |
104
|
|
|
return str_replace($targets, $opts['replace'], $filename); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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.