|
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
|
|
|
/** |
|
16
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
|
17
|
|
|
* @license http://www.fsf.org/copyleft/gpl.html GNU public license |
|
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($folder) |
|
28
|
|
|
{ |
|
29
|
|
|
try { |
|
30
|
|
|
if (!file_exists($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
|
|
|
|
|
35
|
|
|
file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
catch (\Exception $e) { |
|
39
|
|
|
echo 'Caught exception: ', $e->getMessage(), "\n", '<br>'; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $file |
|
45
|
|
|
* @param $folder |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function copyFile($file, $folder) |
|
49
|
|
|
{ |
|
50
|
|
|
return copy($file, $folder); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $src |
|
55
|
|
|
* @param $dst |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function recurseCopy($src, $dst) |
|
58
|
|
|
{ |
|
59
|
|
|
$dir = opendir($src); |
|
60
|
|
|
// @mkdir($dst); |
|
61
|
|
|
if (!@mkdir($dst) && !is_dir($dst)) { |
|
62
|
|
|
throw new \RuntimeException('The directory ' . $dst . ' could not be created.'); |
|
63
|
|
|
} |
|
64
|
|
|
if (false !== $dir) { |
|
65
|
|
|
while (false !== ($file = readdir($dir))) { |
|
66
|
|
|
if (('.' !== $file) && ('..' !== $file)) { |
|
67
|
|
|
if (is_dir($src . '/' . $file)) { |
|
68
|
|
|
self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
|
69
|
|
|
} else { |
|
70
|
|
|
copy($src . '/' . $file, $dst . '/' . $file); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
closedir($dir); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Remove files and (sub)directories |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $src source directory to delete |
|
82
|
|
|
* |
|
83
|
|
|
* @uses \Xmf\Module\Helper::getHelper() |
|
84
|
|
|
* @uses \Xmf\Module\Helper::isUserAdmin() |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool true on success |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function deleteDirectory($src) |
|
89
|
|
|
{ |
|
90
|
|
|
// Only continue if user is a 'global' Admin |
|
91
|
|
|
if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$success = false; |
|
96
|
|
|
// remove old files |
|
97
|
|
|
$dirInfo = new \SplFileInfo($src); |
|
98
|
|
|
// validate is a directory |
|
99
|
|
|
if ($dirInfo->isDir()) { |
|
100
|
|
|
$dirScan = scandir($src, SCANDIR_SORT_NONE); |
|
101
|
|
|
if (false !== $dirScan) { |
|
102
|
|
|
$fileList = array_diff($dirScan, ['..', '.']); |
|
103
|
|
|
foreach ($fileList as $k => $v) { |
|
104
|
|
|
$fileInfo = new \SplFileInfo("{$src}/{$v}"); |
|
105
|
|
|
if ($fileInfo->isDir()) { |
|
106
|
|
|
// recursively handle subdirectories |
|
107
|
|
|
if (!$success = self::deleteDirectory($fileInfo->getRealPath())) { |
|
108
|
|
|
break; |
|
109
|
|
|
} |
|
110
|
|
|
} elseif (!($success = unlink($fileInfo->getRealPath()))) { |
|
111
|
|
|
break; // delete the file |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
// now delete this (sub)directory if all the files are gone |
|
115
|
|
|
if ($success) { |
|
116
|
|
|
$success = rmdir($dirInfo->getRealPath()); |
|
117
|
|
|
} |
|
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) |
|
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
|
|
|
return rmdir($src); // remove the directory & return results |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Recursively move files from one directory to another |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $src - Source of files being moved |
|
172
|
|
|
* @param string $dest - Destination of files being moved |
|
173
|
|
|
* |
|
174
|
|
|
* @return bool true on success |
|
175
|
|
|
*/ |
|
176
|
|
|
public static function rmove($src, $dest) |
|
177
|
|
|
{ |
|
178
|
|
|
// Only continue if user is a 'global' Admin |
|
179
|
|
|
if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
|
180
|
|
|
return false; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// If source is not a directory stop processing |
|
184
|
|
|
if (!is_dir($src)) { |
|
185
|
|
|
return false; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
// If the destination directory does not exist and could not be created stop processing |
|
189
|
|
|
if (!is_dir($dest) && !mkdir($dest) && !is_dir($dest)) { |
|
190
|
|
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
// Open the source directory to read in files |
|
194
|
|
|
$iterator = new \DirectoryIterator($src); |
|
195
|
|
|
foreach ($iterator as $fObj) { |
|
196
|
|
|
if ($fObj->isFile()) { |
|
197
|
|
|
rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
|
198
|
|
|
} elseif (!$fObj->isDot() && $fObj->isDir()) { |
|
199
|
|
|
// Try recursively on directory |
|
200
|
|
|
self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
|
201
|
|
|
// rmdir($fObj->getPath()); // now delete the directory |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
$iterator = null; // clear iterator Obj to close file/directory |
|
|
|
|
|
|
205
|
|
|
return rmdir($src); // remove the directory & return results |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Recursively copy directories and files from one directory to another |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $src - Source of files being moved |
|
212
|
|
|
* @param string $dest - Destination of files being moved |
|
213
|
|
|
* |
|
214
|
|
|
* @uses \Xmf\Module\Helper::getHelper() |
|
215
|
|
|
* @uses \Xmf\Module\Helper::isUserAdmin() |
|
216
|
|
|
* |
|
217
|
|
|
* @return bool true on success |
|
218
|
|
|
*/ |
|
219
|
|
|
public static function rcopy($src, $dest) |
|
220
|
|
|
{ |
|
221
|
|
|
// Only continue if user is a 'global' Admin |
|
222
|
|
|
if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
|
223
|
|
|
return false; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
// If source is not a directory stop processing |
|
227
|
|
|
if (!is_dir($src)) { |
|
228
|
|
|
return false; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
// If the destination directory does not exist and could not be created stop processing |
|
232
|
|
|
if (!is_dir($dest) && !mkdir($dest) && !is_dir($dest)) { |
|
233
|
|
|
return false; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
// Open the source directory to read in files |
|
237
|
|
|
$iterator = new \DirectoryIterator($src); |
|
238
|
|
|
foreach ($iterator as $fObj) { |
|
239
|
|
|
if ($fObj->isFile()) { |
|
240
|
|
|
copy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
|
241
|
|
|
} elseif (!$fObj->isDot() && $fObj->isDir()) { |
|
242
|
|
|
self::rcopy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
return true; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|