/
controller.php
463 lines (433 loc) · 13.1 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?php
/**
* Controller of Sitemapper_XH.
*
* PHP versions 4 and 5
*
* @category CMSimple_XH
* @package Sitemapper
* @author Christoph M. Becker <cmbecker69@gmx.de>
* @copyright 2011-2015 Christoph M. Becker <http://3-magi.net/>
* @license http://www.gnu.org/licenses/gpl-3.0.en.html GNU GPLv3
* @link http://3-magi.net/?CMSimple_XH/Sitemapper_XH
*/
/**
* The fully qualified absolute URL of the current (sub)site.
*/
define(
'SITEMAPPER_URL',
'http'
. (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 's' : '')
. '://'
. (empty($plugin_cf['sitemapper']['canonical_hostname'])
? $_SERVER['HTTP_HOST']
: $plugin_cf['sitemapper']['canonical_hostname']
. ($_SERVER['SERVER_PORT'] < 1024 ? '' : ':' . $_SERVER['SERVER_PORT']))
. preg_replace('/index.php$/', '', $sn)
);
/**
* The controller class.
*
* @category CMSimple_XH
* @package Sitemapper
* @author Christoph M. Becker <cmbecker69@gmx.de>
* @license http://www.gnu.org/licenses/gpl-3.0.en.html GNU GPLv3
* @link http://3-magi.net/?CMSimple_XH/Sitemapper_XH
*/
class Sitemapper_Controller
{
/**
* The model.
*
* @access private
*
* @var object
*/
var $_model;
/**
* Initializes a controller.
*
* @return void
*
* @global array The content of the pages.
* @global array The paths of system files and folders.
* @global array The configuration of the core.
* @global array The configuration of the plugins.
* @global array The page data router.
*
* @access public
*/
function Sitemapper_Controller()
{
global $c, $pth, $cf, $plugin_cf, $pd_router;
$this->_model = new Sitemapper_Model(
$cf['language']['default'], $pth['folder']['base'],
$c, $pd_router->find_all(),
$plugin_cf['sitemapper']['ignore_hidden_pages'],
$plugin_cf['sitemapper']['changefreq'],
$plugin_cf['sitemapper']['priority']
);
}
/**
* Returns a string with special HTML characters escaped.
*
* @param string $str A string.
*
* @return string
*
* @access private
*/
function _hsc($str)
{
return htmlspecialchars($str, ENT_COMPAT, 'UTF_8');
}
/**
* Renders a template.
*
* @param string $_template The name of the template.
* @param array $_bag Variables available in the template.
*
* @return string
*
* @global array The paths of system files and folders.
* @global array The configuration of the core.
*
* @access private
*/
function _render($_template, $_bag)
{
global $pth, $cf;
$_template = $pth['folder']['plugins'] . 'sitemapper/views/'
. $_template . '.htm';
$_xhtml = $cf['xhtml']['endtags'];
unset($pth, $cf);
extract($_bag);
ob_start();
include $_template;
$o = ob_get_clean();
if (!$_xhtml) {
$o = str_replace('/>', '>', $o);
}
return $o;
}
/**
* Renders an XML template.
*
* @param string $_template The name of the template.
* @param array $_bag Variables available in the template.
*
* @return string
*
* @global array The paths of system files and folders.
*
* @access private
*/
function _renderXML($_template, $_bag)
{
global $pth;
$_template = $pth['folder']['plugins'] . 'sitemapper/views/'
. $_template . '.xml';
unset($pth, $cf);
extract($_bag);
ob_start();
include $_template;
$o = ob_get_clean();
return $o;
}
/**
* Returns the sitemap index.
*
* @return string XML.
*
* @global array The configuration of the core.
*
* @access private
*/
function _sitemapIndex()
{
global $cf;
$sitemaps = array();
foreach ($this->_model->installedSubsites() as $ss) {
$base = SITEMAPPER_URL;
if ($ss != $cf['language']['default']) {
$base .= $ss . '/';
}
$sitemap = array(
'loc' => $base . '?sitemapper_sitemap',
'time' => $this->_model->subsiteLastMod($ss)
);
array_walk($sitemap, array($this, '_hsc'));
$sitemaps[] = $sitemap;
}
return '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL
. $this->_renderXML('index', array('sitemaps' => $sitemaps));
}
/**
* Returns the sitemap of the current subsite/language.
*
* @return string XML.
*
* @global array The "URLs" of the pages.
* @global int The number of pages.
* @global array The configuration of the plugins.
*
* @access private
*/
function _subsiteSitemap()
{
global $u, $cl, $plugin_cf;
$urls = array();
for ($i = 0; $i < $cl; $i++) {
if (!$this->_model->isPageExcluded($i)) {
$seperator = $plugin_cf['sitemapper']['clean_urls'] ? '' : '?';
$priority = $this->_model->pagePriority($i);
$url = array(
'loc' => SITEMAPPER_URL . $seperator . $u[$i],
'lastmod' => $this->_model->pageLastMod($i),
'changefreq' => $this->_model->pageChangefreq($i),
'priority' => $priority
);
array_walk($url, array($this, '_hsc'));
$urls[] = $url;
}
}
return '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL
. $this->_renderXML('sitemap', array('urls' => $urls));
}
/**
* Returns the sitemaps.
*
* @return array
*
* @global array The configuration of the core.
*
* @access private
*/
function _sitemaps()
{
global $cf;
$sitemap = array(
'name' => 'index',
'href' => CMSIMPLE_ROOT . '?sitemapper_index'
);
$sitemaps = array($sitemap);
foreach ($this->_model->installedSubsites() as $ss) {
$subdir = $ss != $cf['language']['default'] ? $ss.'/' : '';
$sitemap = array(
'name' => $ss,
'href' => CMSIMPLE_ROOT . $subdir . '?sitemapper_sitemap'
);
$sitemaps[] = $sitemap;
}
return $sitemaps;
}
/**
* Returns the system checks.
*
* @return array
*
* @global array The paths of system files and folders.
* @global array The localization of the core.
* @global array The localization of the plugins.
*
* @access private
*/
function _systemChecks()
{
global $pth, $tx, $plugin_tx;
$ptx = $plugin_tx['sitemapper'];
$phpVersion = '4.3.10';
$checks = array();
$checks[sprintf($ptx['syscheck_phpversion'], $phpVersion)]
= version_compare(PHP_VERSION, $phpVersion) >= 0 ? 'ok' : 'fail';
foreach (array('pcre') as $ext) {
$checks[sprintf($ptx['syscheck_extension'], $ext)]
= extension_loaded($ext) ? 'ok' : 'fail';
}
$checks[$ptx['syscheck_magic_quotes']]
= !get_magic_quotes_runtime() ? 'ok' : 'fail';
$checks[$ptx['syscheck_encoding']]
= strtoupper($tx['meta']['codepage']) == 'UTF-8' ? 'ok' : 'warn';
$folders = array();
foreach (array('config/', 'languages/') as $folder) {
$folders[] = $pth['folder']['plugins'] . 'sitemapper/' . $folder;
}
foreach ($folders as $folder) {
$checks[sprintf($ptx['syscheck_writable'], $folder)]
= is_writable($folder) ? 'ok' : 'warn';
}
return $checks;
}
/**
* Returns the plugin information view.
*
* @return string (X)HTML.
*
* @global array The paths of system files and folders.
* @global array The localization of the plugins.
*
* @access private
*/
function _info()
{
global $pth, $plugin_tx;
$ptx = $plugin_tx['sitemapper'];
$labels = array(
'sitemaps' => $ptx['sitemaps'],
'syscheck' => $ptx['syscheck_title'],
'about' => $ptx['about']
);
$sitemaps = $this->_sitemaps();
foreach (array('ok', 'warn', 'fail') as $state) {
$images[$state] = $pth['folder']['plugins']
. 'sitemapper/images/' . $state . '.png';
}
$checks = $this->_systemChecks();
$icon = $pth['folder']['plugins'] . 'sitemapper/sitemapper.png';
$version = SITEMAPPER_VERSION;
$bag = compact(
'labels', 'sitemaps', 'images', 'checks', 'icon', 'version'
);
return $this->_render('info', $bag);
}
/**
* Sends a sitemap as HTTP response.
*
* @param string $body The response body.
*
* @return void
*
* @access private
*/
function _respondWithSitemap($body)
{
header('HTTP/1.0 200 OK');
header('Content-Type: application/xml; charset=utf-8');
echo $body;
exit;
}
/**
* Dispatches on Sitemapper related requests.
*
* @return void
*
* @global string The value of the "admin" GET or POST parameter.
* @global string The value of the "action" GET or POST parameter.
* @global string The name of the plugin.
* @global string The (X)HTML to be placed in the contents area.
* @global string Whether the plugin administration is requested.
* @global string The requested special functionality.
* @global string The current language.
* @global array The configuration of the core.
*
* @access private
*/
function _dispatch()
{
global $admin, $action, $plugin, $o, $sitemapper, $f, $sl, $cf;
if (XH_ADM && isset($sitemapper) && $sitemapper == 'true') {
$o .= print_plugin_admin('off');
switch ($admin) {
case '':
$o .= $this->_info();
break;
default:
$o .= plugin_admin_common($action, $admin, $plugin);
}
} elseif (isset($_GET['sitemapper_index'])
&& $sl == $cf['language']['default']
) {
$f = 'sitemapper_index';
} elseif (isset($_GET['sitemapper_sitemap'])) {
$f = 'sitemapper_sitemap';
}
}
/**
* Initializes the controller object.
*
* @return void
*
* @global array The paths of system files and folders.
* @global array The localization of the plugins.
* @global object The page data router.
*
* @access public
*/
function init()
{
global $pth, $plugin_tx, $pd_router;
$pd_router->add_interest('sitemapper_changefreq');
$pd_router->add_interest('sitemapper_priority');
if (function_exists('XH_afterPluginLoading')) {
XH_afterPluginLoading(array($this, 'dispatchAfterPluginLoading'));
}
if (XH_ADM) {
if (function_exists('XH_registerStandardPluginMenuItems')) {
XH_registerStandardPluginMenuItems(false);
}
$pd_router->add_tab(
$plugin_tx['sitemapper']['tab'],
$pth['folder']['plugins'] . 'sitemapper/sitemapper_view.php'
);
}
$this->_dispatch();
}
/**
* Returns the page data tab view.
*
* @param array $pageData The page's data.
*
* @return string (X)HTML.
*
* @global string The "URL" of the currently selected page.
* @global array The paths of system files and folders.
* @global array The localization of the plugins.
*
* @access public
*/
function pageDataTab($pageData)
{
global $sn, $su, $pth, $plugin_tx;
$ptx = $plugin_tx['sitemapper'];
$action = $sn . '?' . $su;
$help = array(
'changefreq' => $ptx['cf_changefreq'],
'priority' => $ptx['cf_priority']
);
$helpIcon = $pth['folder']['plugins'] . 'sitemapper/images/help.png';
$changefreqOptions = $this->_model->changefreqs;
array_unshift($changefreqOptions, '');
$changefreqOptions = array_flip($changefreqOptions);
foreach ($changefreqOptions as $opt => $dummy) {
$changefreqOptions[$opt]
= $pageData['sitemapper_changefreq'] == $opt;
}
$priority = $pageData['sitemapper_priority'];
$bag = compact(
'action', 'helpIcon', 'help', 'changefreqOptions', 'priority'
);
return $this->_render('pdtab', $bag);
}
/**
* Dispatches to sitemap requests.
*
* @return void
*
* @global string The requested special functionality.
*
* @access public
*/
function dispatchAfterPluginLoading()
{
global $f;
switch ($f) {
case 'sitemapper_index':
$this->_respondWithSitemap($this->_sitemapIndex());
break;
case 'sitemapper_sitemap':
$this->_respondWithSitemap($this->_subsiteSitemap());
break;
}
}
}
?>