<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	查看評論頁面

	$RCSfile: modelcomment.php,v $
	$Revision: 1.1 $
	$Date: 2007/07/13 14:58:14 $
*/

if(!defined('IN_SUPESITE')) {
	exit('Access Denied');
}

include_once(S_ROOT.'./include/common.inc.php');
include_once(S_ROOT.'./function/model.func.php');

$modelname = empty($_SGET['name']) ? trim(postget('name')) : trim($_SGET['name']);
$cacheinfo = getmodelinfoall('modelname', $modelname);
if(empty($cacheinfo['models'])) {
	messagebox('error', 'visit_the_channel_does_not_exist', S_URL);
}
$modelsinfoarr = $cacheinfo['models'];

getcookie(0);

if(empty($modelsinfoarr['allowcomment'])) {
	messagebox('error', 'not_found', S_URL);
}

if(submitcheck('submitcomm', 1)) {
	$itemid = empty($_POST['itemid']) ? 0 : intval($_POST['itemid']);
	if(empty($itemid)) messagebox('error', 'not_found', S_URL);

	getcookie(1);
	if(empty($_SGLOBAL['supe_uid'])) {
		if(empty($_SCONFIG['allowguest'])) {
			$referarr = array();
			$referquery = empty($_SERVER['HTTP_REFERER']) ? '' : parse_url($_SERVER['HTTP_REFERER']);
			$referquery = empty($referquery['query']) ? '' : $referquery['query'];
			if(!empty($referquery)) {
				$referquery = addslashes($referquery);
				$referarr = parseparameter(str_replace(array('-','_'), '/', $referquery));
			}

			if(!empty($referarr['action']) && $referarr['action'] == 'model') {
				setcookie('_refer', rawurlencode(geturl('action/model/name/'.$modelname.'/itemid/'.$itemid, 1)));
			} else {
				setcookie('_refer', rawurlencode(geturl('action/modelcomment/name/'.$modelname.'/itemid/'.$itemid, 1)));
			}
			messagebox('error', 'no_login', geturl('action/login'));
		}
	}

	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname($modelname.'items').' WHERE itemid=\''.$itemid.'\' AND allowreply=\'1\'');
	if(!$item = $_SGLOBAL['db']->fetch_array($query)) messagebox('error', 'no_permission', S_URL);

	$_POST['messagecomm'] = shtmlspecialchars(trim($_POST['messagecomm']));
	if(strlen($_POST['messagecomm']) < 2 || strlen($_POST['messagecomm']) > 10000) messagebox('error', 'message_length_error');
	$_POST['messagecomm'] = str_replace('[br]', '<br>', $_POST['messagecomm']);
	$_POST['messagecomm'] = str_replace('[quote]', '<blockquote><h5>'.$lang['quote'].'</h5><p>', $_POST['messagecomm']);
	$_POST['messagecomm'] = str_replace('[/quote]', '<p></blockquote>', $_POST['messagecomm']);
	$setsqlarr = array(
		'itemid' => $itemid,
		'authorid' => $_SGLOBAL['supe_uid'],
		'author' => $_SGLOBAL['supe_username'],
		'ip' => $_SGLOBAL['onlineip'],
		'dateline' => $_SGLOBAL['timestamp'],
		'message' => $_POST['messagecomm']
	);
	if(!empty($modelsinfoarr['allowfilter'])) $setsqlarr = scensor($setsqlarr, 1);
	inserttable($modelname.'comments', $setsqlarr);

	$_SGLOBAL['db']->query('UPDATE '.tname($modelname.'items').' SET lastpost='.$_SGLOBAL['timestamp'].', replynum=replynum+1 WHERE itemid=\''.$itemid.'\'');
	if(!empty($_SCONFIG['ucmode']) && $_SGLOBAL['supe_uid'] != $item['uid'] && $item['uid'] != 0 && $_SGLOBAL['supe_uid'] != 0) {
		$feed['icon'] = 'post';
		$feed['title_template'] = 'feed_model_comment_title';
		$feed['title_data'] = array(
			'author' =>'<a href="space.php?uid='.$item['uid'].'" >'.$item['username'].'</a>',
			'modelname' => '<a href="'.S_URL_ALL.'/m.php?name='.$modelname.'" >'.$cacheinfo['models']['modelalias'].'</a>',
			'modelpost' =>'<a href="'.geturl('action/model/name/'.$modelname.'/itemid/'.$itemid, 1).'" >'.$item['subject'].'</a>'
		);
		postfeed($feed);
	}
	messagebox('ok', 'succeed', geturl('action/modelcomment/name/'.$modelname.'/itemid/'.$itemid));
}

if(!empty($_SGET['op']) && $_SGET['op'] == 'delete') {
	$cid = empty($_SGET['cid']) ? 0 : intval($_SGET['cid']);
	if(empty($cid)) {
		messagebox('error', 'not_found', S_URL);
	}
	$itemid = empty($_SGET['itemid']) ? 0 : intval($_SGET['itemid']);
	if(empty($itemid)) {
		messagebox('error', 'not_found', S_URL);
	}

	$deleteflag = false;
	getcookie(1);

	if(empty($_SGLOBAL['group'])) {
		messagebox('error', 'no_permission');
	}

	if($cid && $itemid && $_SGLOBAL['supe_uid']) {
		$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname($modelname.'comments').' WHERE cid=\''.$cid.'\'');
		if($comment = $_SGLOBAL['db']->fetch_array($query)) {
			if($_SGLOBAL['group']['groupid'] == 1 || $comment['authorid'] == $_SGLOBAL['supe_uid']) {
				$_SGLOBAL['db']->query('UPDATE '.tname($modelname.'items').' SET replynum=replynum-1 WHERE itemid=\''.$comment['itemid'].'\'');
				$_SGLOBAL['db']->query('DELETE FROM '.tname($modelname.'comments').' WHERE cid=\''.$cid.'\'');
				$deleteflag = true;
			}
		}
	}
	if($deleteflag) {
		messagebox('ok', 'succeed', geturl('action/modelcomment/name/'.$modelname.'/itemid/'.$itemid));
	} else {
		messagebox('error', 'no_permission');
	}
}

$perpage = !empty($modelsinfoarr['listperpage']) ? $modelsinfoarr['listperpage'] : 20;

$page = empty($_SGET['page']) ? 0 : intval($_SGET['page']);
$page = ($page<1)?1:$page;
$start = ($page-1)*$perpage;

$itemid = empty($_SGET['itemid'])?0:intval($_SGET['itemid']);
if(empty($itemid)) {
	messagebox('error', 'not_found', S_URL);
}

//導航
$channelsmore = array();
if(!empty($channels['menus']) && count($channels['menus']) > 12) {
	$channelsmore = $channels['menus'];
	for($i = 0; $i < 12; $i++) {
		array_shift($channelsmore);
	}
}

//自定義類別
if(!empty($cacheinfo['columns'])) {
	foreach($cacheinfo['columns'] as $tmpvalue) {
		if(!empty($tmpvalue['fielddata'])) {
			$temparr = explode("\r\n", $tmpvalue['fielddata']);
			if($tmpvalue['formtype'] != 'linkage') {
				$gatherarr[$tmpvalue['fieldname']] = $temparr;
			}
		}
	}
}

//投稿鏈接
$posturl = geturl('action/modelpost/name/'.$modelsinfoarr['modelname']);

$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname($modelname.'items').' WHERE itemid=\''.$itemid.'\' AND allowreply=\'1\'');
if(!$item = $_SGLOBAL['db']->fetch_array($query)) {
	messagebox('error', 'not_found', S_URL);
}

if(!empty($item['subjectimage'])) {
	$item['subjectimage'] = A_URL.'/'.$item['subjectimage'];
}

//分類
$guidearr = array();
$guidearr[] = array('url' => empty($channels['menus'][$modelsinfoarr['modelname']]) ? S_URL.'/m.php?name='.$modelsinfoarr['modelname'] : $channels['menus'][$modelsinfoarr['modelname']]['url'],'name' => $modelsinfoarr['modelalias']);
$guidearr[] = array('url' => geturl('action/model/name/'.$modelsinfoarr['modelname'].'/itemid/'.$itemid),'name' => $item['subject']);
$guidearr[] = array('url' => '#','name' => $lang['view_comments']);

$listcount = $item['replynum'];
$iarr = array();
$multipage = '';

if($listcount) {
	$i = ($page-1)*$perpage + 1;
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname($modelname.'comments').' WHERE itemid=\''.$itemid.'\' ORDER BY dateline DESC LIMIT '.$start.','.$perpage);
	while ($comment = $_SGLOBAL['db']->fetch_array($query)) {
		$comment['message'] = snl2br($comment['message']);
		$comment['num'] = $i;
		$i++;
		$iarr[] = $comment;
	}
	$urlarr = array('action'=>'modelcomment', 'name' =>$modelname, 'itemid' => $itemid);
	$multipage = multi($listcount, $perpage, $page, $urlarr, 0);
}

$title = $item['subject'].' - '.$_SCONFIG['sitename'];
$keywords = !empty($modelsinfoarr['seokeywords']) ? $modelsinfoarr['seokeywords'] : $modelsinfoarr['modelalias'];
$description = !empty($modelsinfoarr['seodescription']) ? $modelsinfoarr['seodescription'] : $modelsinfoarr['modelalias'];
$title = strip_tags($title);
$keywords = strip_tags($keywords);
$description = strip_tags($description);

if(allowfeed()) {
	$addfeedcheck = addfeedcheck(8) ? 'checked="checked"' : '';
}
//模板
if(empty($modelsinfoarr['tpl'])) {
	$tpldir = 'model/data/'.$modelsinfoarr['modelname'];
} else {
	$tpldir = 'mthemes/'.$modelsinfoarr['tpl'];
}

include template($tpldir.'/viewcomment.html.php', 1);
?>