6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
22 function display_forums($root_data = '', $display_moderators = true, $return_moderators = false)
24 global $db, $auth, $user, $template;
25 global $phpbb_root_path, $phpEx, $config;
27 $forum_rows = $subforums = $forum_ids = $forum_ids_moderator = $forum_moderators = $active_forum_ary = array();
28 $parent_id = $visible_forums = 0;
32 $mark_read = request_var('mark', '');
34 if ($mark_read == 'all')
41 if ($mark_read == 'forums')
46 $root_data = array('forum_id' => 0);
51 $sql_where = 'left_id > ' . $root_data['left_id'] . ' AND left_id < ' . $root_data['right_id'];
54 // Handle marking everything read
55 if ($mark_read == 'all')
57 $redirect = build_url(array('mark', 'hash'));
58 meta_refresh(3, $redirect);
60 if (check_link_hash(request_var('hash', ''), 'global'))
65 $user->lang['FORUMS_MARKED'] . '<br /><br />' .
66 sprintf($user->lang['RETURN_INDEX'], '<a href="' . $redirect . '">', '</a>')
71 trigger_error(sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>'));
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;
83 'LEFT_JOIN' => array(),
86 if ($config['load_db_lastread'] && $user->data['is_registered'])
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';
91 else if ($config['load_anon_lastread'] || $user->data['is_registered'])
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();
96 if (!$user->data['is_registered'])
98 $user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
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) . "'"
109 $sql_array['SELECT'] .= ', fa.user_id';
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'],
117 'WHERE' => $sql_where,
119 'ORDER_BY' => 'f.left_id',
122 $result = $db->sql_query($sql);
124 $forum_tracking_info = array();
125 $branch_root_id = $root_data['forum_id'];
127 // Check for unread global announcements (index page only)
129 if ($root_data['forum_id'] == 0)
131 $unread_ga_list = get_unread_topics($user->data['user_id'], 'AND t.forum_id = 0', '', 1);
133 if (!empty($unread_ga_list))
139 while ($row = $db->sql_fetchrow($result))
141 $forum_id = $row['forum_id'];
144 if ($mark_read == 'forums')
146 if ($auth->acl_get('f_list', $forum_id))
148 $forum_ids[] = $forum_id;
154 // Category with no members
155 if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
161 if (isset($right_id))
163 if ($row['left_id'] < $right_id)
170 if (!$auth->acl_get('f_list', $forum_id))
172 // if the user does not have permissions to list this forum, skip everything until next branch
173 $right_id = $row['right_id'];
177 if ($config['load_db_lastread'] && $user->data['is_registered'])
179 $forum_tracking_info[$forum_id] = (!empty($row['mark_time'])) ? $row['mark_time'] : $user->data['user_lastmark'];
181 else if ($config['load_anon_lastread'] || $user->data['is_registered'])
183 if (!$user->data['is_registered'])
185 $user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
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'];
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'];
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))
197 if (!isset($active_forum_ary['forum_topics']))
199 $active_forum_ary['forum_topics'] = 0;
202 if (!isset($active_forum_ary['forum_posts']))
204 $active_forum_ary['forum_posts'] = 0;
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'];
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'])
215 $active_forum_ary['exclude_forum_id'][] = $forum_id;
220 if ($row['parent_id'] == $root_data['forum_id'] || $row['parent_id'] == $branch_root_id)
222 if ($row['forum_type'] != FORUM_CAT)
224 $forum_ids_moderator[] = (int) $forum_id;
227 // Direct child of current branch
228 $parent_id = $forum_id;
229 $forum_rows[$forum_id] = $row;
231 if ($row['forum_type'] == FORUM_CAT && $row['parent_id'] == $root_data['forum_id'])
233 $branch_root_id = $forum_id;
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'];
238 else if ($row['forum_type'] != FORUM_CAT)
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();
245 if (isset($subforums[$parent_id][$row['parent_id']]) && !$row['display_on_index'])
247 $subforums[$parent_id][$row['parent_id']]['children'][] = $forum_id;
250 if (!$forum_rows[$parent_id]['forum_id_unapproved_topics'] && $row['forum_id_unapproved_topics'])
252 $forum_rows[$parent_id]['forum_id_unapproved_topics'] = $forum_id;
255 $forum_rows[$parent_id]['forum_topics'] += $row['forum_topics'];
257 // Do not list redirects in LINK Forums as Posts.
258 if ($row['forum_type'] != FORUM_LINK)
260 $forum_rows[$parent_id]['forum_posts'] += $row['forum_posts'];
263 if ($row['forum_last_post_time'] > $forum_rows[$parent_id]['forum_last_post_time'])
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;
275 $db->sql_freeresult($result);
277 // Handle marking posts
278 if ($mark_read == 'forums')
280 $redirect = build_url(array('mark', 'hash'));
281 $token = request_var('hash', '');
282 if (check_link_hash($token, 'global'))
284 // Add 0 to forums array to mark global announcements correctly
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);
293 $message = sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>');
294 meta_refresh(3, $redirect);
295 trigger_error($message);
300 // Grab moderators ... if necessary
301 if ($display_moderators)
303 if ($return_moderators)
305 $forum_ids_moderator[] = $root_data['forum_id'];
307 get_moderators($forum_moderators, $forum_ids_moderator);
310 // Used to tell whatever we have to create a dummy category or not.
311 $last_catless = true;
312 foreach ($forum_rows as $row)
315 if ($row['parent_id'] == $root_data['forum_id'] && $row['forum_type'] == FORUM_CAT)
317 $template->assign_block_vars('forumrow', array(
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']))
333 $forum_id = $row['forum_id'];
335 $forum_unread = (isset($forum_tracking_info[$forum_id]) && $row['orig_forum_last_post_time'] > $forum_tracking_info[$forum_id]) ? true : false;
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])
340 $forum_unread = true;
343 $folder_image = $folder_alt = $l_subforums = '';
344 $subforums_list = array();
346 // Generate list of subforums if we need to
347 if (isset($subforums[$forum_id]))
349 foreach ($subforums[$forum_id] as $subforum_id => $subforum_row)
351 $subforum_unread = (isset($forum_tracking_info[$subforum_id]) && $subforum_row['orig_forum_last_post_time'] > $forum_tracking_info[$subforum_id]) ? true : false;
353 if (!$subforum_unread && !empty($subforum_row['children']))
355 foreach ($subforum_row['children'] as $child_id)
357 if (isset($forum_tracking_info[$child_id]) && $subforums[$forum_id][$child_id]['orig_forum_last_post_time'] > $forum_tracking_info[$child_id])
359 // Once we found an unread child forum, we can drop out of this loop
360 $subforum_unread = true;
366 if ($subforum_row['display'] && $subforum_row['name'])
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,
376 unset($subforums[$forum_id][$subforum_id]);
379 // If one subforum is unread the forum gets unread too...
380 if ($subforum_unread)
382 $forum_unread = true;
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';
391 switch ($row['forum_type'])
394 $folder_image = ($forum_unread) ? 'forum_unread' : 'forum_read';
398 $folder_image = 'forum_link';
403 // Which folder should we display?
404 if ($row['forum_status'] == ITEM_LOCKED)
406 $folder_image = ($forum_unread) ? 'forum_unread_locked' : 'forum_read_locked';
407 $folder_alt = 'FORUM_LOCKED';
411 $folder_alt = ($forum_unread) ? 'UNREAD_POSTS' : 'NO_UNREAD_POSTS';
414 // Create last post link information, if appropriate
415 if ($row['forum_last_post_id'])
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'] . '&p=' . $row['forum_last_post_id']) . '#p' . $row['forum_last_post_id'];
423 $last_post_subject = $last_post_time = $last_post_url = '';
426 // Output moderator listing ... if applicable
427 $l_moderator = $moderators_list = '';
428 if ($display_moderators && !empty($forum_moderators[$forum_id]))
430 $l_moderator = (sizeof($forum_moderators[$forum_id]) == 1) ? $user->lang['MODERATOR'] : $user->lang['MODERATORS'];
431 $moderators_list = implode(', ', $forum_moderators[$forum_id]);
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'] : '';
437 $s_subforums_list = array();
438 foreach ($subforums_list as $subforum)
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>';
442 $s_subforums_list = (string) implode(', ', $s_subforums_list);
443 $catless = ($row['parent_id'] == $root_data['forum_id']) ? true : false;
445 if ($row['forum_type'] != FORUM_LINK)
447 $u_viewforum = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']);
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))
455 $u_viewforum = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']);
459 $u_viewforum = $row['forum_link'];
463 $template->assign_block_vars('forumrow', array(
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,
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,
492 'L_SUBFORUM_STR' => $l_subforums,
493 'L_MODERATOR_STR' => $l_moderator,
495 'U_UNAPPROVED_TOPICS' => ($row['forum_id_unapproved_topics']) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&mode=unapproved_topics&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)
501 // Assign subforums loop for style authors
502 foreach ($subforums_list as $subforum)
504 $template->assign_block_vars('forumrow.subforum', array(
505 'U_SUBFORUM' => $subforum['link'],
506 'SUBFORUM_NAME' => $subforum['name'],
507 'S_UNREAD' => $subforum['unread'])
511 $last_catless = $catless;
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') . '&f=' . $root_data['forum_id'] . '&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'),
522 if ($return_moderators)
524 return array($active_forum_ary, $forum_moderators);
527 return array($active_forum_ary, array());
531 * Create forum rules for given forum
533 function generate_forum_rules(&$forum_data)
535 if (!$forum_data['forum_rules'] && !$forum_data['forum_rules_link'])
540 global $template, $phpbb_root_path, $phpEx;
542 if ($forum_data['forum_rules'])
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']);
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'])
555 * Create forum navigation links for given forum, create parent
556 * list if currently null, assign basic forum info to template
558 function generate_forum_nav(&$forum_data)
560 global $db, $user, $template, $auth, $config;
561 global $phpEx, $phpbb_root_path;
563 if (!$auth->acl_get('f_list', $forum_data['forum_id']))
569 $forum_parents = get_forum_parents($forum_data);
571 // Build navigation links
572 if (!empty($forum_parents))
574 foreach ($forum_parents as $parent_forum_id => $parent_data)
576 list($parent_name, $parent_type) = array_values($parent_data);
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))
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))
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']))
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']),
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,
616 * Returns forum parents as an array. Get them from forum_data if available, or update the database otherwise
618 function get_forum_parents(&$forum_data)
622 $forum_parents = array();
624 if ($forum_data['parent_id'] > 0)
626 if ($forum_data['forum_parents'] == '')
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);
635 while ($row = $db->sql_fetchrow($result))
637 $forum_parents[$row['forum_id']] = array($row['forum_name'], (int) $row['forum_type']);
639 $db->sql_freeresult($result);
641 $forum_data['forum_parents'] = serialize($forum_parents);
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);
650 $forum_parents = unserialize($forum_data['forum_parents']);
654 return $forum_parents;
658 * Generate topic pagination
660 function topic_generate_pagination($replies, $url)
662 global $config, $user;
664 // Make sure $per_page is a valid value
665 $per_page = ($config['posts_per_page'] <= 0) ? 1 : $config['posts_per_page'];
667 if (($replies + 1) > $per_page)
669 $total_pages = ceil(($replies + 1) / $per_page);
673 for ($j = 0; $j < $replies + 1; $j += $per_page)
675 $pagination .= '<a href="' . $url . ($j == 0 ? '' : '&start=' . $j) . '">' . $times . '</a>';
676 if ($times == 1 && $total_pages > 5)
678 $pagination .= '<span class="page-dots"> ... </span>';
680 // Display the last three pages
681 $times = $total_pages - 3;
682 $j += ($total_pages - 4) * $per_page;
684 else if ($times < $total_pages)
686 $pagination .= '<span class="page-sep">' . $user->lang['COMMA_SEPARATOR'] . '</span>';
700 * Obtain list of moderators of each forum
702 function get_moderators(&$forum_moderators, $forum_id = false)
704 global $config, $template, $db, $phpbb_root_path, $phpEx, $user, $auth;
706 $forum_id_ary = array();
708 if ($forum_id !== false)
710 if (!is_array($forum_id))
712 $forum_id = array($forum_id);
715 // Exchange key/value pair to be able to faster check for the forum id existence
716 $forum_id_ary = array_flip($forum_id);
720 'SELECT' => 'm.*, u.user_colour, g.group_colour, g.group_type',
723 MODERATOR_CACHE_TABLE => 'm',
726 'LEFT_JOIN' => array(
728 'FROM' => array(USERS_TABLE => 'u'),
729 'ON' => 'm.user_id = u.user_id',
732 'FROM' => array(GROUPS_TABLE => 'g'),
733 'ON' => 'm.group_id = g.group_id',
737 'WHERE' => 'm.display_on_index = 1',
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);
744 while ($row = $db->sql_fetchrow($result))
746 $f_id = (int) $row['forum_id'];
748 if (!isset($forum_id_ary[$f_id]))
753 if (!empty($row['user_id']))
755 $forum_moderators[$f_id][] = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']);
759 $group_name = (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']);
761 if ($user->data['user_id'] != ANONYMOUS && !$auth->acl_get('u_viewprofile'))
763 $forum_moderators[$f_id][] = '<span' . (($row['group_colour']) ? ' style="color:#' . $row['group_colour'] . ';"' : '') . '>' . $group_name . '</span>';
767 $forum_moderators[$f_id][] = '<a' . (($row['group_colour']) ? ' style="color:#' . $row['group_colour'] . ';"' : '') . ' href="' . append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=group&g=' . $row['group_id']) . '">' . $group_name . '</a>';
771 $db->sql_freeresult($result);
777 * User authorisation levels output
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.
783 function gen_forum_auth_level($mode, $forum_id, $forum_status)
785 global $template, $auth, $user, $config;
787 $locked = ($forum_status == ITEM_LOCKED && !$auth->acl_get('m_edit', $forum_id)) ? true : false;
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'],
796 if ($config['allow_attachments'])
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'];
801 foreach ($rules as $rule)
803 $template->assign_block_vars('rules', array('RULE' => $rule));
810 * Generate topic status
812 function topic_status(&$topic_row, $replies, $unread_topic, &$folder_img, &$folder_alt, &$topic_type)
814 global $user, $config;
816 $folder = $folder_new = '';
818 if ($topic_row['topic_status'] == ITEM_MOVED)
820 $topic_type = $user->lang['VIEW_TOPIC_MOVED'];
821 $folder_img = 'topic_moved';
822 $folder_alt = 'TOPIC_MOVED';
826 switch ($topic_row['topic_type'])
829 $topic_type = $user->lang['VIEW_TOPIC_GLOBAL'];
830 $folder = 'global_read';
831 $folder_new = 'global_unread';
835 $topic_type = $user->lang['VIEW_TOPIC_ANNOUNCEMENT'];
836 $folder = 'announce_read';
837 $folder_new = 'announce_unread';
841 $topic_type = $user->lang['VIEW_TOPIC_STICKY'];
842 $folder = 'sticky_read';
843 $folder_new = 'sticky_unread';
848 $folder = 'topic_read';
849 $folder_new = 'topic_unread';
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)
855 $folder_new .= '_hot';
860 if ($topic_row['topic_status'] == ITEM_LOCKED)
862 $topic_type = $user->lang['VIEW_TOPIC_LOCKED'];
863 $folder .= '_locked';
864 $folder_new .= '_locked';
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');
872 if (!empty($topic_row['topic_posted']) && $topic_row['topic_posted'])
874 $folder_img .= '_mine';
878 if ($topic_row['poll_start'] && $topic_row['topic_status'] != ITEM_MOVED)
880 $topic_type = $user->lang['VIEW_TOPIC_POLL'];
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'
888 function display_custom_bbcodes()
890 global $db, $template, $user;
892 // Start counting from 22 for the bbcode ids (every bbcode takes two ids - opening/closing)
893 $num_predefined_bbcodes = 22;
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);
902 while ($row = $db->sql_fetchrow($result))
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'])]))
907 $row['bbcode_helpline'] = $user->lang[strtoupper($row['bbcode_helpline'])];
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('&', '"', "'", '<', '>'), array('&', '"', "\'", '<', '>'), $row['bbcode_helpline']),
920 $db->sql_freeresult($result);
926 function display_reasons($reason_id = 0)
928 global $db, $user, $template;
931 FROM ' . REPORTS_REASONS_TABLE . '
932 ORDER BY reason_order ASC';
933 $result = $db->sql_query($sql);
935 while ($row = $db->sql_fetchrow($result))
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'])]))
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'])];
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)
951 $db->sql_freeresult($result);
955 * Display user activity (action forum/topic)
957 function display_user_activity(&$userdata)
959 global $auth, $template, $db, $user;
960 global $phpbb_root_path, $phpEx;
962 // Do not display user activity for users having more than 5000 posts...
963 if ($userdata['user_posts'] > 5000)
968 $forum_ary = array();
970 // Do not include those forums the user is not having read access to...
971 $forum_read_ary = $auth->acl_getf('!f_read');
973 foreach ($forum_read_ary as $forum_id => $not_allowed)
975 if ($not_allowed['f_read'])
977 $forum_ary[] = (int) $forum_id;
981 $forum_ary = array_unique($forum_ary);
982 $forum_sql = (sizeof($forum_ary)) ? 'AND ' . $db->sql_in_set('forum_id', $forum_ary, true) : '';
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)) : '';
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
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);
1001 if (!empty($active_f_row))
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);
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) : '';
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
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);
1029 if (!empty($active_t_row))
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);
1039 $userdata['active_t_row'] = $active_t_row;
1040 $userdata['active_f_row'] = $active_f_row;
1042 $active_f_name = $active_f_id = $active_f_count = $active_f_pct = '';
1043 if (!empty($active_f_row['num_posts']))
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;
1051 $active_t_name = $active_t_id = $active_t_count = $active_t_pct = '';
1052 if (!empty($active_t_row['num_posts']))
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;
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'];
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)
1076 * Topic and forum watching common code
1078 function watch_topic_forum($mode, &$s_watching, $user_id, $forum_id, $topic_id, $notify_status = 'unset', $start = 0, $item_title = '')
1080 global $template, $db, $user, $phpEx, $start, $phpbb_root_path;
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') ? '&f' : '&f=' . $forum_id . '&t';
1089 // Is user watching this thread?
1090 if ($user_id != ANONYMOUS)
1094 if ($notify_status == 'unset')
1096 $sql = "SELECT notify_status
1098 WHERE $where_sql = $match_id
1099 AND user_id = $user_id";
1100 $result = $db->sql_query($sql);
1102 $notify_status = ($row = $db->sql_fetchrow($result)) ? $row['notify_status'] : NULL;
1103 $db->sql_freeresult($result);
1106 if (!is_null($notify_status) && $notify_status !== '')
1109 if (isset($_GET['unwatch']))
1111 $uid = request_var('uid', 0);
1112 $token = request_var('hash', '');
1114 if ($token && check_link_hash($token, "{$mode}_$match_id") || confirm_box(true))
1116 if ($uid != $user_id || $_GET['unwatch'] != $mode)
1118 $redirect_url = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&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);
1123 $sql = 'DELETE FROM ' . $table_sql . "
1124 WHERE $where_sql = $match_id
1125 AND user_id = $user_id";
1126 $db->sql_query($sql);
1128 $redirect_url = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&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);
1136 $s_hidden_fields = array(
1137 'uid' => $user->data['user_id'],
1142 if ($mode != 'forum')
1144 $s_hidden_fields['t'] = $topic_id;
1147 if ($item_title == '')
1149 $confirm_box_message = 'UNWATCH_' . strtoupper($mode);
1153 $confirm_box_message = $user->lang('UNWATCH_' . strtoupper($mode) . '_DETAILED', $item_title);
1155 confirm_box(false, $confirm_box_message, build_hidden_fields($s_hidden_fields));
1160 $is_watching = true;
1162 if ($notify_status != NOTIFY_YES)
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);
1174 if (isset($_GET['watch']))
1176 $uid = request_var('uid', 0);
1177 $token = request_var('hash', '');
1179 if ($token && check_link_hash($token, "{$mode}_$match_id") || confirm_box(true))
1181 if ($uid != $user_id || $_GET['watch'] != $mode)
1183 $redirect_url = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&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);
1188 $is_watching = true;
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);
1194 $redirect_url = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&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);
1201 $s_hidden_fields = array(
1202 'uid' => $user->data['user_id'],
1207 if ($mode != 'forum')
1209 $s_hidden_fields['t'] = $topic_id;
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));
1224 if ((isset($_GET['unwatch']) && $_GET['unwatch'] == $mode) || (isset($_GET['watch']) && $_GET['watch'] == $mode))
1237 $s_watching['link'] = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&" . (($is_watching) ? 'unwatch' : 'watch') . "=$mode&start=$start&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;
1246 * Get user rank title and image
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
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
1256 function get_user_rank($user_rank, $user_posts, &$rank_title, &$rank_img, &$rank_img_src)
1258 global $ranks, $config, $phpbb_root_path;
1263 $ranks = $cache->obtain_ranks();
1266 if (!empty($user_rank))
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'] : '';
1272 else if ($user_posts !== false)
1274 if (!empty($ranks['normal']))
1276 foreach ($ranks['normal'] as $rank)
1278 if ($user_posts >= $rank['rank_min'])
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'] : '';
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
1300 * @return string Avatar image
1302 function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $alt = 'USER_AVATAR', $ignore_config = false)
1304 global $user, $config, $phpbb_root_path, $phpEx;
1306 if (empty($avatar) || !$avatar_type || (!$config['allow_avatar'] && !$ignore_config))
1313 switch ($avatar_type)
1316 if (!$config['allow_avatar_upload'] && !$ignore_config)
1320 $avatar_img = $phpbb_root_path . "download/file.$phpEx?avatar=";
1323 case AVATAR_GALLERY:
1324 if (!$config['allow_avatar_local'] && !$ignore_config)
1328 $avatar_img = $phpbb_root_path . $config['avatar_gallery_path'] . '/';
1332 if (!$config['allow_avatar_remote'] && !$ignore_config)
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) . '" />';