<?php
include("libs.php");
include(dirname(dirname(__FILE__))."/./configs/config.php");
include(dirname(dirname(__FILE__))."/./configs/db_config.php");
include(dirname(dirname(__FILE__))."/./includes/class.inc.php");
include(dirname(dirname(__FILE__))."/./includes/function.php");
header("Content-Type:text/html;charset=utf-8");
date_default_timezone_set('PRC');
$phpcolor = new mysql();
$phpcolor->open($core_mysql_host,$core_mysql_user,$core_mysql_pw,$core_mysql_dbname);
if(!isset($_GET['action']))
  Header("Location: login.php?action=login");
else
{
  $_POST['username']=submitfilter(2,$_POST['username']);
  if($_GET['action']!="quit" and isset($_COOKIE['core_user_name']))
    Header("Location: login.php?action=quit");
  elseif($_GET['action'] == "submit")
  {
	$phpcolor->table=$core_mysql_prefix."user";
    $phpcolor->field="username,passwd,rank";
    $phpcolor->where="username = '".$_POST['username']."'";
    $userinfo=$phpcolor->exist(1);
    $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*60*60);
    if(empty($_POST['username']) or empty($_POST['passwd']))
    {
	  $phpcolor->table=$core_mysql_prefix."log";
      $phpcolor->field="operate, username, operatetime, ip, type";
      $phpcolor->where="'登陆失败，未将登陆信息填写完整', '".$_POST['username']."', '".$time."','".$_SERVER['REMOTE_ADDR']."' , '1'";
      $phpcolor->write();
      Header("Location: ../error.php?id=1");
    }
    elseif(md5($_POST['passwd']) != $userinfo['passwd'] or $userinfo['rank']!=2)
    {
	  $phpcolor->table=$core_mysql_prefix."log";
      $phpcolor->field="operate, username, operatetime, ip, type";
      $phpcolor->where="'登陆失败，密码错误(".$_POST['passwd'].")', '".$_POST['username']."', '".$time."','".$_SERVER['REMOTE_ADDR']."' , '1'";
      $phpcolor->write();
      Header("Location: ../error.php?id=2");
    }
    elseif($core_verifyopen==1 && $_POST['verify'] != $_SESSION['auth'])
    {
	  $phpcolor->table=$core_mysql_prefix."log";
      $phpcolor->field="operate, username, operatetime, ip, type";
      $phpcolor->where="'登陆失败，验证码错误', '".$_POST['username']."', '".$time."','".$_SERVER['REMOTE_ADDR']."' , '1'";
      $phpcolor->write();
      Header("Location: ../error.php?id=1");
    }
    else
    {
      setcookie("core_user_name", $_POST['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);
	  $phpcolor->table=$core_mysql_prefix."log";
      $phpcolor->field="operate, username, operatetime, ip, type";
      $phpcolor->where="'登陆成功，在后台登陆', '".$_POST['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 = '".$_POST['username']."'";
      $phpcolor->upgrade();
      Header("Location: index.php");
    }
  }
  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: ../login.php?action=login&last=index.php");
  }
  else
  {
    $tpl -> assign("core_verifyopen", $core_verifyopen);
    $tpl -> display("login.htm");
  }
}
?>
