<?php
define('THIS_SCRIPT', 'userpay');
require_once 'global.php';
$lang = load_lang($lang, 'userpay' );
$stats = $lang['HtmlTitle'];
$arrNavMenu = array(
	 0=>array('linktitle'=>$lang['NavTitle1'],'linkurl'=>$lang['NavUrl1'])
	,1=>array('linktitle'=>$lang['HtmlTitle'],'linkurl'=>$lang['NavUrl0'])
);
$fsetting['chansetting']=explode(',',$fsetting['chansetting']);
head(1,0,0,$arrNavMenu);
$arr=explode(',',$fsetting['chansetting']);
$ticket=$arr[14];
if( $userid <= 0 ){
	showmsg($lang['MustLogin']);
}
if( $raction === 'alipay_return' ){
	AliPay_Return();
} else{
	require printout('userpay');
}

footer();

//在线支付返回结果处理，不登录也可执行
function AliPay_Return(){
	global $fsetting;
	if( $fsetting['chansetting'][5] != 0 ){
		AliPay_Return_Old();
	} else{
		$order_no = trim($GLOBALS['order_no']);
		$orderInfo =& getChanOrdersBy($order_no);
		if( $orderInfo === false ){
			print '--'.'>N';
		} else{
			$EnCodeStr = md5(urldecode("msg_id{$GLOBALS['msg_id']}order_no{$order_no}gross".number_format($orderInfo['o_paymoney'],2)."buyer_email{$GLOBALS['buyer_email']}buyer_name{$GLOBALS['buyer_name']}buyer_address{$GLOBALS['buyer_address']}buyer_zipcode{$GLOBALS['buyer_zipcode']}buyer_tel{$GLOBALS['buyer_tel']}buyer_mobile{$GLOBALS['buyer_mobile']}action{$GLOBALS['action']}date{$GLOBALS['date']}{$fsetting['chansetting'][6]}"));
			if( $EnCodeStr === trim($GLOBALS['ac']) ){
				print '--'.'>Y';
				$UserInMoney = $orderInfo['o_paymoney'];
				//更新用户资料
				$userinfo['userticket'] += forum_ticket()*$UserInMoney;
				UpdateUserInfoByName($userName,array('userticket'=>$userinfo['userticket']));
				//更新订单状态
				updateChanOrderState2($orderInfo['o_id']);
			} else{
				print '--'.'>N';
			}
		}
	}
}

function Get_ChallengeWord(){
	global $dv,$db,$_SESSION;
	session_start();
	list($uSec, $sec) = explode(' ', microtime());
	$seed = (float)$sec + ( (float)$uSec * 100000 );
	$maxLength = 12;
	$maxUserID = $db->scalar("SELECT MAX(userid) FROM {$dv}user");
	mt_srand($seed);
	$strRnd = mt_rand(0,9999);
	$strLen = strlen($strRnd);
	if( $strLen < 4 ){
		$strRnd = str_repeat('0',4-$strLen).$strRnd;
	}
	$maxUserID = $strRnd.$maxUserID;
	$maxLength = $maxLength - strlen($maxUserID);
	if( $maxLength >= 1 && $maxLength <= 7 ){
		$maxUserID = str_repeat('0',$maxLength).$maxUserID;
	}
	$_SESSION['challengeWord'] = $maxUserID;
	$challengePassword = getChallengePassword();
	$_SESSION['challengeWord_key'] = md5($maxUserID.':'.$challengePassword);
	return $maxUserID;
}

function Emp_ChallengeWord(){
	global $_SESSION;
	session_start();
	unset($_SESSION['challengeWord'],$_SESSION['challengeWord_key']);
}

function getChallengePassword(){
	global $dv,$db;
	$challengePassword = $db->scalar("SELECT forum_challengepassword FROM {$dv}setup");
	if( $challengePassword === false ){
		$challengePassword = 'x973KsxeVc4fGoYEodMZ';
	}
	return $challengePassword;
}

function &getChallengeInfo(){
	global $dv,$db;
	$challengeInfo = false;
	if( $query = $db->query("SELECT d_forumid,d_username,d_password,d_realname,d_identityno,d_sex,d_postcode,d_address,d_receiver,d_email,d_forumname,d_forumurl,d_telephone,d_mobile,d_forumprovider,d_version,d_challengepassword FROM {$dv}challengeinfo",array('pageSize'=>1)) ){
		$challengeInfo =& $db->fetch_array($query);
	}
	return $challengeInfo;
}

function &getChanOrdersBy($PayCode){
	global $dv,$db;
	$orderInfo = false;
	if( !get_magic_quotes_gpc() ){
		$PayCode = $db->escape_string($PayCode);
	}
	if( $query = $db->query("SELECT o_id,o_type,o_mobile,o_username,o_isapply,o_issuc,o_paymoney,o_paycode,o_boardid,o_topicid,o_addtime FROM {$dv}chanorders WHERE o_issuc=0 AND o_paycode='{$PayCode}'") ) {
		$orderInfo =& $db->fetch_array($query);
	}
	return $orderInfo;
}

function &getChanOrdersByName($userName,$suc=0){
	global $db,$dv,$page;
	if( !is_numeric($page) || $page < 1 ){
		$page = 1;
	}
	$returnVal = array('AbsolutePage'=>$page,'PageSize'=>20,'RecordCount'=>0,'PageCount'=>1,'result_set'=>false);
	$stmt = '';
	if( !get_magic_quotes_gpc() ){
		$userName = $db->escape_string($userName);
	}
	if( $suc === 0 ){
		$countStmt = "SELECT COUNT(1) FROM {$dv}chanorders WHERE o_username='{$userName}'";
		$stmt = "SELECT o_type,o_paycode,o_paymoney,o_issuc,o_addtime,o_id FROM {$dv}chanorders WHERE o_username='{$userName}' ORDER BY o_addtime DESC";
	} elseif( $suc === 1 ){
		$countStmt = "SELECT COUNT(1) FROM {$dv}chanorders WHERE o_username='{$userName}' AND o_issuc=1";
		$stmt = "SELECT o_type,o_paycode,o_paymoney,o_issuc,o_addtime,o_id FROM {$dv}chanorders WHERE o_username='{$userName}' AND o_issuc=1 ORDER BY o_addtime DESC";
	} elseif( $suc === 2 ){
		$countStmt = "SELECT COUNT(1) FROM {$dv}chanorders WHERE o_username='{$userName}' AND o_issuc=0";
		$stmt = "SELECT o_type,o_paycode,o_paymoney,o_issuc,o_addtime,o_id FROM {$dv}chanorders WHERE o_username='{$userName}' AND o_issuc=0 ORDER BY o_addtime DESC";
	}
	if( $countStmt !== '' ){
		$returnVal['RecordCount'] = $db->scalar($countStmt);
		if( $returnVal['RecordCount'] > 0 && $stmt !== '' ){
			$returnVal['PageCount'] = ceil( (float)$returnVal['RecordCount'] / (float)$returnVal['PageSize'] );
			if( $returnVal['AbsolutePage'] > $returnVal['PageCount'] ){
				$returnVal['AbsolutePage'] = $returnVal['PageCount'];
			}
			if( $returnVal['AbsolutePage'] < 1 ){
				$returnVal['AbsolutePage'] = 1;
			}
			$returnVal['result_set'] =& $db->getResultSet($stmt,array('absolutePage'=>$returnVal['AbsolutePage'],'pageSize'=>$returnVal['PageSize']));
		}
	}
	return $returnVal;
}

function &getChanOrdersByIdName($id,$userName){
	global $dv,$db;
	if( !get_magic_quotes_gpc() ){
		$userName = $db->escape_string($userName);
	}
	return $db->getResultSet("SELECT o_id,o_type,o_mobile,o_username,o_isapply,o_issuc,o_paymoney,o_paycode,o_boardid,o_topicid,o_addtime FROM {$dv}chanorders WHERE o_id='{$id}' AND o_username='{$userName}'");
}

function updateChanOrderState($UserInMoney,$orderID){
	global $dv,$db,$fsetting;
	if( !get_magic_quotes_gpc() ){
		$UserInMoney = $db->escape_string($UserInMoney);
		$orderID = $db->escape_string($orderID);
	}
	$db->query("UPDATE {$dv}chanorders SET o_issuc=1,o_paymoney={$UserInMoney} WHERE o_id='{$orderID}'");
}

function updateChanOrderState2($orderID){
	global $dv,$db,$fsetting;
	if( !get_magic_quotes_gpc() ){
		$UserInMoney = $db->escape_string($UserInMoney);
		$orderID = $db->escape_string($orderID);
	}
	$db->query("UPDATE {$dv}chanorders SET o_issuc=1 WHERE o_id='{$orderID}'");
}

function addChanOrders($username,$paymoney,$paycode){
	global $dv,$db;
	if( !get_magic_quotes_gpc() ){
		$username = $db->escape_string($username);
		$paymoney = $db->escape_string($paymoney);
		$paycode = $db->escape_string($paycode);
	}
	$db->query("INSERT INTO {$dv}chanorders(o_type,o_username,o_isapply,o_issuc,o_paymoney,o_paycode,o_addTime) VALUES(2,'{$username}',0,0,{$paymoney},'{$paycode}','".TIME_NOW."')");
}

function &getDSms(){
	global $dv,$db,$userid;
	$returnVal = array('user_exists'=>false,'is_challenge'=>false,'challenge_info'=>false);
	if( $query = $db->query("SELECT usermobile,ischallenge FROM {$dv}user WHERE userid='{$userid}'") ){
		$result =& $db->fetch_array($query);
	}
	if( $result !== false ){
		$returnVal['user_exists'] = true;
		if( $result['ischallenge'] == 0 ){
			unset($result);
		} else{
			$returnVal['is_challenge'] = true;
			$returnVal['user_mobile'] = $result['usermobile'];
			unset($result);
			$result = false;
			if( $query = $db->query("SELECT d_forumid,d_username,d_password,d_realname,d_identityno,d_sex,d_postcode,d_address,d_receiver,d_email,d_forumname,d_forumurl,d_telephone,d_mobile,d_forumprovider,d_version,d_challengepassword FROM {$dv}challengeinfo",array('pageSize'=>1)) ){
				$result =& $db->fetch_array($query);
			}
			if( $result !== false ){
				$returnVal['challenge_info'] = $result;
			}
		}
	}
	return $returnVal;
}

function &getPlusTools(){
	global $dv,$db;
	return $db->getResultSet("SELECT id,toolsname FROM {$dv}plus_tools_info ORDER BY id");
}

function &getMoneyLosgById($argUserID=0){
	global $db,$dv,$page,$userid;
	if( $argUserID <= 0 ){
		$argUserID = $userid;
	}
	if( !is_numeric($page) || $page < 1 ){
		$page = 1;
	}
	$returnVal = array('AbsolutePage'=>$page,'PageSize'=>20,'RecordCount'=>0,'PageCount'=>1,'result_set'=>false);
	$stmt = '';
	if( !get_magic_quotes_gpc() ){
		$userName = $db->escape_string($userName);
	}
	$countStmt = "SELECT COUNT(1) FROM {$dv}moneylog WHERE adduserid='{$argUserID}' AND boardid<>-1";
	$returnVal['RecordCount'] = $db->scalar($countStmt);
	if( $returnVal['RecordCount'] > 0 ){
		$returnVal['PageCount'] = ceil( (float)$returnVal['RecordCount'] / (float)$returnVal['PageSize'] );
		if( $returnVal['AbsolutePage'] > $returnVal['PageCount'] ){
			$returnVal['AbsolutePage'] = $returnVal['PageCount'];
		}
		if( $returnVal['AbsolutePage'] < 1 ){
			$returnVal['AbsolutePage'] = 1;
		}
		$stmt = "SELECT toolsid,countnum,log_money,log_ticket,log_ip,log_time,log_type,conect FROM {$dv}moneylog WHERE adduserid='{$argUserID}' AND boardid<>-1 ORDER BY log_time DESC";
		$returnVal['result_set'] =& $db->getResultSet($stmt,array('absolutePage'=>$returnVal['AbsolutePage'],'pageSize'=>$returnVal['PageSize']));
	}
	return $returnVal;
}


function forum_ticket() {
	global $fsetting;
	$arr=explode(',',$fsetting['chansetting']);
	$f_ticket=$arr[14];
	return $f_ticket;
}
?>