<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	系統調用到的一些操作

	$RCSfile: batch.viewlink.php,v $
	$Revision: 1.8 $
	$Date: 2006/10/16 10:07:59 $
*/

include_once('./include/main.inc.php');

$action = empty($_GET['action'])?'':$_GET['action'];
$itemid = empty($_GET['itemid'])?'':intval($_GET['itemid']);
$item = array();
if($itemid) {
	dbconnect();
	$query = $_SGLOBAL['db']->query("SELECT ii.*, i.* FROM ".tname('spaceitems')." i LEFT JOIN ".tname('spacelinks')." ii ON ii.itemid=i.itemid WHERE i.itemid='$itemid' AND i.folder='1' AND i.type='link'");
	$item = $_SGLOBAL['db']->fetch_array($query);
	if($item['itemtypeid']) {
		$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('itemtypes').' WHERE typeid=\''.$item['itemtypeid'].'\'');
		if($value = $_SGLOBAL['db']->fetch_array($query)) {
			if($value['viewperm']) {
				getcookie(1);
				$_SCONFIG['htmlviewspace'] = 0;
				if($item['uid'] != $_SGLOBAL['supe_uid']) {
					$query2 = $_SGLOBAL['db']->query('SELECT * FROM '.tname('friends').' WHERE uid='.$item['uid'].' AND frienduid='.$_SGLOBAL['supe_uid'].' AND grade IN ('.$value['viewperm'].') LIMIT 1');
					if(!$_SGLOBAL['db']->fetch_array($query2)) {
						messagebox('error', 'not_friend');
					}
				}
			}
			$item['itemtypename'] = $value['typename'];
		}
	}
}

if(empty($action)) {

	if(empty($item)) messagebox('error', 'not_found', S_URL);
	getcookie(1);	
	$code = authcode($item['uid'].'|'.$item['itemid'], 'ENCODE');

	$isupdate = freshcookie($itemid);
	
	if($item['uid'] != $_SGLOBAL['supe_uid']) {
		if($isupdate && $_SGLOBAL['supe_uid']) $_SGLOBAL['db']->query("REPLACE INTO ".tname('tracks')." (uid, itemid, dateline) VALUES ('$_SGLOBAL[supe_uid]', '$itemid', '$_SGLOBAL[timestamp]')");
	}

	if($isupdate) {
		$_SGLOBAL['db']->query('UPDATE '.tname('userspaces').' SET viewnum=viewnum+1 WHERE uid=\''.$item['uid'].'\'');
		$_SGLOBAL['db']->query('UPDATE '.tname('spaceitems').' SET viewnum=viewnum+1 WHERE itemid=\''.$itemid.'\'');
	}
	
	print<<<END
	<head>
	<title>$item[subject] $_SCONFIG[sitename]</title>
	<meta http-equiv="content-type" content="text/html; charset=$charset">
	</head>
	<frameset name="fset" rows="*,30px" border=0 >
		<frame name="main" src="$item[url]" >
		<frame name="bottom" height="30" src="$siteurl/batch.viewlink.php?action=bottom&code=$code" scrolling="no" noresize="true">
	</frameset>	
	</html>
END;
exit;

} elseif ($action == 'bottom') {

	$code = empty($_GET['code'])?'':$_GET['code'];
	$codestr = authcode($code, 'DECODE');
	$codearr = explode('|', $codestr);
	if(count($codearr) != 2) exit;
	$uid = intval($codearr[0]);
	$itemid = intval($codearr[1]);

	$itemurl = geturl('uid/'.$uid.'/action/viewspace/itemid/'.$itemid);
	print<<<END
	<head>
	<title>$_SCONFIG[sitename]</title>
	<meta http-equiv="content-type" content="text/html; charset=$charset">
	<style type="text/css">
	<!--
	body { font:75% Arial, Helvetica, sans-serif, "細明體"; color:#333; background: #CCC; text-align: center; margin: 0; line-height: 30px; }
	li { border-bottom: 1px solid #CCC; padding-bottom: 1em; margin-bottom: 1em; }
	h4 { font-size: 1.2em; margin: 0.5em 0; }
	a { color: #000; text-decoration: underline; }
		a:hover { text-decoration: none; }
		a:visited { color: #666; }
	-->
	</style>
	</head>
	<body>
	$_SCONFIG[sitename]
	<a href="$itemurl" target="_top">我要評論</a>
	| <a href="$siteurl/batch.viewlink.php?action=snapshot&amp;itemid=$itemid" target="main">查看快照</a>
	| <a href="$siteurl/batch.viewlink.php?action=copy&amp;itemid=$itemid" target="main">複製到我的書籤</a>
	| <a href="$siteurl/batch.viewlink.php?action=domain&amp;itemid=$itemid" target="main">查看同站點書籤</a>
	| <a href="$siteurl/batch.viewlink.php?action=subject&amp;itemid=$itemid" target="main">查看同標題書籤</a>
	| <a href="$siteurl" target="_top">返回首頁</a>
	</body>
	</html>
END;
exit;

} elseif ($action == 'domain') {

	if(empty($item)) messagebox('error', 'not_found', S_URL);
	
	$links = '';
	if(!empty($item['domain'])) {
		$domain = str_replace('\'', '', $item['domain']);
		$query = $_SGLOBAL['db']->query("SELECT ii.*, i.* FROM ".tname('spacelinks')." ii LEFT JOIN ".tname('spaceitems')." i ON i.itemid=ii.itemid WHERE ii.domain='$domain'");
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$value['dateline'] = sgmdate($value['dateline']);
			$links .= "<li>
				<h4><a href=\"$siteurl/batch.viewlink.php?itemid=$value[itemid]\" target=\"_top\">$value[subject]</a> (<a href=\"$siteurl/batch.viewlink.php?action=copy&itemid=$value[itemid]\" target=\"_top\">複製</a>)</h4>
				添加人:<a href=\"".geturl("uid/$value[uid]/action/space")."\" target=\"_top\">$value[username]</a> 時間:$value[dateline] 已有 $value[viewnum] 人點擊，$value[replynum] 人評論<br />
				$value[message]
				</li>";
		}
	}
	if(empty($links)) $links = '<li>沒有找到與指定站點 '.$item['domain'].' 相關的書籤</li>';

	print<<<END
	<head>
	<title>$item[domain] 同站相關書籤 $_SCONFIG[sitename]</title>
	<meta http-equiv="content-type" content="text/html; charset=$charset">
	<style type="text/css">
	<!--
	body { font:75% Arial, Helvetica, sans-serif, "細明體"; color:#333; }
	li { border-bottom: 1px solid #CCC; padding-bottom: 1em; margin-bottom: 1em; }
	h4 { font-size: 1.2em; margin: 0.5em 0; }
	a { color: #000; text-decoration: underline; }
		a:hover { text-decoration: none; }
		a:visited { color: #999; }
	-->
	</style>
	</head>
	<body>
	<ul>
	$links
	</ul>
	</body>
	</html>
END;
exit;

} elseif ($action == 'subject') {

	if(empty($item)) messagebox('error', 'not_found', S_URL);
	
	$links = '';
	if(!empty($item['subject'])) {
		include_once(S_ROOT.'./include/common.inc.php');

		$subject = stripsearchkey(cutstr($item['subject'], 18));
		
		$query = $_SGLOBAL['db']->query("SELECT i.* FROM ".tname('spaceitems')." i WHERE i.subject LIKE '%$subject%'");
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$value['dateline'] = sgmdate($value['dateline']);
			$links .= "<li>
				<h4><a href=\"$siteurl/batch.viewlink.php?itemid=$value[itemid]\" target=\"_top\">$value[subject]</a> (<a href=\"$siteurl/batch.viewlink.php?action=copy&itemid=$value[itemid]\" target=\"_top\">複製</a>)</h4>
				添加人:<a href=\"".geturl("uid/$value[uid]/action/space")."\" target=\"_top\">$value[username]</a> 時間:$value[dateline] 已有 $value[viewnum] 人點擊，$value[replynum] 人評論
				</li>";
		}
	}
	if(empty($links)) $links = '<li>沒有找到與標題 《'.$item['subject'].'》 相關的書籤</li>';

	print<<<END
	<head>
	<title>$item[subject] 同標題相關書籤 $_SCONFIG[sitename]</title>
	<meta http-equiv="content-type" content="text/html; charset=$charset">
	<style type="text/css">
	<!--
	body { font:75% Arial, Helvetica, sans-serif, "細明體"; color:#333; }
	li { border-bottom: 1px solid #CCC; padding-bottom: 1em; margin-bottom: 1em; }
	h4 { font-size: 1.2em; margin: 0.5em 0; }
	a { color: #000; text-decoration: underline; }
		a:hover { text-decoration: none; }
		a:visited { color: #999; }
	-->
	</style>
	</head>
	<body>
	<ul>
	$links
	</ul>
	</body>
	</html>
END;
exit;

} elseif ($action == 'copy') {
	
	getcookie(1);
	if(empty($_SGLOBAL['supe_uid'])) messagebox('error', 'no_login', geturl('action/login'));
	if($_SGLOBAL['supe_uid'] == $item['uid']) messagebox('error', 'no_self');
	
	include_once(S_ROOT.'./include/common.inc.php');
	$setsqlarr = array(
		'uid' => $_SGLOBAL['supe_uid'],
		'username' => $_SGLOBAL['supe_username'],
		'type' => 'link',
		'subject' => addslashes($item['subject']),
		'dateline' => $_SGLOBAL['timestamp']
	);
	$newitemid = inserttable('spaceitems', $setsqlarr, 1);
	
	$setsqlarr = array(
		'itemid' => $newitemid,
		'postip' => $_SGLOBAL['onlineip'],
		'url' => addslashes($item['url']),
		'domain' => addslashes($item['domain'])
	);
	inserttable('spacelinks', $setsqlarr);
	
	messagebox('ok', '恭喜，您指定的書籤已經成功\複製了');
	
} elseif ($action == 'snapshot') {
	
	if(empty($item['snaptext'])) messagebox('error', 'no_snaptext');
	if($item['snaptype'] == 1) {
		$item['snaptext'] = nl2br(str_replace(array("\t", '   ', '  '), array('&nbsp; &nbsp; &nbsp; &nbsp; ', '&nbsp; &nbsp;', '&nbsp;&nbsp;'), $item['snaptext']));
	}
	
	print<<<END
	<head>
	<title>$item[subject] 快照 $_SCONFIG[sitename]</title>
	<base href="$item[url]" />
	</head>
	<body>
	$item[snaptext]
	</body>
	</html>
END;
exit;

}

?>