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