<?php
define('THIS_SCRIPT', 'link');
include_once 'global.php';
$lang = load_lang($lang, 'link' );
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'13,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $link_title1,$id,$lang,$action,$link_list,$taxis_id,$link_val,$se_list,$se_val,$nlogo,$ylogo,$readme,$url;
	global $islogo,$logo,$boardname,$http_url;
	$http_url="javascript:void(window.history.back())";
	$link_title1=$lang['$link_title1'];
	select_info();
	if($action=="addok"){
		add();
	}
	elseif($action=="editok"){
		link_edit();
	}
	elseif($action=="taxisok"){
		link_taxis();
	}
	elseif($action=="del"){
		link_del();
	}
	elseif($action=="allok"){
		all_update();
	}
	else{
		link_info();
	}
	include printout('link');
}
function select_info(){
	global $dv,$db,$action,$se_list,$se_val,$nlogo,$id,$ylogo,$http_url;
	$se_list=array();
	$sql="select id,boardname,readme,url,logo,islogo from {$dv}bbslink where id='".intval($id)."'";
	$result=$db->query($sql); 
	while ($arr=$db->fetch_array($result)){
		$se_list[]=$arr;
	}
$db->free_result($result);
}
function add(){
	global $dv,$db,$lang,$forum_name,$link_url,$link_logo,$forum_stu,$islogo,$http_url;
	$forum_name=$_POST['forum_name'];
	$forum_stu=$_POST['forum_stu'];
	$link_url= strpos($_POST['link_url'], 'http://') !== false ? $_POST['link_url'] : 'http://'.$_POST['link_url'];
	$link_logo=$_POST['link_logo'];
	$islogo=$_POST['islogo'];
	$linkid=$db->scalar("select MAX(id) as linkid from {$dv}bbslink");
	$linkid++;
	if($forum_name=="" or $link_url=="" or $forum_stu==""){
		show_msg($lang['error.info2'],$lang['error.info1'],0,'','','link.php',$lang['error.info4']);
		exit;
	}
	else{
		$sql="insert into {$dv}bbslink (id,boardname,readme,url,logo,islogo) values (".$linkid.",'".$forum_name."','".$forum_stu."','".$link_url."','".$link_logo."','".$islogo."')";
		$result=$db->query($sql);
		require_once INC_PATH.'BoardCache.class.php';
		$mycache =& new BoardCache($db);
		$mycache->createforumlink();
		if($result){
		 	show_msg($lang['del.info1'],$lang['succeed.info2'],1,'',$lang['succeed.info3'],$http_url,$lang['error.info4']);
			exit;
		}
		else{
			show_msg($lang['error.info2'],$lang['error.info5'],0,'','',$http_url,$lang['error.info4']);
			exit;
		}
	}
}
function link_info(){
	global $dv,$db,$action,$link_list,$nlogo,$ylogo,$i,$http_url;
	$link_list=array();
	$sql="select id,boardname,readme,url,logo,islogo from {$dv}bbslink order by id";
	$result=$db->query($sql);
	while ($arr=$db->fetch_array($result)){
		$link_list[]=$arr;
	}
	$db->free_result($result);
}
function link_del(){
	global $dv,$db,$id,$lang,$http_url;
	
	$sql="delete from {$dv}bbslink where id='".intval($id)."'";
	$result=$db->query($sql) or die($db->error());
	require_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createforumlink();
	if($result){
		show_msg($lang['del.info1'],$lang['del.info2'],1,$http_url,$lang['succeed.info3'],$http_url,$lang['error.info4']);
		exit;
	}
	else{
		show_msg($lang['error.info2'],$lang['error.info5'],0,$http_url,'',$http_url,$lang['error.info4']);
		exit;
	}
}
function link_edit(){
	global $dv,$db,$lang,$id,$forum_name,$link_url,$link_logo,$forum_stu,$islogo,$http_url;
	$sql="update {$dv}bbslink set boardname='$forum_name',readme='$forum_stu',url='$link_url',logo='$link_logo',islogo='$islogo' where id='".intval($id)."'";
	$result=$db->query($sql);
	require_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createforumlink();
	if($result){
		show_msg($lang['del.info1'],$lang['edit.info2'],1,$http_url,$lang['sys.info'],$http_url,$lang['error.info4']);
		exit;
	}
	else{
		show_msg($lang['error.info2'],$lang['error.info5'],0,$http_url,'',$http_url,$lang['error.info4']);
		exit;
	}
}
function link_taxis(){
	global $dv,$db,$lang,$id,$taxis_id,$http_url;
	$stmt="select count(1) from {$dv}bbslink where id='".intval($taxis_id)."'";
	$result=$db->query($stmt);
	$total=$db->result($result,0);
	if($total==1){
		show_msg($lang['error.info2'],$lang['error.info6'],0,'','',$http_url,$lang['error.info4']);
		exit;
	}
	else{
		$sql="update {$dv}bbslink set id='".intval($taxis_id)."' where id='".intval($id)."'";
		$result=$db->query($sql);
		require_once INC_PATH.'BoardCache.class.php';
		$mycache =& new BoardCache($db);
		$mycache->createforumlink();
		if($result){
			show_msg($lang['succeed.info1'],$lang['succeed.info2'],1,'link.php',$lang['succeed.info3'],$http_url,$lang['error.info4']);
			exit;
		}
		else{
			show_msg($lang['error.info2'],$lang['error.info5'],0,'#','',$http_url,$lang['error.info4']);
			exit;
		}
	}
}
function all_update(){
	global $dv,$db,$lang,$id,$forumname,$url,$islogo,$logo,$stu,$http_url,$link_url;
	$stmt="select count(id) from {$dv}bbslink";
	$result=$db->query($stmt);
	$arr=$db->fetch_array($result);
	$all_count=$db->result($result,0);
	$tmpStr.= ','.implode(',',$id).',';
	if( preg_match('/([0-9]+,)([0-9]+,)*\1/i',$tmpStr,$tmpArr)!== 0 ){
		show_msg($lang['error.info2'],$lang['all.info1'].$tmpArr[1],0,$homology,$lang['all.info2'],$http_url,$lang['error.info4']);
		exit;
	} 
	for($i=0;$i<$all_count;$i++){
		$sql="update {$dv}bbslink set boardname='".$forumname[$i]."',url='".$link_url[$i]."',readme='".$stu[$i]."',logo='".$logo[$i]."',islogo='".$islogo[$i]."' where id='".intval($id[$i])."'";
		$result=$db->query($sql);
	}
	require_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createforumlink();
	if($result){
		show_msg($lang['sys.info'],$lang['all.info3'],1,'','',$http_url,$lang['error.info4']);
		exit;
	}
	else{
		show_msg($lang['error.info2'],$lang['error.info5'],0,'','',$http_url,$lang['error.info4']);
		exit;
	}
}
?>