<?


// START TIME
function microtime_float()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();



$page = "search.php";
include "manager_header.php";

if(isset($_GET['task'])) { $task = $_GET['task']; } elseif(isset($_POST['task'])) { $task = $_POST['task']; } else { $task = "main"; }
if(isset($_GET['page'])) { $page = $_GET['page']; } elseif(isset($_POST['page'])) { $page = $_POST['page']; } else { $page = ""; }
if($show_search != "yes") { header("Location: index.php"); exit(); }

$users_per_page = 20;


// SET ERROR VARS
$is_error = "no";
$error = "";


if($task == "searchdo") {
$is_error = "yes";

if($admin_info[search_username] != 0) { if(isset($_POST['username'])) { $username = $_POST['username']; } else { $username = $_GET['username']; } } else { $username = ""; }
if($admin_info[search_fname] != 0 | $admin_info[search_lname] != 0 | $admin_info[search_display_name] != 0) { if(isset($_POST['name'])) { $name = $_POST['name']; } else { $name = $_GET['name']; } } else { $name = ""; }
if($admin_info[search_email] != 0) { if(isset($_POST['email'])) { $email = $_POST['email']; } else { $email = $_GET['email']; } } else { $email = ""; }
if($admin_info[search_gender] != 0) { if(isset($_POST['gender'])) { $gender = $_POST['gender']; } else { $gender = $_GET['gender']; } } else { $gender = ""; }
if($admin_info[search_city] != 0 | $admin_info[search_state] != 0 | $admin_info[search_country] != 0) { if(isset($_POST['location'])) { $location = $_POST['location']; } else { $location = $_GET['location']; } } else { $location = ""; }
if($admin_info[search_birthday] != 0) { if(isset($_POST['age1']) & isset($_POST['age2'])) { $age1 = $_POST['age1']; $age2 = $_POST['age2']; } else { $age1 = $_GET['age1']; $age2 = $_GET['age2']; } } else { $age1 = ""; $age2 = ""; }
if($admin_info[search_website] != 0) { if(isset($_POST['website'])) { $website = $_POST['website']; } else { $website = $_GET['website']; } } else { $website = ""; }
if($admin_info[search_screenname] != 0) { if(isset($_POST['screenname'])) { $screenname = $_POST['screenname']; } else { $screenname = $_GET['screenname']; } } else { $screenname = ""; }
if($admin_info[search_interests] != 0) { if(isset($_POST['interest'])) { $interest = $_POST['interest']; } else { $interest = $_GET['interest']; } } else { $interest = ""; }


$search_string = "";
$url = "";
if(str_replace(" ", "", $username) != "") { $search_string .= "bhost_users.username LIKE '%$username%'"; $is_error = "no"; $url .= "&username=".urlencode($username); }
if(str_replace(" ", "", $name) != "") { 
  if($search_string != "") { $search_string .= " AND "; } 
  if($admin_info[search_display_name] != 0) { $display_name_query = "(bhost_users.show_name<>'1' AND bhost_users.display_name LIKE '%$name%')"; }
  if($admin_info[search_fname] != 0) { if($display_name_query != "") { $conjunction = "OR"; } $fname_query = "bhost_users.fname LIKE '%$name%'"; }
  if($admin_info[search_lname] != 0) { if($display_name_query != "") { $conjunction = "OR"; } $lname_query = "bhost_users.lname LIKE '%$name%'"; }
  if($fname_query != "" | $lname_query != "") { $name_query = "bhost_users.show_name='1' AND "; }
  if($fname_query != "" & $lname_query != "") { $conjunction2 = "OR"; }
  $search_string .= "($display_name_query $conjunction ($name_query($fname_query $conjunction2 $lname_query)))"; 
  $is_error = "no"; 
  $url .= "&name=".urlencode($name);
}
if(str_replace(" ", "", $email) != "") { if($search_string != "") { $search_string .= " AND "; } $search_string .= "bhost_users.email LIKE '%$email%'"; $is_error = "no"; $url .= "&email=".urlencode($email); }
if(str_replace(" ", "", $gender) != "") { if($search_string != "") { $search_string .= " AND "; } $search_string .= "bhost_users.gender='$gender'"; $is_error = "no"; $url .= "&gender=".urlencode($gender); }
if(str_replace(" ", "", $location) != "") { 
  if($search_string != "") { $search_string .= " AND "; } 
  if($admin_info[search_city] != 0) { $city_query = "bhost_users.city LIKE '%$location%'"; } else { $city_query = ""; }
  if($admin_info[search_state] != 0) { if($city_query != "") { $state_query = " OR "; } $state_query .= "bhost_users.state LIKE '%$location%'"; } else { $state_query = ""; }
  if($admin_info[search_country] != 0) { if($city_query != "" | $state_query != "") { $country_query = " OR "; } $country_query .= "bhost_users.country LIKE '%$location%'"; }
  $search_string .= "($city_query $state_query $country_query)"; 
  $is_error = "no"; 
  $url .= "&location=".urlencode($location);
}
if(str_replace(" ", "", $age1) != "" & str_replace(" ", "", $age2) != "") { 
  if($age2 < $age1) { $age3 = $age1; $age1 = $age2; $age2 = $age3; }
  if($search_string != "") { $search_string .= " AND "; }
   $current_day = cdate("j", time());
   $current_month = cdate("n", time());
   $current_year = cdate("Y", time());
   $from_year = $current_year-$age1;
   $to_year = $current_year-$age2;
   $from_date = MakeTime("0", "0", "0", "$current_month", "$current_day", "$from_year");
   $to_date = MakeTime("0", "0", "0", "$current_month", "$current_day", "$to_year");
   $search_string .= "bhost_users.birthday<='$from_date' AND bhost_users.birthday>'$to_date'";
   $is_error = "no";
   $url .= "&age1=".urlencode($age1)."&age2=".urlencode($age2);
}
if(str_replace(" ", "", $website) != "") { if($search_string != "") { $search_string .= " AND "; } $search_string .= "bhost_users.website LIKE '%$website%'"; $is_error = "no"; $url .= "&website=".urlencode($website); }
if(str_replace(" ", "", $screenname) != "") { if($search_string != "") { $search_string .= " AND "; } $search_string .= "bhost_users.screenname LIKE '%$screenname%'"; $is_error = "no"; $url .= "&screenname=".urlencode($screenname); }
if(str_replace(" ", "", $interest) != "") { if($search_string != "") { $search_string .= " AND "; } $search_string .= "bhost_users.interests LIKE '%$interest%'"; $is_error = "no"; $url .= "&interest=".urlencode($interest); }

$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_field = $custom[Field];
  if(isset($_POST[$custom_field])) { $custom_value = $_POST[$custom_field]; } else { $custom_value = $_GET[$custom_field]; }
  $custom_search = substr($custom[Field], -1);
  $custom_name = str_replace("_", " ", substr($custom[Field], 0, -3));
  if($custom_search != "0" & str_replace(" ", "", $custom_value) != "") {
   if($search_string != "") { $search_string .= " AND "; }
   $search_string .= "bhost_profile.$custom_field LIKE '%$custom_value%'";
   ${"$custom_field"} = $custom_value;
   $is_error = "no";
   $url .= "&$custom_field=".urlencode($custom_value);
  }
 }
}


if((str_replace(" ", "", $age1) == "" & str_replace(" ", "", $age2) != "") | (str_replace(" ", "", $age1) != "" & str_replace(" ", "", $age2) == "")) { $is_error = "yes"; $error = $search1; }

 if($search_string != "") { 
  $users = mysql_query("SELECT bhost_users.u_id, bhost_users.username FROM bhost_users LEFT JOIN bhost_profile ON bhost_users.u_id=bhost_profile.u_id WHERE $search_string ORDER BY bhost_users.username");
  $total_users = mysql_num_rows($users);
  if($total_users == 0) { $is_error = "yes"; $error = "$search29"; }
 }

if($is_error != "yes") {

 // NO USERS FOUND!
 if($total_users == 0) {
 $is_error = "yes";
 $error = $search2;
 }

 if(($total_users % $users_per_page) != 0) {
 $maxpage = ($total_users) / $users_per_page + 1;
 } else {
 $maxpage = ($total_users) / $users_per_page;
 }
 $maxpage = (int) $maxpage;
 if($page > $maxpage) { $page = $maxpage; } elseif($page < 1) { $page = 1; }
 $start = ($page - 1) * $users_per_page;

 if($page - 1 > 0 & $maxpage != 1) {  
 $prev = $page - 1;  
 $pp = "<a href='search.php?page=$prev&task=searchdo$url'>$search3</a>";  
 } else {
 $pp = "<font class='gray'>$search3</font>";  
 }
 if($page + 1 <= $maxpage & $maxpage != 1) {  
 $next = $page + 1;  
 $np = "<a href='search.php?page=$next&task=searchdo$url'>$search4</a>";  
 } else {
 $np = "<font class='gray'>$search4</font>";  
 }

$users = mysql_query("SELECT bhost_users.u_id, bhost_users.g_id, bhost_users.username, bhost_users.profile_privacy, bhost_users.avatar, bhost_users.fname, bhost_users.lname, bhost_users.display_name, bhost_users.show_name, bhost_users.city, bhost_users.state, bhost_users.country, bhost_users.last_login FROM bhost_users LEFT JOIN bhost_profile ON bhost_users.u_id=bhost_profile.u_id WHERE $search_string ORDER BY bhost_users.username LIMIT $start, $users_per_page");






// GET CURRENT RESULT RANGE
$low = (($page - 1) * $users_per_page) + 1;
$high = $low + ($users_per_page - 1);
if($high > $total_users) { $high = $total_users; }
if($low != $high) {
$results = "$search5 $low-$high $search19 $total_users.";
} else {
$results = "$search18 $high $search19 $total_users.";
}





// SHOW SEARCH RESULTS
echo $head;
echo "
$search20
$results
<br><br>
";

if($maxpage != 0 & $maxpage != 1) {
echo "$pp | $np<br><br>";
}

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]'"));

// GET NAME
$name = "";
if($user[show_name] == 1) {
 if($admin_info[field_fname] != "0" & $user[fname] != "") { $name .= "$user[fname]"; }
 if($admin_info[field_lname] != "0" & $user[lname] != "") { if($name != "") { $name .= " "; } $name .= "$user[lname]"; }
}
if($name == "") {
 if($admin_info[field_display_name] != "0" & $user[display_name] != "") { $name = "$user[display_name]"; }
}
if($name != "") { $name = "<br>$search21 $name"; } else { $name = ""; }

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

// GET LOCATION
$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>$search22 $location"; } else { $location = ""; }

// GET LAST LOGIN
if($user[last_login] != "") {
$login_date = cdate("$admin_info[signup_format_date]", timezone($user[last_login]));
$last_login = "<br>$search23 $login_date";
}

$add_to_friends = "";
if($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='./view_friends.php?task=adddyn&new_u=$user[username]' target='_blank'>$search24</a>";
}

$send_message = "";
if($user_info[u_id] != $user[u_id] & $group_info[allow_pms] == 1 & $user_group_info[allow_pms] == 1) {
 $send_message = "<br><a href='./messages.php?task=send&to=$user[username]' target='_blank'>$search25</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>";


if($maxpage != 0 & $maxpage != 1) {
echo "<br>$pp | $np";
}

// SHOW END TIME
//$time_end = microtime_float();
//$time = $time_end - $time_start; $time = round($time, 4);
//echo "<br><br>Search took: $time seconds\n";



echo $foot;
exit;
}


$task = "main";
}






// RESET VARIABLES
if($is_error == "no") {
$username = "";
$name = "";
$email = "";
$gender = "";
$location = "";
$age1 = ""; 
$age2 = "";
$website = "";
$screenname = "";
$interest = "";
$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_field = $custom[Field];
  ${"$custom_field"} = "";
 }
}
}



$ages1 = "";
for($c=1; $c<100; $c++) {
$ages1 .= "<option"; if($age1 == "$c") { $ages1 .= " SELECTED"; } $ages1 .= ">$c</option>";
}
$ages2 = "";
for($c=1; $c<100; $c++) {
$ages2 .= "<option"; if($age2 == "$c") { $ages2 .= " SELECTED"; } $ages2 .= ">$c</option>";
}

$count = 0;
$box = Array();

if($admin_info[search_username] != 0) { $box[$count] = "<td>$search7<br><input type='text' name='username' value='$username' class='text'>&nbsp;&nbsp;</td>"; $count++; }
if($admin_info[search_fname] != 0 | $admin_info[search_lname] != 0 | $admin_info[search_display_name] != 0) { $box[$count] = "<td>$search8<br><input type='text' name='name' value='$name' class='text'>&nbsp;&nbsp;</td>"; $count++; }
if($admin_info[search_email] != 0) { $box[$count] = "<td>$search9<br><input type='text' name='email' value='$email' class='text'>&nbsp;&nbsp;</td>"; $count++; }
if($admin_info[search_gender] != 0) { $box[$count] = "<td>$search10<br><select name='gender'><option></option><option"; if($gender == "$search27") { $box[$count] .= " SELECTED"; } $box[$count] .= ">$search27</option><option"; if($gender == "$search28") { $box[$count] .= " SELECTED"; } $box[$count] .= ">$search28</option></select>&nbsp;&nbsp;</td>"; $count++; }
if($admin_info[search_city] != 0 | $admin_info[search_state] != 0 | $admin_info[search_country] != 0) { $box[$count] = "<td>$search11<br><input type='text' name='location' value='$location' class='text'>&nbsp;&nbsp;</td>"; $count++; }
if($admin_info[search_birthday] != 0) { $box[$count] = "<td>$search12<br><table cellpadding='0' cellspacing='0'><tr><td><select name='age1'><option></option>$ages1</select></td><td>&nbsp;<font class='gray'>$search13</font>&nbsp;</td><td><select name='age2'><option></option>$ages2</select>&nbsp;&nbsp;</td></tr></table></td>"; $count++; }
if($admin_info[search_website] != 0) { $box[$count] = "<td>$search14<br><input type='text' name='website' value='$website' class='text'>&nbsp;&nbsp;</td>"; $count++; }
if($admin_info[search_screenname] != 0) { $box[$count] = "<td>$search15<br><input type='text' name='screenname' value='$screenname' class='text'>&nbsp;&nbsp;</td>"; $count++; }
if($admin_info[search_interests] != 0) { $box[$count] = "<td>$search16<br><input type='text' name='interest' value='$interest' class='text'>&nbsp;&nbsp;</td>"; $count++; }

$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);
  $custom_name = str_replace("_", " ", substr($custom[Field], 0, -3));
  if($custom_search != "0") {
   $box[$count] = "<td>$custom_name<br><input type='text' name='$custom[Field]' value='".${"$custom[Field]"}."' class='text'>&nbsp;&nbsp;</td>"; 
   $count++;
  }
 }
}



echo $head;

echo "
$search6
<br><br>
";
// DISPLAY ERROR
if($is_error == "yes") { 
 if($error == "") {
  $error = "$search26";
 }
 show_error($error);
 echo "<br>";
}
echo "
<table cellpadding='0' cellspacing='0' class='form'>
<form action='search.php' method='POST'>
";

for($a=0;$a<count($box);$a++) {
 if($a%3 == 0) { echo "<tr><td class='form1'><table cellpadding='0' cellspacing='0'><tr>"; }
 echo $box[$a];
 if($a%3 == 2 | $a+1 == count($box)) { echo "</tr></table></td></tr>"; }
}

echo "
</table>

<br>

<input type='submit' class='button' value='$search17'>
<input type='hidden' name='task' value='searchdo'>
<input type='hidden' name='page' value='1'>
</form>
";

echo $foot;
?>