<?php

define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

$userdata = session_pagestart($user_ip, PAGE_CABINET);
init_userprefs($userdata);

if (!empty($board_config['index_announcement']))
{
	include($phpbb_root_path . 'includes/bbcode.'.$phpEx);
}

require($phpbb_root_path . 'Date/DeltaRussian.'.$phpEx);

$sql = "SELECT cat_id, cat_title
	FROM " . MEDAL_CAT_TABLE . "
	ORDER BY cat_order";
if( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, 'Could not query medal categories list', '', __LINE__, __FILE__, $sql);
}

$category_rows = array();
while ( $row = $db->sql_fetchrow($result) )
{
	$category_rows[] = $row;
}
$db->sql_freeresult($result);

$sql = "SELECT m.medal_id, mu.user_id
	FROM " . MEDAL_TABLE . " m, " . MEDAL_USER_TABLE . " mu
	WHERE mu.user_id = '" . $userdata['user_id'] . "'
	AND m.medal_id = mu.medal_id
	ORDER BY m.medal_name";
	
if($result = $db->sql_query($sql))
{
	$medal_list = $db->sql_fetchrowset($result);
	$medal_count = count($medal_list);

	if ( $medal_count )
	{
		$template->assign_block_vars('switch_display_medal', array());
		$template->assign_block_vars('switch_display_medal.medal', array(
			'MEDAL_BUTTON' => '<a href="' . append_sid("profile.$phpEx?mode=medals&amp;" . POST_USERS_URL . "=" . $userdata['user_id']) . '">подробности</a>')
		);
	}
}

for ($i = 0; $i < count($category_rows); $i++)
{
	$cat_id = $category_rows[$i]['cat_id'];

	$sql = "SELECT m.medal_id, m.medal_name,m.medal_description, m.medal_image, m.cat_id, mu.issue_reason, mu.issue_time, c.cat_id, c.cat_title
		FROM " . MEDAL_TABLE . " m, " . MEDAL_USER_TABLE . " mu, " . MEDAL_CAT_TABLE . " c
		WHERE mu.user_id = '" . $userdata['user_id'] . "'
		AND m.cat_id = c.cat_id
		AND m.medal_id = mu.medal_id
		ORDER BY c.cat_order, m.medal_name, mu.issue_time";

	if ($result = $db->sql_query($sql))
	{
		$row = array();
		$rowset = array();
		$medal_time = $lang['Medal_time'] . ': ';
		$medal_reason = $lang['Medal_reason'] . ': ';
		while ($row = $db->sql_fetchrow($result))
		{
			if (empty($rowset[$row['medal_name']]))
			{
				$rowset[$row['medal_name']]['cat_id'] = $row['cat_id'];
				$rowset[$row['medal_name']]['cat_title'] = $row['cat_title'];
				$rowset[$row['medal_name']]['medal_description'] .= $row['medal_description'];
				$rowset[$row['medal_name']]['medal_image'] = $row['medal_image'];
				$row['issue_reason'] = ( $row['issue_reason'] ) ? $row['issue_reason'] : $lang['Medal_no_reason'];
				$rowset[$row['medal_name']]['medal_issue'] = $medal_time . create_date($board_config['default_dateformat'], $row['issue_time'], $board_config['board_timezone']) . '<br/>' . $medal_reason . $row['issue_reason'] . '<br/>';
				$rowset[$row['medal_name']]['medal_count'] = '1';
			}
			else
			{
				$row['issue_reason'] = ( $row['issue_reason'] ) ? $row['issue_reason'] : $lang['Medal_no_reason'];
				$rowset[$row['medal_name']]['medal_issue'] .= $medal_time . create_date($board_config['default_dateformat'], $row['issue_time'], $board_config['board_timezone']) . '<br/>' . $medal_reason . $row['issue_reason'] . '<br/>';
				$rowset[$row['medal_name']]['medal_count'] += '1';
			}
		}

		$medal_name = array();
		$data = array();

		$display_medal = 0;

		while (list($medal_name, $data) = @each($rowset))
		{
			if ( $cat_id == $data['cat_id'] ) { $display_medal = 1; }

			if ( !empty($display_medal) )
			{
				$template->assign_block_vars('switch_display_medal.details', array(
					'MEDAL_CAT' => $data['cat_title'],
					'MEDAL_NAME' => $medal_name,
					'MEDAL_DESCRIPTION' => $data['medal_description'],
					'MEDAL_IMAGE' => '<img src="'. $phpbb_root_path . $data['medal_image'] . '" alt="" />',
					'MEDAL_ISSUE' => $data['medal_issue'],
					'MEDAL_COUNT' => $data['medal_count'],
					'L_MEDAL_DESCRIPTION' => $lang['Medal_description'])
				);
				$display_medal = 0;
			}
		}
	}
}

$sql = 'SELECT count(*) AS total
	FROM ' . ATTACHMENTS_TABLE . '
	WHERE user_id_1 = '.$userdata['user_id'].' AND user_id_2 = 0';
$result = $db->sql_query($sql);
if (!$result)
{
	message_die(GENERAL_ERROR, 'Unable to get attachment information.', '', __LINE__, __FILE__, $sql);
}
$rowatt = $db->sql_fetchrow($result);
if ( $rowatt['total'] > 0 )
{
	$totalfiles = '<a href="' . append_sid("profile.$phpEx?mode=viewfiles&amp;" . POST_USERS_URL .'=' . $userdata['user_id']) . '">' . $rowatt['total'] . '</a>';
} else {
	$totalfiles = 0;
}

$regdate = $userdata['user_regdate'];
$memberdays = max(1, round( ( time() - $regdate ) / 86400 ));
$posts_per_day = $userdata['user_posts'] / $memberdays;

if ( $userdata['user_posts'] != 0  )
{
	$total_posts = get_db_stat('postcount');
	$percentage = ( $total_posts ) ? min(100, ($userdata['user_posts'] / $total_posts) * 100) : 0;
}
else
{
	$percentage = 0;
}

$avatar_img = '<b>'.$lang['No_avatar'].'</b>';
if ( $userdata['user_avatar_type'] && $userdata['user_allowavatar'] )
{
	switch( $userdata['user_avatar_type'] )
	{
		case USER_AVATAR_UPLOAD:
			$avatar_img = ( $board_config['allow_avatar_upload'] ) ? '<img src="' . $board_config['avatar_path'] . '/' . $userdata['user_avatar'] . '" alt="" border="0" />' : '';
			break;
		case USER_AVATAR_REMOTE:
			$avatar_img = ( $board_config['allow_avatar_remote'] ) ? '<img src="' . $userdata['user_avatar'] . '" alt="" border="0" />' : '';
			break;
		case USER_AVATAR_GALLERY:
			$avatar_img = ( $board_config['allow_avatar_local'] ) ? '<img src="' . $board_config['avatar_gallery_path'] . '/' . $userdata['user_avatar'] . '" alt="" border="0" />' : '';
			break;
	}
}


$page_title = $lang['Cabinet'] . ' ' . $userdata['username'];
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
display_upload_attach_box_limits($userdata['user_id']);

$template->set_filenames(array(
	'body' => 'cabinet.tpl')
);

if (function_exists('get_html_translation_table'))
{
	$u_search_author = urlencode(strtr($userdata['username'], array_flip(get_html_translation_table(HTML_ENTITIES))));
}
else
{
	$u_search_author = urlencode(str_replace(array('&amp;', '&#039;', '&quot;', '&lt;', '&gt;'), array('&', "'", '"', '<', '>'), $userdata['username']));
}
if ($board_config['warnings_enabled'] || $board_config['reputation_enabled'])
{
	include($phpbb_root_path . 'includes/functions_reputation.' . $phpEx);

	$is_auth = reputation_auth(NO_ID, $userdata, $userdata);

	if ($board_config['warnings_enabled'] && !$is_auth['no_warn'])
	{
		$warn_img = $warn = $ban_img = $ban = '';

		if ($is_auth['auth_warn'])
		{
			$temp_url = "reputation.$phpEx?mode=warning&amp;" . POST_USERS_URL . "=" . $userdata['user_id'] . "&amp;sid=" . $userdata['session_id'];
			$warn_img = '<a href="' . $temp_url . '">' . $lang['reputation_warn_user'] . '</a><br/>';
			$warn = '<a href="' . $temp_url . '">' . $lang['reputation_warn'] . '</a>';
		}
		if ($is_auth['auth_ban'])
		{
			$temp_url = "reputation.$phpEx?mode=ban&amp;" . POST_USERS_URL . "=" . $userdata['user_id'] . "&amp;sid=" . $userdata['session_id'];
			$ban_img = '<a href="' . $temp_url . '">' . $lang['reputation_ban_user'] . '</a><br/>';
			$ban = '<a href="' . $temp_url . '">' . $lang['reputation_ban'] . '</a>';
		}

		$template->assign_block_vars('warnings', array(
			'L_WARNINGS' => $lang['Warnings'],
			'WARNINGS' => $userdata['user_warnings'],

			'RED' => $ban,
			'RED_IMG' => $ban_img,
			'YELLOW' => $warn,
			'YELLOW_IMG' => $warn_img,
		));

		if ($is_auth['auth_view_warns'] && $userdata['user_warnings'])
		{
			$template->assign_block_vars('warnings.details', array(
				'U_SEARCH' => append_sid("profile_warnings_" . $userdata['user_id'].".html"),
				'L_SEARCH' => sprintf($lang['reputation_search_warnings'], $userdata['username']),
			));
		}
	}
	if ($board_config['reputation_enabled'] && !$is_auth['no_rep'])
	{
		if ( ($board_config['reputation_display'] == REPUTATION_PLUSMINUS) && ((($userdata['user_reputation_plus'] - $userdata['user_reputation']) + $userdata['user_reputation_plus']) > 0) )
		{
			$poloska = @round(($userdata['user_reputation_plus'])/(($userdata['user_reputation_plus'] - $userdata['user_reputation']) + $userdata['user_reputation_plus'])*100,0);
			$poloska = '<img src="images/rate.php?i=' . $poloska . '" alt=""><br/>';
		} else {
			$poloska = '';
		}
		$template->assign_block_vars('reputation', array(
			'L_REPUTATION' => $lang['Reputation'],
			'POLOSKA' => $poloska,
			'U_VIEW_REPUTATION' => append_sid("profile.$phpEx?mode=reputation&amp;" . POST_USERS_URL . '=' . $userdata['user_id']),
			'REPUTATION' => reputation_display($userdata, $is_auth, false),
		));

		if ($is_auth['auth_view_rep'] && ($userdata['user_reputation'] || $userdata['user_reputation_plus']))
		{
			$template->assign_block_vars('reputation.details', array(
				'U_SEARCH' => append_sid("profile_reputation_" . $userdata['user_id']. ".html"),
				'L_SEARCH' => sprintf($lang['reputation_search_reputation'], $userdata['username']),
			));
		}
	}
}

$sql = "SELECT u.user_id, u.username, u.user_level, g.group_id, g.group_name, g.group_single_user, ug.user_pending
	FROM " . USERS_TABLE . " u, " . GROUPS_TABLE . " g, " . USER_GROUP_TABLE . " ug 
	WHERE u.user_id = " . $userdata['user_id'] . " AND ug.user_id = u.user_id AND g.group_id = ug.group_id";
if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, "Couldn't obtain user/group information", "", __LINE__, __FILE__, $sql);
}
$ug_info = array();
while( $row = $db->sql_fetchrow($result) )
{
	$ug_info[] = $row;
}
$db->sql_freeresult($result);

$name = array();
$id = array();
for($i = 0; $i < count($ug_info); $i++)
{
	if( !$ug_info[$i]['group_single_user'] )
	{
		$name[] = $ug_info[$i]['group_name'];
		$id[] = intval($ug_info[$i]['group_id']);
	}
}

$t_usergroup_list = '';
if( count($name) )
{
	for($i = 0; $i < (count($ug_info) - 1); $i++)
	{
		if (!$ug_info[$i]['user_pending'])
		{
			$t_usergroup_list .= '<a href="' . append_sid("groupcp.$phpEx?g=" . $id[$i]) . '">' . $name[$i] . '</a><br/>';
		}
	}
}



$sql = 'SELECT count(*) AS total
	FROM ' . ALBUM_TABLE . '
	WHERE pic_user_id = '.$userdata['user_id'].'';
$result = $db->sql_query($sql);
if (!$result)
{
	message_die(GENERAL_ERROR, 'Unable to get attachment information.', '', __LINE__, __FILE__, $sql);
}
$rowfoto = $db->sql_fetchrow($result);
if ( $rowfoto['total'] > 0 )
{
	$totalfoto = '' . $rowfoto['total'] . '';
} else {
	$totalfoto = 0;
}
$template->assign_vars(array(
	'COLOR' => $userdata['user_nic_color'],
	'USERID' => $userdata['user_id'],
	'USER_AGENT' => $user_agent,
	'USER_IP' => $client_ip,
	'USERNAME' => $userdata['username'],
	'USERGROUP' => $t_usergroup_list,
	'JOINED' => create_date($lang['DATE_FORMAT'], $userdata['user_regdate'], $board_config['board_timezone']),
	'STAJ' => ($dM = new Date_DeltaRussian("mday")) ? $dM->spellDelta($userdata['user_regdate'], time() ) : '',
	'L_USER_MEDAL' =>$lang['Medals'],
	'USER_MEDAL_COUNT' => $medal_count,
	'L_MEDAL_INFORMATION' => $lang['Medal_Information'],
	'L_MEDAL_NAME' => $lang['Medal_name'],
	'L_MEDAL_DETAIL' => $lang['Medal_details'],
	'POSTS' => $userdata['user_posts'],
	'CHAT_POSTS' => $userdata['chat_posts'],
	'MONEY' => $userdata['user_points'],
	'ATTACH' => $totalfiles,

	'SEARCH' => $search,
	'AVATAR_IMG' => $avatar_img,
	'L_NAME_CABINET' => $lang['Welcome_cabinet'],
	'L_JOINED' => $lang['Joined'], 
	'L_SEARCH_USER_POSTS' => sprintf($lang['Search_user_posts'], $userdata['username']), 
	'U_EDITPROFILE' => '- <a href="'.append_sid('profile.'.$phpEx.'?mode=editprofile').'">'.$lang['Edit_Prorile_Reg'].'</a>',
	'U_EDITSTYLE' => '- <a href="'.append_sid('profile.'.$phpEx.'?mode=editstyle').'">'.$lang['Edit_Prorile_Style'].'</a>',
	'U_EDITCONFIG' => '- <a href="'.append_sid('profile.'.$phpEx.'?mode=editconfig').'">'.$lang['Edit_Prorile_Config'].'</a>',
	'U_EDITPROFILEINFO' => '- <a href="'.append_sid('profile.'.$phpEx.'?mode=editprofileinfo').'">'.$lang['Edit_Prorile_Info'].'</a>',

	'U_PERSONAL_GALLERY' => append_sid("album.$phpEx?action=personal&amp;user_id=" . $userdata['user_id']),
	'L_PERSONAL_GALLERY' => sprintf($lang['Personal_Gallery_Of_User'], $userdata['username']),

	'S_PROFILE_ACTION' => append_sid("profile.$phpEx"))
);

if ( $userdata['user_id'] == $userdata['user_id'] )
{
	$template->assign_block_vars('editprofile', array() );
}
if ( $userdata['user_points'] > 0 && $userdata['user_id'] != $userdata['user_id'] )
{
	$template->assign_block_vars('money', array() );
}
if ( $t_usergroup_list != '' )
{
	$template->assign_block_vars('usergroup', array() );
}

$template->pparse('body');

include($phpbb_root_path . 'includes/page_tail.'.$phpEx);

?>