-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.php
280 lines (242 loc) · 8.49 KB
/
route.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
<?php
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
// Adding support for an other web server? Check the end of the file
/**
* Routing method, receives the path portion of the URL relative to tiki root.
* http://example.com/tiki/hello-world?foo-bar
* $path is expectedto be hello-world
*/
function tiki_route($path)
{
/*
// If you are converting to Tiki and want to preserve some URLs, map the urls and remove the comment block
$urlMapping = array(
'wiki/old-page-name' => 'PageName',
'corporate/Privacy+Policy.pdf' => 'dl123',
);
if (isset($urlMapping[$path])) {
$path = $urlMapping[$path];
}
*/
$simple = array(
'articles' => 'tiki-view_articles.php',
'blogs' => 'tiki-list_blogs.php',
'calendar' => 'tiki-calendar.php',
'categories' => 'tiki-browse_categories.php',
'chat' => 'tiki-chat.php',
'contact' => 'tiki-contact.php',
'directories' => 'tiki-directory_browse.php',
'faqs' => 'tiki-list_faqs.php',
'filelist' => 'tiki-list_file_gallery.php',
'forums' => 'tiki-forums.php',
'galleries' => 'tiki-galleries.php',
'login' => 'tiki-login_scr.php',
'logout' => 'tiki-logout.php',
'my' => 'tiki-my_tiki.php',
'newsletters' => 'tiki-newsletters.php',
'quizzes' => 'tiki-list_quizzes.php',
'register' => 'tiki-register.php',
'sheets' => 'tiki-sheets.php',
'stats' => 'tiki-stats.php',
'surveys' => 'tiki-list_surveys.php',
'trackers' => 'tiki-list_trackers.php',
'users' => 'tiki-list_users.php',
'tiki-check' => 'tiki-check.php',
);
foreach ($simple as $key => $file) {
tiki_route_attempt("|^$key$|", $file);
}
/*
Valid:
art123
article123
art123-XYZ
article123-XYZ
*/
tiki_route_attempt('/^(art|article)(\d+)(\-.*)?$/', 'tiki-read_article.php', tiki_route_single(2, 'articleId'));
tiki_route_attempt('|^blog(\d+)(\-.*)?$|', 'tiki-view_blog.php', tiki_route_single(1, 'blogId'));
tiki_route_attempt('|^blogpost(\d+)(\-.*)?$|', 'tiki-view_blog_post.php', tiki_route_single(1, 'postId'));
tiki_route_attempt('|^cat(\d+)(\-.*)?$|', 'tiki-browse_categories.php', tiki_route_single(1, 'parentId'));
tiki_route_attempt_prefix('browseimage', 'tiki-browse_image.php', 'imageId');
tiki_route_attempt('/^event(\d+)(\-.*)?$/', 'tiki-calendar_edit_item.php', tiki_route_single(1, 'viewcalitemId'));
tiki_route_attempt(
'|^cal(\d[\d,]*)$|',
'tiki-calendar.php',
function ($parts) {
$ids = explode(',', $parts[1]);
$ids = array_filter($ids);
return array('calIds' => $ids);
}
);
tiki_route_attempt_prefix('directory', 'tiki-directory_browse.php', 'parent');
tiki_route_attempt_prefix('dirlink', 'tiki-directory_redirect.php', 'siteId');
tiki_route_attempt_prefix('faq', 'tiki-view_faq.php', 'faqId');
tiki_route_attempt_prefix('file', 'tiki-list_file_gallery.php', 'galleryId');
tiki_route_attempt_prefix('forum', 'tiki-view_forum.php', 'forumId');
tiki_route_attempt_prefix('forumthread', 'tiki-view_forum_thread.php', 'comments_parentId');
tiki_route_attempt_prefix('gallery', 'tiki-browse_gallery.php', 'galleryId');
tiki_route_attempt_prefix('img', 'show_image.php', 'id');
tiki_route_attempt_prefix('image', 'show_image.php', 'id');
tiki_route_attempt(
'|^imagescale(\d+)/(\d+)$|',
'show_image.php',
function ($parts) {
return array(
'id' => $parts[1],
'scalesize' => $parts[2],
);
}
);
tiki_route_attempt_prefix('int', 'tiki-integrator.php', 'repID');
tiki_route_attempt_prefix('item', 'tiki-view_tracker_item.php', 'itemId');
tiki_route_attempt_prefix('newsletter', 'tiki-newsletters.php', 'nlId', array('info' => '1'));
tiki_route_attempt_prefix('nl', 'tiki-newsletters.php', 'nlId', array('info' => '1'));
tiki_route_attempt_prefix('poll', 'tiki-poll_form.php', 'pollId');
tiki_route_attempt_prefix('quiz', 'tiki-take_quiz.php', 'quizId');
tiki_route_attempt_prefix('survey', 'tiki-take_survey.php', 'surveyId');
tiki_route_attempt_prefix('tracker', 'tiki-view_tracker.php', 'trackerId');
tiki_route_attempt_prefix('sheet', 'tiki-view_sheets.php', 'sheetId');
tiki_route_attempt_prefix('user', 'tiki-user_information.php', 'userId');
tiki_route_attempt('|^userinfo$|', 'tiki-view_tracker_item.php', function () { return array('view' => ' user'); });
tiki_route_attempt_prefix('dl', 'tiki-download_file.php', 'fileId');
tiki_route_attempt_prefix('thumbnail', 'tiki-download_file.php', 'fileId', array('thumbnail' => ''));
tiki_route_attempt_prefix('display', 'tiki-download_file.php', 'fileId', array('display' => ''));
tiki_route_attempt_prefix('preview', 'tiki-download_file.php', 'fileId', array('preview' => ''));
tiki_route_attempt(
'/^(wiki|page)\-(.+)$/',
'tiki-index.php',
function ($parts) {
return array('page' => $parts[2]);
}
);
tiki_route_attempt(
'/^show:(.+)$/',
'tiki-slideshow.php',
function ($parts) {
return array('page' => urldecode($parts[1]));
}
);
tiki_route_attempt(
'|^tiki\-(\w+)\-(\w+)$|',
'tiki-ajax_services.php',
function ($parts) {
if ($parts[2] == 'x') {
return array(
'controller' => $parts[1],
);
} else {
return array(
'controller' => $parts[1],
'action' => $parts[2],
);
}
}
);
if (false !== $dot = strrpos($path, '.')) {
// Prevent things that look like filenames from being considered for wiki page names
$extension = substr($path, $dot + 1);
if (in_array($extension, array('css', 'gif', 'jpg', 'png', 'php', 'html', 'js', 'htm', 'shtml', 'cgi', 'sql', 'phtml', 'txt', 'ihtml'))) {
return;
}
}
tiki_route_attempt(
'|.*|',
'tiki-index.php',
function ($parts) {
return array('page' => urldecode($parts[0]));
}
);
}
function tiki_route_attempt($pattern, $file, $callback = null, $extra = array())
{
global $path, $inclusion, $base, $full;
if ($inclusion) {
return;
}
if (preg_match($pattern, $path, $parts)) {
$inclusion = $file;
$full = $base . $file;
if ($callback && is_callable($callback)) {
$_GET = array_merge($_GET, $callback($parts), $extra);
}
}
}
function tiki_route_attempt_prefix($prefix, $file, $key, $extra = array())
{
tiki_route_attempt("|^$prefix(\d+)$|", $file, tiki_route_single(1, $key), $extra);
}
function tiki_route_single($index, $name)
{
return function ($parts) use ($index, $name) {
return array($name => $parts[$index]);
};
}
$sapi = php_sapi_name();
$base = null;
$path = null;
$inclusion = null;
// This portion may need to vary depending on the webserver/configuration
switch ($sapi) {
case 'apache2handler':
default:
// Fix $_SERVER['REQUEST_URI', which is ASCII encoded on IIS
// Convert the SERVER variable itself, to fix $_SERVER['REQUEST_URI'] access everywhere
// route.php comes first in the processing. Avoid dependencies.
if (isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'],'IIS') !== false) {
if (mb_detect_encoding($_SERVER['REQUEST_URI'], 'UTF-8', true) == false) {
$_SERVER['REQUEST_URI'] = utf8_encode($_SERVER['REQUEST_URI']);
}
}
if (isset($_SERVER['SCRIPT_URL'])) {
$full = $_SERVER['SCRIPT_URL'];
} elseif (isset($_SERVER['REQUEST_URI'])) {
$full = $_SERVER['REQUEST_URI'];
if (strpos($full, '?') !== false) {
$full = substr($full, 0, strpos($full, '?'));
}
} elseif (isset($_SERVER['REDIRECT_URL'])) {
$full = $_SERVER['REDIRECT_URL'];
} elseif (isset($_SERVER['UNENCODED_URL'])) { // For IIS
$full = $_SERVER['UNENCODED_URL'];
} else {
break;
}
$file = basename(__FILE__);
$base = substr($_SERVER['PHP_SELF'], 0, -strlen($file));
$path = substr($full, strlen($base));
break;
}
// Global check
if (is_null($base) || is_null($path)) {
header('HTTP/1.0 500 Internal Server Error');
header('Content-Type: text/plain; charset=utf-8');
echo "Request could not be understood. Verify routing file.";
exit;
}
tiki_route($path);
if ($inclusion) {
$_SERVER['PHP_SELF'] = $base . $inclusion;
$_SERVER['SCRIPT_NAME'] = $base . basename($inclusion);
include __DIR__ . '/' . $inclusion;
} else {
error_log("No route found - full:$full query:{$_SERVER['QUERY_STRING']}");
// Route to the "no-route" URL, if found
require_once('lib/init/initlib.php');
$local_php = TikiInit::getCredentialsFile();
if ( file_exists($local_php) ) {
include($local_php);
}
if (empty($noroute_url)) {
// Fail
header('HTTP/1.0 404 Not Found');
header('Content-Type: text/plain; charset=utf-8');
echo "No route found. Please see http://dev.tiki.org/URL+Rewriting+Revamp";
} else {
header('Location: '.$noroute_url);
}
exit;
}