includes/functions_display.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 00:18:56 +0400
changeset 15 8804e4d3de74
parent 0 ceef6d64c21d
permissions -rw-r--r--
Added online gamers
     1 <?php
     2 /**
     3 *
     4 * @package phpBB3
     5 * @version $Id$
     6 * @copyright (c) 2005 phpBB Group
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     8 *
     9 */
    10 
    11 /**
    12 * @ignore
    13 */
    14 if (!defined('IN_PHPBB'))
    15 {
    16 	exit;
    17 }
    18 
    19 /**
    20 * Display Forums
    21 */
    22 function display_forums($root_data = '', $display_moderators = true, $return_moderators = false)
    23 {
    24 	global $db, $auth, $user, $template;
    25 	global $phpbb_root_path, $phpEx, $config;
    26 
    27 	$forum_rows = $subforums = $forum_ids = $forum_ids_moderator = $forum_moderators = $active_forum_ary = array();
    28 	$parent_id = $visible_forums = 0;
    29 	$sql_from = '';
    30 
    31 	// Mark forums read?
    32 	$mark_read = request_var('mark', '');
    33 
    34 	if ($mark_read == 'all')
    35 	{
    36 		$mark_read = '';
    37 	}
    38 
    39 	if (!$root_data)
    40 	{
    41 		if ($mark_read == 'forums')
    42 		{
    43 			$mark_read = 'all';
    44 		}
    45 
    46 		$root_data = array('forum_id' => 0);
    47 		$sql_where = '';
    48 	}
    49 	else
    50 	{
    51 		$sql_where = 'left_id > ' . $root_data['left_id'] . ' AND left_id < ' . $root_data['right_id'];
    52 	}
    53 
    54 	// Handle marking everything read
    55 	if ($mark_read == 'all')
    56 	{
    57 		$redirect = build_url(array('mark', 'hash'));
    58 		meta_refresh(3, $redirect);
    59 
    60 		if (check_link_hash(request_var('hash', ''), 'global'))
    61 		{
    62 			markread('all');
    63 
    64 			trigger_error(
    65 				$user->lang['FORUMS_MARKED'] . '<br /><br />' .
    66 				sprintf($user->lang['RETURN_INDEX'], '<a href="' . $redirect . '">', '</a>')
    67 			);
    68 		}
    69 		else
    70 		{
    71 			trigger_error(sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>'));
    72 		}
    73 	}
    74 
    75 	// Display list of active topics for this category?
    76 	$show_active = (isset($root_data['forum_flags']) && ($root_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS)) ? true : false;
    77 
    78 	$sql_array = array(
    79 		'SELECT'	=> 'f.*',
    80 		'FROM'		=> array(
    81 			FORUMS_TABLE		=> 'f'
    82 		),
    83 		'LEFT_JOIN'	=> array(),
    84 	);
    85 
    86 	if ($config['load_db_lastread'] && $user->data['is_registered'])
    87 	{
    88 		$sql_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TRACK_TABLE => 'ft'), 'ON' => 'ft.user_id = ' . $user->data['user_id'] . ' AND ft.forum_id = f.forum_id');
    89 		$sql_array['SELECT'] .= ', ft.mark_time';
    90 	}
    91 	else if ($config['load_anon_lastread'] || $user->data['is_registered'])
    92 	{
    93 		$tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : '';
    94 		$tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();
    95 
    96 		if (!$user->data['is_registered'])
    97 		{
    98 			$user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
    99 		}
   100 	}
   101 
   102 	if ($show_active)
   103 	{
   104 		$sql_array['LEFT_JOIN'][] = array(
   105 			'FROM'	=> array(FORUMS_ACCESS_TABLE => 'fa'),
   106 			'ON'	=> "fa.forum_id = f.forum_id AND fa.session_id = '" . $db->sql_escape($user->session_id) . "'"
   107 		);
   108 
   109 		$sql_array['SELECT'] .= ', fa.user_id';
   110 	}
   111 
   112 	$sql = $db->sql_build_query('SELECT', array(
   113 		'SELECT'	=> $sql_array['SELECT'],
   114 		'FROM'		=> $sql_array['FROM'],
   115 		'LEFT_JOIN'	=> $sql_array['LEFT_JOIN'],
   116 
   117 		'WHERE'		=> $sql_where,
   118 
   119 		'ORDER_BY'	=> 'f.left_id',
   120 	));
   121 
   122 	$result = $db->sql_query($sql);
   123 
   124 	$forum_tracking_info = array();
   125 	$branch_root_id = $root_data['forum_id'];
   126 
   127 	// Check for unread global announcements (index page only)
   128 	$ga_unread = false;
   129 	if ($root_data['forum_id'] == 0)
   130 	{
   131 		$unread_ga_list = get_unread_topics($user->data['user_id'], 'AND t.forum_id = 0', '', 1);
   132 
   133 		if (!empty($unread_ga_list))
   134 		{
   135 			$ga_unread = true;
   136 		}
   137 	}
   138 
   139 	while ($row = $db->sql_fetchrow($result))
   140 	{
   141 		$forum_id = $row['forum_id'];
   142 
   143 		// Mark forums read?
   144 		if ($mark_read == 'forums')
   145 		{
   146 			if ($auth->acl_get('f_list', $forum_id))
   147 			{
   148 				$forum_ids[] = $forum_id;
   149 			}
   150 
   151 			continue;
   152 		}
   153 
   154 		// Category with no members
   155 		if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
   156 		{
   157 			continue;
   158 		}
   159 
   160 		// Skip branch
   161 		if (isset($right_id))
   162 		{
   163 			if ($row['left_id'] < $right_id)
   164 			{
   165 				continue;
   166 			}
   167 			unset($right_id);
   168 		}
   169 
   170 		if (!$auth->acl_get('f_list', $forum_id))
   171 		{
   172 			// if the user does not have permissions to list this forum, skip everything until next branch
   173 			$right_id = $row['right_id'];
   174 			continue;
   175 		}
   176 
   177 		if ($config['load_db_lastread'] && $user->data['is_registered'])
   178 		{
   179 			$forum_tracking_info[$forum_id] = (!empty($row['mark_time'])) ? $row['mark_time'] : $user->data['user_lastmark'];
   180 		}
   181 		else if ($config['load_anon_lastread'] || $user->data['is_registered'])
   182 		{
   183 			if (!$user->data['is_registered'])
   184 			{
   185 				$user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
   186 			}
   187 			$forum_tracking_info[$forum_id] = (isset($tracking_topics['f'][$forum_id])) ? (int) (base_convert($tracking_topics['f'][$forum_id], 36, 10) + $config['board_startdate']) : $user->data['user_lastmark'];
   188 		}
   189 
   190 		// Count the difference of real to public topics, so we can display an information to moderators
   191 		$row['forum_id_unapproved_topics'] = ($auth->acl_get('m_approve', $forum_id) && ($row['forum_topics_real'] != $row['forum_topics'])) ? $forum_id : 0;
   192 		$row['forum_topics'] = ($auth->acl_get('m_approve', $forum_id)) ? $row['forum_topics_real'] : $row['forum_topics'];
   193 
   194 		// Display active topics from this forum?
   195 		if ($show_active && $row['forum_type'] == FORUM_POST && $auth->acl_get('f_read', $forum_id) && ($row['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS))
   196 		{
   197 			if (!isset($active_forum_ary['forum_topics']))
   198 			{
   199 				$active_forum_ary['forum_topics'] = 0;
   200 			}
   201 
   202 			if (!isset($active_forum_ary['forum_posts']))
   203 			{
   204 				$active_forum_ary['forum_posts'] = 0;
   205 			}
   206 
   207 			$active_forum_ary['forum_id'][]		= $forum_id;
   208 			$active_forum_ary['enable_icons'][]	= $row['enable_icons'];
   209 			$active_forum_ary['forum_topics']	+= $row['forum_topics'];
   210 			$active_forum_ary['forum_posts']	+= $row['forum_posts'];
   211 
   212 			// If this is a passworded forum we do not show active topics from it if the user is not authorised to view it...
   213 			if ($row['forum_password'] && $row['user_id'] != $user->data['user_id'])
   214 			{
   215 				$active_forum_ary['exclude_forum_id'][] = $forum_id;
   216 			}
   217 		}
   218 
   219 		//
   220 		if ($row['parent_id'] == $root_data['forum_id'] || $row['parent_id'] == $branch_root_id)
   221 		{
   222 			if ($row['forum_type'] != FORUM_CAT)
   223 			{
   224 				$forum_ids_moderator[] = (int) $forum_id;
   225 			}
   226 
   227 			// Direct child of current branch
   228 			$parent_id = $forum_id;
   229 			$forum_rows[$forum_id] = $row;
   230 
   231 			if ($row['forum_type'] == FORUM_CAT && $row['parent_id'] == $root_data['forum_id'])
   232 			{
   233 				$branch_root_id = $forum_id;
   234 			}
   235 			$forum_rows[$parent_id]['forum_id_last_post'] = $row['forum_id'];
   236 			$forum_rows[$parent_id]['orig_forum_last_post_time'] = $row['forum_last_post_time'];
   237 		}
   238 		else if ($row['forum_type'] != FORUM_CAT)
   239 		{
   240 			$subforums[$parent_id][$forum_id]['display'] = ($row['display_on_index']) ? true : false;
   241 			$subforums[$parent_id][$forum_id]['name'] = $row['forum_name'];
   242 			$subforums[$parent_id][$forum_id]['orig_forum_last_post_time'] = $row['forum_last_post_time'];
   243 			$subforums[$parent_id][$forum_id]['children'] = array();
   244 
   245 			if (isset($subforums[$parent_id][$row['parent_id']]) && !$row['display_on_index'])
   246 			{
   247 				$subforums[$parent_id][$row['parent_id']]['children'][] = $forum_id;
   248 			}
   249 
   250 			if (!$forum_rows[$parent_id]['forum_id_unapproved_topics'] && $row['forum_id_unapproved_topics'])
   251 			{
   252 				$forum_rows[$parent_id]['forum_id_unapproved_topics'] = $forum_id;
   253 			}
   254 
   255 			$forum_rows[$parent_id]['forum_topics'] += $row['forum_topics'];
   256 
   257 			// Do not list redirects in LINK Forums as Posts.
   258 			if ($row['forum_type'] != FORUM_LINK)
   259 			{
   260 				$forum_rows[$parent_id]['forum_posts'] += $row['forum_posts'];
   261 			}
   262 
   263 			if ($row['forum_last_post_time'] > $forum_rows[$parent_id]['forum_last_post_time'])
   264 			{
   265 				$forum_rows[$parent_id]['forum_last_post_id'] = $row['forum_last_post_id'];
   266 				$forum_rows[$parent_id]['forum_last_post_subject'] = $row['forum_last_post_subject'];
   267 				$forum_rows[$parent_id]['forum_last_post_time'] = $row['forum_last_post_time'];
   268 				$forum_rows[$parent_id]['forum_last_poster_id'] = $row['forum_last_poster_id'];
   269 				$forum_rows[$parent_id]['forum_last_poster_name'] = $row['forum_last_poster_name'];
   270 				$forum_rows[$parent_id]['forum_last_poster_colour'] = $row['forum_last_poster_colour'];
   271 				$forum_rows[$parent_id]['forum_id_last_post'] = $forum_id;
   272 			}
   273 		}
   274 	}
   275 	$db->sql_freeresult($result);
   276 
   277 	// Handle marking posts
   278 	if ($mark_read == 'forums')
   279 	{
   280 		$redirect = build_url(array('mark', 'hash'));
   281 		$token = request_var('hash', '');
   282 		if (check_link_hash($token, 'global'))
   283 		{
   284 			// Add 0 to forums array to mark global announcements correctly
   285 			$forum_ids[] = 0;
   286 			markread('topics', $forum_ids);
   287 			$message = sprintf($user->lang['RETURN_FORUM'], '<a href="' . $redirect . '">', '</a>');
   288 			meta_refresh(3, $redirect);
   289 			trigger_error($user->lang['FORUMS_MARKED'] . '<br /><br />' . $message);
   290 		}
   291 		else
   292 		{
   293 			$message = sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>');
   294 			meta_refresh(3, $redirect);
   295 			trigger_error($message);
   296 		}
   297 
   298 	}
   299 
   300 	// Grab moderators ... if necessary
   301 	if ($display_moderators)
   302 	{
   303 		if ($return_moderators)
   304 		{
   305 			$forum_ids_moderator[] = $root_data['forum_id'];
   306 		}
   307 		get_moderators($forum_moderators, $forum_ids_moderator);
   308 	}
   309 
   310 	// Used to tell whatever we have to create a dummy category or not.
   311 	$last_catless = true;
   312 	foreach ($forum_rows as $row)
   313 	{
   314 		// Empty category
   315 		if ($row['parent_id'] == $root_data['forum_id'] && $row['forum_type'] == FORUM_CAT)
   316 		{
   317 			$template->assign_block_vars('forumrow', array(
   318 				'S_IS_CAT'				=> true,
   319 				'FORUM_ID'				=> $row['forum_id'],
   320 				'FORUM_NAME'			=> $row['forum_name'],
   321 				'FORUM_DESC'			=> generate_text_for_display($row['forum_desc'], $row['forum_desc_uid'], $row['forum_desc_bitfield'], $row['forum_desc_options']),
   322 				'FORUM_FOLDER_IMG'		=> '',
   323 				'FORUM_FOLDER_IMG_SRC'	=> '',
   324 				'FORUM_IMAGE'			=> ($row['forum_image']) ? '<img src="' . $phpbb_root_path . $row['forum_image'] . '" alt="' . $user->lang['FORUM_CAT'] . '" />' : '',
   325 				'FORUM_IMAGE_SRC'		=> ($row['forum_image']) ? $phpbb_root_path . $row['forum_image'] : '',
   326 				'U_VIEWFORUM'			=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']))
   327 			);
   328 
   329 			continue;
   330 		}
   331 
   332 		$visible_forums++;
   333 		$forum_id = $row['forum_id'];
   334 
   335 		$forum_unread = (isset($forum_tracking_info[$forum_id]) && $row['orig_forum_last_post_time'] > $forum_tracking_info[$forum_id]) ? true : false;
   336 
   337 		// Mark the first visible forum on index as unread if there's any unread global announcement
   338 		if ($ga_unread && !empty($forum_ids_moderator) && $forum_id == $forum_ids_moderator[0])
   339 		{
   340 			$forum_unread = true;
   341 		}
   342 
   343 		$folder_image = $folder_alt = $l_subforums = '';
   344 		$subforums_list = array();
   345 
   346 		// Generate list of subforums if we need to
   347 		if (isset($subforums[$forum_id]))
   348 		{
   349 			foreach ($subforums[$forum_id] as $subforum_id => $subforum_row)
   350 			{
   351 				$subforum_unread = (isset($forum_tracking_info[$subforum_id]) && $subforum_row['orig_forum_last_post_time'] > $forum_tracking_info[$subforum_id]) ? true : false;
   352 
   353 				if (!$subforum_unread && !empty($subforum_row['children']))
   354 				{
   355 					foreach ($subforum_row['children'] as $child_id)
   356 					{
   357 						if (isset($forum_tracking_info[$child_id]) && $subforums[$forum_id][$child_id]['orig_forum_last_post_time'] > $forum_tracking_info[$child_id])
   358 						{
   359 							// Once we found an unread child forum, we can drop out of this loop
   360 							$subforum_unread = true;
   361 							break;
   362 						}
   363 					}
   364 				}
   365 
   366 				if ($subforum_row['display'] && $subforum_row['name'])
   367 				{
   368 					$subforums_list[] = array(
   369 						'link'		=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $subforum_id),
   370 						'name'		=> $subforum_row['name'],
   371 						'unread'	=> $subforum_unread,
   372 					);
   373 				}
   374 				else
   375 				{
   376 					unset($subforums[$forum_id][$subforum_id]);
   377 				}
   378 
   379 				// If one subforum is unread the forum gets unread too...
   380 				if ($subforum_unread)
   381 				{
   382 					$forum_unread = true;
   383 				}
   384 			}
   385 
   386 			$l_subforums = (sizeof($subforums[$forum_id]) == 1) ? $user->lang['SUBFORUM'] . ': ' : $user->lang['SUBFORUMS'] . ': ';
   387 			$folder_image = ($forum_unread) ? 'forum_unread_subforum' : 'forum_read_subforum';
   388 		}
   389 		else
   390 		{
   391 			switch ($row['forum_type'])
   392 			{
   393 				case FORUM_POST:
   394 					$folder_image = ($forum_unread) ? 'forum_unread' : 'forum_read';
   395 				break;
   396 
   397 				case FORUM_LINK:
   398 					$folder_image = 'forum_link';
   399 				break;
   400 			}
   401 		}
   402 
   403 		// Which folder should we display?
   404 		if ($row['forum_status'] == ITEM_LOCKED)
   405 		{
   406 			$folder_image = ($forum_unread) ? 'forum_unread_locked' : 'forum_read_locked';
   407 			$folder_alt = 'FORUM_LOCKED';
   408 		}
   409 		else
   410 		{
   411 			$folder_alt = ($forum_unread) ? 'UNREAD_POSTS' : 'NO_UNREAD_POSTS';
   412 		}
   413 
   414 		// Create last post link information, if appropriate
   415 		if ($row['forum_last_post_id'])
   416 		{
   417 			$last_post_subject = $row['forum_last_post_subject'];
   418 			$last_post_time = $user->format_date($row['forum_last_post_time']);
   419 			$last_post_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $row['forum_id_last_post'] . '&amp;p=' . $row['forum_last_post_id']) . '#p' . $row['forum_last_post_id'];
   420 		}
   421 		else
   422 		{
   423 			$last_post_subject = $last_post_time = $last_post_url = '';
   424 		}
   425 
   426 		// Output moderator listing ... if applicable
   427 		$l_moderator = $moderators_list = '';
   428 		if ($display_moderators && !empty($forum_moderators[$forum_id]))
   429 		{
   430 			$l_moderator = (sizeof($forum_moderators[$forum_id]) == 1) ? $user->lang['MODERATOR'] : $user->lang['MODERATORS'];
   431 			$moderators_list = implode(', ', $forum_moderators[$forum_id]);
   432 		}
   433 
   434 		$l_post_click_count = ($row['forum_type'] == FORUM_LINK) ? 'CLICKS' : 'POSTS';
   435 		$post_click_count = ($row['forum_type'] != FORUM_LINK || $row['forum_flags'] & FORUM_FLAG_LINK_TRACK) ? $row['forum_posts'] : '';
   436 
   437 		$s_subforums_list = array();
   438 		foreach ($subforums_list as $subforum)
   439 		{
   440 			$s_subforums_list[] = '<a href="' . $subforum['link'] . '" class="subforum ' . (($subforum['unread']) ? 'unread' : 'read') . '" title="' . (($subforum['unread']) ? $user->lang['UNREAD_POSTS'] : $user->lang['NO_UNREAD_POSTS']) . '">' . $subforum['name'] . '</a>';
   441 		}
   442 		$s_subforums_list = (string) implode(', ', $s_subforums_list);
   443 		$catless = ($row['parent_id'] == $root_data['forum_id']) ? true : false;
   444 
   445 		if ($row['forum_type'] != FORUM_LINK)
   446 		{
   447 			$u_viewforum = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']);
   448 		}
   449 		else
   450 		{
   451 			// If the forum is a link and we count redirects we need to visit it
   452 			// If the forum is having a password or no read access we do not expose the link, but instead handle it in viewforum
   453 			if (($row['forum_flags'] & FORUM_FLAG_LINK_TRACK) || $row['forum_password'] || !$auth->acl_get('f_read', $forum_id))
   454 			{
   455 				$u_viewforum = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']);
   456 			}
   457 			else
   458 			{
   459 				$u_viewforum = $row['forum_link'];
   460 			}
   461 		}
   462 
   463 		$template->assign_block_vars('forumrow', array(
   464 			'S_IS_CAT'			=> false,
   465 			'S_NO_CAT'			=> $catless && !$last_catless,
   466 			'S_IS_LINK'			=> ($row['forum_type'] == FORUM_LINK) ? true : false,
   467 			'S_UNREAD_FORUM'	=> $forum_unread,
   468 			'S_AUTH_READ'		=> $auth->acl_get('f_read', $row['forum_id']),
   469 			'S_LOCKED_FORUM'	=> ($row['forum_status'] == ITEM_LOCKED) ? true : false,
   470 			'S_LIST_SUBFORUMS'	=> ($row['display_subforum_list']) ? true : false,
   471 			'S_SUBFORUMS'		=> (sizeof($subforums_list)) ? true : false,
   472 			'S_FEED_ENABLED'	=> ($config['feed_forum'] && !phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $row['forum_options']) && $row['forum_type'] == FORUM_POST) ? true : false,
   473 
   474 			'FORUM_ID'				=> $row['forum_id'],
   475 			'FORUM_NAME'			=> $row['forum_name'],
   476 			'FORUM_DESC'			=> generate_text_for_display($row['forum_desc'], $row['forum_desc_uid'], $row['forum_desc_bitfield'], $row['forum_desc_options']),
   477 			'TOPICS'				=> $row['forum_topics'],
   478 			$l_post_click_count		=> $post_click_count,
   479 			'FORUM_FOLDER_IMG'		=> $user->img($folder_image, $folder_alt),
   480 			'FORUM_FOLDER_IMG_SRC'	=> $user->img($folder_image, $folder_alt, false, '', 'src'),
   481 			'FORUM_FOLDER_IMG_ALT'	=> isset($user->lang[$folder_alt]) ? $user->lang[$folder_alt] : '',
   482 			'FORUM_IMAGE'			=> ($row['forum_image']) ? '<img src="' . $phpbb_root_path . $row['forum_image'] . '" alt="' . $user->lang[$folder_alt] . '" />' : '',
   483 			'FORUM_IMAGE_SRC'		=> ($row['forum_image']) ? $phpbb_root_path . $row['forum_image'] : '',
   484 			'LAST_POST_SUBJECT'		=> censor_text($last_post_subject),
   485 			'LAST_POST_TIME'		=> $last_post_time,
   486 			'LAST_POSTER'			=> get_username_string('username', $row['forum_last_poster_id'], $row['forum_last_poster_name'], $row['forum_last_poster_colour']),
   487 			'LAST_POSTER_COLOUR'	=> get_username_string('colour', $row['forum_last_poster_id'], $row['forum_last_poster_name'], $row['forum_last_poster_colour']),
   488 			'LAST_POSTER_FULL'		=> get_username_string('full', $row['forum_last_poster_id'], $row['forum_last_poster_name'], $row['forum_last_poster_colour']),
   489 			'MODERATORS'			=> $moderators_list,
   490 			'SUBFORUMS'				=> $s_subforums_list,
   491 
   492 			'L_SUBFORUM_STR'		=> $l_subforums,
   493 			'L_MODERATOR_STR'		=> $l_moderator,
   494 
   495 			'U_UNAPPROVED_TOPICS'	=> ($row['forum_id_unapproved_topics']) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=unapproved_topics&amp;f=' . $row['forum_id_unapproved_topics']) : '',
   496 			'U_VIEWFORUM'		=> $u_viewforum,
   497 			'U_LAST_POSTER'		=> get_username_string('profile', $row['forum_last_poster_id'], $row['forum_last_poster_name'], $row['forum_last_poster_colour']),
   498 			'U_LAST_POST'		=> $last_post_url)
   499 		);
   500 
   501 		// Assign subforums loop for style authors
   502 		foreach ($subforums_list as $subforum)
   503 		{
   504 			$template->assign_block_vars('forumrow.subforum', array(
   505 				'U_SUBFORUM'	=> $subforum['link'],
   506 				'SUBFORUM_NAME'	=> $subforum['name'],
   507 				'S_UNREAD'		=> $subforum['unread'])
   508 			);
   509 		}
   510 
   511 		$last_catless = $catless;
   512 	}
   513 
   514 	$template->assign_vars(array(
   515 		'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;f=' . $root_data['forum_id'] . '&amp;mark=forums') : '',
   516 		'S_HAS_SUBFORUM'	=> ($visible_forums) ? true : false,
   517 		'L_SUBFORUM'		=> ($visible_forums == 1) ? $user->lang['SUBFORUM'] : $user->lang['SUBFORUMS'],
   518 		'LAST_POST_IMG'		=> $user->img('icon_topic_latest', 'VIEW_LATEST_POST'),
   519 		'UNAPPROVED_IMG'	=> $user->img('icon_topic_unapproved', 'TOPICS_UNAPPROVED'),
   520 	));
   521 
   522 	if ($return_moderators)
   523 	{
   524 		return array($active_forum_ary, $forum_moderators);
   525 	}
   526 
   527 	return array($active_forum_ary, array());
   528 }
   529 
   530 /**
   531 * Create forum rules for given forum
   532 */
   533 function generate_forum_rules(&$forum_data)
   534 {
   535 	if (!$forum_data['forum_rules'] && !$forum_data['forum_rules_link'])
   536 	{
   537 		return;
   538 	}
   539 
   540 	global $template, $phpbb_root_path, $phpEx;
   541 
   542 	if ($forum_data['forum_rules'])
   543 	{
   544 		$forum_data['forum_rules'] = generate_text_for_display($forum_data['forum_rules'], $forum_data['forum_rules_uid'], $forum_data['forum_rules_bitfield'], $forum_data['forum_rules_options']);
   545 	}
   546 
   547 	$template->assign_vars(array(
   548 		'S_FORUM_RULES'	=> true,
   549 		'U_FORUM_RULES'	=> $forum_data['forum_rules_link'],
   550 		'FORUM_RULES'	=> $forum_data['forum_rules'])
   551 	);
   552 }
   553 
   554 /**
   555 * Create forum navigation links for given forum, create parent
   556 * list if currently null, assign basic forum info to template
   557 */
   558 function generate_forum_nav(&$forum_data)
   559 {
   560 	global $db, $user, $template, $auth, $config;
   561 	global $phpEx, $phpbb_root_path;
   562 
   563 	if (!$auth->acl_get('f_list', $forum_data['forum_id']))
   564 	{
   565 		return;
   566 	}
   567 
   568 	// Get forum parents
   569 	$forum_parents = get_forum_parents($forum_data);
   570 
   571 	// Build navigation links
   572 	if (!empty($forum_parents))
   573 	{
   574 		foreach ($forum_parents as $parent_forum_id => $parent_data)
   575 		{
   576 			list($parent_name, $parent_type) = array_values($parent_data);
   577 
   578 			// Skip this parent if the user does not have the permission to view it
   579 			if (!$auth->acl_get('f_list', $parent_forum_id))
   580 			{
   581 				continue;
   582 			}
   583 
   584 			$template->assign_block_vars('navlinks', array(
   585 				'S_IS_CAT'		=> ($parent_type == FORUM_CAT) ? true : false,
   586 				'S_IS_LINK'		=> ($parent_type == FORUM_LINK) ? true : false,
   587 				'S_IS_POST'		=> ($parent_type == FORUM_POST) ? true : false,
   588 				'FORUM_NAME'	=> $parent_name,
   589 				'FORUM_ID'		=> $parent_forum_id,
   590 				'U_VIEW_FORUM'	=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $parent_forum_id))
   591 			);
   592 		}
   593 	}
   594 
   595 	$template->assign_block_vars('navlinks', array(
   596 		'S_IS_CAT'		=> ($forum_data['forum_type'] == FORUM_CAT) ? true : false,
   597 		'S_IS_LINK'		=> ($forum_data['forum_type'] == FORUM_LINK) ? true : false,
   598 		'S_IS_POST'		=> ($forum_data['forum_type'] == FORUM_POST) ? true : false,
   599 		'FORUM_NAME'	=> $forum_data['forum_name'],
   600 		'FORUM_ID'		=> $forum_data['forum_id'],
   601 		'U_VIEW_FORUM'	=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_data['forum_id']))
   602 	);
   603 
   604 	$template->assign_vars(array(
   605 		'FORUM_ID' 		=> $forum_data['forum_id'],
   606 		'FORUM_NAME'	=> $forum_data['forum_name'],
   607 		'FORUM_DESC'	=> generate_text_for_display($forum_data['forum_desc'], $forum_data['forum_desc_uid'], $forum_data['forum_desc_bitfield'], $forum_data['forum_desc_options']),
   608 
   609 		'S_ENABLE_FEEDS_FORUM'	=> ($config['feed_forum'] && $forum_data['forum_type'] == FORUM_POST && !phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $forum_data['forum_options'])) ? true : false,
   610 	));
   611 
   612 	return;
   613 }
   614 
   615 /**
   616 * Returns forum parents as an array. Get them from forum_data if available, or update the database otherwise
   617 */
   618 function get_forum_parents(&$forum_data)
   619 {
   620 	global $db;
   621 
   622 	$forum_parents = array();
   623 
   624 	if ($forum_data['parent_id'] > 0)
   625 	{
   626 		if ($forum_data['forum_parents'] == '')
   627 		{
   628 			$sql = 'SELECT forum_id, forum_name, forum_type
   629 				FROM ' . FORUMS_TABLE . '
   630 				WHERE left_id < ' . $forum_data['left_id'] . '
   631 					AND right_id > ' . $forum_data['right_id'] . '
   632 				ORDER BY left_id ASC';
   633 			$result = $db->sql_query($sql);
   634 
   635 			while ($row = $db->sql_fetchrow($result))
   636 			{
   637 				$forum_parents[$row['forum_id']] = array($row['forum_name'], (int) $row['forum_type']);
   638 			}
   639 			$db->sql_freeresult($result);
   640 
   641 			$forum_data['forum_parents'] = serialize($forum_parents);
   642 
   643 			$sql = 'UPDATE ' . FORUMS_TABLE . "
   644 				SET forum_parents = '" . $db->sql_escape($forum_data['forum_parents']) . "'
   645 				WHERE parent_id = " . $forum_data['parent_id'];
   646 			$db->sql_query($sql);
   647 		}
   648 		else
   649 		{
   650 			$forum_parents = unserialize($forum_data['forum_parents']);
   651 		}
   652 	}
   653 
   654 	return $forum_parents;
   655 }
   656 
   657 /**
   658 * Generate topic pagination
   659 */
   660 function topic_generate_pagination($replies, $url)
   661 {
   662 	global $config, $user;
   663 
   664 	// Make sure $per_page is a valid value
   665 	$per_page = ($config['posts_per_page'] <= 0) ? 1 : $config['posts_per_page'];
   666 
   667 	if (($replies + 1) > $per_page)
   668 	{
   669 		$total_pages = ceil(($replies + 1) / $per_page);
   670 		$pagination = '';
   671 
   672 		$times = 1;
   673 		for ($j = 0; $j < $replies + 1; $j += $per_page)
   674 		{
   675 			$pagination .= '<a href="' . $url . ($j == 0 ? '' : '&amp;start=' . $j) . '">' . $times . '</a>';
   676 			if ($times == 1 && $total_pages > 5)
   677 			{
   678 				$pagination .= '<span class="page-dots"> ... </span>';
   679 
   680 				// Display the last three pages
   681 				$times = $total_pages - 3;
   682 				$j += ($total_pages - 4) * $per_page;
   683 			}
   684 			else if ($times < $total_pages)
   685 			{
   686 				$pagination .= '<span class="page-sep">' . $user->lang['COMMA_SEPARATOR'] . '</span>';
   687 			}
   688 			$times++;
   689 		}
   690 	}
   691 	else
   692 	{
   693 		$pagination = '';
   694 	}
   695 
   696 	return $pagination;
   697 }
   698 
   699 /**
   700 * Obtain list of moderators of each forum
   701 */
   702 function get_moderators(&$forum_moderators, $forum_id = false)
   703 {
   704 	global $config, $template, $db, $phpbb_root_path, $phpEx, $user, $auth;
   705 
   706 	$forum_id_ary = array();
   707 
   708 	if ($forum_id !== false)
   709 	{
   710 		if (!is_array($forum_id))
   711 		{
   712 			$forum_id = array($forum_id);
   713 		}
   714 
   715 		// Exchange key/value pair to be able to faster check for the forum id existence
   716 		$forum_id_ary = array_flip($forum_id);
   717 	}
   718 
   719 	$sql_array = array(
   720 		'SELECT'	=> 'm.*, u.user_colour, g.group_colour, g.group_type',
   721 
   722 		'FROM'		=> array(
   723 			MODERATOR_CACHE_TABLE	=> 'm',
   724 		),
   725 
   726 		'LEFT_JOIN'	=> array(
   727 			array(
   728 				'FROM'	=> array(USERS_TABLE => 'u'),
   729 				'ON'	=> 'm.user_id = u.user_id',
   730 			),
   731 			array(
   732 				'FROM'	=> array(GROUPS_TABLE => 'g'),
   733 				'ON'	=> 'm.group_id = g.group_id',
   734 			),
   735 		),
   736 
   737 		'WHERE'		=> 'm.display_on_index = 1',
   738 	);
   739 
   740 	// We query every forum here because for caching we should not have any parameter.
   741 	$sql = $db->sql_build_query('SELECT', $sql_array);
   742 	$result = $db->sql_query($sql, 3600);
   743 
   744 	while ($row = $db->sql_fetchrow($result))
   745 	{
   746 		$f_id = (int) $row['forum_id'];
   747 
   748 		if (!isset($forum_id_ary[$f_id]))
   749 		{
   750 			continue;
   751 		}
   752 
   753 		if (!empty($row['user_id']))
   754 		{
   755 			$forum_moderators[$f_id][] = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']);
   756 		}
   757 		else
   758 		{
   759 			$group_name = (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']);
   760 
   761 			if ($user->data['user_id'] != ANONYMOUS && !$auth->acl_get('u_viewprofile'))
   762 			{
   763 				$forum_moderators[$f_id][] = '<span' . (($row['group_colour']) ? ' style="color:#' . $row['group_colour'] . ';"' : '') . '>' . $group_name . '</span>';
   764 			}
   765 			else
   766 			{
   767 				$forum_moderators[$f_id][] = '<a' . (($row['group_colour']) ? ' style="color:#' . $row['group_colour'] . ';"' : '') . ' href="' . append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=group&amp;g=' . $row['group_id']) . '">' . $group_name . '</a>';
   768 			}
   769 		}
   770 	}
   771 	$db->sql_freeresult($result);
   772 
   773 	return;
   774 }
   775 
   776 /**
   777 * User authorisation levels output
   778 *
   779 * @param	string	$mode			Can be forum or topic. Not in use at the moment.
   780 * @param	int		$forum_id		The current forum the user is in.
   781 * @param	int		$forum_status	The forums status bit.
   782 */
   783 function gen_forum_auth_level($mode, $forum_id, $forum_status)
   784 {
   785 	global $template, $auth, $user, $config;
   786 
   787 	$locked = ($forum_status == ITEM_LOCKED && !$auth->acl_get('m_edit', $forum_id)) ? true : false;
   788 
   789 	$rules = array(
   790 		($auth->acl_get('f_post', $forum_id) && !$locked) ? $user->lang['RULES_POST_CAN'] : $user->lang['RULES_POST_CANNOT'],
   791 		($auth->acl_get('f_reply', $forum_id) && !$locked) ? $user->lang['RULES_REPLY_CAN'] : $user->lang['RULES_REPLY_CANNOT'],
   792 		($user->data['is_registered'] && $auth->acl_gets('f_edit', 'm_edit', $forum_id) && !$locked) ? $user->lang['RULES_EDIT_CAN'] : $user->lang['RULES_EDIT_CANNOT'],
   793 		($user->data['is_registered'] && $auth->acl_gets('f_delete', 'm_delete', $forum_id) && !$locked) ? $user->lang['RULES_DELETE_CAN'] : $user->lang['RULES_DELETE_CANNOT'],
   794 	);
   795 
   796 	if ($config['allow_attachments'])
   797 	{
   798 		$rules[] = ($auth->acl_get('f_attach', $forum_id) && $auth->acl_get('u_attach') && !$locked) ? $user->lang['RULES_ATTACH_CAN'] : $user->lang['RULES_ATTACH_CANNOT'];
   799 	}
   800 
   801 	foreach ($rules as $rule)
   802 	{
   803 		$template->assign_block_vars('rules', array('RULE' => $rule));
   804 	}
   805 
   806 	return;
   807 }
   808 
   809 /**
   810 * Generate topic status
   811 */
   812 function topic_status(&$topic_row, $replies, $unread_topic, &$folder_img, &$folder_alt, &$topic_type)
   813 {
   814 	global $user, $config;
   815 
   816 	$folder = $folder_new = '';
   817 
   818 	if ($topic_row['topic_status'] == ITEM_MOVED)
   819 	{
   820 		$topic_type = $user->lang['VIEW_TOPIC_MOVED'];
   821 		$folder_img = 'topic_moved';
   822 		$folder_alt = 'TOPIC_MOVED';
   823 	}
   824 	else
   825 	{
   826 		switch ($topic_row['topic_type'])
   827 		{
   828 			case POST_GLOBAL:
   829 				$topic_type = $user->lang['VIEW_TOPIC_GLOBAL'];
   830 				$folder = 'global_read';
   831 				$folder_new = 'global_unread';
   832 			break;
   833 
   834 			case POST_ANNOUNCE:
   835 				$topic_type = $user->lang['VIEW_TOPIC_ANNOUNCEMENT'];
   836 				$folder = 'announce_read';
   837 				$folder_new = 'announce_unread';
   838 			break;
   839 
   840 			case POST_STICKY:
   841 				$topic_type = $user->lang['VIEW_TOPIC_STICKY'];
   842 				$folder = 'sticky_read';
   843 				$folder_new = 'sticky_unread';
   844 			break;
   845 
   846 			default:
   847 				$topic_type = '';
   848 				$folder = 'topic_read';
   849 				$folder_new = 'topic_unread';
   850 
   851 				// Hot topic threshold is for posts in a topic, which is replies + the first post. ;)
   852 				if ($config['hot_threshold'] && ($replies + 1) >= $config['hot_threshold'] && $topic_row['topic_status'] != ITEM_LOCKED)
   853 				{
   854 					$folder .= '_hot';
   855 					$folder_new .= '_hot';
   856 				}
   857 			break;
   858 		}
   859 
   860 		if ($topic_row['topic_status'] == ITEM_LOCKED)
   861 		{
   862 			$topic_type = $user->lang['VIEW_TOPIC_LOCKED'];
   863 			$folder .= '_locked';
   864 			$folder_new .= '_locked';
   865 		}
   866 
   867 
   868 		$folder_img = ($unread_topic) ? $folder_new : $folder;
   869 		$folder_alt = ($unread_topic) ? 'UNREAD_POSTS' : (($topic_row['topic_status'] == ITEM_LOCKED) ? 'TOPIC_LOCKED' : 'NO_UNREAD_POSTS');
   870 
   871 		// Posted image?
   872 		if (!empty($topic_row['topic_posted']) && $topic_row['topic_posted'])
   873 		{
   874 			$folder_img .= '_mine';
   875 		}
   876 	}
   877 
   878 	if ($topic_row['poll_start'] && $topic_row['topic_status'] != ITEM_MOVED)
   879 	{
   880 		$topic_type = $user->lang['VIEW_TOPIC_POLL'];
   881 	}
   882 }
   883 
   884 /**
   885 * Assign/Build custom bbcodes for display in screens supporting using of bbcodes
   886 * The custom bbcodes buttons will be placed within the template block 'custom_codes'
   887 */
   888 function display_custom_bbcodes()
   889 {
   890 	global $db, $template, $user;
   891 
   892 	// Start counting from 22 for the bbcode ids (every bbcode takes two ids - opening/closing)
   893 	$num_predefined_bbcodes = 22;
   894 
   895 	$sql = 'SELECT bbcode_id, bbcode_tag, bbcode_helpline
   896 		FROM ' . BBCODES_TABLE . '
   897 		WHERE display_on_posting = 1
   898 		ORDER BY bbcode_tag';
   899 	$result = $db->sql_query($sql);
   900 
   901 	$i = 0;
   902 	while ($row = $db->sql_fetchrow($result))
   903 	{
   904 		// If the helpline is defined within the language file, we will use the localised version, else just use the database entry...
   905 		if (isset($user->lang[strtoupper($row['bbcode_helpline'])]))
   906 		{
   907 			$row['bbcode_helpline'] = $user->lang[strtoupper($row['bbcode_helpline'])];
   908 		}
   909 
   910 		$template->assign_block_vars('custom_tags', array(
   911 			'BBCODE_NAME'		=> "'[{$row['bbcode_tag']}]', '[/" . str_replace('=', '', $row['bbcode_tag']) . "]'",
   912 			'BBCODE_ID'			=> $num_predefined_bbcodes + ($i * 2),
   913 			'BBCODE_TAG'		=> $row['bbcode_tag'],
   914 			'BBCODE_HELPLINE'	=> $row['bbcode_helpline'],
   915 			'A_BBCODE_HELPLINE'	=> str_replace(array('&amp;', '&quot;', "'", '&lt;', '&gt;'), array('&', '"', "\'", '<', '>'), $row['bbcode_helpline']),
   916 		));
   917 
   918 		$i++;
   919 	}
   920 	$db->sql_freeresult($result);
   921 }
   922 
   923 /**
   924 * Display reasons
   925 */
   926 function display_reasons($reason_id = 0)
   927 {
   928 	global $db, $user, $template;
   929 
   930 	$sql = 'SELECT *
   931 		FROM ' . REPORTS_REASONS_TABLE . '
   932 		ORDER BY reason_order ASC';
   933 	$result = $db->sql_query($sql);
   934 
   935 	while ($row = $db->sql_fetchrow($result))
   936 	{
   937 		// If the reason is defined within the language file, we will use the localized version, else just use the database entry...
   938 		if (isset($user->lang['report_reasons']['TITLE'][strtoupper($row['reason_title'])]) && isset($user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])]))
   939 		{
   940 			$row['reason_description'] = $user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])];
   941 			$row['reason_title'] = $user->lang['report_reasons']['TITLE'][strtoupper($row['reason_title'])];
   942 		}
   943 
   944 		$template->assign_block_vars('reason', array(
   945 			'ID'			=> $row['reason_id'],
   946 			'TITLE'			=> $row['reason_title'],
   947 			'DESCRIPTION'	=> $row['reason_description'],
   948 			'S_SELECTED'	=> ($row['reason_id'] == $reason_id) ? true : false)
   949 		);
   950 	}
   951 	$db->sql_freeresult($result);
   952 }
   953 
   954 /**
   955 * Display user activity (action forum/topic)
   956 */
   957 function display_user_activity(&$userdata)
   958 {
   959 	global $auth, $template, $db, $user;
   960 	global $phpbb_root_path, $phpEx;
   961 
   962 	// Do not display user activity for users having more than 5000 posts...
   963 	if ($userdata['user_posts'] > 5000)
   964 	{
   965 		return;
   966 	}
   967 
   968 	$forum_ary = array();
   969 
   970 	// Do not include those forums the user is not having read access to...
   971 	$forum_read_ary = $auth->acl_getf('!f_read');
   972 
   973 	foreach ($forum_read_ary as $forum_id => $not_allowed)
   974 	{
   975 		if ($not_allowed['f_read'])
   976 		{
   977 			$forum_ary[] = (int) $forum_id;
   978 		}
   979 	}
   980 
   981 	$forum_ary = array_unique($forum_ary);
   982 	$forum_sql = (sizeof($forum_ary)) ? 'AND ' . $db->sql_in_set('forum_id', $forum_ary, true) : '';
   983 
   984 	$fid_m_approve = $auth->acl_getf('m_approve', true);
   985 	$sql_m_approve = (!empty($fid_m_approve)) ? 'OR ' . $db->sql_in_set('forum_id', array_keys($fid_m_approve)) : '';
   986 
   987 	// Obtain active forum
   988 	$sql = 'SELECT forum_id, COUNT(post_id) AS num_posts
   989 		FROM ' . POSTS_TABLE . '
   990 		WHERE poster_id = ' . $userdata['user_id'] . "
   991 			AND post_postcount = 1
   992 			AND (post_approved = 1
   993 				$sql_m_approve)
   994 			$forum_sql
   995 		GROUP BY forum_id
   996 		ORDER BY num_posts DESC";
   997 	$result = $db->sql_query_limit($sql, 1);
   998 	$active_f_row = $db->sql_fetchrow($result);
   999 	$db->sql_freeresult($result);
  1000 
  1001 	if (!empty($active_f_row))
  1002 	{
  1003 		$sql = 'SELECT forum_name
  1004 			FROM ' . FORUMS_TABLE . '
  1005 			WHERE forum_id = ' . $active_f_row['forum_id'];
  1006 		$result = $db->sql_query($sql, 3600);
  1007 		$active_f_row['forum_name'] = (string) $db->sql_fetchfield('forum_name');
  1008 		$db->sql_freeresult($result);
  1009 	}
  1010 
  1011 	// Obtain active topic
  1012 	// We need to exclude passworded forums here so we do not leak the topic title
  1013 	$forum_ary_topic = array_unique(array_merge($forum_ary, $user->get_passworded_forums()));
  1014 	$forum_sql_topic = (!empty($forum_ary_topic)) ? 'AND ' . $db->sql_in_set('forum_id', $forum_ary_topic, true) : '';
  1015 
  1016 	$sql = 'SELECT topic_id, COUNT(post_id) AS num_posts
  1017 		FROM ' . POSTS_TABLE . '
  1018 		WHERE poster_id = ' . $userdata['user_id'] . "
  1019 			AND post_postcount = 1
  1020 			AND (post_approved = 1
  1021 				$sql_m_approve)
  1022 			$forum_sql_topic
  1023 		GROUP BY topic_id
  1024 		ORDER BY num_posts DESC";
  1025 	$result = $db->sql_query_limit($sql, 1);
  1026 	$active_t_row = $db->sql_fetchrow($result);
  1027 	$db->sql_freeresult($result);
  1028 
  1029 	if (!empty($active_t_row))
  1030 	{
  1031 		$sql = 'SELECT topic_title
  1032 			FROM ' . TOPICS_TABLE . '
  1033 			WHERE topic_id = ' . $active_t_row['topic_id'];
  1034 		$result = $db->sql_query($sql);
  1035 		$active_t_row['topic_title'] = (string) $db->sql_fetchfield('topic_title');
  1036 		$db->sql_freeresult($result);
  1037 	}
  1038 
  1039 	$userdata['active_t_row'] = $active_t_row;
  1040 	$userdata['active_f_row'] = $active_f_row;
  1041 
  1042 	$active_f_name = $active_f_id = $active_f_count = $active_f_pct = '';
  1043 	if (!empty($active_f_row['num_posts']))
  1044 	{
  1045 		$active_f_name = $active_f_row['forum_name'];
  1046 		$active_f_id = $active_f_row['forum_id'];
  1047 		$active_f_count = $active_f_row['num_posts'];
  1048 		$active_f_pct = ($userdata['user_posts']) ? ($active_f_count / $userdata['user_posts']) * 100 : 0;
  1049 	}
  1050 
  1051 	$active_t_name = $active_t_id = $active_t_count = $active_t_pct = '';
  1052 	if (!empty($active_t_row['num_posts']))
  1053 	{
  1054 		$active_t_name = $active_t_row['topic_title'];
  1055 		$active_t_id = $active_t_row['topic_id'];
  1056 		$active_t_count = $active_t_row['num_posts'];
  1057 		$active_t_pct = ($userdata['user_posts']) ? ($active_t_count / $userdata['user_posts']) * 100 : 0;
  1058 	}
  1059 
  1060 	$l_active_pct = ($userdata['user_id'] != ANONYMOUS && $userdata['user_id'] == $user->data['user_id']) ? $user->lang['POST_PCT_ACTIVE_OWN'] : $user->lang['POST_PCT_ACTIVE'];
  1061 
  1062 	$template->assign_vars(array(
  1063 		'ACTIVE_FORUM'			=> $active_f_name,
  1064 		'ACTIVE_FORUM_POSTS'	=> ($active_f_count == 1) ? sprintf($user->lang['USER_POST'], 1) : sprintf($user->lang['USER_POSTS'], $active_f_count),
  1065 		'ACTIVE_FORUM_PCT'		=> sprintf($l_active_pct, $active_f_pct),
  1066 		'ACTIVE_TOPIC'			=> censor_text($active_t_name),
  1067 		'ACTIVE_TOPIC_POSTS'	=> ($active_t_count == 1) ? sprintf($user->lang['USER_POST'], 1) : sprintf($user->lang['USER_POSTS'], $active_t_count),
  1068 		'ACTIVE_TOPIC_PCT'		=> sprintf($l_active_pct, $active_t_pct),
  1069 		'U_ACTIVE_FORUM'		=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $active_f_id),
  1070 		'U_ACTIVE_TOPIC'		=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=' . $active_t_id),
  1071 		'S_SHOW_ACTIVITY'		=> true)
  1072 	);
  1073 }
  1074 
  1075 /**
  1076 * Topic and forum watching common code
  1077 */
  1078 function watch_topic_forum($mode, &$s_watching, $user_id, $forum_id, $topic_id, $notify_status = 'unset', $start = 0, $item_title = '')
  1079 {
  1080 	global $template, $db, $user, $phpEx, $start, $phpbb_root_path;
  1081 
  1082 	$table_sql = ($mode == 'forum') ? FORUMS_WATCH_TABLE : TOPICS_WATCH_TABLE;
  1083 	$where_sql = ($mode == 'forum') ? 'forum_id' : 'topic_id';
  1084 	$match_id = ($mode == 'forum') ? $forum_id : $topic_id;
  1085 	$u_url = "uid={$user->data['user_id']}";
  1086 	$u_url .= ($mode == 'forum') ? '&amp;f' : '&amp;f=' . $forum_id . '&amp;t';
  1087 	$is_watching = 0;
  1088 
  1089 	// Is user watching this thread?
  1090 	if ($user_id != ANONYMOUS)
  1091 	{
  1092 		$can_watch = true;
  1093 
  1094 		if ($notify_status == 'unset')
  1095 		{
  1096 			$sql = "SELECT notify_status
  1097 				FROM $table_sql
  1098 				WHERE $where_sql = $match_id
  1099 					AND user_id = $user_id";
  1100 			$result = $db->sql_query($sql);
  1101 
  1102 			$notify_status = ($row = $db->sql_fetchrow($result)) ? $row['notify_status'] : NULL;
  1103 			$db->sql_freeresult($result);
  1104 		}
  1105 
  1106 		if (!is_null($notify_status) && $notify_status !== '')
  1107 		{
  1108 
  1109 			if (isset($_GET['unwatch']))
  1110 			{
  1111 				$uid = request_var('uid', 0);
  1112 				$token = request_var('hash', '');
  1113 
  1114 				if ($token && check_link_hash($token, "{$mode}_$match_id") || confirm_box(true))
  1115 				{
  1116 					if ($uid != $user_id || $_GET['unwatch'] != $mode)
  1117 					{
  1118 						$redirect_url = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&amp;start=$start");
  1119 						$message = $user->lang['ERR_UNWATCHING'] . '<br /><br />' . sprintf($user->lang['RETURN_' . strtoupper($mode)], '<a href="' . $redirect_url . '">', '</a>');
  1120 						trigger_error($message);
  1121 					}
  1122 
  1123 					$sql = 'DELETE FROM ' . $table_sql . "
  1124 						WHERE $where_sql = $match_id
  1125 							AND user_id = $user_id";
  1126 					$db->sql_query($sql);
  1127 
  1128 					$redirect_url = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&amp;start=$start");
  1129 					$message = $user->lang['NOT_WATCHING_' . strtoupper($mode)] . '<br /><br />';
  1130 					$message .= sprintf($user->lang['RETURN_' . strtoupper($mode)], '<a href="' . $redirect_url . '">', '</a>');
  1131 					meta_refresh(3, $redirect_url);
  1132 					trigger_error($message);
  1133 				}
  1134 				else
  1135 				{
  1136 					$s_hidden_fields = array(
  1137 						'uid'		=> $user->data['user_id'],
  1138 						'unwatch'	=> $mode,
  1139 						'start'		=> $start,
  1140 						'f'			=> $forum_id,
  1141 					);
  1142 					if ($mode != 'forum')
  1143 					{
  1144 						$s_hidden_fields['t'] = $topic_id;
  1145 					}
  1146 
  1147 					if ($item_title == '')
  1148 					{
  1149 						$confirm_box_message = 'UNWATCH_' . strtoupper($mode);
  1150 					}
  1151 					else
  1152 					{
  1153 						$confirm_box_message = $user->lang('UNWATCH_' . strtoupper($mode) . '_DETAILED', $item_title);
  1154 					}
  1155 					confirm_box(false, $confirm_box_message, build_hidden_fields($s_hidden_fields));
  1156 				}
  1157 			}
  1158 			else
  1159 			{
  1160 				$is_watching = true;
  1161 
  1162 				if ($notify_status != NOTIFY_YES)
  1163 				{
  1164 					$sql = 'UPDATE ' . $table_sql . "
  1165 						SET notify_status = " . NOTIFY_YES . "
  1166 						WHERE $where_sql = $match_id
  1167 							AND user_id = $user_id";
  1168 					$db->sql_query($sql);
  1169 				}
  1170 			}
  1171 		}
  1172 		else
  1173 		{
  1174 			if (isset($_GET['watch']))
  1175 			{
  1176 				$uid = request_var('uid', 0);
  1177 				$token = request_var('hash', '');
  1178 
  1179 				if ($token && check_link_hash($token, "{$mode}_$match_id") || confirm_box(true))
  1180 				{
  1181 					if ($uid != $user_id || $_GET['watch'] != $mode)
  1182 					{
  1183 						$redirect_url = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&amp;start=$start");
  1184 						$message = $user->lang['ERR_WATCHING'] . '<br /><br />' . sprintf($user->lang['RETURN_' . strtoupper($mode)], '<a href="' . $redirect_url . '">', '</a>');
  1185 						trigger_error($message);
  1186 					}
  1187 
  1188 					$is_watching = true;
  1189 
  1190 					$sql = 'INSERT INTO ' . $table_sql . " (user_id, $where_sql, notify_status)
  1191 						VALUES ($user_id, $match_id, " . NOTIFY_YES . ')';
  1192 					$db->sql_query($sql);
  1193 
  1194 					$redirect_url = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&amp;start=$start");
  1195 					$message = $user->lang['ARE_WATCHING_' . strtoupper($mode)] . '<br /><br />' . sprintf($user->lang['RETURN_' . strtoupper($mode)], '<a href="' . $redirect_url . '">', '</a>');
  1196 					meta_refresh(3, $redirect_url);
  1197 					trigger_error($message);
  1198 				}
  1199 				else
  1200 				{
  1201 					$s_hidden_fields = array(
  1202 						'uid'		=> $user->data['user_id'],
  1203 						'watch'		=> $mode,
  1204 						'start'		=> $start,
  1205 						'f'			=> $forum_id,
  1206 					);
  1207 					if ($mode != 'forum')
  1208 					{
  1209 						$s_hidden_fields['t'] = $topic_id;
  1210 					}
  1211 
  1212 					$confirm_box_message = (($item_title == '') ? 'WATCH_' . strtoupper($mode) : $user->lang('WATCH_' . strtoupper($mode) . '_DETAILED', $item_title));
  1213 					confirm_box(false, $confirm_box_message, build_hidden_fields($s_hidden_fields));
  1214 				}
  1215 			}
  1216 			else
  1217 			{
  1218 				$is_watching = 0;
  1219 			}
  1220 		}
  1221 	}
  1222 	else
  1223 	{
  1224 		if ((isset($_GET['unwatch']) && $_GET['unwatch'] == $mode) || (isset($_GET['watch']) && $_GET['watch'] == $mode))
  1225 		{
  1226 			login_box();
  1227 		}
  1228 		else
  1229 		{
  1230 			$can_watch = 0;
  1231 			$is_watching = 0;
  1232 		}
  1233 	}
  1234 
  1235 	if ($can_watch)
  1236 	{
  1237 		$s_watching['link'] = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&amp;" . (($is_watching) ? 'unwatch' : 'watch') . "=$mode&amp;start=$start&amp;hash=" . generate_link_hash("{$mode}_$match_id"));
  1238 		$s_watching['title'] = $user->lang[(($is_watching) ? 'STOP' : 'START') . '_WATCHING_' . strtoupper($mode)];
  1239 		$s_watching['is_watching'] = $is_watching;
  1240 	}
  1241 
  1242 	return;
  1243 }
  1244 
  1245 /**
  1246 * Get user rank title and image
  1247 *
  1248 * @param int $user_rank the current stored users rank id
  1249 * @param int $user_posts the users number of posts
  1250 * @param string &$rank_title the rank title will be stored here after execution
  1251 * @param string &$rank_img the rank image as full img tag is stored here after execution
  1252 * @param string &$rank_img_src the rank image source is stored here after execution
  1253 *
  1254 * Note: since we do not want to break backwards-compatibility, this function will only properly assign ranks to guests if you call it for them with user_posts == false
  1255 */
  1256 function get_user_rank($user_rank, $user_posts, &$rank_title, &$rank_img, &$rank_img_src)
  1257 {
  1258 	global $ranks, $config, $phpbb_root_path;
  1259 
  1260 	if (empty($ranks))
  1261 	{
  1262 		global $cache;
  1263 		$ranks = $cache->obtain_ranks();
  1264 	}
  1265 
  1266 	if (!empty($user_rank))
  1267 	{
  1268 		$rank_title = (isset($ranks['special'][$user_rank]['rank_title'])) ? $ranks['special'][$user_rank]['rank_title'] : '';
  1269 		$rank_img = (!empty($ranks['special'][$user_rank]['rank_image'])) ? '<img src="' . $phpbb_root_path . $config['ranks_path'] . '/' . $ranks['special'][$user_rank]['rank_image'] . '" alt="' . $ranks['special'][$user_rank]['rank_title'] . '" title="' . $ranks['special'][$user_rank]['rank_title'] . '" />' : '';
  1270 		$rank_img_src = (!empty($ranks['special'][$user_rank]['rank_image'])) ? $phpbb_root_path . $config['ranks_path'] . '/' . $ranks['special'][$user_rank]['rank_image'] : '';
  1271 	}
  1272 	else if ($user_posts !== false)
  1273 	{
  1274 		if (!empty($ranks['normal']))
  1275 		{
  1276 			foreach ($ranks['normal'] as $rank)
  1277 			{
  1278 				if ($user_posts >= $rank['rank_min'])
  1279 				{
  1280 					$rank_title = $rank['rank_title'];
  1281 					$rank_img = (!empty($rank['rank_image'])) ? '<img src="' . $phpbb_root_path . $config['ranks_path'] . '/' . $rank['rank_image'] . '" alt="' . $rank['rank_title'] . '" title="' . $rank['rank_title'] . '" />' : '';
  1282 					$rank_img_src = (!empty($rank['rank_image'])) ? $phpbb_root_path . $config['ranks_path'] . '/' . $rank['rank_image'] : '';
  1283 					break;
  1284 				}
  1285 			}
  1286 		}
  1287 	}
  1288 }
  1289 
  1290 /**
  1291 * Get user avatar
  1292 *
  1293 * @param string $avatar Users assigned avatar name
  1294 * @param int $avatar_type Type of avatar
  1295 * @param string $avatar_width Width of users avatar
  1296 * @param string $avatar_height Height of users avatar
  1297 * @param string $alt Optional language string for alt tag within image, can be a language key or text
  1298 * @param bool $ignore_config Ignores the config-setting, to be still able to view the avatar in the UCP
  1299 *
  1300 * @return string Avatar image
  1301 */
  1302 function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $alt = 'USER_AVATAR', $ignore_config = false)
  1303 {
  1304 	global $user, $config, $phpbb_root_path, $phpEx;
  1305 
  1306 	if (empty($avatar) || !$avatar_type || (!$config['allow_avatar'] && !$ignore_config))
  1307 	{
  1308 		return '';
  1309 	}
  1310 
  1311 	$avatar_img = '';
  1312 
  1313 	switch ($avatar_type)
  1314 	{
  1315 		case AVATAR_UPLOAD:
  1316 			if (!$config['allow_avatar_upload'] && !$ignore_config)
  1317 			{
  1318 				return '';
  1319 			}
  1320 			$avatar_img = $phpbb_root_path . "download/file.$phpEx?avatar=";
  1321 		break;
  1322 
  1323 		case AVATAR_GALLERY:
  1324 			if (!$config['allow_avatar_local'] && !$ignore_config)
  1325 			{
  1326 				return '';
  1327 			}
  1328 			$avatar_img = $phpbb_root_path . $config['avatar_gallery_path'] . '/';
  1329 		break;
  1330 
  1331 		case AVATAR_REMOTE:
  1332 			if (!$config['allow_avatar_remote'] && !$ignore_config)
  1333 			{
  1334 				return '';
  1335 			}
  1336 		break;
  1337 	}
  1338 
  1339 	$avatar_img .= $avatar;
  1340 	return '<img src="' . (str_replace(' ', '%20', $avatar_img)) . '" width="' . $avatar_width . '" height="' . $avatar_height . '" alt="' . ((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt) . '" />';
  1341 }
  1342 
  1343 ?>