function sp_do_sp_TopicLinkTag($args = '')
{
    #check if forum displayed
    if (sp_abort_display_forum()) {
        return;
    }
    $defs = array('topicId' => '', 'linkText' => '%TOPICNAME%', 'beforeLink' => '', 'afterLink' => '', 'listTags' => 0, 'echo' => 1);
    $a = wp_parse_args($args, $defs);
    $a = apply_filters('sph_TopicLinkTag_args', $a);
    extract($a, EXTR_SKIP);
    # sanitize before use
    $topicId = (int) $topicId;
    $linkText = esc_attr($linkText);
    $beforeLink = sp_filter_title_display($beforeLink);
    $afterLink = sp_filter_title_display($afterLink);
    $listTags = (int) $listTags;
    $echo = (int) $echo;
    if (empty($topicId)) {
        return '';
    }
    sp_forum_api_support();
    if (!empty($beforeLink)) {
        $beforeLink = trim($beforeLink) . ' ';
    }
    if (!empty($afterLink)) {
        $afterLink = ' ' . trim($afterLink);
    }
    $spdb = new spdbComplex();
    $spdb->table = SFTOPICS;
    $spdb->fields = SFTOPICS . '.topic_id, ' . SFTOPICS . '.forum_id, topic_slug, topic_name, forum_name, forum_slug';
    $spdb->join = array(SFFORUMS . ' ON ' . SFTOPICS . '.forum_id = ' . SFFORUMS . '.forum_id');
    $spdb->where = SFTOPICS . '.topic_id=' . $topicId;
    $thistopic = $spdb->select();
    $out = '';
    if ($thistopic) {
        if (sp_can_view($thistopic[0]->forum_id, 'topic-title')) {
            $out = '';
            $linkText = str_replace("%TOPICNAME%", sp_filter_title_display($thistopic[0]->topic_name), $linkText);
            if (empty($linkText)) {
                $linkText = sp_filter_title_display($thistopic[0]->topic_name);
            }
            if ($listTags) {
                $out .= '<li>';
            }
            $out .= '<span>' . $beforeLink . '<a href="' . sp_build_url($thistopic[0]->forum_slug, $thistopic[0]->topic_slug, 0, 0) . '">' . $linkText . '</a>' . $afterLink . '</span>';
            if ($listTags) {
                $out .= '</li>';
            }
        }
    } else {
        $out = sprintf(__('Topic %s not found', 'sp-ttags'), $topicId);
    }
    $out = apply_filters('sph_TopicLinkTag', $out);
    if ($echo) {
        echo $out;
    } else {
        return $out;
    }
}
function sp_do_sp_GroupLinkTag($args = '')
{
    #check if forum displayed
    if (sp_abort_display_forum()) {
        return;
    }
    $defs = array('groupId' => '', 'linkText' => '%GROUPNAME%', 'beforeLink' => '', 'afterLink' => '', 'listTags' => 0, 'echo' => 1);
    $a = wp_parse_args($args, $defs);
    $a = apply_filters('sph_GroupLinkTag_args', $a);
    extract($a, EXTR_SKIP);
    # sanitize before use
    $groupId = (int) $groupId;
    $linkText = esc_attr($linkText);
    $beforeLink = sp_filter_title_display($beforeLink);
    $afterLink = sp_filter_title_display($afterLink);
    $listTags = (int) $listTags;
    $echo = (int) $echo;
    if (empty($groupId)) {
        return '';
    }
    sp_forum_api_support();
    if (!empty($beforeLink)) {
        $beforeLink = trim($beforeLink) . ' ';
    }
    if (!empty($afterLink)) {
        $afterLink = ' ' . trim($afterLink);
    }
    # check user has access to at kleast ine forum in group
    $canview = false;
    $forums = spdb_table(SFFORUMS, "group_id={$groupId}");
    if ($forums) {
        foreach ($forums as $forum) {
            if (sp_can_view($forum->forum_id, 'forum-title')) {
                $canview = true;
            }
        }
    }
    if ($forums && $canview) {
        $grouprec = spdb_table(SFGROUPS, "group_id={$groupId}", 'row');
        $out = '';
        $linkText = str_replace("%GROUPNAME%", sp_filter_title_display($grouprec->group_name), $linkText);
        if (empty($linkText)) {
            $linkText = sp_filter_title_display($grouprec->group_name);
        }
        if ($listTags) {
            $out .= '<li>';
        }
        $out .= '<span>' . $beforeLink . '<a href="' . add_query_arg(array('group' => $groupId), sp_url()) . '">' . $linkText . '</a>' . $afterLink . '</span>';
        if ($listTags) {
            $out .= '</li>';
        }
    }
    $out = apply_filters('sph_GroupLinkTag', $out);
    if ($echo) {
        echo $out;
    } else {
        return $out;
    }
}
function sp_do_sp_ForumLinkTag($args = '')
{
    #check if forum displayed
    if (sp_abort_display_forum()) {
        return;
    }
    $defs = array('forumId' => '', 'linkText' => '%FORUMNAME%', 'beforeLink' => '', 'afterLink' => '', 'listTags' => 0, 'echo' => 1);
    $a = wp_parse_args($args, $defs);
    $a = apply_filters('sph_ForumLinkTag_args', $a);
    extract($a, EXTR_SKIP);
    # sanitize before use
    $forumId = (int) $forumId;
    $linkText = esc_attr($linkText);
    $beforeLink = sp_filter_title_display($beforeLink);
    $afterLink = sp_filter_title_display($afterLink);
    $listTags = (int) $listTags;
    $echo = (int) $echo;
    if (empty($forumId)) {
        return '';
    }
    sp_forum_api_support();
    if (!empty($beforeLink)) {
        $beforeLink = trim($beforeLink) . ' ';
    }
    if (!empty($afterLink)) {
        $afterLink = ' ' . trim($afterLink);
    }
    if (sp_can_view($forumId, 'forum-title')) {
        $forumrec = spdb_table(SFFORUMS, "forum_id={$forumId}", 'row');
        if ($forumrec) {
            $out = '';
            $linkText = str_replace("%FORUMNAME%", sp_filter_title_display($forumrec->forum_name), $linkText);
            if (empty($linkText)) {
                $linkText = sp_filter_title_display($forumrec->forum_name);
            }
            if ($listTags) {
                $out .= '<li>';
            }
            $out .= '<span>' . $beforeLink . '<a href="' . sp_build_url($forumrec->forum_slug, '', 0, 0) . '">' . $linkText . '</a>' . $afterLink . '</span>';
            if ($listTags) {
                $out .= '</li>';
            }
        } else {
            $out = sprintf(__('Forum %s not found', 'sp-ttags'), $forumId);
        }
    }
    $out = apply_filters('sph_ForumLinkTag', $out);
    if ($echo) {
        echo $out;
    } else {
        return $out;
    }
}
function sp_admin_bar_do_new_post_list($addpostlist, $newpostlist)
{
    global $spThisUser;
    # Add waiting if admin or moderator (->moderator covers both)
    if ($spThisUser->moderator && $spThisUser->sfadminbar) {
        $records = spdb_select('set', "SELECT DISTINCT topic_id, forum_id FROM " . SFWAITING, ARRAY_A);
        if ($records) {
            foreach ($records as $r) {
                if (sp_can_view($r['forum_id'], 'topic-title')) {
                    if (!in_array($r['topic_id'], $addpostlist['topics']) && !in_array($r['topic_id'], $newpostlist['topics'])) {
                        $addpostlist['topics'][] = $r['topic_id'];
                        $addpostlist['forums'][] = $r['forum_id'];
                    }
                }
            }
        }
    }
    return $addpostlist;
}
    function sp_groupview_query($groupids = '', $idOrder = false)
    {
        global $spThisUser;
        # can we get the results from the cache?
        $records = array();
        if (empty($spThisUser->inspect['q_spGroupView'])) {
            $records = sp_get_cache('group');
        }
        if (!$records) {
            $WHERE = '';
            if (!empty($groupids)) {
                $gcount = count($groupids);
                $done = 0;
                foreach ($groupids as $id) {
                    $WHERE .= '(' . SFGROUPS . ".group_id={$id})";
                    $done++;
                    if ($done < $gcount) {
                        $WHERE .= ' OR ';
                    }
                }
            }
            $this->groupViewStatus = empty($groupids) ? 'no data' : 'no access';
            # retrieve group and forum records
            $spdb = new spdbComplex();
            $spdb->table = SFGROUPS;
            $spdb->fields = SFGROUPS . '.group_id, group_name, group_desc, group_rss, group_icon, group_message,
								forum_id, forum_name, forum_slug, forum_desc, forum_status, forum_disabled, forum_icon, forum_icon_new, forum_icon_locked, forum_rss_private,
								post_id, post_id_held, topic_count, post_count, post_count_held, parent, children';
            $spdb->join = array(SFFORUMS . ' ON ' . SFGROUPS . '.group_id = ' . SFFORUMS . '.group_id');
            $spdb->where = $WHERE;
            $spdb->orderby = 'group_seq, forum_seq';
            $spdb = apply_filters('sph_groupview_query', $spdb, $this);
            if (!empty($spThisUser->inspect['q_spGroupView'])) {
                $spdb->inspect = 'spGroupView';
                $spdb->show = true;
                $spThisUser->inspect['q_spGroupView'] = false;
            }
            $records = $spdb->select();
            if ($records) {
                sp_add_cache('group', $records);
            }
        }
        $g = '';
        if ($records) {
            # Set status initially to 'no access' in case current user can view no forums
            $this->groupViewStatus = 'no access';
            $gidx = 0;
            $fidx = 0;
            $sidx = 0;
            $cparent = 0;
            $subPostId = 0;
            # define array to collect data
            $p = array();
            $g = array();
            foreach ($records as $r) {
                $groupid = $r->group_id;
                $forumid = $r->forum_id;
                if (sp_can_view($forumid, 'forum-title')) {
                    if ($gidx == 0 || $g[$gidx]->group_id != $groupid) {
                        # reset status to 'data'
                        $this->groupViewStatus = 'data';
                        $gidx = $groupid;
                        $fidx = 0;
                        $g[$gidx] = new stdClass();
                        $g[$gidx]->group_id = $r->group_id;
                        $g[$gidx]->group_name = sp_filter_title_display($r->group_name);
                        $g[$gidx]->group_desc = sp_filter_title_display($r->group_desc);
                        $g[$gidx]->group_rss = esc_url($r->group_rss);
                        $g[$gidx]->group_icon = sanitize_file_name($r->group_icon);
                        $g[$gidx]->group_message = sp_filter_text_display($r->group_message);
                        $g[$gidx]->group_rss_active = 0;
                        $g[$gidx] = apply_filters('sph_groupview_group_records', $g[$gidx], $r);
                    }
                    if (isset($r->forum_id)) {
                        # Is this a subform?
                        if ($r->parent != 0) {
                            $sidx = $r->forum_id;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx] = new stdClass();
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_id = $r->forum_id;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_name = sp_filter_title_display($r->forum_name);
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_slug = $r->forum_slug;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_icon = sanitize_file_name($r->forum_icon);
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_icon_new = sanitize_file_name($r->forum_icon_new);
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_icon_locked = sanitize_file_name($r->forum_icon_locked);
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_disabled = $r->forum_disabled;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_permalink = sp_build_url($r->forum_slug, '', 1, 0);
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->topic_count = $r->topic_count;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_count = $r->post_count;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->parent = $r->parent;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->children = $r->children;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_id = $r->post_id;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->unread = 0;
                            # check if we can look at posts in moderation - if not swap for 'held' values
                            if (!sp_get_auth('moderate_posts', $r->forum_id)) {
                                $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_count = $r->post_count_held;
                                $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_id = $r->post_id_held;
                            }
                            # See if any forums are in the current users newpost list
                            if ($spThisUser->member && isset($spThisUser->newposts['forums'])) {
                                $c = 0;
                                if ($spThisUser->newposts['forums']) {
                                    foreach ($spThisUser->newposts['forums'] as $fnp) {
                                        if ($fnp == $sidx) {
                                            $c++;
                                        }
                                    }
                                }
                                # set the subforum unread count
                                $g[$gidx]->forums[$cparent]->subforums[$sidx]->unread = $c;
                            }
                            # Update top parent counts with subforum counts
                            $g[$gidx]->forums[$cparent]->topic_count_sub += $g[$gidx]->forums[$cparent]->subforums[$sidx]->topic_count;
                            $g[$gidx]->forums[$cparent]->post_count_sub += $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_count;
                            # and what about the most recent post? Is this in a subforum?
                            if ($g[$gidx]->forums[$cparent]->subforums[$sidx]->post_id > $g[$gidx]->forums[$cparent]->post_id && $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_id > $subPostId) {
                                # store the alternative forum id in case we need to display the topic data for this one if inc. subs
                                $g[$gidx]->forums[$cparent]->forum_id_sub = $r->forum_id;
                                # add the last post in subforum to the list for stats retrieval
                                $subPostId = $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_id;
                                $p[$r->forum_id] = $subPostId;
                            }
                        } else {
                            # it's a top level forum
                            $subPostId = 0;
                            $fidx = $forumid;
                            $g[$gidx]->forums[$fidx] = new stdClass();
                            $g[$gidx]->forums[$fidx]->forum_id = $r->forum_id;
                            $g[$gidx]->forums[$fidx]->forum_id_sub = 0;
                            $g[$gidx]->forums[$fidx]->forum_name = sp_filter_title_display($r->forum_name);
                            $g[$gidx]->forums[$fidx]->forum_slug = $r->forum_slug;
                            $g[$gidx]->forums[$fidx]->forum_permalink = sp_build_url($r->forum_slug, '', 1, 0);
                            $g[$gidx]->forums[$fidx]->forum_desc = sp_filter_title_display($r->forum_desc);
                            $g[$gidx]->forums[$fidx]->forum_status = $r->forum_status;
                            $g[$gidx]->forums[$fidx]->forum_disabled = $r->forum_disabled;
                            $g[$gidx]->forums[$fidx]->forum_icon = sanitize_file_name($r->forum_icon);
                            $g[$gidx]->forums[$fidx]->forum_icon_new = sanitize_file_name($r->forum_icon_new);
                            $g[$gidx]->forums[$fidx]->forum_icon_locked = sanitize_file_name($r->forum_icon_locked);
                            $g[$gidx]->forums[$fidx]->forum_rss_private = $r->forum_rss_private;
                            $g[$gidx]->forums[$fidx]->post_id = $r->post_id;
                            $g[$gidx]->forums[$fidx]->topic_count = $r->topic_count;
                            $g[$gidx]->forums[$fidx]->topic_count_sub = $r->topic_count;
                            $g[$gidx]->forums[$fidx]->post_count = $r->post_count;
                            $g[$gidx]->forums[$fidx]->post_count_sub = $r->post_count;
                            $g[$gidx]->forums[$fidx]->parent = $r->parent;
                            $g[$gidx]->forums[$fidx]->children = $r->children;
                            $g[$gidx]->forums[$fidx]->unread = 0;
                            if (empty($g[$gidx]->forums[$fidx]->post_id)) {
                                $g[$gidx]->forums[$fidx]->post_id = 0;
                            }
                            # Can the user create new topics or should we lock the forum?
                            $g[$gidx]->forums[$fidx]->start_topics = sp_get_auth('start_topics', $r->forum_id);
                            # check if we can look at posts in moderation - if not swap for 'held' values
                            if (!sp_get_auth('moderate_posts', $r->forum_id)) {
                                $g[$gidx]->forums[$fidx]->post_id = $r->post_id_held;
                                $g[$gidx]->forums[$fidx]->post_count = $r->post_count_held;
                                $g[$gidx]->forums[$fidx]->post_count_sub = $r->post_count_held;
                                $thisPostid = $r->post_id_held;
                            } else {
                                $thisPostid = $r->post_id;
                            }
                            # See if any forums are in the current users newpost list
                            if ($spThisUser->member && isset($spThisUser->newposts['forums'])) {
                                $c = 0;
                                if ($spThisUser->newposts['forums']) {
                                    foreach ($spThisUser->newposts['forums'] as $fnp) {
                                        if ($fnp == $fidx) {
                                            $c++;
                                        }
                                    }
                                }
                                $g[$gidx]->forums[$fidx]->unread = $c;
                            }
                            if (empty($r->children)) {
                                $cparent = 0;
                            } else {
                                $cparent = $fidx;
                                $sidx = 0;
                            }
                            # Build post id array for collecting stats at the end
                            if (!empty($thisPostid)) {
                                $p[$fidx] = $thisPostid;
                            }
                            $g[$gidx]->forums[$fidx] = apply_filters('sph_groupview_forum_records', $g[$gidx]->forums[$fidx], $r);
                        }
                        # Build special Group level flag on whether to show group RSS button or not (based on any forum in group having RSS access
                        if (sp_get_auth('view_forum', $r->forum_id) && !$r->forum_rss_private) {
                            $g[$gidx]->group_rss_active = 1;
                        }
                    }
                }
            }
        }
        if ($this->includeStats == true) {
            # Go grab the forum stats and data
            if (!empty($p)) {
                $stats = $this->sp_groupview_stats_query($p);
                if ($stats) {
                    foreach ($g as $gr) {
                        foreach ($gr->forums as $f) {
                            if (!empty($stats[$f->forum_id])) {
                                $s = $stats[$f->forum_id];
                                $f->topic_id = $s->topic_id;
                                $f->topic_name = sp_filter_title_display($s->topic_name);
                                $f->topic_slug = $s->topic_slug;
                                $f->post_id = $s->post_id;
                                $f->post_permalink = sp_build_url($f->forum_slug, $s->topic_slug, 0, $s->post_id, $s->post_index);
                                $f->post_date = $s->post_date;
                                $f->post_status = $s->post_status;
                                $f->post_index = $s->post_index;
                                # see if we can display the tooltip
                                if (sp_can_view($f->forum_id, 'post-content', $spThisUser->ID, $s->user_id, $s->topic_id, $s->post_id)) {
                                    $f->post_tip = $s->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($s->post_content, $s->post_status);
                                } else {
                                    $f->post_tip = '';
                                }
                                $f->user_id = $s->user_id;
                                $f->display_name = sp_filter_name_display($s->display_name);
                                $f->guest_name = sp_filter_name_display($s->guest_name);
                            }
                            # do we need to record a possible subforum substitute topic?
                            $fsub = $f->forum_id_sub;
                            if ($fsub != 0 && !empty($stats[$fsub])) {
                                $s = $stats[$fsub];
                                $f->topic_id_sub = $s->topic_id;
                                $f->topic_name_sub = sp_filter_title_display($s->topic_name);
                                $f->topic_slug_sub = $s->topic_slug;
                                $f->post_id_sub = $s->post_id;
                                $f->post_permalink_sub = sp_build_url($f->subforums[$fsub]->forum_slug, $s->topic_slug, 0, $s->post_id, $s->post_index);
                                $f->post_date_sub = $s->post_date;
                                $f->post_status_sub = $s->post_status;
                                $f->post_index_sub = $s->post_index;
                                # see if we can display the tooltip
                                if (sp_can_view($fsub, 'post-content', $spThisUser->ID, $s->user_id, $s->topic_id, $s->post_id)) {
                                    $f->post_tip_sub = $s->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($s->post_content, $s->post_status);
                                } else {
                                    $f->post_tip_sub = '';
                                }
                                $f->user_id_sub = $s->user_id;
                                $f->display_name_sub = sp_filter_name_display($s->display_name);
                                $f->guest_name_sub = sp_filter_name_display($s->guest_name);
                            }
                            $f = apply_filters('sph_groupview_stats_records', $f, $s);
                        }
                    }
                    unset($stats);
                }
            }
        }
        # Do we need to re-order IDs based on passed in IDs
        if ($groupids && $idOrder) {
            $n = array();
            foreach ($groupids as $gid) {
                if (array_key_exists($gid, $g)) {
                    $n[$gid] = $g[$gid];
                }
            }
            $g = $n;
            unset($n);
        }
        return $g;
    }
    function sp_topicview_query($topicid = 0, $cPage = 1, $forumid = 0)
    {
        global $spGlobals, $spThisUser, $spVars;
        # do we have a valid topic id
        if ($topicid == 0) {
            $this->topicViewStatus = 'no data';
            return;
        } else {
            $WHERE = SFTOPICS . '.topic_id=' . $topicid;
        }
        # default to no access
        $this->topicViewStatus = 'no access';
        # some setup vars
        $startlimit = 0;
        $lastpage = 0;
        # how many posts per page?
        $ppaged = $spGlobals['display']['posts']['perpage'];
        if (!$ppaged) {
            $ppaged = 10;
        }
        # setup where we are in the post list (paging)
        if ($cPage != 1) {
            $startlimit = ($cPage - 1) * $ppaged;
        }
        $LIMIT = $startlimit . ', ' . $ppaged;
        # Set up order by
        $setSort = false;
        $reverse = false;
        $setSort = $spGlobals['display']['posts']['sortdesc'];
        if (isset($spGlobals['sort_order']['topic'])) {
            $reverse = array_search($topicid, (array) $spGlobals['sort_order']['topic']) !== false ? true : false;
        }
        if (isset($spThisUser->postDESC) && $spThisUser->postDESC) {
            $reverse = !$reverse;
        }
        if ($setSort xor $reverse) {
            $ORDER = 'post_pinned DESC, ' . SFPOSTS . ".post_id DESC";
        } else {
            $ORDER = 'post_pinned DESC, ' . SFPOSTS . ".post_id ASC";
        }
        # add newpost/sfwaiting support for admins
        $waitCheck = ', NULL AS new_post';
        if ($spThisUser->admin || $spThisUser->moderator) {
            $waitCheck = ', ' . SFWAITING . '.post_count AS new_post';
        }
        # Discover if this topic is in users new post list
        $maybeNewPost = false;
        if ($spThisUser->member && sp_is_in_users_newposts($topicid)) {
            $maybeNewPost = true;
        }
        # retrieve topic and post records
        $spdb = new spdbComplex();
        $spdb->table = SFTOPICS;
        $spdb->found_rows = true;
        $spdb->fields = 'group_id, ' . SFTOPICS . '.topic_id, ' . SFTOPICS . '.forum_id, topic_name, topic_slug, topic_status, topic_pinned, topic_icon, topic_opened, ' . SFTOPICS . '.post_count, forum_name, forum_slug, forum_status,
							  forum_disabled, forum_rss_private, ' . SFPOSTS . '.post_id, ' . spdb_zone_datetime('post_date') . ', ' . SFPOSTS . '.user_id, ' . SFTOPICS . '.user_id AS topic_starter,
							  guest_name, guest_email, post_status, post_pinned, post_index, post_edit, poster_ip, source, post_content' . $waitCheck;
        $spdb->join = array(SFPOSTS . ' ON ' . SFTOPICS . '.topic_id=' . SFPOSTS . '.topic_id', SFFORUMS . ' ON ' . SFTOPICS . '.forum_id=' . SFFORUMS . '.forum_id');
        if ($spThisUser->admin || $spThisUser->moderator) {
            $spdb->left_join = array(SFWAITING . ' ON ' . SFPOSTS . '.post_id=' . SFWAITING . '.post_id');
        }
        $spdb->where = $WHERE;
        $spdb->orderby = $ORDER;
        $spdb->limits = $LIMIT;
        $spdb = apply_filters('sph_topicview_query', $spdb, $this);
        if (!empty($spThisUser->inspect['q_spTopicView'])) {
            $spdb->inspect = 'spTopicView';
            $spdb->show = true;
        }
        $records = $spdb->select();
        $t = array();
        if ($records) {
            $tidx = $topicid;
            $pidx = 0;
            $r = current($records);
            if (sp_get_auth('view_forum', $r->forum_id)) {
                $this->topicViewStatus = 'data';
                # construct the parent topic object
                $t[$tidx] = new stdClass();
                $t[$tidx]->topic_id = $r->topic_id;
                $t[$tidx]->forum_id = $r->forum_id;
                $t[$tidx]->group_id = $r->group_id;
                $t[$tidx]->forum_name = sp_filter_title_display($r->forum_name);
                $t[$tidx]->topic_name = sp_filter_title_display($r->topic_name);
                $t[$tidx]->topic_slug = $r->topic_slug;
                $t[$tidx]->topic_opened = $r->topic_opened;
                $t[$tidx]->forum_status = $r->forum_status;
                $t[$tidx]->topic_pinned = $r->topic_pinned;
                $t[$tidx]->forum_disabled = $r->forum_disabled;
                $t[$tidx]->forum_slug = $r->forum_slug;
                $t[$tidx]->forum_rss_private = $r->forum_rss_private;
                $t[$tidx]->topic_permalink = sp_build_url($r->forum_slug, $r->topic_slug, 1, 0);
                $t[$tidx]->topic_status = $r->topic_status;
                $t[$tidx]->topic_icon = sanitize_file_name($r->topic_icon);
                $t[$tidx]->rss = '';
                $t[$tidx]->editmode = 0;
                $t[$tidx]->tools_flag = 1;
                $t[$tidx]->display_page = $this->topicPage;
                $t[$tidx]->posts_per_page = $ppaged;
                $t[$tidx]->unread = 0;
                # user calc_rows and nor post_count as - for example - some posts may be hiodden by choice.
                $t[$tidx]->post_count = spdb_select('var', 'SELECT FOUND_ROWS()');
                # Can the user create new topics or should we lock the forum?
                $t[$tidx]->start_topics = sp_get_auth('start_topics', $r->forum_id);
                $t[$tidx]->reply_topics = sp_get_auth('reply_topics', $r->forum_id);
                $t[$tidx]->reply_own_topics = sp_get_auth('reply_own_topics', $r->forum_id);
                # grab topic start info
                $t[$tidx]->topic_starter = $r->topic_starter;
                $totalPages = $r->post_count / $ppaged;
                if (!is_int($totalPages)) {
                    $totalPages = intval($totalPages) + 1;
                }
                $t[$tidx]->total_pages = $totalPages;
                if ($setSort xor $reverse) {
                    if ($cPage == 1) {
                        $lastpage = true;
                    }
                } else {
                    if ($cPage == $totalPages) {
                        $lastpage = true;
                    }
                }
                $t[$tidx]->last_page = $lastpage;
                $t[$tidx] = apply_filters('sph_topicview_topic_record', $t[$tidx], $r);
                reset($records);
                unset($r);
                # now loop through the post records
                $newPostFlag = false;
                $firstPostPage = 1;
                $pinned = 0;
                # define post id and post user id arrays for plugins to use in combined filter
                $p = array();
                $u = array();
                foreach ($records as $r) {
                    $pidx = $r->post_id;
                    $p[] = $pidx;
                    # prepare for user object
                    $cUser = $spThisUser->ID == $r->user_id;
                    $cSmall = !$cUser;
                    $t[$tidx]->posts[$pidx] = new stdClass();
                    $t[$tidx]->posts[$pidx]->post_id = $r->post_id;
                    $t[$tidx]->posts[$pidx]->post_date = $r->post_date;
                    $t[$tidx]->posts[$pidx]->user_id = $r->user_id;
                    $t[$tidx]->posts[$pidx]->guest_name = sp_filter_name_display($r->guest_name);
                    $t[$tidx]->posts[$pidx]->guest_email = sp_filter_email_display($r->guest_email);
                    $t[$tidx]->posts[$pidx]->post_status = $r->post_status;
                    $t[$tidx]->posts[$pidx]->post_pinned = $r->post_pinned;
                    $t[$tidx]->posts[$pidx]->post_index = $r->post_index;
                    $t[$tidx]->posts[$pidx]->poster_ip = $r->poster_ip;
                    $t[$tidx]->posts[$pidx]->source = $r->source;
                    $t[$tidx]->posts[$pidx]->post_permalink = sp_build_url($r->forum_slug, $r->topic_slug, $cPage, $r->post_id);
                    $t[$tidx]->posts[$pidx]->edits = '';
                    $t[$tidx]->posts[$pidx]->last_post = 0;
                    $t[$tidx]->posts[$pidx]->last_post_on_page = 0;
                    $t[$tidx]->posts[$pidx]->first_post_on_page = $firstPostPage;
                    $t[$tidx]->posts[$pidx]->editmode = 0;
                    $t[$tidx]->posts[$pidx]->post_content = sp_filter_content_display($r->post_content);
                    $t[$tidx]->posts[$pidx]->first_pinned = 0;
                    $t[$tidx]->posts[$pidx]->last_pinned = 0;
                    $t[$tidx]->posts[$pidx]->postUser = new stdClass();
                    $t[$tidx]->posts[$pidx]->postUser = clone sp_get_user($r->user_id, $cUser, $cSmall);
                    # populate the user guest name and email in case the poster is a guest
                    if ($r->user_id == 0) {
                        $t[$tidx]->posts[$pidx]->postUser->guest_name = $t[$tidx]->posts[$pidx]->guest_name;
                        $t[$tidx]->posts[$pidx]->postUser->guest_email = $t[$tidx]->posts[$pidx]->guest_email;
                        $t[$tidx]->posts[$pidx]->postUser->display_name = $t[$tidx]->posts[$pidx]->guest_name;
                        $t[$tidx]->posts[$pidx]->postUser->ip = $t[$tidx]->posts[$pidx]->poster_ip;
                    }
                    # pinned status
                    if ($firstPostPage == 1 && $r->post_pinned) {
                        $t[$tidx]->posts[$pidx]->first_pinned = true;
                        $pinned = $pidx;
                    }
                    if ($firstPostPage == 0 && $pinned > 0 && $r->post_pinned == false) {
                        $t[$tidx]->posts[$pinned]->last_pinned = true;
                    } elseif ($r->post_pinned) {
                        $pinned = $pidx;
                    }
                    $firstPostPage = 0;
                    # Is this a new post for the current user?
                    if ($spThisUser->guest) {
                        $newPostFlag = false;
                    } else {
                        if ($maybeNewPost && strtotime($r->post_date) > strtotime($spThisUser->lastvisit)) {
                            $newPostFlag = true;
                        }
                        if (isset($r->new_post)) {
                            $newPostFlag = true;
                        }
                    }
                    $t[$tidx]->posts[$pidx]->new_post = $newPostFlag;
                    # do we need to hide an admin post?
                    if (!sp_get_auth('view_admin_posts', $r->forum_id) && sp_is_forum_admin($r->user_id)) {
                        $adminview = sp_get_sfmeta('adminview', 'message');
                        if ($adminview) {
                            $t[$tidx]->posts[$pidx]->post_content = '<div class="spMessage">';
                            $t[$tidx]->posts[$pidx]->post_content .= sp_filter_text_display($adminview[0]['meta_value']);
                            $t[$tidx]->posts[$pidx]->post_content .= '</div>';
                        } else {
                            $t[$tidx]->posts[$pidx]->post_content = '';
                        }
                    }
                    # do we need to hide an others posts?
                    if (sp_get_auth('view_own_admin_posts', $r->forum_id) && !sp_is_forum_admin($r->user_id) && !sp_is_forum_mod($r->user_id) && $spThisUser->ID != $r->user_id) {
                        $userview = sp_get_sfmeta('userview', 'message');
                        if ($userview) {
                            $t[$tidx]->posts[$pidx]->post_content = '<div class="spMessage">';
                            $t[$tidx]->posts[$pidx]->post_content .= sp_filter_text_display($userview[0]['meta_value']);
                            $t[$tidx]->posts[$pidx]->post_content .= '</div>';
                        } else {
                            $t[$tidx]->posts[$pidx]->post_content = '';
                        }
                    }
                    # Is this post to be edited?
                    if ($spVars['displaymode'] == 'edit' && $spVars['postedit'] == $r->post_id) {
                        $t[$tidx]->editmode = 1;
                        $t[$tidx]->editpost_id = $r->post_id;
                        $t[$tidx]->editpost_content = sp_filter_content_edit($r->post_content);
                        $t[$tidx]->posts[$pidx]->editmode = 1;
                    }
                    # Add edit history
                    if (!empty($r->post_edit) && is_serialized($r->post_edit)) {
                        $edits = unserialize($r->post_edit);
                        $eidx = 0;
                        foreach ($edits as $e) {
                            $t[$tidx]->posts[$pidx]->edits[$eidx] = new stdClass();
                            $t[$tidx]->posts[$pidx]->edits[$eidx]->by = $e['by'];
                            $t[$tidx]->posts[$pidx]->edits[$eidx]->at = $e['at'];
                            $eidx++;
                        }
                    }
                    if (!in_array($r->user_id, $u)) {
                        $u[] = $r->user_id;
                    }
                    $t[$tidx]->posts[$pidx] = apply_filters('sph_topicview_post_records', $t[$tidx]->posts[$pidx], $r);
                }
                # index of post IDs with position in listing
                $t[$tidx]->post_keys = $p;
                $t[$tidx]->posts[$pidx]->last_post = $lastpage;
                $t[$tidx]->posts[$pidx]->last_post_on_page = 1;
                # save last post on page id
                $t[$tidx]->last_post_id = $r->post_id;
                # allow plugins to add more data to combined topic/post data structure
                $t[$tidx] = apply_filters('sph_topicview_combined_data', $t[$tidx], $p, $u);
                unset($records);
            } else {
                # check for view forum lists but not topic lists
                if (sp_can_view($r->forum_id, 'forum-title')) {
                    $this->topicViewStatus = 'sneak peek';
                }
            }
        }
        return $t;
    }
Esempio n. 7
0
 function sp_forumview_build_subforums($forumid, $f, $fidx, $subs)
 {
     global $spThisUser;
     ksort($subs);
     foreach ($subs as $sub) {
         if (sp_can_view($sub->forum_id, 'topic-title')) {
             $f[$fidx]->subforums[$sub->forum_id] = new stdClass();
             $f[$fidx]->subforums[$sub->forum_id]->top_parent = $fidx;
             $f[$fidx]->subforums[$sub->forum_id]->top_sub_parent = $sub->topSubParent;
             $f[$fidx]->subforums[$sub->forum_id]->forum_id = $sub->forum_id;
             $f[$fidx]->subforums[$sub->forum_id]->forum_id_sub = 0;
             $f[$fidx]->subforums[$sub->forum_id]->forum_name = sp_filter_title_display($sub->forum_name);
             $f[$fidx]->subforums[$sub->forum_id]->forum_permalink = sp_build_url($sub->forum_slug, '', 1, 0);
             $f[$fidx]->subforums[$sub->forum_id]->forum_slug = $sub->forum_slug;
             $f[$fidx]->subforums[$sub->forum_id]->forum_desc = sp_filter_title_display($sub->forum_desc);
             $f[$fidx]->subforums[$sub->forum_id]->forum_status = $sub->forum_status;
             $f[$fidx]->subforums[$sub->forum_id]->forum_disabled = $sub->forum_disabled;
             $f[$fidx]->subforums[$sub->forum_id]->forum_icon = sanitize_file_name($sub->forum_icon);
             $f[$fidx]->subforums[$sub->forum_id]->forum_icon_new = sanitize_file_name($sub->forum_icon_new);
             $f[$fidx]->subforums[$sub->forum_id]->topic_icon = sanitize_file_name($sub->topic_icon);
             $f[$fidx]->subforums[$sub->forum_id]->topic_icon_new = sanitize_file_name($sub->topic_icon_new);
             $f[$fidx]->subforums[$sub->forum_id]->topic_icon_locked = sanitize_file_name($sub->topic_icon_locked);
             $f[$fidx]->subforums[$sub->forum_id]->topic_icon_pinned = sanitize_file_name($sub->topic_icon_pinned);
             $f[$fidx]->subforums[$sub->forum_id]->forum_rss_private = $sub->forum_rss_private;
             $f[$fidx]->subforums[$sub->forum_id]->post_id = $sub->post_id;
             $f[$fidx]->subforums[$sub->forum_id]->post_id_held = $sub->post_id_held;
             $f[$fidx]->subforums[$sub->forum_id]->topic_count = $sub->topic_count;
             $f[$fidx]->subforums[$sub->forum_id]->topic_count_sub = $sub->topic_count;
             $f[$fidx]->subforums[$sub->forum_id]->post_count = $sub->post_count;
             $f[$fidx]->subforums[$sub->forum_id]->post_count_sub = $sub->post_count;
             $f[$fidx]->subforums[$sub->forum_id]->post_count_held = $sub->post_count_held;
             $f[$fidx]->subforums[$sub->forum_id]->parent = $sub->parent;
             $f[$fidx]->subforums[$sub->forum_id]->children = $sub->children;
             $f[$fidx]->subforums[$sub->forum_id]->unread = 0;
             # Can the user create new topics or should we lock the forum?
             $f[$fidx]->subforums[$sub->forum_id]->start_topics = sp_get_auth('start_topics', $sub->forum_id);
             # See if any forums are in the current users newpost list
             if ($spThisUser->member) {
                 $c = 0;
                 if ($spThisUser->newposts && $spThisUser->newposts['forums']) {
                     foreach ($spThisUser->newposts['forums'] as $fnp) {
                         if ($fnp == $sub->forum_id) {
                             $c++;
                         }
                     }
                 }
                 $f[$fidx]->subforums[$sub->forum_id]->unread = $c;
             }
             # check if we can look at posts in moderation - if not swap for 'held' values
             if (!sp_get_auth('moderate_posts', $sub->forum_id)) {
                 $f[$fidx]->subforums[$sub->forum_id]->post_id = $sub->post_id_held;
                 $f[$fidx]->subforums[$sub->forum_id]->post_count = $sub->post_count_held;
                 $f[$fidx]->subforums[$sub->forum_id]->post_count_sub = $sub->post_count_held;
                 $thisPostid = $sub->post_id_held;
             } else {
                 $thisPostid = $sub->post_id;
             }
             # Build post id array for collecting stats at the end
             if (!empty($thisPostid)) {
                 $p[$sub->forum_id] = $thisPostid;
             }
             # if this subforum has a parent that is differemt to the main forum being dislayed in the view
             # then it has to be a nested subforum so do we need to merge the numbers?
             if ($sub->parent != $forumid) {
                 $f[$fidx]->subforums[$sub->parent]->topic_count_sub += $f[$fidx]->subforums[$sub->forum_id]->topic_count;
                 $f[$fidx]->subforums[$sub->parent]->post_count_sub += $f[$fidx]->subforums[$sub->forum_id]->post_count;
                 # and what about the most recent post? Is this in a nested subforum?
                 if ($f[$fidx]->subforums[$sub->forum_id]->post_id > $f[$fidx]->subforums[$sub->parent]->post_id) {
                     # store the alternative forum id in case we need to display the topic data for this one if inc. subs
                     $f[$fidx]->subforums[$sub->parent]->forum_id_sub = $sub->forum_id;
                 }
             }
         }
     }
     # Go grab the sub forum stats and data
     if (!empty($p)) {
         $stats = $this->sp_subforumview_stats_query($p);
         if ($stats) {
             $s = '';
             foreach ($subs as $sub) {
                 if (!empty($stats[$sub->forum_id])) {
                     $s = $stats[$sub->forum_id];
                     $f[$fidx]->subforums[$sub->forum_id]->topic_id = $s->topic_id;
                     $f[$fidx]->subforums[$sub->forum_id]->topic_name = sp_filter_title_display($s->topic_name);
                     $f[$fidx]->subforums[$sub->forum_id]->topic_slug = $s->topic_slug;
                     $f[$fidx]->subforums[$sub->forum_id]->post_id = $s->post_id;
                     $f[$fidx]->subforums[$sub->forum_id]->post_permalink = sp_build_url($f[$fidx]->subforums[$sub->forum_id]->forum_slug, $s->topic_slug, 0, $s->post_id, $s->post_index);
                     $f[$fidx]->subforums[$sub->forum_id]->post_date = $s->post_date;
                     $f[$fidx]->subforums[$sub->forum_id]->post_status = $s->post_status;
                     $f[$fidx]->subforums[$sub->forum_id]->post_index = $s->post_index;
                     # see if we can display the tooltip
                     if (sp_can_view($sub->forum_id, 'post-content', $spThisUser->ID, $s->user_id, $s->topic_id, $s->post_id)) {
                         $f[$fidx]->subforums[$sub->forum_id]->post_tip = $s->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($s->post_content, $s->post_status);
                     } else {
                         $f[$fidx]->subforums[$sub->forum_id]->post_tip = '';
                     }
                     $f[$fidx]->subforums[$sub->forum_id]->user_id = $s->user_id;
                     $f[$fidx]->subforums[$sub->forum_id]->display_name = sp_filter_name_display($s->display_name);
                     $f[$fidx]->subforums[$sub->forum_id]->guest_name = sp_filter_name_display($s->guest_name);
                 }
                 # do we need to record a possible subforum substitute topic?
                 $fsub = isset($f[$fidx]->subforums[$sub->forum_id]->forum_id_sub) ? $f[$fidx]->subforums[$sub->forum_id]->forum_id_sub : 0;
                 if ($fsub != 0 && !empty($stats[$fsub])) {
                     $s = $stats[$fsub];
                     $f[$fidx]->subforums[$sub->forum_id]->topic_id_sub = $s->topic_id;
                     $f[$fidx]->subforums[$sub->forum_id]->topic_name_sub = sp_filter_title_display($s->topic_name);
                     $f[$fidx]->subforums[$sub->forum_id]->topic_slug_sub = $s->topic_slug;
                     $f[$fidx]->subforums[$sub->forum_id]->post_id_sub = $s->post_id;
                     $f[$fidx]->subforums[$sub->forum_id]->post_permalink_sub = sp_build_url($f[$fidx]->subforums[$fsub]->forum_slug, $s->topic_slug, 0, $s->post_id, $s->post_index);
                     $f[$fidx]->subforums[$sub->forum_id]->post_date_sub = $s->post_date;
                     $f[$fidx]->subforums[$sub->forum_id]->post_status_sub = $s->post_status;
                     $f[$fidx]->subforums[$sub->forum_id]->post_index_sub = $s->post_index;
                     # see if we can display the tooltip
                     if (sp_can_view($fsub, 'post-content', $spThisUser->ID, $s->user_id, $s->topic_id, $s->post_id)) {
                         $f[$fidx]->subforums[$sub->forum_id]->post_tip_sub = $s->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($s->post_content, $s->post_status);
                     } else {
                         $f[$fidx]->subforums[$sub->forum_id]->post_tip_sub = '';
                     }
                     $f[$fidx]->subforums[$sub->forum_id]->user_id_sub = $s->user_id;
                     $f[$fidx]->subforums[$sub->forum_id]->display_name_sub = sp_filter_name_display($s->display_name);
                     $f[$fidx]->subforums[$sub->forum_id]->guest_name_sub = sp_filter_name_display($s->guest_name);
                 }
                 # allow plugins to add more data to combined subforum/post data structure
                 $f[$fidx]->subforums[$sub->forum_id] = apply_filters('sph_forumview_subforum_records', $f[$fidx]->subforums[$sub->forum_id], $s);
             }
         }
         unset($subs);
         unset($stats);
     }
     return $f;
 }
    function sp_listview_query($topicIds, $count, $group, $forumIds, $firstPost, $popup)
    {
        global $spThisUser, $spGlobals;
        # If no topic ids and no count then nothjing to do - return empty
        if (empty($topicIds) && $count == 0) {
            return;
        }
        # set popup flag for new posts
        $this->popup = $popup;
        # Do we have enough topic ids to satisfy count?
        if (empty($topicIds) || $count != 0 && count($topicIds) < $count) {
            $topicIds = $this->sp_listview_populate_topicids($topicIds, $forumIds, $count);
        }
        # Do we havwe too many topic ids?
        if ($topicIds && ($count != 0 && count($topicIds) > $count)) {
            $topicIds = array_slice($topicIds, 0, $count, true);
        }
        if (empty($topicIds)) {
            return;
        }
        # Construct the main WHERE clause and then main query
        $where = SFTOPICS . '.topic_id IN (' . implode(',', $topicIds) . ')';
        if ($group) {
            $orderby = 'group_seq, forum_seq, ' . SFTOPICS . '.post_id DESC';
        } else {
            $orderby = SFTOPICS . '.post_id DESC';
        }
        $spdb = new spdbComplex();
        $spdb->table = SFTOPICS;
        $spdb->fields = SFTOPICS . '.forum_id, forum_name, forum_slug, forum_disabled, ' . SFTOPICS . '.topic_id, topic_name, topic_slug, topic_icon, topic_icon_new, ' . SFTOPICS . '.post_count,
								' . SFTOPICS . '.post_id, post_status, post_index, ' . spdb_zone_datetime('post_date') . ',
								guest_name, ' . SFPOSTS . '.user_id, post_content, display_name';
        $spdb->join = array(SFFORUMS . ' ON ' . SFFORUMS . '.forum_id = ' . SFTOPICS . '.forum_id', SFGROUPS . ' ON ' . SFGROUPS . '.group_id = ' . SFFORUMS . '.group_id', SFPOSTS . ' ON ' . SFPOSTS . '.post_id = ' . SFTOPICS . '.post_id');
        $spdb->left_join = array(SFMEMBERS . ' ON ' . SFMEMBERS . '.user_id = ' . SFPOSTS . '.user_id');
        $spdb->where = $where;
        $spdb->orderby = $orderby;
        $spdb = apply_filters('sph_topic_list_query', $spdb, $this);
        $records = $spdb->select();
        # add filters where required plus extra data
        # And the new array
        $list = array();
        if ($records) {
            # check if all forum ids are the same
            $x = current($records);
            $f = $x->forum_id;
            $single = 1;
            foreach ($records as $r) {
                if ($r->forum_id != $f) {
                    $single = 0;
                }
            }
            reset($records);
            $new = '';
            $first = '';
            # Now we can grab the supplementary post records where there may be new posts...
            if ($spThisUser->member) {
                $new = $this->sp_listview_populate_newposts($topicIds);
            }
            # go and grab the first post info if desired
            if ($firstPost) {
                $first = $this->sp_listview_populate_firstposts($topicIds);
            }
            # Some values we need
            # How many topics to a page?
            $ppaged = $spGlobals['display']['posts']['perpage'];
            if (empty($ppaged) || $ppaged == 0) {
                $ppaged = 20;
            }
            # establish topic sort order
            $order = 'ASC';
            # default
            if ($spGlobals['display']['posts']['sortdesc']) {
                $order = 'DESC';
            }
            # global override
            $listPos = 1;
            foreach ($records as $r) {
                $show = true;
                # can the user see this forum?
                if (!sp_can_view($r->forum_id, 'topic-title')) {
                    $show = false;
                }
                # if in moderattion can this user approve posts?
                if ($r->post_status != 0 && !sp_get_auth('moderate_posts', $r->forum_id)) {
                    $show = false;
                }
                if ($show) {
                    $t = $r->topic_id;
                    $list[$t] = new stdClass();
                    $list[$t]->forum_id = $r->forum_id;
                    $list[$t]->forum_name = sp_filter_title_display($r->forum_name);
                    $list[$t]->forum_disabled = $r->forum_disabled;
                    $list[$t]->forum_permalink = sp_build_url($r->forum_slug, '', 1, 0);
                    $list[$t]->topic_id = $r->topic_id;
                    $list[$t]->topic_name = sp_filter_title_display($r->topic_name);
                    $list[$t]->topic_permalink = sp_build_url($r->forum_slug, $r->topic_slug, 1, 0);
                    $list[$t]->topic_icon = sanitize_file_name($r->topic_icon);
                    $list[$t]->topic_icon_new = sanitize_file_name($r->topic_icon_new);
                    $list[$t]->post_count = $r->post_count;
                    $list[$t]->post_id = $r->post_id;
                    $list[$t]->post_status = $r->post_status;
                    $list[$t]->post_date = $r->post_date;
                    $list[$t]->user_id = $r->user_id;
                    $list[$t]->guest_name = sp_filter_name_display($r->guest_name);
                    $list[$t]->display_name = sp_filter_name_display($r->display_name);
                    if (sp_can_view($r->forum_id, 'post-content', $spThisUser->ID, $r->user_id)) {
                        $list[$t]->post_tip = $r->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($r->post_content, $r->post_status);
                    } else {
                        $list[$t]->post_tip = '';
                    }
                    $list[$t]->list_position = $listPos;
                    if (empty($r->display_name)) {
                        $list[$t]->display_name = $list[$t]->guest_name;
                    }
                    # Lastly determine the page for the post permalink
                    if ($order == 'ASC') {
                        $page = $r->post_index / $ppaged;
                        if (!is_int($page)) {
                            $page = intval($page + 1);
                        }
                    } else {
                        $page = $r->post_count - $r->post_index;
                        $page = $page / $ppaged;
                        $page = intval($page + 1);
                    }
                    $r->page = $page;
                    $list[$t]->post_permalink = sp_build_url($r->forum_slug, $r->topic_slug, $r->page, $r->post_id, $r->post_index);
                    $list[$t]->single_forum = $single;
                    # add in any new post details if they exist
                    if (!empty($new) && array_key_exists($t, $new)) {
                        $list[$t]->new_post_count = $new[$t]->new_post_count;
                        $list[$t]->new_post_post_id = $new[$t]->new_post_post_id;
                        $list[$t]->new_post_post_index = $new[$t]->new_post_post_index;
                        $list[$t]->new_post_post_date = $new[$t]->new_post_post_date;
                        $list[$t]->new_post_user_id = $new[$t]->new_post_user_id;
                        $list[$t]->new_post_display_name = $new[$t]->new_post_display_name;
                        $list[$t]->new_post_guest_name = $new[$t]->new_post_guest_name;
                        $list[$t]->new_post_permalink = sp_build_url($r->forum_slug, $r->topic_slug, 0, $new[$t]->new_post_post_id, $new[$t]->new_post_post_index);
                        if (empty($new[$t]->new_post_display_name)) {
                            $list[$t]->new_post_display_name = $new[$t]->new_post_guest_name;
                        }
                    }
                    # add the first post info if desired
                    if ($firstPost) {
                        $list[$t]->first_post_permalink = sp_build_url($r->forum_slug, $r->topic_slug, 0, $first[$t]->post_id, 1);
                        $list[$t]->first_post_date = $first[$t]->post_date;
                        $list[$t]->first_user_id = $first[$t]->user_id;
                        $list[$t]->first_guest_name = sp_filter_name_display($first[$t]->guest_name);
                        $list[$t]->first_display_name = sp_filter_name_display($first[$t]->display_name);
                        if (sp_can_view($r->forum_id, 'post-content', $spThisUser->ID, $first[$t]->user_id)) {
                            $list[$t]->first_post_tip = $first[$t]->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($first[$t]->post_content, $first[$t]->post_status);
                        } else {
                            $list[$t]->first_post_tip = '';
                        }
                        if (empty($list[$t]->first_display_name)) {
                            $list[$t]->first_display_name = $list[$t]->first_guest_name;
                        }
                    }
                    $list[$t] = apply_filters('sph_topic_list_record', $list[$t], $r);
                    $listPos++;
                }
            }
            unset($records);
            unset($new);
            unset($first);
        }
        return $list;
    }
    function sp_postlistview_query($where, $order, $count, $view, $type)
    {
        global $spGlobals, $spThisUser;
        # If no WHERE clause then return empty
        if (empty($where)) {
            return;
        }
        # build list of forums user can view
        $fids = sp_user_visible_forums($view);
        if (!empty($fids)) {
            $fids = implode(',', $fids);
            $where .= ' AND ' . SFPOSTS . ".forum_id IN ({$fids})";
        }
        # Check order
        if (empty($order)) {
            $order = SFPOSTS . '.post_id DESC';
        }
        $spdb = new spdbComplex();
        $spdb->table = SFPOSTS;
        $spdb->fields = SFPOSTS . '.post_id, post_content, ' . spdb_zone_datetime('post_date') . ', ' . SFPOSTS . '.topic_id, ' . SFPOSTS . '.forum_id,
								  ' . SFPOSTS . '.user_id, guest_name, post_status, post_index, forum_name, forum_slug, forum_disabled, ' . SFFORUMS . '.group_id, group_name,
								  topic_name, topic_slug, ' . SFTOPICS . '.post_count, topic_opened, display_name';
        $spdb->join = array(SFFORUMS . ' ON ' . SFFORUMS . '.forum_id = ' . SFPOSTS . '.forum_id', SFGROUPS . ' ON ' . SFGROUPS . '.group_id = ' . SFFORUMS . '.group_id', SFTOPICS . ' ON ' . SFTOPICS . '.topic_id = ' . SFPOSTS . '.topic_id');
        $spdb->left_join = array(SFMEMBERS . ' ON ' . SFMEMBERS . '.user_id = ' . SFPOSTS . '.user_id');
        $spdb->where = $where;
        $spdb->orderby = $order;
        if ($count) {
            $spdb->limits = $count;
        }
        $spdb = apply_filters('sph_post_list_query', $spdb, $this, $type);
        if (!empty($spThisUser->inspect['q_spPostListView'])) {
            $spdb->inspect = 'spPostListView';
            $spdb->show = true;
        }
        $records = $spdb->select();
        # Now check authorisations and clean up the object
        $list = array();
        # Some values we need
        # How many topics to a page?
        $ppaged = $spGlobals['display']['posts']['perpage'];
        if (empty($ppaged) || $ppaged == 0) {
            $ppaged = 20;
        }
        # establish topic sort order
        $porder = 'ASC';
        # default
        if ($spGlobals['display']['posts']['sortdesc']) {
            $porder = 'DESC';
        }
        # global override
        if ($records) {
            $listPos = 1;
            foreach ($records as $r) {
                if (sp_can_view($r->forum_id, 'forum-title')) {
                    if ($r->post_status == 0 || sp_get_auth('moderate_posts', $r->forum_id)) {
                        $p = $r->post_id;
                        $list[$p] = $r;
                        # Now apply any necessary filters and data changes
                        $list[$p]->post_content = sp_filter_content_display($r->post_content);
                        $list[$p]->post_content_raw = $r->post_content;
                        $list[$p]->forum_name = sp_filter_title_display($r->forum_name);
                        $list[$p]->forum_disabled = $r->forum_disabled;
                        $list[$p]->forum_permalink = sp_build_url($r->forum_slug, '', 1, 0);
                        $list[$p]->topic_permalink = sp_build_url($r->forum_slug, $r->topic_slug, 1, 0);
                        $list[$p]->topic_name = sp_filter_title_display($r->topic_name);
                        $list[$p]->topic_opened = $r->topic_opened;
                        $list[$p]->group_name = sp_filter_title_display($r->group_name);
                        if (sp_can_view($r->forum_id, 'post-content', $spThisUser->ID, $r->user_id, $r->topic_id, $r->post_id)) {
                            $list[$p]->post_tip = $r->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($r->post_content, $r->post_status);
                        } else {
                            $list[$p]->post_tip = '';
                        }
                        # Ensure display name is populated
                        if (empty($r->display_name)) {
                            $list[$p]->display_name = $list[$p]->guest_name;
                        }
                        $list[$p]->display_name = sp_filter_name_display($list[$p]->display_name);
                        # determine the page for the post permalink
                        if ($porder == 'ASC') {
                            $page = $r->post_index / $ppaged;
                            if (!is_int($page)) {
                                $page = intval($page + 1);
                            }
                        } else {
                            $page = $r->post_count - $r->post_index;
                            $page = $page / $ppaged;
                            $page = intval($page + 1);
                        }
                        $list[$p]->post_permalink = sp_build_url($r->forum_slug, $r->topic_slug, $page, $r->post_id, $r->post_index);
                        $list[$p]->list_position = $listPos;
                        $list[$p] = apply_filters('sph_post_list_record', $list[$p], $r, $type);
                    }
                }
                $listPos++;
            }
        }
        return $list;
    }
function sp_track_online()
{
    global $spThisUser, $spVars, $spDevice;
    # dont track feed views
    if ($spVars['pageview'] == 'feed') {
        return;
    }
    # Update tracking
    if ($spThisUser->member) {
        # it's a member
        $trackUserId = $spThisUser->ID;
        $trackName = $spThisUser->user_login;
    } else {
        # Unknown guest
        $trackUserId = 0;
        $trackName = $spThisUser->ip;
    }
    $track = spdb_table(SFTRACK, "trackname='{$trackName}'", 'row');
    $now = current_time('mysql');
    $forumId = isset($spVars['forumid']) ? $spVars['forumid'] : 0;
    $topicId = isset($spVars['topicid']) ? $spVars['topicid'] : 0;
    $pageview = $spVars['pageview'];
    # handle sneak peek
    if (!empty($topicId)) {
        if (!sp_get_auth('view_forum', $forumId)) {
            return;
        }
    } else {
        if (!empty($forumId)) {
            if (!sp_can_view($forumId, 'topic-title')) {
                return;
            }
        }
    }
    # update or start tracking
    if ($track) {
        # they are still here
        spdb_query("UPDATE " . SFTRACK . "\n\t\t\t\t   SET trackdate='" . $now . "', forum_id=" . $forumId . ",  topic_id=" . $topicId . ", pageview='{$pageview}'\n\t\t\t\t   WHERE id=" . $track->id);
        if ($spThisUser->member) {
            sp_update_users_newposts();
        }
        $spThisUser->trackid = $track->id;
        $spThisUser->session_first_visit = false;
        $spThisUser->notification = $track->notification;
    } else {
        # newly arrived
        # set deice being used
        $device = 'D';
        switch ($spDevice) {
            case 'mobile':
                $device = 'M';
                break;
            case 'tablet':
                $device = 'T';
                break;
            case 'desktop':
                $device = 'D';
                break;
        }
        # display classes
        $display = 'spType-' . $spThisUser->usertype;
        if (!empty($spThisUser->rank)) {
            $display .= ' spRank-' . sp_create_slug($spThisUser->rank[0]['name'], false);
        }
        if (!empty($spThisUser->special_rank)) {
            foreach ($spThisUser->special_rank as $rank) {
                $display .= ' spSpecialRank-' . sp_create_slug($rank['name'], false);
            }
        }
        if (!empty($spThisUser->memberships)) {
            foreach ($spThisUser->memberships as $membership) {
                $display .= ' spUsergroup-' . sp_create_slug($membership['usergroup_name'], false);
            }
        }
        spdb_query("INSERT INTO " . SFTRACK . "\n\t\t\t \t   (trackuserid, trackname, forum_id, topic_id, trackdate, pageview, device, display) VALUES\n\t\t\t \t   ({$trackUserId}, '{$trackName}', {$forumId}, {$topicId}, '{$now}', '{$pageview}', '{$device}', '{$display}')");
        $spThisUser->trackid = $spVars['insertid'];
        $spThisUser->session_first_visit = true;
        if ($spThisUser->member) {
            sp_update_users_newposts();
        }
    }
    # Check for expired tracking - some may have left the scene
    $splogin = sp_get_option('sflogin');
    $timeout = $splogin['sptimeout'];
    if (!$timeout) {
        $timeout = 20;
    }
    $expired = spdb_table(SFTRACK, "trackdate < DATE_SUB('{$now}', INTERVAL {$timeout} MINUTE)");
    if ($expired) {
        # if any Members expired - update user meta
        foreach ($expired as $expire) {
            if ($expire->trackuserid > 0) {
                sp_set_last_visited($expire->trackuserid);
            }
        }
        # finally delete them
        spdb_query("DELETE FROM " . SFTRACK . "\n\t\t\t\t\tWHERE trackdate < DATE_SUB('{$now}', INTERVAL {$timeout} MINUTE)");
    }
}
function sp_do_sp_ForumDropdownTag($args = '')
{
    #check if forum displayed
    if (sp_abort_display_forum()) {
        return;
    }
    $defs = array('tagId' => 'spForumDropdownTag', 'tagClass' => 'spLinkTag', 'selectClass' => 'spSelectTag', 'forumList' => 0, 'label' => __("Select forum", 'sp-ttags'), 'length' => 30, 'echo' => 1);
    $a = wp_parse_args($args, $defs);
    $a = apply_filters('sph_ForumDropdownTag_args', $a);
    extract($a, EXTR_SKIP);
    # sanitize before use
    $tagId = esc_attr($tagId);
    $tagClass = esc_attr($tagClass);
    $selectClass = esc_attr($selectClass);
    $forumList = esc_attr($forumList);
    $label = sp_filter_title_display($label);
    $length = (int) $length;
    $echo = (int) $echo;
    global $spThisUser;
    sp_forum_api_support();
    $forum_ids = array();
    if ($forumList == 0) {
        $forum_ids = sp_get_forum_memberships($spThisUser->ID);
    } else {
        $allforums = explode(',', $forumList);
        foreach ($allforums as $thisforum) {
            if (sp_can_view($thisforum, 'forum-title')) {
                $forum_ids[] = $thisforum;
            }
        }
    }
    if (empty($forum_ids)) {
        return;
    }
    # create where clause based on forums that current user can view
    $where = "forum_id IN (" . implode(",", $forum_ids) . ")";
    $spdb = new spdbComplex();
    $spdb->table = SFFORUMS;
    $spdb->fields = 'forum_slug, forum_name';
    $spdb->join = array(SFGROUPS . ' ON ' . SFFORUMS . '.group_id = ' . SFGROUPS . '.group_id');
    $spdb->where = $where;
    $spdb->orderby = 'group_seq, forum_seq';
    $forums = $spdb->select();
    $out = "<div id='{$tagId}' class='{$tagClass}'>";
    $out .= '<select name="forumselect" class="' . $selectClass . '" onChange="javascript:spjChangeForumURL(this)">' . "\n";
    $out .= '<option>' . $label . '</option>' . "\n";
    foreach ($forums as $forum) {
        $out .= '<option value="' . sp_build_url($forum->forum_slug, '', 0, 0) . '">&nbsp;&nbsp;' . sp_create_name_extract(sp_filter_title_display($forum->forum_name), $length) . '</option>' . "\n";
    }
    $out .= '</select>' . "\n";
    $out .= '</div>';
    $out .= '<script type="text/javascript">';
    $out .= 'function spjChangeForumURL(menuObj) {';
    $out .= 'var i = menuObj.selectedIndex;';
    $out .= 'if(i > 0) {';
    $out .= 'if(menuObj.options[i].value != "#") {';
    $out .= 'window.location = menuObj.options[i].value;';
    $out .= '}}}';
    $out .= '</script>';
    $out = apply_filters('sph_ForumDropdownTag', $out);
    if ($echo) {
        echo $out;
    } else {
        return $out;
    }
}
function sp_user_visible_forums($view = 'forum-title')
{
    global $spThisUser, $spGlobals;
    if (empty($spThisUser->auths)) {
        return '';
    }
    $forum_ids = '';
    foreach ($spThisUser->auths as $forum => $forum_auth) {
        if ($forum != 'global' && sp_can_view($forum, $view)) {
            $forum_ids[] = $forum;
        }
    }
    return $forum_ids;
}
function sp_do_sp_HotTopicsTag($args = '')
{
    #check if forum displayed
    if (sp_abort_display_forum()) {
        return;
    }
    global $spThisUser;
    $defs = array('tagId' => 'spHotTopicsTag', 'tagClass' => 'spHotTopicsTag', 'listClass' => 'spListItemTag', 'textClass' => 'spHotTopicTextTag', 'listTags' => 1, 'forumIds' => 0, 'limit' => 5, 'days' => 30, 'showForum' => 1, 'textForum' => __('posted in', 'sp-ttags'), 'showCount' => 1, 'textCount' => __('recent posts', 'sp-ttags'), 'showHotness' => 1, 'textHotness' => __('hotness', 'sp-ttags'), 'echo' => 1);
    $a = wp_parse_args($args, $defs);
    $a = apply_filters('sph_HotTopicsTag_args', $a);
    extract($a, EXTR_SKIP);
    # sanitize before use
    $tagId = esc_attr($tagId);
    $tagClass = esc_attr($tagClass);
    $listClass = esc_attr($listClass);
    $textClass = esc_attr($textClass);
    $listTags = (int) $listTags;
    $forumIds = sp_filter_title_display($forumIds);
    $limit = (int) $limit;
    $days = (int) $days;
    $showForum = (int) $showForum;
    $textForum = sp_filter_title_display($textForum);
    $showCount = (int) $showCount;
    $textCount = sp_filter_title_display($textCount);
    $showHotness = (int) $showHotness;
    $textHotness = sp_filter_title_display($textHotness);
    $echo = (int) $echo;
    sp_forum_api_support();
    $forumList = '';
    if (!empty($forumIds)) {
        # are we passing forum ID's?
        $flist = explode(',', $forumIds);
        foreach ($flist as $id) {
            if (sp_can_view($id, 'topic-title')) {
                $forumList[] = $id;
            }
        }
    } else {
        global $spThisUser;
        $allForums = sp_get_forum_memberships($spThisUser->ID);
        if ($allForums) {
            foreach ($allForums as $id) {
                if (sp_can_view($id, 'topic-title')) {
                    $forumList[] = $id;
                }
            }
        }
    }
    if (!empty($forumList)) {
        $where = ' AND ' . SFPOSTS . '.forum_id IN (' . implode(',', $forumList) . ')';
    } else {
        return '';
    }
    # get any posts that meeet date criteria
    $spdb = new spdbComplex();
    $spdb->table = SFPOSTS;
    $spdb->fields = SFPOSTS . '.topic_id, DATEDIFF(CURDATE(), post_date) AS delta, ' . SFPOSTS . '.forum_id, forum_name, forum_slug, forum_slug, topic_name, topic_slug';
    $spdb->join = array(SFTOPICS . ' ON ' . SFTOPICS . '.topic_id = ' . SFPOSTS . '.topic_id', SFFORUMS . ' ON ' . SFFORUMS . '.forum_id = ' . SFPOSTS . '.forum_id');
    $spdb->where = 'DATE_SUB(CURDATE(),INTERVAL ' . $days . ' DAY) <= post_date' . $where;
    $spdb = apply_filters('sph_HotTopicsTagQuery', $spdb);
    $posts = $spdb->select();
    $out = '';
    $out = $listTags ? "<ul id='{$tagId}' class='{$tagClass}'>" : "<div id='{$tagId}' class='{$tagClass}'>";
    if ($posts) {
        # give each topic with posts a score - currently ln(cur date - post date) for each post
        $score = $count = $forum_name = $forum_slug = $topic_slug = $topic_name = array();
        foreach ($posts as $post) {
            if ($post->delta != $days) {
                $value = apply_filters('sph_HotTopicTagScore', log($days - $post->delta), $post, $a);
                # let plugins modify the hotness algorithm
                $score[$post->topic_id] = isset($score[$post->topic_id]) ? $score[$post->topic_id] + $value : $value;
                $count[$post->topic_id] = isset($count[$post->topic_id]) ? $count[$post->topic_id] + 1 : 1;
                $forum_name[$post->topic_id] = sp_filter_title_display($post->forum_name);
                $forum_slug[$post->topic_id] = $post->forum_slug;
                $topic_slug[$post->topic_id] = $post->topic_slug;
                $topic_name[$post->topic_id] = sp_filter_title_display($post->topic_name);
            }
        }
        # reverse sort the posts and limit to number to display
        arsort($score);
        $topics = array_slice($score, 0, $limit, true);
        # now output the popular topics
        foreach ($topics as $id => $topic) {
            $out .= $listTags ? "<li class='{$listClass}'>" : "<div class='{$textClass}'>";
            $out .= sp_get_topic_url($forum_slug[$id], $topic_slug[$id], $topic_name[$id]);
            if ($showForum) {
                $out .= " {$textForum} {$forum_name[$id]}";
            }
            if ($showCount) {
                $out .= ' (' . $count[$id] . " {$textCount})";
            }
            if ($showHotness) {
                $out .= ' (' . round($score[$id], 2) . " {$textHotness})";
            }
            $out .= $listTags ? '</li>' : '</div>';
        }
    } else {
        $out .= "<div class='{$textClass}'>" . __('No current hot topics to display', 'sp-ttags') . '</div>';
    }
    $out .= $listTags ? '</ul>' : '</div>';
    $out = apply_filters('sph_HotTopicsTag', $out);
    if ($echo) {
        echo $out;
    } else {
        return $out;
    }
}