PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/simple-forum/library/sf-database.php

https://bitbucket.org/crypticrod/sr_wp_code
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

  1. <?php
  2. /*
  3. Simple:Press
  4. Main database routines
  5. $LastChangedDate: 2011-04-26 05:52:27 -0700 (Tue, 26 Apr 2011) $
  6. $Rev: 5981 $
  7. */
  8. if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
  9. die('Access Denied');
  10. }
  11. # ******************************************************************
  12. # GROUP/FORUM VIEW AND GENERAL DB FUNCTIONS
  13. # ******************************************************************
  14. # ------------------------------------------------------------------
  15. # sf_get_combined_groups_and_forums($groupid)
  16. #
  17. # Grabs all groups and forums. Note that the group data is repeated.
  18. # Used to populate 'Select Forum Quicklinks' and Front Main page
  19. # of forum (Group/Forum View)
  20. # $groupid: Optional id to display just a single group
  21. # ------------------------------------------------------------------
  22. function sf_get_combined_groups_and_forums($groupid = null)
  23. {
  24. global $wpdb, $session_groups, $current_user;
  25. If(is_null($groupid) ? $where='' : $where = " WHERE ".SFGROUPS.".group_id=".$groupid." ");
  26. # retrieve group and forum records
  27. $records = $wpdb->get_results(
  28. "SELECT ".SFGROUPS.".group_id, group_name, group_desc, group_rss, group_icon, group_message,
  29. forum_id, forum_name, forum_slug, forum_desc, forum_status, forum_icon, forum_rss_private, post_id, topic_count, parent, children
  30. FROM ".SFGROUPS."
  31. JOIN ".SFFORUMS." ON ".SFGROUPS.".group_id = ".SFFORUMS.".group_id
  32. ".$where."
  33. ORDER BY group_seq, forum_seq;");
  34. # rebuild into an array grabbing permissions on the way
  35. $groups=array();
  36. if($records)
  37. {
  38. # Set initially to Access Denied in case current user can view no forums
  39. $groups[0]['group_id'] = "Access Denied";
  40. $gindex=-1;
  41. $findex=0;
  42. foreach($records as $record)
  43. {
  44. $groupid=$record->group_id;
  45. $forumid=$record->forum_id;
  46. 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))
  47. {
  48. if($gindex == -1 || $groups[$gindex]['group_id'] != $groupid)
  49. {
  50. $gindex++;
  51. $findex=-1;
  52. $groups[$gindex]['group_id']=$record->group_id;
  53. $groups[$gindex]['group_name']=$record->group_name;
  54. $groups[$gindex]['group_desc']=$record->group_desc;
  55. $groups[$gindex]['group_rss']=$record->group_rss;
  56. $groups[$gindex]['group_icon']=$record->group_icon;
  57. $groups[$gindex]['group_message']=$record->group_message;
  58. }
  59. if(isset($record->forum_id))
  60. {
  61. $groups[$gindex]['forums'][$findex]['forum_id']=$record->forum_id;
  62. $groups[$gindex]['forums'][$findex]['forum_name']=$record->forum_name;
  63. $groups[$gindex]['forums'][$findex]['forum_slug']=$record->forum_slug;
  64. $groups[$gindex]['forums'][$findex]['forum_desc']=$record->forum_desc;
  65. $groups[$gindex]['forums'][$findex]['forum_status']=$record->forum_status;
  66. $groups[$gindex]['forums'][$findex]['forum_icon']=$record->forum_icon;
  67. $groups[$gindex]['forums'][$findex]['forum_rss_private']=$record->forum_rss_private;
  68. $groups[$gindex]['forums'][$findex]['post_id']=$record->post_id;
  69. $groups[$gindex]['forums'][$findex]['topic_count']=$record->topic_count;
  70. $groups[$gindex]['forums'][$findex]['parent']=$record->parent;
  71. $groups[$gindex]['forums'][$findex]['children']=$record->children;
  72. $findex++;
  73. }
  74. }
  75. }
  76. } else {
  77. $records = sf_get_groups_all(false, false);
  78. if($records)
  79. {
  80. foreach($records as $record)
  81. {
  82. $groups[$gindex]['group_id']=$record->group_id;
  83. $groups[$gindex]['group_name']=$record->group_name;
  84. $groups[$gindex]['group_desc']=$record->group_desc;
  85. $groups[$gindex]['group_rss']=$record->group_rss;
  86. $groups[$gindex]['group_icon']=$record->group_icon;
  87. $groups[$gindex]['group_message']=$record->group_message;
  88. $gindex++;
  89. }
  90. }
  91. }
  92. $session_groups = $groups;
  93. return $groups;
  94. }
  95. # ------------------------------------------------------------------
  96. # sf_get_combined_groups_and_forums_bloglink()
  97. #
  98. # Grabs all groups and forums. Soecial cut down version for
  99. # populating the blog link add post drop down
  100. # ------------------------------------------------------------------
  101. function sf_get_combined_groups_and_forums_bloglink()
  102. {
  103. global $wpdb, $current_user;
  104. # retrieve group and forum records
  105. $records = $wpdb->get_results(
  106. "SELECT ".SFGROUPS.".group_id, group_name,
  107. forum_id, forum_name
  108. FROM ".SFGROUPS."
  109. JOIN ".SFFORUMS." ON ".SFGROUPS.".group_id = ".SFFORUMS.".group_id
  110. ".$where."
  111. ORDER BY group_seq, forum_seq;");
  112. # rebuild into an array grabbing permissions on the way
  113. $groups=array();
  114. $gindex=-1;
  115. $findex=0;
  116. if($records)
  117. {
  118. foreach($records as $record)
  119. {
  120. $groupid=$record->group_id;
  121. $forumid=$record->forum_id;
  122. if (sf_user_can($current_user->ID, 'Can create linked topics', $forumid) && sf_user_can($current_user->ID, 'Can start new topics', $forumid))
  123. {
  124. if($gindex == -1 || $groups[$gindex]['group_id'] != $groupid)
  125. {
  126. $gindex++;
  127. $findex=0;
  128. $groups[$gindex]['group_id']=$record->group_id;
  129. $groups[$gindex]['group_name']=$record->group_name;
  130. }
  131. if(isset($record->forum_id))
  132. {
  133. $groups[$gindex]['forums'][$findex]['forum_id']=$record->forum_id;
  134. $groups[$gindex]['forums'][$findex]['forum_name']=$record->forum_name;
  135. $findex++;
  136. }
  137. }
  138. }
  139. }
  140. return $groups;
  141. }
  142. function sf_get_combined_forum_stats($posts)
  143. {
  144. global $wpdb, $current_user;
  145. $clause='';
  146. if($posts)
  147. {
  148. $pcount = count($posts);
  149. $done = 0;
  150. foreach ($posts as $post)
  151. {
  152. $clause.= "(".SFPOSTS.".post_id=".$post.")";
  153. $done++;
  154. if($done < $pcount) $clause.= " OR ";
  155. }
  156. } else {
  157. $record=array();
  158. $record['topic_count'] = '0';
  159. $record['post_count'] = '0';
  160. $record['udate'] = '';
  161. return $record;
  162. }
  163. $records = $wpdb->get_results(
  164. "SELECT ".SFPOSTS.".post_id, ".SFPOSTS.".topic_id, topic_name, ".SFPOSTS.".forum_id, ".sf_zone_datetime('post_date').",
  165. UNIX_TIMESTAMP(post_date) as udate, guest_name, ".SFPOSTS.".user_id, post_content, post_status, ".SFMEMBERS.".display_name, post_index,
  166. ".SFFORUMS.".topic_count, ".SFFORUMS.".post_count, topic_slug
  167. FROM ".SFPOSTS."
  168. LEFT JOIN ".SFFORUMS." ON ".SFPOSTS.".forum_id = ".SFFORUMS.".forum_id
  169. LEFT JOIN ".SFTOPICS." ON ".SFPOSTS.".topic_id = ".SFTOPICS.".topic_id
  170. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  171. WHERE ".$clause.";", ARRAY_A);
  172. if($records)
  173. {
  174. $sfdisplay=array();
  175. $sfdisplay=sf_get_option('sfdisplay');
  176. $forums = array();
  177. foreach($records as $record)
  178. {
  179. $forums[$record['forum_id']] = $record;
  180. # do we need a post tooltip on the topic link?
  181. 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))
  182. {
  183. $forums[$record['forum_id']]['post_tip']=sf_filter_tooltip_display($record['post_content'], $record['post_status']);
  184. $forums[$record['forum_id']]['post_content']='';
  185. }
  186. }
  187. }
  188. return $forums;
  189. }
  190. # ------------------------------------------------------------------
  191. # sf_get_group_record()
  192. #
  193. # Returns a single group row
  194. # $groupid: group_id of group to return
  195. # $asArray: return as an array if true
  196. # Note: No permission checking is performed
  197. # ------------------------------------------------------------------
  198. function sf_get_group_record($groupid, $asArray=false)
  199. {
  200. global $wpdb;
  201. if(!$groupid) return '';
  202. $sql=(
  203. "SELECT *
  204. FROM ".SFGROUPS."
  205. WHERE group_id=".$groupid.";");
  206. if($asArray) return $wpdb->get_row($sql, ARRAY_A);
  207. return $wpdb->get_row($sql);
  208. }
  209. # ------------------------------------------------------------------
  210. # sf_get_groups_all()
  211. #
  212. # Return ALL group records - no permission checking
  213. # $id_only: Optionsal = return just ids
  214. # $asArray: Optional - return as array
  215. # ------------------------------------------------------------------
  216. function sf_get_groups_all($id_only=false, $asArray=false)
  217. {
  218. global $wpdb;
  219. if($id_only ? $FROM='group_id' : $FROM='*');
  220. $sql=("SELECT ".$FROM." FROM ".SFGROUPS." ORDER BY group_seq");
  221. if($asArray) return $wpdb->get_results($sql, ARRAY_A);
  222. return $wpdb->get_results($sql);
  223. }
  224. # ------------------------------------------------------------------
  225. # sf_group_exists()
  226. #
  227. # Check the existence of a group by id
  228. # $groupid: group to check for
  229. # ------------------------------------------------------------------
  230. function sf_group_exists($groupid)
  231. {
  232. global $wpdb;
  233. if(empty($groupid)) return false;
  234. if($wpdb->get_var(
  235. "SELECT group_name
  236. FROM ".SFGROUPS."
  237. WHERE group_id=".$groupid))
  238. {
  239. return true;
  240. }
  241. return false;
  242. }
  243. # ------------------------------------------------------------------
  244. # sf_get_group_rss_url()
  245. #
  246. # Returns the RSS feed URL for a Group (custom or standard)
  247. # $groupid: group to return
  248. # ------------------------------------------------------------------
  249. function sf_get_group_rss_url($groupid)
  250. {
  251. global $wpdb, $sfglobals;
  252. if (empty($groupid)) return '';
  253. $url = $wpdb->get_var("SELECT group_rss FROM ".SFGROUPS." WHERE group_id=".$groupid);
  254. if (empty($url))
  255. {
  256. $rssopt = sf_get_option('sfrss');
  257. if ($rssopt['sfrssfeedkey'] && isset($sfglobals['member']['feedkey']))
  258. $url = sf_build_qurl('group='.$groupid, 'xfeed=group', 'feedkey='.$sfglobals['member']['feedkey']);
  259. else
  260. $url = sf_build_qurl('group='.$groupid, 'xfeed=group');
  261. }
  262. return $url;
  263. }
  264. # ------------------------------------------------------------------
  265. # sf_get_group_name_from_forum()
  266. #
  267. # Returns the Group Name when only the forum id is known
  268. # $forumid: forum to lookup for group name
  269. # ------------------------------------------------------------------
  270. function sf_get_group_name_from_forum($forumid)
  271. {
  272. global $wpdb;
  273. if(!$forumid) return '';
  274. return $wpdb->get_var(
  275. "SELECT ".SFGROUPS.".group_name
  276. FROM ".SFGROUPS."
  277. JOIN ".SFFORUMS." ON ".SFFORUMS.".group_id = ".SFGROUPS.".group_id
  278. WHERE ".SFFORUMS.".forum_id=".$forumid);
  279. }
  280. # ******************************************************************
  281. # FORUM/TOPIC VIEW AND GENERAL DB FUNCTIONS
  282. # ******************************************************************
  283. # ------------------------------------------------------------------
  284. # sf_get_combined_forums_and_topics($forumid)
  285. #
  286. # Grabs all forums and their topics. Note that the forum data is
  287. # repeated. Used to populate Topic Listing page of topics
  288. # (Forum/Topics View)
  289. # $forumid: forum id to display
  290. # $currentpage: index to paging
  291. # ------------------------------------------------------------------
  292. function sf_get_combined_forums_and_topics($forumid, $currentpage)
  293. {
  294. global $wpdb, $sfvars, $sfglobals, $current_user;
  295. if(!$forumid) return '';
  296. $sfvars['searchresults'] = 0;
  297. # rebuild into an array
  298. $forums=array();
  299. # Set initially to Access Denied in case current user can view no forums
  300. $forums[0]['forum_id']="Access Denied";
  301. # quick permission check
  302. if(!sf_can_view_forum($forumid) && !sf_user_can($current_user->ID, 'Can view forum and topic lists only', $forumid)) return $forums;
  303. # some setup vars
  304. $startlimit = 0;
  305. # get post tooltip state
  306. $sfdisplay = array();
  307. $sfdisplay = sf_get_option('sfdisplay');
  308. # how many topics per page?
  309. $tpaged = $sfglobals['display']['topics']['perpage'];
  310. if(!$tpaged) $tpaged=20;
  311. # setup where we are in the topic list (paging)
  312. if($sfvars['searchpage'] == 0)
  313. {
  314. if($currentpage != 1)
  315. {
  316. $startlimit = ((($currentpage-1) * $tpaged));
  317. }
  318. } else {
  319. if($sfvars['searchpage'] == 1)
  320. {
  321. $currentpage = 1;
  322. } else {
  323. $startlimit = ((($sfvars['searchpage']-1) * $tpaged));
  324. }
  325. }
  326. $LIMIT = " LIMIT ".$startlimit.', '.$tpaged;
  327. if($sfvars['searchpage'] == 0)
  328. {
  329. $topicsort = $sfglobals['display']['topics']['sortnewtop'];
  330. if ($topicsort)
  331. {
  332. $ORDER = " ORDER BY topic_pinned DESC, ".SFTOPICS.".post_id DESC";
  333. } else {
  334. $ORDER = " ORDER BY topic_pinned DESC, ".SFTOPICS.".post_id ASC";
  335. }
  336. } else {
  337. $ORDER = " ORDER BY ".SFTOPICS.".post_id DESC";
  338. }
  339. if($sfvars['searchpage'] == 0)
  340. {
  341. # standar forum view
  342. $SELECT = "SELECT ";
  343. $MATCH = "";
  344. $ANDWHERE = "";
  345. } else {
  346. $searchvalue=urldecode($sfvars['searchvalue']);
  347. if(empty($searchvalue))
  348. {
  349. return '';
  350. }
  351. # what sort of search is it?
  352. if($sfvars['searchtype'] == 6) {
  353. # topic status search
  354. $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
  355. $MATCH = "";
  356. $ANDWHERE = " AND topic_status_flag=".$sfvars['searchvalue']." ";
  357. } elseif($sfvars['searchtype'] == 8) {
  358. # members 'posted in' sepcified forum search
  359. $userid = $sfvars['searchvalue'];
  360. $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
  361. $MATCH = "";
  362. $ANDWHERE = " AND ".SFPOSTS.".user_id=".$userid." ";
  363. } elseif($sfvars['searchtype'] == 9) {
  364. # members 'started' in sepcified forum search
  365. $userid = $sfvars['searchvalue'];
  366. $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
  367. $MATCH = "";
  368. $ANDWHERE = " AND ".SFPOSTS.".user_id=".$userid." ";
  369. } elseif($sfvars['searchinclude'] == 4) {
  370. # it's a tag search
  371. $searchtag = sf_create_slug($searchvalue, 'tag');
  372. $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
  373. $MATCH = SFTOPICS.".topic_id IN (SELECT topic_id FROM ".SFTAGMETA." JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  374. WHERE tag_slug = '".$searchtag."' AND forum_id=".$forumid.") AND ";
  375. $ANDWHERE = "";
  376. } else {
  377. # general keyword search
  378. $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
  379. $searchterm = sf_construct_search_term($searchvalue, $sfvars['searchtype']);
  380. switch($sfvars['searchinclude'])
  381. {
  382. case 1:
  383. $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 ";
  384. break;
  385. case 2:
  386. $MATCH = "MATCH(".SFPOSTS.".post_content) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE) AND ";
  387. break;
  388. case 3:
  389. $MATCH = "MATCH(".SFTOPICS.".topic_name) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE) AND ";
  390. break;
  391. }
  392. $ANDWHERE = "";
  393. }
  394. }
  395. # retrieve forum and topic records - is a type 1, 2, 3 or 8 then we need a separate query (for now)
  396. # NO post_content or post_index = 1
  397. if (!empty($sfvars['searchtype']) && ($sfvars['searchtype'] == 1 || $sfvars['searchtype'] == 2 || $sfvars['searchtype'] == 3 || $sfvars['searchtype'] == 8))
  398. {
  399. $records = sf_query_results(
  400. $SELECT.SFFORUMS.".forum_id, forum_slug, forum_name, forum_status, group_id, topic_count, forum_icon, forum_desc, topic_status_set,
  401. parent, children, forum_message,
  402. ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
  403. topic_status, topic_pinned, topic_opened, topic_subs, topic_watches, topic_status_flag,
  404. post_ratings, use_tags, blog_post_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count, ".SFTOPICS.".user_id, post_status
  405. FROM ".SFFORUMS."
  406. JOIN ".SFTOPICS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  407. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  408. WHERE ".$MATCH.SFFORUMS.".forum_id=".$forumid.$ANDWHERE.$ORDER.$LIMIT.";");
  409. } else {
  410. $records = sf_query_results(
  411. $SELECT.SFFORUMS.".forum_id, forum_slug, forum_name, forum_status, group_id, topic_count, forum_icon, forum_desc, topic_status_set,
  412. parent, children, forum_message,
  413. ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
  414. topic_status, topic_pinned, topic_opened, topic_subs, topic_watches, topic_status_flag,
  415. post_ratings, use_tags, blog_post_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count, ".SFTOPICS.".user_id, post_content, post_status
  416. FROM ".SFFORUMS."
  417. JOIN ".SFTOPICS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  418. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  419. WHERE ".$MATCH.SFFORUMS.".forum_id=".$forumid.$ANDWHERE." AND ".SFPOSTS.".post_index=1 ".$ORDER.$LIMIT.";");
  420. }
  421. if (!empty($sfvars['searchpage']) && $records)
  422. {
  423. $sfvars['searchresults'] = $wpdb->get_var("SELECT FOUND_ROWS()");
  424. }
  425. $findex=-1;
  426. $tindex=0;
  427. if($records)
  428. {
  429. $topiclist = '';
  430. foreach($records as $record)
  431. {
  432. $forumid=$record->forum_id;
  433. if($findex == -1 || $forums[$findex]['forum_id'] != $forumid)
  434. {
  435. $findex++;
  436. $tindex=0;
  437. $forums[$findex]['forum_id']=$record->forum_id;
  438. $forums[$findex]['forum_slug']=$record->forum_slug;
  439. $forums[$findex]['forum_name']=$record->forum_name;
  440. $forums[$findex]['forum_desc']=$record->forum_desc;
  441. $forums[$findex]['forum_status']=$record->forum_status;
  442. $forums[$findex]['group_id']=$record->group_id;
  443. $forums[$findex]['topic_count']=$record->topic_count;
  444. $forums[$findex]['forum_icon']=$record->forum_icon;
  445. $forums[$findex]['topic_status_set']=$record->topic_status_set;
  446. $forums[$findex]['post_ratings']=$record->post_ratings;
  447. $forums[$findex]['use_tags']=$record->use_tags;
  448. $forums[$findex]['parent']=$record->parent;
  449. $forums[$findex]['children']=$record->children;
  450. $forums[$findex]['forum_message']=$record->forum_message;
  451. }
  452. $forums[$findex]['topics'][$tindex]['topic_id']=$record->topic_id;
  453. $forums[$findex]['topics'][$tindex]['topic_slug']=$record->topic_slug;
  454. $forums[$findex]['topics'][$tindex]['topic_name']=$record->topic_name;
  455. $forums[$findex]['topics'][$tindex]['topic_date']=$record->topic_date;
  456. $forums[$findex]['topics'][$tindex]['topic_status']=$record->topic_status;
  457. $forums[$findex]['topics'][$tindex]['topic_pinned']=$record->topic_pinned;
  458. $forums[$findex]['topics'][$tindex]['topic_opened']=$record->topic_opened;
  459. $forums[$findex]['topics'][$tindex]['topic_subs']=$record->topic_subs;
  460. $forums[$findex]['topics'][$tindex]['topic_watches']=$record->topic_watches;
  461. $forums[$findex]['topics'][$tindex]['topic_status_flag']=$record->topic_status_flag;
  462. $forums[$findex]['topics'][$tindex]['post_ratings']=$record->post_ratings;
  463. $forums[$findex]['topics'][$tindex]['use_tags']=$record->use_tags;
  464. $forums[$findex]['topics'][$tindex]['blog_post_id']=$record->blog_post_id;
  465. $forums[$findex]['topics'][$tindex]['post_id']=$record->post_id;
  466. $forums[$findex]['topics'][$tindex]['post_count']=$record->post_count;
  467. $forums[$findex]['topics'][$tindex]['user_id']=$record->user_id;
  468. # do we need a post tooltip on the topic link?
  469. if ($sfdisplay['topics']['posttip'] && (!empty($sfvars['searchtype']) && $sfvars['searchtype'] != 8) && $current_user->sfaccess)
  470. {
  471. $forums[$findex]['topics'][$tindex]['post_tip'] = sf_filter_tooltip_display($record->post_content, $record->post_status);
  472. } else {
  473. $forums[$findex]['topics'][$tindex]['post_tip']="";
  474. }
  475. # save topic in list for getting tags later
  476. $topiclist[] = $record->topic_id;
  477. $tindex++;
  478. }
  479. # Now get tags in one query for these topics
  480. $topiclist = implode(',', $topiclist);
  481. $sql = "SELECT tag_name, tag_slug, topic_id
  482. FROM ".SFTAGS."
  483. JOIN ".SFTAGMETA." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  484. WHERE topic_id IN (".$topiclist.")
  485. ORDER BY topic_id";
  486. $tags = $wpdb->get_results($sql);
  487. # Now sort the tags into the monsterous forum array
  488. if ($tags)
  489. {
  490. foreach ($forums as $findex => $forum)
  491. {
  492. foreach ($forum['topics'] as $tindex => $topic)
  493. {
  494. if ($topic['use_tags'])
  495. {
  496. $topictags = '';
  497. foreach ($tags as $tag)
  498. {
  499. if ($topic['topic_id'] == $tag->topic_id)
  500. {
  501. # build tag list for this forum/topic
  502. $topictags[] = $tag;
  503. }
  504. }
  505. # save the tags into the master array
  506. $forums[$findex]['topics'][$tindex]['tags'] = $topictags;
  507. }
  508. }
  509. }
  510. }
  511. } else {
  512. $record = sf_get_forum_record($forumid);
  513. if($record)
  514. {
  515. $forums[0]['forum_id']=$record->forum_id;
  516. $forums[0]['forum_slug']=$record->forum_slug;
  517. $forums[0]['forum_name']=$record->forum_name;
  518. $forums[0]['forum_desc']=$record->forum_desc;
  519. $forums[0]['forum_status']=$record->forum_status;
  520. $forums[0]['group_id']=$record->group_id;
  521. $forums[0]['topic_count']=$record->topic_count;
  522. $forums[0]['forum_icon']=$record->forum_icon;
  523. $forums[0]['topic_status_set']=$record->topic_status_set;
  524. $forums[0]['post_ratings']=$record->post_ratings;
  525. $forums[0]['use_tags']=$record->use_tags;
  526. $forums[0]['parent']=$record->parent;
  527. $forums[0]['children']=$record->children;
  528. $forums[0]['forum_message']=$record->forum_message;
  529. }
  530. }
  531. return $forums;
  532. }
  533. function sf_get_subforums($sublist)
  534. {
  535. global $wpdb;
  536. if (!$sublist) return '';
  537. $subforums = unserialize($sublist);
  538. $forumlist = array();
  539. foreach ($subforums as $forumid)
  540. {
  541. if (sf_can_view_forum($forumid)) $forumlist[] = $forumid;
  542. }
  543. if (empty($forumlist)) return '';
  544. $where = " WHERE forum_id IN (" . implode(",", $forumlist) . ")";
  545. $subforums = $wpdb->get_results("SELECT * FROM ".SFFORUMS.$where." ORDER BY forum_seq", ARRAY_A);
  546. return $subforums;
  547. }
  548. # ------------------------------------------------------------------
  549. # sf_get_combined_topic_stats()
  550. #
  551. # Returns the first and last post data for a topic
  552. # $postid: post id from new posts list
  553. # $user_id for checking of user has posted in topic -
  554. # ------------------------------------------------------------------
  555. function sf_get_combined_topic_stats($posts, $userid=0)
  556. {
  557. global $wpdb, $current_user;
  558. $clause='';
  559. if($posts)
  560. {
  561. $pcount = count($posts);
  562. $done = 0;
  563. foreach ($posts as $topic=>$postindex)
  564. {
  565. $clause.= "(topic_id = ".$topic." AND post_index = 1 OR topic_id = ".$topic." AND post_index=".$postindex.")";
  566. $done++;
  567. if($done < $pcount) $clause.= " OR ";
  568. }
  569. } else {
  570. return;
  571. }
  572. $records = $wpdb->get_results(
  573. "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
  574. FROM ".SFPOSTS."
  575. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  576. WHERE ".$clause."
  577. ORDER BY post_id ASC;", ARRAY_A);
  578. # If forum view are we looking to see if user has posted in ant of the topics?
  579. if($records && $userid)
  580. {
  581. $clause='';
  582. $pcount = count($posts);
  583. $done = 0;
  584. foreach ($posts as $topic=>$postindex)
  585. {
  586. $clause.= "(topic_id = ".$topic." AND user_id = ".$userid.")";
  587. $done++;
  588. if($done < $pcount) $clause.= " OR ";
  589. }
  590. $userposted = $wpdb->get_results(
  591. "SELECT DISTINCT topic_id, user_id
  592. FROM ".SFPOSTS."
  593. WHERE ".$clause.";");
  594. } else {
  595. $userposted = '';
  596. }
  597. # Now to format and combine results
  598. if ($records)
  599. {
  600. $sfdisplay=array();
  601. $sfdisplay=sf_get_option('sfdisplay');
  602. $topics = array();
  603. foreach ($records as $record)
  604. {
  605. $topics[$record['topic_id']][$record['post_index']] = $record;
  606. # do we need a post tooltip on the topic link?
  607. if ($sfdisplay['topics']['posttip'] && $current_user->sfaccess)
  608. {
  609. $topics[$record['topic_id']][$record['post_index']]['post_tip']=sf_filter_tooltip_display($record['post_content'], $record['post_status']);
  610. $topics[$record['topic_id']][$record['post_index']]['post_content']='';
  611. }
  612. }
  613. foreach ($records as $record)
  614. {
  615. $topics[$record['topic_id']]['thisuser'] = false;
  616. if ($userposted)
  617. {
  618. foreach ($userposted as $user)
  619. {
  620. if ($user->topic_id == $record['topic_id'])
  621. {
  622. $topics[$record['topic_id']]['thisuser'] = true;
  623. break;
  624. }
  625. }
  626. }
  627. }
  628. return $topics;
  629. } else {
  630. return '';
  631. }
  632. }
  633. # ******************************************************************
  634. # FORUM/TOPIC VIEW AND GENERAL DB FUNCTIONS
  635. # ******************************************************************
  636. # ------------------------------------------------------------------
  637. # sf_get_watched_topics($currentpagge)
  638. #
  639. # Grabs all watched topics
  640. # $currentpage: index to paging
  641. # ------------------------------------------------------------------
  642. function sf_get_watched_topics($currentpage=0)
  643. {
  644. global $wpdb, $current_user, $sfglobals;
  645. # quick permission check
  646. if (!$current_user->sfwatch) return '';
  647. $limit='';
  648. # get watched topics
  649. $list = $sfglobals['member']['watches'];
  650. if (empty($list)) return '';
  651. # create where clause of watched topics
  652. $where = " WHERE ".SFTOPICS.".topic_id IN (" . implode(",", $list) . ")";
  653. # retrieve watched topic records
  654. $query = "SELECT ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
  655. topic_status, topic_pinned, topic_opened, topic_watches, topic_status_flag,
  656. blog_post_id, ".SFTOPICS.".forum_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  657. forum_slug, forum_name, topic_status_set, post_ratings, group_id, post_content, post_status
  658. FROM ".SFTOPICS."
  659. JOIN ".SFFORUMS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  660. JOIN ".SFPOSTS." ON ".SFTOPICS.".post_id = ".SFPOSTS.".post_id ".
  661. $where." ORDER BY topic_date DESC ".$limit;
  662. $records = $wpdb->get_results($query, ARRAY_A);
  663. if($records)
  664. {
  665. $sfdisplay=array();
  666. $sfdisplay=sf_get_option('sfdisplay');
  667. # do we need a post tooltip on the topic link?
  668. if($sfdisplay['topics']['posttip'])
  669. {
  670. for($x=0; $x<count($records); $x++)
  671. {
  672. if ($current_user->sfaccess)
  673. {
  674. $records[$x]['post_tip']=sf_filter_tooltip_display($records[$x]['post_content'], $records[$x]['post_status']);
  675. $records[$x]['post_content']='';
  676. }
  677. }
  678. }
  679. }
  680. $watched['records'] = $records;
  681. $watched['count'] = count($list);
  682. return $watched;
  683. }
  684. # ------------------------------------------------------------------
  685. # sf_get_subscribed_topics($currentpagge)
  686. #
  687. # Grabs all subscribed topics
  688. # $currentpage: index to paging
  689. # ------------------------------------------------------------------
  690. function sf_get_subscribed_topics($currentpage=0)
  691. {
  692. global $wpdb, $current_user, $sfglobals;
  693. $limit = '';
  694. # quick permission check
  695. if (!$current_user->sfsubscriptions) return '';
  696. # get subscribed topics
  697. $list = $sfglobals['member']['subscribe'];
  698. if (empty($list)) return '';
  699. # create where clause of subscribed topics
  700. $where = " WHERE ".SFTOPICS.".topic_id IN (" . implode(",", $list) . ")";
  701. # retrieve watched topic records
  702. $query = "SELECT ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
  703. topic_status, topic_pinned, topic_opened, topic_subs, topic_status_flag,
  704. blog_post_id, ".SFTOPICS.".forum_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  705. forum_slug, forum_name, topic_status_set, post_ratings, group_id, post_content, post_status
  706. FROM ".SFTOPICS."
  707. JOIN ".SFFORUMS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  708. JOIN ".SFPOSTS." ON ".SFTOPICS.".post_id = ".SFPOSTS.".post_id ".
  709. $where." ORDER BY topic_date DESC ".$limit;
  710. $records = $wpdb->get_results($query, ARRAY_A);
  711. if($records)
  712. {
  713. $sfdisplay=array();
  714. $sfdisplay=sf_get_option('sfdisplay');
  715. # do we need a post tooltip on the topic link?
  716. if ($sfdisplay['topics']['posttip'])
  717. {
  718. for($x=0; $x<count($records); $x++)
  719. {
  720. if ($current_user->sfaccess)
  721. {
  722. $records[$x]['post_tip']=sf_filter_tooltip_display($records[$x]['post_content'], $records[$x]['post_status']);
  723. $records[$x]['post_content']='';
  724. }
  725. }
  726. }
  727. }
  728. $subscribed['records'] = $records;
  729. $subscribed['count'] = count($list);
  730. return $subscribed;
  731. }
  732. # ------------------------------------------------------------------
  733. # sf_get_related_topics($tags)
  734. #
  735. # Grabs related topics based on tags
  736. # ------------------------------------------------------------------
  737. function sf_get_related_topics($tags)
  738. {
  739. global $wpdb;
  740. if(!$tags) return '';
  741. # build list of tags for the topic id
  742. $taglist = '';
  743. foreach ($tags as $tag)
  744. {
  745. if ($taglist == '')
  746. {
  747. $taglist = "('".$tag->tag_slug."'";
  748. } else {
  749. $taglist.= ",'".$tag->tag_slug."'";
  750. }
  751. }
  752. $taglist.= ")";
  753. # now grab the results
  754. $LIMIT = ' LIMIT 10';
  755. $ORDER = ' ORDER BY topic_id DESC';
  756. $WHERE = SFTOPICS.".topic_id IN (SELECT topic_id FROM ".SFTAGMETA." JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  757. WHERE tag_slug IN ".$taglist.")";
  758. $topics = $wpdb->get_results(
  759. "SELECT SQL_CALC_FOUND_ROWS DISTINCT
  760. ".SFTOPICS.".topic_id, topic_name, topic_slug, ".SFTOPICS.".forum_id, forum_name, forum_slug,
  761. ".sf_zone_datetime('topic_date').", topic_status, topic_pinned, topic_opened,
  762. topic_watches, topic_status_flag, blog_post_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  763. topic_status_set, post_ratings, group_id
  764. FROM ".SFTOPICS."
  765. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  766. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  767. WHERE ".$WHERE.$ORDER.$LIMIT.";", ARRAY_A);
  768. return $topics;
  769. }
  770. # ------------------------------------------------------------------
  771. # sf_get_memberlists()
  772. #
  773. # Builds viewable member lists for current user
  774. # ------------------------------------------------------------------
  775. function sf_get_memberlists($currentpage, $search)
  776. {
  777. global $wpdb, $current_user, $sfglobals;
  778. $data = '';
  779. $data->records = '';
  780. $data->count = 0;
  781. $sfmemberopts = sf_get_option('sfmemberopts');
  782. if ($current_user->forumadmin || ($sfmemberopts['sfshowmemberlist'] && $current_user->sfmemberlist))
  783. {
  784. # are we limiting member lists to memberships?
  785. $where = ' WHERE posts > -2';
  786. $sfmemberopts = sf_get_option('sfmemberopts');
  787. if ($sfmemberopts['sflimitmemberlist'] && !$current_user->forumadmin)
  788. {
  789. # get usergroups user has membership in
  790. $ugs = sf_get_user_memberships($current_user->ID);
  791. #if no usergroup memberships return empty list
  792. if ($ugs)
  793. {
  794. $ug_ids='';
  795. foreach ($ugs as $ug)
  796. {
  797. $ug_ids[] = $ug['usergroup_id'];
  798. }
  799. # if no visible usergroups, dont return any search results
  800. if (empty($ug_ids)) return $data;
  801. } else {
  802. return $data;
  803. }
  804. # create where clause based on user memberships
  805. $where.= " AND (".SFMEMBERSHIPS.".usergroup_id IN (" . implode(",", $ug_ids) . ") OR ".SFMEMBERSHIPS.".usergroup_id IS NULL)";
  806. }
  807. if ($search != '')
  808. {
  809. $where.= " AND ".SFMEMBERS.'.display_name LIKE "'.$search.'%"';
  810. }
  811. # how many members per page?
  812. $startlimit = 0;
  813. $tpaged = $sfglobals['display']['topics']['perpage'];
  814. if(!$tpaged) $tpaged=20;
  815. if ($currentpage != 1)
  816. {
  817. $startlimit = ((($currentpage-1) * $tpaged));
  818. }
  819. $limit = " LIMIT ".$startlimit.', '.$tpaged;
  820. # retrieve members list records
  821. $query = "SELECT SQL_CALC_FOUND_ROWS DISTINCT IFNULL(".SFMEMBERSHIPS.".usergroup_id, '0') as usergroup_id, ".SFMEMBERS.".user_id,
  822. ".SFMEMBERS.".display_name, user_email, posts, lastvisit, IFNULL(usergroup_name, 'Admin') as usergroup_name,
  823. IFNULL(usergroup_desc, 'Forum Administrator') as usergroup_desc
  824. FROM ".SFUSERS."
  825. LEFT JOIN ".SFMEMBERS." ON ".SFUSERS.".ID = ".SFMEMBERS.".user_id
  826. LEFT JOIN ".SFMEMBERSHIPS." ON ".SFMEMBERSHIPS.".user_id = ".SFMEMBERS.".user_id
  827. LEFT JOIN ".SFUSERGROUPS." ON ".SFUSERGROUPS.".usergroup_id = ".SFMEMBERSHIPS.".usergroup_id
  828. ".$where."
  829. ORDER BY usergroup_id, ".SFMEMBERS.".display_name "
  830. .$limit;
  831. $records = $wpdb->get_results($query, ARRAY_A);
  832. $data->records = $records;
  833. $data->count = $wpdb->get_var("SELECT FOUND_ROWS()");
  834. }
  835. return $data;
  836. }
  837. function sf_get_forum_memberships($user_id)
  838. {
  839. global $wpdb, $current_user;
  840. if ($current_user->guest)
  841. {
  842. $value = sf_get_sfmeta('default usergroup', 'sfguests');
  843. $guests = $value[0]['meta_value'];
  844. $sql = "SELECT forum_id
  845. FROM ".SFPERMISSIONS."
  846. WHERE usergroup_id=".$guests;
  847. } else {
  848. $sql = "SELECT forum_id
  849. FROM ".SFPERMISSIONS."
  850. JOIN ".SFMEMBERSHIPS." ON ".SFPERMISSIONS.".usergroup_id = ".SFMEMBERSHIPS.".usergroup_id
  851. WHERE user_id=".$user_id;
  852. }
  853. return $wpdb->get_results($sql);
  854. }
  855. function sf_get_membership_count($usergroup_id)
  856. {
  857. global $wpdb;
  858. if(!$usergroup_id) return '';
  859. $sql = "SELECT COUNT(*)
  860. FROM ".SFMEMBERSHIPS."
  861. WHERE ".SFMEMBERSHIPS.".usergroup_id=".$usergroup_id;
  862. return $wpdb->get_var($sql);
  863. }
  864. # ------------------------------------------------------------------
  865. # sf_get_last_post_in_topic()
  866. #
  867. # Returns post details of the latest post in the requested topic
  868. # $topicid: requested topic
  869. # NOTE: This one remains as used in a template tag
  870. # ------------------------------------------------------------------
  871. function sf_get_last_post_in_topic($topicid)
  872. {
  873. global $wpdb;
  874. if(!$topicid) return '';
  875. return $wpdb->get_row(
  876. "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
  877. FROM ".SFPOSTS."
  878. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  879. LEFT JOIN ".SFUSERS." ON ".SFPOSTS.".user_id = ".SFUSERS.".ID
  880. WHERE topic_id = ".$topicid." AND post_status = 0
  881. ORDER BY post_id DESC LIMIT 1");
  882. }
  883. # ------------------------------------------------------------------
  884. # sf_get_postratings()
  885. #
  886. # Returns post ratings
  887. # $postid: post_id of post to return
  888. # $asArray: return as an array if true
  889. # Note: No permission checking is performed
  890. # ------------------------------------------------------------------
  891. function sf_get_postratings($postid, $asArray=false)
  892. {
  893. global $wpdb;
  894. if(!$postid) return '';
  895. $sql=(
  896. "SELECT *
  897. FROM ".SFPOSTRATINGS."
  898. WHERE post_id=".$postid.";");
  899. if($asArray) return $wpdb->get_row($sql, ARRAY_A);
  900. return $wpdb->get_row($sql);
  901. }
  902. # ------------------------------------------------------------------
  903. # sf_update_postratings()
  904. #
  905. # Upates post ratings
  906. # $postid: post_id
  907. # $count: number of votes
  908. # $sum: ratings sum
  909. # $ips: array of ips voted for guests
  910. # $members: members that have voted
  911. # Note: No permission checking is performed
  912. # ------------------------------------------------------------------
  913. function sf_update_postratings($postid, $count, $sum, $ips, $members)
  914. {
  915. global $wpdb;
  916. if(!$postid) return '';
  917. $sql=(
  918. "UPDATE ".SFPOSTRATINGS."
  919. SET vote_count=$count, ratings_sum=$sum, ips='".$ips."', members='".$members."'
  920. WHERE post_id=".$postid.";");
  921. $wpdb->query($sql);
  922. return;
  923. }
  924. # ------------------------------------------------------------------
  925. # Add post ratings
  926. # $postid: post_id
  927. # $count: number of votes
  928. # $sum: ratings sum
  929. # $ips: array of ips voted for guests
  930. # $members: members that have voted
  931. # Note: No permission checking is performed
  932. # ------------------------------------------------------------------
  933. function sf_add_postratings($postid, $count, $sum, $ips, $members)
  934. {
  935. global $wpdb;
  936. if(!$postid) return '';
  937. $sql=(
  938. "INSERT INTO ".SFPOSTRATINGS." (post_id, vote_count, ratings_sum, ips, members)
  939. VALUES ($postid, $count, $sum, '".$ips."', '".$members."');");
  940. $wpdb->query($sql);
  941. return;
  942. }
  943. # ------------------------------------------------------------------
  944. # sf_get_topic_ratings()
  945. #
  946. # Returns post ratings
  947. # $topicid: post_id of post to return
  948. # Note: No permission checking is performed
  949. # ------------------------------------------------------------------
  950. function sf_get_topic_ratings($topicid)
  951. {
  952. global $wpdb;
  953. if(!$topicid) return '';
  954. $sql = ("
  955. SELECT vote_count, ratings_sum
  956. FROM ".SFPOSTRATINGS."
  957. JOIN ".SFPOSTS." ON ".SFPOSTS.".topic_id = ".$topicid."
  958. WHERE ".SFPOSTRATINGS.".post_id = ".SFPOSTS.".post_id");
  959. return $wpdb->get_results($sql);
  960. }
  961. # ------------------------------------------------------------------
  962. # sf_get_forum_record()
  963. #
  964. # Returns a single forum row
  965. # $forumid: forum_id of forum to return
  966. # Note: No permission checking is performed
  967. # ------------------------------------------------------------------
  968. function sf_get_forum_record($forumid)
  969. {
  970. global $wpdb, $session_forums;
  971. if(!$forumid) return '';
  972. # check if in the session forums cache
  973. if($session_forums)
  974. {
  975. foreach($session_forums as $forum)
  976. {
  977. if($forum->forum_id == $forumid)
  978. {
  979. return $forum;
  980. }
  981. }
  982. }
  983. $sql=(
  984. "SELECT *
  985. FROM ".SFFORUMS."
  986. WHERE forum_id=".$forumid.";");
  987. $thisforum = $wpdb->get_row($sql);
  988. $session_forums[] = $thisforum;
  989. return $thisforum;
  990. }
  991. # ------------------------------------------------------------------
  992. # sf_get_forum_record_from_slug()
  993. #
  994. # Returns a single forum row
  995. # $forumslug: forum_slug of forum to return
  996. # $asArray: return as an array if true
  997. # Note: No permission checking is performed
  998. # ------------------------------------------------------------------
  999. function sf_get_forum_record_from_slug($forumslug, $asArray=false)
  1000. {
  1001. global $wpdb;
  1002. if(!$forumslug) return '';
  1003. $sql=(
  1004. "SELECT *
  1005. FROM ".SFFORUMS."
  1006. WHERE forum_slug='".$forumslug."';");
  1007. if($asArray) return $wpdb->get_row($sql, ARRAY_A);
  1008. return $wpdb->get_row($sql);
  1009. }
  1010. # ------------------------------------------------------------------
  1011. # sf_get_group_record_from_slug()
  1012. #
  1013. # Returns a single group and forum row
  1014. # $forumslug: forum_slug of group and forum to return
  1015. # $asArray: return as an array if true
  1016. # Note: No permission checking is performed
  1017. # ------------------------------------------------------------------
  1018. function sf_get_group_record_from_slug($forumslug, $asArray=false)
  1019. {
  1020. global $wpdb;
  1021. if(!$forumslug) return '';
  1022. $sql=(
  1023. "SELECT *
  1024. FROM ".SFFORUMS."
  1025. JOIN ".SFGROUPS." ON ".SFFORUMS.".group_id = ".SFGROUPS.".group_id
  1026. WHERE forum_slug='".$forumslug."';");
  1027. if($asArray) return $wpdb->get_row($sql, ARRAY_A);
  1028. return $wpdb->get_row($sql);
  1029. }
  1030. # ------------------------------------------------------------------
  1031. # sf_get_forums_all()
  1032. #
  1033. # Returns complete recordset of forums
  1034. # $id_only: limit recordset to forum_id and slug only
  1035. # $asArray: return results as an array
  1036. # Note: No permission checking is performed
  1037. # ------------------------------------------------------------------
  1038. function sf_get_forums_all($id_only=false, $asArray=false)
  1039. {
  1040. global $wpdb;
  1041. if($id_only ? $FROM='forum_id, forum_slug' : $FROM='*');
  1042. $sql=("SELECT ".$FROM." FROM ".SFFORUMS." ORDER BY forum_seq");
  1043. if($asArray) return $wpdb->get_results($sql, ARRAY_A);
  1044. return $wpdb->get_results($sql);
  1045. }
  1046. # ------------------------------------------------------------------
  1047. # sf_forum_exists()
  1048. #
  1049. # Check the existence of a forum by id
  1050. # $forumid: forum to check for
  1051. # ------------------------------------------------------------------
  1052. function sf_forum_exists($forumid)
  1053. {
  1054. global $wpdb;
  1055. if(empty($forumid)) return false;
  1056. if($wpdb->get_var(
  1057. "SELECT forum_name
  1058. FROM ".SFFORUMS."
  1059. WHERE forum_id=".$forumid))
  1060. {
  1061. return true;
  1062. }
  1063. return false;
  1064. }
  1065. # ------------------------------------------------------------------
  1066. # sf_forum_locked()
  1067. #
  1068. # Returns the lock status of a forum
  1069. # $forumid: forum to check for
  1070. # ------------------------------------------------------------------
  1071. function sf_forum_locked($forumid)
  1072. {
  1073. global $wpdb;
  1074. return $wpdb->get_var("SELECT forum_status FROM ".SFFORUMS." WHERE forum_id=".$forumid);
  1075. }
  1076. # ------------------------------------------------------------------
  1077. # sf_get_forum_rss_url()
  1078. #
  1079. # Returns the RSS URL for a forum (custom or standard)
  1080. # $forumid: forum to return
  1081. # $forumslug: slug for the url
  1082. # ------------------------------------------------------------------
  1083. function sf_get_forum_rss_url($forumid, $forumslug)
  1084. {
  1085. global $wpdb, $sfglobals;
  1086. if (empty($forumid)) return '';
  1087. $url = $wpdb->get_var("SELECT forum_rss FROM ".SFFORUMS." WHERE forum_id=".$forumid);
  1088. if (empty($url))
  1089. {
  1090. $rssopt = sf_get_option('sfrss');
  1091. if ($rssopt['sfrssfeedkey'])
  1092. $url = sf_build_qurl('forum='.$forumslug, 'xfeed=forum', 'feedkey='.$sfglobals['member']['feedkey']);
  1093. else
  1094. $url = sf_build_qurl('forum='.$forumslug, 'xfeed=forum');
  1095. }
  1096. return $url;
  1097. }
  1098. # ------------------------------------------------------------------
  1099. # sf_get_topic_status_set()
  1100. #
  1101. # Returns the topic status set name for a forum
  1102. # $forumid: forum to return
  1103. # ------------------------------------------------------------------
  1104. function sf_get_topic_status_set($forumid)
  1105. {
  1106. global $wpdb;
  1107. if(empty($forumid)) return '';
  1108. return $wpdb->get_var(
  1109. "SELECT topic_status_set FROM ".SFFORUMS." WHERE forum_id=".$forumid);
  1110. }
  1111. function sf_get_topic_status_from_forum($forumid, $statusflag)
  1112. {
  1113. global $wpdb;
  1114. if(!$forumid) return '';
  1115. $flag='';
  1116. $set=sf_get_topic_status_set($forumid);
  1117. if($set != 0)
  1118. {
  1119. $flag=sf_get_topic_status_flag($set, $statusflag);
  1120. }
  1121. return $flag;
  1122. }
  1123. # ------------------------------------------------------------------
  1124. # sf_find_user_in_topic()
  1125. #
  1126. # Searches a topics posts to see if user has ever posted in it for
  1127. # the forums topic list icon
  1128. # $topicid: topic to search
  1129. # $userid: user to look for
  1130. # %%FUTURE OPTIMISE%%
  1131. # ------------------------------------------------------------------
  1132. function sf_find_user_in_topic($topicid, $userid)
  1133. {
  1134. global $wpdb;
  1135. if(!$topicid || !$userid) return '';
  1136. return $wpdb->get_col(
  1137. "SELECT user_id
  1138. FROM ".SFPOSTS."
  1139. WHERE topic_id=".$topicid."
  1140. AND user_id=".$userid);
  1141. }
  1142. # ------------------------------------------------------------------
  1143. # sf_get_forum_from_topic()
  1144. #
  1145. # returng the firum id when only the topic is known
  1146. # $topicid: topic to search
  1147. # ------------------------------------------------------------------
  1148. function sf_get_forum_from_topic($topicid)
  1149. {
  1150. global $wpdb;
  1151. if(!$topicid) return '';
  1152. return $wpdb->get_var(
  1153. "SELECT forum_id
  1154. FROM ".SFTOPICS."
  1155. WHERE topic_id=".$topicid);
  1156. }
  1157. # ------------------------------------------------------------------
  1158. # sf_get_forum_name()
  1159. #
  1160. # Returns forum name when only the slug is known
  1161. # $forumslug: forum to return
  1162. # ------------------------------------------------------------------
  1163. function sf_get_forum_name($forumslug)
  1164. {
  1165. global $wpdb;
  1166. if(!$forumslug) return '';
  1167. return $wpdb->get_var(
  1168. "SELECT forum_name
  1169. FROM ".SFFORUMS."
  1170. WHERE forum_slug='".$forumslug."'");
  1171. }
  1172. # ------------------------------------------------------------------
  1173. # sf_get_forum_name_from_id()
  1174. #
  1175. # Returns forum name when only the slug is known
  1176. # $forumid: forum to return
  1177. # ------------------------------------------------------------------
  1178. function sf_get_forum_name_from_id($forumid)
  1179. {
  1180. global $wpdb;
  1181. if(!$forumid) return '';
  1182. return $wpdb->get_var(
  1183. "SELECT forum_name
  1184. FROM ".SFFORUMS."
  1185. WHERE forum_id=".$forumid);
  1186. }
  1187. # ------------------------------------------------------------------
  1188. # sf_get_forum_slug()
  1189. #
  1190. # Returns forum slug when only the id is known
  1191. # $forumid: forum to return
  1192. # ------------------------------------------------------------------
  1193. function sf_get_forum_slug($forumid)
  1194. {
  1195. global $wpdb;
  1196. if(!$forumid) return '';
  1197. return $wpdb->get_var(
  1198. "SELECT forum_slug
  1199. FROM ".SFFORUMS."
  1200. WHERE forum_id=".$forumid);
  1201. }
  1202. # ------------------------------------------------------------------
  1203. # sf_get_forum_id()
  1204. #
  1205. # Returns forum id when only the slug is known
  1206. # $forumslug: forum to return
  1207. # ------------------------------------------------------------------
  1208. function sf_get_forum_id($forumslug)
  1209. {
  1210. global $wpdb;
  1211. if(!$forumslug) return '';
  1212. return $wpdb->get_var(
  1213. "SELECT forum_id
  1214. FROM ".SFFORUMS."
  1215. WHERE forum_slug='".$forumslug."'");
  1216. }
  1217. # ------------------------------------------------------------------
  1218. # sf_get_topics_forum_id()
  1219. #
  1220. # Returns forum id when only the topic id is known
  1221. # $topicid: forum to return from topic record
  1222. # ------------------------------------------------------------------
  1223. function sf_get_topics_forum_id($topicid)
  1224. {
  1225. global $wpdb;
  1226. if(!$topicid) return '';
  1227. return $wpdb->get_var(
  1228. "SELECT forum_id
  1229. FROM ".SFTOPICS."
  1230. WHERE topic_id=".$topicid);
  1231. }
  1232. # ******************************************************************
  1233. # TOPIC/POST VIEW AND GENERAL DB FUNCTIONS
  1234. # ******************************************************************
  1235. # ------------------------------------------------------------------
  1236. # sf_get_combined_topics_and_posts()
  1237. #
  1238. # Returns a page of posts for specified topic
  1239. # $topicid: topic and posts to load
  1240. # ------------------------------------------------------------------
  1241. function sf_get_combined_topics_and_posts($topicid)
  1242. {
  1243. global $wpdb, $sfvars, $sfglobals;
  1244. if(!$topicid) return '';
  1245. # sadly have to grab the topic row first because we need obverride sort order if set
  1246. $topic = $wpdb->get_row(
  1247. "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,
  1248. forum_slug, forum_status, topic_status_set, topic_status_flag, user_id, use_tags
  1249. FROM ".SFTOPICS."
  1250. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  1251. WHERE topic_id = ".$topicid.";", ARRAY_A);
  1252. # quick permission check
  1253. if(!sf_can_view_forum($topic['forum_id'])) return '';
  1254. # grab the tags if enabled for this forum
  1255. if ($topic['use_tags'])
  1256. {
  1257. $sql = "SELECT ".SFTAGS.".tag_name, tag_slug
  1258. FROM ".SFTAGMETA."
  1259. JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  1260. WHERE ".SFTAGMETA.".topic_id = ".$topicid;
  1261. $topic['tags'] = $wpdb->get_results($sql);
  1262. }
  1263. # now for the posts
  1264. $ORDER="ASC"; # default
  1265. if($sfglobals['display']['posts']['sortdesc']) $ORDER="DESC"; # global override
  1266. $ppaged=$sfglobals['display']['posts']['perpage'];
  1267. if(!$ppaged) $ppaged=20;
  1268. if($sfvars['page'] == 1 ? $startlimit = 0 : $startlimit = ((($sfvars['page']-1) * $ppaged)));
  1269. $topic['topic_page'] = $sfvars['page'];
  1270. $tpages = ($topic['post_count'] / $ppaged);
  1271. if(!is_int($tpages))
  1272. {
  1273. $tpages = intval($topic['post_count'] / $ppaged) +1;
  1274. }
  1275. $topic['topic_total_pages'] = $tpages;
  1276. $LIMIT = ' LIMIT '.$startlimit.', '.$ppaged;
  1277. $records = $wpdb->get_results(
  1278. "SELECT ".SFPOSTS.".post_id, post_content, ".sf_zone_datetime('post_date').", UNIX_TIMESTAMP(post_date) as udate, ".SFPOSTS.".user_id, guest_name, guest_email,
  1279. post_status, post_pinned, post_index, post_edit,
  1280. ".SFMEMBERS.".display_name, admin, posts, signature, avatar, pm,
  1281. user_url, user_email, rating_id, vote_count, ratings_sum, ips, members
  1282. FROM ".SFPOSTS."
  1283. LEFT JOIN ".SFUSERS." ON ".SFPOSTS.".user_id = ".SFUSERS.".ID
  1284. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  1285. LEFT JOIN ".SFPOSTRATINGS." ON ".SFPOSTRATINGS.".post_id = ".SFPOSTS.".post_id
  1286. WHERE topic_id = ".$topicid."
  1287. ORDER BY post_pinned DESC, ".SFPOSTS.".post_id ".$ORDER.$LIMIT, ARRAY_A);
  1288. $topic['posts'] = $records;
  1289. return $topic;
  1290. }
  1291. # ------------------------------------------------------------------
  1292. # sf_get_topic_record()
  1293. #
  1294. # Returns a single topic row
  1295. # $topicid: topic_id of topic to return
  1296. # Note: No permission checking is performed
  1297. # ------------------------------------------------------------------
  1298. function sf_get_topic_record($topicid)
  1299. {
  1300. global $wpdb, $session_topics;
  1301. if(!$topicid) return '';
  1302. # see if in the current session topics cache
  1303. if($session_topics)
  1304. {
  1305. foreach($session_topics as $topic)
  1306. {
  1307. if($topic->topic_id == $topicid)
  1308. {
  1309. return $topic;
  1310. }
  1311. }
  1312. }
  1313. $sql=(
  1314. "SELECT *
  1315. FROM ".SFTOPICS."
  1316. WHERE topic_id=".$topicid.";");
  1317. $thistopic = $wpdb->get_row($sql);
  1318. $session_topics[] = $thistopic;
  1319. return $thistopic;
  1320. }
  1321. # ------------------------------------------------------------------
  1322. # sf_get_topic_record_from_slug()
  1323. #
  1324. # Returns a single topic row
  1325. # $topicslug: topic_slug of topic to return
  1326. # Note: No permission checking is performed
  1327. # ------------------------------------------------------------------
  1328. function sf_get_topic_record_from_slug($topicslug)
  1329. {
  1330. global $wpdb, $session_topics;
  1331. if(!$topicslug) return '';
  1332. # see if in the current session topics cache
  1333. if($session_topics)
  1334. {
  1335. foreach($session_topics as $topic)
  1336. {
  1337. if($topic->topic_slug == $topicslug)
  1338. {
  1339. return $topic;
  1340. }
  1341. }
  1342. }
  1343. $sql=(
  1344. "SELECT *
  1345. FROM ".SFTOPICS."
  1346. WHERE topic_slug='".$topicslug."';");
  1347. $thistopic = $wpdb->get_row($sql);
  1348. $session_topics[] = $thistopic;
  1349. return $thistopic;
  1350. }
  1351. # ------------------------------------------------------------------
  1352. # sf_get_topic_record_from_blogpostid()
  1353. #
  1354. # Returns a single topic row
  1355. # $blogpostid: blog post id to see if any linked topics
  1356. # Note: No permission checking

Large files files are truncated, but you can click here to view the full file