<?php
if (!defined('R_P')) exit('Access Denied');
if(!intval($_SET['trackback'])) trackback_response(1, "Forbidden to ping back");
$param = array();
$param['id'] = intval($_GET['id']);
$param['channelid'] = intval($_GET['channelid']);
$param['url']  = isset($_POST['url'])  ? $_POST['url']     : '';
$charset = isset($_POST['charset']) ? $_POST['charset'] : $_SET['def_charset'];
$charset = strtolower($charset);
if($param['id'] < 1 || empty($channel[$param['channelid']])) trackback_response(1, 'I need an ID for this to work.');
if($charset != 'gb2312' && $charset != 'gbk' && $charset != 'utf-8') trackback_response(1, 'charset error');
$title  = isset($_POST['title']) ? trim($_POST['title']) : '';
$param['content']   = isset($_POST['excerpt']) ? trim($_POST['excerpt'])    : '';
$param['username'] = isset($_POST['blog_name']) ? trim($_POST['blog_name'])  : '';
$param['type'] = 'trackback';
$param['content'] = "[b][url={$param['url']}]{$title}[/url][/b]\n" . $param['content'];

$param['content'] = qbase::iconvs($param['content'] , $_SET['def_charset'], $charset);
$param['username'] = qbase::iconvs($param['username'] , $_SET['def_charset'], $charset);

$param['content'] = qbase::FilterString(strip_tags($param['content']));
$param['ip'] = qbase::get_ip();
$param['addtime'] = $_SERVER['REQUEST_TIME'];
$param['vote'] = 2;
$param['uid'] = 0;
$param['is_pass'] = intval($channel[$channelid]['coment_pass']) ;

if (empty($param['title']) && empty($param['url']) && empty($param['username'])) {
	qbase::showmsg("It's a wrong way","default", W_P);
	exit;
}

if ( !empty($param['url']) && !empty($title) ) {
	header('Content-Type: text/xml; charset=' . $_SET['def_charset'] );

	$info = qbase::get_object('content', $param['channelid'], $param['id'])->get_item();
	if(empty($info)) trackback_response(1, "not work");
	if($info['nocoment'] == 1 || $info['c_allow'] == 0) trackback_response(1, "Forbidden to ping back");

	$num = qdb::queryAlone("select count(*) as num from ".TABLEPRE."comment where channelid = '{$param['channelid']}' and id = '{$param['id']}' and url = '{$param['url']}' ");
	if ( $num ) trackback_response(1, 'A ping from that URL for this post is extist');

	qbase::get_object("comment", $param['channelid'], $param['id'])->add($param);
	trackback_response(0);
}else{
	trackback_response(1, "bad request");
}

function trackback_response($error_no = 0, $error_message = '') {
	global $_SET;
	header('Content-Type: text/xml; charset=' . $_SET['def_charset'] );
	if ($error_no) {
		echo '<?xml version="1.0" encoding="'. $_SET['def_charset'] ."\"?>\n";
		echo "<response>\n";
		echo "<error>1</error>\n";
		echo "<message>{$error_message}</message>\n";
		echo "</response>";
	} else {
		echo '<?xml version="1.0" encoding="'. $_SET['def_charset'] . "\"?>\n";
		echo "<response>\n";
		echo "<error>0</error>\n";
		echo "</response>";
	}
	exit;
}
?>