Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 94 lines (75 sloc) 3.528 kB
07959a2 Development version of PunBB 1.3
Anatoly authored
1 <?php
619e67e Added phpDoc comments to the start of every PHP file, replacing the e…
Anatoly authored
2 /**
3 * Rewrites SEF URLs to their actual files.
4 *
e2caa53 @dimkalinux Update copyright.
dimkalinux authored
5 * @copyright (C) 2008-2012 PunBB, partially based on code (C) 2008-2009 FluxBB.org
619e67e Added phpDoc comments to the start of every PHP file, replacing the e…
Anatoly authored
6 * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
7 * @package PunBB
8 */
07959a2 Development version of PunBB 1.3
Anatoly authored
9
10
11 define('FORUM_ROOT', './');
12 require FORUM_ROOT.'include/essentials.php';
13
14 // Bring in all the rewrite rules
1fa1ff7 Modified the way URL schemes work to allow custom rewrite rules to be…
Anatoly authored
15 if (file_exists(FORUM_ROOT.'include/url/'.$forum_config['o_sef'].'/rewrite_rules.php'))
16 require FORUM_ROOT.'include/url/'.$forum_config['o_sef'].'/rewrite_rules.php';
17 else
18 require FORUM_ROOT.'include/url/Default/rewrite_rules.php';
07959a2 Development version of PunBB 1.3
Anatoly authored
19
20 // Allow extensions to create their own rewrite rules/modify existing rules
21 ($hook = get_hook('re_rewrite_rules')) ? eval($hook) : null;
22
c39fade Make sure we have a base URL if the db needs updated.
Anatoly authored
23 // If query string is not set properly, create one and set $_GET
24 // E.g. lighttpd's 404 handler does not pass query string
25 if ((!isset($_SERVER['QUERY_STRING']) || empty($_SERVER['QUERY_STRING'])) && strpos($_SERVER['REQUEST_URI'], '?') !== false)
26 {
27 $_SERVER['QUERY_STRING'] = parse_url('http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
28 $_SERVER['QUERY_STRING'] = isset($_SERVER['QUERY_STRING']['query']) ? $_SERVER['QUERY_STRING']['query'] : '';
29 parse_str($_SERVER['QUERY_STRING'], $_GET);
30 }
31
07959a2 Development version of PunBB 1.3
Anatoly authored
32 // We determine the path to the script, since we need to separate the path from the data to be rewritten
33 $path_to_script = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
34 if (substr($path_to_script, -1) != '/')
d537dde @Shutnik Fix code style: remove unneeded spaces
Shutnik authored
35 $path_to_script = $path_to_script.'/';
07959a2 Development version of PunBB 1.3
Anatoly authored
36
37 // We create our own request URI with the path removed and only the parts to rewrite included
eaa1f85 Fixing a bug in rewrite.php causing setups with a space in the path n…
Anatoly authored
38 $request_uri = substr(urldecode($_SERVER['REQUEST_URI']), strlen($path_to_script));
07959a2 Development version of PunBB 1.3
Anatoly authored
39 if (strpos($request_uri, '?') !== false)
40 $request_uri = substr($request_uri, 0, strpos($request_uri, '?'));
41
42 $rewritten_url = '';
43 $url_parts = array();
44 // We go through every rewrite rule
45 foreach ($forum_rewrite_rules as $rule => $rewrite_to)
46 {
47 // We have a match!
48 if (preg_match($rule, $request_uri))
49 {
50 $rewritten_url = preg_replace($rule, $rewrite_to, $request_uri);
51 $url_parts = explode('?', $rewritten_url);
52
53 // If there is a query string
54 if (isset($url_parts[1]))
55 {
56 $query_string = explode('&', $url_parts[1]);
57
58 // Set $_GET properly for all of the variables
59 // We also set $_REQUEST if it's not already set
60 foreach ($query_string as $cur_param)
61 {
62 $param_data = explode('=', $cur_param);
63
64 // Sometimes, parameters don't set a value (eg: script.php?foo), so we set them to null
65 $param_data[1] = isset($param_data[1]) ? $param_data[1] : null;
66
67 // We don't want to be overwriting values in $_REQUEST that were set in POST or COOKIE
68 if (!isset($_POST[$param_data[0]]) && !isset($_COOKIE[$param_data[0]]))
69 $_REQUEST[$param_data[0]] = urldecode($param_data[1]);
70
71 $_GET[$param_data[0]] = urldecode($param_data[1]);
72 }
73 }
74 break;
75 }
76 }
77
78 // If we don't know what to rewrite to, we show a bad request messsage
79 if (empty($rewritten_url))
80 {
c5f3e94 Corrected [444] to allow sending different response code (reported by…
Roman authored
81 define('FORUM_HTTP_RESPONSE_CODE_SET', 1);
82 header('HTTP/1.1 404 Not Found');
07959a2 Development version of PunBB 1.3
Anatoly authored
83
84 // Allow an extension to override the "Bad request" message with a custom 404 page
85 ($hook = get_hook('re_page_not_found')) ? eval($hook) : null;
86
c4bb7b6 @Shutnik Fix code style: use space before closing single tags
Shutnik authored
87 error('Page Not found (Error 404):<br />The requested page <em>'.forum_htmlencode($request_uri).'</em> could not be found.');
07959a2 Development version of PunBB 1.3
Anatoly authored
88 }
89
90 // We change $_SERVER['PHP_SELF'] so that it reflects the file we're actually loading
91 $_SERVER['PHP_SELF'] = str_replace('rewrite.php', $url_parts[0], $_SERVER['PHP_SELF']);
92
93 require FORUM_ROOT.$url_parts[0];
Something went wrong with that request. Please try again.