<?php
define('THIS_SCRIPT','plus');
include_once 'global.php';
$lang = load_lang($lang,'plus');
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'36,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $dv,$db,$lang,$action,$carte_list,$group_list,$p_setting,$groupid,$selgroupid,$parentgid;
	switch ($_GET['action']){
		case "add":
			Select_Group($selgroupid);
		break;
		case "addok":
			meun_add();
		break;
		case "m_edit":
			Select_Group($selgroupid);
			meun_editlist();
		break;
		case "editok":
			meun_editok();
		break;
		case 'm_del':
			meun_del();
		break;
		default:
			menu_list();
		break;
	}
include printout('plus');
}
function menu_list(){
	global $lang,$dv,$db,$carte_list;
	$carte_list=array();
	$sql=$db->query("SELECT id,plus_type,plus_name,isuse,plus_setting,mainpage,isshowmenu,plus_copyright,plus_adminpage,plus_id FROM {$dv}plus Order by id desc");
	while ($arr=$db->fetch_array($sql)){
		$carte_list[]=$arr;
	}
}
function Select_Group($selgroupid){
	global $lang,$dv,$db,$group_list;
	$group_list=array();
	$selgroupid = ",".$selgroupid.",";
	$sql=$db->query("SELECT usergroupid,title,usertitle,parentgid,issetting FROM {$dv}usergroups WHERE parentgid>0 order by parentgid");
	while($arr=$db->fetch_array($sql)){
		$group_list[]=$arr;
	}
}
Function sys_groupname($parentgid){
	global $lang,$dv,$db; 
	switch ($parentgid){
		case 1:
			$groupname=$lang['plus.info119'];
		break;
		case 2:
			$groupname=$lang['plus.info120'];
		break;
		case 3:
			$groupname=$lang['plus.info121'];
		break;
		case 4:
			$groupname=$lang['plus.info122'];
		break;
		case 4:
			$groupname=$lang['plus.info123'];
		break;
		case 4:
			$groupname=$lang['plus.info124'];
		break;
	}
	return ($groupname);
}
function meun_editlist(){
	global $lang,$dv,$db,$carte_list;
	$id=$_GET['id'];
	if($id=="" or $id==0 or !is_numeric($id)){
		show_msg($lang['sys.info'],$lang['meun.info'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$carte_list=array();
	$sql=$db->query("SELECT id,plus_type,plus_name,isuse,plus_setting,mainpage,isshowmenu,plus_copyright,plus_adminpage,plus_id FROM {$dv}plus WHERE id=".intval($id)."");
	while ($arr=$db->fetch_array($sql)){
		$carte_list[]=$arr;
	}
}
function meun_add(){
	global $lang,$dv,$db,$title,$isuse,$stype,$readme,$windowtype,$windowwidth,$windowheight,$url,$plus_adminpage,$plusid,$usetime;
	global $timesetting,$groupid,$plusmaster,$plus_userpost,$plus_userwealth,$plus_userep,$plus_usercp,$plus_userpower,$plus_adduserwealth,$plus_adduserep,$plus_addusercp,$plus_adduserpower,$plus_setting;
	$title=$_POST['title'];
	$isuse=$_POST['isuse'];
	$stype=$_POST['stype'];
	$readme=$_POST['readme'];
	$windowtype=$_POST['windowtype'];
	$windowwidth=$_POST['windowwidth'];
	$windowheight=$_POST['windowheight'];
	$url=$_POST['url'];
	$plus_adminpage=htmlspecialchars($_POST['plus_adminpage']);
	$plusid=$_POST['plusid'];
	$usetime=$_POST['usetime'];
	$timesetting=$_POST['timesetting'];
	$groupid=$_POST['groupid'];
	$plusmaster=$_POST['plusmaster'];
	$plus_userpost=$_POST['plus_userpost'];
	$plus_userwealth=$_POST['plus_userwealth'];
	$plus_userep=$_POST['plus_userep'];
	$plus_usercp=$_POST['plus_usercp'];
	$plus_userpower=$_POST['plus_userpower'];
	$plus_adduserwealth=$_POST['plus_adduserwealth'];
	$plus_adduserep=$_POST['plus_adduserep'];
	$plus_addusercp=$_POST['plus_addusercp'];
	$plus_adduserpower=$_POST['plus_adduserpower'];
	$plus_setting=explode("\r\n",$_POST['plus_setting']);
	$plus_settingdata="";
	if($plusid=="" or $title==""){
		show_msg($lang['sys.info'],$lang['plus.info1'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$count=$db->scalar("SELECT count(1) FROM {$dv}plus WHERE plus_id='".$plusid."'");
	if($count>0){
		show_msg($lang['sys.info'],$lang['plus.info2'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$num=count($plus_setting);//20	
	for($i=0;$i<$num;$i++){
		$str=explode("=",$plus_setting[$i]);
		if(count($str)==1){
			if($plus_settingdata==""){
				$plus_settingdata=str_replace(" ","",$str[1]).",";
				$tmpstr=str_replace(" ","",$str[0]).",";
			}
		}
		else{
			$plus_settingdata.=str_replace(" ","",$str[1]).",";
			$tmpstr.=str_replace(" ","",$str[0]).",";
		}
	}
	$tmpstr=substr($tmpstr,0,-1);
	$plus_settingdata.="|||".$tmpstr."|||";
	$plusmaster=explode("\r\n",$plusmaster);
	$masterlist="";
	$c=count($plusmaster);
	for($j=0;$j<$c;$j++){
		if(str_replace(" ","",$plusmaster[$j])<>""){
			if($masterlist==""){
				$masterlist=$plusmaster[$j];
			}
			else{
				$masterlist.="|".$plusmaster[$j];
			}
		}
	}
	If ($usetime=="") $usetime=0;
	If ($timesetting=="") $timesetting="0|24";
	$groupsetting=str_replace(",","@",trim($groupid));
	If (trim($plus_userpost)=="") $plus_userpost=0;
	If (trim($plus_userwealth)=="") $plus_userwealth=0;
	If (trim($plus_userep)=="") $plus_userep=0;
	If (trim($plus_usercp)=="") $plus_usercp=0;
	If (trim($plus_userpower)=="") $plus_userpower=0;
	If (trim($plus_adduserwealth)=="") $plus_adduserwealth=0;
	If (trim($plus_adduserep)=="") $plus_adduserep=0;
	If (trim($plus_addusercp)=="") $plus_addusercp=0;
	If (trim($plus_adduserpower)=="") $plus_adduserpower=0;
	$tmp_str=$usetime.",".$timesetting.",".$groupsetting.",".$masterlist.",".$plus_userpost.",";
	$tmp_str.=$plus_userwealth.",".$plus_userep.",".$plus_usercp.",".$plus_userpower.",";
	$tmp_str.=$plus_adduserwealth.",".$plus_adduserep.",".$plus_addusercp.",".$plus_adduserpower.",";
	$plus_settingdata.=$tmp_str;
	$plus_settingdata=str_replace(",,,","",$plus_settingdata);
	$sql=$db->query("INSERT INTO {$dv}plus (plus_type,plus_name,isuse,plus_setting,mainpage,isshowmenu,plus_copyright,plus_adminpage,plus_id) values ('".$stype."','".str_replace(CHR(34),"",$title)."',".$isuse.",'".$windowtype."|".$windowwidth."|".$windowheight."|||".$plus_settingdata."','".str_replace(CHR(34),"",$url)."',1,'".str_replace(CHR(34),"",$readme)."','".$plus_adminpage."','".$plusid."')");
	if($sql){
		show_msg($lang['sys.info'],$lang['plus.info3'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['plus.info4'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
function meun_editok(){
	global $lang,$dv,$db,$title,$isuse,$stype,$readme,$windowtype,$windowwidth,$windowheight,$url,$plus_adminpage,$plusid;
	global $usetime;
	global $timesetting,$groupid,$plusmaster,$plus_userpost,$plus_userwealth,$plus_userep,$plus_usercp,$plus_userpower;
	global $plus_adduserwealth,$plus_adduserep,$plus_addusercp,$plus_adduserpower,$plus_setting,$id;
	$id=$_GET['id'];
	$title=$_POST['title'];
	$isuse=$_POST['isuse'];
	$stype=$_POST['stype'];
	$readme=$_POST['readme'];
	$windowtype=$_POST['windowtype'];
	$windowwidth=$_POST['windowwidth'];
	$windowheight=$_POST['windowheight'];
	$url=$_POST['url'];
	$plus_adminpage=htmlspecialchars($_POST['plus_adminpage']);
	$plusid=$_POST['plusid'];
	$usetime=$_POST['usetime'];
	$timesetting=$_POST['timesetting'];
	$groupid=$_POST['groupid'];
	$plusmaster=$_POST['plusmaster'];
	$plus_userpost=$_POST['plus_userpost'];
	$plus_userwealth=$_POST['plus_userwealth'];
	$plus_userep=$_POST['plus_userep'];
	$plus_usercp=$_POST['plus_usercp'];
	$plus_userpower=$_POST['plus_userpower'];
	$plus_adduserwealth=$_POST['plus_adduserwealth'];
	$plus_adduserep=$_POST['plus_adduserep'];
	$plus_addusercp=$_POST['plus_addusercp'];
	$plus_adduserpower=$_POST['plus_adduserpower'];
	$plus_setting=explode("\r\n",$_POST['plus_setting']);
	$plus_settingdata="";
	if($plusid=="" or $title==""){
		show_msg($lang['sys.info'],$lang['plus.info1'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$count=$db->scalar("SELECT count(1) FROM {$dv}plus WHERE plus_id='".$plusid."' and id<>".$id."");
	if($count>0){
		show_msg($lang['sys.info'],$lang['plus.info2'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$num=count($plus_setting);//20	
	for($i=0;$i<$num;$i++){
		$str=explode("=",$plus_setting[$i]);
		if(count($str)==1){
			if($plus_settingdata==""){
				$plus_settingdata=str_replace(" ","",$str[1]).",";
				$tmpstr=str_replace(" ","",$str[0]).",";
			}
		}
		else{
			$plus_settingdata.=str_replace(" ","",$str[1]).",";
			$tmpstr.=str_replace(" ","",$str[0]).",";
		}
	}
	$tmpstr=substr($tmpstr,0,-1);
	$plus_settingdata.="|||".$tmpstr."|||";
	$plusmaster=explode("\r\n",$plusmaster);
	$masterlist="";
	$c=count($plusmaster);
	for($j=0;$j<$c;$j++){
		if(str_replace(" ","",$plusmaster[$j])<>""){
			if($masterlist==""){
				$masterlist=$plusmaster[$j];
			}
			else{
				$masterlist.="|".$plusmaster[$j];
			}
		}
	}
	If ($usetime=="") $usetime=0;
	If ($timesetting=="") $timesetting="0|24";
	$groupsetting=str_replace(",","@",trim($groupid));
	If (trim($plus_userpost)=="") $plus_userpost=0;
	If (trim($plus_userwealth)=="") $plus_userwealth=0;
	If (trim($plus_userep)=="") $plus_userep=0;
	If (trim($plus_usercp)=="") $plus_usercp=0;
	If (trim($plus_userpower)=="") $plus_userpower=0;
	If (trim($plus_adduserwealth)=="") $plus_adduserwealth=0;
	If (trim($plus_adduserep)=="") $plus_adduserep=0;
	If (trim($plus_addusercp)=="") $plus_addusercp=0;
	If (trim($plus_adduserpower)=="") $plus_adduserpower=0;
	$tmp_str=$usetime.",".$timesetting.",".$groupsetting.",".$masterlist.",".$plus_userpost.",";
	$tmp_str.=$plus_userwealth.",".$plus_userep.",".$plus_usercp.",".$plus_userpower.",";
	$tmp_str.=$plus_adduserwealth.",".$plus_adduserep.",".$plus_addusercp.",".$plus_adduserpower;
	$plus_settingdata.=$tmp_str;
	$plus_settingdata=str_replace(",,,","",$plus_settingdata);
	if(intval($isuse)==1 && $plusid=='honor'){
		createhonor();
	}
	if(intval($isuse)==1 && $plusid=='dv_bank'){
		createbank();
	}
	$sql=$db->query("UPDATE {$dv}plus SET plus_id='".$plusid."',plus_type='".$stype."',plus_name='".str_replace(CHR(34),"",$title)."',isuse=".$isuse.",isshowmenu=1,mainpage='".str_replace(CHR(34),"",$url)."',plus_copyright='".str_replace(CHR(34),"",$readme)."',plus_setting='".$windowtype."|".$windowwidth."|".$windowheight."|||".$plus_settingdata."',plus_adminpage='".$plus_adminpage."' WHERE id='".$id."'");
	require_once INC_PATH.'BoardCache.class.php';
	Create_plus_honor();
	if($sql){
		show_msg($lang['sys.info'],$lang['plus.info7'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['plus.info8'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
function meun_del(){
	global $lang,$dv,$db,$id;
	$id=$_GET['id'];
	if($id=="" or $id==0 or !is_numeric($id)){
		show_msg($lang['sys.info'],$lang['meun.info'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		$sql=$db->query("DELETE FROM {$dv}plus WHERE id='".$id."'");
		if($sql){
			show_msg($lang['sys.info'],$lang['plus.info5'],1,'','','plus.php',$lang['cmdback']);
		exit;
		}
		else{
			show_msg($lang['sys.info'],$lang['plus.info6'],0,'','','plus.php',$lang['cmdback']);
		exit;
		}
	}
}

function createhonor() {
	global $dv,$db;
	$betable=$db->scalar("show tables like '".$dv."honor_list'");
	if(!$betable){		
		$db->query("DROP TABLE IF EXISTS ".$dv."honor_list");
		$db->query("CREATE TABLE IF NOT EXISTS ".$dv."honor_list (
		  `id` int(11) unsigned NOT NULL auto_increment,
		  `honorname` varchar(40) default NULL,
		  `honorpic` varchar(255) default NULL,
		  `honorread` tinytext,
		  PRIMARY KEY  (`id`)
		);");
		$db->query("DROP TABLE IF EXISTS ".$dv."honor_user");
		$db->query("CREATE TABLE IF NOT EXISTS ".$dv."honor_user (
		  `id` int(11) NOT NULL auto_increment,
		  `username` varchar(40) default NULL,
		  `honorname` varchar(255) default NULL,
		  `honorpic` varchar(255) default NULL,
		  `issueuser` varchar(40) default NULL,
		  `reason` tinytext,
		  `honortime` int(10) unsigned default NULL,
		  `pos` int(2) unsigned default NULL,
		  PRIMARY KEY  (`id`)
		);");
	}
	if(!(beField())){
		$db->query("ALTER TABLE `".$dv."user` ADD userhonor VARCHAR(255) NULL;");
	}

}


function createbank() {
	global $dv,$db;
	$betable=$db->scalar("show tables like '".$dv."banklog'");
	if(!$betable){		
		$db->query("DROP TABLE IF EXISTS ".$dv."banklog");
		$db->query("CREATE TABLE IF NOT EXISTS ".$dv."banklog (
		  `id` int(10) unsigned NOT NULL auto_increment,
		  `userid` mediumint(8) unsigned NOT NULL default '0',
		  `username` varchar(18) NOT NULL default '',
		  `acttime` datetime NOT NULL default '0000-00-00 00:00:00',
		  `action` varchar(255) NOT NULL default '0',
		  PRIMARY KEY  (`id`),
		  KEY `userid` (`userid`,`username`),
		  KEY `userid_2` (`userid`)
		);");
		$db->query("DROP TABLE IF EXISTS ".$dv."bankstatus");
		$db->query("CREATE TABLE IF NOT EXISTS ".$dv."bankstatus (
		  `id` int(11) NOT NULL auto_increment,
		  `userid` int(11) NOT NULL default '0',
		  `username` varchar(16) NOT NULL default '',
		  `bankstatus` int(11) NOT NULL default '0',
		  `bankpass` varchar(40) NOT NULL default '',
		  `money` float NOT NULL default '0',
		  `loanmoney` float NOT NULL default '0',
		  `enddate` int(11) NOT NULL default '0',
		  `begdatetime` datetime NOT NULL default '0000-00-00 00:00:00',
		  `updatetime` datetime NOT NULL default '0000-00-00 00:00:00',
		  PRIMARY KEY  (`id`),
		  KEY `userid` (`userid`,`username`),
		  KEY `userid_2` (`userid`)
		);");
	}
}

function & beField() {
	global $db,$dv;
	$fieldlist='';
	$exitsField=false;
	$sql=$db->query("SHOW COLUMNS FROM {$dv}user");
	while ($fieltarr=$db->fetch_array($sql)) {
		$fieldlist.=$fieltarr['Field'].',';
	}
	$endfield=explode(',',$fieldlist);
	if(in_array('userhonor',$endfield)){
		$exitsField=true;
	}
	else{
		$exitsField=false;
	}
	return $exitsField;
}
?>