home *** CD-ROM | disk | FTP | other *** search
/ Clickx 23 / Clickx 23.iso / DATA / wordpress / wp-blog-header.php < prev    next >
Encoding:
PHP Script  |  2005-05-13  |  8.4 KB  |  264 lines

  1. <?php
  2.  
  3. if (! isset($wp_did_header)):
  4. if ( !file_exists( dirname(__FILE__) . '/wp-config.php') ) {
  5.     if ( strstr( $_SERVER['PHP_SELF'], 'wp-admin') ) $path = '';
  6.     else $path = 'wp-admin/';
  7.     die("There doesn't seem to be a <code>wp-config.php</code> file. I need this before we can get started. Need more help? <a href='http://wordpress.org/docs/faq/#wp-config'>We got it</a>. You can <a href='{$path}setup-config.php'>create a <code>wp-config.php</code> file through a web interface</a>, but this doesn't work for all server setups. The safest way is to manually create the file.");
  8. }
  9.  
  10. $wp_did_header = true;
  11.  
  12. require_once( dirname(__FILE__) . '/wp-config.php');
  13.  
  14. $query_vars = array();
  15.  
  16. // Process PATH_INFO and 404.
  17. if ((isset($_GET['error']) && $_GET['error'] == '404') ||
  18.         ((! empty($_SERVER['PATH_INFO'])) &&
  19.         ('/' != $_SERVER['PATH_INFO']) &&
  20.          (false === strpos($_SERVER['PATH_INFO'], '.php'))
  21.         )) {
  22.  
  23.     // If we match a rewrite rule, this will be cleared.
  24.     $error = '404';
  25.  
  26.     // Fetch the rewrite rules.
  27.     $rewrite = $wp_rewrite->wp_rewrite_rules();
  28.  
  29.     if (! empty($rewrite)) {
  30.         $pathinfo = $_SERVER['PATH_INFO'];
  31.         $req_uri = $_SERVER['REQUEST_URI'];      
  32.         $home_path = parse_url(get_settings('home'));
  33.         $home_path = $home_path['path'];
  34.  
  35.         // Trim path info from the end and the leading home path from the
  36.         // front.  For path info requests, this leaves us with the requesting
  37.         // filename, if any.  For 404 requests, this leaves us with the
  38.         // requested permalink.    
  39.         $req_uri = str_replace($pathinfo, '', $req_uri);
  40.         $req_uri = str_replace($home_path, '', $req_uri);
  41.         $req_uri = trim($req_uri, '/');
  42.         $pathinfo = trim($pathinfo, '/');
  43.  
  44.         // The requested permalink is in $pathinfo for path info requests and
  45.         //  $req_uri for other requests.
  46.         if (! empty($pathinfo)) {
  47.             $request = $pathinfo;
  48.         } else {
  49.             $request = $req_uri;
  50.         }
  51.  
  52.         // Look for matches.
  53.         $request_match = $request;
  54.         foreach ($rewrite as $match => $query) {
  55.             // If the requesting file is the anchor of the match, prepend it
  56.             // to the path info.
  57.         if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0)) {
  58.           $request_match = $req_uri . '/' . $request;
  59.         }
  60.  
  61.             if (preg_match("!^$match!", $request_match, $matches)) {
  62.                 // Got a match.
  63.                 // Trim the query of everything up to the '?'.
  64.                 $query = preg_replace("!^.+\?!", '', $query);
  65.  
  66.                 // Substitute the substring matches into the query.
  67.                 eval("\$query = \"$query\";");
  68.  
  69.                 // Parse the query.
  70.                 parse_str($query, $query_vars);
  71.  
  72.                 // If we're processing a 404 request, clear the error var
  73.                 // since we found something.
  74.                 if (isset($_GET['error'])) {
  75.                     unset($_GET['error']);
  76.                 }
  77.  
  78.                 if (isset($error)) {
  79.                     unset($error);
  80.                 }
  81.  
  82.                 break;
  83.             }
  84.         }
  85.     }
  86.  }
  87.  
  88. $wpvarstoreset = array('m','p','posts','w', 'cat','withcomments','s','search','exact', 'sentence', 'debug', 'calendar','page','paged','more','tb', 'pb','author','order','orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup');
  89.  
  90. $wpvarstoreset = apply_filters('query_vars', $wpvarstoreset);
  91.  
  92. for ($i=0; $i<count($wpvarstoreset); $i += 1) {
  93.     $wpvar = $wpvarstoreset[$i];
  94.     if (!isset($$wpvar)) {
  95.         if (empty($_POST[$wpvar])) {
  96.             if (empty($_GET[$wpvar]) && empty($query_vars[$wpvar])) {
  97.                 $$wpvar = '';
  98.             } elseif (!empty($_GET[$wpvar])) {
  99.                 $$wpvar = $_GET[$wpvar];
  100.             } else {
  101.                 $$wpvar = $query_vars[$wpvar];
  102.             }
  103.         } else {
  104.             $$wpvar = $_POST[$wpvar];
  105.         }
  106.     }
  107. }
  108.  
  109. // Sending HTTP headers
  110.  
  111. if ( !empty($error) && '404' == $error ) {
  112.     if ( preg_match('/cgi/', php_sapi_name()) )
  113.         @header('Status: 404 Not Found');
  114.     else
  115.         @header('HTTP/1.x 404 Not Found');
  116.  } else if ( empty($feed) ) {
  117.     @header('X-Pingback: '. get_bloginfo('pingback_url'));
  118.     @header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
  119. } else {
  120.     // We're showing a feed, so WP is indeed the only thing that last changed
  121.     if ( $withcomments )
  122.         $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT';
  123.     else 
  124.         $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
  125.     $wp_etag = '"' . md5($wp_last_modified) . '"';
  126.     @header("Last-Modified: $wp_last_modified");
  127.     @header("ETag: $wp_etag");
  128.     @header('X-Pingback: ' . get_bloginfo('pingback_url'));
  129.  
  130.     // Support for Conditional GET
  131.     if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) $client_etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
  132.     else $client_etag = false;
  133.  
  134.     $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE']);
  135.     // If string is empty, return 0. If not, attempt to parse into a timestamp
  136.     $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
  137.  
  138.     // Make a timestamp for our most recent modification...    
  139.     $wp_modified_timestamp = strtotime($wp_last_modified);
  140.  
  141.     if ( ($client_last_modified && $client_etag) ?
  142.         (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
  143.         (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
  144.         if ( preg_match('/cgi/',php_sapi_name()) ) {
  145.             header('Status: 304 Not Modified');
  146.             echo "\r\n\r\n";
  147.             exit;
  148.         } else {
  149.             if ( version_compare(phpversion(), '4.3.0', '>=') )
  150.                 header('Not Modified', TRUE, 304);
  151.             else
  152.                 header('HTTP/1.x 304 Not Modified');
  153.             exit;
  154.         }
  155.     }
  156. }
  157.  
  158. $use_gzipcompression = get_settings('gzipcompression');
  159.  
  160. $more_wpvars = array('posts_per_page', 'posts_per_archive_page', 'what_to_show', 'showposts', 'nopaging');
  161.  
  162. // Construct the query string.
  163. $query_string = '';
  164. foreach (array_merge($wpvarstoreset, $more_wpvars) as $wpvar) {
  165.     if ($$wpvar != '') {
  166.         $query_string .= (strlen($query_string) < 1) ? '' : '&';
  167.         $query_string .= $wpvar . '=' . rawurlencode($$wpvar);
  168.     }
  169. }
  170.  
  171. $query_string = apply_filters('query_string', $query_string);
  172.  
  173. update_category_cache();
  174. get_currentuserinfo();
  175.  
  176. // Call query posts to do the work.
  177. $posts = & query_posts($query_string);
  178.  
  179. // Extract updated query vars back into global namespace.
  180. extract($wp_query->query_vars);
  181.  
  182. if ( is_single() || is_page() ) {
  183.     $more = 1;
  184.     $single = 1;
  185. }
  186.  
  187. // Issue a 404 if a permalink request doesn't match any posts.  Don't issue a
  188. // 404 if one was already issued, if the request was a search, or if the
  189. // request was a regular query string request rather than a permalink request.
  190. if ( (0 == count($posts)) && !is_404() && !is_search()
  191.         && ( isset($rewrite) || (!empty($_SERVER['QUERY_STRING']) &&
  192.         (false === strpos($_SERVER['REQUEST_URI'], '?'))) ) ) {
  193.     $wp_query->is_404 = true;
  194.     if ( preg_match('/cgi/', php_sapi_name()) )
  195.         @header('Status: 404 Not Found');
  196.     else
  197.         @header('HTTP/1.x 404 Not Found');
  198. }
  199.  
  200. if ($pagenow != 'post.php' && $pagenow != 'edit.php') {
  201.     if ( get_settings('gzipcompression') ) 
  202.         gzip_compression();
  203. }
  204.  
  205. // Template redirection
  206. if ( defined('WP_USE_THEMES') && constant('WP_USE_THEMES') ) {
  207.     do_action('template_redirect');
  208.     if ( is_feed() && empty($doing_rss) ) {
  209.         include(ABSPATH . '/wp-feed.php');
  210.         exit;
  211.     } else if ( is_trackback() && empty($doing_trackback) ) {
  212.         include(ABSPATH . '/wp-trackback.php');
  213.         exit;
  214.     } else if ( is_404() && get_404_template() ) {
  215.         include(get_404_template());
  216.         exit;
  217.     } else if ( is_search() && get_search_template() ) {
  218.         include(get_search_template());
  219.         exit;
  220.     } else if ( is_home() && get_home_template() ) {
  221.         include(get_home_template());
  222.         exit;
  223.     } else if ( is_single() && get_single_template() ) {
  224.         include(get_single_template());
  225.         exit;
  226.     } else if ( is_page() && get_page_template() ) {
  227.         include(get_page_template());
  228.         exit;
  229.     } else if ( is_category() && get_category_template()) {
  230.         include(get_category_template());
  231.         exit;        
  232.     } else if ( is_author() && get_author_template() ) {
  233.         include(get_author_template());
  234.         exit;
  235.     } else if ( is_date() && get_date_template() ) {
  236.         include(get_date_template());
  237.         exit;
  238.     } else if ( is_archive() && get_archive_template() ) {
  239.         include(get_archive_template());
  240.         exit;
  241.     } else if ( is_comments_popup() && get_comments_popup_template() ) {
  242.         include(get_comments_popup_template());
  243.         exit;
  244.     } else if ( is_paged() && get_paged_template() ) {
  245.         include(get_paged_template());
  246.         exit;
  247.     } else if ( file_exists(TEMPLATEPATH . "/index.php") ) {
  248.         include(TEMPLATEPATH . "/index.php");
  249.         exit;
  250.     }
  251. } else {
  252.     // Process feeds and trackbacks even if not using themes.
  253.     if ( is_feed() && empty($doing_rss) ) {
  254.         include(ABSPATH . '/wp-feed.php');
  255.         exit;
  256.     } else if ( is_trackback() && empty($doing_trackback) ) {
  257.         include(ABSPATH . '/wp-trackback.php');
  258.         exit;
  259.     }
  260. }
  261.  
  262. endif;
  263. ?>
  264.