<?php
/*
Plugin Name: WordPress 2.0.3 Tuneup
Version: 0.5
Plugin URI: http://txfx.net/code/wordpress/wordpress-203-tuneup/
Description: Fixes a number of annoying bugs in WordPress 2.0.3
Author: Mark Jaquith
Author URI: http://txfx.net/
*/

if ( '2.0.3' == get_bloginfo('version') ) {

if ( !function_exists('check_admin_referer') ) :
function check_admin_referer($action = -1) {
	global $pagenow;
	$adminurl = strtolower(get_settings('siteurl')).'/wp-admin';
	$referer = strtolower($_SERVER['HTTP_REFERER']);
	if ( !wp_verify_nonce($_REQUEST['_wpnonce'], $action) &&
		!(-1 == $action && strstr($referer, $adminurl)) ) {
		
		$html  = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>\n<html xmlns='http://www.w3.org/1999/xhtml' lang='en' xml:lang='en'>\n\n";
		$html .= "<head>\n\t<title>" . __('WordPress Confirmation') . "</title>\n";
		$html .= "</head>\n<body>\n";
		// Remove extra layer of slashes.
		$_POST = stripslashes_deep($_POST);
		if ( $_POST ) {
			$q = http_build_query($_POST);
			$q = explode( ini_get('arg_separator.output'), $q);
			$html .= "\t<form method='post' action='$pagenow'>\n";
			foreach ( (array) $q as $a ) {
				$v = substr(strstr($a, '='), 1);
				$k = substr($a, 0, -(strlen($v)+1));
				$html .= "\t\t<input type='hidden' name='" . wp_specialchars( urldecode($k), 1 ) . "' value='" . wp_specialchars( urldecode($v), 1 ) . "' />\n";
			}
			$html .= "\t\t<input type='hidden' name='_wpnonce' value='" . wp_create_nonce($action) . "' />\n";
			$html .= "\t\t<p>" . __('Are you sure you want to do this?') . "</p>\n\t\t<p><a href='$adminurl'>No</a> <input type='submit' value='" . __('Yes') . "' /></p>\n\t</form>\n";
		} else {
			$html .= "\t<p>" . __('Are you sure you want to do this?') . "</p>\n\t\t<p><a href='$adminurl'>No</a> <a href='" . add_query_arg( '_wpnonce', wp_create_nonce($action), $_SERVER['REQUEST_URI'] ) . "'>" . __('Yes') . "</a></p>\n";
		}
		$html .= "</body>\n</html>";

		die($html);
	}
	do_action('check_admin_referer');
}endif;


if ( !function_exists('wp_verify_nonce') ) :
function wp_verify_nonce($nonce, $action = -1) {
	$user = wp_get_current_user();
	$uid = $user->id;

	// 2.0.3 fixes
	if ( 'update-comment' == $action ) {
		global $comment_post_ID, $comment_ID;
		$comment_post_ID = (int) $_POST['comment_post_ID'];
		$comment_ID = (int) $_POST['comment_ID'];
		$action = 'update-comment_' . $comment_ID;
	} elseif ( preg_match('#^(update|delete)-bookmark([0-9]+)$#', $action, $matches) ) {
		$action = $matches[1] . '-bookmark_' . $matches[2];
	}

	$i = ceil(time() / 43200);

	//Allow for expanding range, but only do one check if we can
	if( substr(wp_hash($i . $action . $uid), -12, 10) == $nonce || substr(wp_hash(($i - 1) . $action . $uid), -12, 10) == $nonce )
		return true;
	return false;
}
endif;


if ( !function_exists('wp_create_nonce') ) :
function wp_create_nonce($action = -1) {
	$user = wp_get_current_user();
	$uid = $user->id;

	// 2.0.3 fixes
	if ( preg_match('#^update-comment([0-9]+)$#', $action, $matches) ) {
		$action = 'update-comment_' . $matches[1];
	} elseif ( preg_match('#^update-user_[0-9]+$#', $action, $matches) ) {
		global $user_id;
		$action = 'update-user_' . $user_id;
	}

	$i = ceil(time() / 43200);

	return substr(wp_hash($i . $action . $uid), -12, 10);
}
endif;

if ( strpos($_SERVER['REQUEST_URI'], '/wp-admin/link-manager.php') !== false && $_GET['action'] == 'delete' )
	$_GET['action'] = 'Delete';

} else { // not version 2.0.3
	// deactivate the plugin
	$plugin_path = ABSPATH . 'wp-content' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR;
	$plugin = str_replace($plugin_path, '', __FILE__);
	$current = get_settings('active_plugins');
	array_splice($current, array_search( $plugin, $current), 1 ); // Array-fu!
	update_option('active_plugins', $current);
	do_action('deactivate_' . trim( $plugin ));
}
?>