route.php 8.49 KB
Newer Older
1
<?php
2
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
3
//
4 5 6 7 8 9 10 11 12 13 14
// 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
 */
15 16
function tiki_route($path)
{
17
	/*
18 19 20 21 22 23 24 25 26 27 28
	// 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];
	}
	*/

29

30 31 32 33
	$simple = array(
		'articles' => 'tiki-view_articles.php',
		'blogs' => 'tiki-list_blogs.php',
		'calendar' => 'tiki-calendar.php',
34
		'categories' => 'tiki-browse_categories.php',
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
		'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',
drsassafras's avatar
drsassafras committed
53
		'tiki-check' => 'tiki-check.php',
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	);

	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'));
72
	tiki_route_attempt('|^cat(\d+)(\-.*)?$|', 'tiki-browse_categories.php', tiki_route_single(1, 'parentId'));
73
	tiki_route_attempt_prefix('browseimage', 'tiki-browse_image.php', 'imageId');
74
	tiki_route_attempt('/^event(\d+)(\-.*)?$/', 'tiki-calendar_edit_item.php', tiki_route_single(1, 'viewcalitemId'));
75

76 77 78 79 80 81 82 83 84
	tiki_route_attempt(
		'|^cal(\d[\d,]*)$|',
		'tiki-calendar.php',
		function ($parts) {
			$ids = explode(',', $parts[1]);
			$ids = array_filter($ids);
			return array('calIds' => $ids);
		}
	);
85 86 87 88 89 90 91 92 93 94 95

	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');
96 97 98 99 100 101 102 103 104 105
	tiki_route_attempt(
		'|^imagescale(\d+)/(\d+)$|',
		'show_image.php',
		function ($parts) {
			return array(
				'id' => $parts[1],
				'scalesize' => $parts[2],
			);
		}
	);
106 107
	tiki_route_attempt_prefix('int', 'tiki-integrator.php', 'repID');
	tiki_route_attempt_prefix('item', 'tiki-view_tracker_item.php', 'itemId');
108
	tiki_route_attempt_prefix('newsletter', 'tiki-newsletters.php', 'nlId', array('info' => '1'));
109
	tiki_route_attempt_prefix('nl', 'tiki-newsletters.php', 'nlId', array('info' => '1'));
110 111 112 113 114 115
	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');
116
	tiki_route_attempt('|^userinfo$|', 'tiki-view_tracker_item.php', function () { return array('view' => ' user'); });
117 118

	tiki_route_attempt_prefix('dl', 'tiki-download_file.php', 'fileId');
jonnybradley's avatar
jonnybradley committed
119
	tiki_route_attempt_prefix('thumbnail', 'tiki-download_file.php', 'fileId', array('thumbnail' => ''));
120 121 122
	tiki_route_attempt_prefix('display', 'tiki-download_file.php', 'fileId', array('display' => ''));
	tiki_route_attempt_prefix('preview', 'tiki-download_file.php', 'fileId', array('preview' => ''));

123 124 125 126 127 128 129 130 131 132 133
	tiki_route_attempt(
		'/^(wiki|page)\-(.+)$/',
		'tiki-index.php',
		function ($parts) {
			return array('page' => $parts[2]);
		}
	);
	tiki_route_attempt(
		'/^show:(.+)$/',
		'tiki-slideshow.php',
		function ($parts) {
134
			return array('page' => urldecode($parts[1]));
135 136
		}
	);
137

138
	tiki_route_attempt(
139
		'|^tiki\-(\w+)\-(\w+)$|',
140 141
		'tiki-ajax_services.php',
		function ($parts) {
142 143 144 145 146 147 148 149 150 151
			if ($parts[2] == 'x') {
				return array(
					'controller' => $parts[1],
				);
			} else {
				return array(
					'controller' => $parts[1],
					'action' => $parts[2],
				);
			}
152 153
		}
	);
154 155 156 157 158 159 160 161 162

	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;
		}
	}

163 164 165 166
	tiki_route_attempt(
		'|.*|',
		'tiki-index.php',
		function ($parts) {
167
			return array('page' => urldecode($parts[0]));
168 169
		}
	);
170 171
}

172 173
function tiki_route_attempt($pattern, $file, $callback = null, $extra = array())
{
174
	global $path, $inclusion, $base, $full;
175 176 177 178 179 180 181 182

	if ($inclusion) {
		return;
	}

	if (preg_match($pattern, $path, $parts)) {
		$inclusion = $file;

183 184
		$full = $base . $file;

185
		if ($callback && is_callable($callback)) {
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
			$_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]);
	};
}
202 203 204 205 206 207 208 209 210 211 212

$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:
213 214 215

	// Fix $_SERVER['REQUEST_URI', which is ASCII encoded on IIS
	//	Convert the SERVER variable itself, to fix $_SERVER['REQUEST_URI'] access everywhere
216
	//	route.php comes first in the processing.  Avoid dependencies.
217
	if (isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'],'IIS') !== false) {
218 219 220 221
		if (mb_detect_encoding($_SERVER['REQUEST_URI'], 'UTF-8', true) == false) {
			$_SERVER['REQUEST_URI'] = utf8_encode($_SERVER['REQUEST_URI']);
		}
	}
222

223 224
	if (isset($_SERVER['SCRIPT_URL'])) {
		$full = $_SERVER['SCRIPT_URL'];
225 226
	} elseif (isset($_SERVER['REQUEST_URI'])) {
		$full = $_SERVER['REQUEST_URI'];
227 228 229
		if (strpos($full, '?') !== false) {
			$full = substr($full, 0, strpos($full, '?'));
		}
230
	} elseif (isset($_SERVER['REDIRECT_URL'])) {
231
		$full = $_SERVER['REDIRECT_URL'];
232 233
	} elseif (isset($_SERVER['UNENCODED_URL'])) {	// For IIS
		$full = $_SERVER['UNENCODED_URL'];
234 235 236 237
	} else {
		break;
	}

238 239
	$file = basename(__FILE__);
	$base = substr($_SERVER['PHP_SELF'], 0, -strlen($file));
240
	$path = substr($full, strlen($base));
241 242 243 244 245 246 247
	break;
}

// Global check

if (is_null($base) || is_null($path)) {
	header('HTTP/1.0 500 Internal Server Error');
248
	header('Content-Type: text/plain; charset=utf-8');
249 250 251 252 253 254 255 256

	echo "Request could not be understood. Verify routing file.";
	exit;
}

tiki_route($path);

if ($inclusion) {
257 258
	$_SERVER['PHP_SELF'] = $base . $inclusion;
	$_SERVER['SCRIPT_NAME'] = $base . basename($inclusion);
259 260
	include __DIR__ . '/' . $inclusion;
} else {
261
	error_log("No route found - full:$full query:{$_SERVER['QUERY_STRING']}");
262

263 264 265 266 267 268 269 270 271 272
	// 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');
273

274 275 276 277
		echo "No route found. Please see http://dev.tiki.org/URL+Rewriting+Revamp";
	} else {
		header('Location: '.$noroute_url);
	}
278 279 280
	exit;
}