<?php

/**
 * @package     JohnCMS
 * @link        http://johncms.com
 * @copyright   Copyright (C) 2008-2011 JohnCMS Community
 * @license     LICENSE.txt (see attached file)
 * @version     VERSION.txt (see attached file)
 * @author      http://johncms.com/about
 */

define('_IN_JOHNCMS', 1);

$lng_profile = core::load_lng('profile');

/*
-----------------------------------------------------------------
Закрываем от неавторизованных юзеров
-----------------------------------------------------------------
*/
if (!$user_id) {
header('location: /login.php');
    exit;
}

/*
-----------------------------------------------------------------
Получаем данные пользователя
-----------------------------------------------------------------
*/
$user = functions::get_user($user);
if (!$user) {
    echo functions::display_error($lng['user_does_not_exist']);
    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',
    'stat'      => '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']);

    // Меню анкеты

if ($user['id'] == $user_id)
if (empty($user['yearofbirth']) || empty($user['imname']) || empty($user['live']) || empty($user['about']))
{
echo '<div align="center" class="gmenu">Aziz foydalanuvchi ma\'lumotlaringiz to\'liq to\'ldirilmagan, iltimos ko\'proq do\'stlar ortirish uchun ma\'lumotlaringizni to\'ldiring!!</div>';
echo '<div class="gmenu" align="center"><b><a href="/users/profile.php?act=edit">To\'ldirish</a></b></div>';
}


    //Уведомление о дне рожденья
    if ($user['dayb'] == date('j', time()) && $user['monthb'] == date('n', time())) {
echo '<div class="gmenu">Tug\'ilgan kuningiz bilan tabriklaymiz !!!</div>';
    }

    // Информация о юзере
    $arg = array(
        'lastvisit' => 1,
        'iphist'    => 1,
        'header'    => '<b>ID:' . $user['id'] . '</b>'
    );

if (file_exists(('/files/users/photo/' . $user['id'] . '.jpg'))) {
echo '<table width="100%" class="list2"><tr><td>';
echo '<a href="/files/users/photo/' . $user['id'] . '.jpg"><img src="/files/users/photo/' . $user['id'] . '_small.jpg" alt="' . $user['name'] . '" style="float:left;"/></a>';
} else {
echo '<table width="100%" class="list2"><tr><td>';
if ($user['sex']) {
echo '<img src="/files/users/photo/' . ($user['sex'] == 'm' ? 'm' : 'w') .'.png" style="width:48px; height:48px; float:left;" alt="' . ($user['sex'] == 'm' ? 'М' : 'Ж') . '" />';
}
}
echo '</td>';
echo '<td>';
            if ($user['sex'])
echo (time() > $user['lastdate'] + 300 ? '<img src="/images/' . ($user['sex'] == 'm' ? 'man' : 'woman') . ($user['rights'] > 7 && $user['rights'] < 10 ? '_sv' : '') . ($user['rights'] > 6 && $user['rights'] < 9 ? '_adm' : '') . ($user['rights'] > 5 && $user['rights'] < 7 ? '_smod' : '') . ($user['rights'] > 4 && $user['rights'] < 6 ? '_lmod' : '') . ($user['rights'] > 3 && $user['rights'] < 5 ? '_dmod' : '') . ($user['rights'] > 2 && $user['rights'] < 4 ? '_fmod' : '') . '_off.gif" width="14" height="14" style="margin-bottom: -1px;" alt=""/>&#160;' : '<img src="/images/' . ($user['sex'] == 'm' ? 'man' : 'woman') . ($user['rights'] > 7 && $user['rights'] < 10 ? '_sv' : '') . ($user['rights'] > 6 && $user['rights'] < 9 ? '_adm' : '') . ($user['rights'] > 5 && $user['rights'] < 7 ? '_smod' : '') . ($user['rights'] > 4 && $user['rights'] < 6 ? '_lmod' : '') . ($user['rights'] > 3 && $user['rights'] < 5 ? '_dmod' : '') . ($user['rights'] > 2 && $user['rights'] < 4 ? '_fmod' : '') . '_on.gif" width="14" height="14" style="margin-bottom: -1px;"  alt=""/>&#160;');
                else
                    echo functions::image('del.png');
$bansu = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_ban_users` WHERE `user_id` = '" . $user['id'] . "'"), 0);
    if ($bansu) { 
echo '<s><b>' . $user['name'] . '</b></s>';
} else {
echo '<b>' . $user['name'] . '</b>';
}
echo '</br><span class="gray">' . ($user['id'] != $user_id ? 'Ismi' : 'Ismingiz') . ':</span><b> ' . (empty($user['imname']) ? 'kiritilmagan' : $user['imname']) . '</b>';
if(!empty($user['yearofbirth'])){
function calc_age($d, $m, $y)
{
$age = date('Y') - $y;
$_m = date('m');
if($_m < $m || ($_m == $m && date('d') < $d))
$age--;

return $age;
}
$user_age = calc_age($user['dayb'], $user['monthb'], $user['yearofbirth']);
echo '</br><span class="gray">' . ($user['id'] != $user_id ? 'Yoshi':'Yoshingiz') . ':</snap> <b>'.$user_age.'</b> da';
}
echo '</br><span class="gray">' . ($user['id'] != $user_id ? 'Yashash shahri' : 'Shahar') . ':</span> <b>' . (empty($user['live']) ? 'kiritilmagan' : $user['live']) . '</b>';
$lastvisit = time() > $user['lastdate'] + 300 ? date("d.m.Y (H:i)", $user['lastdate']) : false;
if ($lastvisit)
echo '</br><span class="gray">Oxirgi tashrifi:</span><b> ' . $lastvisit . '</b>';
echo '</td></tr><table>';

echo '<table width="100%"><tr>';

if ($user['id'] == $user_id)
{
$new_sys_mail = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_mail` WHERE `from_id`='$user_id' AND `read`='0' AND `sys`='1' AND `delete`!='$user_id';"), 0);
   if ($new_sys_mail) {
echo '<td class="panel_td"><a href="' . $home . '/mail/index.php?act=systems"><img src="' . $home . '/images/profil/sms.png"><span color="green"> Xabar +' . $new_sys_mail . '</snap></a></td>';
} else {
$new_mail = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_mail` LEFT JOIN `cms_contact` ON `cms_mail`.`user_id`=`cms_contact`.`from_id` AND `cms_contact`.`user_id`='$user_id' WHERE `cms_mail`.`from_id`='$user_id' AND `cms_mail`.`sys`='0' AND `cms_mail`.`read`='0' AND `cms_mail`.`delete`!='$user_id' AND `cms_contact`.`ban`!='1' AND `cms_mail`.`spam`='0'"), 0);
if ($new_mail) {
echo '<td class="menu"><a href="' . $home . '/mail/index.php?act=new"><img src="' . $home . '/images/profil/sms.png"><span color="green"> Xabar +' . $new_mail . '</snap></a></td>' ;
} else {
echo '<td class="menu"><a href="/mail/index.php"><img src="' . $home . '/images/profil/sms.png"> Xabarlar</td>';
}
}
} else {
if (!functions::is_ignor($user['id']) && functions::is_contact($user['id']) != 2&& empty($ban['1']) && empty($ban['3'])) {
echo '<td class="menu"><a href="/mail/index.php?act=write&amp;id=' . $user['id'] . '"> <img src="' . $home . '/images/profil/sms.png"> Yozish</td>';
}
}
if ($user['id'] != $user_id) {
echo '<td class="menu">';
// Контакты
        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_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="/users/profile.php?act=friends&amp;do=ok&amp;id=' . $user['id'] . '">' . $lng_profile['confirm_friendship'] . '</a> | <a class="underline" href="/users/profile.php?act=friends&amp;do=no&amp;id=' . $user['id'] . '">' . $lng_profile['decline_friendship'] . '</a>';
                } else if ($fr_out == 1) {
$friend = '<a class="underline" href="/users/profile.php?act=friends&amp;do=cancel&amp;id=' . $user['id'] . '">' . $lng_profile['canceled_demand_friend'] . '</a>';
                } else {
$friend = '<a href="/users/profile.php?act=friends&amp;do=add&amp;id=' . $user['id'] . '"><img src="' . $home . '/images/profil/druzya.png"> Do\'stlashish</a>';
                }
            } else {
$friend = '<a href="/users/profile.php?act=friends&amp;do=delete&amp;id=' . $user['id'] . '"><font color="green"><img src="' . $home . '/images/profil/druzya2.png"> Bekor</a></font>';
            }
echo '' . $friend . '</td>';
        }
} else {
echo '<td class="menu"><a href="/users/profile.php?act=friends&amp;user=' . $user['id'] . '"><img src="' . $home . '/images/profil/ico12.png"> Do\'stlar</a></td>';
}
echo '</td>';

echo '<tr></table>';
    // Если юзер ожидает подтверждения регистрации, выводим напоминание
    if ($rights >= 7 && !$user['preg'] && empty($user['regadm'])) {
        echo '<div class="rmenu">' . $lng_profile['awaiting_registration'] . '</div>';
    }
    // Карма
$bancount = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_ban_users` WHERE `user_id` = '" . $user['id'] . "'"), 0);
if ($bancount) {
echo 'Siz BAN langansiz';
} else {
echo '<table width="100%"><td class="umenu" width="50%" align="center"><b>' . ($user['id'] != $user_id ? 'Anketasi' : 'Anketam') . '</b></td><td class="menu" align="center"><a href="/users/profile.php?act=info&amp;user=' . $user['id'] . '">' . ($user['id'] != $user_id ? 'Ma\'lumotlari' : 'Ma\'lumotlarim') . '</a></td></table>';
    // Меню выбора


$online_friends = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_contact` LEFT JOIN `users` ON `cms_contact`.`from_id`=`users`.`id` WHERE `cms_contact`.`user_id`='$user_id' AND `cms_contact`.`type`='2' AND `cms_contact`.`friends`='1' AND `lastdate` > " . (time() - 300) . ""), 0);
    $total_photo = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_album_files` WHERE `user_id` = '" . $user['id'] . "'"), 0);


    $total_friends = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_contact` WHERE `user_id`='{$user['id']}' AND `type`='2' AND `friends`='1'"), 0);
echo '<div class="list2"><a href="/users/profile.php?act=guestbook&use=' . $user['id'] . '"><img src="' . $home . '/images/profil/gostevaya.png"> Shaxsiy mehmonxona</a>&#160;(' . $user['comm_count'] . ')</div>';
$blog = mysql_result(mysql_query("SELECT COUNT(*) FROM `blogs` WHERE user='$user[id]' ".($user['id'] == $user_id || $rights > 6 ? "" : "AND close!=1 AND close!=3").""), 0);
echo '<div class="list1"><a href="/blogs/index.php?act=my&id=' . $user['id'] . '"><img src="' . $home . '/images/profil/blog.png"> ' . ($user['id'] != $user_id ? 'Shaxsiy bloglari' : 'Mening bloglarim') . '</a>&#160;(' . $blog . ')</div>' .
'<div class="menu"><a href="chat.php"><img src="' . $home . '/images/chat.png"> O\'zimizning Chat</a> (' . counters::mchat() .')</div>' .
'<div class="list2"><a href="/igri/index.php"><img src="' . $home . '/images/profil/igri.png"> Onlayn o\'yinlar</a>&#160;<font color="red">ta\'mirda</font></div>' .
'<div class="list1"><a href="/users/album.php?act=list&amp;user=' . $user['id'] . '"><img src="' . $home . '/images/profil/foto.png"> Fotoalbom</a>&#160;(' . $total_photo . ')</div>' .
'<div class="list2"><a href="/users/profile.php?act=friends&amp;user=' . $user['id'] . '"><img src="' . $home . '/images/profil/druzya.png"> Do\'stlar</a>&#160;(' . $total_friends . ')';
if ($user['id'] == $user_id) 
{
echo ' / <a href="/users/profile.php?act=friends&amp;do=online"> Onlaynda</a> (' . $online_friends . ')';
}

echo '</div><div class="menu"><a href="users/index.php">A\'zolar </a> (' . counters::users() . ') /';
$hsaytda = mysql_result(mysql_query("SELECT COUNT(*) FROM `users` WHERE `lastdate` > '" . (time() - 300) . "'"), 0);
echo ' <a href="' . $home . '/users/index.php?act=online">Hozir saytda</a> (' . $hsaytda . ')</div>';
echo '<div class="list1"><a href="/users/profile.php?act=activity&user=' . $user['id'] . '"><img src="' . $home . '/images/profil/komm.png"> ' . ($user['id'] != $user_id ? 'Aktivligi' : 'Aktivligim') . '</a></div>';

include ('users/guests.php');
if ($user['id'] == $user_id) {
echo '<div class="list2"><a href="/users/rsearch.php"><img src="' . $home . '/images/profil/poisk.png"> Yangi do\'stlar topish </a></div>';
}
if ($user['id'] == $user_id) {
echo '<div class="list1"><a href="/users/faol.php"><img src="' . $home . '/images/rate.gif"> Saytimiz faollari</a></div>';
}
if ($user['id'] == $user_id || $rights == 9 || ($rights == 7 && $rights > $user['rights'])) {
echo '<div class="menu"><a href="/users/profile.php?act=edit&amp;user=' . $user['id'] . '"><img src="' . $home . '/images/profil/nastroyka.png"> Anketani o\'zgartirish</a></div>';
}
if ($user['id'] == $user_id || $rights == 9 || ($rights == 7 && $rights > $user['rights'])) {
echo '<div class="menu"><a href="/users/profile.php?act=password&amp;user=' . $user['id'] . '"><img src="' . $home . '/images/profil/nastroyka.png"> Parolni o\'zgartirish</a></div>';
} 
 if ($user['id'] == $user_id)
{
echo '<div class="menu"><a href="/users/profile.php?act=settings&mod=mail"><img src="' . $home . '/images/profil/nastroyka.png"> Porchta sozlamalari</a></div>';
}
if ($user['id'] == $user_id)
{
echo '<div class="menu"><a href="/users/profile.php?act=settings"><img src="' . $home . '/images/profil/nastroyka.png"> Sozlamalar</a></div>';
}
echo '</div>';
echo '<table width="100%"><tr>';
 if ($user['id'] != $user_id) {
 if (functions::is_contact($user['id']) != 2) {
            if (!functions::is_contact($user['id'])) {
echo '<td class="list1" width="50%"><img src="/images/users.png" width="16" height="16"/>&#160;<a href="/mail/index.php?id=' . $user['id'] . '"><font color="green">Kontaktga qo\'shish</font></a></td>';
            } else {
echo '<td class="list1" width="50%"><img src="/images/users.png" width="16" height="16"/>&#160;<a href="/mail/index.php?act=deluser&amp;id=' . $user['id'] . '"><font color="red">Kontakdan olish</font></a></td>';
}
}
        if (functions::is_contact($user['id']) != 2) {
echo '<td class="list2"><img src="/images/del.png" width="16" height="16"/>&#160;<a href="/mail/index.php?act=ignor&amp;id=' . $user['id'] . '&amp;add"><font color="red">Qora ro\'yxatga</font></a></td>';
        } else {
echo '<td class="list2"><img src="/images/del.png" width="16" height="16"/>&#160;<a href="/mail/index.php?act=ignor&amp;id=' . $user['id'] . '&amp;del"><font color="green">Qora ro\'yxatdan olish</font></a></td>';
        }
}
echo '</tr></table>';
$count_ignor = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_contact`
WHERE `user_id`='" . $user_id . "' AND `ban`='1';"), 0);
if ($user['id'] == $user_id) {
echo '<div class="menu"><a href="/mail/index.php?act=ignor"><img src="/images/profil/qora.png"> Qora ro\'yxatdagilar</a>&nbsp;(' . $count_ignor . ')</div>';
}


if ($user['id'] != $user_id && $rights >= 7 && $rights > $user['rights']) {
echo '<div class="rmenu">';
echo '<a href="' . $set['homeurl'] . '/' . $set['admp'] . '/index.php?act=usr_del&amp;id=' . $user['id'] . '"> | ' . $lng['delete'] . '</a>';
    }
    if ($user['id'] != $user_id && $rights > $user['rights']) {
echo '<a href="profile.php?act=ban&amp;mod=do&amp;user=' . $user['id'] . '"> | ' . $lng['ban_do'] . '</a>';
}
echo '</div>';
}

}


