/
xrouting.plugin.php
executable file
·174 lines (135 loc) · 7.72 KB
/
xrouting.plugin.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
<?php
switch ($modx->event->name) {
// "refresh cache" part
case 'OnContextSave':
case 'OnContextRemove':
case 'OnSiteRefresh':
$contexts = array();
$cacheKey = 'xrouting_contextmap';
$cacheOptions = array();
// build context array
$query = $modx->newQuery('modContext');
$query->where(array('modContext.key:NOT IN' => array('mgr')));
$query->sortby($modx->escape('modContext') . '.' . $modx->escape('key'), 'DESC');
$contextsGraph = $modx->getCollectionGraph('modContext', '{"ContextSettings":{}}', $query);
foreach ($contextsGraph as $context) {
$contextSettings = array();
foreach ($context->ContextSettings as $cSetting) {
$contextSettings[$cSetting->get('key')] = $cSetting->get('value');
}
if (!empty($contextSettings['http_host']) && !empty($contextSettings['base_url'])) {
// add http_host to hosts list
$contexts['_hosts'][$contextSettings['http_host']][] = $context->get('key');
// add alias hosts to host list
if (!empty($contextSettings['http_host_aliases'])) {
foreach (explode(',',$contextSettings['http_host_aliases']) as $alias) {
$contexts['_hosts'][$alias][] = $context->get('key');
}
}
// add context settings
$contexts[$context->get('key')] = $contextSettings;
}
}
unset($contextsGraph);
$modx->cacheManager->set($cacheKey, $contexts, 0, $cacheOptions);
break;
// "routing" part
default:
case 'OnHandleRequest':
if ($modx->context->get('key') == 'mgr') return;
$contexts = array();
$cacheKey = 'xrouting_contextmap';
$cacheOptions = array();
$contexts = $modx->cacheManager->get($cacheKey, $cacheOptions);
if (empty($contexts)) {
// build context array
$query = $modx->newQuery('modContext');
$query->where(array('modContext.key:NOT IN' => array('mgr')));
$query->sortby($modx->escape('modContext') . '.' . $modx->escape('key'), 'DESC');
$contextsGraph = $modx->getCollectionGraph('modContext', '{"ContextSettings":{}}', $query);
foreach ($contextsGraph as $context) {
$contextSettings = array();
foreach ($context->ContextSettings as $cSetting) {
$contextSettings[$cSetting->get('key')] = $cSetting->get('value');
}
if (!empty($contextSettings['http_host']) && !empty($contextSettings['base_url'])) {
// add http_host to hosts list
$contexts['_hosts'][$contextSettings['http_host']][] = $context->get('key');
// add alias hosts to host list
if (!empty($contextSettings['http_host_aliases'])) {
foreach (explode(',',$contextSettings['http_host_aliases']) as $alias) {
$contexts['_hosts'][$alias][] = $context->get('key');
}
}
// add context settings
$contexts[$context->get('key')] = $contextSettings;
}
}
unset($contextsGraph);
$modx->cacheManager->set($cacheKey, $contexts, 0, $cacheOptions);
}
if (!empty($contexts)) {
$http_host = $_SERVER['HTTP_HOST'];
if ($modx->getOption('xrouting.include_www', null, true)) {
$http_host = str_replace('www.','',$http_host);
}
$modx_base_url = false;
$baseUrlSetting = $modx->getObject('modSystemSetting', 'base_url');
if ($baseUrlSetting) $modx_base_url = $baseUrlSetting->get('value');
if (!$modx_base_url) $modx_base_url = MODX_BASE_URL;
$requestUrl = str_replace('//', '/', $modx_base_url . ($_REQUEST[$modx->getOption('request_param_alias', null, 'q')] ?? ''));
$matches = array();
// find matching hosts
$matched_contexts = $contexts['_hosts'][$http_host];
foreach ((array) $matched_contexts as $index => $ckey) {
$context = $contexts[$ckey];
$strpos = strpos($requestUrl, $contexts[$ckey]['base_url']);
if ($strpos === 0) {
$matches[strlen($contexts[$ckey]['base_url'])] = $ckey;
}
}
// modify request for the matched context
if (!empty($matches)) {
$cSettings = $contexts[$matches[max(array_keys($matches))]];
$cKey = $matches[max(array_keys($matches))];
// do we need to switch the context?
if ($modx->context->get('key') != $cKey) {
$modx->switchContext($cKey);
$cultureKey = $modx->getOption('cultureKey', null, 'en');
if (!empty($_SESSION['cultureKey'])) $cultureKey = $_SESSION['cultureKey'];
if (!empty($_REQUEST['cultureKey'])) $cultureKey = $_REQUEST['cultureKey'];
$modx->cultureKey = $cultureKey;
$modx->setOption('cultureKey', $cultureKey);
// set locale since $modx->_initCulture is called before OnHandleRequest
if ($modx->getOption('setlocale', null, true)) {
$locale = setlocale(LC_ALL, null);
setlocale(LC_ALL, $modx->getOption('locale', null, $locale, true));
}
}
// remove base_url from request query
if ($cSettings['base_url'] != $modx_base_url) {
$newRequestUrl = str_replace($cSettings['base_url'],'',$requestUrl);
$_REQUEST[$modx->getOption('request_param_alias', null, 'q')] = $newRequestUrl;
}
} else if ($_REQUEST['xrouting-debug'] != '1' || !$modx->getOption('xrouting.allow_debug_info', null, false)) {
// if no match found
if ($modx->getOption('xrouting.show_no_match_error', null, true)) {
$modx->sendErrorPage();
} else {
$modx->switchContext($modx->getOption('xrouting.default_context', null, 'web'));
}
}
// output debug info
if (isset($_REQUEST['xrouting-debug']) && $_REQUEST['xrouting-debug'] == '1' && $modx->getOption('xrouting.allow_debug_info', null, false)) {
$debuginfo = '<pre>';
$debuginfo .= "## MODX context map:\n\n" . print_r($contexts,true) . "\n\n\n";
$debuginfo .= "## Requested URL: " . ($_REQUEST[$modx->getOption('request_param_alias', null, 'q')] ?? '') . "\n\n\n";
$debuginfo .= "## Requested URL with base_url: ". $requestUrl ."\n\n\n";
$debuginfo .= "## Matched context(s) (Array key defines match quality):\n\n" . print_r($matches,true) . "\n\n\n";
$debuginfo .= "## Request will go to context: " . $matches[@max(array_keys($matches))] . "\n\n\n";
$debuginfo .= "## Modified request URL: " . $newRequestUrl . "\n\n\n";
die($debuginfo);
}
}
break;
}