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