<?php
define('THIS_SCRIPT', 'plus_tools_info');
include_once 'global.php';
$lang = load_lang($lang, 'plus_tools_info' );
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'41,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $lang,$dv,$db,$action,$orders,$set_list,$edit_list,$tools_setting,$forum_setting;
	switch($action){
		case "setting":
			tools_setting();
		break;
		case "all_star":
			allstar_setting();
		break;
		case "editinfo":
			edittools();
		break;
		case "save_tools":
			save_tool();
		break;
		case "list":
			tools_list();
		break;
		case "update_userstock":
			update_user_stock();
		break;
		default :
			tools_list();
		break;
	}
include_once printout('plus_tools_info');
}
function tools_setting(){
	header("location:forum_settings.php#settingxu");
}
function tools_list(){
	global $lang,$dv,$db,$orders,$set_list;
	$set_list=array();
	switch (intval($orders)){
		case 0:
			$orders="sysstock";
		break;
		case 1:
			$orders="usermoney";
		break;
		case 2:
			$orders="userstock";
		break;
		case 3:
			$orders="isstar";
		break;
		default:
			$orders="sysstock";
		break;
	}
	$sql=$db->query("SELECT id,toolsname,toolsinfo,isstar,sysstock,userstock,usermoney,userticket,buytype FROM {$dv}plus_tools_info ORDER BY ".$orders);
	while ($set=$db->fetch_array($sql)){
		$set_list[]=$set;
	}
}
function allstar_setting(){
	global $lang,$dv,$db,$star;
	$star=$_POST['star'];
	if($star==0){
		show_msg($lang['sys.info'],$lang['tool.info16'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$sql=$db->query("UPDATE {$dv}plus_tools_info SET isstar=0");
	$count=count($star);
	for($i=0;$i<=$count;$i++){
		$editid.=$star[$i].",";
	}
	$editid=substr($editid,0,-2);
	$stmt=$db->query("UPDATE {$dv}plus_tools_info SET isstar=1 WHERE id in (".$editid.")");
	if($stmt){
		show_msg($lang['sys.info'],$lang['tool.info14'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['tool.info15'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
function edittools(){
	global $lang,$dv,$db,$editid,$edit_list;
	$edit_list=array();
	$editid=$_GET['editid'];
	if($editid=="" or !is_numeric($editid)){
		show_msg($lang['sys.info'],$lang['tool.info16'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		$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 WHERE id=".intval($editid)."");
		while($arr=$db->fetch_array($sql)){
			$edit_list[]=$arr;
		}
	}
$db->free_result($sql);
}
function save_tool(){
	global $lang,$dv,$db,$editid,$tools_setting,$toolsname,$toolsinfo,$toolsimg,$truetoolsgroupid,$trueboardid;
	global $isstar,$usermoney;
	global $userticket,$sysstock,$buytype,$userpost,$userwealth,$userep,$usercp,$forum_setting;
	$editid=$_GET['editid'];
	$tools_setting=$_POST['tools_setting'];
	for($i=0;$i<=60;$i++){
		if($tools_setting[$i]==""){
			$chan_setting=0;
		}
		else{
			$chan_setting=str_replace(",","",$tools_setting[$i]);
		}
		if($i==0){
			$tool_setting=$chan_setting;
		}
		else{
			$tool_setting.=",".$chan_setting;
		}
	}
	$isstar=$_POST['isstar'];
	$usermoney=$_POST['usermoney'];
	$userticket=$_POST['userticket'];
	$sysstock=$_POST['sysstock'];
	$buytype=$_POST['buytype'];
	$userpost=$_POST['userpost'];
	$userwealth=$_POST['userwealth'];
	$userep=$_POST['userep'];
	$usercp=$_POST['usercp'];
	$toolsname=substr(trim($_POST['toolsname']),0,50);
	$toolsinfo=substr(trim($_POST['toolsinfo']),0,250);
	$toolsimg =substr(trim($_POST['toolsimg']),0,150);
	$toolsgroupid =trim(str_replace(" ","",$_POST['truetoolsgroupid']));
	$endgid=substr($toolsgroupid,-1,1);
	if($endgid==','){
		$toolsgroupid=substr($toolsgroupid,0,-1);
	}
	$toolsboardid=trim(str_replace(" ","",$_POST['boardid']));
	$endbid=substr($toolsboardid,-1,1);
	if($endbid==','){
		$toolsboardid=substr($toolsboardid,0,-1);
	}
	If (substr($toolsgroupid,-1)==","){
		$toolsgroupid=substr($toolsgroupid,-1,strlen($toolsgroupid)-1);
	}
	If(substr($toolsboardid,-1)==","){
		$toolsboardid=substr($toolsboardid,-1,strlen($toolsboardid)-1);
	}
	if($editid=="" or !is_numeric($editid)){
		show_msg($lang['sys.info'],$lang['tool.info16'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($toolsname=="" or $toolsinfo==""){
		show_msg($lang['sys.info'],$lang['tool.info17'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		$toolsname=str_replace($lang['tool.info63'],"",$toolsname);
		$toolsinfo=str_replace($lang['tool.info63'],"",$toolsinfo);
	}
	if(strlen($toolsname)>50){
		show_msg($lang['sys.info'],$lang['tool.info18'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$sql=$db->query("UPDATE {$dv}plus_tools_info SET toolsname='".$toolsname."',toolsinfo='".$toolsinfo."',isstar='".$isstar."',sysstock='".$sysstock."',userstock='".$userstock."',usermoney='".$usermoney."',userpost='".$userpost."',userwealth='".$userwealth."',userep='".$userep."',usercp='".$usercp."',usergroupid='".$toolsgroupid."',boardid='".$toolsboardid."',userticket='".$userticket."',buytype='".$buytype."',toolsimg='".$toolsimg."',toolssetting='".$tool_setting."' WHERE id=".$editid."");
	if($sql){
		show_msg($lang['sys.info'],$lang['tool.info19'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['tool.info20'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
function update_user_stock(){
	global $lang,$dv,$db;
	$num=$db->scalar("SELECT count(1) from {$dv}plus_tools_buss as ptb inner join {$dv}plus_tools_info as pti on pti.id=ptb.toolsid");
	$sql=$db->query("UPDATE {$dv}plus_tools_info SET userstock=".$num."");
	edit_reback($sql);
}
function edit_reback($sql_str){
	global $lang;
	if($sql_str){
		show_msg($lang['sys.info'],$lang['tool.info21'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['tool.info22'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
?>