Completed
Pull Request — master (#347)
by Victor
02:27
created

FilesystemHelper::scandirFiltered()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace Owncloud\Updater\Utils;
23
24
class FilesystemHelper {
25
26
	/**
27
	 * Wrapper for filemtime function
28
	 * @param string $path
29
	 * @return integer
30
	 */
31
	public function filemtime($path){
32
		return filemtime($path);
33
	}
34
35
	/**
36
	 * Wrapper for scandir function. 
37
	 * Filters current and parent directories
38
	 * @param string $path
39
	 * @return array
40
	 */
41
	public function scandirFiltered($path){
42
		$content = $this->scandir($path);
43
		if (is_array($content)){
44
			return array_diff($content, ['.', '..']);
45
		}
46
		return [];
47
	}
48
49
	/**
50
	 * Wrapper for scandir function
51
	 * @param string $path
52
	 * @return array
53
	 */
54
	public function scandir($path){
55
		return scandir($path);
56
	}
57
58
	/**
59
	 * Wrapper for file_exists function
60
	 * @param string $path
61
	 * @return bool
62
	 */
63
	public function fileExists($path){
64
		return file_exists($path);
65
	}
66
67
	/**
68
	 * Wrapper for is_writable function
69
	 * @param string $path
70
	 * @return bool
71
	 */
72
	public function isWritable($path){
73
		return is_writable($path);
74
	}
75
76
	/**
77
	 * Wrapper for is_dir function
78
	 * @param string $path
79
	 * @return bool
80
	 */
81
	public function isDir($path){
82
		return is_dir($path);
83
	}
84
85
	/**
86
	 * Wrapper for md5_file function
87
	 * @param string $path
88
	 * @return string
89
	 */
90
	public function md5File($path){
91
		return md5_file($path);
92
	}
93
94
	/**
95
	 * Wrapper for mkdir
96
	 * @param string $path
97
	 * @param bool $isRecursive
98
	 * @throws \Exception on error
99
	 */
100
	public function mkdir($path, $isRecursive = false){
101
		if (!mkdir($path, 0755, $isRecursive)){
102
			throw new \Exception("Unable to create $path");
103
		}
104
	}
105
106
	/**
107
	 * Copy recursive
108
	 * @param string $src  - source path
109
	 * @param string $dest - destination path
110
	 * @throws \Exception on error
111
	 */
112
	public function copyr($src, $dest, $stopOnError = true){
113
		if (is_dir($src)){
114
			if (!is_dir($dest)){
115
				try{
116
					$this->mkdir($dest);
117
				} catch (\Exception $e){
118
					if ($stopOnError){
119
						throw $e;
120
					}
121
				}
122
			}
123
			$files = scandir($src);
124
			foreach ($files as $file){
125
				if (!in_array($file, [".", ".."])){
126
					$this->copyr("$src/$file", "$dest/$file", $stopOnError);
127
				}
128
			}
129
		} elseif (file_exists($src)){
130
			if (!copy($src, $dest) && $stopOnError){
131
				throw new \Exception("Unable to copy $src to $dest");
132
			}
133
		}
134
	}
135
136
	/**
137
	 * Moves file/directory
138
	 * @param string $src  - source path
139
	 * @param string $dest - destination path
140
	 * @throws \Exception on error
141
	 */
142
	public function move($src, $dest){
143
		if (!rename($src, $dest)){
144
			throw new \Exception("Unable to move $src to $dest");
145
		}
146
	}
147
148
	/**
149
	 * Check permissions recursive
150
	 * @param string $src  - path to check
151
	 * @param Collection $collection - object to store incorrect permissions
152
	 */
153
	public function checkr($src, $collection){
154
		if (!file_exists($src)){
155
			return;
156
		}
157
		if (!is_writable($src)){
158
			$collection->addNotWritable($src);
159
		}
160
		if (!is_readable($src)){
161
			$collection->addNotReadable($src);
162
		}
163
		if (is_dir($src)){
164
			$files = scandir($src);
165
			foreach ($files as $file){
166
				if (!in_array($file, [".", ".."])){
167
					$this->checkr("$src/$file", $collection);
168
				}
169
			}
170
		}
171
	}
172
173
	/**
174
	 * @param string $path
175
	 */
176
	public function removeIfExists($path) {
177
		if (!file_exists($path)) {
178
			return;
179
		}
180
181
		if (is_dir($path)) {
182
			$this->rmdirr($path);
183
		} else {
184
			@unlink($path);
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...
185
		}
186
	}
187
188
	public function rmdirr($dir) {
189
		if(is_dir($dir)) {
190
			$files = scandir($dir);
191
			foreach($files as $file) {
192
				if ($file != "." && $file != "..") {
193
					$this->rmdirr("$dir/$file");
194
				}
195
			}
196
			@rmdir($dir);
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...
197
		}elseif(file_exists($dir)) {
198
			@unlink($dir);
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...
199
		}
200
		if(file_exists($dir)) {
201
			return false;
202
		}else{
203
			return true;
204
		}
205
	}
206
207
	/**
208
	 *
209
	 * @param string $old
210
	 * @param string $new
211
	 * @param string $temp
212
	 * @param string $dirName
213
	 */
214
	public function tripleMove($old, $new, $temp, $dirName){
215 View Code Duplication
		if ($this->fileExists($old . '/' . $dirName)){
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...
216
			$this->copyr($old . '/' . $dirName, $temp . '/' . $dirName, false);
217
			$this->rmdirr($old . '/' . $dirName);
218
		}
219 View Code Duplication
		if ($this->fileExists($new . '/' . $dirName)){
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...
220
			$this->copyr($new . '/' . $dirName, $old . '/' . $dirName, false);
221
			$this->rmdirr($new . '/' . $dirName);
222
		}
223
	}
224
225
}
226