1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
About Utility Class Definition |
4
|
|
|
|
5
|
|
|
You may not change or alter any portion of this comment or credits of |
6
|
|
|
supporting developers from this source code or any supporting source code |
7
|
|
|
which is considered copyrighted (c) material of the original comment or credit |
8
|
|
|
authors. |
9
|
|
|
|
10
|
|
|
This program is distributed in the hope that it will be useful, but |
11
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
13
|
|
|
*/ |
14
|
|
|
/** |
15
|
|
|
* Module: About |
16
|
|
|
* |
17
|
|
|
* @package :: \module\about\class |
18
|
|
|
* @license http://www.fsf.org/copyleft/gpl.html GNU public license |
19
|
|
|
* @copyright https://xoops.org 2001-2017 © XOOPS Project |
20
|
|
|
* @author ZySpec <[email protected]> |
21
|
|
|
* @author Mamba <[email protected]> |
22
|
|
|
* @since :: File available since version 1.54 |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* AboutUtility |
27
|
|
|
* |
28
|
|
|
* Static utility class to provide common functionality |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
class AboutUtility |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* Verifies XOOPS version meets minimum requirements for this module |
36
|
|
|
* @static |
37
|
|
|
* @param XoopsModule $module |
|
|
|
|
38
|
|
|
* @param null|string $requiredVer |
39
|
|
|
* |
40
|
|
|
* @return bool true if meets requirements, false if not |
41
|
|
|
*/ |
42
|
|
|
public static function checkVerXoops(XoopsModule $module = null, $requiredVer = null) |
43
|
|
|
{ |
44
|
|
|
$moduleDirName = basename(dirname(__DIR__)); |
45
|
|
|
if (null === $module) { |
46
|
|
|
$module = XoopsModule::getByDirname($moduleDirName); |
47
|
|
|
} |
48
|
|
|
xoops_loadLanguage('admin', $moduleDirName); |
49
|
|
|
//check for minimum XOOPS version |
50
|
|
|
$currentVer = substr(XOOPS_VERSION, 6); // get the numeric part of string |
51
|
|
|
$currArray = explode('.', $currentVer); |
52
|
|
|
if (null === $requiredVer) { |
53
|
|
|
$requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string |
54
|
|
|
} |
55
|
|
|
$reqArray = explode('.', $requiredVer); |
56
|
|
|
$success = true; |
57
|
|
|
foreach ($reqArray as $k => $v) { |
58
|
|
|
if (isset($currArray[$k])) { |
59
|
|
|
if ($currArray[$k] > $v) { |
60
|
|
|
break; |
61
|
|
|
} elseif ($currArray[$k] == $v) { |
62
|
|
|
continue; |
63
|
|
|
} else { |
64
|
|
|
$success = false; |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
} else { |
68
|
|
|
if ((int)$v > 0) { // handles versions like x.x.x.0_RC2 |
69
|
|
|
$success = false; |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (false === $success) { |
76
|
|
|
$module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_XOOPS, $requiredVer, $currentVer)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $success; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* |
84
|
|
|
* Verifies PHP version meets minimum requirements for this module |
85
|
|
|
* @static |
86
|
|
|
* @param XoopsModule $module |
87
|
|
|
* |
88
|
|
|
* @return bool true if meets requirements, false if not |
89
|
|
|
*/ |
90
|
|
|
public static function checkVerPhp(XoopsModule $module) |
91
|
|
|
{ |
92
|
|
|
xoops_loadLanguage('admin', $module->dirname()); |
93
|
|
|
// check for minimum PHP version |
94
|
|
|
$success = true; |
95
|
|
|
$verNum = PHP_VERSION; |
96
|
|
|
$reqVer = $module->getInfo('min_php'); |
97
|
|
|
if (false !== $reqVer && '' !== $reqVer) { |
98
|
|
|
if (version_compare($verNum, (string)$reqVer, '<')) { |
99
|
|
|
$module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_PHP, $reqVer, $verNum)); |
100
|
|
|
$success = false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
return $success; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* Remove files and (sub)directories |
110
|
|
|
* |
111
|
|
|
* @param string $src source directory to delete |
112
|
|
|
* |
113
|
|
|
* @see Xmf\Module\Helper::getHelper() |
114
|
|
|
* @see Xmf\Module\Helper::isUserAdmin() |
115
|
|
|
* |
116
|
|
|
* @return bool true on success |
117
|
|
|
*/ |
118
|
|
|
public static function deleteDirectory($src) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
// Only continue if user is a 'global' Admin |
121
|
|
View Code Duplication |
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
|
|
|
|
122
|
|
|
return false; |
123
|
|
|
} |
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), array('..', '.')); |
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
|
|
|
} else { |
139
|
|
|
// delete the file |
140
|
|
|
if (!($success = unlink($fileInfo->getRealPath()))) { |
141
|
|
|
break; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
// now delete this (sub)directory if all the files are gone |
146
|
|
|
if ($success) { |
147
|
|
|
$success = rmdir($dirInfo->getRealPath()); |
148
|
|
|
} |
149
|
|
|
} else { |
150
|
|
|
// input is not a valid directory |
151
|
|
|
$success = false; |
152
|
|
|
} |
153
|
|
|
return $success; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* |
158
|
|
|
* Recursively remove directory |
159
|
|
|
* |
160
|
|
|
* @todo currently won't remove directories with hidden files, should it? |
161
|
|
|
* |
162
|
|
|
* @param string $src directory to remove (delete) |
163
|
|
|
* |
164
|
|
|
* @return bool true on success |
165
|
|
|
*/ |
166
|
|
|
public static function rrmdir($src) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
// Only continue if user is a 'global' Admin |
169
|
|
View Code Duplication |
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
|
|
|
|
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// If source is not a directory stop processing |
174
|
|
|
if (!is_dir($src)) { |
175
|
|
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$success = true; |
|
|
|
|
179
|
|
|
|
180
|
|
|
// Open the source directory to read in files |
181
|
|
|
$iterator = new DirectoryIterator($src); |
182
|
|
|
foreach ($iterator as $fObj) { |
183
|
|
|
if ($fObj->isFile()) { |
184
|
|
|
$filename = $fObj->getPathname(); |
185
|
|
|
$fObj = null; // clear this iterator object to close the file |
186
|
|
|
if (!unlink($filename)) { |
187
|
|
|
return false; // couldn't delete the file |
188
|
|
|
} |
189
|
|
|
} elseif (!$fObj->isDot() && $fObj->isDir()) { |
190
|
|
|
// Try recursively on directory |
191
|
|
|
self::rrmdir($fObj->getPathname()); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
$iterator = null; // clear iterator Obj to close file/directory |
|
|
|
|
195
|
|
|
return rmdir($src); // remove the directory & return results |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Recursively move files from one directory to another |
200
|
|
|
* |
201
|
|
|
* @param string $src - Source of files being moved |
202
|
|
|
* @param string $dest - Destination of files being moved |
203
|
|
|
* |
204
|
|
|
* @return bool true on success |
205
|
|
|
*/ |
206
|
|
|
public static function rmove($src, $dest) |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
// Only continue if user is a 'global' Admin |
209
|
|
View Code Duplication |
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
|
|
|
|
210
|
|
|
return false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// If source is not a directory stop processing |
214
|
|
|
if (!is_dir($src)) { |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
// If the destination directory does not exist and could not be created stop processing |
219
|
|
|
if (!is_dir($dest) && !mkdir($dest, 0755)) { |
220
|
|
|
return false; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// Open the source directory to read in files |
224
|
|
|
$iterator = new DirectoryIterator($src); |
225
|
|
View Code Duplication |
foreach ($iterator as $fObj) { |
|
|
|
|
226
|
|
|
if ($fObj->isFile()) { |
227
|
|
|
rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
228
|
|
|
} elseif (!$fObj->isDot() && $fObj->isDir()) { |
229
|
|
|
// Try recursively on directory |
230
|
|
|
self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
231
|
|
|
// rmdir($fObj->getPath()); // now delete the directory |
|
|
|
|
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
$iterator = null; // clear iterator Obj to close file/directory |
|
|
|
|
235
|
|
|
return rmdir($src); // remove the directory & return results |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Recursively copy directories and files from one directory to another |
240
|
|
|
* |
241
|
|
|
* @param string $src - Source of files being moved |
242
|
|
|
* @param string $dest - Destination of files being moved |
243
|
|
|
* |
244
|
|
|
* @see Xmf\Module\Helper::getHelper() |
245
|
|
|
* @see Xmf\Module\Helper::isUserAdmin() |
246
|
|
|
* |
247
|
|
|
* @return bool true on success |
248
|
|
|
*/ |
249
|
|
|
public static function rcopy($src, $dest) |
|
|
|
|
250
|
|
|
{ |
251
|
|
|
// Only continue if user is a 'global' Admin |
252
|
|
View Code Duplication |
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
|
|
|
|
253
|
|
|
return false; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
// If source is not a directory stop processing |
257
|
|
|
if (!is_dir($src)) { |
258
|
|
|
return false; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
// If the destination directory does not exist and could not be created stop processing |
262
|
|
|
if (!is_dir($dest) && !mkdir($dest, 0755)) { |
263
|
|
|
return false; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
// Open the source directory to read in files |
267
|
|
|
$iterator = new DirectoryIterator($src); |
268
|
|
View Code Duplication |
foreach($iterator as $fObj) { |
|
|
|
|
269
|
|
|
if($fObj->isFile()) { |
270
|
|
|
copy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
271
|
|
|
} else if(!$fObj->isDot() && $fObj->isDir()) { |
272
|
|
|
self::rcopy($fObj->getPathname(), "{$dest}/" . $fObj-getFilename()); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
return true; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.