<?php
function echo_json( $data, $callback = false ) {
		if ( $callback ) {
				exit( $callback . '(' . $data . ')' );
		}
		exit( $data );
}
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == "login" ) {
		define( "SAXUE_NEED_SESSION", 1 );
} 
include "../core.php";
$callback = $_REQUEST['callback'] ? $_REQUEST['callback'] : false;
if ( !empty( $_SESSION['HTTP_REFERER'] ) && basename( $_SESSION['HTTP_REFERER'] ) != "login.php" ) {
		$jumpurl = $_SESSION['HTTP_REFERER'];
} elseif ( !empty( $_SERVER['HTTP_REFERER'] ) && basename( $_SERVER['HTTP_REFERER'] ) != "login.php" ) {
		$jumpurl = $_SERVER['HTTP_REFERER'];
} else {
		$jumpurl = '/member/';
} 
if ( !empty( $_SESSION['saxueUserId'] ) ) {
		header( "Location: " . $jumpurl );
		exit();
} 
saxue_getconfigs( 'configs', 'member' );
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == "login" ) {
		// 登录验证
		//@session_regenerate_id();
		$_REQUEST['account'] = saxue_htmlstr( $_REQUEST['account'] );
		if ( empty( $_REQUEST['account'] ) ) {
				echo_json( json_encode( array( 'flag' => 0, 'msg' => '请输入帐号' ) ), $callback );
		} 
		$_REQUEST['password'] = trim( $_REQUEST['password'] );
		if ( empty( $_REQUEST['password'] ) ) {
				echo_json( json_encode( array( 'flag' => 0, 'msg' => '请输入密码' ) ), $callback );
		} 
		if ( !isset( $_POST['vcode'] ) && !empty( $saxueConfigs['member']['logincheckcode'] ) && ( empty( $_POST['checkcode'] ) || ( strtolower( $_POST['checkcode'] ) != strtolower( $_SESSION['saxueCheckCode'] ) ) ) ) {
				echo_json( json_encode( array( 'flag' => 0, 'msg' => '验证码错误' ) ), $callback );
		} 
		include SAXUE_ROOT_PATH . "/model/member_users.php";
		$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" );
		if ( false !== strpos( $_REQUEST['account'], '@' ) ) {
				$users_handler -> queryobjects( new criteria( "email", $_REQUEST['account'] ) );
		} else {
				$users_handler -> queryobjects( new criteria( "uname", $_REQUEST['account'] ) );
		} 
		$saxueUsers = $users_handler -> getobject();
		if ( !$saxueUsers ) {
				echo_json( json_encode( array( 'flag' => 0, 'msg' => '帐号不存在' ) ), $callback );
		} 
		$truepass = $saxueUsers -> getvar( "password", "n" );
		$encpass = $users_handler -> encryptpass( $_REQUEST['password'] );
		if ( $truepass != $encpass ) {
				echo_json( json_encode( array( 'flag' => 0, 'msg' => '帐号或密码错误' ) ), $callback );
		} 
		if ( $saxueUsers -> getvar( "status", "n" ) != 1 ) {
				echo_json( json_encode( array( 'flag' => 0, 'msg' => '您的账户没有激活' ) ), $callback );
		}
		// 更新登录数据
		$uid = $saxueUsers -> getvar( "uid", "n" );
		include SAXUE_ROOT_PATH . "/model/member_userdata.php";
		$userdata_handler = &saxuememberuserdatahandler :: getinstance( "saxuememberuserdatahandler" );
		$saxueUserdata = $userdata_handler -> get( $uid );
		$saxueConfigs['member']['loginscore'] = intval( $saxueConfigs['member']['loginscore'] );
		$prelogintime = $saxueUserdata-> getvar( "logintime", "n" );
		$logintimes = $saxueUserdata -> getvar( "logintimes", "n" ) + 1;
		$loginscore = false;
		if ( $saxueConfigs['member']['loginscore'] > 0 && date( 'Y-m-d', $prelogintime ) != date( "Y-m-d" ) ) {
				$score = $saxueUsers -> getvar( "score", "n" );
				$score += $saxueConfigs['member']['loginscore'];
				$saxueUsers -> setvar( "score", $score );
				$saxueUsers -> unsetnew();
				$users_handler -> insert( $saxueUsers );
				$loginscore = true;
		}
		$saxueUserdata -> setvar( "logintimes", $logintimes );
		$saxueUserdata -> setvar( "prelogintime", $prelogintime );
		$saxueUserdata -> setvar( "preloginip", $saxueUserdata -> getvar( "loginip", "n" ) );
		$saxueUserdata -> setvar( "logintime", SAXUE_NOW_TIME );
		$saxueUserdata -> setvar( "loginip", saxue_userip() );
		$saxueUserdata -> unsetnew();
		$userdata_handler -> insert( $saxueUserdata );
		unset( $_SESSION['HTTP_REFERER'] );
		//header( "P3P: CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\"" );
		$_SESSION['saxueUserId'] = $saxueUsers -> getvar( "uid", "n" );
		$_SESSION['saxueUserName'] = $saxueUsers -> getvar( "uname", "n" );
		$_SESSION['saxueUserEmail'] = $saxueUsers -> getvar( "email", "n" );
		$_SESSION['saxueUserScore'] = $saxueUsers -> getvar( "score", "n" );
		$_SESSION['saxueUserMoney'] = $saxueUsers -> getvar( "money", "n" );
		$_SESSION['saxueLastLogin'] = strtotime( date( 'Y-m-d' ) . ' 23:59:59' );
		if ( $loginscore ) {
				include SAXUE_ROOT_PATH . "/model/member_score.php";
				$score_handler = &saxuememberscorehandler :: getinstance( "saxuememberscorehandler" );
				$_record = array( 'uid' => $_SESSION['saxueUserId'], 'amount' => $saxueConfigs['member']['loginscore'], 'balance' => $score, 'type' => 2 );
				$score_handler -> addlog( $_record );
		}
		echo_json( json_encode( array( 'flag' => 1 ) ), $callback );
} 
include SAXUE_WEB_PATH . "/header.php";
$saxueTpl -> assign( "jumpurl", saxue_htmlstr( $jumpurl ) );
$saxueTpl -> assign( "config", $saxueConfigs['member'] );
$saxueTpl -> setcaching( 0 );
$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . '/member/login.html';
include SAXUE_WEB_PATH . "/footer.php";
