elFinderPluginSanitizer   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 61
Duplicated Lines 63.93 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 39
loc 61
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B cmdPreprocess() 17 17 5
A onUpLoadPreSave() 12 12 3
A getOpts() 10 10 3
A sanitizeFileName() 0 4 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
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...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $cmd 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...
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) {
0 ignored issues
show
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...
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) {
0 ignored issues
show
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
		$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