punbb/rewrite.php
author Dan
Sun, 15 Jun 2008 01:42:31 -0400
changeset 9 a932ce8c4827
parent 6 5e1f1e916419
permissions -rw-r--r--
More progress, compatibility updates for 1.1.4, etc.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
     1
<?php
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
     2
/***********************************************************************
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
     3
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
     4
  Copyright (C) 2002-2008  PunBB.org
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
     5
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
     6
  This file is part of PunBB.
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
     7
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
     8
  PunBB is free software; you can redistribute it and/or modify it
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
     9
  under the terms of the GNU General Public License as published
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    10
  by the Free Software Foundation; either version 2 of the License,
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    11
  or (at your option) any later version.
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    12
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    13
  PunBB is distributed in the hope that it will be useful, but
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    14
  WITHOUT ANY WARRANTY; without even the implied warranty of
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    15
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    16
  GNU General Public License for more details.
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    17
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    18
  You should have received a copy of the GNU General Public License
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    19
  along with this program; if not, write to the Free Software
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    20
  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    21
  MA  02111-1307  USA
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    22
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    23
************************************************************************/
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    24
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    25
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    26
// define('PUN_ROOT', './');
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    27
// require PUN_ROOT.'include/essentials.php';
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    28
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    29
// Bring in all the rewrite rules
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    30
require PUN_ROOT.'include/rewrite_rules.php';
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    31
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    32
// import globals (I really hope this isn't dangerous)
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    33
foreach ( $GLOBALS as $key => $_ )
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    34
{
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    35
  $$key =& $GLOBALS[$key];
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    36
}
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    37
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    38
// Allow extensions to create their own rewrite rules/modify existing rules
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    39
($hook = get_hook('re_rewrite_rules')) ? eval($hook) : null;
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    40
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    41
// We determine the path to the script, since we need to separate the path from the data to be rewritten
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    42
$path_to_script = contentPath . $paths->nslist['Special'] . 'Forum';
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    43
if (substr($path_to_script, -1) != '/')
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    44
	$path_to_script  = $path_to_script.'/';
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    45
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    46
// We create our own request URI with the path removed and only the parts to rewrite included
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    47
// $request_uri = substr($_SERVER['REQUEST_URI'], strlen($path_to_script));
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    48
// if (strpos($request_uri, '?') !== false)
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    49
// 	$request_uri = substr($request_uri, 0, strpos($request_uri, '?'));
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    50
$request_uri = $paths->getAllParams();
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    51
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    52
$rewritten_url = '';
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    53
$url_parts = array();
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    54
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    55
$pun_rewrite_rules['/^$/'] = 'index.php';
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    56
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    57
// die('<pre>' . htmlspecialchars(print_r($pun_rewrite_rules, true)) . '</pre>');
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    58
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    59
// We go through every rewrite rule
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    60
foreach ($pun_rewrite_rules as $rule => $rewrite_to)
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    61
{
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    62
	// We have a match!
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    63
	if (preg_match($rule, $request_uri))
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    64
	{
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    65
		$rewritten_url = preg_replace($rule, $rewrite_to, $request_uri);
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    66
		$url_parts = explode('?', $rewritten_url);
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    67
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    68
		// If there is a query string
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    69
		if (isset($url_parts[1]))
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    70
		{
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    71
			$query_string = explode('&', $url_parts[1]);
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    72
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    73
			// Set $_GET properly for all of the variables
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    74
			// We also set $_REQUEST if it's not already set
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    75
			foreach ($query_string as $cur_param)
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    76
			{
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    77
				$param_data = explode('=', $cur_param);
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    78
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    79
				if (!isset($_REQUEST[$param_data[0]]))
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    80
					$_REQUEST[$param_data[0]] = urldecode($param_data[1]);
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    81
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    82
				$_GET[$param_data[0]] = urldecode($param_data[1]);
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    83
			}
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    84
		}
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    85
		break;
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    86
	}
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    87
}
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    88
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    89
// If we don't know what to rewrite to, we show a bad request messsage
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    90
if (empty($rewritten_url))
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    91
{
9
a932ce8c4827 More progress, compatibility updates for 1.1.4, etc.
Dan
parents: 6
diff changeset
    92
	header('HTTP/1.1 404 Not Found');
6
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    93
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    94
	// Allow an extension to override the "Bad request" message with a custom 404 page
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    95
	($hook = get_hook('re_page_not_found')) ? eval($hook) : null;
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    96
9
a932ce8c4827 More progress, compatibility updates for 1.1.4, etc.
Dan
parents: 6
diff changeset
    97
	die_friendly('Page not found', '<p>You have requested a forum URL that is invalid. Please press your browser\'s Back button to return to the page from whence you came, or return to the <a href="' . makeUrlNS('Special', 'Forum') . '">forum index</a>.</p>');
6
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    98
}
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
    99
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
   100
// We change $_SERVER['PHP_SELF'] so that it reflects the file we're actually loading
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
   101
$_SERVER['PHP_SELF'] = str_replace('rewrite.php', $url_parts[0], $_SERVER['PHP_SELF']);
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
   102
5e1f1e916419 Big upgrade to 1.3 beta. Basic things are working.
Dan
parents:
diff changeset
   103
require PUN_ROOT.$url_parts[0];