elFinderPluginSanitizer::cmdPostprocess()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 6
nop 4
dl 16
loc 16
rs 8.2222
c 0
b 0
f 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $elfinder is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $elfinder is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $thash is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $src is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $elfinder is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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