<?php
define('_IN_JOHNCMS', 1);
require('../incfiles/core.php');
$lng_profile = core::load_lng('profile');
/*
-----------------------------------------------------------------
Закрываем от неавторизованных юзеров
-----------------------------------------------------------------
*/
if (!$user_id) {
    require('../incfiles/head.php');
    echo functions::display_error($lng['access_guest_forbidden']);
    require('../incfiles/end.php');
    exit;
}
/*
-----------------------------------------------------------------
Получаем данные пользователя
-----------------------------------------------------------------
*/
$user = functions::get_user($user);
if (!$user) {
    require('../incfiles/head.php');
    echo functions::display_error($lng['user_does_not_exist']);
    require('../incfiles/end.php');
    exit;
}
/*
-----------------------------------------------------------------
Переключаем режимы работы
-----------------------------------------------------------------
*/
$array = array(
    'activity'  => 'includes/profile',
    'ban'       => 'includes/profile',
    'edit'      => 'includes/profile',
    'images'    => 'includes/profile',
    'info'      => 'includes/profile',
    'ip'        => 'includes/profile',
    'guestbook' => 'includes/profile',
    'karma'     => 'includes/profile',
    'office'    => 'includes/profile',
    'password'  => 'includes/profile',
    'reset'     => 'includes/profile',
    'settings'  => 'includes/profile',
    'til'  => 'includes/profile',
'stat'      => 'includes/profile',
    'medal'      => 'includes/profile',
    'friends'   => 'includes/profile'
);
$path = !empty($array[$act]) ? $array[$act] . '/' : '';
if (array_key_exists($act, $array) && file_exists($path . $act . '.php')) {
    require_once($path . $act . '.php');
} else {
    /*
    -----------------------------------------------------------------
    Анкета пользователя
    -----------------------------------------------------------------
    */
    $headmod = 'profile,' . $user['id'];
    $textl = $lng['profile'] . ': ' . htmlspecialchars($user['name']);
    require('../incfiles/head.php');
    echo '<div class="phdr"><b>' . ($user['id'] != $user_id ? $lng_profile['user_profile'] : $lng_profile['my_profile']) . '</b></div>';

    // Меню анкеты
    $menu = array();
    if ($user['id'] == $user_id || $rights == 9 || ($rights == 7 && $rights > $user['rights'])) {
        $menu[] = '<a href="profile.'.$akb_for.'?act=edit&amp;user=' . $user['id'] . '">' . $lng['edit'] . '</a>';
    }
    if ($user['id'] != $user_id && $rights >= 7 && $rights > $user['rights']) {
        $menu[] = '<a href="' . $set['homeurl'] . '/' . $set['admp'] . '/index.'.$akb_for.'?act=usr_del&amp;id=' . $user['id'] . '">' . $lng['delete'] . '</a>';
    }
    if ($user['id'] != $user_id && $rights > $user['rights']) {
        $menu[] = '<a href="profile.'.$akb_for.'?act=ban&amp;mod=do&amp;user=' . $user['id'] . '">' . $lng['ban_do'] . '</a>';
}
    if (!empty($menu)) {
        echo '<div class="topmenu">' . functions::display_menu($menu) . '</div>';
    }

    //Уведомление о дне рожденья
    if ($user['dayb'] == date('j', time()) && $user['monthb'] == date('n', time())) {
        echo '<div class="gmenu">' . $lng['birthday'] . '!!!</div>';
    }
/// Agent /// 
if (isset($_POST['submit'])) { $user['balans'] =  isset($_POST['balans']) ?  functions::check(mb_substr($_POST['balans'], 0, 10)) : ''; $updatebalans = mysql_query("UPDATE `users` SET  `balans` = '" . $user['balans'] . "'  WHERE `id` = '" . $user['id'] . "'"); if($updatebalans) echo '<div  class="gmenu"><center><b><big><font color="red">Balans  yangilandi ura!</font></big></b></center></div>'; }

// $_SERVER['HTTP_USER_AGENT'] $usagent = $user['browser'];
    // Информация о юзере
    $arg = array(
'lastvisit' => 1,
'iphist' => 1,
'header'    => ''
    );

    if ($user['id'] != core::$user_id) {
        $arg['footer'] = '<span class="gray">' . core::$lng['where'] . ':</span> ' . functions::display_place($user['id'], $user['place']);
    }

    echo '<div class="user"><p>' . functions::display_user($user, $arg) . '</p></div>';

	// Если юзер ожидает подтверждения регистрации, выводим напоминание
    if ($rights >= 7 && !$user['preg'] && empty($user['regadm'])) {
        echo '<div class="rmenu">' . $lng_profile['awaiting_registration'] . '</div>';
    }
echo'<div class="menu">';
if ($user['id'] == $user_id) {
echo '<p><a href="../users/profile.'.$akb_for.'?act=images&amp;mod=up_photo&amp;user='.$user['id'].'" ><span style="border-radius:6px;">Profilga foto yuklash</span><br></a></p>';
 }else{ 
echo '<a href="../mail/index.'.$akb_for.'?act=write&amp;id=' .$user['id'].'" ><span class="tmn"style="border-radius:6px">Xabar yuborish</span></a>';
}
echo'</div>';
    // Меню выбора
echo'<div class="menu">';
if (!empty($user['mail']) && $user['mailvis'] || $rights >= 7 || $user['id'] == $user_id) { 
echo'ID:' . $user['id'] . ' <font color="red"> (!) </font><br/>E-mail: ' .$user['mail']. '<font color="red">(!)</font>'; }
echo '<br/>' . $lng_profile['name'] . ':</a> ' .  (empty($user['imname']) ? '' : $user['imname']) . '</br>'; 
echo '' . $lng_profile['birt'] . ':</span> ' .  (empty($user['dayb']) ? '' :  sprintf("%02d", $user['dayb']) . '.' .  sprintf("%02d", $user['monthb']) . '.' . $user['yearofbirth']) . '</br>';
echo'' . $lng_profile['city'] . ':</span> ' .  (empty($user['live']) ? '' : $user['live']) . '';
echo '<br/>Saytda bo`ldi: ' . functions::timecount($user['total_on_site']) . '<br />';
$lastvisit = time() > $user['lastdate'] + 300 ? date("d.m.Y | H:i", $user['lastdate']) : true; 
echo 'So`ngi tashrif: ' . date("d.m.Y | H:i", $user['lastdate']) . '';
echo'</div>';
$count_contacts = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_contact` WHERE `user_id`='" . $user_id . "' AND `ban`!='1';"), 0);
if ($user['id'] != $user_id) { 
}else{
echo'<div class="menu">
<i class="fa fa-user-edit"></i> <a href="../users/profile.'.$akb_for.'?act=edit">Shaxsiy ma`lumotlarni o`zgartirish</a><br/>
<i class="fa fa-key"></i>  <a href="../users/profile.'.$akb_for.'?act=password"> Parolni o`zgartirish</a><br/>
<i class="fa fa-user-cog"></i>  <a href="../users/profile.'.$akb_for.'?act=settings"> Shaxsiy sozlanmalar</a></br>
<i class="fa fa-bookmark"></i>  <a href="/forum/?act=my_zaklad">Shaxsiy zakladkalar</a></div>';
}
$mavzulari = mysql_result( mysql_query("SELECT COUNT(*) FROM `forum` WHERE `user_id` = '".$user_id."' AND `type` = 't'".($rights>=7 ? '':" AND `close`!='1'")),0);
echo'<div class="menu">';
echo 'Tangalari: '.$user['balans'].'<br />';
echo  'Forumdagi postlar soni:</span> <a href="profile.'.$akb_for.'?act=activity&amp;user=' . $user['id'] . '">' . $user['postforum'] . '';
echo'</a>';
echo'</div>';
    $total_photo = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_album_files` WHERE `user_id` = '" . $user['id'] . "'"), 0);
    $bancount = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_ban_users` WHERE `user_id` = '" . $user['id'] . "'"), 0);
    if ($bancount) {
echo '<div class="menu"><img src="../2.png"/> <a href="profile.'.$akb_for.'?act=ban&amp;user=' . $user['id'] . '">' . $lng['infringements'] . '</a> (' . $bancount . ')</div>';
    }
$total_friends = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_contact` WHERE `user_id`='{$user['id']}' AND `type`='2' AND `friends`='1'"), 0);
if ($user_id && $user_id != $user['id']) {
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_mehmon` WHERE `id_user` = '".$user_id."' AND `id_ank` = '".$user['id']."' LIMIT 1"),0)){
mysql_query("UPDATE `forum_mehmon` SET `read`='1',`time` = '". time() ."' WHERE `id_ank` = '".$user['id']."' AND `id_user` = '".$user_id."' LIMIT 1");
	} else {
mysql_query("INSERT INTO `forum_mehmon` SET `read`='1',`id_user` = '".$user_id."',`id_ank` = '".$user['id']."',`time` = '". time() ."'");
	}
	}
$h = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_rahmat` WHERE `user_id` = '".$user['id']."'"),0);
$r = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_rahmat` WHERE `kimga` = '".$user['id']."'"),0);
		echo '<div class="menu">
<span style="color: gray;">Rahmat aytdi:</span> <b>'.$h.'</b> marta<div class="br"></div>
<span style="color: gray;">Rahmat aytishdi:</span> <b>'.$r.'</b> marta<div class="br"></div></div>';
$v1 = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_votte` WHERE `user_id` = '".$user['id']."' AND `votte`='1'"),0);
$v2 = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_votte` WHERE `kimga` = '".$user['id']."' AND `votte`='1'"),0);
$v3 = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_votte` WHERE `user_id` = '".$user['id']."' AND `votte`='2'"),0);
$v4 = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_votte` WHERE `kimga` = '".$user['id']."' AND `votte`='2'"),0);
echo '<div class="menu"> 
<span style="color: gray;"><font color="green">+</font> ovoz berdi:</span> <b>'.$v1.'</b> marta<div class="br"></div>
<span style="color: gray;"><font color="green">+</font> ovoz berishdi:</span> <b>'.$v2.'</b> marta<div class="br"></div>
<span style="color: gray;"><font color="red">-</font> ovoz berdi:</span> <b>'.$v3.'</b> marta<div class="br"></div>
<span style="color: gray;"><font color="red">-</font> ovoz berishdi:</span> <b>'.$v4.'</b> marta<div class="br"></div></div>';
if ($user['id'] != $user_id) { }else{

echo' <div class="menu">
<i class="fa fa-envelope"></i>  <a href="../mail/index.'.$akb_for.'?act=input">Xabarlar</a> (' .$count_contacts. ')<br/>'; 
echo'<i class="fa fa-bookmark"></i>  <a href="/chat.'.$akb_for.'">Master chat</a><br/>';
echo'<i class="fa fa-images"></i>  <a href="album.'.$akb_for.'?act=list&amp;user=' . $user['id'] . '">' . $lng['photo_album'] . '</a> (' . $total_photo . ') </div><br/>';
}

if ($rights == 9) { echo '<div class="phdr"><b>' . $lng['balance'] . ': ' . $user['balans'] .  '</b></div>'; echo '<div class="menu"><form  action="profile.'.$akb_for.'?user='.$user['id'] .'" method="post">'; echo '<input type="text"  maxlength="10" name="balans"  /><input type="submit"  name="submit" value="' . $lng['edit'] . '" />'; echo '</form></div>'; }
echo '<div class="phdr"> <b>Shaxsiy fotosuratlar</b></div>'; $req = mysql_query("SELECT *  FROM `cms_album_files` WHERE  `user_id` = '" . $user['id'] . "' ORDER BY  RAND() DESC LIMIT 5"); echo '<div class="menu">'; while ($res =  mysql_fetch_assoc($req)) { echo '<img '.$style.' class="short-image" src="../files/users/album/' . $user['id'] . '/' . $res['tmb_name'] . '"width="80"height="80"/>'; } echo '</div>';
    if ($user['id'] != $user_id) {
        echo '<div class="menu"><p>';
        // Контакты
        if (!functions::is_ignor($user['id']) && functions::is_contact($user['id']) != 2) {
            if (!functions::is_friend($user['id'])) {
                $fr_in = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_contact` WHERE `type`='2' AND `from_id`='$user_id' AND `user_id`='{$user['id']}'"), 0);
                $fr_out = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_contact` WHERE `type`='2' AND `user_id`='$user_id' AND `from_id`='{$user['id']}'"), 0);
                if ($fr_in == 1) {
                    $friend = '<a class="underline" href="profile.'.$akb_for.'?act=friends&amp;do=ok&amp;id=' . $user['id'] . '">' . $lng_profile['confirm_friendship'] . '</a> | <a class="underline" href="profile.'.$akb_for.'?act=friends&amp;do=no&amp;id=' . $user['id'] . '">' . $lng_profile['decline_friendship'] . '</a>';
                } else if ($fr_out == 1) {
                    $friend = '<a class="underline" href="profile.'.$akb_for.'?act=friends&amp;do=cancel&amp;id=' . $user['id'] . '">' . $lng_profile['canceled_demand_friend'] . '</a>';
                } else {
                    $friend = '<a href="profile.'.$akb_for.'?act=friends&amp;do=add&amp;id=' . $user['id'] . '">' . $lng_profile['in_friend'] . '</a>';
                }
            } else {
            }
        }

        if (functions::is_contact($user['id']) != 2) {
            if (!functions::is_contact($user['id'])) {
echo '<div><img src="../2.png" width="16" height="16"/> <a href="../mail/index.'.$akb_for.'?id=' . $user['id'] . '">' . $lng_profile['add_contacts'] . '</a></div>';
            } else {
echo '<div><img src="../2.png" width="16" height="16"/> <a href="../mail/index.'.$akb_for.'?act=deluser&amp;id=' . $user['id'] . '">' . $lng_profile['delete_contacts'] . '</a></div>';
            }
        }
if (functions::is_contact($user['id']) != 2) {
echo '<div><img src="../2.png" width="16" height="16"/> <a href="../mail/index.'.$akb_for.'?act=ignor&amp;id=' . $user['id'] . '&amp;add">' . $lng_profile['add_ignor'] . '</a></div>';
        } else {
echo '<div><img src="../2.png" width="16" height="16"/>&#160;<a href="../mail/index.'.$akb_for.'?act=ignor&amp;id=' . $user['id'] . '&amp;del">' . $lng_profile['delete_ignor'] . '</a></div>';
        }
        echo '</p>';

        if (!functions::is_ignor($user['id']) && functions::is_contact($user['id']) != 2 && empty($ban['1']) && empty($ban['3'])) {
        }
        echo '</div>';
    }
}

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