<?php
define('THIS_SCRIPT', 'plus_tools_user');
include_once 'global.php';
$lang = load_lang($lang, 'plus_tools_user' );
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'43,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $lang,$dv,$db,$action,$tool_list,$dv_adminusername,$_SESSION,$user_arr,$page_size,$page,$total,$page_count,$offset,$p;
	global $edit_list;
	 toolsname_list();
	 switch ($action){
		case "giveok":
			give_tools();
		break;
		case "paylist":
			user_list(2);
		break;
		case "editinfo":
			edit_info();
		break;
		case "saveedit":
			edit_user();
		break;
		case "tools_manage":
			user_list(1);
		break;
		case "tool_sale":
			user_list(2);
		break;
		case "del_usertools":
			del_usertool();
		break;
		case "cancel_state":
			cancel_toolstate();
		break;
		default:
			user_list(1);
		break;
	 }
include_once printout('plus_tools_user');
}
function toolsname_list(){
	global $lang,$dv,$db,$tool_list;
	$tool_list=array();
	$sql=$db->query("SELECT id,toolsname,toolsinfo,isstar,sysstock,userstock,usermoney,userpost,userwealth,userep,usercp,usergroupid,boardid,userticket,buytype,toolsimg,toolssetting FROM {$dv}plus_tools_info");
	while ($arr=$db->fetch_array($sql)){
		$tool_list[]=$arr;
	}
$db->free_result($sql);
}
function give_tools(){
	global $_SESSION,$lang,$dv,$db,$toolid,$target_user,$give_num,$give_money,$dv_adminusername;
	$toolid=intval($_POST['toolid']);
	$target_user=str_replace(" ","",$_POST['target_user']);
	$give_num=intval($_POST['give_num']);
	$give_money=intval($_POST['give_money']);
	$nowdate=strtotime("now");
	if($toolid==0 or $target_user=="" or ($give_num==0 AND $give_money==0)){
		show_msg($lang['sys.info'],$lang['user.info7'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$sql=$db->query("SELECT toolsname FROM {$dv}plus_tools_info WHERE id=".$toolid."");
	while ($arr=$db->fetch_array($sql)){
		if($arr['toolsname']==""){
			show_msg($lang['sys.info'],$lang['user.info8'],0,'','',$lang['url'],$lang['cmdback']);
		exit;
		}
		else{
			$toolsname=$arr['toolsname'];
		}
	}
	$sql=$db->query("SELECT userid,username,usermoney,userticket FROM {$dv}user WHERE username='".$target_user."'");
	while ($arr=$db->fetch_array($sql)){
		if($arr['username']==""){
			show_msg($lang['sys.info'],$lang['user.info9'],0,'','',$lang['url'],$lang['cmdback']);
		exit;
		}
		else{
			$stmt=$db->query("SELECT id FROM {$dv}plus_tools_buss WHERE userid=".$arr['userid']." and toolsid=".$toolid."");
			while ($set=$db->fetch_array($stmt)){
				if(is_null($set['id']) or $set['id']==""){
					$insql=$db->query("INSERT INTO {$dv}plus_tools_buss (userid,username,toolsid,toolsname,toolscount) values (".$arr['userid'].",'".$target_user."',".$toolid.",'".$toolsname."',".$give_num.")");
				}
				else{
					$upsql=$db->query("UPDATE {$dv}plus_tools_buss SET toolscount=toolscount+".$give_num." WHERE userid=".$arr['userid']." and toolsid=".$toolid."");
				}
			}
			$usertrueip=forumusertrueip();
			$instmt=$db->query("INSERT INTO {$dv}moneylog (toolsid,addusername,adduserid,log_ip,log_type,boardid,conect,hmoney,log_time) values (".$toolid.",'".$arr['username']."',".$arr['userid'].",'".$usertrueip."',0,-1,'".$_SESSION['dv_adminusername'].$lang['user.info10'].$give_num.$lang['user.info11'].$toolsname.$lang['user.info12'].$arr['username'].$lang['user.info13']."','".$arr['usermoney']."|".$arr['userticket']."','".$nowdate."')");
		}
	}
	$sql=$db->query("UPDATE {$dv}plus_tools_info SET userstock=userstock+".$give_num." WHERE id=".$toolid."");
	if($target_user<>"" and $give_money<>0){
		$stmt=$db->query("SELECT userid,username,usermoney,userticket FROM {$dv}user WHERE username='".$target_user."'");
		while ($list=$db->fetch_array($stmt)){
			if(is_null($list['username']) or $list['username']==""){
				show_msg($lang['sys.info'],$lang['user.info8'],0,'','',$lang['url'],$lang['cmdback']);
			exit;
			}
			else{
				$upstmt=$db->query("UPDATE {$dv}user SET usermoney=usermoney+".$give_money." WHERE userid=".$list['userid']."");
				$instmt=$db->query("INSERT INTO {$dv}moneylog (toolsid,addusername,adduserid,log_ip,log_type,boardid,conect,hmoney,log_time) values (".$toolid.",'".$list['username']."',".$list['userid'].",'".$usertrueip."',0,-1,'".$_SESSION['dv_adminusername'].$lang['user.info14'].$give_money.$lang['user.info15'].$list['username'].$lang['user.info16']."','".($list['usermoney']+$give_money)."|".$list['userticket']."','".$nowdate."')");
				if($instmt){
					show_msg($lang['sys.info'],$lang['user.info17'],1,'','',$lang['url'],$lang['cmdback']);
				exit;
				}
				else{
					show_msg($lang['sys.info'],$lang['user.info18'],0,'','',$lang['url'],$lang['cmdback']);
				exit;
				}
			}
		}
	}
	show_msg($lang['sys.info'],$lang['user.info17'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
}
function user_list($list_type){
	global $lang,$db,$dv,$keyword,$list_num,$taxisid,$toolname,$user_arr,$page_size,$page,$total,$page_count,$offset,$p;
	$user_arr=array();
	$keyword=str_replace(" ","",$_POST['keyword']);
	$list_num=intval($_POST['list_num']);//list_num:列表排序;
	$taxisid=intval($_POST['taxisid']);//排序;升或降;
	$toolname=intval($_POST['toolname']);//道具的名称;
	if($toolname>0){
		$search_str=" toolsid=".$toolname." ";
	}
	if(!is_numeric($list_num) or $list_num==""){
		$list_num=0;
	}
	$page_search="keyword=".$keyword."toolstype=".$toolname."list_num=".$list_num."taxisid=".$taxisid;
	switch ($list_num){
		case 1:
			$list_numstr=" salemoney";
			$list_type=2;
		break;
		case 2:
			$list_numstr=" saleticket";
			$list_type=2;
		break;
		case 3:
			$list_numstr=" toolscount";
		break;
		case 4:
			$list_numstr=" salecount";
			$list_type=2;
		break;
		default:
			$list_numstr=" id";
		break;
	}
	if($taxisid==1){
		$list_numstr.=" desc";
	}
	if($list_type>1){
		$tools_values_1=$lang['user.info27'];//价格
	}
	if($list_type==3){
		if($search_str<>""){
			$search_str.=" and salecount<>0";
		}
		else{
			$search_str=" salecount<>0";
		}
	}
	elseif($list_type==2){
		if($search_str<>""){
			$search_str.=" and salecount<>0";
		}
		else{
			$search_str=" salecount<>0";
		}
	}
	if($search_str<>""){
		if($keyword<>""){
			$search_str.=" and username like '%".$keyword."%'";
		}
	}
	else{
		if($keyword<>""){
			$search_str.=" username like '%".$keyword."%'";
		}
	}
	if($search_str<>""){
		$search_str="WHERE ".$search_str;
	}
	$total=$db->scalar("SELECT count(1) FROM {$dv}plus_tools_buss ".$search_str." order by".$list_numstr);
	page_info(20);
	$sql=$db->query("SELECT toolsname,toolscount,salecount,username,salemoney,saleticket,updatetime,id FROM {$dv}plus_tools_buss ".$search_str." order by".$list_numstr,array('absolutePage'=>$offset,'pageSize'=>$page_size));
	while ($arr=$db->fetch_array($sql)){
		$user_arr[]=$arr;
	}
$db->free_result($sql);
}
function edit_info(){
	global $lang,$db,$dv,$id,$edit_list;
	$edit_list=array();
	$id=$_GET['id'];
	if(!is_numeric($id) or $id==""){
		show_msg($lang['sys.info'],$lang['user.info30'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		$id=intval($_GET['id']);
	}
	$sql=$db->query("SELECT id,userid,username,toolsid,toolsname,toolscount,salecount,updatetime,salemoney,saleticket FROM {$dv}plus_tools_buss WHERE id=".intval($id)."");
	while ($set=$db->fetch_array($sql)){
		$edit_list[]=$set;
	}
$db->free_result($sql);
}
function edit_user(){
	global $lang,$dv,$db,$id,$toolscount,$salecount,$salemoney,$saleticket,$_SESSION;
	$id=$_GET['id'];
	$toolscount=$_POST['toolscount'];
	$salecount=$_POST['salecount'];
	$salemoney=$_POST['salemoney'];
	$saleticket=$_POST['saleticket'];
	if(!is_numeric($id) or $id==""){
		show_msg($lang['sys.info'],$lang['user.info30'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		$id=intval($_GET['id']);
	}
	if(!is_numeric($toolscount) or $toolscount==""){
		show_msg($lang['sys.info'],$lang['user.info30'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if(!is_numeric($salecount) or $salecount==""){
		show_msg($lang['sys.info'],$lang['user.info30'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if(!is_numeric($salemoney) or $salemoney==""){
		show_msg($lang['sys.info'],$lang['user.info30'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if(!is_numeric($saleticket) or $saleticket==""){
		show_msg($lang['sys.info'],$lang['user.info30'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$sql=$db->query("SELECT id,userid,username,toolsid,toolsname,toolscount,salecount,updatetime,salemoney,saleticket FROM {$dv}plus_tools_buss WHERE id=".intval($id)."");
	while ($Rs=$db->fetch_array($sql)){
		if($Rs['username']=="" or is_null($Rs['username'])){
			show_msg($lang['sys.info'],$lang['user.info31'],0,'','',$lang['url'],$lang['cmdback']);
		exit;
		}
		else{
			$stmt=$db->query("SELECT usermoney,userticket FROM {$dv}user Where userid=".$Rs['userid']."");
			while ($set=$db->fetch_array($stmt)){
				$usertrueip=forumusertrueip();
				$date=strtotime("now");
				$insql=$db->query("INSERT INTO {$dv}moneylog (toolsid,addusername,adduserid,log_ip,log_type,boardid,conect,hmoney,log_time) values (".$Rs['toolsid'].",'".$Rs['username']."','".$Rs['userid']."','".$usertrueip."',0,-1,'".$_SESSION['dv_adminusername'].$lang['user.info34'].($toolscount-$Rs['toolscount']).$lang['user.info35'].($salecount-$Rs['salecount']).$lang['user.info36'].($salemoney-$Rs['salemoney']).$lang['user.info37'].($saleticket-$Rs['saleticket'])."','".$set['usermoney']."|".$set['userticket']."','".$date."')");
				$upsql=$db->query("UPDATE {$dv}plus_tools_buss SET toolscount=".$toolscount.",salecount=".$salecount.",salemoney=".$salemoney.",saleticket=".$saleticket.",updatetime='".$date."' WHERE id=".intval($id)."");
				if($upsql){
					show_msg($lang['sys.info'],$lang['user.info32'],1,'','',$lang['url'],$lang['cmdback']);
				exit;
				}
				else{
					show_msg($lang['sys.info'],$lang['user.info33'],0,'','',$lang['url'],$lang['cmdback']);
				exit;
				}
			}
		}
	}
}
function del_usertool(){
	global $lang,$dv,$db,$id,$usertrueip,$_SESSION;
	$id=$_GET['id'];
	if(!is_numeric($id) or $id==""){
		show_msg($lang['sys.info'],$lang['user.info30'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		$id=intval($_GET['id']);
	}
	$date=strtotime("now");
	$usertrueip=forumusertrueip();
	$p_sql=$db->query("SELECT id,userid,username,toolsid,toolsname,toolscount,salecount,updatetime,salemoney,saleticket FROM {$dv}plus_tools_buss WHERE id=".intval($id)."");
	$Rs=$db->fetch_array($p_sql);
	$u_sql=$db->query("SELECT usermoney,userticket FROM {$dv}user WHERE userid=".$Rs['userid']."");
	$set=$db->fetch_array($u_sql);
	$in_sql=$db->query("INSERT INTO {$dv}moneylog (toolsid,addusername,adduserid,log_ip,log_type,boardid,conect,hmoney,log_time) values (".$Rs['toolsid'].",'".$Rs['username']."','".$Rs['userid']."','".$usertrueip."',0,-1,'".$_SESSION['dv_adminusername'].$lang['user.info38']."','".$set['usermoney']."|".$set['userticket']."','".$date."')");
	$d_sql=$db->query("DELETE FROM {$dv}plus_tools_buss WHERE id=".intval($id)."");
	if($d_sql){
		show_msg($lang['sys.info'],$lang['user.info39'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['user.info40'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
function cancel_toolstate(){
	global $lang,$dv,$db,$id,$salecount,$_SESSION;
	$salecount=$_GET['sale_num'];
	$id=$_GET['id'];
	if(!is_numeric($id) or $id==""){
		show_msg($lang['sys.info'],$lang['user.info30'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		$id=intval($_GET['id']);
	}
	$date=strtotime("now");
	$usertrueip=forumusertrueip();
	$usql=$db->query("UPDATE {$dv}plus_tools_buss SET toolscount=toolscount+".$salecount.",salemoney=0,saleticket=0,salecount=0 WHERE id=".intval($id)."");
	$stmt=$db->query("SELECT id,userid,username,toolsid,toolsname,toolscount,salecount,updatetime,salemoney,saleticket FROM {$dv}plus_tools_buss WHERE id=".intval($id)."");
	$Rs=$db->fetch_array($stmt);
	$sql=$db->query("SELECT usermoney,userticket FROM {$dv}user WHERE userid=".$arr['userid']."");
	$Trs=$db->fetch_array($sql);
	$insql=$db->query("INSERT INTO {$dv}moneylog (toolsid,addusername,adduserid,log_ip,log_type,boardid,conect,hmoney,log_time) values (".$Rs['toolsid'].",'".$Rs['username']."','".$Rs['userid']."','".$usertrueip."',0,-1,'".$_SESSION['dv_adminusername'].$lang['user.info41']."','".$Trs['usermoney']."|".$Trs['userticket']."','".$date."')");
	if($insql){
		show_msg($lang['sys.info'],$lang['user.info42'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['user.info43'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
?>