Viewing file: get_forum.php (17.93 KB) -rw-rw-r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
/**
*
* @author Robert Johnston
*
* @package Forum Runner
* @version CVS/SVN: $Id: $
* @copyright (c) 2010 End of Time Studios, LLC
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
chdir(MCWD);
chdir('../');
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
fr_set_debug();
$user->session_begin();
$auth->acl($user->data);
$user->setup('viewforum');
$user->page['root_script_path'] = str_replace('/forumrunner', '', $user->page['root_script_path']);
function
do_get_forum ()
{
global $user, $db, $auth, $config, $template, $topics_count, $forum_id,
$sort_days, $sort_key, $sort_dir, $forum_data, $start;
$fr_forum_data = $fr_thread_data = $fr_announce_data = $fr_sticky_thread_data = array();
$forum_id = request_var('forumid', 0);
$mark_read = request_var('mark', '');
// We send page & num, figure out start
$page = request_var('page', 1);
$perpage = request_var('perpage', 15);
$config['topics_per_page'] = $perpage;
$start = (($page - 1) * $perpage);
$default_sort_days = (!empty($user->data['user_topic_show_days'])) ? $user->data['user_topic_show_days'] : 0;
$default_sort_key = (!empty($user->data['user_topic_sortby_type'])) ? $user->data['user_topic_sortby_type'] : 't';
$default_sort_dir = (!empty($user->data['user_topic_sortby_dir'])) ? $user->data['user_topic_sortby_dir'] : 'd';
$sort_days = request_var('st', $default_sort_days);
$sort_key = request_var('sk', $default_sort_key);
$sort_dir = request_var('sd', $default_sort_dir);
// Forums.
if (!$forum_id || $forum_id == -1) {
$fr_forum_data = fr_get_forum_data(0);
$topics_count = 0;
} else {
$sql_from = FORUMS_TABLE . ' f';
$lastread_select = '';
// Grab appropriate forum data
if ($config['load_db_lastread'] && $user->data['is_registered'])
{
$sql_from .= ' LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
AND ft.forum_id = f.forum_id)';
$lastread_select .= ', ft.mark_time';
}
if ($user->data['is_registered'])
{
$sql_from .= ' LEFT JOIN ' . FORUMS_WATCH_TABLE . ' fw ON (fw.forum_id = f.forum_id AND fw.user_id = ' . $user->data['user_id'] . ')';
$lastread_select .= ', fw.notify_status';
}
$sql = "SELECT f.* $lastread_select
FROM $sql_from
WHERE f.forum_id = $forum_id";
$result = $db->sql_query($sql);
$forum_data = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
if (!$forum_data)
{
trigger_error('NO_FORUM');
}
// Permissions check
if (!$auth->acl_gets('f_list', 'f_read', $forum_id) || ($forum_data['forum_type'] == FORUM_LINK && $forum_data['forum_link'] && !$auth->acl_get('f_read', $forum_id)))
{
if ($user->data['user_id'] != ANONYMOUS)
{
trigger_error('SORRY_AUTH_READ');
}
trigger_error('SORRY_AUTH_READ');
}
// Forum is passworded ... check whether access has been granted to this
// user this session, if not show login box
if ($forum_data['forum_password'])
{
if (!fr_login_forum_box($forum_data)) {
json_error(ERR_NEED_PASSWORD, RV_NEED_FORUM_PASSWORD);
}
}
$fr_forum_data = fr_get_forum_data($forum_id);
$topics_count = ($auth->acl_get('m_approve', $forum_id)) ? $forum_data['forum_topics_real'] : $forum_data['forum_topics'];
}
$out['forums'] = $fr_forum_data;
$out['canpost'] = $auth->acl_get('f_post', $forum_id);
// Threads
if ($auth->acl_get('f_read', $forum_id)) {
// Get Stickies
$tmp = process_forums(POST_STICKY, false);
$fr_sticky_thread_data = $tmp['threads'];
// Get Normal/Announcement
$tmp = process_forums(POST_NORMAL, true);
$fr_thread_data = $tmp['threads'];
$fr_announce_data = $tmp['announcements'];
}
$out['threads'] = $fr_thread_data;
$out['total_threads'] = $topics_count;
if ($page == 1) {
$out['threads_sticky'] = $fr_sticky_thread_data;
$out['total_sticky_threads'] = count($fr_sticky_thread_data);
$out['announcements'] = $fr_announce_data;
$out['total_announcements'] = count($fr_announce_data);
}
return $out;
}
function
fr_get_forum_data ($forum_id)
{
global $db, $user, $forum_data, $auth;
$out = array();
$where = '';
$fr_exclude = fr_get_config('fr_exclude_forums');
if (strlen($fr_exclude)) {
$where .= ' AND ' .
$db->sql_in_set('f.forum_id', split(',', fr_get_config('fr_exclude_forums')), true, true) . ' AND ' .
$db->sql_in_set('f.parent_id', split(',', fr_get_config('fr_exclude_forums')), true, true);
}
// Get Forum Info
$sql = $db->sql_build_query('SELECT', array(
'SELECT' => 'f.*',
'FROM' => array(
FORUMS_TABLE => 'f',
),
'ORDER_BY' => 'f.left_id ASC',
'WHERE' => 'f.parent_id = ' . $forum_id . $where
));
$result = $db->sql_query($sql);
$skip_until = false;
while ($row = $db->sql_fetchrow($result)) {
if ($skip_until !== false && $row['left_id'] < $skip_until) {
continue;
}
$skip_until = false;
if (!$auth->acl_get('f_list', $row['forum_id'])) {
$skip_until = $row['right_id'];
continue;
}
// If this forum has a password, check to see if we have the
// proper cookie. If so, don't prompt for one.
$password = false;
if ($row['forum_password'] != '') {
$sql = 'SELECT forum_id
FROM ' . FORUMS_ACCESS_TABLE . '
WHERE forum_id = ' . $row['forum_id'] . '
AND user_id = ' . $user->data['user_id'] . "
AND session_id = '" . $db->sql_escape($user->session_id) . "'";
$presult = $db->sql_query($sql);
$prow = $db->sql_fetchrow($presult);
$db->sql_freeresult($presult);
if (!$prow) {
$password = true;
}
}
$data = array(
'id' => $row['forum_id'],
'name' => prepare_utf8_string(strip_tags($row['forum_name'])),
'password' => $password,
);
$icon = fr_get_forum_icon($forum_id, true);
if ($icon) {
$data['icon'] = $icon;
}
if ($row['forum_link'] != '') {
$data['link'] = fr_fix_url($row['forum_link']);
$linkicon = fr_get_forum_icon($forum_id, false, true);
if ($linkicon) {
$data['icon'] = $linkicon;
}
}
if ($row['forum_desc'] != '') {
$data['desc'] = prepare_utf8_string(strip_tags($row['forum_desc']));
}
$out[] = $data;
}
return $out;
}
function
process_forums ($topic_type, $limit = false)
{
global $user, $db, $auth, $config, $template, $topics_count, $forum_id,
$sort_days, $sort_key, $sort_dir, $forum_data, $start;
$previewtype = request_var('previewtype', 2);
$out = $out_announce = $active_forum_ary = array();
$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 'r' => $user->lang['REPLIES'], 's' => $user->lang['SUBJECT'], 'v' => $user->lang['VIEWS']);
$sort_by_sql = array('a' => 't.topic_first_poster_name', 't' => 't.topic_last_post_time', 'r' => 't.topic_replies', 's' => 't.topic_title', 'v' => 't.topic_views');
$sql_limit_time = '';
// Make sure $start is set to the last page if it exceeds the amount
if ($start < 0 || $start > $topics_count)
{
$start = ($start < 0) ? 0 : floor(($topics_count - 1) / $config['topics_per_page']) * $config['topics_per_page'];
}
$s_display_active = ($forum_data['forum_type'] == FORUM_CAT && ($forum_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS)) ? true : false;
// Grab all topic data
$rowset = $announcement_list = $topic_list = $global_announce_list = array();
$sql_array = array(
'SELECT' => 't.*',
'FROM' => array(
TOPICS_TABLE => 't'
),
'LEFT_JOIN' => array(),
);
$sql_approved = ($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND t.topic_approved = 1';
if ($user->data['is_registered'])
{
if ($config['load_db_track'])
{
$sql_array['LEFT_JOIN'][] = array('FROM' => array(TOPICS_POSTED_TABLE => 'tp'), 'ON' => 'tp.topic_id = t.topic_id AND tp.user_id = ' . $user->data['user_id']);
$sql_array['SELECT'] .= ', tp.topic_posted';
}
if ($config['load_db_lastread'])
{
$sql_array['LEFT_JOIN'][] = array('FROM' => array(TOPICS_TRACK_TABLE => 'tt'), 'ON' => 'tt.topic_id = t.topic_id AND tt.user_id = ' . $user->data['user_id']);
$sql_array['SELECT'] .= ', tt.mark_time';
if ($s_display_active && sizeof($active_forum_ary))
{
$sql_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TRACK_TABLE => 'ft'), 'ON' => 'ft.forum_id = t.forum_id AND ft.user_id = ' . $user->data['user_id']);
$sql_array['SELECT'] .= ', ft.mark_time AS forum_mark_time';
}
}
}
if ($forum_data['forum_type'] == FORUM_POST)
{
// Obtain announcements ... removed sort ordering, sort by time in all cases
$sql = $db->sql_build_query('SELECT', array(
'SELECT' => $sql_array['SELECT'],
'FROM' => $sql_array['FROM'],
'LEFT_JOIN' => $sql_array['LEFT_JOIN'],
'WHERE' => 't.forum_id IN (' . $forum_id . ', 0)
AND t.topic_type IN (' . POST_ANNOUNCE . ', ' . POST_GLOBAL . ')',
'ORDER_BY' => 't.topic_time DESC',
));
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$rowset[$row['topic_id']] = $row;
$announcement_list[] = $row['topic_id'];
if ($row['topic_type'] == POST_GLOBAL)
{
$global_announce_list[$row['topic_id']] = true;
}
else
{
$topics_count--;
}
}
$db->sql_freeresult($result);
}
// If the user is trying to reach late pages, start searching from the end
$store_reverse = false;
$sql_limit = $config['topics_per_page'];
if ($start > $topics_count / 2)
{
$store_reverse = true;
if ($start + $config['topics_per_page'] > $topics_count)
{
$sql_limit = min($config['topics_per_page'], max(1, $topics_count - $start));
}
// Select the sort order
$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'ASC' : 'DESC');
$sql_start = max(0, $topics_count - $sql_limit - $start);
}
else
{
// Select the sort order
$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
$sql_start = $start;
}
if ($forum_data['forum_type'] == FORUM_POST || !sizeof($active_forum_ary))
{
$sql_where = 't.forum_id = ' . $forum_id;
}
else if (empty($active_forum_ary['exclude_forum_id']))
{
$sql_where = $db->sql_in_set('t.forum_id', $active_forum_ary['forum_id']);
}
else
{
$get_forum_ids = array_diff($active_forum_ary['forum_id'], $active_forum_ary['exclude_forum_id']);
$sql_where = (sizeof($get_forum_ids)) ? $db->sql_in_set('t.forum_id', $get_forum_ids) : 't.forum_id = ' . $forum_id;
}
// Grab just the sorted topic ids
$sql = 'SELECT t.topic_id
FROM ' . TOPICS_TABLE . " t
WHERE $sql_where
AND t.topic_type IN (" . $topic_type . ")
$sql_approved
$sql_limit_time
ORDER BY t.topic_type " . ((!$store_reverse) ? 'DESC' : 'ASC') . ', ' . $sql_sort_order;
if ($limit) {
$result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
} else {
$result = $db->sql_query($sql);
}
while ($row = $db->sql_fetchrow($result))
{
$topic_list[] = (int) $row['topic_id'];
}
$db->sql_freeresult($result);
// For storing shadow topics
$shadow_topic_list = array();
if (sizeof($topic_list))
{
// SQL array for obtaining topics/stickies
$sql_array = array(
'SELECT' => $sql_array['SELECT'],
'FROM' => $sql_array['FROM'],
'LEFT_JOIN' => $sql_array['LEFT_JOIN'],
'WHERE' => $db->sql_in_set('t.topic_id', $topic_list),
);
// If store_reverse, then first obtain topics, then stickies, else the other way around...
// Funnily enough you typically save one query if going from the last page to the middle (store_reverse) because
// the number of stickies are not known
$sql = $db->sql_build_query('SELECT', $sql_array);
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
if ($row['topic_status'] == ITEM_MOVED)
{
$shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];
}
$rowset[$row['topic_id']] = $row;
}
$db->sql_freeresult($result);
}
// If we have some shadow topics, update the rowset to reflect their topic information
if (sizeof($shadow_topic_list))
{
$sql = 'SELECT *
FROM ' . TOPICS_TABLE . '
WHERE ' . $db->sql_in_set('topic_id', array_keys($shadow_topic_list));
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$orig_topic_id = $shadow_topic_list[$row['topic_id']];
// If the shadow topic is already listed within the rowset (happens for active topics for example), then do not include it...
if (isset($rowset[$row['topic_id']]))
{
// We need to remove any trace regarding this topic. :)
unset($rowset[$orig_topic_id]);
unset($topic_list[array_search($orig_topic_id, $topic_list)]);
$topics_count--;
continue;
}
// Do not include those topics the user has no permission to access
if (!$auth->acl_get('f_read', $row['forum_id']))
{
// We need to remove any trace regarding this topic. :)
unset($rowset[$orig_topic_id]);
unset($topic_list[array_search($orig_topic_id, $topic_list)]);
$topics_count--;
continue;
}
// We want to retain some values
$row = array_merge($row, array(
'topic_moved_id' => $rowset[$orig_topic_id]['topic_moved_id'],
'topic_status' => $rowset[$orig_topic_id]['topic_status'],
'topic_type' => $rowset[$orig_topic_id]['topic_type'],
));
// Shadow topics are never reported
$row['topic_reported'] = 0;
$rowset[$orig_topic_id] = $row;
}
$db->sql_freeresult($result);
}
unset($shadow_topic_list);
// Ok, adjust topics count for active topics list
if ($s_display_active)
{
$topics_count = 1;
}
$topic_list = ($store_reverse) ? array_merge($announcement_list, array_reverse($topic_list)) : array_merge($announcement_list, $topic_list);
$topic_tracking_info = $tracking_topics = array();
if (sizeof($topic_list)) {
$mark_forum_read = true;
$mark_time_forum = 0;
if ($config['load_db_lastread'] && $user->data['is_registered'])
{
$topic_tracking_info = get_topic_tracking($forum_id, $topic_list, $rowset, array($forum_id => $forum_data['mark_time']), $global_announce_list);
$mark_time_forum = (!empty($forum_data['mark_time'])) ? $forum_data['mark_time'] : $user->data['user_lastmark'];
}
else if ($config['load_anon_lastread'] || $user->data['is_registered'])
{
$topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_list, $global_announce_list);
if (!$user->data['is_registered'])
{
$user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
}
$mark_time_forum = (isset($tracking_topics['f'][$forum_id])) ? (int) (base_convert($tracking_topics['f'][$forum_id], 36, 10) + $config['board_startdate']) : $user->data['user_lastmark'];
}
foreach ($topic_list as $topic_id) {
$thread = &$rowset[$topic_id];
// Replies
$replies = ($auth->acl_get('m_approve', $forum_id)) ? $thread['topic_replies_real'] : $thread['topic_replies'];
if ($thread['topic_status'] == ITEM_MOVED)
{
$topic_id = $thread['topic_moved_id'];
$unread_topic = false;
}
else
{
$unread_topic = (isset($topic_tracking_info[$topic_id]) && $thread['topic_last_post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
}
$avresult = $db->sql_query("
SELECT u.user_avatar, u.user_avatar_type, u.user_avatar_width, u.user_avatar_height
FROM " . POSTS_TABLE . " p
LEFT JOIN " . USERS_TABLE . " u
ON u.user_id = p.poster_id
WHERE p.post_id = {$thread['topic_first_post_id']}
");
$avrow = $db->sql_fetchrow($avresult);
$db->sql_freeresult($avresult);
$avatarurl = process_avatarurl(fr_get_user_avatar($avrow['user_avatar'], $avrow['user_avatar_type'], $avrow['user_avatar_width'], $avrow['user_avatar_height']));
$tmp = array(
'thread_id' => $thread['topic_id'],
'new_posts' => $unread_topic,
'forum_id' => $thread['forum_id'],
'thread_title' => prepare_utf8_string($thread['topic_title']),
'total_posts' => $thread['topic_replies'],
'post_lastposttime' => prepare_utf8_string(date_trunc($user->format_date($thread['topic_last_post_time']))),
);
if ($previewtype == 1) {
// First Post
$tmp = array_merge($tmp, array(
'post_username' => prepare_utf8_string(strip_tags($thread['topic_first_poster_name'])),
'post_userid' => $thread['topic_poster'],
'thread_preview' => prepare_utf8_string(fr_get_preview($thread['topic_first_post_id'])),
));
} else if ($previewtype == 2) {
// Last Post
$tmp = array_merge($tmp, array(
'post_username' => prepare_utf8_string(strip_tags($thread['topic_last_poster_name'])),
'post_userid' => $thread['topic_last_poster_id'],
'thread_preview' => prepare_utf8_string(fr_get_preview($thread['topic_last_post_id'])),
));
}
if ($avatarurl != '') {
$tmp['avatarurl'] = $avatarurl;
}
if ($thread['poll_start']) {
$tmp['poll'] = true;
}
if ($thread['topic_attachment']) {
$tmp['attach'] = true;
}
if ($topic_type == POST_NORMAL) {
if (in_array($thread['topic_id'], $announcement_list)) {
$out_announce[] = $tmp;
} else {
$out[] = $tmp;
}
} else if ($topic_type == POST_STICKY && !in_array($thread['topic_id'], $announcement_list)) {
$out[] = $tmp;
}
}
}
return array(
'threads' => $out,
'announcements' => $out_announce,
);
}
?>
|