1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Contact\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
|
|
|
use DirectoryIterator; |
16
|
|
|
use Exception; |
17
|
|
|
use RuntimeException; |
18
|
|
|
use SplFileInfo; |
19
|
|
|
use XoopsUser; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
23
|
|
|
* @license https://www.fsf.org/copyleft/gpl.html GNU public license |
24
|
|
|
* @author mamba <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
trait FilesManagement |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Function responsible for checking if a directory exists, we can also write in and create an index.html file |
30
|
|
|
* |
31
|
|
|
* @param string $folder The full path of the directory to check |
32
|
|
|
*/ |
33
|
|
|
public static function createFolder($folder): void |
34
|
|
|
{ |
35
|
|
|
try { |
36
|
|
|
if (!\is_dir($folder)) { |
37
|
|
|
if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) { |
38
|
|
|
throw new RuntimeException(\sprintf('Unable to create the %s directory', $folder)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
42
|
|
|
} |
43
|
|
|
} catch (Exception $e) { |
44
|
|
|
echo 'Caught exception: ', $e->getMessage(), "\n", '<br>'; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $file |
50
|
|
|
* @param $folder |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public static function copyFile(string $file, string $folder): bool |
54
|
|
|
{ |
55
|
|
|
return \copy($file, $folder); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $src |
60
|
|
|
* @param $dst |
61
|
|
|
*/ |
62
|
|
|
public static function recurseCopy($src, $dst): void |
63
|
|
|
{ |
64
|
|
|
$dir = \opendir($src); |
65
|
|
|
// @mkdir($dst); |
66
|
|
|
if (!@\mkdir($dst) && !\is_dir($dst)) { |
67
|
|
|
throw new RuntimeException('The directory ' . $dst . ' could not be created.'); |
68
|
|
|
} |
69
|
|
|
while (false !== ($file = \readdir($dir))) { |
70
|
|
|
if (('.' !== $file) && ('..' !== $file)) { |
71
|
|
|
if (\is_dir($src . '/' . $file)) { |
72
|
|
|
self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
73
|
|
|
} else { |
74
|
|
|
\copy($src . '/' . $file, $dst . '/' . $file); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
\closedir($dir); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Remove files and (sub)directories |
83
|
|
|
* |
84
|
|
|
* @param string $src source directory to delete |
85
|
|
|
* |
86
|
|
|
* @return bool true on success |
87
|
|
|
* @uses \Xmf\Module\Helper::isUserAdmin() |
88
|
|
|
* |
89
|
|
|
* @uses \Xmf\Module\Helper::getHelper() |
90
|
|
|
*/ |
91
|
|
|
public static function deleteDirectory($src): bool |
92
|
|
|
{ |
93
|
|
|
// Only continue if user is a 'global' Admin |
94
|
|
|
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$success = true; |
99
|
|
|
// remove old files |
100
|
|
|
$dirInfo = new SplFileInfo($src); |
101
|
|
|
// validate is a directory |
102
|
|
|
if ($dirInfo->isDir()) { |
103
|
|
|
$fileList = \array_diff(\scandir($src, \SCANDIR_SORT_NONE), ['..', '.']); |
104
|
|
|
foreach ($fileList as $k => $v) { |
105
|
|
|
$fileInfo = new SplFileInfo("{$src}/{$v}"); |
106
|
|
|
if ($fileInfo->isDir()) { |
107
|
|
|
// recursively handle subdirectories |
108
|
|
|
if (!$success = self::deleteDirectory($fileInfo->getRealPath())) { |
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
} elseif (!($success = \unlink($fileInfo->getRealPath()))) { |
112
|
|
|
break; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
// now delete this (sub)directory if all the files are gone |
116
|
|
|
if ($success) { |
117
|
|
|
$success = \rmdir($dirInfo->getRealPath()); |
118
|
|
|
} |
119
|
|
|
} else { |
120
|
|
|
// input is not a valid directory |
121
|
|
|
$success = false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $success; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Recursively remove directory |
129
|
|
|
* |
130
|
|
|
* @todo currently won't remove directories with hidden files, should it? |
131
|
|
|
* |
132
|
|
|
* @param string $src directory to remove (delete) |
133
|
|
|
* |
134
|
|
|
* @return bool true on success |
135
|
|
|
*/ |
136
|
|
|
public static function rrmdir($src): bool |
137
|
|
|
{ |
138
|
|
|
// Only continue if user is a 'global' Admin |
139
|
|
|
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// If source is not a directory stop processing |
144
|
|
|
if (!\is_dir($src)) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$success = true; |
|
|
|
|
149
|
|
|
|
150
|
|
|
// Open the source directory to read in files |
151
|
|
|
$iterator = new DirectoryIterator($src); |
152
|
|
|
foreach ($iterator as $fObj) { |
153
|
|
|
if ($fObj->isFile()) { |
154
|
|
|
$filename = $fObj->getPathname(); |
155
|
|
|
$fObj = null; // clear this iterator object to close the file |
|
|
|
|
156
|
|
|
if (!\unlink($filename)) { |
157
|
|
|
return false; // couldn't delete the file |
158
|
|
|
} |
159
|
|
|
} elseif (!$fObj->isDot() && $fObj->isDir()) { |
160
|
|
|
// Try recursively on directory |
161
|
|
|
self::rrmdir($fObj->getPathname()); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
$iterator = null; // clear iterator Obj to close file/directory |
|
|
|
|
165
|
|
|
|
166
|
|
|
return \rmdir($src); // remove the directory & return results |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Recursively move files from one directory to another |
171
|
|
|
* |
172
|
|
|
* @param string $src - Source of files being moved |
173
|
|
|
* @param string $dest - Destination of files being moved |
174
|
|
|
* |
175
|
|
|
* @return bool true on success |
176
|
|
|
*/ |
177
|
|
|
public static function rmove($src, $dest): bool |
178
|
|
|
{ |
179
|
|
|
// Only continue if user is a 'global' Admin |
180
|
|
|
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
181
|
|
|
return false; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// If source is not a directory stop processing |
185
|
|
|
if (!\is_dir($src)) { |
186
|
|
|
return false; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// If the destination directory does not exist and could not be created stop processing |
190
|
|
|
if (!\is_dir($dest) && !\mkdir($dest) && !\is_dir($dest)) { |
191
|
|
|
return false; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// Open the source directory to read in files |
195
|
|
|
$iterator = new DirectoryIterator($src); |
196
|
|
|
foreach ($iterator as $fObj) { |
197
|
|
|
if ($fObj->isFile()) { |
198
|
|
|
\rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
199
|
|
|
} elseif (!$fObj->isDot() && $fObj->isDir()) { |
200
|
|
|
// Try recursively on directory |
201
|
|
|
self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
202
|
|
|
// rmdir($fObj->getPath()); // now delete the directory |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
$iterator = null; // clear iterator Obj to close file/directory |
|
|
|
|
206
|
|
|
|
207
|
|
|
return \rmdir($src); // remove the directory & return results |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Recursively copy directories and files from one directory to another |
212
|
|
|
* |
213
|
|
|
* @param string $src - Source of files being moved |
214
|
|
|
* @param string $dest - Destination of files being moved |
215
|
|
|
* |
216
|
|
|
* @return bool true on success |
217
|
|
|
* @uses \Xmf\Module\Helper::isUserAdmin() |
218
|
|
|
* |
219
|
|
|
* @uses \Xmf\Module\Helper::getHelper() |
220
|
|
|
*/ |
221
|
|
|
public static function rcopy($src, $dest): bool |
222
|
|
|
{ |
223
|
|
|
// Only continue if user is a 'global' Admin |
224
|
|
|
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
225
|
|
|
return false; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// If source is not a directory stop processing |
229
|
|
|
if (!\is_dir($src)) { |
230
|
|
|
return false; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// If the destination directory does not exist and could not be created stop processing |
234
|
|
|
if (!\is_dir($dest) && !\mkdir($dest) && !\is_dir($dest)) { |
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
// Open the source directory to read in files |
239
|
|
|
$iterator = new DirectoryIterator($src); |
240
|
|
|
foreach ($iterator as $fObj) { |
241
|
|
|
if ($fObj->isFile()) { |
242
|
|
|
\copy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
243
|
|
|
} elseif (!$fObj->isDot() && $fObj->isDir()) { |
244
|
|
|
self::rcopy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return true; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|