|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
Xsitemap 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: xSitemap |
|
16
|
|
|
* |
|
17
|
|
|
* @package \module\xsitemap\class |
|
18
|
|
|
* @license http://www.fsf.org/copyleft/gpl.html GNU public license |
|
19
|
|
|
* @copyright http://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
|
|
|
* XsitemapUtility |
|
27
|
|
|
* |
|
28
|
|
|
* Static utility class to provide common functionality |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
class XsitemapUtility extends XoopsObject |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* |
|
35
|
|
|
* Verifies XOOPS version meets minimum requirements for this module |
|
36
|
|
|
* @static |
|
37
|
|
|
* @param XoopsModule $module |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool true if meets requirements, false if not |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function checkVerXoops(XoopsModule $module) |
|
42
|
|
|
{ |
|
43
|
|
|
xoops_loadLanguage('admin', $module->dirname()); |
|
44
|
|
|
//check for minimum XOOPS version |
|
45
|
|
|
$currentVer = substr(XOOPS_VERSION, 6); // get the numeric part of string |
|
46
|
|
|
$currArray = explode('.', $currentVer); |
|
47
|
|
|
$requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string |
|
48
|
|
|
$reqArray = explode('.', $requiredVer); |
|
49
|
|
|
$success = true; |
|
50
|
|
|
foreach ($reqArray as $k => $v) { |
|
51
|
|
|
if (isset($currArray[$k])) { |
|
52
|
|
|
if ($currArray[$k] > $v) { |
|
53
|
|
|
break; |
|
54
|
|
|
} elseif ($currArray[$k] == $v) { |
|
55
|
|
|
continue; |
|
56
|
|
|
} else { |
|
57
|
|
|
$success = false; |
|
58
|
|
|
break; |
|
59
|
|
|
} |
|
60
|
|
|
} else { |
|
61
|
|
|
if ((int)$v > 0) { // handles versions like x.x.x.0_RC2 |
|
62
|
|
|
$success = false; |
|
63
|
|
|
break; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (false === $success) { |
|
69
|
|
|
$module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_XOOPS, $requiredVer, $currentVer)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $success; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* |
|
77
|
|
|
* Verifies PHP version meets minimum requirements for this module |
|
78
|
|
|
* @static |
|
79
|
|
|
* @param XoopsModule $module |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool true if meets requirements, false if not |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function checkVerPhp(XoopsModule $module) |
|
84
|
|
|
{ |
|
85
|
|
|
xoops_loadLanguage('admin', $module->dirname()); |
|
86
|
|
|
// check for minimum PHP version |
|
87
|
|
|
$success = true; |
|
88
|
|
|
$verNum = PHP_VERSION; |
|
89
|
|
|
$reqVer =& $module->getInfo('min_php'); |
|
90
|
|
|
if ((false !== $reqVer) && ('' !== $reqVer)) { |
|
91
|
|
|
if (version_compare($verNum, (string)$reqVer, '<')) { |
|
92
|
|
|
$module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_PHP, $reqVer, $verNum)); |
|
93
|
|
|
$success = false; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $success; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* |
|
102
|
|
|
* Remove files and (sub)directories |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $src source directory to delete |
|
105
|
|
|
* |
|
106
|
|
|
* @uses \Xmf\Module\Helper::getHelper() |
|
107
|
|
|
* @uses \Xmf\Module\Helper::isUserAdmin() |
|
108
|
|
|
* |
|
109
|
|
|
* @return bool true on success |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function deleteDirectory($src) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
// Only continue if user is a 'global' Admin |
|
114
|
|
View Code Duplication |
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
|
|
|
|
|
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$success = true; |
|
119
|
|
|
// remove old files |
|
120
|
|
|
$dirInfo = new SplFileInfo($src); |
|
121
|
|
|
// validate is a directory |
|
122
|
|
|
if ($dirInfo->isDir()) { |
|
123
|
|
|
$fileList = array_diff(scandir($src), array('..', '.')); |
|
124
|
|
|
foreach ($fileList as $k => $v) { |
|
125
|
|
|
$fileInfo = new SplFileInfo("{$src}/{$v}"); |
|
126
|
|
|
if ($fileInfo->isDir()) { |
|
127
|
|
|
// recursively handle subdirectories |
|
128
|
|
|
if (!$success = self::deleteDirectory($fileInfo->getRealPath())) { |
|
129
|
|
|
break; |
|
130
|
|
|
} |
|
131
|
|
|
} else { |
|
132
|
|
|
// delete the file |
|
133
|
|
|
if (!($success = unlink($fileInfo->getRealPath()))) { |
|
134
|
|
|
break; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
// now delete this (sub)directory if all the files are gone |
|
139
|
|
|
if ($success) { |
|
140
|
|
|
$success = rmdir($dirInfo->getRealPath()); |
|
141
|
|
|
} |
|
142
|
|
|
} else { |
|
143
|
|
|
// input is not a valid directory |
|
144
|
|
|
$success = false; |
|
145
|
|
|
} |
|
146
|
|
|
return $success; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* |
|
151
|
|
|
* Recursively remove directory |
|
152
|
|
|
* |
|
153
|
|
|
* @todo currently won't remove directories with hidden files, should it? |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $src directory to remove (delete) |
|
156
|
|
|
* |
|
157
|
|
|
* @return bool true on success |
|
158
|
|
|
*/ |
|
159
|
|
|
public static function rrmdir($src) |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
// Only continue if user is a 'global' Admin |
|
162
|
|
View Code Duplication |
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
|
|
|
|
|
|
163
|
|
|
return false; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// If source is not a directory stop processing |
|
167
|
|
|
if (!is_dir($src)) { |
|
168
|
|
|
return false; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$success = true; |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
// Open the source directory to read in files |
|
174
|
|
|
$iterator = new DirectoryIterator($src); |
|
175
|
|
|
foreach ($iterator as $fObj) { |
|
176
|
|
|
if ($fObj->isFile()) { |
|
177
|
|
|
$filename = $fObj->getPathname(); |
|
178
|
|
|
$fObj = null; // clear this iterator object to close the file |
|
179
|
|
|
if (!unlink($filename)) { |
|
180
|
|
|
return false; // couldn't delete the file |
|
181
|
|
|
} |
|
182
|
|
|
} elseif (!$fObj->isDot() && $fObj->isDir()) { |
|
183
|
|
|
// Try recursively on directory |
|
184
|
|
|
self::rrmdir($fObj->getPathname()); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
$iterator = null; // clear iterator Obj to close file/directory |
|
|
|
|
|
|
188
|
|
|
return rmdir($src); // remove the directory & return results |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Recursively move files from one directory to another |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $src - Source of files being moved |
|
195
|
|
|
* @param string $dest - Destination of files being moved |
|
196
|
|
|
* |
|
197
|
|
|
* @return bool true on success |
|
198
|
|
|
*/ |
|
199
|
|
|
public static function rmove($src, $dest) |
|
|
|
|
|
|
200
|
|
|
{ |
|
201
|
|
|
// Only continue if user is a 'global' Admin |
|
202
|
|
View Code Duplication |
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
|
|
|
|
|
|
203
|
|
|
return false; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
// If source is not a directory stop processing |
|
207
|
|
|
if (!is_dir($src)) { |
|
208
|
|
|
return false; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
// If the destination directory does not exist and could not be created stop processing |
|
212
|
|
|
if (!is_dir($dest) && !mkdir($dest, 0755)) { |
|
213
|
|
|
return false; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
// Open the source directory to read in files |
|
217
|
|
|
$iterator = new DirectoryIterator($src); |
|
218
|
|
View Code Duplication |
foreach ($iterator as $fObj) { |
|
|
|
|
|
|
219
|
|
|
if ($fObj->isFile()) { |
|
220
|
|
|
rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
|
221
|
|
|
} elseif (!$fObj->isDot() && $fObj->isDir()) { |
|
222
|
|
|
// Try recursively on directory |
|
223
|
|
|
self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
|
224
|
|
|
// rmdir($fObj->getPath()); // now delete the directory |
|
|
|
|
|
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
$iterator = null; // clear iterator Obj to close file/directory |
|
|
|
|
|
|
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
|
|
|
* @uses \Xmf\Module\Helper::getHelper() |
|
238
|
|
|
* @uses \Xmf\Module\Helper::isUserAdmin() |
|
239
|
|
|
* |
|
240
|
|
|
* @return bool true on success |
|
241
|
|
|
*/ |
|
242
|
|
|
public static function rcopy($src, $dest) |
|
|
|
|
|
|
243
|
|
|
{ |
|
244
|
|
|
// Only continue if user is a 'global' Admin |
|
245
|
|
View Code Duplication |
if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
|
|
|
|
|
|
246
|
|
|
return false; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
// If source is not a directory stop processing |
|
250
|
|
|
if (!is_dir($src)) { |
|
251
|
|
|
return false; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
// If the destination directory does not exist and could not be created stop processing |
|
255
|
|
|
if (!is_dir($dest) && !mkdir($dest, 0755)) { |
|
256
|
|
|
return false; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
// Open the source directory to read in files |
|
260
|
|
|
$iterator = new DirectoryIterator($src); |
|
261
|
|
View Code Duplication |
foreach ($iterator as $fObj) { |
|
|
|
|
|
|
262
|
|
|
if ($fObj->isFile()) { |
|
263
|
|
|
copy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
|
264
|
|
|
} else if (!$fObj->isDot() && $fObj->isDir()) { |
|
265
|
|
|
self::rcopy($fObj->getPathname(), "{$dest}/" . $fObj - getFilename()); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
return true; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
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.