<?php
require_once('inc/common.inc.php');
CommTool::checkIncludeByAdminPhp();

$action = $_GET['action'];
if("logout" == $action)                                                                      
{
  $oldAdminUser = $_SESSION['adminuser'];
  $_SESSION['adminid'] = "";
  $_SESSION['adminuser'] = "";
  $_SESSION['adminpassword'] = "";
  if($configIsLog)CommTool::writeLog("$oldAdminUser exit the system");                      
  promptMessage("index.php", $promptLogoutSuccess, $configJumpTime);
  exit;
}
else if("relogin" == $action)                                                                
{
  $oldAdminUser = $_SESSION['adminuser'];
  $_SESSION['adminid'] = "";
  $_SESSION['adminuser'] = "";
  $_SESSION['adminpassword'] = "";
  $userName = trim($_POST['username']);
  $password = $_POST['pass'];
  if($configIsLog)CommTool::writeLog("$oldAdminUser Exit. Change into $userName to login");   
  if(1 > strlen($userName) || 1 > strlen($password))
  {
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=login&menu=login", $promptNonLogin, $configJumpTime);
    exit;
  }
  $userPassword = CommTool::encryptMd5($password, $configEncryptTimes);                       
  $strSql = "select id, adminuser, adminpassword from {$configTableHead}admin where adminuser = '$userName' and adminpassword = '$userPassword';";
  $row = $dbInstance->getOne($strSql);
  if(1 > $row['id'])                                                                         
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} login to system failed");   
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=login&menu=login", $promptLoginFail, $configJumpTime);
    exit;
  }
  else
  {                                                                                           
    $_SESSION['adminid'] = $row['id'];
    $_SESSION['adminuser'] = $row['adminuser'];
    $_SESSION['adminpassword'] = $row['adminpassword'];
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} login to system Success");    
    promptMessage("index.php", $promptLoginSuccess, $configJumpTime);
    exit;
  }
}
else if("editpassword" == $action)
{
  if(empty($oldAdminPassword) || 5 > strlen($oldAdminPassword) || 15 < strlen($oldAdminPassword))  
  {
    promptMessage("javascript:history.go(-1)", $promptCorrectEditAdminOldPassword, $configJumpTime);
    exit;
  }
  $oldAdminPassword = CommTool::encryptMd5($oldAdminPassword, $configEncryptTimes);                
  if($_SESSION['adminpassword'] != $oldAdminPassword)
  {
    promptMessage("javascript:history.go(-1)", $promptCorrectAdminOldPasswordFail, $configJumpTime);
    exit;
  }
  if(empty($newAdminPassword) || 5 > strlen($newAdminPassword) || 15 < strlen($newAdminPassword))
  {
    promptMessage("javascript:history.go(-1)", $promptCorrectAdminNewsPassword, $configJumpTime);
    exit;
  }
  if($newAdminPassword != $newAdminPasswordCheck)
  {
    promptMessage("javascript:history.go(-1)", $promptTheTwoAdminPasswordNotConsist, $configJumpTime);
    exit;
  }
  $newAdminPassword = CommTool::encryptMd5($newAdminPassword, $configEncryptTimes);
  $strSql = "update {$configTableHead}admin set adminpassword='{$newAdminPassword}' where id={$_SESSION['adminid']};";
  if($dbInstance->query($strSql))                                                            
  {
    $_SESSION['adminid'] = "";
    $_SESSION['adminuser'] = "";
    $_SESSION['adminpassword'] = "";
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=login&menu=login", $promptEditAdminPasswordSuccess, $configJumpTime, 'eidtpassword');
    exit;
  }
  else
  {
    promptMessage("javascript:history.go(-1)", $promptEditDataFail, $configJumpTime);
    exit;
  }  
}
else if("" == $action)                                                                      
{
  $userName = trim($_POST['username']);
  $password = $_POST['pass'];
  $checkCode = trim($_POST['checkCode']);
  if(empty($checkCode) || $_SESSION["code"] != $checkCode)                                  
  {
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=login&menu=login", $promptEmptyCheckCode, $configJumpTime, $adminHttpImgPath, $kuLanguage);
    exit;  
  }
  if(1 > strlen($userName) || 1 > strlen($password))
  {
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=login&menu=login", $promptEmptyLogin, $configJumpTime);
    exit;
  }
  $userPassword = CommTool::encryptMd5($password, $configEncryptTimes);                      
  $strSql = "select id, adminuser, adminpassword from {$configTableHead}admin where adminuser = '$userName' and adminpassword = '$userPassword';";
  $row = $dbInstance->getOne($strSql);
  if(1 > $row['id'])
  {
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=login&menu=login", $promptLoginFail, $configJumpTime);
    exit;
  }
  else
  {
      $_SESSION['adminid'] = $row['id'];
      $_SESSION['adminuser'] = $row['adminuser'];
      $_SESSION['adminpassword'] = $row['adminpassword'];
      if($configIsLog)CommTool::writeLog("");         
      if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} login to system");         
      
      $adminmodifyip = getIP();
      $strSql = "update {$configTableHead}admin set regtime='{$nowTime}', adminmodifyip='{$adminmodifyip}' where id={$_SESSION['adminid']}";
      if($dbInstance->query($strSql))
      {
        promptMessage($adminHttpPath.'index.php', $promptLoginSuccess, $configJumpTime);
      exit;
      }
      else
      {
        $_SESSION['adminid'] = '';
        $_SESSION['adminuser'] = '';
        $_SESSION['adminpassword'] = '';
        promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=login&menu=login", $promptLoginUpdateFail, $configJumpTime);
    exit;
      }
  }
}