<?php
session_start();
/*************************************************************\
+-------------------------------------------------------------+
| 通灵佛教网在线烧香程序 System                                 |
| Copyright (c) 2003-2010 Alfred China                        |
| All rights reserved.                                        |
+-------------------------------------------------------------+
\*************************************************************/
require_once('chk.php');

if($mode == "login" && $REQUEST_METHOD == "POST"){
	
	if($_POST['code'] != $_SESSION['code']){
       	echo "<script language=\"JavaScript\">\r\n"; 
		echo " alert(\"验证码验证失败，请重试！\");\r\n"; 
		echo " history.go(-1);\r\n";echo "</script>"; 
		exit; 
    }
		
	$username = addslashes($_POST["username"]);
	$password = md5($_POST["password"]);
	$user_data = ng_query_first("SELECT $sql_tbl[users].uid, $sql_tbl[users].username, $sql_tbl[users].password, $sql_tbl[users].status FROM $sql_tbl[users] WHERE username='$username'");
	
	if(empty($user_data)){
		
		echo "<script language=\"JavaScript\">\r\n"; 
		echo " alert(\"您输入的用户名还未注册，请重试！\");\r\n"; 
		echo " history.go(-1);\r\n";echo "</script>"; 
		exit; 
		
	}elseif($user_data["status"] == 'N'){
		
		echo "<script language=\"JavaScript\">\r\n"; 
		echo " alert(\"您的用户名已被禁用，请联系管理员解封！\");\r\n"; 
		echo " history.go(-1);\r\n";echo "</script>"; 
		exit; 
		
	}elseif($password == $user_data["password"] && !empty($password)){
		
		$username = $username;
		setcookie("uid", $user_data['uid'], 0,"/",""); 
		setcookie("username", $user_data['username'], 0,"/",""); 
		setcookie("ngpassword", $user_data['password'], 0,"/",""); 
			
		$tm = time();
		db_query("UPDATE $sql_tbl[users] SET logindtm='$tm', loginip = '$REMOTE_ADDR', logins = logins+1 WHERE username='$username'");
		db_query("REPLACE INTO $sql_tbl[logins] (login, date_time, usertype, action, status, ip) VALUES ('$username','$tm','C','login','success','$REMOTE_ADDR')");
		
		$strmess = urlencode("登录成功");
		ng_header_location("users.php?mode=gongde&strmess=$strmess");
			
	}else{
			
		db_query("REPLACE INTO $sql_tbl[logins] (login, date_time, usertype, action, status, ip) VALUES ('$username','".time()."','C','login','failure', '$REMOTE_ADDR')");

		echo "<script language=\"JavaScript\">\r\n"; 
		echo " alert(\"您输入的密码错误，请重试！\");\r\n"; 
		echo " history.go(-1);\r\n";echo "</script>"; 
		exit; 
	}
	
}elseif($mode == "logout"){
	
	db_query("REPLACE INTO $sql_tbl[logins] (login, date_time, usertype, action, status, ip) VALUES ('$username','".time()."','C','logout','success','$REMOTE_ADDR')");
	
	$uid = '';
	$username = '';
	setcookie("uid", "", 0,"/",""); 
	setcookie("username", "", 0,"/",""); 
	setcookie("ngpassword", "", 0,"/",""); 
	$strmess = urlencode("您已注销登录");
	ng_header_location("index.php?strmess=$strmess");
	
}

$metawords = $netgou["meta_keywords"];
$metaintro = $netgou["meta_descr"];
$titlename = $netgou["web_name"];

$ng_obstart && function_exists('ob_gzhandler') ? ob_start('ob_gzhandler') : ob_start();
$ng_obstart ? $ng_gzip="Gzip enabled":$ng_gzip="Gzip disabled";
require_once PrintTemp('header');
require_once PrintTemp('login');
require_once PrintTemp('footer');
$output=str_replace(array('<!--<!--<!---->','<!--<!---->','<!---->'),array('','',''),ob_get_contents());
ob_end_clean();
echo $output;
flush;
exit;
break;
?>