<?
include "header.php";

if(isset($_GET['friend'])) { $friend = $_GET['friend']; $browse = "users"; } else { $friend = ""; }
if(isset($_GET['friendsof'])) { $friendsof = $_GET['friendsof']; $browse = "users"; } else { $friendsof = ""; }
if(isset($_GET['category'])) { $category = str_replace("+", " ", $_GET['category']); $browse = "weblogs"; } else { $category = ""; }
if(isset($_GET['fname'])) { $fname = str_replace("+", " ", $_GET['fname']); $browse = "users"; } else { $fname = ""; }
if(isset($_GET['lname'])) { $lname = str_replace("+", " ", $_GET['lname']); $browse = "users"; } else { $lname = ""; }
if(isset($_GET['display_name'])) { $display_name = str_replace("+", " ", $_GET['display_name']); $browse = "users"; } else { $display_name = ""; }
if(isset($_GET['gender'])) { $gender = str_replace("+", " ", $_GET['gender']); $browse = "users"; } else { $gender = ""; }
if(isset($_GET['birthday'])) { $birthday = str_replace("+", " ", $_GET['birthday']); $browse = "users"; } else { $birthday = ""; }
if(isset($_GET['age'])) { $age = str_replace("+", " ", $_GET['age']); $browse = "users"; } else { $age = ""; }
if(isset($_GET['city'])) { $city = str_replace("+", " ", $_GET['city']); $browse = "users"; } else { $city = ""; }
if(isset($_GET['state'])) { $state = str_replace("+", " ", $_GET['state']); $browse = "users"; } else { $state = ""; }
if(isset($_GET['country'])) { $country = str_replace("+", " ", $_GET['country']); $browse = "users"; } else { $country = ""; }
if(isset($_GET['interest'])) { $interest = str_replace("+", " ", $_GET['interest']); $browse = "users"; } else { $interest = ""; }


if($friend != "") {
 $owner_info = mysql_fetch_assoc(mysql_query("SELECT u_id FROM bhost_users WHERE username='$friend'"));
 $users = mysql_query("SELECT bhost_users.u_id, bhost_users.username, bhost_users.profile_privacy, bhost_users.show_name, bhost_users.display_name, bhost_users.fname, bhost_users.lname, bhost_users.avatar, bhost_users.city, bhost_users.state, bhost_users.country, bhost_users.last_login, bhost_users.g_id FROM bhost_friends LEFT JOIN bhost_users ON bhost_friends.u_id=bhost_users.u_id WHERE bhost_friends.friend_u_id='$owner_info[u_id]' ORDER BY bhost_users.username");
 $words = "$browse22 <b>\"$friend\"</b>";
} elseif($friendsof != "") {
 $owner_info = mysql_fetch_assoc(mysql_query("SELECT u_id FROM bhost_users WHERE username='$friendsof'"));
 $users = mysql_query("SELECT bhost_users.u_id, bhost_users.username, bhost_users.profile_privacy, bhost_users.show_name, bhost_users.display_name, bhost_users.fname, bhost_users.lname, bhost_users.avatar, bhost_users.city, bhost_users.state, bhost_users.country, bhost_users.last_login, bhost_users.g_id FROM bhost_friends LEFT JOIN bhost_users ON bhost_friends.friend_u_id=bhost_users.u_id WHERE bhost_friends.u_id='$owner_info[u_id]' ORDER BY bhost_users.username");
 $words = "$browse21 <b>\"$friendsof\"</b>";
} elseif($category != "") {
 $category_info = mysql_fetch_assoc(mysql_query("SELECT wc_id FROM bhost_categories WHERE name='$category'"));
 $weblogs = mysql_query("SELECT weblog, title FROM bhost_weblogs WHERE wc_id='$category_info[wc_id]'");
 $words = "$browse20 <b>\"$category\"</b>";
} elseif($fname != "" & $admin_info[search_fname] == "2") {
 $users = mysql_query("SELECT u_id, username, profile_privacy, show_name, fname, lname, display_name, avatar, city, state, country, last_login, g_id FROM bhost_users WHERE fname='$fname' AND show_name='1'");
 $words = "$browse5 <b>\"$fname\"</b>";
} elseif($lname != "" & $admin_info[search_lname] == "2") {
 $users = mysql_query("SELECT u_id, username, profile_privacy, show_name, fname, lname, display_name, avatar, city, state, country, last_login, g_id FROM bhost_users WHERE lname='$lname' AND show_name='1'");
 $words = "$browse6 <b>\"$lname\"</b>";
} elseif($display_name != "" & $admin_info[search_display_name] == "2") {
 $users = mysql_query("SELECT u_id, username, profile_privacy, show_name, fname, lname, display_name, avatar, city, state, country, last_login, g_id FROM bhost_users WHERE display_name='$display_name'");
 $words = "$browse7 <b>\"$display_name\"</b>";
} elseif($gender != "" & $admin_info[search_gender] == "2") {
 $users = mysql_query("SELECT u_id, username, profile_privacy, show_name, fname, lname, display_name, avatar, city, state, country, last_login, g_id FROM bhost_users WHERE gender='$gender'");
 $words = "$browse8 <b>\"$gender\"</b>";
} elseif($birthday != "" & $admin_info[search_birthday] == "2") {
 $users = mysql_query("SELECT u_id, username, profile_privacy, show_name, fname, lname, display_name, avatar, city, state, country, last_login, g_id FROM bhost_users WHERE birthday='$birthday'");
 $words = "$browse9 <b>\"".cdate("F j, Y", $birthday)."\"</b>";
} elseif($age != "" & $admin_info[search_birthday] == "2") {
 $current_day = cdate("j", time());
 $current_month = cdate("n", time());
 $current_year = cdate("Y", time());
 $from_year = $current_year-$age-1;
 $to_year = $current_year-$age;
 $from_date = MakeTime("0", "0", "0", "$current_month", "$current_day", "$from_year");
 $to_date = MakeTime("0", "0", "0", "$current_month", "$current_day", "$to_year");
 $users = mysql_query("SELECT u_id, username, profile_privacy, show_name, fname, lname, display_name, avatar, city, state, country, last_login, g_id FROM bhost_users WHERE birthday<='$to_date' AND birthday>'$from_date'");
 $words = "$browse10 <b>\"$age\"</b>";
} elseif($city != "" & $admin_info[search_city] == "2") {
 $users = mysql_query("SELECT u_id, username, profile_privacy, show_name, fname, lname, display_name, avatar, city, state, country, last_login, g_id FROM bhost_users WHERE city='$city'");
 $words = "$browse11 <b>\"$city\"</b>";
} elseif($state != "" & $admin_info[search_state] == "2") {
 $users = mysql_query("SELECT u_id, username, profile_privacy, show_name, fname, lname, display_name, avatar, city, state, country, last_login, g_id FROM bhost_users WHERE state='$state'");
 $words = "$browse12 <b>\"$state\"</b>";
} elseif($country != "" & $admin_info[search_country] == "2") {
 $users = mysql_query("SELECT u_id, username, profile_privacy, show_name, fname, lname, display_name, avatar, city, state, country, last_login, g_id FROM bhost_users WHERE country='$country'");
 $words = "$browse13 <b>\"$country\"</b>";
} elseif($interest != "" & $admin_info[search_interests] == "2") {
 $users = mysql_query("SELECT u_id, username, profile_privacy, show_name, fname, lname, display_name, avatar, city, state, country, last_login, g_id FROM bhost_users WHERE interests LIKE '$interest,%' OR interests LIKE '%,$interest' OR interests LIKE '%,$interest,%' OR interests LIKE '$interest'");
 $words = "$browse14 <b>\"$interest\"</b>";
} else {
 $custom_fields = mysql_query("SHOW COLUMNS FROM bhost_profile FROM `$mysql_database`");
 $custom_field = "";
 $custom_value = "";
 while($custom = mysql_fetch_assoc($custom_fields)) {
  if($custom[Field] != "p_id" & $custom[Field] != "u_id") {
   $custom_field = $custom[Field];
   $custom_value = str_replace("+", " ", $_GET[$custom_field]);
   $custom_search = substr($custom_field, -1);
   if($custom_value != "" & $custom_search == "2") {
    break;
   }
  }
 }
 
 if($custom_value == "") {
  $all_users = mysql_query("SELECT u_id FROM bhost_profile");
  $u_ids = "u_id='0'";
  while($profiles = mysql_fetch_assoc($all_users)) {
   $u_ids .= " OR u_id='$profiles[u_id]'";
  }
  $users = mysql_query("SELECT u_id, username, profile_privacy, show_name, fname, lname, display_name, avatar, city, state, country, last_login, g_id FROM bhost_users WHERE $u_ids");
  $words = "$browse28";
  $new_words = "yes";
 } else {
  $all_users = mysql_query("SELECT u_id FROM bhost_profile WHERE $custom_field LIKE '$custom_value,%' OR $custom_field LIKE '%,$custom_value' OR $custom_field LIKE '%,$custom_value,%' OR $custom_field LIKE '$custom_value'");
  $u_ids = "u_id='0'";
  while($profiles = mysql_fetch_assoc($all_users)) {
   $u_ids .= " OR u_id='$profiles[u_id]'";
  }
  $users = mysql_query("SELECT u_id, username, profile_privacy, show_name, fname, lname, display_name, avatar, city, state, country, last_login, g_id FROM bhost_users WHERE $u_ids");
  $words = "$browse15 $custom_field: <b>\"$custom_value\"</b>";
 }
}





if($browse == "weblogs") {

  $total_weblogs = mysql_num_rows($weblogs);

  if($total_weblogs == 0) { 
  echo $head;
  echo "
  <h2>$browse16</h2>
  $browse17
  ";
  echo $foot;
  exit(); 
  }


  echo $head;
  echo "
  <h2>$browse18 ($total_weblogs)</h2>
  $browse19 $words
  <br><br>
  ";


  // SHOW WEBLOGS

  while($weblog = mysql_fetch_assoc($weblogs)) {
  echo "
  <div style='margin-top: 4px;'><a href='".url("index", "$weblog[weblog]")."'><img src='".url("/images/", "images/user_nogender.gif")."' border='0' style='vertical-align: middle;'></a> <b><a href='".url("index", "$weblog[weblog]")."'>$weblog[title]</a></b><br></div>
  ";
  }

  echo $foot;

} else {

  $total_users = mysql_num_rows($users);

  if($total_users == 0) { 
  echo $head;
  echo "
  <h2>$browse3</h2>
  $browse4
  ";
  echo $foot;
  exit(); 
  }


  echo $head;
  echo "
  <h2>$browse1 ($total_users)</h2>
  "; if($new_words == "yes") { echo "$words"; } else { echo "$browse2 $words"; } echo "
  <br><br>
  ";


  // SHOW USERS
  echo "<table cellspacing='0' cellpadding='0'>";
  while($user = mysql_fetch_assoc($users)) {
  $user_group_info = mysql_fetch_assoc(mysql_query("SELECT allow_pms, allow_profileprivacy FROM bhost_groups WHERE g_id='$user[g_id]'"));
  if($user[show_name] == 1) { $name = "<br>$browse23 $user[fname] $user[lname]"; } else { $name = "<br>$browse23 $user[display_name]"; }

  $avatar = $user[avatar];
  if($avatar == "") { $avatar = url("/images/", "./images/noavatar.gif"); }

  $c = 0;
  $location = array();
  if($user[city] != "" & $admin_info[field_city] != "0") { $location[$c] = $user[city]; $c++; }
  if($user[state] != "" & $admin_info[field_state] != "0") { $location[$c] = $user[state]; $c++; }
  if($user[country] != "" & $admin_info[field_country] != "0") { $location[$c] = $user[country]; $c++; }
  $location = implode($location, ", ");
  if($location != "") { $location = "<br>$browse24 $location"; } else { $location = ""; }

  if($user[last_login] != "" & $user[last_login] != "0") {
  $login_date = cdate("$admin_info[signup_format_date]", timezone($user[last_login]));
  $last_login = "<br>$browse25 $login_date";
  }

  $add_to_friends = "";
  if($is_logged_in == "yes" & $user_info[u_id] != $user[u_id] & $group_info[allow_friends] == 1 & mysql_num_rows(mysql_query("SELECT fr_id, friend_u_id FROM bhost_friends WHERE u_id='$user_info[u_id]' AND friend_u_id='$user[u_id]'")) == 0) {
   $add_to_friends = "<br><a href='".url("./manager/view_friends.php?task=adddyn&new_u=$user[username]", "")."' target='_blank'>$browse26</a>";
  }

  $send_message = "";
  if($is_logged_in == "yes" & $user_info[u_id] != $user[u_id] & $group_info[allow_pms] == 1 & $user_group_info[allow_pms] == 1) {
   $send_message = "<br><a href='".url("./manager/messages.php?task=send&to=$user[username]", "")."' target='_blank'>$browse27</a>";
  }

  $private = "no";
  if($user_group_info[allow_profileprivacy] == "1") {
   if($user[profile_privacy] == "2" & $is_logged_in != "yes" & $is_admin_logged_in != "yes") {
    $private = "yes";
   } elseif($user[profile_privacy] == "1" & $is_admin_logged_in != "yes" & ($is_logged_in != "yes" | mysql_num_rows(mysql_query("SELECT fr_id FROM bhost_friends WHERE u_id='$user[u_id]' AND friend_u_id='$user_info[u_id]'")) == 0) & $user_info[u_id] != $user[u_id]) {
    $private = "yes";
   } elseif($user[profile_privacy] == "0" & $user_info[u_id] != $user[u_id] & $is_admin_logged_in != "yes") {
    $private = "yes";
   }
  }

  echo "
  <tr>
  <td class='result1' align='center'>"; if($private != "yes") { echo "<a href='".url("profile", "", $user[username])."'><img src='$avatar' border='0' class='avatar'></a>"; } else { echo "<img src='$avatar' border='0' class='avatar'>"; } echo "</td>
  <td class='result2' valign='middle'>
  <font class='bold'>"; if($private != "yes") { echo "<a href='".url("profile", "", $user[username])."'>$user[username]</a>"; } else { echo "$user[username]"; } echo "</font>
  $name
  $location
  $last_login
  $add_to_friends
  $send_message
  </td>
  </tr>
  ";
  }
  echo "</table>";

  echo $foot;

}

?>