<?
if($previewpage == "preview" | $previewpage == "previewentry") {
include "./mysqlcon.php";
include "./functions.php";
$admin_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_admin"));
} else {
include "../mysqlcon.php";
include "../functions.php";
$admin_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_admin"));
}




// CHECK IF DEBUG MODE IS ON
if($admin_info[debug] == 0) {
error_reporting(0);
} else {
error_reporting(E_ALL ^ E_NOTICE);
}




// CHECK IF IP ADDRESS IS BANNED
$banned_ips = $admin_info['banned_ips_access'];
if($banned_ips != "") {
$myip = $_SERVER['REMOTE_ADDR'];
$banned_ip = explode(",", $banned_ips);
$total = count($banned_ip);
$count = 0;
while($count < $total) {
$set = explode(".", $banned_ip[$count]);
$myset = explode(".", $myip);
$c = 0;
$match = "";
while($c < 4) {
if($set[$c] == "*") { $match[$c] = 1; } else { if($set[$c] == $myset[$c]) { $match[$c] = 1; }}
$c++;
}
if($match[0] == 1 AND $match[1] == 1 AND $match[2] == 1 AND $match[3] == 1) {
header("Location: ../login.php");
exit();
}
$count++;
}
}



$n = basename($_SERVER['PHP_SELF']);

if($n != "tip.php") {

  if(isset($_COOKIE['username']) & isset($_COOKIE['password']) & isset($_COOKIE['u_id'])) {
  $u_id = $_COOKIE['u_id'];
  $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'"));
    if(stripslashes($_COOKIE['username']) != crypt($user_info[username], $user_info[code]) | stripslashes($_COOKIE['password']) != $user_info[password]) {
    header("Location: ../login.php");
    exit;
    }
  } else {
    header("Location: ../login.php");
    exit;
  }

  update_last_activity();
}

// DEFINE USER GROUP
$group_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_groups WHERE g_id='$user_info[g_id]'"));


// WEBLOG INFO
if($n != "changepass.php" & $n != "edit_profile.php" & $n != "index.php" & $n != "create_weblog.php" & $n != "delete_weblog.php" & $n != "support.php" & $n != "support_email.php" & $n != "support_faq.php" & $n != "support_guide.php" & $n != "uploads.php" & $n != "view_friends.php" & $n != "weblog_cookies.php" & $n != "search.php" & $n != "account.php" & $n != "messages.php") {
 if(isset($_COOKIE['w_id'])) {
  $w_id = $_COOKIE['w_id'];
  $weblog = mysql_query("SELECT * FROM bhost_weblogs WHERE w_id='$w_id'");
  if(mysql_num_rows($weblog) != 1) {
   header("Location: index.php");
   exit();
  } else {
   $weblog_info = mysql_fetch_assoc($weblog);
   $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(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$w_id' AND u_id='$user_info[u_id]'")) == 1) {
     $user_status = "owner";
   } elseif(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$w_id' AND level1 LIKE '%,$user_info[u_id],%'")) == 1) {
     $user_status = "admin";
   } elseif(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$w_id' AND level2 LIKE '%,$user_info[u_id],%'")) == 1) {
     $user_status = "member";
   }
  }
 } else {
  header("Location: index.php");
  exit();
 }
}




// INCLUDE LANGUAGE FILE
if($previewpage == "preview" | $previewpage == "previewentry") {
 if($group_info[allow_language] == "1" & $user_info[language] != "" & file_exists("./lang/$user_info[language]")) {
  include "./lang/$user_info[language]";
 } else {
  include "./lang/$admin_info[language]";
 }
} else {
 if($group_info[allow_language] == "1" & $user_info[language] != "" & file_exists("../lang/$user_info[language]")) {
  include "../lang/$user_info[language]";
 } else {
  include "../lang/$admin_info[language]";
 }
}


// CHECK MOBLOGGING
if($owner_group_info[allow_moblogging] == "1" & $weblog_info[moblog_on] == "1") {
check_moblog($weblog_info[w_id]);
}




// CHECK IF ACCOUNT IS DISABLED
if($user_info[enabled] == 0) {
echo "
$admin_info[html_external_header]
<div class='content_bottom'>
$manager_header18
<br><br>
<form action='../login.php' method='POST'>
<input type='submit' class='button' value='$manager_header19'>
</form>
</div>
$admin_info[html_external_footer]
";
exit();
}



// IF USER NEEDS TO PAY, SEND THEM TO THE PAYMENT PAGE
if($n != "account.php" & $n != "support.php" & $n != "support_guide.php" & $n != "support_faq.php" & $n != "support_email.php") {
 if($group_info[type] != 0) {
  $today = MakeTime("0", "0", "0", date("n", time()), date("j", time()), date("Y", time()));
  switch($group_info[frequency]) {
   case "":
    $check_last_payment = $user_info[last_payment]+1;
    break;
   case "once":
    $check_last_payment = 0;
    break;
   case "weekly":
    $check_last_payment = $today-(7*24*60*60);
    break;
   case "monthly":
     if(date("n", $today) == "3" & (date("j", $today) == "31" | date("j", $today) == "30" | date("j", $today) == "29")) {
      if(date("L", $today) == "1") { $day = 29; } else { $day = 28; }
      $check_last_payment = MakeTime("0", "0", "0", date("n", time())-1, $day, date("Y", time()));
     } elseif(date("j", $today) == 31 & (date("n", $today) == "5" | date("n", $today) == "7" | date("n", $today) == "10" | date("n", $today) == "12")) {
      $check_last_payment = MakeTime("0", "0", "0", date("n", time())-1, 30, date("Y", time()));
     } else {
      $check_last_payment = MakeTime("0", "0", "0", date("n", time())-1, date("j", time()), date("Y", time()));
     }
    break;
   case "yearly":
    $check_last_payment = MakeTime("0", "0", "0", date("n", time()), date("j", time()), date("Y", time())-1);
    break;
  }

  if($user_info[last_payment] <= $check_last_payment) { header("Location: account.php"); exit(); }
 }
}

// CHECKS FOR SEARCH PAGE
if($admin_info[search_username] == "0" & $admin_info[search_email] == "0" & $admin_info[search_fname] == "0" & $admin_info[search_lname] == "0" & $admin_info[search_display_name] == "0" & $admin_info[search_website] == "0" & $admin_info[search_screenname] == "0" & $admin_info[search_gender] == "0" & $admin_info[search_birthday] == "0" & $admin_info[search_city] == "0" & $admin_info[search_state] == "0" & $admin_info[search_country] == "0" & $admin_info[search_interests] == "0") {
 $show_search = "no";

 $custom_fields = mysql_query("SHOW COLUMNS FROM bhost_profile FROM `$mysql_database`");
 while($custom = mysql_fetch_assoc($custom_fields)) {
  if($custom[Field] != "p_id" & $custom[Field] != "u_id") {
   $custom_search = substr($custom[Field], -1);
   if($custom_search != "0") {
    $show_search = "yes";
   }
  }
 }
} else {
 $show_search = "yes";
} 







// CREATE MENU
$sep = " &nbsp;&nbsp;&nbsp;&nbsp; ";
$tab1 = 1;
$tab2 = 1;
$tab3 = 1;
$tab4 = 1;

switch($page) {
case "view_entries.php":
case "edit_entry.php":
case "delete_entry.php":
case "add_entry.php":
case "ping.php":
case "entrycat.php":
case "mobile_settings.php":
$tab1 = "2";
$submenu = "
<a href='view_entries.php' class='menu'>$manager_header2</a>
 $sep <a href='add_entry.php' class='menu'>$manager_header1</a>
"; if($owner_group_info[allow_entry_cat] == "1" AND $user_status != "member") { $submenu .= " $sep <a href='entrycat.php' class='menu'>$manager_header3</a>"; } $submenu .= "
"; if($owner_group_info[allow_pinging] == "1") { $submenu .= " $sep <a href='ping.php' class='menu'>$manager_header4</a> "; } $submenu .= "
"; if($owner_group_info[allow_moblogging] == "1" & $user_status != "member") { $submenu .= " $sep <a href='mobile_settings.php' class='menu'>$manager_header33</a> "; } $submenu .= "
";
break;

case "choose_template.php":
case "edit_template.php":
if($user_status != "member") {
$tab2 = "2";
$submenu = "
"; if($owner_group_info[allow_edittemplate] == "1") { $submenu .= " <a href='edit_template.php' class='menu'>$manager_header5</a> $sep "; } $submenu .= "
"; if($owner_group_info[templates] != "") { $submenu .= "<a href='choose_template.php' class='menu'>$manager_header6</a> "; if($owner_group_info[allow_edittemplate] == "1") { $submenu .= " $sep "; }} $submenu .= "
"; if($owner_group_info[allow_edittemplate] == "1") { $submenu .= " <a href='support_guide.php' class='menu'>$manager_header7</a>"; } $submenu .= "
";
}
break;

case "settings.php":
case "members.php":
case "comments.php":
case "trackback_settings.php":
case "sitefeed.php":
case "podcast_settings.php":
case "links.php":
if($user_status != "member") {
$tab3 = "2";
$submenu = "
<a href='settings.php' class='menu'>$manager_header30</a>
"; if($owner_group_info[allow_comments] == "1") { $submenu .= " $sep <a href='comments.php' class='menu'>$manager_header8</a> "; } $submenu .= "
"; if($owner_group_info[allow_trackback] == "1") { $submenu .= " $sep <a href='trackback_settings.php' class='menu'>$manager_header9</a> "; } $submenu .= "
 $sep <a href='links.php' class='menu'>$manager_header29</a>
"; if($owner_group_info[allow_multiauthor] == "1") { $submenu .= " $sep <a href='members.php' class='menu'>$manager_header28</a> "; } $submenu .= "
"; if($owner_group_info[allow_rss] == "1") { $submenu .= " $sep <a href='sitefeed.php' class='menu'>$manager_header10</a> "; } $submenu .= "
"; if($owner_group_info[allow_podcast] == "1") { $submenu .= " $sep <a href='podcast_settings.php' class='menu'>$manager_header32</a> "; } $submenu .= "
";
}
break;

case "index.php":
case "uploads.php":
case "support.php":
case "support_email.php":
case "support_guide.php":
case "support_faq.php":
case "create_weblog.php":
case "delete_weblog.php":
case "edit_profile.php":
case "view_friends.php":
case "search.php":
case "account.php":
case "messages.php":

// GET UNREAD MESSAGES
if($group_info[allow_pms] == 1) {
$get_pms = mysql_query("SELECT pm_id, from_username FROM bhost_pms WHERE to_u_id='$user_info[u_id]' AND viewed='0'");
$pms = mysql_num_rows($get_pms);
if($pms == 1) { $pm_info = mysql_fetch_assoc($get_pms); $pm_from = $pm_info[from_username]; $pm_id = $pm_info[pm_id]; }
if($pms != 0) { $pms_text = "&nbsp;($pms)"; } else { $pms_text = ""; }
}


$tab1 = "2";
$mainmenu = 1;
$submenu = "
<td class='tab' NOWRAP><a href='index.php' class='menu'>$manager_header26</a></td>
<td class='tab' NOWRAP><a href='edit_profile.php' class='menu'>$manager_header12</a></td>
"; if($group_info[allow_friends] == 1) { $submenu .= "<td class='tab' NOWRAP><a href='view_friends.php' class='menu'>$manager_header13</a></td>"; } $submenu .= "
"; if($group_info[allow_album] == 1 OR $group_info[allow_podcast] == 1 OR $group_info[allow_uploads] == 1) { $submenu .= "<td class='tab' NOWRAP><a href='uploads.php' class='menu'>$manager_header14</a></td>"; } $submenu .= "
"; if($group_info[allow_pms] == 1) { $submenu .= " <td class='tab'><a href='messages.php' class='menu' NOWRAP>$manager_header31$pms_text</a></td>"; } $submenu .= "
"; if($show_search == "yes") { $submenu .= " <td class='tab'><a href='search.php' class='menu' NOWRAP>$manager_header27</a></td>"; } $submenu .= "
";
break;

default:
$mainmenu = 0;
break;
}





// GET UPGRADE/DOWNGRADE OPTIONS
if($admin_info[allow_upgrades] == 1 OR $admin_info[allow_downgrades] == 1) {
$upgrades = mysql_query("SELECT * FROM bhost_groups WHERE g_id <> '$group_info[g_id]' AND order_group > '$group_info[order_group]' AND order_group <> '0'");
$downgrades = mysql_query("SELECT * FROM bhost_groups WHERE g_id <> '$group_info[g_id]' AND order_group <= '$group_info[order_group]' AND order_group <> '0'");
$upgrades_total = mysql_num_rows($upgrades);
$downgrades_total = mysql_num_rows($downgrades);
$total_options = $upgrades_total + $downgrades_total;

if($total_options > 0) {
 if($upgrades_total > 0 AND $admin_info[allow_upgrades] == 1) {
  $option = "&uarr;<a href='account.php?task=upgrade' class='sidemenu'>$manager_header34</a>$sep";
 } elseif($downgrades_total > 0 AND $admin_info[allow_downgrades] == 1) {
  $option = "&uarr;<a href='account.php?task=upgrade' class='sidemenu'>$manager_header35</a>$sep";
 } else {
  $option = "";
 }
}
}

// DECIDE WHETHER OR NOT TO SHOW BACK TO MY ACCOUNT PAGE
if(!isset($mainmenu)) {
$back_to_account = "<font class='bold'>&#171;</font> <a href='index.php' class='sidemenu'>$manager_header11</a>$sep";
}

// SET LOGOUT, RETURN TO BLOG MANAGER, UPGRADE, AND HELP LINKS
$other_links = "
$back_to_account
$option
<font class='bold'>?</font> <a href='support.php' class='sidemenu'>$manager_header23</a>$sep
<font class='bold'>X</font> <a href='logout.php' class='sidemenu'>$manager_header16</a>
";








// SET HEADER
$head = "
$admin_info[html_internal_header]
";

if(!isset($mainmenu)) {
$head .= "
<table cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td class='title'>
<a href='index.php' class='title'>$user_info[username]</a> 
<font class='title_editing'>($manager_header36 <a href='view_entries.php' class='title'>$weblog_info[weblog]</a>)</font>
</td>
</tr>
</table>

<table cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td class='tab$tab1' NOWRAP><a href='view_entries.php' class='menu'>$manager_header17</a></td>
";
if($user_status != "member") {
if($owner_group_info[allow_edittemplate] == "1" OR $owner_group_info[templates] != "") {
$head .= "<td class='tab$tab2' NOWRAP><a href='"; if($owner_group_info[allow_edittemplate] == "1") { $head .= "edit_template.php"; } else { $head .= "choose_template.php"; } $head .= "' class='menu'>$manager_header20</a></td>";
}
$head .= "<td class='tab$tab3' NOWRAP><a href='settings.php' class='menu'>$manager_header21</a></td>";
}
$head .= "
<td class='tab1' NOWRAP><a href='".url("index", "$weblog_info[weblog]")."' target='_blank' class='menu'>$manager_header22</a></td>
<td class='menu_rightside1' width='100%' NOWRAP>$other_links</td>
</tr>
</table>
";

$head .= "
<table cellpadding='0' cellspacing='0' width='100%'>
<tr><td class='submenu' NOWRAP>$submenu</td></tr>
</table>
<div class='shadow'>&nbsp;</div>
";



} else {





$head .= "
<table cellpadding='0' cellspacing='0' width='100%'>
<tr><td class='title'><a href='index.php' class='title'>$user_info[username]</a></td></tr>
</table>
<table cellpadding='0' cellspacing='0' width='100%'>
<tr>
$submenu
<td class='menu_rightside' width='100%' NOWRAP>$other_links</td>
</tr>
</table>
<div class='shadow'>&nbsp;</div>
";
}



$head .= "
<div class='content_bottom'>
";








// SET WEBLOG MANAGER FOOTER
$foot = "
</div>
$admin_info[html_internal_footer]
";






?>