<?php

/*
Plugin Name: Get User By Name (fast)
*/

function wp_validate_auth_cookie($cookie = '') {
	if ( empty($cookie) ) {
		global $bb;
		if ( empty($_COOKIE[$bb->authcookie]) ) {return false;}
		$cookie = $_COOKIE[$bb->authcookie];
	}

	$cookie_elements = explode('|', $cookie);
	if ( count($cookie_elements) != 3 ) {return false;}

	list($username, $expiration, $hmac) = $cookie_elements;

	$expired = $expiration;

	// Allow a grace period for POST and AJAX requests
	if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] )
		$expired += 3600;

	if ( $expired < time() ) {return false;}

	$key = wp_hash($username . '|' . $expiration);
	$hash = hash_hmac('md5', $username . '|' . $expiration, $key);
	
	if ( $hmac != $hash ) {return false;}
	
	$username = sanitize_user( $username, true );
	global $bbdb;
	if ( $user = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->users WHERE user_login = %s", $username ) ) ) {		
		bb_append_meta( $user, 'user' );
	}
	
	if ( empty($user->ID) ) {return false;}

	return $user->ID;
}

?>
