<?php
include("require.php");
if(!isset($_GET['action']) and !isset($_COOKIE["core_user_name"]))
  jump("login.php?action=login&last=index.php");
if($_GET['action'] == "submit")
{
  $_POST['username']=iconv("UTF-8", "gbk", $_POST['username']);
  $phpcolor->table="user";
  $phpcolor->field="*";
  $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*3600);
  if(!$userinfo=$phpcolor->exist(1))
  {
	//整合UC
    if ($uc_open=="1")
    {
	  $phpcolor->close();
      $uc = new mysql();
      $uc->open($uc_dbhost,$uc_dbuser,$uc_dbpw,$uc_dbname);
      $uc->table="members";
	  $uc->field="*";
      $uc->where="username = '".$_POST['username']."'";
      if(!$ucinfo=$phpcolor->exist(1))
	    die("0");
      elseif(md5(md5($_POST['passwd']).$ucinfo['salt']) != $ucinfo['password'])
        die("3");
	  $uc->close();
      $phpcolor->open();
	  $user_reg_passwd = md5($_POST['passwd']);
      $phpcolor->table="user";
	  $phpcolor->field="username, passwd, email, safemail, question, answer, registered, logintime, loginip";
      $phpcolor->where="'".$_POST['username']."', '".$user_reg_passwd."', '".$ucinfo['email']."', '".$ucinfo['email']."', '', '', '".$time."', '".$time."', '".$_SERVER['REMOTE_ADDR']."'";
      $phpcolor->write();
    }
  }
  if($_POST['username']=="" || $_POST['passwd']=="")
    die("1");
  if($core_vlogin==1 && $_POST['verify'] != $_SESSION['auth'])
    die("2");
  if(md5($_POST['passwd']) != $userinfo['passwd'])
  {
    if($userinfo["rank"]==2)
	{
	  $neirong="登陆失败：密码错误(".$_POST['passwd'].")";
      $phpcolor->logwrite($neirong,$userinfo['username'],$time,1);
	}
    die("3");
  }
  $cookieurl=str_replace("http://www.","",$core_url);
  $cookieurl=str_replace("/","",$cookieurl);
  if(isset($_POST['long']) && $_POST['long']=="1")
  {
	setcookie("core_user_name", $userinfo['username'], time()+$core_time*3600+604800,"/",$cookieurl);
    setcookie("core_user_passwd", md5($userinfo['passwd'].$core_scode), time()+$core_time*3600+604800,"/",$cookieurl);
    setcookie("core_user_rank", $userinfo['rank'], time()+$core_time*3600+604800,"/",$cookieurl);
    setcookie("core_user_long", "0", time()+$core_time*3600+604800,"/",$cookieurl);
    setcookie("core_user_name", $userinfo['username'], time()+$core_time*3600+604800);
    setcookie("core_user_passwd", md5($userinfo['passwd'].$core_scode), time()+$core_time*3600+604800);
    setcookie("core_user_rank", $userinfo['rank'], time()+$core_time*3600+604800);
    setcookie("core_user_long", "1", time()+$core_time*3600+604800);
  }
  else
  {
	setcookie("core_user_name", $userinfo['username'], time()+$core_time*3600+3600,"/",$cookieurl);
    setcookie("core_user_passwd", md5($userinfo['passwd'].$core_scode), time()+$core_time*3600+3600,"/",$cookieurl);
    setcookie("core_user_rank", $userinfo['rank'], time()+$core_time*3600+3600,"/",$cookieurl);
    setcookie("core_user_long", "0", time()+$core_time*3600+3600,"/",$cookieurl);
    setcookie("core_user_name", $userinfo['username'], time()+$core_time*3600+3600);
    setcookie("core_user_passwd", md5($userinfo['passwd'].$core_scode), time()+$core_time*3600+3600);
    setcookie("core_user_rank", $userinfo['rank'], time()+$core_time*3600+3600);
    setcookie("core_user_long", "0", time()+$core_time*3600+3600);
  }
  if($userinfo['rank']==2)
  {
	$neirong="登陆成功，在前台登陆";
    $phpcolor->logwrite($neirong,$userinfo["username"],$time,1);
  }
  $yztime=date('Y-m-d',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  $phpcolor->where="logintime like '%".$yztime."%' and username='".$userinfo['username']."'";
  $pd=$phpcolor->exist(2);
  if($int_udl>0 and !$pd)
    $phpcolor->upintegral($userinfo["username"],"",$int_udl,10,$time);
  $phpcolor->field="logintime='".$time."', loginip='".$_SERVER['REMOTE_ADDR']."'";
  $phpcolor->where="username = '".$userinfo['username']."'";
  $phpcolor->upgrade();
  die("4");
}
if($_GET['action'] == "quit" or isset($_COOKIE['core_user_name']))
{
  $cookieurl=str_replace("http://www.","",$core_url);
  $cookieurl=str_replace("/","",$cookieurl);
  setcookie("core_user_name", "", time()-10000000, "/",$cookieurl);
  setcookie("core_user_passwd", "", time()-10000000, "/",$cookieurl);
  setcookie("core_user_rank", "", time()-10000000, "/",$cookieurl);
  setcookie("core_user_long", "", time()-10000000, "/",$cookieurl);
  setcookie("core_user_name", "", time()-10000000);
  setcookie("core_user_passwd", "", time()-10000000);
  setcookie("core_user_rank", "", time()-10000000);
  setcookie("core_user_long", "", time()-10000000);
  if(isset($_GET['last']))
  {
    $_GET['last'] = str_replace("=and=","&",$_GET['last']);
    jump($_GET['last']);
  }
  else
    jump("index.php");
}
$tpl -> assign("jump", $_GET["last"]);
$tpl -> assign("core_vlogin", $core_vlogin);
$tpl -> assign("core_header_title", "登录 - ");
$tpl -> display("$core_style/login.htm");
?>