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