<?
$page = "members.php";
include "manager_header.php";
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }

if($user_status == "member") { exit(); }

// SET ERRORS
$member_error = "";


if($task == "admindo") {
if(isset($_POST['u_id'])) { $u_id = $_POST['u_id']; } else { header("Location: members.php"); exit(); }
if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE u_id='$u_id'")) == 0) { header("Location: members.php"); exit(); }
$member_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'"));
if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$weblog_info[w_id]' AND (level2 LIKE '%,$member_info[u_id],%')")) == 0) { header("Location: members.php"); exit(); }

$new_level2 = str_replace(",$member_info[u_id],", ",", $weblog_info[level2]);
if($weblog_info[level1] == "") { $new_level1 = ","; } else { $new_level1 = $weblog_info[level1]; }
$new_level1 .= "$member_info[u_id],";

mysql_query("UPDATE bhost_weblogs SET level1='$new_level1', level2='$new_level2' WHERE w_id='$weblog_info[w_id]'");
header("Location: members.php");
exit();
}







if($task == "admin") {
if(isset($_GET['u_id'])) { $u_id = $_GET['u_id']; } else { header("Location: members.php"); exit(); }
if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE u_id='$u_id'")) == 0) { header("Location: members.php"); exit(); }
$member_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'"));
if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$weblog_info[w_id]' AND (level2 LIKE '%,$member_info[u_id],%')")) == 0) { header("Location: members.php"); exit(); }

echo $head;
echo "
$members1
<br><br>

<table cellpadding='0' cellspacing='0'>
<form action='members.php' method='POST'>
<tr>
<td>
<input type='submit' class='button' value='$members2'>&nbsp;
<input type='hidden' name='task' value='admindo'>
<input type='hidden' name='u_id' value='$member_info[u_id]'>
</td>
</form>
<form action='members.php' method='POST'>
<td>
<input type='submit' class='button' value='$members3'>
<input type='hidden' name='task' value='main'>
</td>
</form>
</tr>
</table>
";
echo $foot;
exit();
}






if($task == "unadmindo") {
if(isset($_POST['u_id'])) { $u_id = $_POST['u_id']; } else { header("Location: members.php"); exit(); }
if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE u_id='$u_id'")) == 0) { header("Location: members.php"); exit(); }
$member_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'"));
if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$weblog_info[w_id]' AND (level1 LIKE '%,$member_info[u_id],%')")) == 0) { header("Location: members.php"); exit(); }

$new_level1 = str_replace(",$member_info[u_id],", ",", $weblog_info[level1]);
if($weblog_info[level2] == "") { $new_level2 = ","; } else { $new_level2 = $weblog_info[level2]; }
$new_level2 .= "$member_info[u_id],";

mysql_query("UPDATE bhost_weblogs SET level1='$new_level1', level2='$new_level2' WHERE w_id='$weblog_info[w_id]'");
if($user_info[u_id] == $member_info[u_id]) {
header("Location: view_entries.php");
exit();
} else {
header("Location: members.php");
exit();
}}






if($task == "unadmin") {
if(isset($_GET['u_id'])) { $u_id = $_GET['u_id']; } else { header("Location: members.php"); exit(); }
if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE u_id='$u_id'")) == 0) { header("Location: members.php"); exit(); }
$member_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'"));
if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$weblog_info[w_id]' AND (level1 LIKE '%,$member_info[u_id],%')")) == 0) { header("Location: members.php"); exit(); }

echo $head;
echo "$members4";
if($user_info[u_id] == $member_info[u_id]) {
echo "$members5";
} else {
echo "$members6";
}
echo "
<br><br>
<table cellpadding='0' cellspacing='0'>
<form action='members.php' method='POST'>
<tr>
<td>
<input type='submit' class='button' value='$members2'>&nbsp;
<input type='hidden' name='task' value='unadmindo'>
<input type='hidden' name='u_id' value='$member_info[u_id]'>
</td>
</form>
<form action='members.php' method='POST'>
<td>
<input type='submit' class='button' value='$members3'>
<input type='hidden' name='task' value='main'>
</td>
</tr>
</form>
</table>
";
echo $foot;
exit();
}







if($task == "ownerdo") {
if($weblog_info[u_id] != $user_info[u_id]) { header("Location: members.php"); exit(); }
if(isset($_POST['u_id'])) { $u_id = $_POST['u_id']; } else { header("Location: members.php"); exit(); }
if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE u_id='$u_id'")) == 0) { header("Location: members.php"); exit(); }
$member_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'"));
if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$weblog_info[w_id]' AND (level1 LIKE '%,$member_info[u_id],%' OR level2 LIKE '%,$member_info[u_id],%')")) == 0) { header("Location: members.php"); exit(); }
$member_group_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_groups WHERE g_id='$member_info[g_id]'"));

if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE u_id='$member_info[u_id]'")) >= $member_group_info[num_weblogs]) {
 header("Location: members.php?task=owner&u_id=$u_id");
 exit();
}


$new_level1 = str_replace(",$member_info[u_id],", ",", $weblog_info[level1]);
$new_level2 = str_replace(",$member_info[u_id],", ",", $weblog_info[level2]);
if($weblog_info[level1] == "") { $new_level1 = ","; }
$new_level1 .= "$user_info[u_id],";

mysql_query("UPDATE bhost_weblogs SET u_id='$member_info[u_id]', owner='$member_info[username]', level1='$new_level1', level2='$new_level2' WHERE w_id='$weblog_info[w_id]'");
header("Location: members.php");
exit();
}





if($task == "owner") {
if($weblog_info[u_id] != $user_info[u_id]) { header("Location: members.php"); exit(); }
if(isset($_GET['u_id'])) { $u_id = $_GET['u_id']; } else { header("Location: members.php"); exit(); }
if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE u_id='$u_id'")) == 0) { header("Location: members.php"); exit(); }
$member_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'"));
if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$weblog_info[w_id]' AND (level1 LIKE '%,$member_info[u_id],%' OR level2 LIKE '%,$member_info[u_id],%')")) == 0) { header("Location: members.php"); exit(); }
$member_group_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_groups WHERE g_id='$member_info[g_id]'"));

if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE u_id='$member_info[u_id]'")) >= $member_group_info[num_weblogs]) {
echo $head;
echo "
$members31
<br><br>
<table cellpadding='0' cellspacing='0'>
<form action='members.php' method='POST'>
<tr>
<td>
<input type='submit' class='button' value='$members32'>&nbsp;
<input type='hidden' name='task' value='main'>
</td>
</form>
</tr>
</table>
";
echo $foot;
exit();
}

echo $head;
echo "
$members7
<br><br>

<table cellpadding='0' cellspacing='0'>
<form action='members.php' method='POST'>
<tr>
<td>
<input type='submit' class='button' value='$members2'>&nbsp;
<input type='hidden' name='task' value='ownerdo'>
<input type='hidden' name='u_id' value='$member_info[u_id]'>
</td>
</form>
<form action='members.php' method='POST'>
<td>
<input type='submit' class='button' value='$members3'>
<input type='hidden' name='task' value='main'>
</td>
</tr>
</form>
</table>
";
echo $foot;
exit();
}




if($task == "removedo") {
if(isset($_POST['u_id'])) { $u_id = $_POST['u_id']; } else { header("Location: members.php"); exit(); }
if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE u_id='$u_id'")) == 0) { header("Location: members.php"); exit(); }
$member_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'"));
if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$weblog_info[w_id]' AND (level1 LIKE '%,$member_info[u_id],%' OR level2 LIKE '%,$member_info[u_id],%')")) == 0) { header("Location: members.php"); exit(); }

$new_level1 = str_replace(",$member_info[u_id],", ",", $weblog_info[level1]);
$new_level2 = str_replace(",$member_info[u_id],", ",", $weblog_info[level2]);

mysql_query("UPDATE bhost_weblogs SET level1='$new_level1', level2='$new_level2' WHERE w_id='$weblog_info[w_id]'");
if($user_info[u_id] == $member_info[u_id]) {
header("Location: index.php");
exit();
} else {
header("Location: members.php");
exit();
}}




if($task == "remove") {
if(isset($_GET['u_id'])) { $u_id = $_GET['u_id']; } else { header("Location: members.php"); exit(); }
if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE u_id='$u_id'")) == 0) { header("Location: members.php"); exit(); }
$member_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'"));
if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$weblog_info[w_id]' AND (level1 LIKE '%,$member_info[u_id],%' OR level2 LIKE '%,$member_info[u_id],%')")) == 0) { header("Location: members.php"); exit(); }

echo $head;
echo "$members8";
if($user_info[u_id] == $member_info[u_id]) {
echo "$members18";
} else {
echo "$members19";
}
echo "
<br><br>
<table cellpadding='0' cellspacing='0'>
<form action='members.php' method='POST'>
<tr>
<td>
<input type='submit' class='button' value='$members2'>&nbsp;
<input type='hidden' name='task' value='removedo'>
<input type='hidden' name='u_id' value='$member_info[u_id]'>
</td>
</form>
<form action='members.php' method='POST'>
<td>
<input type='submit' class='button' value='$members3'>
<input type='hidden' name='task' value='main'>
</td>
</tr>
</form>
</table>
";
echo $foot;
exit();
}











if($task == "invitememberdo") {
$member = $_POST['member'];
$user_message = stripslashes($_POST['user_message']);

// ERRORS
if(str_replace(" ", "", $member) == "") { $member_error = "$members22"; }
if(strtolower($member) == strtolower($user_info[username])) { $member_error = "$members23"; }
$member_info = mysql_fetch_assoc(mysql_query("SELECT u_id, g_id, fname, lname, email FROM bhost_users WHERE username='$member'"));
if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$weblog_info[w_id]' AND (level1 LIKE '%,$member_info[u_id],%' OR level2 LIKE '%,$member_info[u_id],%')")) != 0) { $member_error = "$members24"; }
if(mysql_num_rows(mysql_query("SELECT w_id FROM bhost_weblogs WHERE w_id='$weblog_info[w_id]' AND (invited LIKE '%,$member_info[u_id],%')")) != 0) { $member_error = "$members25"; }
$member_group_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_groups WHERE g_id='$member_info[g_id]'"));
if($member_group_info[allow_multiauthor] != "1") { $member_error = "$members26"; }
if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE username='$member'")) == 0) { $member_error = "$members27"; }

// IF NO ERRORS, UPDATE DATABASE AND SEND INVITE
if($member_error == "") {
if($weblog_info[invited] == "") { $new_invited = ","; } else { $new_invited = $weblog_info[invited]; }
$new_invited .= $member_info[u_id].",";
mysql_query("UPDATE bhost_weblogs SET invited='$new_invited' WHERE w_id='$weblog_info[w_id]'");
$weblog_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_weblogs WHERE w_id='$weblog_info[w_id]'"));

if(str_replace(" ", "", $weblog_info[title]) == "") { $weblog_info[title] = $weblog_info[weblog]; }


$subject = $admin_info[invite_subject];
$subject = str_replace("<weblogtitle>", $weblog_info[title], $subject);
$subject = str_replace("<weblogurl>", url("index", "$weblog_info[weblog]"), $subject);
$subject = str_replace("<inviter>", $user_info[username], $subject);
$subject = str_replace("<fname>", $member_info[fname], $subject);
$subject = str_replace("<lname>", $member_info[lname], $subject);
$subject = str_replace("<loginurl>", url("login", "$weblog_info[weblog]"), $subject);
$message = $admin_info[invite_message];
$message = str_replace("<weblogtitle>", $weblog_info[title], $message);
$message = str_replace("<weblogurl>", url("index", "$weblog_info[weblog]"), $message);
$message = str_replace("<inviter>", $user_info[username], $message);
$message = str_replace("<fname>", $member_info[fname], $message);
$message = str_replace("<lname>", $member_info[lname], $message);
$message = str_replace("<loginurl>", url("login", "$weblog_info[weblog]"), $message);

if(str_replace(" ", "", $user_message) != "") { $message = $message."\n\n\n".$members30."\n".$user_message; }

mail("$member_info[email]", "$subject", "$message", "From: $user_info[email]");
header("Location: members.php");
exit();
} else {
$task = "invitemember";
}}


// INVITE MEMBER
if($task == "invitemember") {
if($member_error == "") { $member = ""; $user_message = ""; } else { $member_error .= "<br><br>"; }
echo $head;
echo "$members20
$members21
<br><br>
"; show_error($member_error); echo "
<form action='members.php' method='POST'>
$members28<br>
<input type='text' class='text' name='member' value='$member' size='20' maxlength='30'><br><br>
$members29<br>
<textarea name='user_message' rows='7' cols='60'>$user_message</textarea><br><br>
<table cellspacing='0' cellpadding='0'>
<tr>
<td>
<input type='submit' class='button' value='$members2'>&nbsp;
<input type='hidden' name='task' value='invitememberdo'>
</td>
</form>
<form action='members.php' method='POST'>
<td>
<input type='submit' class='button' value='$members3'>
<input type='hidden' name='task' value='main'>
</form>
</td>
</tr>
</table>
";
echo $foot;
exit();
}





// MAIN SECTION
if($task == "main") {

echo $head;

echo "
$members9
<br><br>

<table cellspacing='0' cellpadding='0' class='memberlist'>
<tr>
<td class='header2' width='150'>$members10</td>
<td class='header2' width='150'>$members11</td>
<td class='header2' NOWRAP>$members12 &nbsp;&nbsp;</td>
<td class='header' width='1'>$members13</td>
<td
</tr>
<tr>
<td class='list2a' NOWRAP><a href='".url("profile", "", "$owner_info[username]")."'>$owner_info[username]</a></td>
<td class='list2a' NOWRAP>$owner_info[fname] $owner_info[lname]</td>
<td class='list2a' NOWRAP><a href='mailto:$owner_info[email]'>$owner_info[email]</a></td>
<td class='list2b' NOWRAP>
<img src='../images/member_checkbox2_d.gif' border='0' class='icon'> <font class='gray'>$members14</font> &nbsp;&nbsp; 
"; if($owner_info[u_id] == $user_info[u_id]) { echo "<img src='../images/member_radio2.gif' border='0' class='icon'> <font class='gray'>$members15</font> &nbsp;&nbsp; "; } echo "
<img src='../images/member_remove_d.gif' border='0' class='icon'> <font class='gray'>$members16</font>
</td>
</tr>
";

$admins = explode(",", trim($weblog_info[level1]));
for($a=0;$a<count($admins);$a++) {
 $admin_query = mysql_query("SELECT * FROM bhost_users WHERE u_id='$admins[$a]'");
 if(mysql_num_rows($admin_query) == 1) { 
  $admin_info = mysql_fetch_assoc($admin_query);
  $admin_group_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_groups WHERE g_id='$admin_info[g_id]'"));
  if($admin_group_info[allow_multiauthor] == "1") {
  echo "
   <tr>
   <td class='list2a' NOWRAP><a href='".url("profile", "", "$admin_info[username]")."'>$admin_info[username]</a></td>
   <td class='list2a' NOWRAP>$admin_info[fname] $admin_info[lname]</td>
   <td class='list2a' NOWRAP><a href='mailto:$admin_info[email]'>$admin_info[email]</a></td>
   <td class='list2b' NOWRAP>
   <a href='members.php?task=unadmin&u_id=$admin_info[u_id]'><img src='../images/member_checkbox2.gif' border='0' class='icon'></a> <a href='members.php?task=unadmin&u_id=$admin_info[u_id]'>$members14</a> &nbsp;&nbsp; 
   "; if($owner_info[u_id] == $user_info[u_id]) { echo "<a href='members.php?task=owner&u_id=$admin_info[u_id]'><img src='../images/member_radio1.gif' border='0' class='icon'></a> <a href='members.php?task=owner&u_id=$admin_info[u_id]'>$members15</a> &nbsp;&nbsp; "; } echo "
   <a href='members.php?task=remove&u_id=$admin_info[u_id]'><img src='../images/member_remove.gif' border='0' class='icon'></a> <a href='members.php?task=remove&u_id=$admin_info[u_id]'>$members16</a>
   </td>
   </tr> 
  ";
  }
 }
}
$members = explode(",", trim($weblog_info[level2]));
for($a=0;$a<count($members);$a++) {
 $member_query = mysql_query("SELECT * FROM bhost_users WHERE u_id='$members[$a]'");
 if(mysql_num_rows($member_query) == 1) { 
  $member_info = mysql_fetch_assoc($member_query);
  $member_group_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_groups WHERE g_id='$member_info[g_id]'"));
  if($member_group_info[allow_multiauthor] == "1") {
  echo "
   <tr>
   <td class='list2a' NOWRAP><a href='".url("profile", "", "$member_info[username]")."'>$member_info[username]</a></td>
   <td class='list2a' NOWRAP>$member_info[fname] $member_info[lname]</td>
   <td class='list2a' NOWRAP><a href='mailto:$member_info[email]'>$member_info[email]</a></td>
   <td class='list2b' NOWRAP>
   <a href='members.php?task=admin&u_id=$member_info[u_id]'><img src='../images/member_checkbox1.gif' border='0' class='icon'></a> <a href='members.php?task=admin&u_id=$member_info[u_id]'>$members14</a> &nbsp;&nbsp; 
   "; if($owner_info[u_id] == $user_info[u_id]) { echo "<a href='members.php?task=owner&u_id=$member_info[u_id]'><img src='../images/member_radio1.gif' border='0' class='icon'></a> <a href='members.php?task=owner&u_id=$member_info[u_id]'>$members15</a> &nbsp;&nbsp; "; } echo "
   <a href='members.php?task=remove&u_id=$member_info[u_id]'><img src='../images/member_remove.gif' border='0' class='icon'></a> <a href='members.php?task=remove&u_id=$member_info[u_id]'>$members16</a>
   </td>
   </tr> 
  ";
  }
 }
}

echo "
</table>

<br>
<form action='members.php' method='POST'>
<input type='submit' class='button' value='$members17'>
<input type='hidden' name='task' value='invitemember'>
</form>
";



if(str_replace(",", "", $weblog_info[invited]) != "") {
echo "
<br>

$members33
<br><br>

<table cellspacing='0' cellpadding='0'>
<tr>
<td class='header2' width='150'>$members10</td>
<td class='header' width='150'>$members11</td>
</tr>
";
$invited = explode(",", trim($weblog_info[invited]));
for($a=0;$a<count($invited);$a++) {
 $invited_query = mysql_query("SELECT * FROM bhost_users WHERE u_id='$invited[$a]'");
 if(mysql_num_rows($invited_query) == 1) { 
  $invited_info = mysql_fetch_assoc($invited_query);
  echo "
   <tr>
   <td class='list2a'><a href='".url("profile", "", "$invited_info[username]")."'>$invited_info[username]</a></td>
   <td class='list2b'>"; if($invited_info[show_name] == "1") { echo "$invited_info[fname] $invited_info[lname]"; } else { echo "$invited_info[display_name]"; } echo "</td>
   </tr> 
  ";
 }
}

echo "</table>";
}

echo $foot;
exit();
}

?>