<?php
define('THIS_SCRIPT', 'announcements');
require_once 'global.php';
$lang = load_lang($lang, 'announcements' );

if ($action == 'showone'){
	$istoptable = 0;
}
if ($boardid == 0){
	$arrNavMenu = array(
			0=>array('linktitle'=>$lang['strings1'])
	);
	$stats = $lang['strings1'];
	if (admin_boardset_check())
	LoadStyleJs();
	else
	head(1,0,0,$arrNavMenu);

}else{
	$stats = $boardinfo['boardtype'].' - '.$lang['strings1'];
	$gettitle = $lang['strings1'];
	if (admin_boardset_check())
	LoadStyleJs();
	else
	head();


}
$canpostann = $caneditann = $canmodifyusername = false;
if (($master == true || $superboardmaster == true || ($boardmaster == true && $boardid != 0)) && $groupsetting[25] == 1){
	$canpostann = true;
}
if ($founduserper == true && $groupsetting[25] == 1){
	$canpostann = true;
}elseif($founduserper == true && $groupsetting[25] == 0){
	$canpostann = false;
}
if (($master || $superboardmaster || $boardmaster) && $groupsetting[26] == 1){
	$caneditann = true;
}
if ($founduserper == true && $groupsetting[26] == 1){
	$caneditann = true;
}elseif($founduserper == true && $groupsetting[26] == 0){
	$caneditann = false;
}

switch($action){
	case 'addann':
		require_once printout('announcements');
		break;
	case 'saveann':
		saveann();
		break;
	case 'editann':
		editann();
		break;
	case 'editanninfo':
		editanninfo();
		break;
	case 'saveedit':
		saveedit();
		break;
	case 'delann':
		delann();
		break;
	default:
		pagemain();
		break;
}

function pagemain(){
	global $dv,$db,$boardid,$action,$canpostann,$caneditann,$lang;
	if ($action == 'showone'){
		$showid = $GLOBALS['id'];
		if ($showid == '' || !is_numeric($showid)){
			$query=$db->query("select title,content,username,addtime,bgs from {$dv}bbsnews where boardid='$boardid' order by id desc Limit 1");
		}else{
			$query=$db->query("select title,content,username,addtime,bgs from {$dv}bbsnews where boardid='$boardid' And id='$showid'");
		}
	}else{
		$query=$db->query("select title,content,username,addtime,bgs from {$dv}bbsnews where boardid='$boardid' order by id desc");
	}
	include INC_PATH.'dv_code.php';
	require_once printout('announcements');
}

function saveann(){
	global $dv,$db,$boardid,$action,$canpostann,$caneditann,$username,$title,$content,$bgs,$boardinfo,$lang;
	if ($canpostann == false){
		showmsg($lang['strings3']);
	}
	if ($username == '' || $title == '' || $content == ''){
		showmsg($lang['strings4']);
	}
	$db->query("insert into {$dv}bbsnews (username,title,content,bgs,boardid,addtime) values ('$username','$title','$content','$bgs','$boardid','".TIME_NOW."')");
	if ($boardid == 0){
		$db->query("Insert Into {$dv}log (l_announceid,l_boardid,l_touser,l_username,l_content,l_ip,l_type,l_addtime) values (0,'$boardid','".$lang['tpl.str39']."','$GLOBALS[membername]','".$lang['tpl.str40']."','$GLOBALS[usertrueip]',3,'".TIME_NOW."')");
	}else{
		$db->query("insert into {$dv}log (l_announceid,l_boardid,l_touser,l_username,l_content,l_ip,l_type,l_addtime) values (0,'$boardid','".$lang['tpl.str39']."','$GLOBALS[membername]','".$lang['tpl.str41']." $boardinfo[boardtype]".$lang['tpl.str39']."','$GLOBALS[usertrueip]',3,'".TIME_NOW."')");
	}
	require_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createforumlink();
	$mycache->createAll();
	showmsg($lang['strings5']);
}

function editann(){
	global $dv,$db,$boardid,$action,$caneditann,$lang;
	if ($caneditann == false){
		showmsg($lang['strings6']);
	}
	if ($boardid == 0){
		$query=$db->query("select id,boardid,title,username,addtime,bgs from {$dv}bbsnews order by addtime desc");
	}else{
		$query=$db->query("select id,boardid,title,username,addtime,bgs from {$dv}bbsnews where boardid='$boardid' order by addtime desc");
	}
	require_once printout('announcements');
}

function editanninfo(){
	global $dv,$db,$boardid,$action,$caneditann,$lang,$boardmaster;
	$showid = $GLOBALS['id'];
	if ($caneditann == false){
		showmsg($lang['strings6']);
	}
	if ($showid == '' || !is_numeric($showid)){
		showmsg($lang['strings2']);
	}
	$query=$db->query("select id,title,content,username,addtime,bgs from {$dv}bbsnews where id='$showid'");
	$anns =& $db->fetch_array($query);
	if ($anns['id'] == ''){
		showmsg($lang['strings7']);
	}
	require_once printout('announcements');
}

function saveedit(){
	global $dv,$db,$boardid,$action,$caneditann,$lang,$boardinfo,$username,$title,$content,$bgs;
	$showid = $GLOBALS['id'];
	if ($caneditann == false){
		showmsg($lang['strings6']);
	}
	if ($showid == '' || !is_numeric($showid)){
		showmsg($lang['strings2']);
	}
	if ($username == '' || $title == '' || $content == ''){
		showmsg($lang['strings4']);
	}
	$db->query("Update {$dv}bbsnews Set title='$title',content='$content',addtime='".TIME_NOW."',boardid='$boardid',bgs='$bgs' Where id='$showid'");
	if ($boardid == 0){
		$db->query("Insert Into {$dv}log (l_announceid,l_boardid,l_touser,l_username,l_content,l_ip,l_type,l_addtime) values (0,'$boardid','".$lang['tpl.str39']."','$GLOBALS[membername]','".$lang['tpl.str42']."','$GLOBALS[usertrueip]',3,'".TIME_NOW."')");
	}else{
		$db->query("insert into {$dv}log (l_announceid,l_boardid,l_touser,l_username,l_content,l_ip,l_type,l_addtime) values (0,'$boardid','".$lang['tpl.str39']."','$GLOBALS[membername]','".$lang['tpl.str41']." $boardinfo[boardtype]".$lang['tpl.str42']."','$GLOBALS[usertrueip]',3,'".TIME_NOW."')");
	}
	require_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createforumlink();
	$mycache->createAll();
	showmsg($lang['strings8']);
}

function delann(){
	global $dv,$db,$boardid,$action,$caneditann,$lang;
	$showid = $GLOBALS['id'];
	if ($caneditann == false){
		showmsg($lang['strings6']);
	}
	$showid=str_replace(array(';','--',')',' ',','),array(''),$showid);
	if ($showid == '' || !is_numeric($showid)){
		showmsg($lang['strings2']);
	}
	$db->query("delete from {$dv}bbsnews where id in ($showid)");
	$db->query("insert into {$dv}log (l_announceid,l_boardid,l_touser,l_username,l_content,l_ip,l_type,l_addtime) values (0,'$boardid','".$lang['tpl.str39']."','$GLOBALS[membername]','".$lang['tpl.str43']."','$GLOBALS[usertrueip]',3,'".TIME_NOW."')");
	require_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createforumlink();
	$mycache->createAll();
	showmsg($lang['strings9']);
}
	if (!admin_boardset_check())
	footer();


?>