<?
include "mysqlcon.php";
include "functions.php";
include "./include/trackback_cls.php";
$admin_info = mysql_fetch_assoc(mysql_query("SELECT url FROM bhost_admin"));
$is_error = "no";

if(isset($_GET['e_id'])) { 
  $e_id = $_GET['e_id'];
  if(mysql_num_rows(mysql_query("SELECT * FROM bhost_entries WHERE e_id='$e_id'")) != 1) { 
    $is_error = "yes";
    $error = "The entry ID specified is invalid.";
  }
} else { 
  $is_error = "yes";
  $error = "You must specify an entry ID.";
}


$entry_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_entries WHERE e_id='$e_id'"));
$author_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$entry[author_u_id]'"));
$weblog_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_weblogs WHERE w_id='$entry_info[w_id]'"));
$owner_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$weblog_info[u_id]'"));
$owner_group_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_groups WHERE g_id='$owner_info[g_id]'"));


if($owner_group_info[allow_trackback] != "1" | $entry_info[trackbacks] != "1") { 
  $is_error = "yes";
  $error = "This weblog entry does not allow trackbacks.";
}



header("Content-Type: text/xml");



$trackback = new Trackback("$weblog_info[title]", "$author_info[display_name]", "UTF-8");
$tb_w_id = $weblog_info[w_id];
$tb_e_id = $trackback->get_e_id;
$tb_url = $trackback->url;
$tb_title = $trackback->title;
$tb_excerpt = $trackback->excerpt;
if(isset($_POST['blog_name'])) { $tb_blog_name = $_POST['blog_name']; } else { $tb_blog_name = ""; }
$tb_ip = $_SERVER['REMOTE_ADDR'];
$tb_date = time();

if(str_replace(" ", "", $tb_url) == "") {
  $link = url("entry", "$owner_info[username]", "$tb_e_id");
  header("Location: $link");
  exit();
}

if(mysql_num_rows(mysql_query("SELECT * FROM bhost_trackbacks WHERE e_id='$tb_e_id' AND tb_url='$tb_url' AND tb_blog_name='$tb_blog_name'")) != 0) {
  $is_error = "yes";
  $error = "A trackback has already been sent from your blog URL to this entry.";
}

$previous_trackback = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_trackbacks WHERE e_id='$tb_e_id' AND tb_ip='$tb_ip' ORDER BY tb_id DESC LIMIT 1"));
if(($tb_date-$previous_trackback[tb_date]) < 15) {
  $is_error = "yes";
  $error = "You may only post a trackback once every 15 seconds.";
}

if(antispam($tb_blog_name, $tb_title, $tb_excerpt, $tb_url)) {
  $is_error = "yes";
  $error = "Your trackback appears to be spam. If you don't think your trackback is spam, please email the owner of the blog you are pinging.";
}


if($is_error != "yes") {
   mysql_query("INSERT INTO bhost_trackbacks (w_id, e_id, tb_blog_name, tb_url, tb_date, tb_title, tb_excerpt, tb_ip) VALUES ('$tb_w_id', '$tb_e_id', '$tb_blog_name', '$tb_url', '$tb_date', '$tb_title', '$tb_excerpt', '$tb_ip')");
   echo $trackback->recieve(true);
} else {
   echo $trackback->recieve(false, "$error");
}
?>