Completed
Push — master ( 2a86ef...b71188 )
by Victor
03:23
created

FilesystemHelper::fileExists()   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 md5_file function
55
	 * @param string $path
56
	 * @return string
57
	 */
58
	public function md5File($path){
59
		return md5_file($path);
60
	}
61
62
	/**
63
	 * Wrapper for mkdir
64
	 * @param string $path
65
	 * @param bool $isRecursive
66
	 * @throws \Exception on error
67
	 */
68
	public function mkdir($path, $isRecursive = false){
69
		if (!mkdir($path, 0755, $isRecursive)){
70
			throw new \Exception("Unable to create $path");
71
		}
72
	}
73
74
	/**
75
	 * Copy recursive
76
	 * @param string $src  - source path
77
	 * @param string $dest - destination path
78
	 * @throws \Exception on error
79
	 */
80
	public function copyr($src, $dest, $stopOnError = true){
81
		if (is_dir($src)){
82
			if (!is_dir($dest)){
83
				try{
84
					$this->mkdir($dest);
85
				} catch (\Exception $e){
86
					if ($stopOnError){
87
						throw $e;
88
					}
89
				}
90
			}
91
			$files = scandir($src);
92
			foreach ($files as $file){
93
				if (!in_array($file, [".", ".."])){
94
					$this->copyr("$src/$file", "$dest/$file", $stopOnError);
95
				}
96
			}
97
		} elseif (file_exists($src)){
98
			if (!copy($src, $dest) && $stopOnError){
99
				throw new \Exception("Unable to copy $src to $dest");
100
			}
101
		}
102
	}
103
104
	/**
105
	 * Moves file/directory
106
	 * @param string $src  - source path
107
	 * @param string $dest - destination path
108
	 * @throws \Exception on error
109
	 */
110
	public function move($src, $dest){
111
		if (!rename($src, $dest)){
112
			throw new \Exception("Unable to move $src to $dest");
113
		}
114
	}
115
116
	/**
117
	 * Check permissions recursive
118
	 * @param string $src  - path to check
119
	 * @param Collection $collection - object to store incorrect permissions
120
	 */
121
	public function checkr($src, $collection){
122
		if (!file_exists($src)){
123
			return;
124
		}
125
		if (!is_writable($src)){
126
			$collection->addNotWritable($src);
127
		}
128
		if (!is_readable($src)){
129
			$collection->addNotReadable($src);
130
		}
131
		if (is_dir($src)){
132
			$files = scandir($src);
133
			foreach ($files as $file){
134
				if (!in_array($file, [".", ".."])){
135
					$this->checkr("$src/$file", $collection);
136
				}
137
			}
138
		}
139
	}
140
141
	public function removeIfExists($path) {
142
		if (!file_exists($path)) {
143
			return;
144
		}
145
146
		if (is_dir($path)) {
147
			$this->rmdirr($path);
148
		} else {
149
			@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...
150
		}
151
	}
152
153
	protected function rmdirr($dir) {
154
		if(is_dir($dir)) {
155
			$files = scandir($dir);
156
			foreach($files as $file) {
157
				if ($file != "." && $file != "..") {
158
					$this->rmdirr("$dir/$file");
159
				}
160
			}
161
			@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...
162
		}elseif(file_exists($dir)) {
163
			@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...
164
		}
165
		if(file_exists($dir)) {
166
			return false;
167
		}else{
168
			return true;
169
		}
170
	}
171
172
	/**
173
	 *
174
	 * @param string $old
175
	 * @param string $new
176
	 * @param string $temp
177
	 * @param string $dirName
178
	 */
179
	public function tripleMove($old, $new, $temp, $dirName){
180 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...
181
			$this->move($old . '/' . $dirName, $temp . '/' . $dirName);
182
		}
183 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...
184
			$this->move($new . '/' . $dirName, $old . '/' . $dirName);
185
		}
186
	}
187
188
}
189