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
|
|
|
/** |
25
|
|
|
* Class FilesystemHelper |
26
|
|
|
* |
27
|
|
|
* @package Owncloud\Updater\Utils |
28
|
|
|
*/ |
29
|
|
|
class FilesystemHelper { |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Wrapper for filemtime function |
33
|
|
|
* @param string $path |
34
|
|
|
* @return integer |
35
|
|
|
*/ |
36
|
|
|
public function filemtime($path){ |
37
|
|
|
return filemtime($path); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Wrapper for scandir function. |
42
|
|
|
* Filters current and parent directories |
43
|
|
|
* @param string $path |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function scandirFiltered($path){ |
47
|
|
|
$content = $this->scandir($path); |
48
|
|
|
if (is_array($content)){ |
49
|
|
|
return array_diff($content, ['.', '..']); |
50
|
|
|
} |
51
|
|
|
return []; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Wrapper for scandir function |
56
|
|
|
* @param string $path |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function scandir($path){ |
60
|
|
|
return scandir($path); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Wrapper for file_exists function |
65
|
|
|
* @param string $path |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function fileExists($path){ |
69
|
|
|
return file_exists($path); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Wrapper for is_writable function |
74
|
|
|
* @param string $path |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function isWritable($path){ |
78
|
|
|
return is_writable($path); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Wrapper for is_dir function |
83
|
|
|
* @param string $path |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function isDir($path){ |
87
|
|
|
return is_dir($path); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Wrapper for md5_file function |
92
|
|
|
* @param string $path |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function md5File($path){ |
96
|
|
|
return md5_file($path); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Wrapper for mkdir |
101
|
|
|
* @param string $path |
102
|
|
|
* @param bool $isRecursive |
103
|
|
|
* @throws \Exception on error |
104
|
|
|
*/ |
105
|
|
|
public function mkdir($path, $isRecursive = false){ |
106
|
|
|
if (!mkdir($path, 0755, $isRecursive)){ |
107
|
|
|
throw new \Exception("Unable to create $path"); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Copy recursive |
113
|
|
|
* @param string $src - source path |
114
|
|
|
* @param string $dest - destination path |
115
|
|
|
* @throws \Exception on error |
116
|
|
|
*/ |
117
|
|
|
public function copyr($src, $dest, $stopOnError = true){ |
118
|
|
|
if (is_dir($src)){ |
119
|
|
|
if (!is_dir($dest)){ |
120
|
|
|
try{ |
121
|
|
|
$this->mkdir($dest); |
122
|
|
|
} catch (\Exception $e){ |
123
|
|
|
if ($stopOnError){ |
124
|
|
|
throw $e; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
$files = scandir($src); |
129
|
|
|
foreach ($files as $file){ |
130
|
|
|
if (!in_array($file, [".", ".."])){ |
131
|
|
|
$this->copyr("$src/$file", "$dest/$file", $stopOnError); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} elseif (file_exists($src)){ |
135
|
|
|
if (!copy($src, $dest) && $stopOnError){ |
136
|
|
|
throw new \Exception("Unable to copy $src to $dest"); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Moves file/directory |
143
|
|
|
* @param string $src - source path |
144
|
|
|
* @param string $dest - destination path |
145
|
|
|
* @throws \Exception on error |
146
|
|
|
*/ |
147
|
|
|
public function move($src, $dest){ |
148
|
|
|
if (!rename($src, $dest)){ |
149
|
|
|
throw new \Exception("Unable to move $src to $dest"); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Check permissions recursive |
155
|
|
|
* @param string $src - path to check |
156
|
|
|
* @param Collection $collection - object to store incorrect permissions |
157
|
|
|
*/ |
158
|
|
|
public function checkr($src, $collection){ |
159
|
|
|
if (!file_exists($src)){ |
160
|
|
|
return; |
161
|
|
|
} |
162
|
|
|
if (!is_writable($src)){ |
163
|
|
|
$collection->addNotWritable($src); |
164
|
|
|
} |
165
|
|
|
if (!is_readable($src)){ |
166
|
|
|
$collection->addNotReadable($src); |
167
|
|
|
} |
168
|
|
|
if (is_dir($src)){ |
169
|
|
|
$files = scandir($src); |
170
|
|
|
foreach ($files as $file){ |
171
|
|
|
if (!in_array($file, [".", ".."])){ |
172
|
|
|
$this->checkr("$src/$file", $collection); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $path |
180
|
|
|
*/ |
181
|
|
|
public function removeIfExists($path) { |
182
|
|
|
if (!file_exists($path)) { |
183
|
|
|
return; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (is_dir($path)) { |
187
|
|
|
$this->rmdirr($path); |
188
|
|
|
} else { |
189
|
|
|
@unlink($path); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param $dir |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
|
|
public function rmdirr($dir) { |
198
|
|
|
if(is_dir($dir)) { |
199
|
|
|
$files = scandir($dir); |
200
|
|
|
foreach($files as $file) { |
201
|
|
|
if ($file != "." && $file != "..") { |
202
|
|
|
$this->rmdirr("$dir/$file"); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
@rmdir($dir); |
|
|
|
|
206
|
|
|
}elseif(file_exists($dir)) { |
207
|
|
|
@unlink($dir); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
if(file_exists($dir)) { |
210
|
|
|
return false; |
211
|
|
|
}else{ |
212
|
|
|
return true; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* |
218
|
|
|
* @param string $old |
219
|
|
|
* @param string $new |
220
|
|
|
* @param string $temp |
221
|
|
|
* @param string $dirName |
222
|
|
|
*/ |
223
|
|
|
public function tripleMove($old, $new, $temp, $dirName){ |
224
|
|
View Code Duplication |
if ($this->fileExists($old . '/' . $dirName)){ |
|
|
|
|
225
|
|
|
$this->copyr($old . '/' . $dirName, $temp . '/' . $dirName, false); |
226
|
|
|
$this->rmdirr($old . '/' . $dirName); |
227
|
|
|
} |
228
|
|
View Code Duplication |
if ($this->fileExists($new . '/' . $dirName)){ |
|
|
|
|
229
|
|
|
$this->copyr($new . '/' . $dirName, $old . '/' . $dirName, false); |
230
|
|
|
$this->rmdirr($new . '/' . $dirName); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: