<?php
/*********************************************************
程序名称：站长助手 WEBMASTER HELPER
作者：feixin
email:kusite@etang.com
版权：feixin (http://www.zc18.com)
//////////////////////////////////////////////////////////
文件名：auth.php
此文件功能描述：用户权限操作类
最后编辑日期：2003-6-22
*********************************************************/
if (!defined('IN_WH')) {
  die("不要黑我啊");
}

if(!defined('INCLUDE_WH_AUTH_CLASS'))
{
	define('INCLUDE_WH_AUTH_CLASS', TRUE);
//---------------------------------------

class WHauth {
	var $user;
	var $pw;
	var $time;

	function WHauth($loging = "login")
	{
		$method = "cookie";//cookie, http, session,现在只支持cookie
		//if(empty($method) && $method != "cookie" && $method != "http" && $method != "session"){ $method = "cookie";}

		$this->time = time()+3600;
		if($loging != "login"){
			$logout_function = $method."_logout";
			$this->$logout_function();
		}
		else
		{
			$auth_function = $method."_auth";
			
			$login = $this->$auth_function();

			if(!$login){
				$login_function = $method."_login_form";
				$this->$login_function();
				exit;
			}
		}

	}

	function cookie_logout()
	{
		if(!$this->cookie_auth()){return false;}
		global $HTTP_COOKIE_VARS;
		unset($HTTP_COOKIE_VARS['WH_cookie_pw'], $HTTP_COOKIE_VARS['WH_cookie_user']);
		$this->user = $this->pw = NULL;
		
		$this->cookie_set_auth();

		global $WH;
		$WH->header();
		$WH->message("<b>你已经退出系统！</b>", "", 1);
	}
		
	
	function cookie_login_form()
	{
		global $HTTP_COOKIE_VARS, $WH;
		$logintime = $HTTP_COOKIE_VARS['WH_cookie_login_times'];
		if($logintime > 10){
			$WH->header();
			$WH->message("晕，你已经尝试登录10次失败了！手工修改config.php文件吧！或清除cookie重新尝试！", "", 1);
		}
		if(empty($logintime)){ $logintime = 0;}
		$html ='<b>请先登录</b>&nbsp;你已尝试登录<b>'.$logintime.'</b>次
			<form method="post" action="index.php" name="login_form">
			名字(随便，但不能空)：<br /><input type="text" name="wh_username" value="WH_user" size="24" onfocus="this.select()" /><br />
			密码:<br /><input type="password" name="wh_password" value="" size="24" onfocus="this.select()" /><br /><input type="submit" name=submit value="确认">
				</form>';

		$WH->header();
		$WH->message($html, "", 1);
	}


	function cookie_auth()
	{
		global $HTTP_COOKIE_VARS, $HTTP_POST_VARS;

		if(!$HTTP_POST_VARS['wh_password']){

			if (!empty($_COOKIE) && isset($_COOKIE['WH_cookie_user'])) {
				$this->user = $_COOKIE['WH_cookie_user'];
			}
			else if (!empty($HTTP_COOKIE_VARS) && isset($HTTP_COOKIE_VARS['WH_cookie_user'])) {
				$this->user = $HTTP_COOKIE_VARS['WH_cookie_user'];
			}

			if (!empty($_COOKIE) && isset($_COOKIE['WH_cookie_pw'])) {
				$this->pw = $_COOKIE['WH_cookie_pw'];
			}
			else if (!empty($HTTP_COOKIE_VARS) && isset($HTTP_COOKIE_VARS['WH_cookie_pw'])) {
				$this->pw = $HTTP_COOKIE_VARS['WH_cookie_pw'];
			}

			if(empty($this->user)){ $this->user = "WH_user";}

			if($this->pw == WH_LOGIN_PW){return true;}
			else { return false;}
		}
		else
		{

			if($HTTP_POST_VARS['wh_username'] != "" && MD5($HTTP_POST_VARS['wh_password']) == WH_LOGIN_PW){
				$this->user = $wh_username;
				$this->pw = WH_LOGIN_PW;
				$this->cookie_set_auth();
				global $WH;
				$WH->header();
				$WH->message("登录成功", "index.php", 1, 5);
				return true;
			} else { 
				$logintime = $HTTP_COOKIE_VARS['WH_cookie_login_times'];
				$logintime++;
				setcookie('WH_cookie_login_times',
                $logintime,
                $this->time,
                "/", '',
                0);
				return false;
			}
		}


	}


	function cookie_set_auth()
	{
		setcookie('WH_cookie_pw',
                $this->pw,
                $this->time,
                "/", '',
                0);

		setcookie('WH_cookie_login_times',
                0,
                $this->time,
                "/", '',
                0);

		setcookie('WH_cookie_user',
                $this->user,
                $this->time,
                "/", '',
                0);



	}

	function http_login_form()
	{
		//

	}

	function http_auth()
	{
		//
	}

	function http_set_auth()
	{
		//
	}

	function session_auth()
	{
		//
	}


}
	


}//END IF
?>