/wp-content/plugins/simple-forum/library/sf-database.php
PHP | 4129 lines | 2665 code | 598 blank | 866 comment | 551 complexity | 3491957a84532d539ee63760e4318d23 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /*
- Simple:Press
- Main database routines
- $LastChangedDate: 2011-04-26 05:52:27 -0700 (Tue, 26 Apr 2011) $
- $Rev: 5981 $
- */
-
- if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
- die('Access Denied');
- }
-
- # ******************************************************************
- # GROUP/FORUM VIEW AND GENERAL DB FUNCTIONS
- # ******************************************************************
-
- # ------------------------------------------------------------------
- # sf_get_combined_groups_and_forums($groupid)
- #
- # Grabs all groups and forums. Note that the group data is repeated.
- # Used to populate 'Select Forum Quicklinks' and Front Main page
- # of forum (Group/Forum View)
- # $groupid: Optional id to display just a single group
- # ------------------------------------------------------------------
- function sf_get_combined_groups_and_forums($groupid = null)
- {
- global $wpdb, $session_groups, $current_user;
-
- If(is_null($groupid) ? $where='' : $where = " WHERE ".SFGROUPS.".group_id=".$groupid." ");
-
- # retrieve group and forum records
- $records = $wpdb->get_results(
- "SELECT ".SFGROUPS.".group_id, group_name, group_desc, group_rss, group_icon, group_message,
- forum_id, forum_name, forum_slug, forum_desc, forum_status, forum_icon, forum_rss_private, post_id, topic_count, parent, children
- FROM ".SFGROUPS."
- JOIN ".SFFORUMS." ON ".SFGROUPS.".group_id = ".SFFORUMS.".group_id
- ".$where."
- ORDER BY group_seq, forum_seq;");
-
- # rebuild into an array grabbing permissions on the way
- $groups=array();
-
- if($records)
- {
- # Set initially to Access Denied in case current user can view no forums
- $groups[0]['group_id'] = "Access Denied";
- $gindex=-1;
- $findex=0;
-
- foreach($records as $record)
- {
- $groupid=$record->group_id;
- $forumid=$record->forum_id;
-
- if (sf_can_view_forum($forumid) || sf_user_can($current_user->ID, 'Can view forum lists only', $forumid) || sf_user_can($current_user->ID, 'Can view forum and topic lists only', $forumid))
- {
- if($gindex == -1 || $groups[$gindex]['group_id'] != $groupid)
- {
- $gindex++;
- $findex=-1;
- $groups[$gindex]['group_id']=$record->group_id;
- $groups[$gindex]['group_name']=$record->group_name;
- $groups[$gindex]['group_desc']=$record->group_desc;
- $groups[$gindex]['group_rss']=$record->group_rss;
- $groups[$gindex]['group_icon']=$record->group_icon;
- $groups[$gindex]['group_message']=$record->group_message;
- }
- if(isset($record->forum_id))
- {
- $groups[$gindex]['forums'][$findex]['forum_id']=$record->forum_id;
- $groups[$gindex]['forums'][$findex]['forum_name']=$record->forum_name;
- $groups[$gindex]['forums'][$findex]['forum_slug']=$record->forum_slug;
- $groups[$gindex]['forums'][$findex]['forum_desc']=$record->forum_desc;
- $groups[$gindex]['forums'][$findex]['forum_status']=$record->forum_status;
- $groups[$gindex]['forums'][$findex]['forum_icon']=$record->forum_icon;
- $groups[$gindex]['forums'][$findex]['forum_rss_private']=$record->forum_rss_private;
- $groups[$gindex]['forums'][$findex]['post_id']=$record->post_id;
- $groups[$gindex]['forums'][$findex]['topic_count']=$record->topic_count;
- $groups[$gindex]['forums'][$findex]['parent']=$record->parent;
- $groups[$gindex]['forums'][$findex]['children']=$record->children;
- $findex++;
- }
- }
- }
- } else {
- $records = sf_get_groups_all(false, false);
- if($records)
- {
- foreach($records as $record)
- {
- $groups[$gindex]['group_id']=$record->group_id;
- $groups[$gindex]['group_name']=$record->group_name;
- $groups[$gindex]['group_desc']=$record->group_desc;
- $groups[$gindex]['group_rss']=$record->group_rss;
- $groups[$gindex]['group_icon']=$record->group_icon;
- $groups[$gindex]['group_message']=$record->group_message;
- $gindex++;
- }
- }
- }
- $session_groups = $groups;
- return $groups;
- }
-
- # ------------------------------------------------------------------
- # sf_get_combined_groups_and_forums_bloglink()
- #
- # Grabs all groups and forums. Soecial cut down version for
- # populating the blog link add post drop down
- # ------------------------------------------------------------------
- function sf_get_combined_groups_and_forums_bloglink()
- {
- global $wpdb, $current_user;
-
- # retrieve group and forum records
- $records = $wpdb->get_results(
- "SELECT ".SFGROUPS.".group_id, group_name,
- forum_id, forum_name
- FROM ".SFGROUPS."
- JOIN ".SFFORUMS." ON ".SFGROUPS.".group_id = ".SFFORUMS.".group_id
- ".$where."
- ORDER BY group_seq, forum_seq;");
-
- # rebuild into an array grabbing permissions on the way
- $groups=array();
- $gindex=-1;
- $findex=0;
- if($records)
- {
- foreach($records as $record)
- {
- $groupid=$record->group_id;
- $forumid=$record->forum_id;
-
- if (sf_user_can($current_user->ID, 'Can create linked topics', $forumid) && sf_user_can($current_user->ID, 'Can start new topics', $forumid))
- {
- if($gindex == -1 || $groups[$gindex]['group_id'] != $groupid)
- {
- $gindex++;
- $findex=0;
- $groups[$gindex]['group_id']=$record->group_id;
- $groups[$gindex]['group_name']=$record->group_name;
- }
- if(isset($record->forum_id))
- {
- $groups[$gindex]['forums'][$findex]['forum_id']=$record->forum_id;
- $groups[$gindex]['forums'][$findex]['forum_name']=$record->forum_name;
- $findex++;
- }
- }
- }
- }
-
- return $groups;
- }
-
- function sf_get_combined_forum_stats($posts)
- {
- global $wpdb, $current_user;
-
- $clause='';
-
- if($posts)
- {
- $pcount = count($posts);
- $done = 0;
-
- foreach ($posts as $post)
- {
- $clause.= "(".SFPOSTS.".post_id=".$post.")";
- $done++;
- if($done < $pcount) $clause.= " OR ";
- }
- } else {
- $record=array();
- $record['topic_count'] = '0';
- $record['post_count'] = '0';
- $record['udate'] = '';
-
- return $record;
- }
-
- $records = $wpdb->get_results(
- "SELECT ".SFPOSTS.".post_id, ".SFPOSTS.".topic_id, topic_name, ".SFPOSTS.".forum_id, ".sf_zone_datetime('post_date').",
- UNIX_TIMESTAMP(post_date) as udate, guest_name, ".SFPOSTS.".user_id, post_content, post_status, ".SFMEMBERS.".display_name, post_index,
- ".SFFORUMS.".topic_count, ".SFFORUMS.".post_count, topic_slug
- FROM ".SFPOSTS."
- LEFT JOIN ".SFFORUMS." ON ".SFPOSTS.".forum_id = ".SFFORUMS.".forum_id
- LEFT JOIN ".SFTOPICS." ON ".SFPOSTS.".topic_id = ".SFTOPICS.".topic_id
- LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
- WHERE ".$clause.";", ARRAY_A);
-
- if($records)
- {
- $sfdisplay=array();
- $sfdisplay=sf_get_option('sfdisplay');
-
- $forums = array();
- foreach($records as $record)
- {
- $forums[$record['forum_id']] = $record;
-
- # do we need a post tooltip on the topic link?
- if (($sfdisplay['topics']['posttip']==true) && (sf_can_view_forum($record['forum_id'])==true) && (sf_user_can($current_user->ID, 'Can view forum lists only', $record['forum_id'])==false) && (sf_user_can($current_user->ID, 'Can view forum and topic lists only', $record['forum_id'])==false))
- {
- $forums[$record['forum_id']]['post_tip']=sf_filter_tooltip_display($record['post_content'], $record['post_status']);
- $forums[$record['forum_id']]['post_content']='';
- }
- }
- }
-
- return $forums;
- }
-
- # ------------------------------------------------------------------
- # sf_get_group_record()
- #
- # Returns a single group row
- # $groupid: group_id of group to return
- # $asArray: return as an array if true
- # Note: No permission checking is performed
- # ------------------------------------------------------------------
- function sf_get_group_record($groupid, $asArray=false)
- {
- global $wpdb;
-
- if(!$groupid) return '';
-
- $sql=(
- "SELECT *
- FROM ".SFGROUPS."
- WHERE group_id=".$groupid.";");
- if($asArray) return $wpdb->get_row($sql, ARRAY_A);
- return $wpdb->get_row($sql);
- }
-
- # ------------------------------------------------------------------
- # sf_get_groups_all()
- #
- # Return ALL group records - no permission checking
- # $id_only: Optionsal = return just ids
- # $asArray: Optional - return as array
- # ------------------------------------------------------------------
- function sf_get_groups_all($id_only=false, $asArray=false)
- {
- global $wpdb;
-
- if($id_only ? $FROM='group_id' : $FROM='*');
-
- $sql=("SELECT ".$FROM." FROM ".SFGROUPS." ORDER BY group_seq");
- if($asArray) return $wpdb->get_results($sql, ARRAY_A);
- return $wpdb->get_results($sql);
- }
-
- # ------------------------------------------------------------------
- # sf_group_exists()
- #
- # Check the existence of a group by id
- # $groupid: group to check for
- # ------------------------------------------------------------------
- function sf_group_exists($groupid)
- {
- global $wpdb;
-
- if(empty($groupid)) return false;
- if($wpdb->get_var(
- "SELECT group_name
- FROM ".SFGROUPS."
- WHERE group_id=".$groupid))
- {
- return true;
- }
- return false;
- }
-
- # ------------------------------------------------------------------
- # sf_get_group_rss_url()
- #
- # Returns the RSS feed URL for a Group (custom or standard)
- # $groupid: group to return
- # ------------------------------------------------------------------
- function sf_get_group_rss_url($groupid)
- {
- global $wpdb, $sfglobals;
-
- if (empty($groupid)) return '';
-
- $url = $wpdb->get_var("SELECT group_rss FROM ".SFGROUPS." WHERE group_id=".$groupid);
- if (empty($url))
- {
- $rssopt = sf_get_option('sfrss');
- if ($rssopt['sfrssfeedkey'] && isset($sfglobals['member']['feedkey']))
- $url = sf_build_qurl('group='.$groupid, 'xfeed=group', 'feedkey='.$sfglobals['member']['feedkey']);
- else
- $url = sf_build_qurl('group='.$groupid, 'xfeed=group');
- }
-
- return $url;
- }
-
- # ------------------------------------------------------------------
- # sf_get_group_name_from_forum()
- #
- # Returns the Group Name when only the forum id is known
- # $forumid: forum to lookup for group name
- # ------------------------------------------------------------------
- function sf_get_group_name_from_forum($forumid)
- {
- global $wpdb;
-
- if(!$forumid) return '';
-
- return $wpdb->get_var(
- "SELECT ".SFGROUPS.".group_name
- FROM ".SFGROUPS."
- JOIN ".SFFORUMS." ON ".SFFORUMS.".group_id = ".SFGROUPS.".group_id
- WHERE ".SFFORUMS.".forum_id=".$forumid);
- }
-
- # ******************************************************************
- # FORUM/TOPIC VIEW AND GENERAL DB FUNCTIONS
- # ******************************************************************
-
- # ------------------------------------------------------------------
- # sf_get_combined_forums_and_topics($forumid)
- #
- # Grabs all forums and their topics. Note that the forum data is
- # repeated. Used to populate Topic Listing page of topics
- # (Forum/Topics View)
- # $forumid: forum id to display
- # $currentpage: index to paging
- # ------------------------------------------------------------------
- function sf_get_combined_forums_and_topics($forumid, $currentpage)
- {
- global $wpdb, $sfvars, $sfglobals, $current_user;
-
- if(!$forumid) return '';
-
- $sfvars['searchresults'] = 0;
-
- # rebuild into an array
- $forums=array();
-
- # Set initially to Access Denied in case current user can view no forums
- $forums[0]['forum_id']="Access Denied";
-
- # quick permission check
- if(!sf_can_view_forum($forumid) && !sf_user_can($current_user->ID, 'Can view forum and topic lists only', $forumid)) return $forums;
-
- # some setup vars
- $startlimit = 0;
-
- # get post tooltip state
- $sfdisplay = array();
- $sfdisplay = sf_get_option('sfdisplay');
-
- # how many topics per page?
- $tpaged = $sfglobals['display']['topics']['perpage'];
- if(!$tpaged) $tpaged=20;
-
- # setup where we are in the topic list (paging)
- if($sfvars['searchpage'] == 0)
- {
- if($currentpage != 1)
- {
- $startlimit = ((($currentpage-1) * $tpaged));
- }
- } else {
- if($sfvars['searchpage'] == 1)
- {
- $currentpage = 1;
- } else {
- $startlimit = ((($sfvars['searchpage']-1) * $tpaged));
- }
- }
-
- $LIMIT = " LIMIT ".$startlimit.', '.$tpaged;
-
- if($sfvars['searchpage'] == 0)
- {
- $topicsort = $sfglobals['display']['topics']['sortnewtop'];
- if ($topicsort)
- {
- $ORDER = " ORDER BY topic_pinned DESC, ".SFTOPICS.".post_id DESC";
- } else {
- $ORDER = " ORDER BY topic_pinned DESC, ".SFTOPICS.".post_id ASC";
- }
- } else {
- $ORDER = " ORDER BY ".SFTOPICS.".post_id DESC";
- }
-
- if($sfvars['searchpage'] == 0)
- {
- # standar forum view
- $SELECT = "SELECT ";
- $MATCH = "";
- $ANDWHERE = "";
- } else {
-
- $searchvalue=urldecode($sfvars['searchvalue']);
- if(empty($searchvalue))
- {
- return '';
- }
-
- # what sort of search is it?
- if($sfvars['searchtype'] == 6) {
-
- # topic status search
- $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
- $MATCH = "";
- $ANDWHERE = " AND topic_status_flag=".$sfvars['searchvalue']." ";
-
- } elseif($sfvars['searchtype'] == 8) {
-
- # members 'posted in' sepcified forum search
- $userid = $sfvars['searchvalue'];
- $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
- $MATCH = "";
- $ANDWHERE = " AND ".SFPOSTS.".user_id=".$userid." ";
-
- } elseif($sfvars['searchtype'] == 9) {
-
- # members 'started' in sepcified forum search
- $userid = $sfvars['searchvalue'];
- $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
- $MATCH = "";
- $ANDWHERE = " AND ".SFPOSTS.".user_id=".$userid." ";
-
- } elseif($sfvars['searchinclude'] == 4) {
-
- # it's a tag search
- $searchtag = sf_create_slug($searchvalue, 'tag');
- $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
- $MATCH = SFTOPICS.".topic_id IN (SELECT topic_id FROM ".SFTAGMETA." JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
- WHERE tag_slug = '".$searchtag."' AND forum_id=".$forumid.") AND ";
- $ANDWHERE = "";
-
- } else {
-
- # general keyword search
- $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
-
- $searchterm = sf_construct_search_term($searchvalue, $sfvars['searchtype']);
-
- switch($sfvars['searchinclude'])
- {
- case 1:
- $MATCH = "(MATCH(".SFPOSTS.".post_content) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE) OR MATCH(".SFTOPICS.".topic_name) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE)) AND ";
- break;
- case 2:
- $MATCH = "MATCH(".SFPOSTS.".post_content) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE) AND ";
- break;
- case 3:
- $MATCH = "MATCH(".SFTOPICS.".topic_name) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE) AND ";
- break;
- }
- $ANDWHERE = "";
- }
- }
-
- # retrieve forum and topic records - is a type 1, 2, 3 or 8 then we need a separate query (for now)
- # NO post_content or post_index = 1
- if (!empty($sfvars['searchtype']) && ($sfvars['searchtype'] == 1 || $sfvars['searchtype'] == 2 || $sfvars['searchtype'] == 3 || $sfvars['searchtype'] == 8))
- {
- $records = sf_query_results(
- $SELECT.SFFORUMS.".forum_id, forum_slug, forum_name, forum_status, group_id, topic_count, forum_icon, forum_desc, topic_status_set,
- parent, children, forum_message,
- ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
- topic_status, topic_pinned, topic_opened, topic_subs, topic_watches, topic_status_flag,
- post_ratings, use_tags, blog_post_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count, ".SFTOPICS.".user_id, post_status
- FROM ".SFFORUMS."
- JOIN ".SFTOPICS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
- JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
- WHERE ".$MATCH.SFFORUMS.".forum_id=".$forumid.$ANDWHERE.$ORDER.$LIMIT.";");
- } else {
- $records = sf_query_results(
- $SELECT.SFFORUMS.".forum_id, forum_slug, forum_name, forum_status, group_id, topic_count, forum_icon, forum_desc, topic_status_set,
- parent, children, forum_message,
- ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
- topic_status, topic_pinned, topic_opened, topic_subs, topic_watches, topic_status_flag,
- post_ratings, use_tags, blog_post_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count, ".SFTOPICS.".user_id, post_content, post_status
- FROM ".SFFORUMS."
- JOIN ".SFTOPICS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
- JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
- WHERE ".$MATCH.SFFORUMS.".forum_id=".$forumid.$ANDWHERE." AND ".SFPOSTS.".post_index=1 ".$ORDER.$LIMIT.";");
- }
-
- if (!empty($sfvars['searchpage']) && $records)
- {
- $sfvars['searchresults'] = $wpdb->get_var("SELECT FOUND_ROWS()");
- }
-
- $findex=-1;
- $tindex=0;
-
- if($records)
- {
- $topiclist = '';
- foreach($records as $record)
- {
- $forumid=$record->forum_id;
-
- if($findex == -1 || $forums[$findex]['forum_id'] != $forumid)
- {
- $findex++;
- $tindex=0;
- $forums[$findex]['forum_id']=$record->forum_id;
- $forums[$findex]['forum_slug']=$record->forum_slug;
- $forums[$findex]['forum_name']=$record->forum_name;
- $forums[$findex]['forum_desc']=$record->forum_desc;
- $forums[$findex]['forum_status']=$record->forum_status;
- $forums[$findex]['group_id']=$record->group_id;
- $forums[$findex]['topic_count']=$record->topic_count;
- $forums[$findex]['forum_icon']=$record->forum_icon;
- $forums[$findex]['topic_status_set']=$record->topic_status_set;
- $forums[$findex]['post_ratings']=$record->post_ratings;
- $forums[$findex]['use_tags']=$record->use_tags;
- $forums[$findex]['parent']=$record->parent;
- $forums[$findex]['children']=$record->children;
- $forums[$findex]['forum_message']=$record->forum_message;
- }
- $forums[$findex]['topics'][$tindex]['topic_id']=$record->topic_id;
- $forums[$findex]['topics'][$tindex]['topic_slug']=$record->topic_slug;
- $forums[$findex]['topics'][$tindex]['topic_name']=$record->topic_name;
- $forums[$findex]['topics'][$tindex]['topic_date']=$record->topic_date;
- $forums[$findex]['topics'][$tindex]['topic_status']=$record->topic_status;
- $forums[$findex]['topics'][$tindex]['topic_pinned']=$record->topic_pinned;
- $forums[$findex]['topics'][$tindex]['topic_opened']=$record->topic_opened;
- $forums[$findex]['topics'][$tindex]['topic_subs']=$record->topic_subs;
- $forums[$findex]['topics'][$tindex]['topic_watches']=$record->topic_watches;
- $forums[$findex]['topics'][$tindex]['topic_status_flag']=$record->topic_status_flag;
- $forums[$findex]['topics'][$tindex]['post_ratings']=$record->post_ratings;
- $forums[$findex]['topics'][$tindex]['use_tags']=$record->use_tags;
- $forums[$findex]['topics'][$tindex]['blog_post_id']=$record->blog_post_id;
- $forums[$findex]['topics'][$tindex]['post_id']=$record->post_id;
- $forums[$findex]['topics'][$tindex]['post_count']=$record->post_count;
- $forums[$findex]['topics'][$tindex]['user_id']=$record->user_id;
-
- # do we need a post tooltip on the topic link?
- if ($sfdisplay['topics']['posttip'] && (!empty($sfvars['searchtype']) && $sfvars['searchtype'] != 8) && $current_user->sfaccess)
- {
- $forums[$findex]['topics'][$tindex]['post_tip'] = sf_filter_tooltip_display($record->post_content, $record->post_status);
- } else {
- $forums[$findex]['topics'][$tindex]['post_tip']="";
- }
-
- # save topic in list for getting tags later
- $topiclist[] = $record->topic_id;
-
- $tindex++;
- }
-
- # Now get tags in one query for these topics
- $topiclist = implode(',', $topiclist);
- $sql = "SELECT tag_name, tag_slug, topic_id
- FROM ".SFTAGS."
- JOIN ".SFTAGMETA." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
- WHERE topic_id IN (".$topiclist.")
- ORDER BY topic_id";
- $tags = $wpdb->get_results($sql);
-
- # Now sort the tags into the monsterous forum array
- if ($tags)
- {
- foreach ($forums as $findex => $forum)
- {
- foreach ($forum['topics'] as $tindex => $topic)
- {
- if ($topic['use_tags'])
- {
- $topictags = '';
- foreach ($tags as $tag)
- {
- if ($topic['topic_id'] == $tag->topic_id)
- {
- # build tag list for this forum/topic
- $topictags[] = $tag;
- }
- }
- # save the tags into the master array
- $forums[$findex]['topics'][$tindex]['tags'] = $topictags;
- }
- }
- }
- }
- } else {
- $record = sf_get_forum_record($forumid);
- if($record)
- {
- $forums[0]['forum_id']=$record->forum_id;
- $forums[0]['forum_slug']=$record->forum_slug;
- $forums[0]['forum_name']=$record->forum_name;
- $forums[0]['forum_desc']=$record->forum_desc;
- $forums[0]['forum_status']=$record->forum_status;
- $forums[0]['group_id']=$record->group_id;
- $forums[0]['topic_count']=$record->topic_count;
- $forums[0]['forum_icon']=$record->forum_icon;
- $forums[0]['topic_status_set']=$record->topic_status_set;
- $forums[0]['post_ratings']=$record->post_ratings;
- $forums[0]['use_tags']=$record->use_tags;
- $forums[0]['parent']=$record->parent;
- $forums[0]['children']=$record->children;
- $forums[0]['forum_message']=$record->forum_message;
- }
- }
- return $forums;
- }
-
- function sf_get_subforums($sublist)
- {
- global $wpdb;
-
- if (!$sublist) return '';
-
- $subforums = unserialize($sublist);
- $forumlist = array();
- foreach ($subforums as $forumid)
- {
- if (sf_can_view_forum($forumid)) $forumlist[] = $forumid;
- }
-
- if (empty($forumlist)) return '';
-
- $where = " WHERE forum_id IN (" . implode(",", $forumlist) . ")";
- $subforums = $wpdb->get_results("SELECT * FROM ".SFFORUMS.$where." ORDER BY forum_seq", ARRAY_A);
-
- return $subforums;
- }
-
- # ------------------------------------------------------------------
- # sf_get_combined_topic_stats()
- #
- # Returns the first and last post data for a topic
- # $postid: post id from new posts list
- # $user_id for checking of user has posted in topic -
- # ------------------------------------------------------------------
- function sf_get_combined_topic_stats($posts, $userid=0)
- {
- global $wpdb, $current_user;
-
- $clause='';
- if($posts)
- {
- $pcount = count($posts);
- $done = 0;
-
- foreach ($posts as $topic=>$postindex)
- {
- $clause.= "(topic_id = ".$topic." AND post_index = 1 OR topic_id = ".$topic." AND post_index=".$postindex.")";
- $done++;
- if($done < $pcount) $clause.= " OR ";
- }
- } else {
- return;
- }
-
- $records = $wpdb->get_results(
- "SELECT post_id, topic_id, forum_id, ".sf_zone_datetime('post_date').", UNIX_TIMESTAMP(post_date) as udate, guest_name, ".SFPOSTS.".user_id, post_index, post_status, post_content, ".SFMEMBERS.".display_name
- FROM ".SFPOSTS."
- LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
- WHERE ".$clause."
- ORDER BY post_id ASC;", ARRAY_A);
-
- # If forum view are we looking to see if user has posted in ant of the topics?
- if($records && $userid)
- {
- $clause='';
- $pcount = count($posts);
- $done = 0;
-
- foreach ($posts as $topic=>$postindex)
- {
- $clause.= "(topic_id = ".$topic." AND user_id = ".$userid.")";
- $done++;
- if($done < $pcount) $clause.= " OR ";
- }
-
- $userposted = $wpdb->get_results(
- "SELECT DISTINCT topic_id, user_id
- FROM ".SFPOSTS."
- WHERE ".$clause.";");
- } else {
- $userposted = '';
- }
- # Now to format and combine results
- if ($records)
- {
- $sfdisplay=array();
- $sfdisplay=sf_get_option('sfdisplay');
-
- $topics = array();
- foreach ($records as $record)
- {
- $topics[$record['topic_id']][$record['post_index']] = $record;
-
- # do we need a post tooltip on the topic link?
- if ($sfdisplay['topics']['posttip'] && $current_user->sfaccess)
- {
- $topics[$record['topic_id']][$record['post_index']]['post_tip']=sf_filter_tooltip_display($record['post_content'], $record['post_status']);
- $topics[$record['topic_id']][$record['post_index']]['post_content']='';
- }
- }
-
- foreach ($records as $record)
- {
- $topics[$record['topic_id']]['thisuser'] = false;
- if ($userposted)
- {
- foreach ($userposted as $user)
- {
- if ($user->topic_id == $record['topic_id'])
- {
- $topics[$record['topic_id']]['thisuser'] = true;
- break;
- }
- }
- }
- }
- return $topics;
- } else {
- return '';
- }
- }
-
- # ******************************************************************
- # FORUM/TOPIC VIEW AND GENERAL DB FUNCTIONS
- # ******************************************************************
-
- # ------------------------------------------------------------------
- # sf_get_watched_topics($currentpagge)
- #
- # Grabs all watched topics
- # $currentpage: index to paging
- # ------------------------------------------------------------------
- function sf_get_watched_topics($currentpage=0)
- {
- global $wpdb, $current_user, $sfglobals;
-
- # quick permission check
- if (!$current_user->sfwatch) return '';
-
- $limit='';
-
- # get watched topics
- $list = $sfglobals['member']['watches'];
- if (empty($list)) return '';
- # create where clause of watched topics
- $where = " WHERE ".SFTOPICS.".topic_id IN (" . implode(",", $list) . ")";
-
- # retrieve watched topic records
- $query = "SELECT ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
- topic_status, topic_pinned, topic_opened, topic_watches, topic_status_flag,
- blog_post_id, ".SFTOPICS.".forum_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
- forum_slug, forum_name, topic_status_set, post_ratings, group_id, post_content, post_status
- FROM ".SFTOPICS."
- JOIN ".SFFORUMS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
- JOIN ".SFPOSTS." ON ".SFTOPICS.".post_id = ".SFPOSTS.".post_id ".
- $where." ORDER BY topic_date DESC ".$limit;
-
- $records = $wpdb->get_results($query, ARRAY_A);
-
- if($records)
- {
- $sfdisplay=array();
- $sfdisplay=sf_get_option('sfdisplay');
-
- # do we need a post tooltip on the topic link?
- if($sfdisplay['topics']['posttip'])
- {
- for($x=0; $x<count($records); $x++)
- {
- if ($current_user->sfaccess)
- {
- $records[$x]['post_tip']=sf_filter_tooltip_display($records[$x]['post_content'], $records[$x]['post_status']);
- $records[$x]['post_content']='';
- }
- }
- }
- }
-
- $watched['records'] = $records;
- $watched['count'] = count($list);
-
- return $watched;
- }
-
- # ------------------------------------------------------------------
- # sf_get_subscribed_topics($currentpagge)
- #
- # Grabs all subscribed topics
- # $currentpage: index to paging
- # ------------------------------------------------------------------
- function sf_get_subscribed_topics($currentpage=0)
- {
- global $wpdb, $current_user, $sfglobals;
-
- $limit = '';
-
- # quick permission check
- if (!$current_user->sfsubscriptions) return '';
-
- # get subscribed topics
- $list = $sfglobals['member']['subscribe'];
- if (empty($list)) return '';
-
- # create where clause of subscribed topics
- $where = " WHERE ".SFTOPICS.".topic_id IN (" . implode(",", $list) . ")";
-
- # retrieve watched topic records
- $query = "SELECT ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
- topic_status, topic_pinned, topic_opened, topic_subs, topic_status_flag,
- blog_post_id, ".SFTOPICS.".forum_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
- forum_slug, forum_name, topic_status_set, post_ratings, group_id, post_content, post_status
- FROM ".SFTOPICS."
- JOIN ".SFFORUMS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
- JOIN ".SFPOSTS." ON ".SFTOPICS.".post_id = ".SFPOSTS.".post_id ".
- $where." ORDER BY topic_date DESC ".$limit;
- $records = $wpdb->get_results($query, ARRAY_A);
-
- if($records)
- {
- $sfdisplay=array();
- $sfdisplay=sf_get_option('sfdisplay');
-
- # do we need a post tooltip on the topic link?
- if ($sfdisplay['topics']['posttip'])
- {
- for($x=0; $x<count($records); $x++)
- {
- if ($current_user->sfaccess)
- {
- $records[$x]['post_tip']=sf_filter_tooltip_display($records[$x]['post_content'], $records[$x]['post_status']);
- $records[$x]['post_content']='';
- }
- }
- }
- }
-
- $subscribed['records'] = $records;
- $subscribed['count'] = count($list);
-
- return $subscribed;
- }
-
- # ------------------------------------------------------------------
- # sf_get_related_topics($tags)
- #
- # Grabs related topics based on tags
- # ------------------------------------------------------------------
- function sf_get_related_topics($tags)
- {
- global $wpdb;
-
- if(!$tags) return '';
-
- # build list of tags for the topic id
- $taglist = '';
- foreach ($tags as $tag)
- {
- if ($taglist == '')
- {
- $taglist = "('".$tag->tag_slug."'";
- } else {
- $taglist.= ",'".$tag->tag_slug."'";
- }
- }
- $taglist.= ")";
-
- # now grab the results
- $LIMIT = ' LIMIT 10';
- $ORDER = ' ORDER BY topic_id DESC';
- $WHERE = SFTOPICS.".topic_id IN (SELECT topic_id FROM ".SFTAGMETA." JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
- WHERE tag_slug IN ".$taglist.")";
- $topics = $wpdb->get_results(
- "SELECT SQL_CALC_FOUND_ROWS DISTINCT
- ".SFTOPICS.".topic_id, topic_name, topic_slug, ".SFTOPICS.".forum_id, forum_name, forum_slug,
- ".sf_zone_datetime('topic_date').", topic_status, topic_pinned, topic_opened,
- topic_watches, topic_status_flag, blog_post_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
- topic_status_set, post_ratings, group_id
- FROM ".SFTOPICS."
- JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
- JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
- WHERE ".$WHERE.$ORDER.$LIMIT.";", ARRAY_A);
-
- return $topics;
- }
-
- # ------------------------------------------------------------------
- # sf_get_memberlists()
- #
- # Builds viewable member lists for current user
- # ------------------------------------------------------------------
- function sf_get_memberlists($currentpage, $search)
- {
- global $wpdb, $current_user, $sfglobals;
-
- $data = '';
- $data->records = '';
- $data->count = 0;
-
- $sfmemberopts = sf_get_option('sfmemberopts');
- if ($current_user->forumadmin || ($sfmemberopts['sfshowmemberlist'] && $current_user->sfmemberlist))
- {
- # are we limiting member lists to memberships?
- $where = ' WHERE posts > -2';
- $sfmemberopts = sf_get_option('sfmemberopts');
- if ($sfmemberopts['sflimitmemberlist'] && !$current_user->forumadmin)
- {
- # get usergroups user has membership in
- $ugs = sf_get_user_memberships($current_user->ID);
-
- #if no usergroup memberships return empty list
- if ($ugs)
- {
- $ug_ids='';
- foreach ($ugs as $ug)
- {
- $ug_ids[] = $ug['usergroup_id'];
- }
-
- # if no visible usergroups, dont return any search results
- if (empty($ug_ids)) return $data;
- } else {
- return $data;
- }
-
- # create where clause based on user memberships
- $where.= " AND (".SFMEMBERSHIPS.".usergroup_id IN (" . implode(",", $ug_ids) . ") OR ".SFMEMBERSHIPS.".usergroup_id IS NULL)";
- }
-
- if ($search != '')
- {
- $where.= " AND ".SFMEMBERS.'.display_name LIKE "'.$search.'%"';
- }
-
- # how many members per page?
- $startlimit = 0;
- $tpaged = $sfglobals['display']['topics']['perpage'];
- if(!$tpaged) $tpaged=20;
-
- if ($currentpage != 1)
- {
- $startlimit = ((($currentpage-1) * $tpaged));
- }
- $limit = " LIMIT ".$startlimit.', '.$tpaged;
-
- # retrieve members list records
- $query = "SELECT SQL_CALC_FOUND_ROWS DISTINCT IFNULL(".SFMEMBERSHIPS.".usergroup_id, '0') as usergroup_id, ".SFMEMBERS.".user_id,
- ".SFMEMBERS.".display_name, user_email, posts, lastvisit, IFNULL(usergroup_name, 'Admin') as usergroup_name,
- IFNULL(usergroup_desc, 'Forum Administrator') as usergroup_desc
- FROM ".SFUSERS."
- LEFT JOIN ".SFMEMBERS." ON ".SFUSERS.".ID = ".SFMEMBERS.".user_id
- LEFT JOIN ".SFMEMBERSHIPS." ON ".SFMEMBERSHIPS.".user_id = ".SFMEMBERS.".user_id
- LEFT JOIN ".SFUSERGROUPS." ON ".SFUSERGROUPS.".usergroup_id = ".SFMEMBERSHIPS.".usergroup_id
- ".$where."
- ORDER BY usergroup_id, ".SFMEMBERS.".display_name "
- .$limit;
-
- $records = $wpdb->get_results($query, ARRAY_A);
- $data->records = $records;
-
- $data->count = $wpdb->get_var("SELECT FOUND_ROWS()");
- }
-
- return $data;
- }
-
- function sf_get_forum_memberships($user_id)
- {
- global $wpdb, $current_user;
-
- if ($current_user->guest)
- {
- $value = sf_get_sfmeta('default usergroup', 'sfguests');
- $guests = $value[0]['meta_value'];
- $sql = "SELECT forum_id
- FROM ".SFPERMISSIONS."
- WHERE usergroup_id=".$guests;
- } else {
- $sql = "SELECT forum_id
- FROM ".SFPERMISSIONS."
- JOIN ".SFMEMBERSHIPS." ON ".SFPERMISSIONS.".usergroup_id = ".SFMEMBERSHIPS.".usergroup_id
- WHERE user_id=".$user_id;
- }
-
- return $wpdb->get_results($sql);
- }
-
- function sf_get_membership_count($usergroup_id)
- {
- global $wpdb;
-
- if(!$usergroup_id) return '';
-
- $sql = "SELECT COUNT(*)
- FROM ".SFMEMBERSHIPS."
- WHERE ".SFMEMBERSHIPS.".usergroup_id=".$usergroup_id;
- return $wpdb->get_var($sql);
- }
-
- # ------------------------------------------------------------------
- # sf_get_last_post_in_topic()
- #
- # Returns post details of the latest post in the requested topic
- # $topicid: requested topic
- # NOTE: This one remains as used in a template tag
- # ------------------------------------------------------------------
- function sf_get_last_post_in_topic($topicid)
- {
- global $wpdb;
-
- if(!$topicid) return '';
-
- return $wpdb->get_row(
- "SELECT post_id, topic_id, forum_id, post_status, post_index, ".sf_zone_datetime('post_date').", UNIX_TIMESTAMP(post_date) as udate, guest_name, guest_email, ".SFPOSTS.".user_id, ".SFMEMBERS.".display_name, user_email
- FROM ".SFPOSTS."
- LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
- LEFT JOIN ".SFUSERS." ON ".SFPOSTS.".user_id = ".SFUSERS.".ID
- WHERE topic_id = ".$topicid." AND post_status = 0
- ORDER BY post_id DESC LIMIT 1");
- }
-
- # ------------------------------------------------------------------
- # sf_get_postratings()
- #
- # Returns post ratings
- # $postid: post_id of post to return
- # $asArray: return as an array if true
- # Note: No permission checking is performed
- # ------------------------------------------------------------------
- function sf_get_postratings($postid, $asArray=false)
- {
- global $wpdb;
-
- if(!$postid) return '';
-
- $sql=(
- "SELECT *
- FROM ".SFPOSTRATINGS."
- WHERE post_id=".$postid.";");
- if($asArray) return $wpdb->get_row($sql, ARRAY_A);
- return $wpdb->get_row($sql);
- }
-
- # ------------------------------------------------------------------
- # sf_update_postratings()
- #
- # Upates post ratings
- # $postid: post_id
- # $count: number of votes
- # $sum: ratings sum
- # $ips: array of ips voted for guests
- # $members: members that have voted
- # Note: No permission checking is performed
- # ------------------------------------------------------------------
- function sf_update_postratings($postid, $count, $sum, $ips, $members)
- {
- global $wpdb;
-
- if(!$postid) return '';
-
- $sql=(
- "UPDATE ".SFPOSTRATINGS."
- SET vote_count=$count, ratings_sum=$sum, ips='".$ips."', members='".$members."'
- WHERE post_id=".$postid.";");
- $wpdb->query($sql);
- return;
- }
-
- # ------------------------------------------------------------------
- # Add post ratings
- # $postid: post_id
- # $count: number of votes
- # $sum: ratings sum
- # $ips: array of ips voted for guests
- # $members: members that have voted
- # Note: No permission checking is performed
- # ------------------------------------------------------------------
- function sf_add_postratings($postid, $count, $sum, $ips, $members)
- {
- global $wpdb;
-
- if(!$postid) return '';
-
- $sql=(
- "INSERT INTO ".SFPOSTRATINGS." (post_id, vote_count, ratings_sum, ips, members)
- VALUES ($postid, $count, $sum, '".$ips."', '".$members."');");
- $wpdb->query($sql);
- return;
- }
-
- # ------------------------------------------------------------------
- # sf_get_topic_ratings()
- #
- # Returns post ratings
- # $topicid: post_id of post to return
- # Note: No permission checking is performed
- # ------------------------------------------------------------------
- function sf_get_topic_ratings($topicid)
- {
- global $wpdb;
-
- if(!$topicid) return '';
-
- $sql = ("
- SELECT vote_count, ratings_sum
- FROM ".SFPOSTRATINGS."
- JOIN ".SFPOSTS." ON ".SFPOSTS.".topic_id = ".$topicid."
- WHERE ".SFPOSTRATINGS.".post_id = ".SFPOSTS.".post_id");
- return $wpdb->get_results($sql);
- }
-
- # ------------------------------------------------------------------
- # sf_get_forum_record()
- #
- # Returns a single forum row
- # $forumid: forum_id of forum to return
- # Note: No permission checking is performed
- # ------------------------------------------------------------------
- function sf_get_forum_record($forumid)
- {
- global $wpdb, $session_forums;
-
- if(!$forumid) return '';
-
- # check if in the session forums cache
- if($session_forums)
- {
- foreach($session_forums as $forum)
- {
- if($forum->forum_id == $forumid)
- {
- return $forum;
- }
- }
- }
-
- $sql=(
- "SELECT *
- FROM ".SFFORUMS."
- WHERE forum_id=".$forumid.";");
- $thisforum = $wpdb->get_row($sql);
- $session_forums[] = $thisforum;
- return $thisforum;
- }
-
- # ------------------------------------------------------------------
- # sf_get_forum_record_from_slug()
- #
- # Returns a single forum row
- # $forumslug: forum_slug of forum to return
- # $asArray: return as an array if true
- # Note: No permission checking is performed
- # ------------------------------------------------------------------
- function sf_get_forum_record_from_slug($forumslug, $asArray=false)
- {
- global $wpdb;
-
- if(!$forumslug) return '';
-
- $sql=(
- "SELECT *
- FROM ".SFFORUMS."
- WHERE forum_slug='".$forumslug."';");
- if($asArray) return $wpdb->get_row($sql, ARRAY_A);
- return $wpdb->get_row($sql);
- }
-
- # ------------------------------------------------------------------
- # sf_get_group_record_from_slug()
- #
- # Returns a single group and forum row
- # $forumslug: forum_slug of group and forum to return
- # $asArray: return as an array if true
- # Note: No permission checking is performed
- # ------------------------------------------------------------------
- function sf_get_group_record_from_slug($forumslug, $asArray=false)
- {
- global $wpdb;
-
- if(!$forumslug) return '';
-
- $sql=(
- "SELECT *
- FROM ".SFFORUMS."
- JOIN ".SFGROUPS." ON ".SFFORUMS.".group_id = ".SFGROUPS.".group_id
- WHERE forum_slug='".$forumslug."';");
- if($asArray) return $wpdb->get_row($sql, ARRAY_A);
- return $wpdb->get_row($sql);
- }
-
- # ------------------------------------------------------------------
- # sf_get_forums_all()
- #
- # Returns complete recordset of forums
- # $id_only: limit recordset to forum_id and slug only
- # $asArray: return results as an array
- # Note: No permission checking is performed
- # ------------------------------------------------------------------
- function sf_get_forums_all($id_only=false, $asArray=false)
- {
- global $wpdb;
-
- if($id_only ? $FROM='forum_id, forum_slug' : $FROM='*');
- $sql=("SELECT ".$FROM." FROM ".SFFORUMS." ORDER BY forum_seq");
- if($asArray) return $wpdb->get_results($sql, ARRAY_A);
- return $wpdb->get_results($sql);
- }
-
- # ------------------------------------------------------------------
- # sf_forum_exists()
- #
- # Check the existence of a forum by id
- # $forumid: forum to check for
- # ------------------------------------------------------------------
- function sf_forum_exists($forumid)
- {
- global $wpdb;
-
- if(empty($forumid)) return false;
- if($wpdb->get_var(
- "SELECT forum_name
- FROM ".SFFORUMS."
- WHERE forum_id=".$forumid))
- {
- return true;
- }
- return false;
- }
-
- # ------------------------------------------------------------------
- # sf_forum_locked()
- #
- # Returns the lock status of a forum
- # $forumid: forum to check for
- # ------------------------------------------------------------------
- function sf_forum_locked($forumid)
- {
- global $wpdb;
-
- return $wpdb->get_var("SELECT forum_status FROM ".SFFORUMS." WHERE forum_id=".$forumid);
- }
-
- # ------------------------------------------------------------------
- # sf_get_forum_rss_url()
- #
- # Returns the RSS URL for a forum (custom or standard)
- # $forumid: forum to return
- # $forumslug: slug for the url
- # ------------------------------------------------------------------
- function sf_get_forum_rss_url($forumid, $forumslug)
- {
- global $wpdb, $sfglobals;
-
- if (empty($forumid)) return '';
-
- $url = $wpdb->get_var("SELECT forum_rss FROM ".SFFORUMS." WHERE forum_id=".$forumid);
- if (empty($url))
- {
- $rssopt = sf_get_option('sfrss');
- if ($rssopt['sfrssfeedkey'])
- $url = sf_build_qurl('forum='.$forumslug, 'xfeed=forum', 'feedkey='.$sfglobals['member']['feedkey']);
- else
- $url = sf_build_qurl('forum='.$forumslug, 'xfeed=forum');
- }
-
- return $url;
- }
-
- # ------------------------------------------------------------------
- # sf_get_topic_status_set()
- #
- # Returns the topic status set name for a forum
- # $forumid: forum to return
- # ------------------------------------------------------------------
- function sf_get_topic_status_set($forumid)
- {
- global $wpdb;
-
- if(empty($forumid)) return '';
- return $wpdb->get_var(
- "SELECT topic_status_set FROM ".SFFORUMS." WHERE forum_id=".$forumid);
- }
-
- function sf_get_topic_status_from_forum($forumid, $statusflag)
- {
- global $wpdb;
-
- if(!$forumid) return '';
-
- $flag='';
- $set=sf_get_topic_status_set($forumid);
- if($set != 0)
- {
- $flag=sf_get_topic_status_flag($set, $statusflag);
- }
- return $flag;
- }
-
- # ------------------------------------------------------------------
- # sf_find_user_in_topic()
- #
- # Searches a topics posts to see if user has ever posted in it for
- # the forums topic list icon
- # $topicid: topic to search
- # $userid: user to look for
- # %%FUTURE OPTIMISE%%
- # ------------------------------------------------------------------
- function sf_find_user_in_topic($topicid, $userid)
- {
- global $wpdb;
-
- if(!$topicid || !$userid) return '';
-
- return $wpdb->get_col(
- "SELECT user_id
- FROM ".SFPOSTS."
- WHERE topic_id=".$topicid."
- AND user_id=".$userid);
- }
-
- # ------------------------------------------------------------------
- # sf_get_forum_from_topic()
- #
- # returng the firum id when only the topic is known
- # $topicid: topic to search
- # ------------------------------------------------------------------
-
- function sf_get_forum_from_topic($topicid)
- {
- global $wpdb;
-
- if(!$topicid) return '';
-
- return $wpdb->get_var(
- "SELECT forum_id
- FROM ".SFTOPICS."
- WHERE topic_id=".$topicid);
- }
-
- # ------------------------------------------------------------------
- # sf_get_forum_name()
- #
- # Returns forum name when only the slug is known
- # $forumslug: forum to return
- # ------------------------------------------------------------------
- function sf_get_forum_name($forumslug)
- {
- global $wpdb;
-
- if(!$forumslug) return '';
-
- return $wpdb->get_var(
- "SELECT forum_name
- FROM ".SFFORUMS."
- WHERE forum_slug='".$forumslug."'");
- }
-
- # ------------------------------------------------------------------
- # sf_get_forum_name_from_id()
- #
- # Returns forum name when only the slug is known
- # $forumid: forum to return
- # ------------------------------------------------------------------
- function sf_get_forum_name_from_id($forumid)
- {
- global $wpdb;
-
- if(!$forumid) return '';
-
- return $wpdb->get_var(
- "SELECT forum_name
- FROM ".SFFORUMS."
- WHERE forum_id=".$forumid);
- }
-
- # ------------------------------------------------------------------
- # sf_get_forum_slug()
- #
- # Returns forum slug when only the id is known
- # $forumid: forum to return
- # ------------------------------------------------------------------
- function sf_get_forum_slug($forumid)
- {
- global $wpdb;
-
- if(!$forumid) return '';
-
- return $wpdb->get_var(
- "SELECT forum_slug
- FROM ".SFFORUMS."
- WHERE forum_id=".$forumid);
- }
-
- # ------------------------------------------------------------------
- # sf_get_forum_id()
- #
- # Returns forum id when only the slug is known
- # $forumslug: forum to return
- # ------------------------------------------------------------------
-
- function sf_get_forum_id($forumslug)
- {
- global $wpdb;
-
- if(!$forumslug) return '';
-
- return $wpdb->get_var(
- "SELECT forum_id
- FROM ".SFFORUMS."
- WHERE forum_slug='".$forumslug."'");
- }
-
- # ------------------------------------------------------------------
- # sf_get_topics_forum_id()
- #
- # Returns forum id when only the topic id is known
- # $topicid: forum to return from topic record
- # ------------------------------------------------------------------
- function sf_get_topics_forum_id($topicid)
- {
- global $wpdb;
-
- if(!$topicid) return '';
-
- return $wpdb->get_var(
- "SELECT forum_id
- FROM ".SFTOPICS."
- WHERE topic_id=".$topicid);
- }
-
- # ******************************************************************
- # TOPIC/POST VIEW AND GENERAL DB FUNCTIONS
- # ******************************************************************
-
- # ------------------------------------------------------------------
- # sf_get_combined_topics_and_posts()
- #
- # Returns a page of posts for specified topic
- # $topicid: topic and posts to load
- # ------------------------------------------------------------------
- function sf_get_combined_topics_and_posts($topicid)
- {
- global $wpdb, $sfvars, $sfglobals;
-
- if(!$topicid) return '';
-
- # sadly have to grab the topic row first because we need obverride sort order if set
- $topic = $wpdb->get_row(
- "SELECT topic_id, topic_slug, ".SFTOPICS.".forum_id, topic_name, ".SFTOPICS.".post_count, topic_subs, topic_watches, topic_status, post_ratings, use_tags, blog_post_id,
- forum_slug, forum_status, topic_status_set, topic_status_flag, user_id, use_tags
- FROM ".SFTOPICS."
- JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
- WHERE topic_id = ".$topicid.";", ARRAY_A);
-
- # quick permission check
- if(!sf_can_view_forum($topic['forum_id'])) return '';
-
- # grab the tags if enabled for this forum
- if ($topic['use_tags'])
- {
- $sql = "SELECT ".SFTAGS.".tag_name, tag_slug
- FROM ".SFTAGMETA."
- JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
- WHERE ".SFTAGMETA.".topic_id = ".$topicid;
- $topic['tags'] = $wpdb->get_results($sql);
- }
-
- # now for the posts
- $ORDER="ASC"; # default
- if($sfglobals['display']['posts']['sortdesc']) $ORDER="DESC"; # global override
-
- $ppaged=$sfglobals['display']['posts']['perpage'];
- if(!$ppaged) $ppaged=20;
-
- if($sfvars['page'] == 1 ? $startlimit = 0 : $startlimit = ((($sfvars['page']-1) * $ppaged)));
-
- $topic['topic_page'] = $sfvars['page'];
-
- $tpages = ($topic['post_count'] / $ppaged);
- if(!is_int($tpages))
- {
- $tpages = intval($topic['post_count'] / $ppaged) +1;
- }
- $topic['topic_total_pages'] = $tpages;
-
- $LIMIT = ' LIMIT '.$startlimit.', '.$ppaged;
-
- $records = $wpdb->get_results(
- "SELECT ".SFPOSTS.".post_id, post_content, ".sf_zone_datetime('post_date').", UNIX_TIMESTAMP(post_date) as udate, ".SFPOSTS.".user_id, guest_name, guest_email,
- post_status, post_pinned, post_index, post_edit,
- ".SFMEMBERS.".display_name, admin, posts, signature, avatar, pm,
- user_url, user_email, rating_id, vote_count, ratings_sum, ips, members
- FROM ".SFPOSTS."
- LEFT JOIN ".SFUSERS." ON ".SFPOSTS.".user_id = ".SFUSERS.".ID
- LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
- LEFT JOIN ".SFPOSTRATINGS." ON ".SFPOSTRATINGS.".post_id = ".SFPOSTS.".post_id
- WHERE topic_id = ".$topicid."
- ORDER BY post_pinned DESC, ".SFPOSTS.".post_id ".$ORDER.$LIMIT, ARRAY_A);
-
- $topic['posts'] = $records;
-
- return $topic;
- }
-
- # ------------------------------------------------------------------
- # sf_get_topic_record()
- #
- # Returns a single topic row
- # $topicid: topic_id of topic to return
- # Note: No permission checking is performed
- # ------------------------------------------------------------------
- function sf_get_topic_record($topicid)
- {
- global $wpdb, $session_topics;
-
- if(!$topicid) return '';
-
- # see if in the current session topics cache
- if($session_topics)
- {
- foreach($session_topics as $topic)
- {
- if($topic->topic_id == $topicid)
- {
- return $topic;
- }
- }
- }
- $sql=(
- "SELECT *
- FROM ".SFTOPICS."
- WHERE topic_id=".$topicid.";");
- $thistopic = $wpdb->get_row($sql);
- $session_topics[] = $thistopic;
- return $thistopic;
- }
-
- # ------------------------------------------------------------------
- # sf_get_topic_record_from_slug()
- #
- # Returns a single topic row
- # $topicslug: topic_slug of topic to return
- # Note: No permission checking is performed
- # ------------------------------------------------------------------
- function sf_get_topic_record_from_slug($topicslug)
- {
- global $wpdb, $session_topics;
-
- if(!$topicslug) return '';
-
- # see if in the current session topics cache
- if($session_topics)
- {
- foreach($session_topics as $topic)
- {
- if($topic->topic_slug == $topicslug)
- {
- return $topic;
- }
- }
- }
- $sql=(
- "SELECT *
- FROM ".SFTOPICS."
- WHERE topic_slug='".$topicslug."';");
- $thistopic = $wpdb->get_row($sql);
- $session_topics[] = $thistopic;
- return $thistopic;
- }
-
- # ------------------------------------------------------------------
- # sf_get_topic_record_from_blogpostid()
- #
- # Returns a single topic row
- # $blogpostid: blog post id to see if any linked topics
- # Note: No permission checking…
Large files files are truncated, but you can click here to view the full file