<?php
require './include/common.inc.php';
include_once HS_ROOT.'/include/mail.inc.php';
sajax_init();
sajax_export("doSub");
sajax_handle_client_request();

//if(!$_userid) showmessage($LANG['please_login'], '/member/login.php?forward='.urlencode($PHP_URL));
if(!$_userid) header('location:/member/login.php?forward='.urlencode($PHP_URL));
function doSub($cardid,$cardpass,$chkcode)
{
	global $db,$_username,$session,$LANG,$PHP_TIME,$PHP_IP,$HS,$MOD;
	if($cardid == '') return "请输入充值卡卡号";
	if($cardpass == '') return "请输入充值卡密码";
	if($chkcode == '') return "请输入验证码";
	$cardid = str_replace(' ','',$cardid);
	$cardpass = str_replace(' ','',$cardpass);
	if(strlen($cardid) != 20) return "充值卡卡号长度不符";
	if(strlen($cardpass) != 10) return "充值卡密码长度不符";
	if(strlen($chkcode) != 4) return "验证码长度不符";
	if(!preg_match("/^[0-9a-z]{8,20}$/i", $cardid)) return "充值卡输入有误";
	if(!preg_match("/^[0-9a-z]{4,10}$/i", $cardpass)) return "充值卡密码输入有误";
	if(!is_object($session)) $session = new hs_session();
	if(strtolower($_SESSION['checkcode']) != strtolower($chkcode))
	{
	  unset($_SESSION['checkcode']);
	  return "errcode";
	}

	
	$r = $db->get_one("SELECT * FROM ".TABLE_PAY_CARD." WHERE cardid='$cardid' LIMIT 0,1");
	if(!$r) return "不存在的充值卡卡号，请再次确认!";
	if($r['regtime'] > 0) return "该充值卡已经充值过!";
	if($r['enddate'] != '0000-00-00' && $r['enddate'] < date('Y-m-d')) return "该充值卡已过期!";
	
	$id = $r['id'];
	$price = $r['price'];
	$fprice = $price*$MOD['fpay'];
	$db->query("UPDATE ".TABLE_PAY_CARD." SET username='$_username',regtime=$PHP_TIME,regip='$PHP_IP' WHERE id=$id");
	pay_money_add($_username, $fprice, '充值卡号:'.$cardid);
	
	$billpay = trim($HS['billpay']);
	$billprice = $price*$billpay;
	$bill = $db->get_one("SELECT * FROM ".TABLE_BILL." WHERE tgname='$_username' LIMIT 0,1");
	if ($bill)
	{
	$biller = $bill['biller'];
	bill_money_add($biller, $billprice, $_username, '推广奖励');
	}
	
  return "true";
}

	$head['title'] = "充值卡充值";

	include template($mod, 'index');

?>