Completed
Pull Request — master (#230)
by Lukas
09:26
created

FilesystemHelper::isDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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 scandir function
28
	 * @param string $path
29
	 * @return array
30
	 */
31
	public function scandir($path){
32
		return scandir($path);
33
	}
34
35
	/**
36
	 * Wrapper for file_exists function
37
	 * @param string $path
38
	 * @return bool
39
	 */
40
	public function fileExists($path){
41
		return file_exists($path);
42
	}
43
44
	/**
45
	 * Wrapper for is_writable function
46
	 * @param string $path
47
	 * @return bool
48
	 */
49
	public function isWritable($path){
50
		return is_writable($path);
51
	}
52
53
	/**
54
	 * Wrapper for is_dir function
55
	 * @param string $path
56
	 * @return bool
57
	 */
58
	public function isDir($path){
59
		return is_dir($path);
60
	}
61
62
	/**
63
	 * Wrapper for md5_file function
64
	 * @param string $path
65
	 * @return string
66
	 */
67
	public function md5File($path){
68
		return md5_file($path);
69
	}
70
71
	/**
72
	 * Wrapper for mkdir
73
	 * @param string $path
74
	 * @param bool $isRecursive
75
	 * @throws \Exception on error
76
	 */
77
	public function mkdir($path, $isRecursive = false){
78
		if (!mkdir($path, 0755, $isRecursive)){
79
			throw new \Exception("Unable to create $path");
80
		}
81
	}
82
83
	/**
84
	 * Copy recursive
85
	 * @param string $src  - source path
86
	 * @param string $dest - destination path
87
	 * @throws \Exception on error
88
	 */
89
	public function copyr($src, $dest, $stopOnError = true){
90
		if (is_dir($src)){
91
			if (!is_dir($dest)){
92
				try{
93
					$this->mkdir($dest);
94
				} catch (\Exception $e){
95
					if ($stopOnError){
96
						throw $e;
97
					}
98
				}
99
			}
100
			$files = scandir($src);
101
			foreach ($files as $file){
102
				if (!in_array($file, [".", ".."])){
103
					$this->copyr("$src/$file", "$dest/$file", $stopOnError);
104
				}
105
			}
106
		} elseif (file_exists($src)){
107
			if (!copy($src, $dest) && $stopOnError){
108
				throw new \Exception("Unable to copy $src to $dest");
109
			}
110
		}
111
	}
112
113
	/**
114
	 * Moves file/directory
115
	 * @param string $src  - source path
116
	 * @param string $dest - destination path
117
	 * @throws \Exception on error
118
	 */
119
	public function move($src, $dest){
120
		if (!rename($src, $dest)){
121
			throw new \Exception("Unable to move $src to $dest");
122
		}
123
	}
124
125
	/**
126
	 * Check permissions recursive
127
	 * @param string $src  - path to check
128
	 * @param Collection $collection - object to store incorrect permissions
129
	 */
130
	public function checkr($src, $collection){
131
		if (!file_exists($src)){
132
			return;
133
		}
134
		if (!is_writable($src)){
135
			$collection->addNotWritable($src);
136
		}
137
		if (!is_readable($src)){
138
			$collection->addNotReadable($src);
139
		}
140
		if (is_dir($src)){
141
			$files = scandir($src);
142
			foreach ($files as $file){
143
				if (!in_array($file, [".", ".."])){
144
					$this->checkr("$src/$file", $collection);
145
				}
146
			}
147
		}
148
	}
149
150
	public function removeIfExists($path) {
151
		if (!file_exists($path)) {
152
			return;
153
		}
154
155
		if (is_dir($path)) {
156
			$this->rmdirr($path);
157
		} else {
158
			@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...
159
		}
160
	}
161
162
	protected function rmdirr($dir) {
163
		if(is_dir($dir)) {
164
			$files = scandir($dir);
165
			foreach($files as $file) {
166
				if ($file != "." && $file != "..") {
167
					$this->rmdirr("$dir/$file");
168
				}
169
			}
170
			@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...
171
		}elseif(file_exists($dir)) {
172
			@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...
173
		}
174
		if(file_exists($dir)) {
175
			return false;
176
		}else{
177
			return true;
178
		}
179
	}
180
181
	/**
182
	 *
183
	 * @param string $old
184
	 * @param string $new
185
	 * @param string $temp
186
	 * @param string $dirName
187
	 */
188
	public function tripleMove($old, $new, $temp, $dirName){
189 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...
190
			$this->move($old . '/' . $dirName, $temp . '/' . $dirName);
191
		}
192 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...
193
			$this->move($new . '/' . $dirName, $old . '/' . $dirName);
194
		}
195
	}
196
197
}
198