<?php
define('IN_OUN', true);
include_once( "../config.inc.php");
include_once( ROOT_PATH."includes/funcomm.php");
include_once( ROOT_PATH."class/mydb.php");
$oPub = new mydb($dbhost,$dbuser,$dbpw,$dbname);

/* 初始化设置 */
@ini_set('memory_limit',          '16M');
@ini_set('session.cache_expire',  180);
@ini_set('session.use_trans_sid', 1);
@ini_set('session.use_cookies',   1);
@ini_set('session.auto_start',    1);
@ini_set('display_errors',        0);
@ini_set('error_reporting',       1); 
session_start();

if (defined('DEBUG_MODE') == false) {
    define('DEBUG_MODE', 0);
} 

if(PHP_VERSION < '4.1.0') {
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
	$_COOKIE = &$HTTP_COOKIE_VARS;
	$_SERVER = &$HTTP_SERVER_VARS;
	$_ENV = &$HTTP_ENV_VARS;
	$_FILES = &$HTTP_POST_FILES;
} 
$oPub = new mydb($dbhost,$dbuser,$dbpw,$dbname); 
$dbhost = $dbuser = $dbpw = $dbname = NULL;

foreach(array('_COOKIE', '_POST', '_GET') as $_request) {
	foreach($$_request as $_key => $_value) {
		$_key{0} != '_' && $$_key = daddslashes($_value);
	}
}

$PHP_SELF = dhtmlspecialchars($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
$BASESCRIPT = basename($PHP_SELF);
list($BASEFILENAME) = explode('.', $BASESCRIPT);
$boardurl = htmlspecialchars('http://'.$_SERVER['HTTP_HOST'].preg_replace("/\/+(api|archiver|wap)?\/*$/i", '', substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'))).'/');

define('DOCUMENT_ROOT', str_replace('\\','/',strtolower($_SERVER["DOCUMENT_ROOT"])));
$SUBPATH = (substr(DOCUMENT_ROOT,-1) == '/')?str_replace(DOCUMENT_ROOT, '', strtolower(ROOT_PATH)):str_replace(DOCUMENT_ROOT.'/', '',strtolower(ROOT_PATH));

header('content-type: text/html; charset=utf-8'); 

/* 通过用户输入的域名取得网站配置信息 */
$havedomin = FALSE;
$db_table = $pre."sysconfig";
$_SERVER["SERVER_NAME"] = ($_SERVER["SERVER_PORT"] != 80)?$_SERVER["SERVER_NAME"].':'.$_SERVER["SERVER_PORT"]:$_SERVER["SERVER_NAME"];
if($Aconf['allow_multi']){
	$sql = "SELECT * FROM ".$pre."sysconfigfast WHERE main_domin='".$_SERVER["SERVER_NAME"]."' AND states <> 1 ORDER BY scid ASC LIMIT 1";
}else{
	$sql = "SELECT * FROM ".$pre."sysconfigfast limit 1";
}
$Anorm = $oPub->getRow($sql); 
if($Anorm){
    $Aconf['domain_url']  = 'http://'.$Anorm['main_domin'].'/'.$SUBPATH;
	$havedomin = true;
} else if(strpos($_SERVER["SERVER_NAME"],$Aconf['mail_url'] ) ) {
    $user_name = str_replace('.'.$Aconf['mail_url'],'',$_SERVER["SERVER_NAME"]);
    $sql = "SELECT * FROM ".$db_table." WHERE user_name='".$user_name."' AND states <> 1 ORDER BY scid ASC LIMIT 1";
    $Anorm = $oPub->selectrow($sql);
	if($Anorm) { 
	    $Aconf['domain_url']  = 'http://'.$Anorm['user_name'].'.'.$Aconf['mail_url'].'/'.$SUBPATH;
		$_SERVER["SERVER_NAME"] = $Anorm['user_name'].'.'.$Aconf['mail_url'];
		$havedomin = true;
	}
} else if($_SERVER["SERVER_NAME"] == $Aconf['mail_url']) {
    $sql = "SELECT * FROM ".$db_table." WHERE main_domin='www.".$_SERVER["SERVER_NAME"]."' AND states <> 1 ORDER BY scid ASC LIMIT 1";
    $Anorm = $oPub->selectrow($sql);
	if($Anorm) {
		$Aconf['domain_url']  = 'http://'.$_SERVER["SERVER_NAME"].'/'.$SUBPATH;
		$havedomin = true;
	} 
}

if(!$havedomin )
{
   $urlstr  =  '<br/><span style="color:#C00;font-weight: bold">友情提示:域名输入错误</span><br/><br/>';
   $urlstr .=  '<A HREF="http://www.'.$Aconf['mail_url'].'/'.$SUBPATH.'">进入本站默认首页>></A></span>';
   $title   = '友情提示:域名输入错误';
   $strShow = showMessage($urlstr,$title);
   echo $strShow;
   exit;
} else {
	/* 基本配置信息 */
	$Asets = explode("{|}",$Anorm['sets']);
	if(count($Asets))
    foreach ($Asets AS $v)
    {
	   $At = array();
	   $At = explode("[|]",$v);
	   if($At[0])
	   {
	       $Anorm[$At[0]] = $At[1];
		}
	}
	$Aconf['web_title']   = $Anorm['header_title']; 
    $Aconf['domain_id']      = $Anorm['scid'];
	$Aconf['domain_user_id'] = $Anorm['user_id'];
    $Aconf['footer_title']   = $Anorm['footer_title'];
    $Aconf['notices']         = $Anorm['notices'];

	unset($Anorm);
}
/* 网站数据读取基础 */
$un_domain_url     =  'domain_url';
$un_domain_id      =  'domain_id';
$un_domain_user_id =  'domain_user_id'; 
$un_action_list    =  'action_list';
$un_user_rank      =  'user_rank';
$un_login_fail     =  'login_fail';
$un_auser_id       =  'auser_id';


$_SESSION[$un_domain_url]         = $Aconf['mainurl'] ;
$_SESSION[$un_domain_id]        = $Aconf['domain_id'];
$_SESSION[$un_domain_user_id]   = $Aconf['domain_user_id'];


if($_POST['user_name'] != '' && $_POST['password'] != '') {   

	if($_SESSION[authnum] != strtoupper($_POST["authnum"]) || empty($_POST["authnum"])){ 
		 echo "<SCRIPT language='javascript'>\nalert('验证码错误!!');top.location='index.php';</script>";
		 exit;		
	} 

    $db_table = $pre.'admin_user';
    $user_name = clean_html(trim($_POST['user_name']));
	$password  = md5(trim($_POST['password']));
 
	$sql = "SELECT user_id FROM ".$db_table." 
	       WHERE `user_name` LIKE '".$user_name."'  
		   AND `password` LIKE '".$password."'
		   AND `domain_id` = '".$_SESSION[$un_domain_id]."' limit 1";   	   
	$Auser = $oPub->selectrow($sql); 
	if($Auser['user_id'] > 0) {
       $_SESSION[$un_auser_id]    = $Auser['user_id'];
	   /* 记录登录ip 及地址 */
	   $last_ip = real_ip();
       $Afields=array('last_login'=>gmtime(),'last_ip'=>$last_ip);
	   $condition = 'user_id='.$_SESSION[$un_auser_id];
	   $oPub->update($db_table,$Afields,$condition);

	   /*记录登录日志 */
	   $db_table = $pre.'account_log';
	   $change_desc = real_ip().' |  '.date("m月d日 h:i").' |  domain_id:'.$_SESSION[$un_domain_id];

	   /* 记录后台登录地址ip 到 session   如果取得的用户ip 与记录的IP地址不符合则自动退出 */
	   $Alast_ip = explode('.',$last_ip);
	   $last_ip  = $Alast_ip[0].'.'.$Alast_ip[1].'.'.$Alast_ip[2];
	   $_SESSION['admin_login_ip'] = $last_ip;
	   
	   if(empty($last_ip)) {
		   $change_desc .= ' | '.$user_name.' 得不到你的IP地址，操作被禁止';
           $Afields=array('user_id'=>$Auser['user_id'],'type'=>'login','change_desc'=>$change_desc,'domain_id'=>$_SESSION[$un_domain_id]);
           $oPub->install($db_table,$Afields);
           echo "<SCRIPT language='javascript'>\nalert('得不到你的IP地址，操作被禁止');top.location='/';</script>";
		   exit;
		} else {
		   $change_desc .= ' | '.$user_name.' 成功登陆';
           $Afields=array('user_id'=>$Auser['user_id'],'type'=>'login','change_desc'=>$change_desc,'domain_id'=>$_SESSION[$un_domain_id]);
           $oPub->install($db_table,$Afields);
		}
	    //header("Location: index.php");
		echo "<SCRIPT language='javascript'>top.location='index.php';</script>";
 	} else {
	   echo "<SCRIPT language='javascript'>\nalert('登录失败!!\\n用户名与密码错误');top.location='login.php';</script>";
	   exit;
	}
	unset($Auser);
}
?>
<?php
   include_once( "header.php");
?>	
<DIV class=content>
 
<TABLE width="300" border="0" align="center" height="223">	
  <TR height="50">   
    <TD height="52" colspan="3" align="left" valign="bottom" ><span style="float:left;font-weight: bold"><a href="../"><?php echo $Aconf['web_title'];?>首页</a> >>管理登录</span></TD> 
  </TR>	
  <form name="form1" method="post" action=""> 
  <TR >   
    <TD height="14" colspan="3" align="center" >&nbsp;</TD> 
  </TR>
  <TR >
    <TD width="297" height="22" align="right" ><b>用户名:</b></TD>
    <TD colspan="2" align="left" ><input name="user_name" type="text" size="20" value="" /></TD>
  </TR>
  <TR >
    <TD height="22" align="right" ><b>密　码:</b></TD>
    <TD colspan="2" align="left" ><input name="password" type="password" size="20" value="" /></TD>
  </TR>
  <TR >
    <TD height="26" align="right" ><b>验证码:</b></TD>
    <TD width="48" align="left" ><input name="authnum" type="text" size="7" value="" /></TD>
    <TD width="361"  align="left" ><img src="logincode.php" width="70" height="20" border="0" alt="验证码" /> </TD>
  </TR>
  <TR >
    <TD height="23" align="right" >&nbsp;</TD>
    <TD colspan="2" align="left" ><input type="submit" name="Submit" value="用户登陆" style="background-color: #FFCC66"/></TD>
  </TR>	

 </form>
</TABLE
></DIV>
<?php
include_once( "footer.php");
?>