/
FilesManagement.php
321 lines (307 loc) · 10.2 KB
/
FilesManagement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
declare(strict_types=1);
namespace XoopsModules\Suico\Common;
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* @category Module
* @package suico
* @copyright {@link https://xoops.org/ XOOPS Project}
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
* @author Marcello Brandão aka Suico, Mamba, LioMJ <https://xoops.org>
*/
use DirectoryIterator;
use RuntimeException;
use SplFileInfo;
use Throwable;
use XoopsUser;
/**
* Trait FilesManagement
* @package XoopsModules\Suico\Common
*/
trait FilesManagement
{
/**
* Function responsible for checking if a directory exists, we can also write in and create an index.html file
*
* @param string $folder The full path of the directory to check
*
* @throws RuntimeException
*/
public static function createFolder(
$folder
) {
try {
if (!\file_exists($folder)) {
if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) {
throw new RuntimeException(\sprintf('Unable to create the %s directory', $folder));
}
file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>');
}
} catch (Throwable $throwable) {
echo 'Caught exception: ', $throwable->getMessage(), '<br>';
}
}
/**
* @param $file
* @param $folder
* @return bool
*/
public static function copyFile(
$file,
$folder
) {
return \copy($file, $folder);
}
/**
* @param $src
* @param $dst
* @return bool
*/
public static function recurseCopy($src, $dst)
{
$dir = \opendir($src);
if (false === $dir) {
return false;
}
// @mkdir($dst);
if (!@\mkdir($dst) && !\is_dir($dst)) {
throw new RuntimeException('The directory ' . $dst . ' could not be created.');
}
while (false !== ($file = \readdir($dir))) {
if (('.' !== $file) && ('..' !== $file)) {
if (\is_dir($src . '/' . $file)) {
self::recurseCopy($src . '/' . $file, $dst . '/' . $file);
} else {
\copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
\closedir($dir);
}
/**
* Copy a file, or recursively copy a folder and its contents
* @param string $source Source path
* @param string $dest Destination path
* @return bool Returns true on success, false on failure
* @author Aidan Lister <aidan@php.net>
* @version 1.0.1
* @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
*/
public static function xcopy(
$source,
$dest
) {
// Check for symlinks
if (\is_link($source)) {
return \symlink(\readlink($source), $dest);
}
// Simple copy for a file
if (\is_file($source)) {
return \copy($source, $dest);
}
// Make destination directory
if (!\is_dir($dest)) {
if (!\mkdir($dest) && !\is_dir($dest)) {
throw new RuntimeException(\sprintf('Directory "%s" was not created', $dest));
}
}
// Loop through the folder
$dir = \dir($source);
if (@\is_dir($dir)) {
while (false !== $entry = $dir->read()) {
// Skip pointers
if ('.' === $entry || '..' === $entry) {
continue;
}
// Deep copy directories
self::xcopy("${source}/${entry}", "${dest}/${entry}");
}
// Clean up
$dir->close();
}
return true;
}
/**
* Remove files and (sub)directories
*
* @param string $src source directory to delete
*
* @return bool true on success
* @uses \Xmf\Module\Helper::isUserAdmin()
*
* @uses \Xmf\Module\Helper::getHelper()
*/
public static function deleteDirectory(
$src
) {
// Only continue if user is a 'global' Admin
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
return false;
}
$success = true;
// remove old files
$dirInfo = new SplFileInfo($src);
// validate is a directory
if ($dirInfo->isDir()) {
$fileList = \array_diff(\scandir($src, \SCANDIR_SORT_NONE), ['..', '.']);
foreach ($fileList as $k => $v) {
$fileInfo = new SplFileInfo("{$src}/{$v}");
if ($fileInfo->isDir()) {
// recursively handle subdirectories
if (!$success = self::deleteDirectory(
$fileInfo->getRealPath()
)) {
break;
}
} elseif (!($success = \unlink($fileInfo->getRealPath()))) {
break;
}
}
// now delete this (sub)directory if all the files are gone
if ($success) {
$success = \rmdir($dirInfo->getRealPath());
}
} else {
// input is not a valid directory
$success = false;
}
return $success;
}
/**
* Recursively remove directory
*
* @todo currently won't remove directories with hidden files, should it?
*
* @param string $src directory to remove (delete)
*
* @return bool true on success
*/
public static function rrmdir(
$src
) {
// Only continue if user is a 'global' Admin
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
return false;
}
// If source is not a directory stop processing
if (!\is_dir($src)) {
return false;
}
$success = true;
// Open the source directory to read in files
$iterator = new DirectoryIterator($src);
foreach ($iterator as $fObj) {
if ($fObj->isFile()) {
$filename = $fObj->getPathname();
$fObj = null; // clear this iterator object to close the file
if (!\unlink($filename)) {
return false; // couldn't delete the file
}
} elseif (!$fObj->isDot() && $fObj->isDir()) {
// Try recursively on directory
self::rrmdir($fObj->getPathname());
}
}
$iterator = null; // clear iterator Obj to close file/directory
return \rmdir($src); // remove the directory & return results
}
/**
* Recursively move files from one directory to another
*
* @param string $src - Source of files being moved
* @param string $dest - Destination of files being moved
*
* @return bool true on success
*/
public static function rmove(
$src,
$dest
) {
// Only continue if user is a 'global' Admin
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
return false;
}
// If source is not a directory stop processing
if (!\is_dir($src)) {
return false;
}
// If the destination directory does not exist and could not be created stop processing
if (!\is_dir(
$dest
)
&& !\mkdir(
$dest
)
&& !\is_dir(
$dest
)) {
return false;
}
// Open the source directory to read in files
$iterator = new DirectoryIterator($src);
foreach ($iterator as $fObj) {
if ($fObj->isFile()) {
\rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
} elseif (!$fObj->isDot() && $fObj->isDir()) {
// Try recursively on directory
self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
// rmdir($fObj->getPath()); // now delete the directory
}
}
$iterator = null; // clear iterator Obj to close file/directory
return \rmdir($src); // remove the directory & return results
}
/**
* Recursively copy directories and files from one directory to another
*
* @param string $src - Source of files being moved
* @param string $dest - Destination of files being moved
*
* @return bool true on success
* @uses \Xmf\Module\Helper::isUserAdmin()
*
* @uses \Xmf\Module\Helper::getHelper()
*/
public static function rcopy(
$src,
$dest
) {
// Only continue if user is a 'global' Admin
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
return false;
}
// If source is not a directory stop processing
if (!\is_dir($src)) {
return false;
}
// If the destination directory does not exist and could not be created stop processing
if (!\is_dir(
$dest
)
&& !\mkdir(
$dest
)
&& !\is_dir(
$dest
)) {
return false;
}
// Open the source directory to read in files
$iterator = new DirectoryIterator($src);
foreach ($iterator as $fObj) {
if ($fObj->isFile()) {
\copy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
} elseif (!$fObj->isDot() && $fObj->isDir()) {
self::rcopy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
}
}
return true;
}
}