From b70022525e8c7bf5988812992fe421eb6d7980b8 Mon Sep 17 00:00:00 2001 From: rboren Date: Tue, 17 Feb 2004 02:50:57 +0000 Subject: [PATCH] Support PATH_INFO permalinks. Move rewrite rule generation to rewrite_rules(). git-svn-id: http://svn.automattic.com/wordpress/trunk@881 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-blog-header.php | 48 ++++++++++++- wp-includes/functions.php | 139 +++++++++++++++++++++++++++++++++++++- 2 files changed, 184 insertions(+), 3 deletions(-) diff --git a/wp-blog-header.php b/wp-blog-header.php index 9d5f3711b0..d6f83a1db3 100644 --- a/wp-blog-header.php +++ b/wp-blog-header.php @@ -10,16 +10,60 @@ if (!file_exists($curpath . '/wp-config.php')) require($curpath.'/wp-config.php'); +/* Process PATH_INFO, if set. */ +$path_info = array(); +if (! empty($_SERVER['PATH_INFO'])) { + // Fetch the rewrite rules. + $rewrite = rewrite_rules('matches'); + + $pathinfo = $_SERVER['PATH_INFO']; + // Trim leading '/'. + $pathinfo = preg_replace("!^/!", '', $pathinfo); + + if (! empty($rewrite)) { + // Get the name of the file requesting path info. + $req_uri = $HTTP_SERVER_VARS['REQUEST_URI']; + $req_uri = str_replace($pathinfo, '', $req_uri); + $req_uri = preg_replace("!/+$!", '', $req_uri); + $req_uri = explode('/', $req_uri); + $req_uri = $req_uri[count($req_uri)-1]; + + // Look for matches. + $pathinfomatch = $pathinfo; + foreach ($rewrite as $match => $query) { + // If the request URI is the anchor of the match, prepend it + // to the path info. + if (preg_match("!^$req_uri!", $match)) { + $pathinfomatch = $req_uri . '/' . $pathinfo; + } + + if (preg_match("!^$match!", $pathinfomatch, $matches)) { + // Got a match. + // Trim the query of everything up to the '?'. + $query = preg_replace("!^.+\?!", '', $query); + + // Substitute the substring matches into the query. + eval("\$query = \"$query\";"); + + // Parse the query. + parse_str($query, $path_info); + } + } + } +} + $wpvarstoreset = array('m','p','posts','w', 'cat','withcomments','s','search','exact', 'sentence','poststart','postend','preview','debug', 'calendar','page','paged','more','tb', 'pb','author','order','orderby', 'year', 'monthnum', 'day', 'name', 'category_name', 'feed', 'author_name'); for ($i=0; $i \ No newline at end of file +/* rewrite_rules + * Construct rewrite matches and queries from permalink structure. + * matches - The name of the match array to use in the query strings. + * If empty, $1, $2, $3, etc. are used. + * Returns an associate array of matches and queries. + */ +function rewrite_rules($matches = '') { + + function preg_index($number, $matches = '') { + $match_prefix = '$'; + $match_suffix = ''; + + if (! empty($matches)) { + $match_prefix = '$' . $matches . '['; + $match_suffix = ']'; + } + + return "$match_prefix$number$match_suffix"; + } + + $rewrite = array(); + + $permalink_structure = get_settings('permalink_structure'); + + if (empty($permalink_structure)) { + return $rewrite; + } + + $rewritecode = array( + '%year%', + '%monthnum%', + '%day%', + '%postname%', + '%post_id%' + ); + + $rewritereplace = array( + '([0-9]{4})?', + '([0-9]{1,2})?', + '([0-9]{1,2})?', + '([0-9a-z-]+)?', + '([0-9]+)?' + ); + + $queryreplace = array ( + 'year=', + 'monthnum=', + 'day=', + 'name=', + 'p=' + ); + + + $match = str_replace('/', '/?', $permalink_structure); + $match = preg_replace('|/[?]|', '', $match, 1); + + $match = str_replace($rewritecode, $rewritereplace, $match); + $match = preg_replace('|[?]|', '', $match, 1); + + $feedmatch = str_replace('?/?', '/', $match); + $trackbackmatch = $feedmatch; + + preg_match_all('/%.+?%/', $permalink_structure, $tokens); + + $query = 'index.php?'; + $feedquery = 'wp-feed.php?'; + $trackbackquery = 'wp-trackback.php?'; + for ($i = 0; $i < count($tokens[0]); ++$i) { + if (0 < $i) { + $query .= '&'; + $feedquery .= '&'; + $trackbackquery .= '&'; + } + + $query_token = str_replace($rewritecode, $queryreplace, $tokens[0][$i]) . preg_index($i+1, $matches); + $query .= $query_token; + $feedquery .= $query_token; + $trackbackquery .= $query_token; + } + ++$i; + + // Add post paged stuff + $match .= '([0-9]+)?/?'; + $query .= '&page=' . preg_index($i, $matches); + + // Add post feed stuff + $feedregex = '(feed|rdf|rss|rss2|atom)/?'; + $feedmatch .= $feedregex; + $feedquery .= '&feed=' . preg_index($i, $matches); + + // Add post trackback stuff + $trackbackregex = 'trackback/?'; + $trackbackmatch .= $trackbackregex; + + // Site feed + $sitefeedmatch = 'feed/?([0-9a-z-]+)?/?$'; + $sitefeedquery = $site_root . 'wp-feed.php?feed=' . preg_index(1, $matches); + + // Site comment feed + $sitecommentfeedmatch = 'comments/feed/?([0-9a-z-]+)?/?$'; + $sitecommentfeedquery = $site_root . 'wp-feed.php?feed=' . preg_index(1, $matches) . '&withcomments=1'; + + // Code for nice categories and authors, currently not very flexible + $front = substr($permalink_structure, 0, strpos($permalink_structure, '%')); + $catmatch = $front . 'category/'; + $catmatch = preg_replace('|^/+|', '', $catmatch); + + $catfeedmatch = $catmatch . '(.*)/' . $feedregex; + $catfeedquery = 'wp-feed.php?category_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches); + + $catmatch = $catmatch . '?(.*)'; + $catquery = 'index.php?category_name=' . preg_index(1, $matches); + + $authormatch = $front . 'author/'; + $authormatch = preg_replace('|^/+|', '', $authormatch); + + $authorfeedmatch = $authormatch . '(.*)/' . $feedregex; + $authorfeedquery = 'wp-feed.php?author_name=' . preg_index(1, $matches) . '&feed=' . preg_index(2, $matches); + + $authormatch = $authormatch . '?(.*)'; + $authorquery = 'index.php?author_name=' . preg_index(1, $matches); + + $rewrite = array( + $catfeedmatch => $catfeedquery, + $catmatch => $catquery, + $authorfeedmatch => $authorfeedquery, + $authormatch => $authorquery, + $match => $query, + $feedmatch => $feedquery, + $trackbackmatch => $tracbackquery, + $sitefeedmatch => $sitefeedquery, + $sitecommentfeedmatch => $sitecommentfeedquery + ); + + return $rewrite; +} + +?>