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