<?

include "mysqlcon.php";

$n = basename($_SERVER['PHP_SELF']);
$admin_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_admin"));

include "functions.php";

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




// SET HTML HEADER AND FOOTER
$head = "
$admin_info[html_external_header]
<div class='content_bottom'>
";

$foot = "
</div>
$admin_info[html_external_footer]
";




// SET HOMEPAGE
if($admin_info[homepage] == "") { $homepage = "login.php"; } else { $homepage = $admin_info[homepage]; }


if($n != "login.php" & $n != "signup.php" & $n != "forgot.php" & $n != "verify.php" & $n != "tip.php" & $n != "example.php") {
if($n != "browse.php") {
if($n == "profile.php" | $n == "album.php") {
if(isset($_POST['u'])) { $u = $_POST['u']; } elseif(isset($_GET['u'])) { $u = $_GET['u']; } else { $u = ""; }
$q = mysql_query("SELECT * FROM bhost_users WHERE username='$u'");
if(mysql_num_rows($q) == 0 & $u != "") { header("Location: $homepage"); exit(); }
$owner_info = @mysql_fetch_assoc($q);
$owner_group_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_groups WHERE g_id='$owner_info[g_id]'"));
} else {
// FOR BACKWARDS COMPATIBILITY:
if(isset($_POST['u'])) { $_POST['w'] = $_POST['u']; } elseif(isset($_GET['u'])) { $_GET['w'] = $_GET['u']; }
if(isset($_POST['w'])) { $w = $_POST['w']; } elseif(isset($_GET['w'])) { $w = $_GET['w']; } else { header("Location: $homepage"); exit(); } 
$q = mysql_query("SELECT * FROM bhost_weblogs WHERE weblog='$w'");
if(mysql_num_rows($q) == 0) { header("Location: $homepage"); exit(); }
$weblog_info = mysql_fetch_assoc($q);
$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]'"));
}




// CHECK IF USER BANNED IP ADDRESS
$banned_ips = $weblog_info['banned_ips'];
if($banned_ips != "") {
$myip = $_SERVER['REMOTE_ADDR'];
$banned_ip = explode(",", $banned_ips);
$total = count($banned_ip);
for($c=0;$c<$total;$c++) {
$banned = str_replace("*", "[0-9]{1,3}", $banned_ip[$c]);
$banned = str_replace(".", "\.", $banned);

if(preg_match("/\b$banned\b/", $myip)) {
echo $head;
echo "$header15";
echo $foot;
exit();
}
}
}
}

// DEFAULT VALUES FOR VARIABLES
$is_admin_logged_in = "no";
$is_logged_in = "no";
$is_friend = "no";
$user_status = "guest";

if(isset($_COOKIE['admin_username']) & isset($_COOKIE['admin_password'])) {
  if(stripslashes($_COOKIE['admin_username']) != crypt($admin_info[username], "admin") | stripslashes($_COOKIE['admin_password']) != crypt($admin_info[password], "admin")) {
   $is_logged_in = "no";
  } else {
   $is_logged_in = "yes";
   $is_admin_logged_in = "yes";
   $user_info[username] = "<b>Administrator</b>";
   $user_info[u_id] = 0;
  }
} elseif(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]) {
  $is_logged_in = "yes";
  update_last_activity();
 } else {
  $is_logged_in = "no";
 }
} else {
 $is_logged_in = "no";
}
}

// CHECK USER STATUS AT THE WEBLOG
if($is_logged_in == "yes") {
 if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$weblog_info[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='$weblog_info[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='$weblog_info[w_id]' AND level2 LIKE '%,$user_info[u_id],%'")) == 1) {
   $user_status = "member";
 } else {
   $user_status = "guest";
   $where = "u_id = '$owner_info[u_id]'";
   if($weblog_info[friends] == "1") {
    $admins = explode(",", trim($weblog_info[level1]));
    for($a=0;$a<count($admins);$a++) {
     if($admins[$a] != "") { $where .= " OR u_id='$admins[$a]'"; }
    }
   } elseif($weblog_info[friends] == "2") {
    $admins = explode(",", trim($weblog_info[level1]));
    for($a=0;$a<count($admins);$a++) {
     if($admins[$a] != "") { $where .= " OR u_id='$admins[$a]'"; }
    }
    $members = explode(",", trim($weblog_info[level2]));
    for($a=0;$a<count($members);$a++) {
     if($members[$a] != "") { $where .= " OR u_id='$members[$a]'"; }
    }
   }
   if(mysql_num_rows(mysql_query("SELECT fr_id FROM bhost_friends WHERE ($where) AND friend_u_id='$user_info[u_id]'")) != 0) { $is_friend = "yes"; }
 }
 if($is_admin_logged_in == "yes") { $user_status = "site_owner"; }
} else {
 $user_status = "guest";
}

// INCLUDE LANGUAGE FILE
if($is_logged_in == "no" | $is_admin_logged_in == "yes") {
 include "./lang/$admin_info[language]";
} else {
 $group_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_groups WHERE g_id='$user_info[g_id]'"));
 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 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);
for($c=0;$c<$total;$c++) {

$banned = str_replace("*", "[0-9]{1,3}", $banned_ip[$c]);
$banned = str_replace(".", "\.", $banned);

if(preg_match("/\b$banned\b/", $myip)) {
echo $head;
echo "$header15";
echo $foot;
exit();
}
}
}


// CHECK IF WEBLOG IS DISABLED
if($w != "" AND $weblog_info[enabled] == 0 AND $weblog_info[w_id] != "") {
echo "
$admin_info[html_header]
<div class='content_bottom'>
$header18
<br><br>
<form action='login.php' method='POST'>
<input type='submit' class='button' value='$header19'>
</form>
</div>
$admin_info[html_footer]
";
exit();
}




?>