<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	TRACKBACK

	$RCSfile: trackback.php,v $
	$Revision: 1.18 $
	$Date: 2006/09/21 08:42:27 $
*/

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

header('Content-type: text/xml');
$id = empty($_GET['id'])?0:intval($_GET['id']);
$encode = empty($_GET['encode'])?'gbk':strtolower(addslashes(trim($_GET['encode'])));
if(isset($_REQUEST['url'])){
    $title = addslashes(trim($_REQUEST['title']));
    $content = addslashes(trim(strip_tags($_REQUEST['excerpt'])));
    $from = addslashes(trim($_REQUEST['blog_name']));
    $url = addslashes(trim($_REQUEST['url']));
}
$error = 1;

if(empty($url) || substr($url, 0, 7) != 'http://') {
    $msg = 'Invalid Parameter!';
} elseif(empty($id)) {
    $msg = 'TrackBack info is missing!';
} elseif(empty($encode)) {
    $msg = 'Character encoding is null!';
} else {
	include_once('./include/common.inc.php');
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('spaceitems').' WHERE itemid=\''.$id.'\' AND type=\'blog\' AND folder=\'1\'');
	if($item = $_SGLOBAL['db']->fetch_array($query)) {
		$insertsqlarr = array(
			'itemid' => $id,
			'type' => 'blog',
			'uid' => $item['uid'],
			'authorid' => 0,
			'author' => encodeconvert($encode, $from),
			'ip' => $_SGLOBAL['onlineip'],
			'dateline' => $_SGLOBAL['timestamp'],
			'rates' => 0,
			'message' => encodeconvert($encode, $content),
			'url' => $url,
			'subject' => encodeconvert($encode, $title)
		);
		$cid = inserttable('spacecomments', $insertsqlarr, 1);	
        if(!empty($cid)) {
        	$_SGLOBAL['db']->query('UPDATE '.tname('spaceitems').' SET trackbacknum=trackbacknum+1 WHERE itemid=\''.$id.'\'');	
        	$error = 0;
        } else {
        	$msg = 'Could not save trackback data, possibly because of mysql database!';
        }
	} else {
		$msg = 'Could not save trackback data!';
	}
}

echo '<?xml version="1.0" encoding="'.$_SCONFIG['charset'].'"?>';
if($error){
	echo "<response><error>1</error><message>$msg</message></response>";
} else {
	echo "<response><error>0</error></response>";
}

?>