<?php

define('_IN_JOHNCMS', 1);
$textl = 'Foydalanuvchilarni qidirish';
$headmod = 'usersearch';
require_once('../incfiles/core.php');
require('../incfiles/head.php');

echo '<div class="phdr" style="text-align: center"><b>Yangi do\'stlar topish</b></div>';


$sex = isset($_REQUEST['sex']) ? abs(intval($_REQUEST['sex'])) : 0;
$from_age = isset($_REQUEST['from']) ? abs(intval($_REQUEST['from'])) : 0;
$to_age = isset($_REQUEST['to']) ? abs(intval($_REQUEST['to'])) : 0;
$rostot_rag = isset($_REQUEST['rostot']) ? abs(intval($_REQUEST['rostot'])) : 0;
$rostdo_rag = isset($_REQUEST['rostdo']) ? abs(intval($_REQUEST['rostdo'])) : 0;
$vesot_vag = isset($_REQUEST['vesot']) ? abs(intval($_REQUEST['vesot'])) : 0;
$vesdo_vag = isset($_REQUEST['vesdo']) ? abs(intval($_REQUEST['vesdo'])) : 0;
$live = isset($_POST['live']) ? mb_strtolower(functions::check(mb_substr($_POST['live'], 0, 50)), 'utf-8') : '';
$live = $live ? $live : mb_strtolower(functions::check(mb_substr(rawurldecode($_GET['live']), 0, 50)), 'utf-8');
$photo = intval($_REQUEST['photo']) ? 1 : 0;
$online = intval($_REQUEST['online']) ? 1 : 0;
$new = intval($_REQUEST['new']) ? 1 : 0;

//Пол
if ($sex) {
$sql = '`sex` = \'' . ($sex == 1 ? 'm' : 'zh') . '\'';
$md = date('m-d', time());

//Возраст от и до  
if ($from_age) {
$year_from = date('Y', time()) - $from_age;
$date = $year_from . '-' . $md;
$sql .= ' AND UNIX_TIMESTAMP(CONCAT_WS("-",`yearofbirth`,`monthb`,`dayb`)) <= UNIX_TIMESTAMP(\'' . $date . '\') AND `yearofbirth` != \'0\'';
}
    
if ($to_age) {
$year_to = date('Y', time()) - $to_age;
$date = $year_to . '-' . $md;
$sql .= ' AND UNIX_TIMESTAMP(CONCAT_WS("-",`yearofbirth`,`monthb`,`dayb`)) >= UNIX_TIMESTAMP(\'' . $date . '\')';
}

//Рост От    
if ($rostot_rag) {
$sql.=" AND `rost`<= '$rostdo_rag' ";
}
//Рост До
if ($rostdo_rag) {
$sql.=" AND `rost`>= '$rostot_rag' ";
}

//Вес От    
if ($vesot_vag) {
$sql.=" AND `ves`<= '$vesdo_vag' ";
}
//Вес До
if ($vesdo_vag) {
$sql.=" AND `ves`>= '$vesot_vag' ";
}

//Из города   
if ($live)
$sql .=	' AND LOWER(`live`) LIKE \'%' . functions::trans($live) . '%\' OR LOWER(`live`) LIKE \'%' . functions::rus_lat($live) . '%\'';

if ($photo) {
$sql .= ' AND `id` IN ( ';
$dir = opendir('../files/users/photo');
while ($file = readdir($dir)) {
$name = explode(".", $file);
$names = preg_replace('#^(.*?)_small.(gif|jpg|png)$#isU', '$1', $file);
if ($name[1] == 'jpg' && $name[0] != $names.'_small' && $name[0] != $user_id) {
$name[0] = $name[0].', ';
$sql .= $name[0];
}
}
closedir($dir);
$sql .= '0 )';
}

if ($online)
$sql .= ' AND `lastdate` > \'' . (time() - 300) . '\'';
if ($new)
$sql .= ' AND `datereg` > \'' . (time() - 86400) . '\'';
$error = array();
    
if ($from_age && $to_age && $from_age >= $to_age)
$error[] = 'Yoshni xato kiritdingiz!';
    if ($from_age > 80 || $to_age > 80)
$error[] = 'Maksimal yosh - 80.';
if ($live && mb_strlen($live) < 2)
$error[] = 'Shaharda minimal 2 ta belgi bo\'lishi kerak.';
    
if (!$error) {
$req = mysql_query("SELECT COUNT(*) FROM `users` WHERE `id` != '$user_id' AND $sql");
$total = mysql_result($req, 0);
if ($total) {
$req = mysql_query("SELECT * FROM `users` WHERE `id` != '$user_id' AND $sql ORDER BY `id` LIMIT $start, $kmess");
while ($res = mysql_fetch_array($req)) {
$ages = (int)((time() - mktime(0, 0, 0, $res['monthb'], $res['dayb'], $res['yearofbirth'])) / (60 * 60 * 24 * 365.25));
$rages = $res['rost'];
$vages = $res['ves'];

//Возраст          
if ($to_age || $from_age)
$age = '<li><span class="gray">Yoshi:</span> ' . $ages . '</li>';
//Рост 
if ($rostot_rag || $rostdo_rag)
$rag = '<li><span class="gray">Bo\'yi:</span> ' . $rages . '</li>';
//Bec    
if ($vesot_vag || $vesdo_vag)
$vag = '<li><span class="gray">Vazni:</span> ' . $vages . '</li>';
             
          
if ($live)
$city = '<li><span class="gray">Shahar:</span> ' . $res['live'] . '</li>';
echo $i % 2 ? '<div class="list2">' : '<div class="list1">';
$arg = array (
'iphide' => 1,
'body' => $age.$rag.$vag.$city
 );
echo functions::display_user($res, $arg);
echo '</div>';
++$i;
}
            
echo '<div class="phdr">Jami: ' . $total . '</div>';

if ($total > $kmess) {
echo '<div class="topmenu">' . functions::display_pagination('rsearch.php?&amp;live=' . rawurlencode($live) . '&amp;sex=' . $sex . '&amp;from=' . $from_age. '&amp;to=' . $to_age . '&amp;rostot=' . $rostot_age . '&amp;rostdo=' . $rostdo_age . '&amp;vesot=' . $vesot_age . '&amp;vesdo=' . $vesdo_age . '&amp;on=' . $online  . '&amp;new=' . $new . '&amp;photo=' . $photo . '&amp;', $start, $total, $kmess) . '</div>' .
  '<p><form action="rsearch.php" method="post">' .
  '<input type="hidden" name="live" value="' . functions::checkout($live) . '" /><input type="hidden" name="sex" value="' . $sex . '" />' .
  '<input type="hidden" name="from" value="' . $from_age . '" /><input type="hidden" name="to" value="' . $to_age . '" />' .
  '<input type="hidden" name="rostot" value="' . $rostot_age . '" /><input type="hidden" name="rostdo" value="' . $rostdo_age . '" />' .
  '<input type="hidden" name="vesot" value="' . $vesot_age . '" /><input type="hidden" name="vesdo" value="' . $vesdo_age . '" />' .
  '<input type="hidden" name="on" value="' . $online . '" /><input type="hidden" name="new" value="' . $new . '" />' .
  '<input type="hidden" name="photo" value="' . $photo . '" /><input type="text" name="page" size="2"/>' .
'<input type="submit" value="O\'tish &gt;&gt;"/>' .
  '</form></p>';
  
}
} else {
echo '<div class="rmenu" style="text-align: center"><b>Qidiruvda hech nima topilmadi!</b></div>';
}
} else {
echo functions::display_error($error);
}
} else {
echo '<form action="rsearch.php" method="post">' .
'<div class="gmenu"><p>' .
'<b>Qidiramiz</b></p><select name="sex">' . ($datauser['sex'] == 'm' ? '<option value="2" select="selected">Ayollarni</option><option value="1">Erkaklarni</option>' :
'<option value="1" select="selected">Erkaklarni</option><option value="2">Ayollarni</option>') . '</select>' .
'<p><b>Yoshi</b></p><input type="text" maxlength="2" size="2" name="from" value=""/> ' .
'dan <input type="text" maxlength="2" size="2" name="to" value=""/> gacha' .
'</br><p><b>Qaysi shahardan </b><small>(min. 2, maks. 50)</small></p><input type="text" name="live"/></div>' .
'<div class="gmenu"><p><input type="checkbox" name="photo" value="1" /> - faqat fotosuratlilar</p>' .
'<p><input type="checkbox" name="online" value="1" /> - faqat onlayndagilar</p>' .
'<p><input type="checkbox" name="new" value="1" /> -  faqat yangilarni</p></div>' .
'<div class="gmenu"><input type="submit" value="Qidirish" name="submit" />' .
'</div></form>';
}
echo '<div class="phdr">' . ($sex ? '<a href="rsearch.php"><b> Yana qidirish</b></a>' : '<a href="index.php">Ortga</a>') . '</div>';

require_once('../incfiles/end.php');

?>