<?
include "header.php";
include "./include/trackback_cls.php";

if(isset($_POST['page'])) { $page = $_POST['page']; } elseif(isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; }
if(isset($_GET['category'])) { $category = $_GET['category']; } else { $category = ""; }


// SET PRIVACY SETTINGS
if($is_admin_logged_in == "yes") {
$privacy = "AND (privacy='0' OR privacy='1' OR privacy='2' OR privacy='3')";
} elseif($user_status == "owner" | $user_status == "admin" | $user_status == "member" | $user_status == "site_owner") {
$privacy = "AND (privacy='0' OR privacy='1' OR privacy='2' OR privacy='3')";
} elseif($owner_group_info[allow_friends] == "1" & $is_logged_in == "yes" & $user_status == "guest" & $is_friend == "yes") {
$privacy = "AND (privacy='1' OR privacy='2' OR privacy='3')";
} elseif($is_logged_in == "yes") {
$privacy = "AND (privacy='2' OR privacy='3')";
} else {
$privacy = "AND privacy='3'";
}

// IF PRIVACY FEATURE IS DISABLED BY ADMIN
if($owner_group_info[allow_privacy] != 1) {
$privacy = "AND (privacy='0' OR privacy='1' OR privacy='2' OR privacy='3')";
}

// RETRIEVE TEMPLATE
$template = $weblog_info[template];
$template = bh_engine($template);

//REPLACE windowtitle VARIABLE
$template = str_replace("<%WindowTitle%>", "$weblog_info[title] - BlogHoster", $template);

// REMOVE ANYTHING BETWEEN <IFENTRYPAGE> CONTAINERS
$ifentrypagecount = occurrencecount("<IfEntryPage>", "</IfEntryPage>", $template, "0");
for($a=0;$a<$ifentrypagecount;$a++) {
$template = str_replace("<IfEntryPage>".between("<IfEntryPage>", "</IfEntryPage>", $template)."</IfEntryPage>", "", $template);
}

// REMOVE ANYTHING BETWEEN <IFARCHIVEPAGE> CONTAINERS
$ifarchivepagecount = occurrencecount("<IfArchivePage>", "</IfArchivePage>", $template, "0");
for($a=0;$a<$ifarchivepagecount;$a++) {
$template = str_replace("<IfArchivePage>".between("<IfArchivePage>", "</IfArchivePage>", $template)."</IfArchivePage>", "", $template);
}

// REMOVE ANYTHING BETWEEN <IFFRIENDSPAGE> CONTAINERS
$iffriendspagecount = occurrencecount("<IfFriendsPage>", "</IfFriendsPage>", $template, "0");
for($a=0;$a<$iffriendspagecount;$a++) {
$template = str_replace("<IfFriendsPage>".between("<IfFriendsPage>", "</IfFriendsPage>", $template)."</IfFriendsPage>", "", $template);
}


// CHECK FOR COMMENT PERMISSIONS
if($owner_group_info[allow_comments] == "0" | $weblog_info[show_comments] == "0") {
$remove_comments = "yes"; 
} elseif($owner_group_info[allow_friends] == "1" & $weblog_info[show_comments] == "1" & ($user_status != "owner" & $user_status != "admin" & $user_status != "member" & $user_status != "site_owner") & $is_admin_logged_in != "yes" & ($is_logged_in == "no" | ($user_status == "guest" & $is_friend != "yes"))) { 
$remove_comments = "yes";
} elseif($weblog_info[show_comments] == "2" & $is_logged_in == "no" & $is_admin_logged_in != "yes") {
$remove_comments = "yes";
} else {
$remove_comments = "no";
}


// REMOVE ANYTHING BETWEEN <IFCOMMENTSALLOWED> AND <COMMENT> CONTAINERS IF COMMENTS NOT ALLOWED
if($remove_comments == "yes") {
 $ifcommentsallowedcount = occurrencecount("<IfCommentsAllowed>", "</IfCommentsAllowed>", $template, "0");
 for($a=0;$a<$ifcommentsallowedcount;$a++) {
 $template = str_replace("<IfCommentsAllowed>".between("<IfCommentsAllowed>", "</IfCommentsAllowed>", $template)."</IfCommentsAllowed>", "", $template);
 }
 $commentcontcount = occurrencecount("<Comment>", "</Comment>", $template, "0");
 for($a=0;$a<$commentcontcount;$a++) {
 $template = str_replace("<Comment>".between("<Comment>", "</Comment>", $template)."</Comment>", "", $template);
 }
}




// CHECK FOR POST COMMENT PERMISSIONS
if($owner_group_info[allow_comments] == "0" | $weblog_info[show_comments] == "0" | $weblog_info[who_comments] == "0") {
$remove_post_comments = "yes"; 
} elseif($owner_group_info[allow_friends] == "1" & $weblog_info[who_comments] == "1" & ($user_status != "owner" & $user_status != "admin" & $user_status != "member" & $user_status != "site_owner") & $is_admin_logged_in != "yes" & ($is_logged_in == "no" | ($user_status == "guest" & $is_friend != "yes"))) { 
$remove_post_comments = "yes";
} elseif($weblog_info[who_comments] == "2" & $is_logged_in == "no" & $is_admin_logged_in != "yes") {
$remove_post_comments = "yes";
} else {
$remove_post_comments = "no";
}


// REMOVE ANYTHING BETWEEN <IFCOMMENTPOSTALLOWED> CONTAINERS IF POST COMMENTS NOT ALLOWED
if($remove_post_comments == "yes") {
 $ifpostcommentsallowedcount = occurrencecount("<IfCommentPostAllowed>", "</IfCommentPostAllowed>", $template, "0");
 for($a=0;$a<$ifpostcommentsallowedcount;$a++) {
 $template = str_replace("<IfCommentPostAllowed>".between("<IfCommentPostAllowed>", "</IfCommentPostAllowed>", $template)."</IfCommentPostAllowed>", "", $template);
 }
}


// REMOVE ANYTHING BETWEEN <IFTRACKBACKSALLOWED> AND <TRACKBACK> CONTAINERS IF TRACKBACKS NOT ALLOWED
if($owner_group_info[allow_trackback] == "0") {
 $iftrackbacksallowedcount = occurrencecount("<IfTrackbacksAllowed>", "</IfTrackbacksAllowed>", $template, "0");
 for($a=0;$a<$iftrackbacksallowedcount;$a++) {
 $template = str_replace("<IfTrackbacksAllowed>".between("<IfTrackbacksAllowed>", "</IfTrackbacksAllowed>", $template)."</IfTrackbacksAllowed>", "", $template);
 }
 $trackbackcontcount = occurrencecount("<Trackback>", "</Trackback>", $template, "0");
 for($a=0;$a<$trackbackcontcount;$a++) {
 $template = str_replace("<Trackback>".between("<Trackback>", "</Trackback>", $template)."</Trackback>", "", $template);
 }
}



//IF DATES SET
$oldest_entry = mysql_fetch_assoc(mysql_query("SELECT e_id, date FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND draft='0' $privacy ORDER BY date ASC LIMIT 1"));
$newest_entry = mysql_fetch_assoc(mysql_query("SELECT e_id, date FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND draft='0' $privacy ORDER BY date DESC LIMIT 1"));

 $fromyear = cdate("Y", $oldest_entry[date]);
 $toyear = cdate("Y", $newest_entry[date]);
 $y = "";
 $frommonth = 1;
 $tomonth = 12;
 $m = "";
 $fromday = 1;
 $today = 31;
 $d = "";

if(isset($_GET['thisy'])) { 
 $y = $_GET['thisy'];
 if((int)$y == $y & is_numeric($y)) { 
  $fromyear = $y; 
  $toyear = $y; 
  if(isset($_GET['thism'])) { 
   $m = $_GET['thism'];
   if((int)$m > 1 & (int)$m < 12) { 
    $frommonth = $m; 
    $tomonth = $m; 
    if($m == "2" & $today > 29) { if(date("L", MakeTime("0", "0", "0", "$m", "10", "$y")) == "1") { $today = 29; } else { $today = 28; } } elseif(($m == "4" | $m == "6" | $m == "9" | $m == "11") & $today > 30) { $today = 30; }
    if(isset($_GET['thisd'])) { 
     $d = $_GET['thisd'];
     if((int)$d > 1 & (int)$d < 31) { 
      $fromday = $d; 
      $today = $d+1; 
     } 
    }
   } 
  }
 } 
}

$fromdate = MakeTime("0", "0", "0", "$frommonth", "$fromday", "$fromyear");
$todate = MakeTime("0", "0", "0", "$tomonth", "$today", "$toyear");


// CREATE PAGE VARIABLES
//IF CATEGORIES PAGE
if($category != "") {
$cat_name = str_replace("+", " ", $category);
$category = mysql_fetch_assoc(mysql_query("SELECT ec_id FROM bhost_entrycategories WHERE name='$cat_name' AND w_id='$weblog_info[w_id]'"));
$total_entries = mysql_num_rows(mysql_query("SELECT e_id FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND draft='0' AND ec_id='$category[ec_id]' AND (date >= '$fromdate' AND date < '$todate') $privacy"));
$file = "index_cat";
} else {
$total_entries = mysql_num_rows(mysql_query("SELECT e_id FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND draft='0' AND (date >= '$fromdate' AND date < '$todate') $privacy"));
$cat_name = "";
$file = "index";
}



if($weblog_info[entries_per_page] == 0) { $weblog_info[entries_per_page] = 5; }
if(($total_entries % $weblog_info[entries_per_page]) != 0) {
$maxpage = ($total_entries) / $weblog_info[entries_per_page] + 1;
} else {
$maxpage = ($total_entries) / $weblog_info[entries_per_page];
}
$maxpage = (int) $maxpage;
if($maxpage == 0) { $maxpage = 1; }
if($page > $maxpage) { $page = $maxpage; } elseif($page < 1) { $page = 1; }
$start = ($page - 1) * $weblog_info[entries_per_page];
$prev = $page - 1;
$next = $page + 1;

// REMOVE ANYTHING BETWEEN <IFPAGES> CONTAINERS IF ONLY ONE PAGE
if($maxpage == 1) {
 $ifpagescount = occurrencecount("<IfPages>", "</IfPages>", $template, "0");
 for($a=0;$a<$ifpagescount;$a++) {
 $template = str_replace("<IfPages>".between("<IfPages>", "</IfPages>", $template)."</IfPages>", "", $template);
 }
}

// REMOVE ANYTHING BETWEEN <LATERLINK> IF NOTHING LATER
if($page == 1) {
 $template = str_replace("<LaterLink>", "", $template);
 $template = str_replace("</LaterLink>", "", $template);
}

// REMOVE ANYTHING BETWEEN <EARLIERLINK> IF NOTHING EARLIER
if($page == $maxpage) {
 $template = str_replace("<EarlierLink>", "", $template);
 $template = str_replace("</EarlierLink>", "", $template);
}

// INPUT PAGE VARIABLES
$search_array2 = Array("<LaterLink>", "</LaterLink>", "<EarlierLink>", "</EarlierLink>", "<%CurrentPage%>", "<%TotalPages%>");
$replace_array2 = Array("<a class='laterlink' href='".url("$file", "$weblog_info[weblog]", "", "", "", "", "&thisy=$y&thism=$m&thisd=$d", "$prev", "$cat_name")."'>", "</a>", "<a class='earlierlink' href='".url("$file", "$weblog_info[weblog]", "", "", "", "", "&thisy=$y&thism=$m&thisd=$d", "$next", "$cat_name")."'>", "</a>", "$page", "$maxpage");
$template = str_replace($search_array2, $replace_array2, $template);

// REMOVE ENTRY CATEGORIES IF NOT ALLOWED
if($owner_group_info[allow_entry_cat] != "1") {
 $ifcategoriesallowedcount = occurrencecount("<IfCategoriesAllowed>", "</IfCategoriesAllowed>", $template, "0");
 for($a=0;$a<$ifcategoriesallowedcount;$a++) {
 $template = str_replace("<IfCategoriesAllowed>".between("<IfCategoriesAllowed>", "</IfCategoriesAllowed>", $template)."</IfCategoriesAllowed>", "", $template);
 }
 $template = str_replace("<%EntryCategory%>", "", $template);
}


// CHECK FOR ENTRIES
$entrycount = occurrencecount("<Entry>", "</Entry>", $template, "0");
for($i=0;$i<$entrycount;$i++) {
$insert = "";


//IF CATEGORIES PAGE
if($category != "") {
$cat_name = str_replace("+", " ", $_GET['category']);
$category = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_entrycategories WHERE name='$cat_name' AND w_id='$weblog_info[w_id]'"));
$entries = mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND draft='0' AND ec_id='$category[ec_id]' AND (date >= '$fromdate' AND date < '$todate') $privacy ORDER BY date DESC, e_id DESC LIMIT $start, $weblog_info[entries_per_page]");
} else {
$entries = mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND draft='0' AND (date >= '$fromdate' AND date < '$todate') $privacy ORDER BY date DESC, e_id DESC LIMIT $start, $weblog_info[entries_per_page]");
}


$entry = between("<Entry>", "</Entry>", $template);
while($entry_info = mysql_fetch_assoc($entries)) {
$entry_author = mysql_fetch_assoc(mysql_query("SELECT username, display_name, avatar FROM bhost_users WHERE u_id='$entry_info[author_u_id]'"));
$numofcomments = mysql_num_rows(mysql_query("SELECT c_id FROM bhost_comments WHERE w_id='$weblog_info[w_id]' AND e_id='$entry_info[e_id]'"));
$numoftrackbacks = mysql_num_rows(mysql_query("SELECT tb_id FROM bhost_trackbacks WHERE w_id='$weblog_info[w_id]' AND e_id='$entry_info[e_id]'"));

if($entry_author[display_name] == "") { $entry_author[display_name] = $entry_author[username]; }

$remove_entry_comments = "no";
$this_entry = $entry;
if($entry_info[comments] == "0") { 
 // REMOVE ANYTHING BETWEEN <IFCOMMENTSALLOWED> AND <COMMENT> CONTAINERS IF COMMENTS NOT ALLOWED IN ENTRY
 $ifcommentsallowedcount = occurrencecount("<IfCommentsAllowed>", "</IfCommentsAllowed>", $this_entry, "0");
 for($a=0;$a<$ifcommentsallowedcount;$a++) {
 $this_entry = str_replace("<IfCommentsAllowed>".between("<IfCommentsAllowed>", "</IfCommentsAllowed>", $this_entry)."</IfCommentsAllowed>", "", $this_entry);
 }
 $commentcontcount = occurrencecount("<Comment>", "</Comment>", $this_entry, "0");
 for($a=0;$a<$commentcontcount;$a++) {
 $this_entry = str_replace("<Comment>".between("<Comment>", "</Comment>", $this_entry)."</Comment>", "", $this_entry);
 }
 $ifpostcommentsallowedcount = occurrencecount("<IfCommentPostAllowed>", "</IfCommentPostAllowed>", $this_entry, "0");
 for($a=0;$a<$ifpostcommentsallowedcount;$a++) {
 $this_entry = str_replace("<IfCommentPostAllowed>".between("<IfCommentPostAllowed>", "</IfCommentPostAllowed>", $this_entry)."</IfCommentPostAllowed>", "", $this_entry);
 }
$remove_entry_comments = "yes"; 
}


// REMOVE ANYTHING BETWEEN <IFTRACKBACKSALLOWED> AND <TRACKBACK> CONTAINERS IF TRACKBACKS NOT ALLOWED
if($owner_group_info[allow_trackback] == "0" | $entry_info[trackbacks] == "0") {
 $iftrackbacksallowedcount = occurrencecount("<IfTrackbacksAllowed>", "</IfTrackbacksAllowed>", $this_entry, "0");
 for($a=0;$a<$iftrackbacksallowedcount;$a++) {
 $this_entry = str_replace("<IfTrackbacksAllowed>".between("<IfTrackbacksAllowed>", "</IfTrackbacksAllowed>", $this_entry)."</IfTrackbacksAllowed>", "", $this_entry);
 }
 $trackbackcontcount = occurrencecount("<Trackback>", "</Trackback>", $this_entry, "0");
 for($a=0;$a<$trackbackcontcount;$a++) {
 $this_entry = str_replace("<Trackback>".between("<Trackback>", "</Trackback>", $this_entry)."</Trackback>", "", $this_entry);
 }
}

if($entry_info[ec_id] == 0) {
 $ifcategoriesallowedcount = occurrencecount("<IfCategoriesAllowed>", "</IfCategoriesAllowed>", $this_entry, "0");
 for($a=0;$a<$ifcategoriesallowedcount;$a++) {
 $this_entry = str_replace("<IfCategoriesAllowed>".between("<IfCategoriesAllowed>", "</IfCategoriesAllowed>", $this_entry)."</IfCategoriesAllowed>", "", $this_entry);
 }
$entry_category = "$weblog_index2";
} else {
$entry_cat = mysql_fetch_assoc(mysql_query("SELECT ec_id, name FROM bhost_entrycategories WHERE ec_id='$entry_info[ec_id]' AND w_id='$weblog_info[w_id]'"));
$entry_category = "<a href='".url("index_cat", "$weblog_info[weblog]", "", "0", "", "", "", "", "$entry_cat[name]")."'>$entry_cat[name]</a>";
}



$insert .= "<a name='e$entry_info[e_id]'></a>".$this_entry;
  $entrytitle = str_replace("&lt;", "<", $entry_info[title]);
  $entrytitle = str_replace("&gt;", ">", $entrytitle);
  $entrybody = str_replace("&lt;", "<", $entry_info[contents]);
  $entrybody = str_replace("&gt;", ">", $entrybody);
  $entrybody = str_replace("&#39;", "'", $entrybody);

// CHECK FOR MISSING AVATAR FIRST
$entry_avatar = $entry_author[avatar];
$avatar_stripped = str_replace(" ", "", $entry_avatar);
if($avatar_stripped == "" OR $avatar_stripped == "http://") {
$entry_avatar = url("/images/", "images/noavatar2.gif");
}

$search_array3 = Array("<%EntryDate%>", "<%EntryTitle%>", "<%EntryURL%>", "<%EntryBody%>", "<%EntryAuthor%>", "<%EntryAuthorURL%>", "<%EntryAuthorAvatar%>", "<%EntryTime%>", "<%EntryCategory%>", "<SendThisLink>", "</SendThisLink>");
$replace_array3 = Array(cdate("$weblog_info[format_date]", timezone($entry_info[date])), "$entrytitle", url("entry", "$weblog_info[weblog]", "", "$entry_info[e_id]"), "$entrybody", "$entry_author[display_name]", url("profile", "", "$entry_author[username]"), "$entry_avatar", cdate("$weblog_info[format_time]", timezone($entry_info[date])), "$entry_category", "<a href='".url("sendlink", "$weblog_info[weblog]", "", "$entry_info[e_id]")."'>", "</a>");
$insert = str_replace($search_array3, $replace_array3, $insert);


if($remove_comments != "yes" & $remove_entry_comments != "yes") {
 $insert = str_replace("<%EntryCommentCount%>", $numofcomments, $insert);
 $insert = str_replace("<%CommentPostURL%>", url("post_comment", "$weblog_info[weblog]", "", "$entry_info[e_id]"), $insert);

  $commentcount = occurrencecount("<Comment>", "</Comment>", $insert, "0");
  for($f=0;$f<$commentcount;$f++) {
  $insert2 = "";
  $comment = between("<Comment>", "</Comment>", $insert);
  $comments = mysql_query("SELECT * FROM bhost_comments WHERE w_id='$weblog_info[w_id]' AND e_id='$entry_info[e_id]' ORDER BY date");
   while($comment_info = mysql_fetch_assoc($comments)) {
    $comment_author_query = mysql_query("SELECT u_id, avatar FROM bhost_users WHERE username='$comment_info[username]' AND u_id='$comment_info[author_u_id]'");
    $comment_author_check = mysql_num_rows($comment_author_query);
    $comment_author = @mysql_fetch_assoc($comment_author_query);
    if($comment_author_check == 1) { 
     $commentauthor = "<a href='".url("profile", "", "$comment_info[username]")."'>$comment_info[username]</a>"; 
     if(str_replace(" ", "", $comment_author[avatar]) == "") { $commentauthoravatar = url("/images/", "images/noavatar.gif"); } else { $commentauthoravatar = "$comment_author[avatar]"; }
    } else { 
     if($comment_info[author_u_id] != "0") {
     $commentauthor = "<a href='".url("profile_nouser", "", "")."'>$comment_info[username]</a>";
     } else {
     $commentauthor = $comment_info[username];
     }
     $commentauthoravatar = url("/images/", "images/noavatar.gif");
    }
    

    $this_comment = $comment;
    $comment_edit = "no";
    $comment_delete = "no";
    $comment_ip = "no";
    if($is_admin_logged_in == "yes") {
     $comment_edit = "yes";
     $comment_delete = "yes";
     if($weblog_info[logip_comments] == "1") { $comment_ip = "yes"; } else { $comment_ip = "no"; }
    } elseif((($user_status == "owner" | $user_status == "admin" | $user_status == "site_owner") | ($user_status == "member" & $user_info[u_id] == $entry_info[author_u_id])) & $is_logged_in == "yes") {
     $comment_edit = "yes";
     $comment_delete = "yes";
     if($weblog_info[logip_comments] == "1") { $comment_ip = "yes"; } else { $comment_ip = "no"; }
    } elseif($user_info[u_id] == $comment_author[u_id] & $is_logged_in == "yes") {
     if($weblog_info[manage_comments] == "3") {
      $comment_edit = "yes";
      $comment_delete = "yes";
      $comment_ip = "no";
     } elseif($weblog_info[manage_comments] == "2") {
      $comment_edit = "no";
      $comment_delete = "yes";
      $comment_ip = "no";
     } elseif($weblog_info[manage_comments] == "1") {
      $comment_edit = "yes";
      $comment_delete = "no";
      $comment_ip = "no";
     } else {
      $comment_edit = "no";
      $comment_delete = "no";
      $comment_ip = "no";
     }
    } else {
     $comment_edit = "no";
     $comment_delete = "no";
     $comment_ip = "no";
    }
    if($comment_edit == "yes") {
     $this_comment = str_replace("<CommentEdit>", "<a href='".url("edit_comment", "$weblog_info[weblog]", "", "$entry_info[e_id]", "", "", "&c_id=$comment_info[c_id]")."'>", $this_comment);
     $this_comment = str_replace("</CommentEdit>", "</a>", $this_comment);
    } else {
      $this_comment = str_replace("<CommentEdit>".between("<CommentEdit>", "</CommentEdit>", $this_comment)."</CommentEdit>", "", $this_comment);
    }
    if($comment_delete == "yes") {
     $this_comment = str_replace("<CommentDelete>", "<a href='".url("delete_comment", "$weblog_info[weblog]", "", "$entry_info[e_id]", "", "", "&c_id=$comment_info[c_id]")."'>", $this_comment);
     $this_comment = str_replace("</CommentDelete>", "</a>", $this_comment);
    } else {
      $this_comment = str_replace("<CommentDelete>".between("<CommentDelete>", "</CommentDelete>", $this_comment)."</CommentDelete>", "", $this_comment);
    }
    if($comment_ip == "yes") {
     if($comment_info[IP] == "") { $IP = "Not Logged"; } else { $IP = $comment_info[IP]; }
     $this_comment = str_replace("<%CommentAuthorIP%>", "($IP)", $this_comment);
    } else {
     $this_comment = str_replace("<%CommentAuthorIP%>", "", $this_comment);
    }

   $insert2 .= "<a name='c$comment_info[c_id]'></a>".$this_comment;  
   $search_array4 = Array("<%CommentDate%>", "<%CommentTitle%>", "<%CommentBody%>", "<%CommentAuthor%>", "<%CommentAuthorAvatar%>", "<%CommentTime%>", "<%CommentURL%>");
   $replace_array4 = Array(cdate("$weblog_info[format_date]", timezone($comment_info[date])), "$comment_info[title]", "$comment_info[contents]", "$commentauthor", "$commentauthoravatar", cdate("$weblog_info[format_time]", timezone($comment_info[date])), url("entry", "$weblog_info[weblog]", "", "$entry_info[e_id]", "", "", "#c$comment_info[c_id]"));
   $insert2 = str_replace($search_array4, $replace_array4, $insert2);
   }
  $insert = str_replace("<Comment>".$comment."</Comment>", $insert2, $insert);
  }
}

 if($owner_group_info[allow_trackback] != "0" | $entry_info[trackbacks] != "0") {
  $insert = str_replace("<%EntryTrackbackURL%>", url("trackback", "$weblog_info[weblog]", "", "$entry_info[e_id]"), $insert);
  $insert = str_replace("<%TrackbackCount%>", $numoftrackbacks, $insert);

  $trackback_class = new Trackback("$weblog_info[title]", "$entry_info[display_name]", "UTF-8");
  $trackbackrdf = $trackback_class->rdf_autodiscover(date("r", $entry_info[date]), $entry_info[title], $entry_info[contents], url("entry", "$weblog_info[weblog]", "", "$entry_info[e_id]"), url("trackback", "$weblog_info[weblog]", "", "$entry_info[e_id]"));
  $insert = str_replace("<%TrackbackRDF%>", $trackbackrdf, $insert);

  $trackbackcount = occurrencecount("<Trackback>", "</Trackback>", $insert, "0");
  for($f=0;$f<$trackbackcount;$f++) {
  $insert3 = "";
  $trackback = between("<Trackback>", "</Trackback>", $insert);
  $trackbacks = mysql_query("SELECT * FROM bhost_trackbacks WHERE e_id='$entry_info[e_id]' ORDER BY tb_date");
   while($trackback_info = mysql_fetch_assoc($trackbacks)) {
    $this_trackback = $trackback;
    $trackback_delete = "no";
    if($is_admin_logged_in == "yes") {
     $trackback_delete = "yes";
    } elseif((($user_status == "owner" | $user_status == "admin" | $user_status == "site_owner") | ($user_status == "member" & $user_info[u_id] == $entry_info[author_u_id])) & $is_logged_in == "yes") {
     $trackback_delete = "yes";
    } else {
     $trackback_delete = "no";
    }
    if($trackback_delete == "yes") {
     $this_trackback = str_replace("<TrackbackDelete>", "<a href='".url("delete_trackback", "$weblog_info[weblog]", "", "$entry_info[e_id]", "", "", "&tb_id=$trackback_info[tb_id]")."'>", $this_trackback);
     $this_trackback = str_replace("</TrackbackDelete>", "</a>", $this_trackback);
    } else {
      $this_trackback = str_replace("<TrackbackDelete>".between("<TrackbackDelete>", "</TrackbackDelete>", $this_trackback)."</TrackbackDelete>", "", $this_trackback);
    }

   $insert3 .= "<a name='tb$trackback_info[tb_id]'></a>".$this_trackback;  
   $search_array5 = Array("<%TrackbackBlogName%>", "<%TrackbackURL%>", "<%TrackbackTitle%>", "<%TrackbackExcerpt%>", "<%TrackbackDate%>", "<%TrackbackTime%>");
   $replace_array5 = Array("$trackback_info[tb_blog_name]", "$trackback_info[tb_url]", "$trackback_info[tb_title]", "$trackback_info[tb_excerpt]", cdate("$weblog_info[format_date]", timezone($trackback_info[tb_date])), cdate("$weblog_info[format_time]", timezone($trackback_info[tb_date])));
   $insert3 = str_replace($search_array5, $replace_array5, $insert3);
   }
  $insert = str_replace("<Trackback>".$trackback."</Trackback>", $insert3, $insert);
  }
 }

}
$template = str_replace("<Entry>".$entry."</Entry>", $insert, $template);

}


$search_vars = Array("<IfPages>", "</IfPages>", "<EarlierLink>", "</EarlierLink>", "<LaterLink>", "</LaterLink>", "<RecentEntry>", "</RecentEntry>", "<Entry>", "</Entry>", "<IfCommentsAllowed>", "</IfCommentsAllowed>", "<IfCommentPostAllowed>", "</IfCommentPostAllowed>", "<Comment>", "</Comment>", "<CommentEdit>", "</CommentEdit>", "<CommentDelete>", "</CommentDelete>", "<IfMainPage>", "</IfMainPage>", "<IfEntryPage>", "</IfEntryPage>", "<IfArchivePage>", "</IfArchivePage>", "<IfCategoriesAllowed>", "</IfCategoriesAllowed>", "<IfTrackbacksAllowed>", "</IfTrackbacksAllowed>");
$template = str_replace($search_vars, "", $template);


// OUTPUT WEBLOG
echo $template;
?>