Completed
Push — 2.x ( 82b028...ec6e0b )
by Naoki
04:23
created

elFinderPluginNormalizer::cmdPreprocess()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %
Metric Value
dl 17
loc 17
rs 8.8571
cc 5
eloc 11
nc 4
nop 4
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
 *			)
30
 *		),
31
 *		// each volume configure (optional)
32
 *		'roots' => array(
33
 *			array(
34
 *				'driver' => 'LocalFileSystem',
35
 *				'path'   => '/path/to/files/',
36
 *				'URL'    => 'http://localhost/to/files/'
37
 *				'plugin' => array(
38
 *					'Normalizer' => array(
39
 *						'enable' => true,
40
 *						'nfc'    => true,
41
 *						'nfkc'   => true,
42
 * 						'lowercase'   => false
43
 *					)
44
 *				)
45
 *			)
46
 *		)
47
 *	);
48
 *
49
 * @package elfinder
50
 * @author Naoki Sawada
51
 * @license New BSD
52
 */
53
class elFinderPluginNormalizer
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...
54
{
55
	private $opts = array();
56
	
57
	public function __construct($opts) {
58
		$defaults = array(
59
			'enable' => true, // For control by volume driver
60
			'nfc'    => true, // Canonical Decomposition followed by Canonical Composition
61
			'nfkc'   => true,  // Compatibility Decomposition followed by Canonical
62
			'lowercase'   => false  // Make chars lowercase
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 $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...
69
		$opts = $this->getOpts($volume);
70
		if (! $opts['enable']) {
71
			return false;
72
		}
73
		
74
		if (isset($args['name'])) {
75
			if (is_array($args['name'])) {
76
				foreach($args['name'] as $i => $name) {
77
					$args['name'][$i] = $this->normalize($name, $opts);
78
				}
79
			} else {
80
				$args['name'] = $this->normalize($args['name'], $opts);
81
			}
82
		}
83
		return true;
84
	}
85
	
86 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...
87
		$opts = $this->getOpts($volume);
88
		if (! $opts['enable']) {
89
			return false;
90
		}
91
		
92
		if ($path) {
93
			$path = $this->normalize($path, $opts);
94
		}
95
		$name = $this->normalize($name, $opts);
96
		return true;
97
	}
98
	
99 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...
100
		$opts = $this->opts;
101
		if (is_object($volume)) {
102
			$volOpts = $volume->getOptionsPlugin('Normalizer');
103
			if (is_array($volOpts)) {
104
				$opts = array_merge($this->opts, $volOpts);
105
			}
106
		}
107
		return $opts;
108
	}
109
	
110
	private function normalize($str, $opts) {
111
		if ($opts['nfc'] || $opts['nfkc']) {
112
			if (class_exists('Normalizer')) {
113 View Code Duplication
				if ($opts['nfc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_C))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
114
					$str = Normalizer::normalize($str, Normalizer::FORM_C);
115 View Code Duplication
				if ($opts['nfkc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_KC))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
116
					$str = Normalizer::normalize($str, Normalizer::FORM_KC);
117
			} else {
118
				if (! class_exists('I18N_UnicodeNormalizer')) {
119
					@ include_once 'I18N/UnicodeNormalizer.php';
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
120
				}
121
				if (class_exists('I18N_UnicodeNormalizer')) {
122
					$normalizer = new I18N_UnicodeNormalizer();
123
					if ($opts['nfc'])
124
						$str = $normalizer->normalize($str, 'NFC');
125
					if ($opts['nfkc'])
126
						$str = $normalizer->normalize($str, 'NFKC');
127
				}
128
			}
129
		}
130
		if ($opts['lowercase']) {
131
			$str = strtolower($str);
132
		}
133
		return $str;
134
	}
135
}
136