PageRenderTime 68ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/openfarmtech/weblog-content
PHP | 4078 lines | 2634 code | 590 blank | 854 comment | 527 complexity | 4cee442dba5d7797a932258a26ec28a4 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.0, LGPL-3.0, BSD-3-Clause, GPL-3.0, LGPL-2.1, AGPL-3.0, CC-BY-SA-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: 2010-08-14 04:52:34 -0700 (Sat, 14 Aug 2010) $
  6. $Rev: 4424 $
  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;
  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))
  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;
  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'])
  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;
  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)) 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 8 then we need a separate query (for now)
  396. if($sfvars['searchtype'] == 8) # NO post_content or post_index = 1
  397. {
  398. $records = sf_query_results(
  399. $SELECT.SFFORUMS.".forum_id, forum_slug, forum_name, forum_status, group_id, topic_count, forum_icon, forum_desc, topic_status_set,
  400. parent, children, forum_message,
  401. ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
  402. topic_status, topic_pinned, topic_opened, topic_subs, topic_watches, topic_status_flag,
  403. post_ratings, use_tags, blog_post_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count, ".SFTOPICS.".user_id, post_status
  404. FROM ".SFFORUMS."
  405. JOIN ".SFTOPICS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  406. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  407. WHERE ".$MATCH.SFFORUMS.".forum_id=".$forumid.$ANDWHERE.$ORDER.$LIMIT.";");
  408. } else {
  409. $records = sf_query_results(
  410. $SELECT.SFFORUMS.".forum_id, forum_slug, forum_name, forum_status, group_id, topic_count, forum_icon, forum_desc, topic_status_set,
  411. parent, children, forum_message,
  412. ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
  413. topic_status, topic_pinned, topic_opened, topic_subs, topic_watches, topic_status_flag,
  414. post_ratings, use_tags, blog_post_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count, ".SFTOPICS.".user_id, post_content, post_status
  415. FROM ".SFFORUMS."
  416. JOIN ".SFTOPICS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  417. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  418. WHERE ".$MATCH.SFFORUMS.".forum_id=".$forumid.$ANDWHERE." AND ".SFPOSTS.".post_index=1 ".$ORDER.$LIMIT.";");
  419. }
  420. if($sfvars['searchpage'] && $records)
  421. {
  422. $sfvars['searchresults'] = $wpdb->get_var("SELECT FOUND_ROWS()");
  423. }
  424. $findex=-1;
  425. $tindex=0;
  426. if($records)
  427. {
  428. $topiclist = '';
  429. foreach($records as $record)
  430. {
  431. $forumid=$record->forum_id;
  432. if($findex == -1 || $forums[$findex]['forum_id'] != $forumid)
  433. {
  434. $findex++;
  435. $tindex=0;
  436. $forums[$findex]['forum_id']=$record->forum_id;
  437. $forums[$findex]['forum_slug']=$record->forum_slug;
  438. $forums[$findex]['forum_name']=$record->forum_name;
  439. $forums[$findex]['forum_desc']=$record->forum_desc;
  440. $forums[$findex]['forum_status']=$record->forum_status;
  441. $forums[$findex]['group_id']=$record->group_id;
  442. $forums[$findex]['topic_count']=$record->topic_count;
  443. $forums[$findex]['forum_icon']=$record->forum_icon;
  444. $forums[$findex]['topic_status_set']=$record->topic_status_set;
  445. $forums[$findex]['post_ratings']=$record->post_ratings;
  446. $forums[$findex]['use_tags']=$record->use_tags;
  447. $forums[$findex]['parent']=$record->parent;
  448. $forums[$findex]['children']=$record->children;
  449. $forums[$findex]['forum_message']=$record->forum_message;
  450. }
  451. $forums[$findex]['topics'][$tindex]['topic_id']=$record->topic_id;
  452. $forums[$findex]['topics'][$tindex]['topic_slug']=$record->topic_slug;
  453. $forums[$findex]['topics'][$tindex]['topic_name']=$record->topic_name;
  454. $forums[$findex]['topics'][$tindex]['topic_date']=$record->topic_date;
  455. $forums[$findex]['topics'][$tindex]['topic_status']=$record->topic_status;
  456. $forums[$findex]['topics'][$tindex]['topic_pinned']=$record->topic_pinned;
  457. $forums[$findex]['topics'][$tindex]['topic_opened']=$record->topic_opened;
  458. $forums[$findex]['topics'][$tindex]['topic_subs']=$record->topic_subs;
  459. $forums[$findex]['topics'][$tindex]['topic_watches']=$record->topic_watches;
  460. $forums[$findex]['topics'][$tindex]['topic_status_flag']=$record->topic_status_flag;
  461. $forums[$findex]['topics'][$tindex]['post_ratings']=$record->post_ratings;
  462. $forums[$findex]['topics'][$tindex]['use_tags']=$record->use_tags;
  463. $forums[$findex]['topics'][$tindex]['blog_post_id']=$record->blog_post_id;
  464. $forums[$findex]['topics'][$tindex]['post_id']=$record->post_id;
  465. $forums[$findex]['topics'][$tindex]['post_count']=$record->post_count;
  466. $forums[$findex]['topics'][$tindex]['user_id']=$record->user_id;
  467. # do we need a post tooltip on the topic link?
  468. if($sfdisplay['topics']['posttip'] && $sfvars['searchtype'] != 8)
  469. {
  470. $forums[$findex]['topics'][$tindex]['post_tip'] = sf_filter_tooltip_display($record->post_content, $record->post_status);
  471. } else {
  472. $forums[$findex]['topics'][$tindex]['post_tip']="";
  473. }
  474. # save topic in list for getting tags later
  475. $topiclist[] = $record->topic_id;
  476. $tindex++;
  477. }
  478. # Now get tags in one query for these topics
  479. $topiclist = implode(',', $topiclist);
  480. $sql = "SELECT tag_name, tag_slug, topic_id
  481. FROM ".SFTAGS."
  482. JOIN ".SFTAGMETA." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  483. WHERE topic_id IN (".$topiclist.")
  484. ORDER BY topic_id";
  485. $tags = $wpdb->get_results($sql);
  486. # Now sort the tags into the monsterous forum array
  487. if ($tags)
  488. {
  489. foreach ($forums as $findex => $forum)
  490. {
  491. foreach ($forum['topics'] as $tindex => $topic)
  492. {
  493. if ($topic['use_tags'])
  494. {
  495. $topictags = '';
  496. foreach ($tags as $tag)
  497. {
  498. if ($topic['topic_id'] == $tag->topic_id)
  499. {
  500. # build tag list for this forum/topic
  501. $topictags[] = $tag;
  502. }
  503. }
  504. # save the tags into the master array
  505. $forums[$findex]['topics'][$tindex]['tags'] = $topictags;
  506. }
  507. }
  508. }
  509. }
  510. } else {
  511. $record = sf_get_forum_record($forumid);
  512. if($record)
  513. {
  514. $forums[0]['forum_id']=$record->forum_id;
  515. $forums[0]['forum_slug']=$record->forum_slug;
  516. $forums[0]['forum_name']=$record->forum_name;
  517. $forums[0]['forum_desc']=$record->forum_desc;
  518. $forums[0]['forum_status']=$record->forum_status;
  519. $forums[0]['group_id']=$record->group_id;
  520. $forums[0]['topic_count']=$record->topic_count;
  521. $forums[0]['forum_icon']=$record->forum_icon;
  522. $forums[0]['topic_status_set']=$record->topic_status_set;
  523. $forums[0]['post_ratings']=$record->post_ratings;
  524. $forums[0]['use_tags']=$record->use_tags;
  525. $forums[0]['parent']=$record->parent;
  526. $forums[0]['children']=$record->children;
  527. $forums[0]['forum_message']=$record->forum_message;
  528. }
  529. }
  530. return $forums;
  531. }
  532. function sf_get_subforums($sublist)
  533. {
  534. global $wpdb;
  535. if (!$sublist) return '';
  536. $subforums = unserialize($sublist);
  537. $forumlist = array();
  538. foreach ($subforums as $forumid)
  539. {
  540. if (sf_can_view_forum($forumid)) $forumlist[] = $forumid;
  541. }
  542. if (empty($forumlist)) return '';
  543. $where = " WHERE forum_id IN (" . implode(",", $forumlist) . ")";
  544. $subforums = $wpdb->get_results("SELECT * FROM ".SFFORUMS.$where." ORDER BY forum_seq", ARRAY_A);
  545. return $subforums;
  546. }
  547. # ------------------------------------------------------------------
  548. # sf_get_combined_topic_stats()
  549. #
  550. # Returns the first and last post data for a topic
  551. # $postid: post id from new posts list
  552. # $user_id for checking of user has posted in topic -
  553. # ------------------------------------------------------------------
  554. function sf_get_combined_topic_stats($posts, $userid=0)
  555. {
  556. global $wpdb;
  557. $clause='';
  558. if($posts)
  559. {
  560. $pcount = count($posts);
  561. $done = 0;
  562. foreach ($posts as $topic=>$postindex)
  563. {
  564. $clause.= "(topic_id = ".$topic." AND post_index = 1 OR topic_id = ".$topic." AND post_index=".$postindex.")";
  565. $done++;
  566. if($done < $pcount) $clause.= " OR ";
  567. }
  568. } else {
  569. return;
  570. }
  571. $records = $wpdb->get_results(
  572. "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
  573. FROM ".SFPOSTS."
  574. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  575. WHERE ".$clause."
  576. ORDER BY post_id ASC;", ARRAY_A);
  577. # If forum view are we looking to see if user has posted in ant of the topics?
  578. if($records && $userid)
  579. {
  580. $clause='';
  581. $pcount = count($posts);
  582. $done = 0;
  583. foreach ($posts as $topic=>$postindex)
  584. {
  585. $clause.= "(topic_id = ".$topic." AND user_id = ".$userid.")";
  586. $done++;
  587. if($done < $pcount) $clause.= " OR ";
  588. }
  589. $userposted = $wpdb->get_results(
  590. "SELECT DISTINCT topic_id, user_id
  591. FROM ".SFPOSTS."
  592. WHERE ".$clause.";");
  593. } else {
  594. $userposted = '';
  595. }
  596. # Now to format and combine results
  597. if ($records)
  598. {
  599. $sfdisplay=array();
  600. $sfdisplay=sf_get_option('sfdisplay');
  601. $topics = array();
  602. foreach ($records as $record)
  603. {
  604. $topics[$record['topic_id']][$record['post_index']] = $record;
  605. # do we need a post tooltip on the topic link?
  606. if($sfdisplay['topics']['posttip'])
  607. {
  608. $topics[$record['topic_id']][$record['post_index']]['post_tip']=sf_filter_tooltip_display($record['post_content'], $record['post_status']);
  609. $topics[$record['topic_id']][$record['post_index']]['post_content']='';
  610. }
  611. }
  612. foreach ($records as $record)
  613. {
  614. $topics[$record['topic_id']]['thisuser'] = false;
  615. if ($userposted)
  616. {
  617. foreach ($userposted as $user)
  618. {
  619. if ($user->topic_id == $record['topic_id'])
  620. {
  621. $topics[$record['topic_id']]['thisuser'] = true;
  622. break;
  623. }
  624. }
  625. }
  626. }
  627. return $topics;
  628. } else {
  629. return '';
  630. }
  631. }
  632. # ******************************************************************
  633. # FORUM/TOPIC VIEW AND GENERAL DB FUNCTIONS
  634. # ******************************************************************
  635. # ------------------------------------------------------------------
  636. # sf_get_watched_topics($currentpagge)
  637. #
  638. # Grabs all watched topics
  639. # $currentpage: index to paging
  640. # ------------------------------------------------------------------
  641. function sf_get_watched_topics($currentpage=0)
  642. {
  643. global $wpdb, $current_user, $sfglobals;
  644. # quick permission check
  645. if (!$current_user->sfwatch) return '';
  646. $limit='';
  647. # get watched topics
  648. $list = $sfglobals['member']['watches'];
  649. if (empty($list)) return '';
  650. # create where clause of watched topics
  651. $where = " WHERE ".SFTOPICS.".topic_id IN (" . implode(",", $list) . ")";
  652. # retrieve watched topic records
  653. $query = "SELECT ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
  654. topic_status, topic_pinned, topic_opened, topic_watches, topic_status_flag,
  655. blog_post_id, ".SFTOPICS.".forum_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  656. forum_slug, forum_name, topic_status_set, post_ratings, group_id, post_content, post_status
  657. FROM ".SFTOPICS."
  658. JOIN ".SFFORUMS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  659. JOIN ".SFPOSTS." ON ".SFTOPICS.".post_id = ".SFPOSTS.".post_id ".
  660. $where." ORDER BY topic_date DESC ".$limit;
  661. $records = $wpdb->get_results($query, ARRAY_A);
  662. if($records)
  663. {
  664. $sfdisplay=array();
  665. $sfdisplay=sf_get_option('sfdisplay');
  666. # do we need a post tooltip on the topic link?
  667. if($sfdisplay['topics']['posttip'])
  668. {
  669. for($x=0; $x<count($records); $x++)
  670. {
  671. $records[$x]['post_tip']=sf_filter_tooltip_display($records[$x]['post_content'], $records[$x]['post_status']);
  672. $records[$x]['post_content']='';
  673. }
  674. }
  675. }
  676. $watched['records'] = $records;
  677. $watched['count'] = count($list);
  678. return $watched;
  679. }
  680. # ------------------------------------------------------------------
  681. # sf_get_subscribed_topics($currentpagge)
  682. #
  683. # Grabs all subscribed topics
  684. # $currentpage: index to paging
  685. # ------------------------------------------------------------------
  686. function sf_get_subscribed_topics($currentpage=0)
  687. {
  688. global $wpdb, $current_user, $sfglobals;
  689. $limit = '';
  690. # quick permission check
  691. if (!$current_user->sfsubscriptions) return '';
  692. # get subscribed topics
  693. $list = $sfglobals['member']['subscribe'];
  694. if (empty($list)) return '';
  695. # create where clause of subscribed topics
  696. $where = " WHERE ".SFTOPICS.".topic_id IN (" . implode(",", $list) . ")";
  697. # retrieve watched topic records
  698. $query = "SELECT ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
  699. topic_status, topic_pinned, topic_opened, topic_subs, topic_status_flag,
  700. blog_post_id, ".SFTOPICS.".forum_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  701. forum_slug, forum_name, topic_status_set, post_ratings, group_id, post_content, post_status
  702. FROM ".SFTOPICS."
  703. JOIN ".SFFORUMS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  704. JOIN ".SFPOSTS." ON ".SFTOPICS.".post_id = ".SFPOSTS.".post_id ".
  705. $where." ORDER BY topic_date DESC ".$limit;
  706. $records = $wpdb->get_results($query, ARRAY_A);
  707. if($records)
  708. {
  709. $sfdisplay=array();
  710. $sfdisplay=sf_get_option('sfdisplay');
  711. # do we need a post tooltip on the topic link?
  712. if($sfdisplay['topics']['posttip'])
  713. {
  714. for($x=0; $x<count($records); $x++)
  715. {
  716. $records[$x]['post_tip']=sf_filter_tooltip_display($records[$x]['post_content'], $records[$x]['post_status']);
  717. $records[$x]['post_content']='';
  718. }
  719. }
  720. }
  721. $subscribed['records'] = $records;
  722. $subscribed['count'] = count($list);
  723. return $subscribed;
  724. }
  725. # ------------------------------------------------------------------
  726. # sf_get_related_topics($tags)
  727. #
  728. # Grabs related topics based on tags
  729. # ------------------------------------------------------------------
  730. function sf_get_related_topics($tags)
  731. {
  732. global $wpdb;
  733. if(!$tags) return '';
  734. # build list of tags for the topic id
  735. $taglist = '';
  736. foreach ($tags as $tag)
  737. {
  738. if ($taglist == '')
  739. {
  740. $taglist = "('".$tag->tag_slug."'";
  741. } else {
  742. $taglist.= ",'".$tag->tag_slug."'";
  743. }
  744. }
  745. $taglist.= ")";
  746. # now grab the results
  747. $LIMIT = ' LIMIT 10';
  748. $ORDER = ' ORDER BY topic_id DESC';
  749. $WHERE = SFTOPICS.".topic_id IN (SELECT topic_id FROM ".SFTAGMETA." JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  750. WHERE tag_slug IN ".$taglist.")";
  751. $topics = $wpdb->get_results(
  752. "SELECT SQL_CALC_FOUND_ROWS DISTINCT
  753. ".SFTOPICS.".topic_id, topic_name, topic_slug, ".SFTOPICS.".forum_id, forum_name, forum_slug,
  754. ".sf_zone_datetime('topic_date').", topic_status, topic_pinned, topic_opened,
  755. topic_watches, topic_status_flag, blog_post_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  756. topic_status_set, post_ratings, group_id
  757. FROM ".SFTOPICS."
  758. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  759. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  760. WHERE ".$WHERE.$ORDER.$LIMIT.";", ARRAY_A);
  761. return $topics;
  762. }
  763. # ------------------------------------------------------------------
  764. # sf_get_memberlists()
  765. #
  766. # Builds viewable member lists for current user
  767. # ------------------------------------------------------------------
  768. function sf_get_memberlists($currentpage, $search)
  769. {
  770. global $wpdb, $current_user, $sfglobals;
  771. $data = '';
  772. $data->records = '';
  773. $data->count = 0;
  774. $sfmemberopts = sf_get_option('sfmemberopts');
  775. if ($current_user->forumadmin || ($sfmemberopts['sfshowmemberlist'] && $current_user->sfmemberlist))
  776. {
  777. # are we limiting member lists to memberships?
  778. $where = '';
  779. $sfmemberopts = sf_get_option('sfmemberopts');
  780. if ($sfmemberopts['sflimitmemberlist'] && !$current_user->forumadmin)
  781. {
  782. # get usergroups user has membership in
  783. $ugs = sf_get_user_memberships($current_user->ID);
  784. #if no usergroup memberships return empty list
  785. if ($ugs)
  786. {
  787. $ug_ids = '';
  788. foreach ($ugs as $ug)
  789. {
  790. $ug_ids[] = $ug['usergroup_id'];
  791. }
  792. # if no visible usergroups, dont return any search results
  793. if (empty($ug_ids)) return $data;
  794. } else {
  795. return $data;
  796. }
  797. # create where clause based on user memberships
  798. $where = " WHERE ".SFPERMISSIONS.".usergroup_id IN (" . implode(",", $ug_ids) . ") ";
  799. }
  800. if ($search != '')
  801. {
  802. if ($where == '') $where = 'WHERE '; else $where .= ' AND ';
  803. $where.= SFMEMBERS.'.display_name LIKE "'.$search.'%"';
  804. }
  805. # how many members per page?
  806. $startlimit = 0;
  807. $tpaged = $sfglobals['display']['topics']['perpage'];
  808. if(!$tpaged) $tpaged=20;
  809. if ($currentpage != 1)
  810. {
  811. $startlimit = ((($currentpage-1) * $tpaged));
  812. }
  813. $limit = " LIMIT ".$startlimit.', '.$tpaged;
  814. # retrieve members list records
  815. $query = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ".SFPERMISSIONS.".usergroup_id, ".SFMEMBERSHIPS.".user_id, ".SFMEMBERS.".display_name, user_email, posts, lastvisit, usergroup_name, usergroup_desc
  816. FROM ".SFPERMISSIONS."
  817. JOIN ".SFUSERGROUPS." ON ".SFUSERGROUPS.".usergroup_id = ".SFPERMISSIONS.".usergroup_id
  818. JOIN ".SFMEMBERSHIPS." ON ".SFMEMBERSHIPS.".usergroup_id = ".SFPERMISSIONS.".usergroup_id
  819. JOIN ".SFMEMBERS." ON ".SFMEMBERS.".user_id = ".SFMEMBERSHIPS.".user_id
  820. JOIN ".SFUSERS." ON ".SFUSERS.".ID = ".SFMEMBERSHIPS.".user_id
  821. ".$where."
  822. ORDER BY usergroup_id, ".SFMEMBERS.".display_name "
  823. .$limit;
  824. $records = $wpdb->get_results($query, ARRAY_A);
  825. $data->records = $records;
  826. $data->count = $wpdb->get_var("SELECT FOUND_ROWS()");
  827. }
  828. return $data;
  829. }
  830. function sf_get_forum_memberships($user_id)
  831. {
  832. global $wpdb, $current_user;
  833. if ($current_user->guest)
  834. {
  835. $value = sf_get_sfmeta('default usergroup', 'sfguests');
  836. $guests = $value[0]['meta_value'];
  837. $sql = "SELECT forum_id
  838. FROM ".SFPERMISSIONS."
  839. WHERE usergroup_id=".$guests;
  840. } else {
  841. $sql = "SELECT forum_id
  842. FROM ".SFPERMISSIONS."
  843. JOIN ".SFMEMBERSHIPS." ON ".SFPERMISSIONS.".usergroup_id = ".SFMEMBERSHIPS.".usergroup_id
  844. WHERE user_id=".$user_id;
  845. }
  846. return $wpdb->get_results($sql);
  847. }
  848. function sf_get_membership_count($usergroup_id)
  849. {
  850. global $wpdb;
  851. if(!$usergroup_id) return '';
  852. $sql = "SELECT COUNT(*)
  853. FROM ".SFMEMBERSHIPS."
  854. WHERE ".SFMEMBERSHIPS.".usergroup_id=".$usergroup_id;
  855. return $wpdb->get_var($sql);
  856. }
  857. # ------------------------------------------------------------------
  858. # sf_get_last_post_in_topic()
  859. #
  860. # Returns post details of the latest post in the requested topic
  861. # $topicid: requested topic
  862. # NOTE: This one remains as used in a template tag
  863. # ------------------------------------------------------------------
  864. function sf_get_last_post_in_topic($topicid)
  865. {
  866. global $wpdb;
  867. if(!$topicid) return '';
  868. return $wpdb->get_row(
  869. "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
  870. FROM ".SFPOSTS."
  871. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  872. LEFT JOIN ".SFUSERS." ON ".SFPOSTS.".user_id = ".SFUSERS.".ID
  873. WHERE topic_id = ".$topicid." AND post_status = 0
  874. ORDER BY post_id DESC LIMIT 1");
  875. }
  876. # ------------------------------------------------------------------
  877. # sf_get_postratings()
  878. #
  879. # Returns post ratings
  880. # $postid: post_id of post to return
  881. # $asArray: return as an array if true
  882. # Note: No permission checking is performed
  883. # ------------------------------------------------------------------
  884. function sf_get_postratings($postid, $asArray=false)
  885. {
  886. global $wpdb;
  887. if(!$postid) return '';
  888. $sql=(
  889. "SELECT *
  890. FROM ".SFPOSTRATINGS."
  891. WHERE post_id=".$postid.";");
  892. if($asArray) return $wpdb->get_row($sql, ARRAY_A);
  893. return $wpdb->get_row($sql);
  894. }
  895. # ------------------------------------------------------------------
  896. # sf_update_postratings()
  897. #
  898. # Upates post ratings
  899. # $postid: post_id
  900. # $count: number of votes
  901. # $sum: ratings sum
  902. # $ips: array of ips voted for guests
  903. # $members: members that have voted
  904. # Note: No permission checking is performed
  905. # ------------------------------------------------------------------
  906. function sf_update_postratings($postid, $count, $sum, $ips, $members)
  907. {
  908. global $wpdb;
  909. if(!$postid) return '';
  910. $sql=(
  911. "UPDATE ".SFPOSTRATINGS."
  912. SET vote_count=$count, ratings_sum=$sum, ips='".$ips."', members='".$members."'
  913. WHERE post_id=".$postid.";");
  914. $wpdb->query($sql);
  915. return;
  916. }
  917. # ------------------------------------------------------------------
  918. # Add post ratings
  919. # $postid: post_id
  920. # $count: number of votes
  921. # $sum: ratings sum
  922. # $ips: array of ips voted for guests
  923. # $members: members that have voted
  924. # Note: No permission checking is performed
  925. # ------------------------------------------------------------------
  926. function sf_add_postratings($postid, $count, $sum, $ips, $members)
  927. {
  928. global $wpdb;
  929. if(!$postid) return '';
  930. $sql=(
  931. "INSERT INTO ".SFPOSTRATINGS." (post_id, vote_count, ratings_sum, ips, members)
  932. VALUES ($postid, $count, $sum, '".$ips."', '".$members."');");
  933. $wpdb->query($sql);
  934. return;
  935. }
  936. # ------------------------------------------------------------------
  937. # sf_get_topic_ratings()
  938. #
  939. # Returns post ratings
  940. # $topicid: post_id of post to return
  941. # Note: No permission checking is performed
  942. # ------------------------------------------------------------------
  943. function sf_get_topic_ratings($topicid)
  944. {
  945. global $wpdb;
  946. if(!$topicid) return '';
  947. $sql = ("
  948. SELECT vote_count, ratings_sum
  949. FROM ".SFPOSTRATINGS."
  950. JOIN ".SFPOSTS." ON ".SFPOSTS.".topic_id = ".$topicid."
  951. WHERE ".SFPOSTRATINGS.".post_id = ".SFPOSTS.".post_id");
  952. return $wpdb->get_results($sql);
  953. }
  954. # ------------------------------------------------------------------
  955. # sf_get_forum_record()
  956. #
  957. # Returns a single forum row
  958. # $forumid: forum_id of forum to return
  959. # Note: No permission checking is performed
  960. # ------------------------------------------------------------------
  961. function sf_get_forum_record($forumid)
  962. {
  963. global $wpdb, $session_forums;
  964. if(!$forumid) return '';
  965. # check if in the session forums cache
  966. if($session_forums)
  967. {
  968. foreach($session_forums as $forum)
  969. {
  970. if($forum->forum_id == $forumid)
  971. {
  972. return $forum;
  973. }
  974. }
  975. }
  976. $sql=(
  977. "SELECT *
  978. FROM ".SFFORUMS."
  979. WHERE forum_id=".$forumid.";");
  980. $thisforum = $wpdb->get_row($sql);
  981. $session_forums[] = $thisforum;
  982. return $thisforum;
  983. }
  984. # ------------------------------------------------------------------
  985. # sf_get_forum_record_from_slug()
  986. #
  987. # Returns a single forum row
  988. # $forumslug: forum_slug of forum to return
  989. # $asArray: return as an array if true
  990. # Note: No permission checking is performed
  991. # ------------------------------------------------------------------
  992. function sf_get_forum_record_from_slug($forumslug, $asArray=false)
  993. {
  994. global $wpdb;
  995. if(!$forumslug) return '';
  996. $sql=(
  997. "SELECT *
  998. FROM ".SFFORUMS."
  999. WHERE forum_slug='".$forumslug."';");
  1000. if($asArray) return $wpdb->get_row($sql, ARRAY_A);
  1001. return $wpdb->get_row($sql);
  1002. }
  1003. # ------------------------------------------------------------------
  1004. # sf_get_group_record_from_slug()
  1005. #
  1006. # Returns a single group and forum row
  1007. # $forumslug: forum_slug of group and forum to return
  1008. # $asArray: return as an array if true
  1009. # Note: No permission checking is performed
  1010. # ------------------------------------------------------------------
  1011. function sf_get_group_record_from_slug($forumslug, $asArray=false)
  1012. {
  1013. global $wpdb;
  1014. if(!$forumslug) return '';
  1015. $sql=(
  1016. "SELECT *
  1017. FROM ".SFFORUMS."
  1018. JOIN ".SFGROUPS." ON ".SFFORUMS.".group_id = ".SFGROUPS.".group_id
  1019. WHERE forum_slug='".$forumslug."';");
  1020. if($asArray) return $wpdb->get_row($sql, ARRAY_A);
  1021. return $wpdb->get_row($sql);
  1022. }
  1023. # ------------------------------------------------------------------
  1024. # sf_get_forums_all()
  1025. #
  1026. # Returns complete recordset of forums
  1027. # $id_only: limit recordset to forum_id and slug only
  1028. # $asArray: return results as an array
  1029. # Note: No permission checking is performed
  1030. # ------------------------------------------------------------------
  1031. function sf_get_forums_all($id_only=false, $asArray=false)
  1032. {
  1033. global $wpdb;
  1034. if($id_only ? $FROM='forum_id, forum_slug' : $FROM='*');
  1035. $sql=("SELECT ".$FROM." FROM ".SFFORUMS." ORDER BY forum_seq");
  1036. if($asArray) return $wpdb->get_results($sql, ARRAY_A);
  1037. return $wpdb->get_results($sql);
  1038. }
  1039. # ------------------------------------------------------------------
  1040. # sf_forum_exists()
  1041. #
  1042. # Check the existence of a forum by id
  1043. # $forumid: forum to check for
  1044. # ------------------------------------------------------------------
  1045. function sf_forum_exists($forumid)
  1046. {
  1047. global $wpdb;
  1048. if(empty($forumid)) return false;
  1049. if($wpdb->get_var(
  1050. "SELECT forum_name
  1051. FROM ".SFFORUMS."
  1052. WHERE forum_id=".$forumid))
  1053. {
  1054. return true;
  1055. }
  1056. return false;
  1057. }
  1058. # ------------------------------------------------------------------
  1059. # sf_forum_locked()
  1060. #
  1061. # Returns the lock status of a forum
  1062. # $forumid: forum to check for
  1063. # ------------------------------------------------------------------
  1064. function sf_forum_locked($forumid)
  1065. {
  1066. global $wpdb;
  1067. return $wpdb->get_var("SELECT forum_status FROM ".SFFORUMS." WHERE forum_id=".$forumid);
  1068. }
  1069. # ------------------------------------------------------------------
  1070. # sf_get_forum_rss_url()
  1071. #
  1072. # Returns the RSS URL for a forum (custom or standard)
  1073. # $forumid: forum to return
  1074. # $forumslug: slug for the url
  1075. # ------------------------------------------------------------------
  1076. function sf_get_forum_rss_url($forumid, $forumslug)
  1077. {
  1078. global $wpdb, $sfglobals;
  1079. if (empty($forumid)) return '';
  1080. $url = $wpdb->get_var("SELECT forum_rss FROM ".SFFORUMS." WHERE forum_id=".$forumid);
  1081. if (empty($url))
  1082. {
  1083. $rssopt = sf_get_option('sfrss');
  1084. if ($rssopt['sfrssfeedkey'])
  1085. $url = sf_build_qurl('forum='.$forumslug, 'xfeed=forum', 'feedkey='.$sfglobals['member']['feedkey']);
  1086. else
  1087. $url = sf_build_qurl('forum='.$forumslug, 'xfeed=forum');
  1088. }
  1089. return $url;
  1090. }
  1091. # ------------------------------------------------------------------
  1092. # sf_get_topic_status_set()
  1093. #
  1094. # Returns the topic status set name for a forum
  1095. # $forumid: forum to return
  1096. # ------------------------------------------------------------------
  1097. function sf_get_topic_status_set($forumid)
  1098. {
  1099. global $wpdb;
  1100. if(empty($forumid)) return '';
  1101. return $wpdb->get_var(
  1102. "SELECT topic_status_set FROM ".SFFORUMS." WHERE forum_id=".$forumid);
  1103. }
  1104. function sf_get_topic_status_from_forum($forumid, $statusflag)
  1105. {
  1106. global $wpdb;
  1107. if(!$forumid) return '';
  1108. $flag='';
  1109. $set=sf_get_topic_status_set($forumid);
  1110. if($set != 0)
  1111. {
  1112. $flag=sf_get_topic_status_flag($set, $statusflag);
  1113. }
  1114. return $flag;
  1115. }
  1116. # ------------------------------------------------------------------
  1117. # sf_find_user_in_topic()
  1118. #
  1119. # Searches a topics posts to see if user has ever posted in it for
  1120. # the forums topic list icon
  1121. # $topicid: topic to search
  1122. # $userid: user to look for
  1123. # %%FUTURE OPTIMISE%%
  1124. # ------------------------------------------------------------------
  1125. function sf_find_user_in_topic($topicid, $userid)
  1126. {
  1127. global $wpdb;
  1128. if(!$topicid || !$userid) return '';
  1129. return $wpdb->get_col(
  1130. "SELECT user_id
  1131. FROM ".SFPOSTS."
  1132. WHERE topic_id=".$topicid."
  1133. AND user_id=".$userid);
  1134. }
  1135. # ------------------------------------------------------------------
  1136. # sf_get_forum_from_topic()
  1137. #
  1138. # returng the firum id when only the topic is known
  1139. # $topicid: topic to search
  1140. # ------------------------------------------------------------------
  1141. function sf_get_forum_from_topic($topicid)
  1142. {
  1143. global $wpdb;
  1144. if(!$topicid) return '';
  1145. return $wpdb->get_var(
  1146. "SELECT forum_id
  1147. FROM ".SFTOPICS."
  1148. WHERE topic_id=".$topicid);
  1149. }
  1150. # ------------------------------------------------------------------
  1151. # sf_get_forum_name()
  1152. #
  1153. # Returns forum name when only the slug is known
  1154. # $forumslug: forum to return
  1155. # ------------------------------------------------------------------
  1156. function sf_get_forum_name($forumslug)
  1157. {
  1158. global $wpdb;
  1159. if(!$forumslug) return '';
  1160. return $wpdb->get_var(
  1161. "SELECT forum_name
  1162. FROM ".SFFORUMS."
  1163. WHERE forum_slug='".$forumslug."'");
  1164. }
  1165. # ------------------------------------------------------------------
  1166. # sf_get_forum_name_from_id()
  1167. #
  1168. # Returns forum name when only the slug is known
  1169. # $forumid: forum to return
  1170. # ------------------------------------------------------------------
  1171. function sf_get_forum_name_from_id($forumid)
  1172. {
  1173. global $wpdb;
  1174. if(!$forumid) return '';
  1175. return $wpdb->get_var(
  1176. "SELECT forum_name
  1177. FROM ".SFFORUMS."
  1178. WHERE forum_id=".$forumid);
  1179. }
  1180. # ------------------------------------------------------------------
  1181. # sf_get_forum_slug()
  1182. #
  1183. # Returns forum slug when only the id is known
  1184. # $forumid: forum to return
  1185. # ------------------------------------------------------------------
  1186. function sf_get_forum_slug($forumid)
  1187. {
  1188. global $wpdb;
  1189. if(!$forumid) return '';
  1190. return $wpdb->get_var(
  1191. "SELECT forum_slug
  1192. FROM ".SFFORUMS."
  1193. WHERE forum_id=".$forumid);
  1194. }
  1195. # ------------------------------------------------------------------
  1196. # sf_get_forum_id()
  1197. #
  1198. # Returns forum id when only the slug is known
  1199. # $forumslug: forum to return
  1200. # ------------------------------------------------------------------
  1201. function sf_get_forum_id($forumslug)
  1202. {
  1203. global $wpdb;
  1204. if(!$forumslug) return '';
  1205. return $wpdb->get_var(
  1206. "SELECT forum_id
  1207. FROM ".SFFORUMS."
  1208. WHERE forum_slug='".$forumslug."'");
  1209. }
  1210. # ------------------------------------------------------------------
  1211. # sf_get_topics_forum_id()
  1212. #
  1213. # Returns forum id when only the topic id is known
  1214. # $topicid: forum to return from topic record
  1215. # ------------------------------------------------------------------
  1216. function sf_get_topics_forum_id($topicid)
  1217. {
  1218. global $wpdb;
  1219. if(!$topicid) return '';
  1220. return $wpdb->get_var(
  1221. "SELECT forum_id
  1222. FROM ".SFTOPICS."
  1223. WHERE topic_id=".$topicid);
  1224. }
  1225. # ******************************************************************
  1226. # TOPIC/POST VIEW AND GENERAL DB FUNCTIONS
  1227. # ******************************************************************
  1228. # ------------------------------------------------------------------
  1229. # sf_get_combined_topics_and_posts()
  1230. #
  1231. # Returns a page of posts for specified topic
  1232. # $topicid: topic and posts to load
  1233. # ------------------------------------------------------------------
  1234. function sf_get_combined_topics_and_posts($topicid)
  1235. {
  1236. global $wpdb, $sfvars, $sfglobals;
  1237. if(!$topicid) return '';
  1238. # sadly have to grab the topic row first because we need obverride sort order if set
  1239. $topic = $wpdb->get_row(
  1240. "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,
  1241. forum_slug, forum_status, topic_status_set, topic_status_flag, user_id, use_tags
  1242. FROM ".SFTOPICS."
  1243. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  1244. WHERE topic_id = ".$topicid.";", ARRAY_A);
  1245. # quick permission check
  1246. if(!sf_can_view_forum($topic['forum_id'])) return '';
  1247. # grab the tags if enabled for this forum
  1248. if ($topic['use_tags'])
  1249. {
  1250. $sql = "SELECT ".SFTAGS.".tag_name, tag_slug
  1251. FROM ".SFTAGMETA."
  1252. JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  1253. WHERE ".SFTAGMETA.".topic_id = ".$topicid;
  1254. $topic['tags'] = $wpdb->get_results($sql);
  1255. }
  1256. # now for the posts
  1257. $ORDER="ASC"; # default
  1258. if($sfglobals['display']['posts']['sortdesc']) $ORDER="DESC"; # global override
  1259. $ppaged=$sfglobals['display']['posts']['perpage'];
  1260. if(!$ppaged) $ppaged=20;
  1261. if($sfvars['page'] == 1 ? $startlimit = 0 : $startlimit = ((($sfvars['page']-1) * $ppaged)));
  1262. $topic['topic_page'] = $sfvars['page'];
  1263. $tpages = ($topic['post_count'] / $ppaged);
  1264. if(!is_int($tpages))
  1265. {
  1266. $tpages = intval($topic['post_count'] / $ppaged) +1;
  1267. }
  1268. $topic['topic_total_pages'] = $tpages;
  1269. $LIMIT = ' LIMIT '.$startlimit.', '.$ppaged;
  1270. $records = $wpdb->get_results(
  1271. "SELECT ".SFPOSTS.".post_id, post_content, ".sf_zone_datetime('post_date').", ".SFPOSTS.".user_id, guest_name, guest_email,
  1272. post_status, post_pinned, post_index, post_edit,
  1273. ".SFMEMBERS.".display_name, admin, posts, signature, avatar, pm,
  1274. user_url, user_email, rating_id, vote_count, ratings_sum, ips, members
  1275. FROM ".SFPOSTS."
  1276. LEFT JOIN ".SFUSERS." ON ".SFPOSTS.".user_id = ".SFUSERS.".ID
  1277. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  1278. LEFT JOIN ".SFPOSTRATINGS." ON ".SFPOSTRATINGS.".post_id = ".SFPOSTS.".post_id
  1279. WHERE topic_id = ".$topicid."
  1280. ORDER BY post_pinned DESC, ".SFPOSTS.".post_id ".$ORDER.$LIMIT, ARRAY_A);
  1281. $topic['posts'] = $records;
  1282. return $topic;
  1283. }
  1284. # ------------------------------------------------------------------
  1285. # sf_get_topic_record()
  1286. #
  1287. # Returns a single topic row
  1288. # $topicid: topic_id of topic to return
  1289. # Note: No permission checking is performed
  1290. # ------------------------------------------------------------------
  1291. function sf_get_topic_record($topicid)
  1292. {
  1293. global $wpdb, $session_topics;
  1294. if(!$topicid) return '';
  1295. # see if in the current session topics cache
  1296. if($session_topics)
  1297. {
  1298. foreach($session_topics as $topic)
  1299. {
  1300. if($topic->topic_id == $topicid)
  1301. {
  1302. return $topic;
  1303. }
  1304. }
  1305. }
  1306. $sql=(
  1307. "SELECT *
  1308. FROM ".SFTOPICS."
  1309. WHERE topic_id=".$topicid.";");
  1310. $thistopic = $wpdb->get_row($sql);
  1311. $session_topics[] = $thistopic;
  1312. return $thistopic;
  1313. }
  1314. # ------------------------------------------------------------------
  1315. # sf_get_topic_record_from_slug()
  1316. #
  1317. # Returns a single topic row
  1318. # $topicslug: topic_slug of topic to return
  1319. # Note: No permission checking is performed
  1320. # ------------------------------------------------------------------
  1321. function sf_get_topic_record_from_slug($topicslug)
  1322. {
  1323. global $wpdb, $session_topics;
  1324. if(!$topicslug) return '';
  1325. # see if in the current session topics cache
  1326. if($session_topics)
  1327. {
  1328. foreach($session_topics as $topic)
  1329. {
  1330. if($topic->topic_slug == $topicslug)
  1331. {
  1332. return $topic;
  1333. }
  1334. }
  1335. }
  1336. $sql=(
  1337. "SELECT *
  1338. FROM ".SFTOPICS."
  1339. WHERE topic_slug='".$topicslug."';");
  1340. $thistopic = $wpdb->get_row($sql);
  1341. $session_topics[] = $thistopic;
  1342. return $thistopic;
  1343. }
  1344. # ------------------------------------------------------------------
  1345. # sf_get_topic_record_from_blogpostid()
  1346. #
  1347. # Returns a single topic row
  1348. # $blogpostid: blog post id to see if any linked topics
  1349. # Note: No permission checking is performed
  1350. # ------------------------------------------------------------------
  1351. function sf_get_topic_record_from_blogpostid($postid)
  1352. {
  1353. global $wpdb, $session_topics;
  1354. if (!$postid) return '';
  1355. $sql = (
  1356. "SELECT *
  1357. FROM ".SFTOPICS."
  1358. WHERE blog_post_id=".$postid);
  1359. $thistopic = $wpdb->get_row($sql);
  1360. return $thistopic;
  1361. }
  1362. # ------------------------------------------------------------------
  1363. # sf_get_topics_all()
  1364. #
  1365. # Returns complete recordset of topics
  1366. # $id_only: limit recordset to topic_id and slug only
  1367. # $asArray: returb list as an aray
  1368. # Note: No permission checking is performed
  1369. # ------------------------------------------------------------------
  1370. function sf_get_topics_all($id_only=false, $asArray=false)
  1371. {
  1372. global $wpdb;
  1373. if($id_only ? $FROM='topic_id, topic_slug' : $FROM='*');
  1374. $sql=("SELECT ".$FROM." FROM ".SFTOPICS);
  1375. if($asArray) return $wpdb->get_results($sql, ARRAY_A);
  1376. return $wpdb->get_results

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