<?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',
    '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']);
    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 == 9 && $rights > $user['rights'])) {
        $menu[] = '<a href="profile.php?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.php?act=usr_del&amp;id=' . $user['id'] . '">' . $lng['delete'] . '</a>';
    }
    if ($user['id'] != $user_id && $rights > $user['rights']) {
        $menu[] = '<a href="profile.php?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>';
    }

    // Информация о юзере
    $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>';
    }
	
	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'<div class="phdr">'.$lng['information'].'</div>';
    echo'<div class="list2">';	    
    echo'<div class="info_user"><i class="fa fa-user"></i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$lng_profile['name'].':&nbsp;&nbsp;&nbsp;'.$user['imname'].' <span class="crimson"> (ID: '.$user['id'].')</span><p>'.
		'	                    <i class="fa fa-globe"></i>&nbsp;&nbsp;&nbsp;&nbsp;'.$lng_profile['city'].':&nbsp;&nbsp;'.$user['live'].'<p>'.
		'                       <i class="fa fa-calendar"></i>&nbsp;&nbsp;&nbsp;&nbsp;' . ($user['sex'] == 'm' ? $lng_profile['registered_m'] : $lng_profile['registered_w']) . ':&nbsp;&nbsp;' . date("d.m.Y", $user['datereg']) . '<p>'.
		'                       <i class="fa fa-calendar-o"></i>&nbsp;&nbsp;&nbsp;&nbsp;'.$lng_profile['birt'].':&nbsp;&nbsp;'.$user['dayb'].'.'.$user['monthb'].'.'.$user['yearofbirth'].'&nbsp;&nbsp;('.$user_age.' '.$lng_profile['age'].')</div>';
	echo'</div>';
$comments = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news_comments` WHERE `user_id` = '".$user['id']."'"), 0);
	echo'<div class="phdr">'.$lng['statistics'].'</div>';
    echo'<div class="list2">';	
		$award = mysql_result(mysql_query("select count(*) from `award_users` where `id_user` = '".$user['id']."'"),0);
	echo'<div class="info_user">&nbsp;<i class="fa fa-gift"></i>&nbsp;&nbsp;&nbsp;<a href="../award/index.php?id=' . $user['id'] . '">'.$lng['awards'].'</a>:&nbsp;&nbsp;'.$award.'';
if ($rights==9) echo'<a href="../award/panel.php?id=' . $user['id'] . '" style="font-size: 14px;"> 
 ['.$lng['rewarding'].']</a>';
echo'<p>';	
	echo'<i class="fa fa-commenting-o"></i>&nbsp;&nbsp;&nbsp;&nbsp;'.$lng['comments'].':&nbsp;&nbsp;'.$comments.'<p>'.
					     '&nbsp;<i class="fa fa-map-marker"></i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . ($user['sex'] == 'm' ? $lng_profile['stayed_m'] : $lng_profile['stayed_w']) . ':&nbsp;&nbsp;' . functions::timecount($user['total_on_site']) . '<p>'.
						 '&nbsp;<i class="fa fa-money"></i>&nbsp;&nbsp;&nbsp;'.$lng['balance'].':&nbsp;&nbsp;'.$user['balans'].'';
						 if ($user['id'] != $user_id)echo'<a href="perevod.php?id=' . $user['id'] . '"> ['.$lng['send'].']</a>';

$req = mysql_query("SELECT `fanclub_vote`.`id`as `vid`, `fanclub`.`id` FROM `fanclub_vote` LEFT JOIN `fanclub` ON `fanclub_vote`.`cid`=`fanclub`.`id` WHERE `fanclub_vote`.`user_id`='" . $user['id'] . "' AND `fanclub_vote`.`count`='1' LIMIT 0,5 ");
    echo '<p>&nbsp;<i class="fa fa-smile-o"></i>&nbsp;&nbsp;&nbsp;&nbsp;'.$lng['fan_plus'].': ';
    while ($res = mysql_fetch_array($req)) {
        echo '<a href="../fanclub/titul_club.php?id=' . $res['id'] . '"><img src="' . $home . '/fanclub/img/' . $res['id'] . '_small.png" width="20" height="20" align="middle" alt="" border="0" style="vertical-align: -2px;"/></a>&nbsp;';
        ++$i;
    }
    $req = mysql_query("SELECT `fanclub_vote`.`id`as `vid`, `fanclub`.`id` FROM `fanclub_vote` LEFT JOIN `fanclub` ON `fanclub_vote`.`cid`=`fanclub`.`id` WHERE `fanclub_vote`.`user_id`='" . $user['id'] . "' AND `fanclub_vote`.`count`='0' LIMIT 0,5 ");
    echo '<p>&nbsp;<i class="fa fa-frown-o"></i>&nbsp;&nbsp;&nbsp;&nbsp;'.$lng['fan_minus'].': ';
    while ($res = mysql_fetch_array($req)) {
        echo '<a href="../fanclub/titul_club.php?id=' . $res['id'] . '"><img src="' . $home . '/fanclub/img/' . $res['id'] . '_small.png" width="20" height="20" align="middle" alt="" border="0"style="vertical-align: -2px;"/></a>&nbsp;';
        ++$i;
    }
						 echo'</div>';
    // Меню выбора
    $total_photo = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_album_files` WHERE `user_id` = '" . $user['id'] . "'"), 0);
	echo'<div class="phdr">'.$lng_profile['activity'].'</div>';
    echo'<div class="list2">';
    $bancount = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_ban_users` WHERE `user_id` = '" . $user['id'] . "'"), 0);

    if ($bancount) {
        echo '<a href="profile.php?act=ban&amp;user=' . $user['id'] . '"><i class="fa fa-ban"></i>&#160;&#160;' . $lng['infringements'] . '</a>&#160;(' . $bancount . ')<p>';
    }

    $total_friends = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_contact` WHERE `user_id`='{$user['id']}' AND `type`='2' AND `friends`='1'"), 0);
    echo'<a href="album.php?act=list&amp;user=' . $user['id'] . '"><i class="fa fa-camera-retro"></i>&#160;&#160;' . $lng['photo_album'] . '</a>&#160;(' . $total_photo . ')<p>' .
        '<a href="profile.php?act=guestbook&amp;user=' . $user['id'] . '"><i class="fa fa-pencil-square-o"></i>&#160;&#160;' . $lng['guestbook'] . '</a>&#160;(' . $user['comm_count'] . ')<p>' .
        '<a href="profile.php?act=friends&amp;user=' . $user['id'] . '"><i class="fa fa-users"></i>&#160;&#160;' . $lng_profile['friends'] . '</a>&#160;(' . $total_friends . ')<p>';
    if ($user['id'] != $user_id) {
        // Контакты
        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.php?act=friends&amp;do=ok&amp;id=' . $user['id'] . '">&#160;<i class="fa fa-plus-square-o"></i>&#160;&#160;' . $lng_profile['confirm_friendship'] . '</a> | <a class="underline" href="profile.php?act=friends&amp;do=no&amp;id=' . $user['id'] . '"><i class="fa fa-times-circle-o"></i>&#160;&#160;' . $lng_profile['decline_friendship'] . '</a>';
                } else if ($fr_out == 1) {
                    $friend = '<a class="underline" href="profile.php?act=friends&amp;do=cancel&amp;id=' . $user['id'] . '"><i class="fa fa-times-circle-o"></i>&#160;&#160;' . $lng_profile['canceled_demand_friend'] . '</a>';
                } else {
                    $friend = '<a href="profile.php?act=friends&amp;do=add&amp;id=' . $user['id'] . '"><i class="fa fa-plus-square-o"></i>&#160;&#160;&#160;' . $lng_profile['in_friend'] . '</a>';
                }
            } else {
                $friend = '<a href="profile.php?act=friends&amp;do=delete&amp;id=' . $user['id'] . '"><i class="fa fa-times-circle-o"></i>&#160;&#160;' . $lng_profile['remov_friend'] . '</a>';
            }
            echo  ''.$friend.'<p>';
        }

        if (functions::is_contact($user['id']) != 2) {
            if (!functions::is_contact($user['id'])) {
                echo '<a href="../mail/index.php?id=' . $user['id'] . '"><i class="fa fa-address-book-o"></i>&#160;&#160;' . $lng_profile['add_contacts'] . '</a><p>';
            } else {
                echo '<a href="../mail/index.php?act=deluser&amp;id=' . $user['id'] . '"><i class="fa fa-times-circle-o"></i>&#160;&#160;' . $lng_profile['delete_contacts'] . '</a><p>';
            }
        }

        if (functions::is_contact($user['id']) != 2) {
            echo '<a href="../mail/index.php?act=ignor&amp;id=' . $user['id'] . '&amp;add"><i class="fa fa-lock"></i>&#160;&#160;&#160;' . $lng_profile['add_ignor'] . '</a><p>';
        } else {
            echo '<a href="../mail/index.php?act=ignor&amp;id=' . $user['id'] . '&amp;del"><i class="fa fa-unlock-alt"></i>&#160;&#160;&#160;' . $lng_profile['delete_ignor'] . '</a><p>';
        }

        if (!functions::is_ignor($user['id']) && functions::is_contact($user['id']) != 2 && empty($ban['1']) && empty($ban['3'])) {
            echo '<p><form action="../mail/index.php?act=write&amp;id=' . $user['id'] . '" method="post"><input type="submit" class="write" value="' . $lng['write'] . '"/></form></p>';
        }
        
    }
    echo '</div>';
	echo '<div class="phdr"><a href="index.php">' . $lng['users'] . '</a></div>';
}

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