<?
if (!defined('_GNUBOARD_')) exit;

/////////////////////////////////////////
//                                     //
//     mics'php - Trackback Sender     //
//                                     //
//     COPYLEFT (c) by micsland.com    //
//                                     //
//     MODIFIED (c) by sir.co.kr       //
//                                     //
/////////////////////////////////////////

// return 存在就会出错
function send_trackback($tb_url, $url, $title, $blog_name, $excerpt) 
{
    /*
    // 修正allow_url_fopen = Off 设置是无法使用的bug
    // 必须allow_url_fopen = On 状态才能使用
    //检查地址
	$p_fp = fopen($tb_url,"r");
	if($p_fp) 
        @fclose($p_fp);
	else 
        return "引用地址已失效或服务不可用";
    */

	//内容整理
	$title = strip_tags($title);
	$excerpt = strip_tags($excerpt);

	$tmp_data = "url=".rawurlencode($url)."&title=".rawurlencode($title)."&blog_name=".rawurlencode($blog_name)."&excerpt=".rawurlencode($excerpt);

	//处理地址i
	$uinfo = parse_url($tb_url);
	if($uinfo[query]) $tmp_data .= "&".$uinfo[query];
	if(!$uinfo[port]) $uinfo[port] = "80";

	//最终传送
	$send_str = "POST ".$uinfo[path]." HTTP/1.1\r\n".
				"Host: ".$uinfo[host]."\r\n".
				"User-Agent: GNUBOARD\r\n".
				"Content-Type: application/x-www-form-urlencoded\r\n".
				"Content-length: ".strlen($tmp_data)."\r\n".
				"Connection: close\r\n\r\n".
				$tmp_data;
    $fp = @fsockopen($uinfo[host],$uinfo[port]);
	if(!$fp) 
        return "引用地址已失效或服务不可用";

	//传送
	//$fp = fsockopen($uinfo[host],$uinfo[port]);
	fputs($fp,$send_str);

	//获取应答
	while(!feof($fp)) $response .= fgets($fp,128);
	fclose($fp);

	//判断地址
	if(!strstr($response,"<response>"))
		return "引用地址错误";

	//获取XML
	$response = strchr($response,"<?");
	$response = substr($response,0,strpos($response,"</response>"));

	//判断是否错误
	if(strstr($response,"<error>0</error>")) 
        return "";
	else {
		$tb_error_str = strchr($response,"<message>");
		$tb_error_str = substr($tb_error_str,0,strpos($tb_error_str,"</message>"));
		$tb_error_str = str_replace("<message>","",$tb_error_str);
		return "引用通告传送失败: $tb_error_str";
	}
}
?>