<?php
include("require.php");
if(!isset($_GET['action']) and !isset($_COOKIE["core_user_name"]))
  Header("Location: login.php?action=login&last=index.php");
elseif($_GET['action'] == "submit")
{
  $_POST['username']=submitfilter(2,$_POST['username']);
  $phpcolor->table=$core_mysql_prefix."user";
  $phpcolor->field="username,passwd,rank";
  $phpcolor->where="username = '".$_POST['username']."' and state != '0' and state != '3'";
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*60*60);
  if(!$userinfo=$phpcolor->exist(1))
    die("0");
  elseif($_POST['username']=="" || $_POST['passwd']=="")
    die("1");
  elseif($core_verifyopen==1 && $_POST['verify'] != $_SESSION['auth'])
    die("2");
  elseif(md5($_POST['passwd']) != $userinfo['passwd'])
  {
    if($userinfo["rank"]==2)
	{
	  $phpcolor->table=$core_mysql_prefix."log";
      $phpcolor->field="operate, username, operatetime, ip, type";
      $phpcolor->where="'登陆失败：密码错误(".$_POST['passwd'].")', '".$userinfo['username']."','".$time."','".$_SERVER['REMOTE_ADDR']."' , '1'";
      $phpcolor->write();
	}
    die("3");
  }
  else
  {
    setcookie("core_user_name", $userinfo['username'], time()+$core_time*60*60+3600);
    setcookie("core_user_passwd", md5($userinfo['passwd'].$core_scode), time()+$core_time*60*60+3600);
	setcookie("core_user_rank", $userinfo['rank'], time()+$core_time*60*60+3600);
	setcookie("core_user_long", "0", time()+$core_time*60*60+3600);
    if(isset($_POST['long']) && $_POST['long']=="1")
	{
      setcookie("core_user_name", $userinfo['username'], time()+$core_time*60*60+604800);
      setcookie("core_user_passwd", md5($userinfo['passwd'].$core_scode), time()+$core_time*60*60+604800);
	  setcookie("core_user_rank", $userinfo['rank'], time()+$core_time*60*60+604800);
	  setcookie("core_user_long", "1", time()+$core_time*60*60+604800);
	}
	if($userinfo['rank']==2)
	{
	  $phpcolor->table=$core_mysql_prefix."log";
      $phpcolor->field="operate, username, operatetime, ip, type";
      $phpcolor->where="'登陆成功，在前台登陆', '".$userinfo["username"]."','".$time."','".$_SERVER['REMOTE_ADDR']."' , '1'";
      $phpcolor->write();
	  $phpcolor->table=$core_mysql_prefix."user";
	}
	$phpcolor->field="logintime='".$time."', loginip='".$_SERVER['REMOTE_ADDR']."'";
    $phpcolor->where="username = '".$userinfo['username']."'";
    $phpcolor->upgrade();
    die("4");
  }
}
elseif($_GET['action'] == "quit" or isset($_COOKIE['core_user_name']))
{
  setcookie("core_user_name", "", time()-10000000);
  setcookie("core_user_passwd", "", time()-10000000);
  setcookie("core_user_rank", "", time()-10000000);
  setcookie("core_user_long", "", time()-10000000);
  Header("Location: ".$_GET['last']);
}
else
{
  $tpl -> assign("jump", $_GET["last"]);
  $tpl -> assign("core_verifyopen", $core_verifyopen);
  $tpl -> assign("core_header_title", "登录 - ");
  $tpl -> display("$core_style/login.htm");
}
?>