<?php

/* Functions to read, write, compute cookies */


if( !defined('COOKIEHASH') )
	define('COOKIEHASH', md5('megatokyo') );  // Generate a slightly more obscure cookie name
if ( !defined('USER_COOKIE') )
	define('USER_COOKIE', 'megatokyoadmin_user_'. COOKIEHASH);
if ( !defined('PASS_COOKIE') )
	define('PASS_COOKIE', 'megatokyoadmin_pass_'. COOKIEHASH);

if ( !defined('COOKIEPATH') )
	define('COOKIEPATH', ADMIN_PATH . '/' );
if ( !defined('COOKIE_DOMAIN') )
	define('COOKIE_DOMAIN', false);

// Get the username and password stored in the cookie
function mt_get_cookie_login() {
	if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) )
		return false;

	return array('login' => $_COOKIE[USER_COOKIE],	'password' => $_COOKIE[PASS_COOKIE]);
}

// Store username and password in a cookie
function mt_setcookie($username, $password, $already_md5 = false, $siteurl = '', $remember = false) {
	if ( !$already_md5 )
		$password = mt_hash_password($password);

	if ( empty($siteurl) )
		$cookiepath = COOKIEPATH;
	else
		$cookiepath = preg_replace('|https?://[^/]+|i', '', $siteurl . '/' );

	if ( $remember )
		$expire = time() + 31536000;
	else
		$expire = 0;

	setcookie(USER_COOKIE, $username, $expire, $cookiepath );
	setcookie(PASS_COOKIE, $password, $expire, $cookiepath );
}

// Force the cookies to expire
function mt_clearcookie() {
	setcookie(USER_COOKIE, ' ', time() - 36000, COOKIEPATH );
	setcookie(PASS_COOKIE, ' ', time() - 36000, COOKIEPATH );
}

?>
