<?php

/* these functions allow this template to work properly in all scenarios */

function d($s) {
		global $wp; global $wp_query;
		header("Content-Type: text/plain; charset=utf-8");
		die("\n".$s."\n");
}


/* overriding classes */

class RevvedUp_WP extends WP {

	function parse_request($extra_query_vars = '') {
		$this->public_query_vars[] = "template_page";
		parent::parse_request($extra_query_vars);

		if ($this->query_vars["template_page"]) {
			$tentative_file_name = TEMPLATEPATH . "/" . $this->query_vars["template_page"] . ".php";
			if (is_readable($tentative_file_name)) {
				global $wp_query;
				$wp_query->is_template_page = true;
				$wp_query->requested_template_page = $this->query_vars["template_page"];
			} else {
				// aca hay que simular un 404 porque no hubo el template solicitado
				$this->query_vars['error'] = 404;
			}
		}
	}

	function query_posts() {
		parent::query_posts();
		if (is_template_page()) {
			global $wp_query;
			$wp_query->is_single = null;
			$wp_query->is_preview  = null;
			$wp_query->is_page  = null;
			$wp_query->is_archive  = null;
			$wp_query->is_date  = null;
			$wp_query->is_year  = null;
			$wp_query->is_month  = null;
			$wp_query->is_day  = null;
			$wp_query->is_time  = null;
			$wp_query->is_author  = null;
			$wp_query->is_category  = null;
			$wp_query->is_search  = null;
			$wp_query->is_feed  = null;
			$wp_query->is_trackback  = null;
			$wp_query->is_home  = null;
			$wp_query->is_404  = null;
			$wp_query->is_comments_popup  = null;
			$wp_query->is_admin  = null;
			$wp_query->is_attachment  = null;
			$wp_query->is_paged  = null;
		}
	}

	function handle_404() {
		/* forces showing of 404 page if a redirect status error is present */
		/* 404.php will handle special cases */

		parent::handle_404();
		
		global $wp_query;
		$rstatus = $_SERVER["REDIRECT_STATUS"];
		if ($rstatus && $rstatus != 200 && $rstatus != 304) {
			$wp_query->set_404();
			$wp_query->redirect_status = intval($rstatus);
			global $known_http_errors;
			$errarray = $known_http_errors[intval($rstatus)];
			$errtitle = $errarray[0];
			$header = "$rstatus ".$errtitle;
			status_header( $header );
		} elseif (is_404()) {
			$wp_query->redirect_status = 404;
		}

	}

}

class RevvedUp_WP_Query extends WP_Query {
	var $is_template_page = false;
	var $requested_template_page = null;
	var $redirect_status = null;
}

function init_revvedup() {
	global $wp; $wp = new RevvedUp_WP();
	global $wp_query; $wp_query = new RevvedUp_WP_Query();
}

function add_revvedup_rewrite_rules($arr) {
	$arr["([a-z]+)$"] = 'index.php?template_page=$matches[1]';
	$arr["([a-z]+)/$"] = 'index.php?template_page=$matches[1]';
	return $arr;
}

function override_template_redirect_with_revvedup() {
	if (is_template_page()) {
			require(TEMPLATEPATH . "/" . get_template_page_name() . ".php");
			die;
	}
}

function generate_mod_rewrite_rules_for_revvedup($rules) {
	$site_root = parse_url(get_settings('siteurl'));
	$site_root = trailingslashit($site_root['path']);
	$baseindexphp = $site_root . "index.php";
	$rules .= "DirectoryIndex index.php index.html $baseindexphp?template_page=directoryindex
ErrorDocument 400 $baseindexphp
ErrorDocument 401 $baseindexphp
ErrorDocument 403 $baseindexphp
ErrorDocument 404 $baseindexphp
ErrorDocument 405 $baseindexphp
ErrorDocument 408 $baseindexphp
ErrorDocument 410 $baseindexphp
ErrorDocument 411 $baseindexphp
ErrorDocument 412 $baseindexphp
ErrorDocument 413 $baseindexphp
ErrorDocument 414 $baseindexphp
ErrorDocument 415 $baseindexphp
ErrorDocument 500 $baseindexphp
ErrorDocument 501 $baseindexphp
ErrorDocument 502 $baseindexphp
ErrorDocument 503 $baseindexphp
ErrorDocument 506 $baseindexphp
";

	if ($site_root == "/") {
		$pathtorevvedup = get_template_directory_uri();
		$rules .= "<IfModule mod_rewrite.c>
RewriteRule ^favicon.ico$ $pathtorevvedup/images/favicon.ico [QSA,L]
</IfModule>
";
	}

	return $rules;
}

add_action("init","init_revvedup");
add_filter('rewrite_rules_array','add_revvedup_rewrite_rules');
add_action("template_redirect","override_template_redirect_with_revvedup");
add_filter("mod_rewrite_rules","generate_mod_rewrite_rules_for_revvedup");



/* HTTP error handling */

$known_http_errors = array(
	400 => array("Bad request",
	"Your web browser performed a request this server could not understand.  There is also the possibility that you are a (sadly unsuccessful) hacker, trying to do nasty things."),
	401 => array("Unauthorized",
	"The page you were trying to load requires authorization."),
	403 => array("Forbidden",
	"The file you were trying to access is off-limits for this server."),
	404 => array("Resource not found",
	"The page you were trying to access cannot be found."),
	405 => array("Method not allowed",
	"The HTTP method you requested is not allowed for this resource"),
	408 => array("Request timed out",
	"The server closed the network connection because the browser didn't finish the request within the specified time."),
	410 => array("No longer at this address",
	"The requested URL is no longer available on this server and there is no forwarding address."),
	411 => array("Bad Content-Length",
	"The issued request requires a valid Content-Length header."),
	412 => array("Precondition failed",
	"The precondition on the request for the URL failed positive evaluation."),
	413 => array("Request entity too large",
	"The requested method does not allow the data transmitted, or the data volume exceeds the capacity limit."),
	414 => array("Submitted URI too large",
	"The length of the requested URL exceeds the capacity limit for this server. The request cannot be processed."),
	415 => array("Unsupported media type",
	"The server does not support the media type transmitted in the request."),
	500 => array("Internal server error",
	"An unknown error took place while generating the page you requested."),
	501 => array("Cannot process request",
	" The server does not support the action requested by the browser."),
	502 => array("Bad gateway",
	"The proxy server received an invalid response from an upstream server."),
	503 => array("Service unavailable",
	"This service is temporarily disabled due to technical difficulties."),
	506 => array("Variant also varies",
	"A variant for the requested entity is itself a negotiable resource.  Access not possible.")
);

function revvedup_get_http_error_code() {
	global $wp_query;
	return $wp_query->redirect_status;
}

function revvedup_get_http_error_title() {
	$code = revvedup_get_http_error_code();
	global $known_http_errors;
	$errarray = $known_http_errors[$code];
	return $errarray[0];
}

function revvedup_get_http_error_description() {
	$code = revvedup_get_http_error_code();
	global $known_http_errors;
	$errarray = $known_http_errors[$code];
	return $errarray[1];
}

/* template page functions */

function is_template_page() {
	/* return true if this is a template page, false if it isn't */
	global $wp_query; return $wp_query->is_template_page;
}

function get_template_page_name() {
/* return the requested template page file name, null if none */
	global $wp_query; return $wp_query->requested_template_page;
}

function is_archives_page() { return get_template_page_name() == "archives"; }
function is_directory_index() { return get_template_page_name() == "directoryindex"; }

function get_the_archives_permalink() {
	global $wp_rewrite;
	$rewrite = $wp_rewrite->wp_rewrite_rules();
	if ($rewrite) {
		return get_settings('siteurl') . "/archives/";
	} else {
		return get_settings('siteurl') . "/index.php?template_page=archives";
	}
}
function the_archives_permalink() { print get_the_archives_permalink(); }


/* integration of useronline */
function is_useronline() { global $bots; global $usersonline; return isset($bots) && isset($usersonline); }
/* fixme a better integration would be to put it right into the template as a special page, and that's it */



?>