<?php
session_start();
header("Content-Type:text/html;Charset=utf-8");
define('THIS_DIR', $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR);//当前目录
include_once(THIS_DIR.'willow-conn.php');

$username = trim($_POST['willow_username']);
$password = trim($_POST['willow_password']);
$identify = trim($_POST['willow_identify']);
$remember = trim($_POST['willow_remember']);

if($identify == ''){
	echo '<script>alert("请输入验证码");location.href="#LOGIN_PAGE#";</script>';
	die();
}

if(!is_numeric($identify) || strlen($identify) != 4){
	echo '<script>alert("验证码输入有误，请返回重新登陆。");location.href="#LOGIN_PAGE#";</script>';
	die();
}

if($identify  != $_SESSION['willow_session_identify']){
	unset($_SESSION['willow_session_identify']);
	echo '<script>alert("验证码输入有误，请返回重新登陆。");location.href="#LOGIN_PAGE#";</script>';
	die();
}

unset($_SESSION['willow_session_identify']);

if($username == '' || strlen($username) > 20 || strlen($username) < 2 ){
	echo '<script>alert("用户名或密码错误，请返回重试。");location.href="#LOGIN_PAGE#";</script>';
	die();
}

if($password == '' || strlen($password) > 16 || strlen($password) < 4 ){
	echo '<script>alert("用户名或密码错误，请返回重试。");location.href="#LOGIN_PAGE#";</script>';
	die();
}

if($remember != ''){
	
	if($remember != 1){
		echo '<script>alert("错误，请返回重试。");location.href="#LOGIN_PAGE#";</script>';
		die();
	}
	
}

$sql = "SELECT willow_m_user_ID,willow_m_user_name,willow_m_user_password,willow_m_user_group,willow_m_user_edit FROM `#TABLE_PREFIX#willow_m_user` WHERE willow_m_user_name = '".$username."'";

$rs = $mydb->getone($sql);
if(!$rs){
	echo '<script>alert("用户名或密码错误，请返回重试。");location.href="#LOGIN_PAGE#";</script>';
	die();
}

if(md5($password) !== $rs[2]){
	echo '<script>alert("用户名或密码错误，请返回重试。");location.href="#LOGIN_PAGE#";</script>';
	die();
}
if(is_numeric($remember)){
	$endtime = time()+30*24*3600;
	setcookie("#WILLOW_M_COOKIES#[willow_m_users_id]",$rs[0],$endtime,"/");
	setcookie("#WILLOW_M_COOKIES#[willow_m_users_name]",$rs[1],$endtime,"/");
	setcookie("#WILLOW_M_COOKIES#[willow_m_users_edit]",$rs[4],$endtime,"/");
	setcookie("#WILLOW_M_COOKIES#[willow_m_users_group]",$rs[3],$endtime,"/");
}else{
	$_SESSION['#WILLOW_M_COOKIES#']['willow_m_users_id']=$rs[0];
	$_SESSION['#WILLOW_M_COOKIES#']['willow_m_users_name']=$rs[1];
	$_SESSION['#WILLOW_M_COOKIES#']['willow_m_users_edit']=$rs[4];
	$_SESSION['#WILLOW_M_COOKIES#']['willow_m_users_group']=$rs[3];
}
header('Location:#WILLOW_M_DEFAULT#');
/*
------------------------------------
Willow Management System
copyright 2012 Gerry
author Gerry Peng <kkgerry@163.com>
version 1.0
------------------------------------
*/
?>