<?
include "header.php";
if(isset($_GET['t_id'])) { $t_id = $_GET['t_id']; } else { exit(); }
if(mysql_num_rows(mysql_query("SELECT t_id FROM bhost_templates WHERE t_id='$t_id'")) != 1) { exit(); }
$template_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_templates WHERE t_id='$t_id'"));


$template = $template_info[html];


// SET AVATAR
$avatar = "./images/templates/avatar.jpg";

// CHANGE <, >, ", AND STATIC VARIABLES
$search_array = Array("&lt;", "&gt;", "&quot;", "<%WindowTitle%>", "<%Title%>", "<%Description%>", "<%Avatar%>", "<%Email%>", "<%ProfileURL%>", "<%MainPageURL%>", "<%ArchiveURL%>", "<%SiteFeedURL%>", "<%FriendsPageURL%>");
$replace_array = Array("<", ">", "\"", "$example1", "$example1", "$example2", "$avatar", "email@domain.com", "#", "#", "#", "#", "#");
$template = str_replace($search_array, $replace_array, $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);
}

// REMOVE COMMENT EDIT/DELETE/IP ADDRESS
$template = str_replace("<%CommentAuthorIP%>", "", $template);
$template = str_replace("<CommentEdit>".between("<CommentEdit>", "</CommentEdit>", $template)."</CommentEdit>", "", $template);
$template = str_replace("<CommentDelete>".between("<CommentDelete>", "</CommentDelete>", $template)."</CommentDelete>", "", $template);

// REMOVE TRACKBACK DELETE
$template = str_replace("<TrackbackDelete>".between("<TrackbackDelete>", "</TrackbackDelete>", $template)."</TrackbackDelete>", "", $template);


// INPUT MEMBERS
$memberscount = occurrencecount("<Members>", "</Members>", $template, "0");
for($f=0;$f<$memberscount;$f++) {
 $insert = "";
 $members = between("<Members>", "</Members>", $template);
 
 $insert .= $members;
 $insert = str_replace("<%MemberUsername%>", "Member1", $insert);
 $insert = str_replace("<%MemberURL%>", "#", $insert);
 
 $insert .= $members;
 $insert = str_replace("<%MemberUsername%>", "Member2", $insert);
 $insert = str_replace("<%MemberURL%>", "#", $insert);

 $template = str_replace("<Members>".$members."</Members>", $insert, $template);
}


// INPUT FRIENDS
$friendscount = occurrencecount("<Friends>", "</Friends>", $template, "0");
for($f=0;$f<$friendscount;$f++) {
 $insert = "";
 $friends = between("<Friends>", "</Friends>", $template);

 $insert .= $friends;
 $insert = str_replace("<%FriendUsername%>", "Username1", $insert);
 $insert = str_replace("<%FriendURL%>", "#", $insert);

 $insert .= $friends;
 $insert = str_replace("<%FriendUsername%>", "Username2", $insert);
 $insert = str_replace("<%FriendURL%>", "#", $insert);

 $insert .= $friends;
 $insert = str_replace("<%FriendUsername%>", "Username3", $insert);
 $insert = str_replace("<%FriendURL%>", "#", $insert);

 $template = str_replace("<Friends>".$friends."</Friends>", $insert, $template);
}


// INPUT LINKS
$linkscount = occurrencecount("<Link>", "</Link>", $template, "0");
for($f=0;$f<$linkscount;$f++) {
 $insert = "";
 $link = between("<Link>", "</Link>", $template);

 $insert .= $link;
 $insert = str_replace("<%LinkTitle%>", "$example35", $insert);
 $insert = str_replace("<%LinkURL%>", "#", $insert);

 $insert .= $link;
 $insert = str_replace("<%LinkTitle%>", "$example36", $insert);
 $insert = str_replace("<%LinkURL%>", "#", $insert);

 $template = str_replace("<Link>".$link."</Link>", $insert, $template);
}


// INPUT ENTRY CATEGORIES
$categoriescount = occurrencecount("<Categories>", "</Categories>", $template, "0");
for($f=0;$f<$categoriescount;$f++) {
 $insert = "";
 $categories = between("<Categories>", "</Categories>", $template);

 $insert .= $categories;
 $insert = str_replace("<%Category%>", "$example33", $insert);
 $insert = str_replace("<%CategoryURL%>", "#", $insert);

 $insert .= $categories;
 $insert = str_replace("<%Category%>", "$example34", $insert);
 $insert = str_replace("<%CategoryURL%>", "#", $insert);

 $template = str_replace("<Categories>".$categories."</Categories>", $insert, $template);
}


// INPUT RECENT ENTRIES
$recententrycount = occurrencecount("<RecentEntry>", "</RecentEntry>", $template, "0");
for($r=0;$r<$recententrycount;$r++) {
 $insert = "";
 $recententry = between("<RecentEntry>", "</RecentEntry>", $template);

  $insert .= $recententry;
  $insert = str_replace("<%RecentEntryTitle%>", "<a href='#'>$example5</a>", $insert);
  $insert = str_replace("<%RecentEntryDate%>", "<a href='#'>".cdate("$example3", timezone($example7))."</a>", $insert);
  $insert = str_replace("<%RecentEntryTime%>", "<a href='#'>".cdate("$example4", timezone($example7))."</a>", $insert);

  $insert .= $recententry;
  $insert = str_replace("<%RecentEntryTitle%>", "<a href='#'>$example8</a>", $insert);
  $insert = str_replace("<%RecentEntryDate%>", "<a href='#'>".cdate("$example3", timezone($example10))."</a>", $insert);
  $insert = str_replace("<%RecentEntryTime%>", "<a href='#'>".cdate("$example4", timezone($example10))."</a>", $insert);

  $insert .= $recententry;
  $insert = str_replace("<%RecentEntryTitle%>", "<a href='#'>$example11</a>", $insert);
  $insert = str_replace("<%RecentEntryDate%>", "<a href='#'>".cdate("$example3", timezone($example13))."</a>", $insert);
  $insert = str_replace("<%RecentEntryTime%>", "<a href='#'>".cdate("$example4", timezone($example13))."</a>", $insert);

  $insert .= $recententry;
  $insert = str_replace("<%RecentEntryTitle%>", "<a href='#'>$example14</a>", $insert);
  $insert = str_replace("<%RecentEntryDate%>", "<a href='#'>".cdate("$example3", timezone($example16))."</a>", $insert);
  $insert = str_replace("<%RecentEntryTime%>", "<a href='#'>".cdate("$example4", timezone($example16))."</a>", $insert);

  $insert .= $recententry;
  $insert = str_replace("<%RecentEntryTitle%>", "<a href='#'>$example17</a>", $insert);
  $insert = str_replace("<%RecentEntryDate%>", "<a href='#'>".cdate("$example3", timezone($example19))."</a>", $insert);
  $insert = str_replace("<%RecentEntryTime%>", "<a href='#'>".cdate("$example4", timezone($example19))."</a>", $insert);


 $template = str_replace("<RecentEntry>".$recententry."</RecentEntry>", $insert, $template);
}


// INPUT CALENDAR
include "./include/calendar_mod.php";
$time = time(); 
$year = cdate("Y", $time); 
$month = cdate("n", $time);
$days = "";
$pn = array('&laquo;'=>"#", '&raquo;'=>"#");
$template = str_replace("<%Calendar%>", generate_calendar($year, $month, $days, 3, "#", 1, $pn), $template);


// INPUT PAGE VARIABLES
$search_array2 = Array("<LaterLink>", "</LaterLink>", "<EarlierLink>", "</EarlierLink>", "<%CurrentPage%>", "<%TotalPages%>");
$replace_array2 = Array("", "", "<a class='earlierlink' href='#'>", "</a>", "$example37", "$example38");
$template = str_replace($search_array2, $replace_array2, $template);

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

$entry = between("<Entry>", "</Entry>", $template);

  $insert .= "<a name='e$entry_info[e_id]'></a>".$entry;
  $search_array3 = Array("<%EntryDate%>", "<%EntryTitle%>", "<%EntryURL%>", "<%EntryBody%>", "<%EntryAuthor%>", "<%EntryAuthorURL%>", "<%EntryAuthorAvatar%>", "<%EntryTime%>", "<%EntryCategory%>", "<SendThisLink>", "</SendThisLink>");
  $replace_array3 = Array(cdate("$example3", timezone($example7)), "$example5", "#", "$example6", "$example20", "#", "$avatar", cdate("$example4", timezone($example7)), "<a href='#'>$example33</a>", "<a href='#'>", "</a>");
  $insert = str_replace($search_array3, $replace_array3, $insert);

    $insert = str_replace("<%EntryCommentCount%>", "2", $insert);
    $insert = str_replace("<%CommentPostURL%>", "#", $insert);

    $commentcount = occurrencecount("<Comment>", "</Comment>", $insert, "0");
    for($f=0;$f<$commentcount;$f++) {
    $insert2 = "";
    $comment = between("<Comment>", "</Comment>", $insert);

     $insert2 .= "<a name='c$comment_info[c_id]'></a>".$comment;         
     $search_array4 = Array("<%CommentDate%>", "<%CommentTitle%>", "<%CommentBody%>", "<%CommentAuthor%>", "<%CommentAuthorAvatar%>", "<%CommentTime%>", "<%CommentURL%>");
     $replace_array4 = Array(cdate("$example3", timezone($example24)), "$example21", "$example22", "$example23", "./images/noavatar.gif", cdate("$example4", timezone($example24)), "#");
     $insert2 = str_replace($search_array4, $replace_array4, $insert2);

     $insert2 .= "<a name='c$comment_info[c_id]'></a>".$comment;         
     $search_array4 = Array("<%CommentDate%>", "<%CommentTitle%>", "<%CommentBody%>", "<%CommentAuthor%>", "<%CommentAuthorAvatar%>", "<%CommentTime%>", "<%CommentURL%>");
     $replace_array4 = Array(cdate("$example3", timezone($example28)), "$example25", "$example26", "$example27", "./images/noavatar.gif", cdate("$example4", timezone($example28)), "#");
     $insert2 = str_replace($search_array4, $replace_array4, $insert2);

    }     

    $insert = str_replace("<%TrackbackCount%>", "1", $insert);
    $insert = str_replace("<%EntryTrackbackURL%>", "#", $insert);

    $trackbackcount = occurrencecount("<Trackback>", "</Trackback>", $insert, "0");
    for($f=0;$f<$trackbackcount;$f++) {
    $insert3 = "";
    $trackback = between("<Trackback>", "</Trackback>", $insert);

     $insert3 .= "<a name='tb$trackback_info[tb_id]'></a>".$trackback;  
     $search_array5 = Array("<%TrackbackBlogName%>", "<%TrackbackURL%>", "<%TrackbackTitle%>", "<%TrackbackExcerpt%>", "<%TrackbackDate%>", "<%TrackbackTime%>");
     $replace_array5 = Array("$example39", "#", "$example40", "$example41", cdate("$example3", timezone($example42)), cdate("$example4", timezone($example42)));
     $insert3 = str_replace($search_array5, $replace_array5, $insert3);

    $insert = str_replace("<Trackback>".$trackback."</Trackback>", $insert3, $insert);  
    }

  $insert .= "<a name='e$entry_info[e_id]'></a>".$entry;
  $search_array3 = Array("<%EntryDate%>", "<%EntryTitle%>", "<%EntryURL%>", "<%EntryBody%>", "<%EntryAuthor%>", "<%EntryAuthorURL%>", "<%EntryAuthorAvatar%>", "<%EntryTime%>", "<%EntryCategory%>", "<SendThisLink>", "</SendThisLink>");
  $replace_array3 = Array(cdate("$example3", timezone($example10)), "$example8", "#", "$example9", "$example20", "#", "$avatar", cdate("$example4", timezone($example10)), "<a href='#'>$example33</a>", "<a href='#'>", "</a>");
  $insert = str_replace($search_array3, $replace_array3, $insert);

    $insert = str_replace("<%EntryCommentCount%>", "0", $insert);
    $insert = str_replace("<%CommentPostURL%>", "#", $insert);
    $insert = str_replace("<%TrackbackCount%>", "0", $insert);
    $insert = str_replace("<%EntryTrackbackURL%>", "#", $insert);

    $commentcount = occurrencecount("<Comment>", "</Comment>", $insert, "0");
    for($f=0;$f<$commentcount;$f++) {
    $insert = str_replace("<Comment>".between("<Comment>", "</Comment>", $insert)."</Comment>", "", $insert);
    }

    $trackbackcount = occurrencecount("<Trackback>", "</Trackback>", $insert, "0");
    for($f=0;$f<$trackbackcount;$f++) {
    $insert = str_replace("<Trackback>".between("<Trackback>", "</Trackback>", $insert)."</Trackback>", "", $insert);
    }

  $insert .= "<a name='e$entry_info[e_id]'></a>".$entry;
  $search_array3 = Array("<%EntryDate%>", "<%EntryTitle%>", "<%EntryURL%>", "<%EntryBody%>", "<%EntryAuthor%>", "<%EntryAuthorURL%>", "<%EntryAuthorAvatar%>", "<%EntryTime%>", "<%EntryCategory%>", "<SendThisLink>", "</SendThisLink>");
  $replace_array3 = Array(cdate("$example3", timezone($example13)), "$example11", "#", "$example12", "$example20", "#", "$avatar", cdate("$example4", timezone($example13)), "<a href='#'>$example34</a>", "<a href='#'>", "</a>");
  $insert = str_replace($search_array3, $replace_array3, $insert);

    $insert = str_replace("<%EntryCommentCount%>", "1", $insert);
    $insert = str_replace("<%CommentPostURL%>", "#", $insert);
    $insert = str_replace("<%TrackbackCount%>", "0", $insert);
    $insert = str_replace("<%EntryTrackbackURL%>", "#", $insert);

    $commentcount = occurrencecount("<Comment>", "</Comment>", $insert, "0");
    for($f=0;$f<$commentcount;$f++) {
    $insert2 = "";
    $comment = between("<Comment>", "</Comment>", $insert);

     $insert2 .= "<a name='c$comment_info[c_id]'></a>".$comment;         
     $search_array4 = Array("<%CommentDate%>", "<%CommentTitle%>", "<%CommentBody%>", "<%CommentAuthor%>", "<%CommentAuthorAvatar%>", "<%CommentTime%>", "<%CommentURL%>");
     $replace_array4 = Array(cdate("$example3", timezone($example32)), "$example29", "$example30", "$example31", "./images/noavatar.gif", cdate("$example4", timezone($example32)), "#");
     $insert2 = str_replace($search_array4, $replace_array4, $insert2);
     
    $insert = str_replace("<Comment>".$comment."</Comment>", $insert2, $insert);
    }

    $trackbackcount = occurrencecount("<Trackback>", "</Trackback>", $insert, "0");
    for($f=0;$f<$trackbackcount;$f++) {
    $insert = str_replace("<Trackback>".between("<Trackback>", "</Trackback>", $insert)."</Trackback>", "", $insert);
    }

  $insert .= "<a name='e$entry_info[e_id]'></a>".$entry;
  $search_array3 = Array("<%EntryDate%>", "<%EntryTitle%>", "<%EntryURL%>", "<%EntryBody%>", "<%EntryAuthor%>", "<%EntryAuthorURL%>", "<%EntryAuthorAvatar%>", "<%EntryTime%>", "<%EntryCategory%>", "<SendThisLink>", "</SendThisLink>");
  $replace_array3 = Array(cdate("$example3", timezone($example16)), "$example14", "#", "$example15", "$example20", "#", "$avatar", cdate("$example4", timezone($example16)), "<a href='#'>$example34</a>", "<a href='#'>", "</a>");
  $insert = str_replace($search_array3, $replace_array3, $insert);

    $insert = str_replace("<%EntryCommentCount%>", "0", $insert);
    $insert = str_replace("<%CommentPostURL%>", "#", $insert);
    $insert = str_replace("<%TrackbackCount%>", "0", $insert);
    $insert = str_replace("<%EntryTrackbackURL%>", "#", $insert);

    $commentcount = occurrencecount("<Comment>", "</Comment>", $insert, "0");
    for($f=0;$f<$commentcount;$f++) {
    $insert = str_replace("<Comment>".between("<Comment>", "</Comment>", $insert)."</Comment>", "", $insert);
    }

    $trackbackcount = occurrencecount("<Trackback>", "</Trackback>", $insert, "0");
    for($f=0;$f<$trackbackcount;$f++) {
    $insert = str_replace("<Trackback>".between("<Trackback>", "</Trackback>", $insert)."</Trackback>", "", $insert);
    }

  $insert .= "<a name='e$entry_info[e_id]'></a>".$entry;
  $search_array3 = Array("<%EntryDate%>", "<%EntryTitle%>", "<%EntryURL%>", "<%EntryBody%>", "<%EntryAuthor%>", "<%EntryAuthorURL%>", "<%EntryAuthorAvatar%>", "<%EntryTime%>", "<%EntryCategory%>", "<SendThisLink>", "</SendThisLink>");
  $replace_array3 = Array(cdate("$example3", timezone($example19)), "$example17", "#", "$example18", "$example20", "#", "$avatar", cdate("$example4", timezone($example19)), "<a href='#'>$example33</a>", "<a href='#'>", "</a>");
  $insert = str_replace($search_array3, $replace_array3, $insert);

    $insert = str_replace("<%EntryCommentCount%>", "0", $insert);
    $insert = str_replace("<%CommentPostURL%>", "#", $insert);
    $insert = str_replace("<%TrackbackCount%>", "0", $insert);
    $insert = str_replace("<%EntryTrackbackURL%>", "#", $insert);

    $commentcount = occurrencecount("<Comment>", "</Comment>", $insert, "0");
    for($f=0;$f<$commentcount;$f++) {
    $insert = str_replace("<Comment>".between("<Comment>", "</Comment>", $insert)."</Comment>", "", $insert);
    }

    $trackbackcount = occurrencecount("<Trackback>", "</Trackback>", $insert, "0");
    for($f=0;$f<$trackbackcount;$f++) {
    $insert = str_replace("<Trackback>".between("<Trackback>", "</Trackback>", $insert)."</Trackback>", "", $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>", "<%TrackbackRDF%>", "<IfTrackbacksAllowed>", "</IfTrackbacksAllowed>");
$template = str_replace($search_vars, "", $template);

// OUTPUT WEBLOG
echo $template;

?>