1
|
|
|
<?php namespace Xoopsmodules\xsitemap; |
|
|
|
|
2
|
|
|
/* |
3
|
|
|
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 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
|
|
|
use Xmf\Request; |
26
|
|
|
use Xoopsmodules\xsitemap\common; |
27
|
|
|
|
28
|
|
|
require_once __DIR__ . '/common/VersionChecks.php'; |
29
|
|
|
require_once __DIR__ . '/common/ServerStats.php'; |
30
|
|
|
require_once __DIR__ . '/common/FilesManagement.php'; |
31
|
|
|
|
32
|
|
|
//require_once __DIR__ . '/../include/common.php'; |
|
|
|
|
33
|
|
|
|
34
|
|
|
$moduleDirName = basename(dirname(__DIR__)); |
35
|
|
|
xoops_loadLanguage('admin', $moduleDirName); |
36
|
|
|
if (!class_exists(ucfirst($moduleDirName) . 'DummyObject')) { |
37
|
|
|
xoops_load('dummy', $moduleDirName); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Class Utility |
42
|
|
|
*/ |
43
|
|
|
class Utility |
44
|
|
|
{ |
45
|
|
|
use common\VersionChecks; //checkVerXoops, checkVerPhp Traits |
46
|
|
|
|
47
|
|
|
use common\ServerStats; // getServerStats Trait |
48
|
|
|
|
49
|
|
|
use common\FilesManagement; // Files Management Trait |
50
|
|
|
|
51
|
|
|
//--------------- Custom module methods ----------------------------- |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
* Show Site map |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public static function generateSitemap() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$block = []; |
62
|
|
|
$moduleDirName = basename(dirname(__DIR__)); |
63
|
|
|
/** @internal can't use Helper since function called during install |
64
|
|
|
* $helper = \Xmf\Module\Helper::getHelper($moduleDirName); |
65
|
|
|
* $pluginHandler = $helper->getHandler('plugin', $moduleDirName); |
66
|
|
|
*/ |
67
|
|
|
xoops_load('plugin', $moduleDirName); |
68
|
|
|
|
69
|
|
|
// Get list of modules admin wants to hide from xsitemap |
70
|
|
|
$invisibleDirnames = empty($GLOBALS['xoopsModuleConfig']['invisible_dirnames']) ? ['xsitemap'] : explode(',', $GLOBALS['xoopsModuleConfig']['invisible_dirnames'] . ',xsitemap'); |
71
|
|
|
$invisibleDirnames = array_map('trim', $invisibleDirnames); |
72
|
|
|
$invisibleDirnames = array_map('mb_strtolower', $invisibleDirnames); |
73
|
|
|
|
74
|
|
|
// Get the mid for any of these modules if they're active and hasmain (visible frontside) |
75
|
|
|
/** @var \XoopsModuleHandler $moduleHandler */ |
76
|
|
|
$moduleHandler = xoops_getHandler('module'); |
77
|
|
|
$invisibleMidArray = []; |
78
|
|
|
foreach ($invisibleDirnames as $hiddenDir) { |
79
|
|
|
$criteria = new \CriteriaCompo(new \Criteria('hasmain', 1)); |
80
|
|
|
$criteria->add(new \Criteria('isactive', 1)); |
81
|
|
|
$criteria->add(new \Criteria('name', $hiddenDir)); |
82
|
|
|
$modObj = $moduleHandler->getByDirname($hiddenDir); |
83
|
|
|
if (false !== $modObj && $modObj instanceof \XoopsModule) { |
|
|
|
|
84
|
|
|
$invisibleMidArray[] = $modObj->mid(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Where user has permissions |
89
|
|
|
/** @var \XoopsGroupPermHandler $modulepermHandler */ |
90
|
|
|
$modulepermHandler = xoops_getHandler('groupperm'); |
91
|
|
|
$groups = ($GLOBALS['xoopsUser'] instanceof \XoopsUser) ? $GLOBALS['xoopsUser']->getGroups() : XOOPS_GROUP_ANONYMOUS; |
|
|
|
|
92
|
|
|
$readAllowed = $modulepermHandler->getItemIds('module_read', $groups); |
93
|
|
|
$filteredMids = array_diff($readAllowed, $invisibleMidArray); |
94
|
|
|
/** @var \XsitemapPluginHandler $pluginHandler */ |
95
|
|
|
$pluginHandler = xoops_getModuleHandler('plugin', $moduleDirName); |
96
|
|
|
$criteria = new \CriteriaCompo(new \Criteria('hasmain', 1)); |
97
|
|
|
$criteria->add(new \Criteria('isactive', 1)); |
98
|
|
|
if (count($filteredMids) > 0) { |
99
|
|
|
$criteria->add(new \Criteria('mid', '(' . implode(',', $filteredMids) . ')', 'IN')); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** @var array $modules */ |
103
|
|
|
$modules = $moduleHandler->getObjects($criteria, true); |
104
|
|
|
|
105
|
|
|
$criteria = new \CriteriaCompo(); |
106
|
|
|
$criteria->setSort('plugin_id'); |
107
|
|
|
$criteria->order = 'ASC'; |
108
|
|
|
$pluginObjArray = $pluginHandler->getAll($criteria); |
109
|
|
|
|
110
|
|
|
/** @var array $sublinks */ |
111
|
|
|
foreach ($modules as $mid => $modObj) { |
112
|
|
|
$sublinks = $modObj->subLink(); |
113
|
|
|
$modDirName = $modObj->getVar('dirname', 'n'); |
114
|
|
|
$block['modules'][$mid] = [ |
115
|
|
|
'id' => $mid, |
116
|
|
|
'name' => $modObj->getVar('name'), |
117
|
|
|
'directory' => $modDirName, |
118
|
|
|
'sublinks' => [] |
119
|
|
|
// init the sublinks array |
120
|
|
|
]; |
121
|
|
|
// Now 'patch' the sublink to include module path |
122
|
|
|
if (count($sublinks) > 0) { |
123
|
|
|
foreach ($sublinks as $sublink) { |
124
|
|
|
$block['modules'][$mid]['sublinks'][] = [ |
125
|
|
|
'name' => $sublink['name'], |
126
|
|
|
'url' => $GLOBALS['xoops']->url("www/modules/{$modDirName}/{$sublink['url']}") |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** @var array $pluginObjArray */ |
132
|
|
|
foreach ($pluginObjArray as $pObj) { |
133
|
|
|
if ((0 == $pObj->getVar('topic_pid')) && in_array($pObj->getVar('plugin_mod_table'), (array)$modObj->getInfo('tables'))) { |
134
|
|
|
$objVars = $pObj->getValues(); |
135
|
|
|
if (1 == $objVars['plugin_online']) { |
136
|
|
|
$tmpMap = self::getSitemap($objVars['plugin_mod_table'], $objVars['plugin_cat_id'], $objVars['plugin_cat_pid'], $objVars['plugin_cat_name'], $objVars['plugin_call'], $objVars['plugin_weight']); |
137
|
|
|
$block['modules'][$mid]['parent'] = isset($tmpMap['parent']) ? $tmpMap['parent'] : null; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $block; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the Sitemap |
148
|
|
|
* |
149
|
|
|
* @param $table |
150
|
|
|
* @param $id_name |
151
|
|
|
* @param $pid_name |
152
|
|
|
* @param $title_name |
153
|
|
|
* @param $url |
154
|
|
|
* @param string $order |
155
|
|
|
* @return array sitemap links |
156
|
|
|
*/ |
157
|
|
|
public static function getSitemap($table, $id_name, $pid_name, $title_name, $url, $order = '') |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
/** @var \XoopsMySQLDatabase $xDB */ |
160
|
|
|
$xDB = \XoopsDatabaseFactory::getDatabaseConnection(); |
161
|
|
|
$myts = \MyTextSanitizer::getInstance(); |
162
|
|
|
|
163
|
|
|
$sql = "SELECT `{$id_name}`, `{$pid_name}`, `{$title_name}` FROM " . $xDB->prefix . "_{$table}"; |
164
|
|
|
$result = $xDB->query($sql); |
165
|
|
|
$objsArray = []; |
166
|
|
|
|
167
|
|
|
while (false !== ($row = $xDB->fetchArray($result))) { |
168
|
|
|
$objsArray[] = new \XsitemapDummyObject($row, $id_name, $pid_name, $title_name); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
//$sql = "SELECT `{$id_name}`, `{$title_name}` FROM " . $xDB->prefix . "_{$table} WHERE `{$pid_name}`= 0"; |
|
|
|
|
172
|
|
|
// v1.54 added in the event categories are flat (don't support hierarchy) |
173
|
|
|
$sql = "SELECT `{$id_name}`, `{$title_name}` FROM " . $xDB->prefix . "_{$table}"; |
174
|
|
|
if ($pid_name !== $id_name) { |
175
|
|
|
$sql .= " WHERE `{$pid_name}`= 0"; |
176
|
|
|
} |
177
|
|
|
if ('' != $order) { |
178
|
|
|
$sql .= " ORDER BY `{$order}`"; |
179
|
|
|
} |
180
|
|
|
$result = $xDB->query($sql); |
181
|
|
|
|
182
|
|
|
$i = 0; |
183
|
|
|
$xsitemap = []; |
184
|
|
|
while (list($catid, $name) = $xDB->fetchRow($result)) { |
185
|
|
|
$xsitemap['parent'][$i] = [ |
186
|
|
|
'id' => $catid, |
187
|
|
|
'title' => $myts->htmlSpecialChars($name), |
188
|
|
|
'url' => $url . $catid |
189
|
|
|
]; |
190
|
|
|
|
191
|
|
|
if (($pid_name !== $id_name) && $GLOBALS['xoopsModuleConfig']['show_subcategories']) { |
192
|
|
|
$j = 0; |
193
|
|
|
$mytree = new \XoopsObjectTree($objsArray, $id_name, $pid_name); |
194
|
|
|
$child_array = $mytree->getAllChild($catid); |
195
|
|
|
/** @var \XoopsObject $child */ |
196
|
|
|
foreach ($child_array as $child) { |
197
|
|
|
$xsitemap['parent'][$i]['child'][$j] = [ |
198
|
|
|
'id' => $child->getVar($id_name), |
199
|
|
|
'title' => $child->getVar($title_name), |
200
|
|
|
'url' => $url . $child->getVar($id_name) |
201
|
|
|
]; |
202
|
|
|
++$j; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
++$i; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $xsitemap; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Save the XML Sitemap |
213
|
|
|
* |
214
|
|
|
* @param array $xsitemap_show |
215
|
|
|
* @return mixed int number of bytes saved | false on failure |
216
|
|
|
*/ |
217
|
|
|
public static function saveSitemap(array $xsitemap_show) |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$xml = new \DOMDocument('1.0', 'UTF-8'); |
220
|
|
|
$xml->preserveWhiteSpace = false; |
221
|
|
|
$xml->formatOutput = true; |
222
|
|
|
$xml_set = $xml->createElement('urlset'); |
223
|
|
|
$xml_set->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); |
224
|
|
|
|
225
|
|
|
if (!empty($xsitemap_show)) { |
226
|
|
|
foreach ($xsitemap_show['modules'] as $mod) { |
227
|
|
|
if ($mod['directory']) { |
228
|
|
|
$xml_url = $xml->createElement('url'); |
229
|
|
|
$xml_url->appendChild($xml->createComment(htmlentities(ucwords($mod['name'])))); |
230
|
|
|
$loc = $xml->createElement('loc', htmlentities($GLOBALS['xoops']->url("www/modules/{$mod['directory']}/index.php"))); |
231
|
|
|
$xml_url->appendChild($loc); |
232
|
|
|
$xml_set->appendChild($xml_url); |
233
|
|
|
} |
234
|
|
|
if (isset($mod['parent']) ? $mod['parent'] : null) { |
235
|
|
View Code Duplication |
foreach ($mod['parent'] as $parent) { |
|
|
|
|
236
|
|
|
$xml_parent = $xml->createElement('url'); |
237
|
|
|
$loc = $xml->createElement('loc', htmlentities($GLOBALS['xoops']->url("www/modules/{$mod['directory']}/{$parent['url']}"))); |
238
|
|
|
$xml_parent->appendChild($loc); |
239
|
|
|
$xml_set->appendChild($xml_parent); |
240
|
|
|
} |
241
|
|
|
$z = 0; |
242
|
|
|
//if ($mod["parent"][$z]["child"]) { |
|
|
|
|
243
|
|
|
if (isset($mod['parent'][$z]['child']) ? $mod['parent'][$z]['child'] : null) { |
244
|
|
View Code Duplication |
foreach ($mod['parent'][$z]['child'] as $child) { |
|
|
|
|
245
|
|
|
$xml_child = $xml->createElement('url'); |
246
|
|
|
$loc = $xml->createElement('loc', htmlentities($GLOBALS['xoops']->url("www/modules/{$mod['directory']}/{$child['url']}"))); |
247
|
|
|
$xml_child->appendChild($loc); |
248
|
|
|
$xml_set->appendChild($xml_child); |
249
|
|
|
} |
250
|
|
|
++$z; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
$xml->appendChild($xml_set); |
256
|
|
|
return $xml->save($GLOBALS['xoops']->path('www/xsitemap.xml')); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.