<?php
define('THIS_SCRIPT', 'topicother');
require_once 'global.php';
$lang = load_lang($lang, 'topicother' );
switch($t){
	case 1:
		lookip_main();
		break;
	case 2:
		//Nothing to do
		break;
	case 3:
		//Nothing to do
		break;
	case 4:
		PostVote_Main();//doing
		break;
	case 5:
		printpage_main();
		break;
	case 6:
		report_main();
		break;
	case 7:
		sendpage_main();
		break;
	case 8:
	case 9:
		PostActive_Main();
	break;
	case 10:
		AdminActive_Main();
	break;
	default:
		Go_Main();//
		break;
}
footer();

function Go_Main()
{
	head();
}//end function Go_Main

function lookip_main(){
	global $dv,$db,$boardid,$userid,$lang,$stats,$t,$action,$ip,$groupsetting,$master,$boardmaster,$superboardmaster,$founduserper,$usergroupid,$fsetting,$usertrueip,$lockid,$reaction;
	$canlookip = $canlockip = false;
	if (($master == true || $boardmaster == true || $superboardmaster == true) && $groupsetting[30] == 1){
		$canlookip = true;
	}
	if ($usergroupid > 3 && $groupsetting[30] == 1){
		$canlookip = true;
	}
	if ($founduserper == true && $groupsetting[30] == 1){
		$canlookip = true;
	}elseif ($founduserper == true && $groupsetting[30] == 0){
		$canlookip = false;
	}
	if (($master == true || $boardmaster == true || $superboardmaster == true) && $groupsetting[31] == 1){
		$canlockip = true;
	}
	if ($usergroupid > 3 && $groupsetting[31] == 1){
		$canlockip = true;
	}
	if ($founduserper == true && $groupsetting[31] == 1){
		$canlockip = true;
	}elseif ($founduserper == true && $groupsetting[31] == 0){
		$canlockip = false;
	}
	$stats = $fsetting['forumname'].' - '.$lang['strings11'];
	$arrNavMenu = array(
			0=>array('linktitle'=>$lang['strings12'],'linkurl'=>'dispuser.php')
			,1=>array('linktitle'=>$lang['strings11'])
	);
	head(1,0,0,$arrNavMenu);
	if ($action == 'setlockip'){
		if ($canlockip == false){
			showmsg($lang['strings17']);
		}
		if ($reaction == 'yes'){
			$sip = $GLOBALS['ip1'];
			if ($sip == ''){
				showmsg($lang['strings13']);
			}else{
				if (strpos($sip,"*.") == true){
					showmsg($lang['strings14']);
				}else{
					if($fsetting['lockip'] != ''){
						$sip = $fsetting['lockip']. '|' .str_replace('|','',$sip);
						$db->query("update {$dv}setup set forum_lockip='$sip'");
						$db->query("insert into {$dv}log (l_touser,l_username,l_content,l_ip,l_type,l_addtime) values ('-','$GLOBALS[membername]','$lang[strings15] $GLOBALS[ip1]','$usertrueip',6,'".TIME_NOW."')");
						//重写缓存forum_setting.php
						require_once INC_PATH.'BoardCache.class.php';
						$mycache =& new BoardCache($db);
						$mycache->createForumSetting();
						showmsg($lang['strings16']);
					}
				}
			}
		}else{
			$useraddress = ipfrom($ip);
		}
	}elseif($action == 'unlock'){
		if ($canlockip == false){
			showmsg($lang['strings17']);
		}
		$sip = $GLOBALS['id'];
		if ($sip == ''){
			showmsg($lang['strings13']);
		}
		if($fsetting['lockip'] != ''){
			$locklist = $fsetting['lockip'];
			$locklist = '|' .$locklist. '|';
			$unlockip = explode('.',$sip);
			if (count($unlockip) == 4){
				$sip = str_replace('|','',$sip);
				$sip = '|' .$sip;
				$locklist = str_replace($sip,'',$locklist);
				$locklist = explode('|',$locklist);
				for($i=1 ;$i<(count($locklist)-1) ;$i++){
					if ($i == 1){
						$ilocklist = $locklist[$i];
					}else{
						$ilocklist .= '|' .$locklist[$i];
					}
				}
				$db->query("update {$dv}setup set forum_lockip='$ilocklist'");
				$db->query("insert into {$dv}log (l_touser,l_username,l_content,l_ip,l_type,l_addtime) values ('-','$GLOBALS[membername]','$lang[strings18] $sip','$usertrueip',6,'".TIME_NOW."')");
				//重写缓存forum_setting.php
				require_once INC_PATH.'BoardCache.class.php';
				$mycache =& new BoardCache($db);
				$mycache->createForumSetting();
				showmsg($lang['strings19']);
			}
		}
	}else{
		$useraddress = ipfrom($ip);
		$igetlockip = getlockip($ip);
	}
	require_once printout('topicother');
}
function getlockip($sip){
	global $fsetting,$lockid;
	$setlockip = false;
	if ($fsetting['lockip'] != '' && $sip != ''){
		$locklist = explode('|',$fsetting['lockip']);
		$struserip = explode('.',$sip);
		if (count($struserip) == 4){
			for ($i=0; $i<count($locklist) ; $i++){
				if ($locklist[$i] != ''){
					$strkillip = explode('.',$locklist[$i]);
					if (count($strkillip) == 4){
						$setlockip = true;
						if($struserip[0] != $strkillip[0] && $strkillip[0] != "*"){$setlockip = false;}
						if($struserip[1] != $strkillip[1] && $strkillip[1] != "*"){$setlockip = false;}
						if($struserip[2] != $strkillip[2] && $strkillip[2] != "*"){$setlockip = false;}
						if($struserip[3] != $strkillip[3] && $strkillip[3] != "*"){$setlockip = false;}
						if($setlockip == true){
							$lockid = $locklist[$i];
							return $setlockip;
							break;
						}
					}
				}
			}
		}
	}
	return $setlockip;
}

function printpage_main(){
	global $dv,$db,$boardid,$userid,$lang,$fsetting,$stats,$id,$groupsetting,$boardinfo,$ForumUrl,$boardmaster,$master,$t;
	$istoptable = 0;
	$stats = $fsetting['forumname'].' - '.$lang['strings1'];
	if ($boardid == 0 || $id == '' || !is_numeric($id)){
		head();
		showmsg($lang['strings2']);
	}
	if ($groupsetting[2] == 0){
		head();
		showmsg($lang['strings3']);
	}
	$query=$db->query("select title,posttable,isbest,locktopic,boardid,postuserid from {$dv}topic where topicid='$id'");
	if ($topics = $db->fetch_array($query)){
		$stats = $topics['title'].' - '.$lang['strings1'].' - '.$fsetting['forumname'];
		$topics['title'] = iHtmlencode($topics['title']);
	}else{
		head();
		showmsg($lang['strings4']);
	}
	if (intval($topics['isbest']) == 0 && $groupsetting[41] == 0){
		head();
		showmsg($lang['strings5']);
	}
	if ($topics['boardid'] == 444){
		head();
		showmsg($lang['strings6']);
	}
	if ($userid != $topics['postuserid'] && $groupsetting[2] == 0){
		head();
		showmsg($lang['strings7']);
	}
	head();
	$query=$db->query("Select b.username,b.topic,b.dateandtime,b.body,u.usergroupid,b.postbuyuser,b.ubblist,b.isbest,b.locktopic,b.getmoneytype,b.parentid,b.announceid,b.rootid,b.signflag from $topics[posttable] b left Join {$dv}user u on b.postuserid=u.userid where b.boardid='$boardid' and b.rootid='$id' and b.locktopic<>2 and b.locktopic<>3 and u.lockuser=0 order by b.announceid");
	require_once printout('topicother');
}

function report_main(){
	global $dv,$db,$userid,$boardinfo,$action,$t,$lang,$gettitle,$boardid,$id,$replyid,$stats,$fsetting,$sendboardmaster,$content,$membername,$ForumUrl;
	$gettitle = $lang['strings8'];
	$stats = $boardinfo['boardtype'].' - '.$lang['strings8'].' - '.$fsetting['forumname'];
	head();
	if ($action == 'send'){
		if ($boardid == 0 || $id == '' || !is_numeric($id) || $sendboardmaster == '' || $content == ''){
			showmsg($lang['strings2']);
		}
		$query=$db->query("select title from {$dv}topic where topicid='$id'");
		if ($topics = $db->fetch_array($query)){
			$body = $content . $lang['strings9'];
			$body = str_replace('{$dvbbsurl}',$ForumUrl,$body);
			$body = str_replace('{$boardid}',$boardid,$body);
			$body = str_replace('{$announceid}',$id,$body);
			$body = str_replace('{$replyid}',$replyid,$body);
			$db->query("insert into {$dv}message (incept,sender,title,content,sendtime,flag,issend) values ('$sendboardmaster','$membername','$lang[strings8]','$body','".TIME_NOW."',0,1)");
			showmsg($lang['strings10']);
		}else{
			showmsg($lang['strings4']);
		}
	}
	require_once printout('topicother');
}

function sendpage_main(){
	global $dv,$db,$lang,$stats,$gettitle,$fsetting,$boardinfo,$userinfo,$t,$membername,$groupsetting,$boardid;
	global $postname,$incept,$mail,$content,$msetting;
	$gettitle = $lang['strings20'];
	$stats = $boardinfo['boardtype'].' - '.$lang['strings20'];
	$announceid = $GLOBALS['id'];
	head();
	if ($groupsetting[15] == 0){
		showmsg($lang['strings21']);
	}
	if ($announceid == '' || !is_numeric($announceid)){
		showmsg($lang['strings2']);
	}
	$query=$db->query("select title,postusername from {$dv}topic where topicid='$announceid'");
	if ($topics = $db->fetch_array($query)){
		if($GLOBALS['action'] == 'sendmail'){
			if ( $postname == '' || $incept == '' || $mail == '' || $content == '' ){
				showmsg($lang['strings24']);
			}
			if (!ereg("^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$",$mail)){
				showmsg($lang['strings25']);
			}
			include_once CACHE_PATH.'mailconfig.php';
			include_once INC_PATH.'sendmail.php';
			if ( $msetting['mailmod'] == 0 ){
				showmsg($lang['strings23']);
			}else{
				sendmail($incept,$topics['title'],$content,"{$fsetting['forumname']} <{$userinfo['useremail']}>");
				showmsg($lang['strings22']);
			}
		}
	}else{
		showmsg($lang['strings4']);
	}
	require_once printout('topicother');
}

//================查看用户来源信息===================
//================帖子投票===========================
function PostVote_Main()
{
	global $dv, $db, $master, $superboardmaster, $boardmaster, $lang, $userid, $boardid, $id, $voteid, $fsetting, $groupsetting, $userinfo, $board_settings;
	$stats = $fsetting['forumname'].' - '.$lang['VoteHeaderTitle'];
	$arrNavMenu = array(
			0=>array('linktitle'=>$lang['VoteHeaderTitle'])
	);
	if (IsReadonly() AND $master === false) {
		head(0,0,0,$arrNavMenu);
		showmsg($lang['vote.err.str1']);
		exit;
	}
	if ($userid <= 0) { // 必须登录。
		head(0,0,0,$arrNavMenu);
		showmsg($lang['MustLogin']);
		exit;
	}
	if (!is_numeric($id) OR $id <= 0) { // 非法的参数。
		head(0,0,0,$arrNavMenu);
		showmsg($lang['err.str1']);
		exit;
	}
	if (!is_numeric($voteid) OR $voteid <= 0) { // 非法的参数。
		head(0,0,0,$arrNavMenu);
		showmsg($lang['err.str1']);
		exit;
	}
	if ($groupsetting[9] == 0) { // 您没有在本论坛发表投票的权限，请<a href=login.php>登录</a>或者同管理员联系。
		head(0,0,0,$arrNavMenu);
		showmsg($lang['err.str2']);
		exit;
	}
	$TopicID = $id;
	$locktopic = $db->scalar("SELECT locktopic FROM {$dv}topic WHERE topicid='{$TopicID}'");
	if (false === $locktopic) { // 您选择的投票已经关闭。
		head(0,0,0,$arrNavMenu);
		showmsg($lang['err.str3']);
		exit;
	}
	$voteinfo = false;
	if ($query = $db->query("SELECT voteid,vote,votenum,votetype,lockvote,voters,timeout,uarticle,uwealth,uep,ucp,upower FROM {$dv}vote WHERE voteid='{$voteid}'", array('pageSize'=>1))) {
		$voteinfo =& $db->fetch_array($query);
	}
	if (false === $voteinfo) { // 您查找的数据暂不存在！
		head(0,0,0,$arrNavMenu);
		showmsg($lang['err.str4']);
		exit;
	} else {
		if (false === $master AND false === $superboardmaster AND false === $boardmaster) {
			if ($voteinfo['uarticle'] > $userinfo['userpost']) { // 检查文章
				head(0,0,0,$arrNavMenu);
				showmsg(str_replace('{$Tpl_UArticle}', $voteinfo['uarticle'], $lang['err.str5']));
				exit;
			}
			if ($voteinfo['uwealth'] > $userinfo['userwealth']) { // 检查金钱
				head(0,0,0,$arrNavMenu);
				showmsg(str_replace('{$Tpl_UWealth}', $voteinfo['uwealth'], $lang['err.str6']));
				exit;
			}
			if ($voteinfo['uep'] > $userinfo['userep']) { // 检查经验
				head(0,0,0,$arrNavMenu);
				showmsg(str_replace('{$Tpl_UEP}', $voteinfo['userep'], $lang['err.str7']));
				exit;
			}
			if ($voteinfo['ucp'] > $userinfo['usercp']) { // 检查魅力
				head(0,0,0,$arrNavMenu);
				showmsg(str_replace('{$Tpl_UCP}', $voteinfo['usercp'], $lang['err.str8']));
				exit;
			}
			if ($voteinfo['upower'] > $userinfo['userpower']) { // 检查威望
				head(0,0,0,$arrNavMenu);
				showmsg(str_replace('{$Tpl_UPower}', $voteinfo['userpower'], $lang['err.str9']));
				exit;
			}
		}
		$voteuserid = $db->scalar("SELECT userid FROM {$dv}voteuser WHERE voteid='{$voteid}' AND userid='{$userid}'");
		if ($voteuserid > 0) { // 您已经投过票了。
			head(0,0,0,$arrNavMenu);
			showmsg($lang['err.str10']);
			exit;
		}
		$arrvotenum = explode('|', $voteinfo['votenum']);
		$arrpostvote = array();
		if ($voteinfo['votetype'] == 1) {
			if (!empty($arrvotenum) AND is_array($arrvotenum)) {
				foreach ($arrvotenum AS $index => $val) {
					$arrpostvote[$index] = $GLOBALS['postvote_'.$index];
				}//end foreach
			}
		}
		$postnum = 0;
		if (!empty($arrvotenum) AND is_array($arrvotenum)) {
			$comma = $separator = $newvotenum = $postoption = '';
			if (isset($GLOBALS['postvote']) AND $voteinfo['votetype'] == 0) { // 单选投票
				$postvote = intval($GLOBALS['postvote']);
				foreach ($arrvotenum AS $index => $val) {
					if ($postvote == $index) {
						$val++;
						$postoption = $index;
					}
					$newvotenum .= "{$separator}{$val}";
					$postnum = 1;
					$separator = '|';
				}//end foreach
			} elseif (!empty($arrpostvote) AND $voteinfo['votetype'] == 1) { // 多选投票
				foreach ($arrvotenum AS $index => $val) {
					if (isset($arrpostvote[$index]) AND $arrpostvote[$index] == $index) {
						$val++;
						$postnum++;
						$postoption .= "{$comma}{$index}";
						$comma = ',';
					}
					$newvotenum .= "{$separator}{$val}";
					$separator = '|';
				}//end foreach
			}
		}
		if ($postnum <= 0) { // 请选取投票项目。
			head(0,0,0,$arrNavMenu);
			showmsg($lang['err.str11']);
			exit;
		}
		$db->query("UPDATE {$dv}vote SET votenum='".$db->escape_string($newvotenum)."', voters=voters+1 WHERE voteid='{$voteid}'");
		$db->query("UPDATE {$dv}topic SET votetotal=votetotal+'{$postnum}' WHERE topicid='{$TopicID}'");
		$db->query("INSERT INTO {$dv}voteuser(voteid,userid,voteoption,votedate) VALUES('{$voteid}', '{$userid}', '{$postoption}', '".TIME_NOW."')");
	}
	if ($board_settings[50] == 1) {
		$db->query("UPDATE {$dv}topic SET lastposttime='".TIME_NOW."' WHERE topicid='{$TopicID}' AND istop=0");
	}
	header("Location: {$_SERVER['HTTP_REFERER']}");
	exit;
}//end function PostVote_Main
//-------------------------edit 2007-8-27 IvanSky  start active------------------------------------------------/

function PostActive_Main(){
 global $dv, $db, $master, $superboardmaster, $boardmaster, $lang, $userid, $boardid, $id, $voteid, $fsetting, $groupsetting, $userinfo, $board_settings;
 $stats = $fsetting['forumname'].' - '.$lang['VoteHeaderTitle'];
 $arrNavMenu = array(
   0=>array('linktitle'=>$lang['ActiveHeaderTitle'])
 );
  if( $_SERVER['HTTP_REFERER'] != '' ){
  $comeurl = $_SERVER['HTTP_REFERER'];
  }else{
   $comeurl = $fsetting['homepage'];
  }
 $TopicID = $GLOBALS['id'];
 $activeid = intval($_GET['activeid']);
 $timemode = $_POST['payment'];
 $systemmode = trim($_POST['contact']);
 $message = trim($_POST['message']);
 
 $gettimemode = trim($_POST['timemode']);
 $getstarttime = trim($_POST['starttime']);
 $getendtime = trim($_POST['endtime']);
 $getexpiretime = trim($_POST['expiretime']);
		

 if($timemode ==0)
 $costnum = 0;
 else
 $costnum = intval(trim($_POST['payvalue']));
  if( $query = $db->query("SELECT u1.sex,u1.strength,u2.usersex FROM {$dv}active as u1,{$dv}user as u2 WHERE activeid={$activeid}")){
  $activeinfo =& $db->fetch_array($query);
   if( !empty($activeinfo) ) {
   $db->free_result($query);
   }
  }
  if( $num = $db->query("SELECT count(*) as num from {$dv}activeuser where activeid='".$activeid."'")){
  $activenum = $db->fetch_array($num);
   if( !empty($activenum) ) {
   $db->free_result($num);
   }
  }
  if ( is_numeric ( $gettimemode ) and isset ( $gettimemode ) )
  {
	if ( $gettimemode == 0 )
	{
		if (!empty($getexpiretime) and $getexpiretime<=strtotime(date("Y-m-d H:i")))
		{
		   head(0,0,0,$arrNavMenu);
		   showmsg($lang['Active_Error.str16']);
		   exit;
		}
	}else{
		if (!empty($getendtime) and $getendtime<=strtotime(date("Y-m-d H:i")))
		{
		   head(0,0,0,$arrNavMenu);
		   showmsg($lang['Active_Error.str16']);
		   exit;
		}	
	}
  }

  if($activenum['num']>=$activeinfo['strength']){
   head(0,0,0,$arrNavMenu);
   showmsg($lang['Active_Error.str1']);
   exit;
  }
  if ($userid==0) { 
   head(0,0,0,$arrNavMenu);
   showmsg($lang['Active_Error.str2']);
   exit;
  }
  if($activeinfo['sex']!=-1){
   if($userinfo['usersex']!=$activeinfo['sex']){
   $setsex=$activeinfo['sex']==1?$lang['Active_boy.str1']:$lang['Active_gril.str1'];
   head(0,0,0,$arrNavMenu);
   $Active_Error_str3=str_replace('{$sex}',$setsex,$lang['Active_Error.str3']);
   showmsg($Active_Error_str3);
   exit;
   }
  }
  $activerid = $db->scalar("SELECT userid FROM {$dv}activeuser WHERE activeid='{$activeid}' AND userid='{$userid}'");
  if ($activerid > 0) { 
   head(0,0,0,$arrNavMenu);
   showmsg($lang['Active_Error.str4']);
   exit;
  }
  if ($timemode!=0) { 
   if($costnum==''){
   head(0,0,0,$arrNavMenu);
   showmsg($lang['Active_Error.str5']);
   exit;
   }
  }
 if (''==$systemmode||strlen($systemmode)<8) { 
  head(0,0,0,$arrNavMenu);
  showmsg($lang['Active_Error.str6']);
  exit;
 }
 if (!empty($systemmode)){
	 if (strpos($systemmode,'@&@') !== false){
		$systemmode = str_replace('@&@','',$systemmode);
	 }
		if ( $_POST[shield_contact] == '1' )
			$systemmode = $systemmode.'@&@1';
		else
			$systemmode = $systemmode.'@&@0';
 }

 if (!is_numeric($activeid)||$activeid<1) { 
  head(0,0,0,$arrNavMenu);
  showmsg($lang['Active_Error.str8']);
  exit;
 }
  if (!empty($message))
 {
	$message=iHtmltocode($message); 
 }
  $result = $db->query("INSERT INTO {$dv}activeuser(activeid,userid,joindate,costmode,costnum,systemmode,message,isexamine) VALUES(".$activeid.",".$userid.",".TIME_NOW.",".$timemode.",".$costnum.",'".$systemmode."','".$message."',0)");
  $db->query("UPDATE {$dv}topic SET lastposttime='".TIME_NOW."' WHERE topicid='{$TopicID}' AND istop=0");
  head(0,0,0,$arrNavMenu);
  showmsg($lang['Active_Error.str9'],1,$comeurl);
 exit;
}

function AdminActive_Main(){
 global $dv, $db, $master, $superboardmaster, $boardmaster, $lang, $userid, $boardid, $id, $voteid, $fsetting, $groupsetting, $userinfo, $board_settings;
 $stats = $fsetting['forumname'].' - '.$lang['Activeadmintittle'];
 $arrNavMenu = array(
   0=>array('linktitle'=>$lang['Activeadmintittle'])
 );
  $activeid = intval($_GET['activeid']);
  if(!is_numeric($activeid)||$activeid<1){
   head(0,0,0,$arrNavMenu);
   showmsg($lang['Active_Error.str8']);
   exit;
  }
  if( $query = $db->query("select postuserid from {$dv}active where activeid={$activeid}")){
  $activeinfo =& $db->fetch_array($query);
   if( !empty($activeinfo) ) {
   $db->free_result($query);
   }
  }
  if($userid!=$activeinfo['postuserid']){
   head(0,0,0,$arrNavMenu);
   showmsg($lang['Active_Error.str10']);
   exit;
  }
  if( $_SERVER['HTTP_REFERER'] != '' ){
  $comeurl = $_SERVER['HTTP_REFERER'];
  }else{
   $comeurl = $fsetting['homepage'];
  }
  $actionz = $_POST['actionz'];
  $activeuserid = intval($_POST['activeuserid']);
  if(empty($activeuserid)){
   head(0,0,0,$arrNavMenu);
   showmsg($lang['Active_Error.str11']);
   exit;
  }
  if ($userid==0) { 
   head(0,0,0,$arrNavMenu);
   showmsg($lang['Active_Error.str12']);
   exit;
  }
switch($actionz){
 case 'is':
  foreach($activeuserid as $val){
   $db->query("update {$dv}activeuser Set isexamine=1 Where id={$val}");
  }
  head(0,0,0,$arrNavMenu);
  showmsg($lang['Active_Error.str13'],1,$comeurl);
  break;
 case 'no':
  foreach($activeuserid as $val){
   $db->query("update {$dv}activeuser Set isexamine=0 Where id={$val}");
  }
  head(0,0,0,$arrNavMenu);
  showmsg($lang['Active_Error.str14'],1,$comeurl);
  break;
 case 'del':
  foreach($activeuserid as $val){
   $db->query("delete from {$dv}activeuser Where id={$val}");
  }
  head(0,0,0,$arrNavMenu);
  showmsg($lang['Active_Error.str15'],1,$comeurl);
  break;
 default:
  head(0,0,0,$arrNavMenu);
  showmsg($lang['Active_Error.str8']);
  break;
}
}
//-------------------------2007-8-27 IvanSky active end-------------------------------
function Argue_PostVote_Main()
{
	global $dv, $db, $master, $superboardmaster, $boardmaster, $lang, $userid, $boardid, $id, $voteid, $fsetting, $groupsetting, $userinfo, $board_settings, $argueid;
	require_once INC_PATH.'argue_mod.functions.php';
	$execresult = argumentation_post_vote($argueid, $userid, $rootid, $boardid);
	if (false === $execresult['execflag']) {
		showmsg($execresult['execmsg']);
		exit;
	}
}//end function Argue_PostVote_Main

?>