/
uri_mapping.php
274 lines (179 loc) · 6.04 KB
/
uri_mapping.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
<?php
/**
* Copyright (c) 2006-2014 Laposa Ltd (http://laposa.co.uk)
* Licensed under the New BSD License. See the file LICENSE.txt for details.
*
*/
class Onxshop_Controller_Uri_Mapping extends Onxshop_Controller {
/**
* main action
*/
public function mainAction() {
/**
* check main domain
*/
if (defined('ONXSHOP_MAIN_DOMAIN')) {
if (array_key_exists('HTTPS', $_SERVER)) $protocol = 'https';
else $protocol = 'http';
if ($_SERVER['HTTP_HOST'] != ONXSHOP_MAIN_DOMAIN) {
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: $protocol://" . ONXSHOP_MAIN_DOMAIN . "{$_SERVER['REQUEST_URI']}" );
//exit the application immediately
exit;
}
}
/**
* input data
*/
$translate = trim($this->GET['translate']);
if ($translate != "/") $translate = rtrim($translate, '/');
if ($this->GET['controller_request']) $controller_request = trim($this->GET['controller_request']);
/**
* file stored rules
*/
if ($custom_translate = $this->proccessFileRules($translate)) {
$controller_request = $custom_translate;
$translate = false;
//force login when request is from bo/ folder
if (preg_match('/bo\//', $controller_request)) {
$auth = Onxshop_Bo_Authentication::getInstance()->login();
if (!$auth) $controller_request = 'sys/401';
$_SESSION['use_page_cache'] = false;
}
}
/**
* initialize database stored
*/
require_once('models/common/common_uri_mapping.php');
$this->Mapper = new common_uri_mapping();
/**
* translate request to $action_to_process
*/
if ($translate) {
if (is_numeric($node_id = trim($translate, '/'))) { // URL like /1234
/**
* short URL redirects
* TODO: allow to pass GET parameters
*/
$this->redirectToSeoURLAndExit($node_id);
} else if (preg_match('/^\/\b(page|node)\b\/([0-9]*)$/', $translate, $match)) { // URL like /page/1234 or /node/1234
$mapped_node_id = $match[2];
$action_to_process = $this->getActionToProcessForExistingPage($mapped_node_id);
} else if ($mapped_node_id = $this->Mapper->translate($translate)) { // URL like /abc-cbs
$action_to_process = $this->getActionToProcessForExistingPage($mapped_node_id);
} else if ($redirect_uri = $this->Mapper->getRedirectURI($translate)) { // URL like /abc-cbs
/**
* explicit redirects
*/
$this->redirectToSeoURLAndExit($redirect_uri['node_id']);
} else if ($translate == '/home') {
$action_to_process = $this->getActionToProcessForExistingPage($this->Mapper->conf['homepage_id']);
} else {
/**
* page not found
*/
msg("{$translate} not found! (linked from {$_SERVER['HTTP_REFERER']})", 'error');
$action_to_process = $this->Mapper->getRequest($this->Mapper->conf['404_id']);
$this->http_status = '404';
}
} else if ($controller_request) {
// used for /request/ and /api/ handling to allow translating URLs
$action_to_process = $controller_request;
}
/**
* process
*/
if ($action_to_process) {
$page_data = $this->processMappedAction($action_to_process);
/**
* URI mapping iself will become output of mapped page
*/
$this->content = $page_data['content'];
} else {
msg("Cannot find action to process", 'error');
}
return true;
}
/**
* redirectToSeoURL
*/
public function redirectToSeoURLAndExit($node_id) {
if (!is_numeric($node_id)) return false;
$seo_redirect_uri = $this->Mapper->stringToSeoUrl("/page/{$node_id}");
header("Location: $seo_redirect_uri", true, 301);
exit;
}
/**
* getActionToProcessForExistingPage
*/
public function getActionToProcessForExistingPage($node_id) {
if (!is_numeric($node_id)) return false;
//save node_id to last record in history
$_SESSION['orig'] = "/page/$node_id";
$_SESSION['history'][count($_SESSION['history'])-1]['node_id'] = $node_id;
$action_to_process = $this->Mapper->getRequest($node_id);
return $action_to_process;
}
/**
* processMappedAction
*/
public function processMappedAction($action_to_process) {
/**
* process action
*/
$Onxshop_Router = new Onxshop_Router();
$Onxshop = $Onxshop_Router->processAction($action_to_process);
if (is_object($Onxshop)) $page_data['content'] = $Onxshop->getContent();
if ($page_data['content'] == "") $page_data['content'] = $this->content;
$page_data['content'] = $this->Mapper->system_uri2public_uri($page_data['content']);
/**
* CDN rewrites for URLs (a.k.a. output filter)
*/
if (ONXSHOP_CDN && (ONXSHOP_CDN_USE_WHEN_SSL || !isset($_SERVER['HTTPS']))) {
require_once('lib/onxshop.cdn.php');
$CDN = new Onxshop_Cdn();
$page_data['content'] = $CDN->processOutputHtml($page_data['content']);
}
return $page_data;
}
/**
* proccessFileRules
*/
public function proccessFileRules($translate) {
$uri_map = $this->getFileRules();
$apply = $this->proccessFileRulesItems($translate, $uri_map);
$parsed = parse_url($apply);
parse_str($parsed['query'], $query);
foreach ($query as $k=>$item) {
$_GET[$k] = $item;
}
if (array_key_exists('controller_request', $query)) return $query['controller_request'];
else return $query['request'];
}
/**
* proccessFileRulesItems
*/
public function proccessFileRulesItems($translate, $uri_map) {
if (!is_array($uri_map)) return false;
foreach ($uri_map as $rule=>$apply) {
$rule = str_replace('/', '\/', $rule);
$rule = str_replace('\\\\', '\\', $rule);
if (preg_match("/$rule/", $translate, $matches)) {
if (is_array($apply)) {
$apply = $this->proccessFileRulesItems($translate, $apply);
}
foreach ($matches as $k=>$v) {
if ($k > 0) $apply = str_replace('$' . $k, $v, $apply);
}
return $apply;
}
}
}
/**
* getRewriteRules
*/
public function getFileRules() {
require_once(ONXSHOP_DIR . 'conf/uri_map.php');
return $uri_map;
}
}