<?php

define('THIS_SCRIPT', 'favlist');
require_once 'global.php';
$lang = load_lang($lang, 'usermanager' );
PageLoad();
LoadFooter();

function PageLoad(){
	global $stats,$hometitle,$arrNavMenu,$fsetting,$stats,$FavName_id,$UserFavName,$redcolor,$dv,$db,$Redcolor,$hometitle,$gettitle;
	global $stats,$lang,$userid,$membername,$boardmaster,$imgurl;
	if( $userid <= 0 ){
		LoadStyleJs();
		showmsg($lang['MustLogin']);
		exit;
	}
	switch($GLOBALS['action']){
		case 'add':
			$stats = $lang['tpl.fav.strings7'];
			$gettitle = $stats;
			head();
			FavAdd_Main();
			break;
		case 'space':
			head();
			FavAdd_space();
			break;
		default:
			$stats = $lang['FavHeaderTitle'];
			$arrNavMenu = array(
				0=>array('linktitle'=>$lang['HeaderTitle1'],'linkurl'=>'usermanager.php')
				,1=>array('linktitle'=>$stats)
			);
			LoadStyleJs();
			MainFunc();
			break;
	}
}//end function PageLoad

function MainFunc(){
	global $_SERVER,$lang,$dv,$db,$userid,$membername,$fsetting,$userinfo,$_DCACHE,$imgurl;
	require printout('usermanager.navmenu');

	$iMagicFace =  explode('|',$userinfo['userface']);
	if(!empty($iMagicFace[1])){
		$UserFace = "<img src=\"".Dv_FilterJS($iMagicFace[1])."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";
	}
	elseif(!empty($iMagicFace[0])){
		$UserFace = "<img src=\"".Dv_FilterJS($iMagicFace[0])."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";
	}
	else{
		$UserFace = "<img src=\"".$userinfo['userface']."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";			
	}
	$TableWidth =& $_DCACHE['stylecss'][1];
	$color =& $_DCACHE['stylecss'][19];
	$user_Article =& $userinfo['userpost'];
	$user_Group =& $userinfo['userclass'];
	$user_Face_left = $UserFace;
	$user_Title =& $userinfo['usertitle'];
	$user_Wealth =& $userinfo['userwealth'];
	$user_EP =& $userinfo['userep'];
	$user_CP =& $userinfo['usercp'];
	$user_Money =& $userinfo['usermoney'];
	$user_Ticket =& $userinfo['userticket'];
	$user_IsBest =& $userinfo['userisbest'];
	$user_AddDate =& $userinfo['joindate'];
	$user_Logins =& $userinfo['userlogins'];
	$username =& $userinfo['username'];
	$msg_newincept = intval($userinfo['uermsg']);

	$fav_del = $imgurl.$lang['tpl.fav.pic13'];
	if( $GLOBALS['action'] == 'delet' ){
		deleteFunc();
	} else{
		$PageListNum = intval($fsetting['otherpagesize']);
		if ($PageListNum < 1) {
			$PageListNum = 20;
		}
		$CurrentPage = intval($GLOBALS['page']);
		$totalrec = intval($db->scalar("SELECT COUNT(1) FROM {$dv}bookmark WHERE username='{$membername}'"));
		if( $totalrec <= 0 ){
			showmsg($lang['tpl.fav.strings50']);
			exit;
		}
		$page_count = 0;
		if( $query = $db->query("SELECT id,username,url,topic,addtime FROM {$dv}bookmark WHERE username='{$membername}' ORDER BY id DESC" ,array('absolutePage'=>$CurrentPage,'pageSize'=>$PageListNum)) ){
			while( $arrFavList[] = $db->fetch_array($query) ){
				$page_count++;
			}
			$db->free_result($query);
			unset($arrFavList[count($arrFavList)-1]);
		}
		$Pcount = ceil( (float)$totalrec / (float)$PageListNum );
		if( $CurrentPage > $Pcount ) $CurrentPage = $Pcount;
		if( $CurrentPage < 1 ) $CurrentPage = 1;
		if( $page_count == 0 ) $CurrentPage = 0;
		require printout('favlist');
	}
}//end function MainFunc

function deleteFunc(){
	global $lang,$_SERVER,$dv,$db,$membername;
	$id = intval($GLOBALS['id']);
	if( $id <= 0 ){
		head();
		showmsg($lang['error.fav.strings1']);
		exit;
	}
	$db->query("DELETE FROM {$dv}bookmark WHERE username='{$membername}' AND id={$id}");
	LoadStyleJs();
	showmsg($lang['tpl.fav.strings46'],1,$_SERVER['HTTP_REFERER']);
}//end function deleteFunc

function FavAdd_Main(){
	global $_SERVER,$lang,$dv,$db,$userid,$membername,$fsetting,$boardid;
	if( !is_numeric($GLOBALS['id']) ){
	} else{
		$AnnounceID = intval($GLOBALS['id']);
	}
	$topic = $db->scalar("SELECT title FROM {$dv}topic WHERE topicid={$AnnounceID}");
	if( $Topic === false ){
		showmsg($lang['error.fav.strings1'],1,$_SERVER['HTTP_REFERER']);
		exit;
	}
	$Url = "dispbbs.php?";
	$Url .= "boardid={$boardid}&id={$AnnounceID}";
	$count = intval($db->scalar("SELECT COUNT(1) FROM {$dv}bookmark WHERE username='{$membername}' AND Url='{$Url}'"));
	if( $count > 0 ){
		showmsg($lang['error.fav.strings2'],1,$_SERVER['HTTP_REFERER']);
		exit;
	}
	$topic = htmlspecialchars($topic);
	$db->query("INSERT INTO {$dv}bookmark(username,topic,url,addtime) VALUES('{$membername}','".substr($topic,0,100)."','{$Url}','".TIME_NOW."')");
	showmsg($lang['tpl.fav.strings8'],1,$_SERVER['HTTP_REFERER']);
}//end function FavAdd_Main

function FavAdd_space(){
	global $_SERVER,$lang,$dv,$db,$userid,$membername,$fsetting,$groupsetting,$boardid,$boardmaster;
	if( $fsetting['plus_blog'] == 0 ){
		showmsg($lang['error.fav.strings3']);
		exit;
	}
	if( !is_numeric($GLOBALS['id']) || !is_numeric($GLOBALS['replyid']) ){
		showmsg($lang['error.fav.strings4']);
		exit;
	}
	$AnnounceID = $TopicID = intval($GLOBALS['id']);
	$ReplyID = intval($GLOBALS['replyid']);
	$boardid = intval($GLOBALS['boardid']);
	if( $query = $db->query("SELECT posttable,boardid,topicmode FROM {$dv}topic WHERE topicid={$TopicID}") ){
		$result = $db->fetch_array($query);
		$db->free_result($query);
	}
	if( !empty($result) && is_array($result) ){
		if( $result['boardid'] != $boardid ){
			showmsg($lang['error.fav.strings29']);
			exit;
		}
		$TotalUseTable = $result['posttable'];
		$TopicMode = $result['topicmode'];
	} else{
		showmsg($lang['error.fav.strings32']);
		exit;
	}
	unset($result);
	if( $query = $db->query("SELECT announceid,parentid,boardid,username,postuserid,topic,body,dateandtime,length,rootid,layer,orders,isbest,ip,expression,locktopic,signflag,emailflag,isagree,isaudit,isupload,postbuyuser,ubblist,getmoney,usetools,getmoneytype FROM {$TotalUseTable} WHERE boardid={$boardid} AND announceid={$ReplyID}", array('pageSize'=>1)) ){
		$result = $db->fetch_array($query);
		$db->free_result($query);
	}
	if( !empty($result) && is_array($result) ){
		if( intval($result['isbest']) == 1 && $groupsetting[41] == 0 ){
			showmsg($lang['error.fav.strings8']);
			exit;
		}
		if( $result['locktopic'] == 444 ){
			showmsg($lang['error.fav.strings8']);
			exit;
		}
		if( $userid != $result['postuserid'] && $groupsetting[2] == 0 ){
			showmsg($lang['error.fav.strings31']);
			exit;
		}
		$PostBuyUser = $result['postbuyuser'];
		if( $result['getmoneytype'] == 3 && $result['parentid'] == 0 && !$boardmaster ){
			if( strpos($PostBuyUser,"|||{$membername}|||") === false ){
				showmsg($lang['error.fav.strings5']);
				exit;
			}
		}
		if( $result['postuserid'] == 0 ){
			$UserGroupID = 7;
		} else{
			$UserGroupID = intval($db->scalar("SELECT usergroupid FROM {$dv}user WHERE userid={$result[postuserid]}"));
		}
		$ReplyID_a = $result['announceid'];
		$AnnounceID_a = $result['announceid'];
		$RootID_a = $result['rootid'];
		$Ubblists = $result['ubblist'];
		$Topic = $result['topic'];
		if( $TopicMode != '1' ){
			$Topic = str_replace('<','&lt;',$Topic);
		} else{
			if( $result['parentid'] != 0 ){
				$Topic = str_replace('<','&lt;',$Topic);
			}
		}
		$Topic = htmlspecialchars(ChkBadWords($Topic));
		if( $result['signflag'] == 2 ){
			$UserName = $lang['UserName1'];
		} elseif( $UserGroupID == 7 ){
			$UserName = $lang['UserName2'];
		} else{
			$UserName = htmlspecialchars(ChkBadWords($result['username']));
		}
		$Body = ChkBadWords($result['body']);
		if( strpos($Ubblists,',39,') !== false ){
		} else{
		}
	} else{
		showmsg($lang['error.fav.strings32']);
		exit;
	}
}//end function FavAdd_space

function EncodeJS($str){
	if( $str == '' ) return '';
	return str_replace("\r",'',str_replace("\r\n","\n",str_replace("'","''",str_replace('\\','\\',str_replace("\n",'',$str)))));
}//end function EncodeJS


?>