<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	留言處理

	$RCSfile: batch.guestbook.php,v $
	$Revision: 1.8 $
	$Date: 2006/10/23 07:38:14 $
*/

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

$action = empty($_GET['action'])?'':$_GET['action'];

if(submitcheck('btncomment', 1)) {
	
	if(empty($_POST['uid'])) $_POST['uid'] = 0;
	$_POST['uid'] = intval($_POST['uid']);
	if(empty($_POST['uid'])) {
		messagebox('error', 'system_error');
	}
	
	getcookie(1);
	$author = $_SGLOBAL['supe_username'];
	if(empty($_SGLOBAL['supe_uid'])) {
		if(empty($_SCONFIG['allowguest'])) {
			messagebox('error', 'no_login', geturl('action/login'));
		}
		if(!empty($_POST['nickname'])) {
			$author = addslashes(cutstr(shtmlspecialchars(stripslashes($_POST['nickname'])), 15));
		}
	}
	
	$_POST['message'] = shtmlspecialchars(trim($_POST['message']));
	if(strlen($_POST['message']) < 2 || strlen($_POST['message']) >5000) {
		messagebox('error', 'message_length_error');
	}
	
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('userspaces').' WHERE uid=\''.$_POST['uid'].'\' AND islock=\'0\'');
	if(!$space = $_SGLOBAL['db']->fetch_array($query)) messagebox('error', 'system_error');
	
	$setsqlarr = array(
		'uid' => $space['uid'],
		'isprivate' => intval($_POST['isprivate']),
		'authorid' => $_SGLOBAL['supe_uid'],
		'author' => $author,
		'ip' => $_SGLOBAL['onlineip'],
		'dateline' => $_SGLOBAL['timestamp'],
		'message' => $_POST['message']
	);
	inserttable('guestbooks', $setsqlarr);
	
	clearspacecache($space['uid'], 'guestbook');
	
	$url = geturl("uid/$space[uid]/action/viewpro/php/1");
	messagebox('ok', 'succeed', $url);

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

	$uid = empty($_GET['uid'])?0:intval($_GET['uid']);
	if(empty($uid)) showxml('parameter error');
	$perpage = 20;
	$guestbooks = array();
	$multipage = '';
	$page = empty($_GET['page'])?1:intval($_GET['page']);
	$page = ($page<2)?1:$page;
	$start = ($page-1)*$perpage;

	$query = $_SGLOBAL['db']->query('SELECT COUNT(*) FROM '.tname('guestbooks').' WHERE uid=\''.$uid.'\'');
	$listcount = $_SGLOBAL['db']->result($query, 0);
	if($listcount) {
		
		getcookie(1);
		$query = $_SGLOBAL['db']->query('SELECT u.*, g.* FROM '.tname('guestbooks').' g LEFT JOIN '.tname('userspaces').' u ON u.uid=g.authorid WHERE g.uid=\''.$uid.'\' ORDER BY g.dateline DESC LIMIT '.$start.','.$perpage);
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$value['photo'] = getphoto($value['authorid'], $value['photo']);
			if($value['isprivate']) {
				if($uid == $_SGLOBAL['supe_uid'] || $value['authorid'] == $_SGLOBAL['supe_uid']) {
					$value['message'] = snl2br($value['message']);
				} else {
					$value['message'] = '本留言內容為悄悄話，你不能查看';
				}
			} else {
				$value['message'] = snl2br($value['message']);
			}
			$guestbooks[] = $value;
		}
		$multipage = multi($listcount, $perpage, $page, 'PAGE');
	}

	if($multipage) {
		$multipage = preg_replace("/\"PAGE\?page\=(\d+?)\"/", '"javascript:;" onclick="javascript:showguestbook(\\1);"', $multipage);
	}

	$html = "";
	
	if($guestbooks) {
		foreach ($guestbooks as $value) {
			if(empty($value['spacename']) || empty($value['authorid'])) {
				$value['author'] = '*'.$value['author'];
				$value['photo'] = "<img src=\"$value[photo]\" border=\"0\" class=\"xspace-signavatar xspace-imgstyle\" alt=\"\" />";
			} else {
				$url = geturl("uid/$value[authorid]/action/space/domain/$value[domain]");
				$value['photo'] = "<a href=\"".$url."\" target=\"_blank\"><img src=\"$value[photo]\" border=\"0\" class=\"xspace-signavatar xspace-imgstyle\" alt=\"\" /></a>";
				$value['author'] = "<a href=\"".$url."\" target=\"_blank\">$value[author]</a>";
			}
			$value['dateline'] = sgmdate($value['dateline']);

			$html .= "
			<dl id=\"xspace-comment{$value[gid]}\">
			<dt>
			$value[photo]
			$value[author]
			<span class=\"xspace-smalltxt\">留言於$value[dateline]</span>
			</dt>
			<dd>
			$value[message]
			</dd>
			</dl>
			";
		}
	} else {
		$html .= "<dd>還沒有留言信息<dd>";
	}
	
	$html .= "<div id=\"xspace-multipage-div\" class=\"xspace-multipage\">$multipage</div>";
	
	showxml($html);

}

?>