PageRenderTime 80ms CodeModel.GetById 26ms 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
  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 is performed
  1357. # ------------------------------------------------------------------
  1358. function sf_get_topic_record_from_blogpostid($postid)
  1359. {
  1360. global $wpdb, $session_topics;
  1361. if (!$postid) return '';
  1362. $sql = (
  1363. "SELECT *
  1364. FROM ".SFTOPICS."
  1365. WHERE blog_post_id=".$postid);
  1366. $thistopic = $wpdb->get_row($sql);
  1367. return $thistopic;
  1368. }
  1369. # ------------------------------------------------------------------
  1370. # sf_get_topics_all()
  1371. #
  1372. # Returns complete recordset of topics
  1373. # $id_only: limit recordset to topic_id and slug only
  1374. # $asArray: returb list as an aray
  1375. # Note: No permission checking is performed
  1376. # ------------------------------------------------------------------
  1377. function sf_get_topics_all($id_only=false, $asArray=false)
  1378. {
  1379. global $wpdb;
  1380. if($id_only ? $FROM='topic_id, topic_slug' : $FROM='*');
  1381. $sql=("SELECT ".$FROM." FROM ".SFTOPICS);
  1382. if($asArray) return $wpdb->get_results($sql, ARRAY_A);
  1383. return $wpdb->get_results($sql);
  1384. }
  1385. # ------------------------------------------------------------------
  1386. # sf_topic_exists()
  1387. #
  1388. # Check the existence of a topic by id
  1389. # $topicid: forum to check for
  1390. # ------------------------------------------------------------------
  1391. function sf_topic_exists($topicid)
  1392. {
  1393. global $wpdb;
  1394. if(empty($topicid)) return false;
  1395. if($wpdb->get_var(
  1396. "SELECT topic_name
  1397. FROM ".SFTOPICS."
  1398. WHERE topic_id=".$topicid))
  1399. {
  1400. return true;
  1401. }
  1402. return false;
  1403. }
  1404. # ------------------------------------------------------------------
  1405. # sf_get_topic_name()
  1406. #
  1407. # Returns topic name when only the topic slug is known
  1408. # $topicslug: Topic to lookup
  1409. # ------------------------------------------------------------------
  1410. function sf_get_topic_name($topicslug)
  1411. {
  1412. global $wpdb;
  1413. if(!$topicslug) return '';
  1414. return $wpdb->get_var(
  1415. "SELECT topic_name
  1416. FROM ".SFTOPICS."
  1417. WHERE topic_slug='".$topicslug."'");
  1418. }
  1419. # ------------------------------------------------------------------
  1420. # sf_get_topic_slug()
  1421. #
  1422. # Returns topic slug when only the topic id is known
  1423. # $topicid: Topic to lookup
  1424. # ------------------------------------------------------------------
  1425. function sf_get_topic_slug($topicid)
  1426. {
  1427. global $wpdb;
  1428. if(!$topicid) return '';
  1429. return $wpdb->get_var(
  1430. "SELECT topic_slug
  1431. FROM ".SFTOPICS."
  1432. WHERE topic_id=".$topicid);
  1433. }
  1434. # ------------------------------------------------------------------
  1435. # sf_get_topic_id()
  1436. #
  1437. # Returns topic id when only the topic slug is known
  1438. # $topicslug: Topic to lookup
  1439. # ------------------------------------------------------------------
  1440. function sf_get_topic_id($topicslug)
  1441. {
  1442. global $wpdb;
  1443. if(!$topicslug) return '';
  1444. return $wpdb->get_var(
  1445. "SELECT topic_id
  1446. FROM ".SFTOPICS."
  1447. WHERE topic_slug='".$topicslug."'");
  1448. }
  1449. # ------------------------------------------------------------------
  1450. # sf_get_topic_name_from_id()
  1451. #
  1452. # Returns topic name when only the id is known
  1453. # $topicid: topic to return
  1454. # ------------------------------------------------------------------
  1455. function sf_get_topic_name_from_id($topicid)
  1456. {
  1457. global $wpdb;
  1458. if(!$topicid) return '';
  1459. return $wpdb->get_var(
  1460. "SELECT topic_name
  1461. FROM ".SFTOPICS."
  1462. WHERE topic_id=".$topicid);
  1463. }
  1464. # ------------------------------------------------------------------
  1465. # sf_get_slugs_from_postid()
  1466. #
  1467. # Returns forum and topic slugs when only the post id is known
  1468. # $postid: Post to lookup
  1469. # ------------------------------------------------------------------
  1470. function sf_get_slugs_from_postid($postid)
  1471. {
  1472. global $wpdb;
  1473. if(!$postid) return '';
  1474. return $wpdb->get_row(
  1475. "SELECT forum_slug, topic_slug, post_index
  1476. FROM ".SFPOSTS."
  1477. JOIN ".SFFORUMS." ON ".SFPOSTS.".forum_id = ".SFFORUMS.".forum_id
  1478. JOIN ".SFTOPICS." ON ".SFPOSTS.".topic_id = ".SFTOPICS.".topic_id
  1479. WHERE ".SFPOSTS.".post_id=".$postid.";");
  1480. }
  1481. # ------------------------------------------------------------------
  1482. # sf_get_posts_count_in_topic()
  1483. #
  1484. # Returns the post count from topic record
  1485. # $topicid: Topic to lookup
  1486. # ------------------------------------------------------------------
  1487. function sf_get_posts_count_in_topic($topicid)
  1488. {
  1489. global $wpdb;
  1490. if(empty($topicid)) return 0;
  1491. return $wpdb->get_var(
  1492. "SELECT post_count
  1493. FROM ".SFTOPICS."
  1494. WHERE topic_id=".$topicid);
  1495. }
  1496. # ------------------------------------------------------------------
  1497. # sf_get_posts_count_in_linked_topic()
  1498. #
  1499. # Returns the post count of approved posts in topic
  1500. # $topicid: Topic to lookup
  1501. # $hidedupes Hide duplicate comments
  1502. # ------------------------------------------------------------------
  1503. function sf_get_posts_count_in_linked_topic($topicid, $hidedupes)
  1504. {
  1505. global $wpdb;
  1506. if(empty($topicid)) return 0;
  1507. $hide='';
  1508. if($hidedupes) $hide = " AND comment_id IS NULL";
  1509. return $wpdb->get_var(
  1510. "SELECT COUNT(post_id)
  1511. FROM ".SFPOSTS."
  1512. WHERE topic_id=".$topicid." AND post_status=0".$hide);
  1513. }
  1514. function sf_update_topic_status_flag($statvalue, $topicid)
  1515. {
  1516. global $wpdb;
  1517. if(!$statvalue || !$topicid) return '';
  1518. return $wpdb->query("UPDATE ".SFTOPICS." SET topic_status_flag=".$statvalue." WHERE topic_id=".$topicid);
  1519. }
  1520. # ******************************************************************
  1521. # NEW/UNREAD POST VIEWS DB FUNCTIONS
  1522. # ******************************************************************
  1523. # ------------------------------------------------------------------
  1524. # sf_get_admins_queued_posts()
  1525. #
  1526. # Returns the admins new post view
  1527. # ------------------------------------------------------------------
  1528. function sf_get_admins_queued_posts()
  1529. {
  1530. global $wpdb, $sfvars;
  1531. $newposts = '';
  1532. $clause = '';
  1533. $records = $wpdb->get_results(
  1534. "SELECT ".SFWAITING.".forum_id, forum_slug, forum_name, topic_status_set, topic_id, ".SFWAITING.".post_count, ".SFWAITING.".post_id
  1535. FROM ".SFWAITING."
  1536. LEFT JOIN ".SFFORUMS." ON ".SFWAITING.".forum_id = ".SFFORUMS.".forum_id
  1537. ORDER BY forum_id;");
  1538. if($records)
  1539. {
  1540. # now grab all of the post record we are going to need in one query
  1541. $pcount = count($records);
  1542. $done = 0;
  1543. foreach ($records as $record)
  1544. {
  1545. $clause.= "(".SFTOPICS.".topic_id = ".$record->topic_id." AND ".SFPOSTS.".post_id >= ".$record->post_id.")";
  1546. $done++;
  1547. if($done < $pcount) $clause.= " OR ";
  1548. }
  1549. $preparedpostrecords = $wpdb->get_results(
  1550. "SELECT ".SFPOSTS.".topic_id, post_content, post_index, ".SFPOSTS.".post_id, post_status, ".SFPOSTS.".user_id, ".SFMEMBERS.".display_name, guest_name, topic_slug, topic_status_flag, topic_name
  1551. FROM ".SFPOSTS."
  1552. LEFT JOIN ".SFTOPICS." ON ".SFPOSTS.".topic_id = ".SFTOPICS.".topic_id
  1553. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  1554. WHERE ".$clause."
  1555. ORDER BY post_id;");
  1556. $sfvars['queue'] = count($preparedpostrecords);
  1557. $newposts = array();
  1558. $findex=-1;
  1559. $pindex=0;
  1560. $tindex=0;
  1561. foreach($records as $record)
  1562. {
  1563. # Check this still has posts in it and they were not removed (it happens)
  1564. $postrecords='';
  1565. foreach($preparedpostrecords as $prepared)
  1566. {
  1567. if($prepared->topic_id == $record->topic_id)
  1568. {
  1569. $postrecords[] = $prepared;
  1570. }
  1571. }
  1572. # So - were they removed? if so donlt add them to the array and remove them from sfwaiting
  1573. if($postrecords == '')
  1574. {
  1575. sf_remove_from_waiting(true, $record->topic_id, $record->post_id);
  1576. continue;
  1577. }
  1578. $forumid=$record->forum_id;
  1579. if($findex == -1 || $newposts[$findex]['forum_id'] != $forumid)
  1580. {
  1581. $findex++;
  1582. $tindex=0;
  1583. $pindex=0;
  1584. $newposts[$findex]['forum_id']=$record->forum_id;
  1585. $newposts[$findex]['forum_name']=$record->forum_name;
  1586. $newposts[$findex]['forum_slug']=$record->forum_slug;
  1587. $newposts[$findex]['topic_status_set']=$record->topic_status_set;
  1588. }
  1589. $newposts[$findex]['topics'][$tindex]['topic_id']=$record->topic_id;
  1590. $newposts[$findex]['topics'][$tindex]['post_id']=$record->post_id;
  1591. # isolate the post records for current topic
  1592. $postrecords='';
  1593. foreach($preparedpostrecords as $prepared)
  1594. {
  1595. if($prepared->topic_id == $record->topic_id)
  1596. {
  1597. $postrecords[] = $prepared;
  1598. }
  1599. }
  1600. if($postrecords)
  1601. {
  1602. $newposts[$findex]['topics'][$tindex]['post_count']=count($postrecords);
  1603. $pindex=0;
  1604. foreach($postrecords as $postrecord)
  1605. {
  1606. $newposts[$findex]['topics'][$tindex]['topic_slug']=$postrecord->topic_slug;
  1607. $newposts[$findex]['topics'][$tindex]['topic_status_flag']=$postrecord->topic_status_flag;
  1608. $newposts[$findex]['topics'][$tindex]['topic_name']=$postrecord->topic_name;
  1609. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['post_id']=$postrecord->post_id;
  1610. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['post_status']=$postrecord->post_status;
  1611. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['post_index']=$postrecord->post_index;
  1612. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['post_content']=$postrecord->post_content;
  1613. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['user_id']=$postrecord->user_id;
  1614. if(empty($postrecord->user_id))
  1615. {
  1616. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['display_name']=$postrecord->guest_name;
  1617. $thisuser = 'Guest';
  1618. } else {
  1619. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['display_name']=$postrecord->display_name;
  1620. $thisuser = 'Member';
  1621. }
  1622. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['user_type']=$thisuser;
  1623. $pindex++;
  1624. }
  1625. }
  1626. $tindex++;
  1627. }
  1628. }
  1629. # if no new posts then housekeep sfwaiting
  1630. if(!$newposts) $wpdb->query("TRUNCATE ".SFWAITING);
  1631. return $newposts;
  1632. }
  1633. # ------------------------------------------------------------------
  1634. # sf_combined_new_posts_list()
  1635. #
  1636. # Extend new post list to include all required data
  1637. # $posts: Posts array (forumid and topicid)
  1638. # $forumsort Sort list into group/forum sequence
  1639. # ------------------------------------------------------------------
  1640. function sf_combined_new_posts_list($posts, $forumsort=false)
  1641. {
  1642. global $wpdb;
  1643. $clause='';
  1644. $record='';
  1645. if($posts)
  1646. {
  1647. $pcount = count($posts);
  1648. $done = 0;
  1649. foreach ($posts as $post)
  1650. {
  1651. if (sf_can_view_forum($post->forum_id, true))
  1652. {
  1653. if ($clause != '') $clause.= ' OR ';
  1654. $clause.= "(".SFTOPICS.".forum_id=".$post->forum_id." AND ".SFTOPICS.".topic_id=".$post->topic_id.")";
  1655. $done++;
  1656. }
  1657. }
  1658. # just return if nothing visible
  1659. if ($clause == '') return '';
  1660. if($forumsort)
  1661. {
  1662. $groupjoin = " JOIN ".SFGROUPS." ON ".SFFORUMS.".group_id = ".SFGROUPS.".group_id ";
  1663. $orderby = " ORDER BY group_seq, forum_seq";
  1664. $groupname = ', group_name';
  1665. } else {
  1666. $groupjoin = '';
  1667. $orderby = ' ORDER BY post_id DESC';
  1668. $groupname = '';
  1669. }
  1670. $record = $wpdb->get_results(
  1671. "SELECT ".SFFORUMS.".forum_id, forum_name, forum_slug,
  1672. topic_name, topic_slug, ".SFTOPICS.".post_id, ".SFTOPICS.".topic_id, topic_status_set,
  1673. post_status, UNIX_TIMESTAMP(".SFPOSTS.".post_date) as udate, post_index, ".SFPOSTS.".user_id ".$groupname."
  1674. FROM ".SFFORUMS."
  1675. JOIN ".SFTOPICS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  1676. LEFT JOIN ".SFPOSTS." ON ".SFTOPICS.".post_id = ".SFPOSTS.".post_id
  1677. ".$groupjoin."
  1678. WHERE ".$clause.$orderby, ARRAY_A);
  1679. }
  1680. return $record;
  1681. }
  1682. # ------------------------------------------------------------------
  1683. # sf_get_users_new_post_list()
  1684. #
  1685. # Returns recordset of current users new-post-list
  1686. # $limit: limit to x number of records
  1687. # ------------------------------------------------------------------
  1688. function sf_get_users_new_post_list($limit)
  1689. {
  1690. global $current_user, $sfglobals;
  1691. if ($current_user->member)
  1692. {
  1693. $newpostlist = $sfglobals['member']['newposts'];
  1694. if($current_user->newpostlist == false)
  1695. {
  1696. $newpostlist = sf_update_users_newposts($newpostlist);
  1697. }
  1698. # we have a live user so construct SQL if anything in newpostslist
  1699. if ($newpostlist['topics'][0] != 0)
  1700. {
  1701. $wanted = $limit;
  1702. $where = ' WHERE';
  1703. if (count($newpostlist['topics']) < $limit) $limit = count($newpostlist['topics']);
  1704. for ($x=0; $x<$limit; $x++)
  1705. {
  1706. $where.= " topic_id=".$newpostlist['topics'][$x];
  1707. if ($x != $limit-1) $where.= " OR";
  1708. }
  1709. $recordset1 = sf_get_users_new_post_list_db($where, '');
  1710. # try and marry the extra count if not enough to satisfy $limit
  1711. if ($limit < $wanted)
  1712. {
  1713. $limit = " LIMIT ".$wanted;
  1714. $where = " WHERE post_status = 0 ";
  1715. $recordset2 = sf_get_users_new_post_list_db($where, $limit);
  1716. if ($recordset2)
  1717. {
  1718. for ($x=0; $x<count($recordset2); $x++)
  1719. {
  1720. if (!in_array($recordset2[$x]->topic_id, $newpostlist['topics']))
  1721. {
  1722. $recordset1[]=$recordset2[$x];
  1723. }
  1724. if (count($recordset1) == $wanted) break;
  1725. }
  1726. }
  1727. }
  1728. return sf_filter_new_post_list($recordset1);
  1729. }
  1730. }
  1731. # but if not a member, empty post list of members query didnlt reach limit...
  1732. if ($current_user->guest || $newpostlist['topics'][0] == 0)
  1733. {
  1734. $limit = " LIMIT ".$limit;
  1735. $where = " WHERE post_status = 0 ";
  1736. $recordset1 = sf_get_users_new_post_list_db($where, $limit);
  1737. return sf_filter_new_post_list($recordset1);
  1738. }
  1739. }
  1740. # ------------------------------------------------------------------
  1741. # sf_get_users_new_post_list_db()
  1742. #
  1743. # Support: Returns recordset of current users new-post-list
  1744. # $where: Option where clause on topic id
  1745. # $limit: limit to x number of records
  1746. # ------------------------------------------------------------------
  1747. function sf_get_users_new_post_list_db($where, $limit)
  1748. {
  1749. global $wpdb, $current_user;
  1750. $where2 = '';
  1751. if (!$current_user->forumadmin)
  1752. {
  1753. # limit to viewable forums based on permissions
  1754. $forums = sf_get_forum_memberships($current_user->ID);
  1755. if ($forums)
  1756. {
  1757. $forum_ids = '';
  1758. foreach ($forums as $forum)
  1759. {
  1760. if (sf_can_view_forum($forum->forum_id))
  1761. {
  1762. $forum_ids[] = $forum->forum_id;
  1763. }
  1764. }
  1765. } else {
  1766. return '';
  1767. }
  1768. # create where clause based on forums that current user can view
  1769. if ($forum_ids != '')
  1770. {
  1771. if ($where != '')
  1772. {
  1773. $where2 = " AND ";
  1774. } else {
  1775. $where2 = " WHERE ";
  1776. }
  1777. $where2.= SFPOSTS.".forum_id IN (" . implode(",", $forum_ids) . ") = 1 ";
  1778. } else {
  1779. return '';
  1780. }
  1781. }
  1782. $records = $wpdb->get_results(
  1783. "SELECT DISTINCT forum_id, topic_id
  1784. FROM ".SFPOSTS
  1785. .$where.$where2."
  1786. ORDER BY post_id DESC"
  1787. .$limit.";");
  1788. return $records;
  1789. }
  1790. # ------------------------------------------------------------------
  1791. # sf_filter_new_post_list()
  1792. #
  1793. # Support: Returns filtered list that current user has permissions to
  1794. # $recordset: Full list of forum/topics
  1795. # ------------------------------------------------------------------
  1796. function sf_filter_new_post_list($recordset)
  1797. {
  1798. if(!$recordset) return '';
  1799. $rlist = array();
  1800. $x = 0;
  1801. foreach($recordset as $record)
  1802. {
  1803. $rlist[$x]->forum_id=$record->forum_id;
  1804. $rlist[$x]->topic_id=$record->topic_id;
  1805. $x++;
  1806. }
  1807. return $rlist;
  1808. }
  1809. # ******************************************************************
  1810. # NEW POSTS FROM WAITING - DASHBOARD
  1811. # ******************************************************************
  1812. # ------------------------------------------------------------------
  1813. # sf_get_unread_forums()
  1814. #
  1815. # Returns list from the waiting table (Admins queue)
  1816. # ------------------------------------------------------------------
  1817. function sf_get_unread_forums()
  1818. {
  1819. global $wpdb;
  1820. return $wpdb->get_results(
  1821. "SELECT topic_id, ".SFWAITING.".forum_id, forum_slug, forum_name, group_id, ".SFWAITING.".post_count, ".SFWAITING.".post_id, topic_status_set
  1822. FROM ".SFFORUMS."
  1823. JOIN ".SFWAITING." ON ".SFFORUMS.".forum_id = ".SFWAITING.".forum_id
  1824. WHERE ".SFWAITING.".post_count > 0
  1825. ORDER BY forum_id, topic_id");
  1826. }
  1827. # ------------------------------------------------------------------
  1828. # sf_get_awaiting_approval()
  1829. #
  1830. # Count of posts currently awaiting moderation
  1831. # ------------------------------------------------------------------
  1832. function sf_get_awaiting_approval()
  1833. {
  1834. global $wpdb;
  1835. return $wpdb->get_var(
  1836. "SELECT COUNT(post_id) AS cnt
  1837. FROM ".SFPOSTS."
  1838. WHERE post_status=1");
  1839. }
  1840. # ------------------------------------------------------------------
  1841. # sf_topic_in_queue()
  1842. #
  1843. # returns true if the topic is in the admin queue
  1844. # $topicid topic being tested
  1845. # ------------------------------------------------------------------
  1846. function sf_topic_in_queue($topicid)
  1847. {
  1848. global $wpdb;
  1849. if(!$topicid) return '';
  1850. return $wpdb->get_var(
  1851. "SELECT post_count
  1852. FROM ".SFWAITING."
  1853. WHERE topic_id=".$topicid);
  1854. }
  1855. # ******************************************************************
  1856. # STATISTICS VIEWS DB FUNCTIONS
  1857. # ******************************************************************
  1858. # ------------------------------------------------------------------
  1859. # sf_get_online_total()
  1860. #
  1861. # Returns total number of users currently tagged as online
  1862. # ------------------------------------------------------------------
  1863. function sf_get_online_total()
  1864. {
  1865. global $wpdb;
  1866. return $wpdb->get_var("SELECT COUNT(id) FROM ".SFTRACK);
  1867. }
  1868. # ------------------------------------------------------------------
  1869. # sf_get_members_online()
  1870. #
  1871. # Returns list of members currently tagged as online
  1872. # ------------------------------------------------------------------
  1873. function sf_get_members_online()
  1874. {
  1875. global $wpdb;
  1876. return $wpdb->get_results("
  1877. SELECT trackuserid, display_name, forum_id, topic_id FROM ".SFTRACK."
  1878. JOIN ".SFMEMBERS." ON ".SFTRACK.".trackuserid = ".SFMEMBERS.".user_id
  1879. ORDER BY trackuserid");
  1880. }
  1881. # ------------------------------------------------------------------
  1882. # sf_is_online()
  1883. #
  1884. # Returns true if member is currently tagged as online
  1885. # ------------------------------------------------------------------
  1886. function sf_is_online($userid)
  1887. {
  1888. global $wpdb, $session_online;
  1889. if(!$userid) return '';
  1890. if(!isset($session_online))
  1891. {
  1892. $session_online = $wpdb->get_col("SELECT trackuserid FROM ".SFTRACK);
  1893. }
  1894. if(in_array($userid, $session_online)) return true;
  1895. return false;
  1896. }
  1897. # ------------------------------------------------------------------
  1898. # sf_get_stats_counts()
  1899. #
  1900. # Returns stats on group/forum/topic/post count
  1901. # ------------------------------------------------------------------
  1902. function sf_get_stats_counts()
  1903. {
  1904. global $wpdb;
  1905. $cnt->groups = 0;
  1906. $cnt->forums = 0;
  1907. $cnt->topics = 0;
  1908. $cnt->posts = 0;
  1909. $groupid='';
  1910. $forums = $wpdb->get_results("SELECT group_id, forum_id, topic_count, post_count FROM ".SFFORUMS." ORDER BY group_id");
  1911. if($forums)
  1912. {
  1913. foreach($forums as $forum)
  1914. {
  1915. if($forum->group_id != $groupid)
  1916. {
  1917. $groupid=$forum->group_id;
  1918. $cnt->groups++;
  1919. }
  1920. $cnt->forums++;
  1921. $cnt->topics+=$forum->topic_count;
  1922. $cnt->posts+=$forum->post_count;
  1923. }
  1924. }
  1925. return $cnt;
  1926. }
  1927. # ------------------------------------------------------------------
  1928. # sf_get_post_stats()
  1929. #
  1930. # Returns stats on posts (admins/moderators and members and updates
  1931. # the guest count
  1932. # ------------------------------------------------------------------
  1933. function sf_get_post_stats()
  1934. {
  1935. global $wpdb;
  1936. $sfcontrols = sf_get_option('sfcontrols');
  1937. # if the hour has passed or been reset
  1938. if($sfcontrols['hourflag'] != date('G'))
  1939. {
  1940. # get admins and moderators
  1941. $records1 = $wpdb->get_results("
  1942. SELECT user_id, display_name, posts, admin, moderator
  1943. FROM ".SFMEMBERS."
  1944. WHERE admin=1 OR moderator=1
  1945. ORDER BY admin DESC, moderator DESC, posts DESC;");
  1946. # now get members
  1947. if(empty($sfcontrols['showtopcount']) || $sfcontrols['showtopcount']==0) $sfcontrols['showtopcount']=6;
  1948. $records2 = $wpdb->get_results("
  1949. SELECT SQL_CALC_FOUND_ROWS user_id, display_name, posts, admin, moderator
  1950. FROM ".SFMEMBERS."
  1951. WHERE admin=0 AND moderator=0
  1952. ORDER BY posts DESC
  1953. LIMIT 0,".$sfcontrols['showtopcount'].";");
  1954. # save the members count
  1955. $sfcontrols['membercount'] = $wpdb->get_var("SELECT FOUND_ROWS()");
  1956. # Finally get guests
  1957. $guests = $wpdb->get_col("
  1958. SELECT DISTINCT guest_name
  1959. FROM ".SFPOSTS."
  1960. WHERE guest_name !='';");
  1961. # save the guest count
  1962. $sfcontrols['guestcount'] = count($guests);
  1963. # merge and save the records
  1964. $stats = array_merge($records1, $records2);
  1965. $sfcontrols['statslist'] = $stats;
  1966. # reset the hour flag and save the controls
  1967. $sfcontrols['hourflag'] = date('G');
  1968. sf_update_option('sfcontrols', $sfcontrols);
  1969. } else {
  1970. $stats = $sfcontrols['statslist'];
  1971. }
  1972. # return results
  1973. return $stats;
  1974. }
  1975. # ------------------------------------------------------------------
  1976. # sf_guests_browsing()
  1977. #
  1978. # Calculates how many guests are browsing current forum or topic
  1979. # ------------------------------------------------------------------
  1980. function sf_guests_browsing()
  1981. {
  1982. global $sfvars, $wpdb;
  1983. $where='';
  1984. if($sfvars['pageview']=='forum') $where = "forum_id=".$sfvars['forumid'];
  1985. if($sfvars['pageview']=='topic') $where = "topic_id=".$sfvars['topicid'];
  1986. if($where=='') return;
  1987. return $wpdb->get_var("
  1988. SELECT COUNT(id)
  1989. FROM ".SFTRACK." WHERE trackuserid=0 AND ".$where);
  1990. }
  1991. # ------------------------------------------------------------------
  1992. # sf_update_max_online()
  1993. #
  1994. # Updates max online setting if exceeded and returns value
  1995. # ------------------------------------------------------------------
  1996. function sf_update_max_online($current)
  1997. {
  1998. $sfcontrols = sf_get_option('sfcontrols');
  1999. $max = $sfcontrols['maxonline'];
  2000. if(empty($max)) $max = 0;
  2001. if($current > $max)
  2002. {
  2003. $sfcontrols['maxonline'] = $current;
  2004. sf_update_option('sfcontrols', $sfcontrols);
  2005. return $current;
  2006. }
  2007. return $max;
  2008. }
  2009. # ******************************************************************
  2010. # FULL TOPIC SEARCH VIEW DB FUNCTIONS
  2011. # ******************************************************************
  2012. # ------------------------------------------------------------------
  2013. # sf_get_combined_full_topic_search()
  2014. #
  2015. # Grabs all forums and their topics in the search result.
  2016. # Also performs the full search for a members postings
  2017. # ------------------------------------------------------------------
  2018. function sf_get_combined_full_topic_search()
  2019. {
  2020. global $wpdb, $sfvars, $current_user, $sfglobals;
  2021. $sfvars['searchresults'] = 0;
  2022. # how many topics per page?
  2023. $tpaged = $sfglobals['display']['topics']['perpage'];
  2024. if(!$tpaged) $tpaged=20;
  2025. if($sfvars['searchpage'] == 1)
  2026. {
  2027. $startlimit = 0;
  2028. } else {
  2029. $startlimit = ((($sfvars['searchpage']-1) * $tpaged));
  2030. }
  2031. $LIMIT = ' LIMIT '.$startlimit.', '.$tpaged;
  2032. $ORDER = ' ORDER BY topic_id DESC';
  2033. # for admins search all forums, for users check permissions
  2034. $where2 = '';
  2035. if (!$current_user->forumadmin)
  2036. {
  2037. # limit to viewable forums based on permissions
  2038. $forums = sf_get_forum_memberships($current_user->ID);
  2039. if ($forums)
  2040. {
  2041. $forum_ids = '';
  2042. foreach ($forums as $forum)
  2043. {
  2044. if (sf_can_view_forum($forum->forum_id))
  2045. {
  2046. $forum_ids[] = $forum->forum_id;
  2047. }
  2048. }
  2049. } else {
  2050. return '';
  2051. }
  2052. # create where clause based on forums that current user can view
  2053. if ($forum_ids != '')
  2054. {
  2055. $where2 = " AND ".SFTOPICS.".forum_id IN (" . implode(",", $forum_ids) . ") ";
  2056. } else {
  2057. return '';
  2058. }
  2059. }
  2060. $searchvalue=urldecode($sfvars['searchvalue']);
  2061. if(empty($searchvalue))
  2062. {
  2063. return '';
  2064. }
  2065. if($sfvars['searchtype'] == 8) {
  2066. # topics user has posted in
  2067. $userid = $sfvars['searchvalue'];
  2068. $records = sf_query_results(
  2069. "SELECT SQL_CALC_FOUND_ROWS DISTINCT
  2070. ".SFTOPICS.".topic_id, topic_name, topic_slug, ".SFTOPICS.".forum_id, ".sf_zone_datetime('topic_date').", topic_status, topic_status_flag, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  2071. forum_name, forum_slug
  2072. FROM ".SFTOPICS."
  2073. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  2074. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  2075. WHERE ".SFPOSTS.".user_id = ".$userid.$where2.$ORDER.$LIMIT.";", ARRAY_A);
  2076. } else if($sfvars['searchtype'] == 9) {
  2077. # topics started by user
  2078. $userid = $sfvars['searchvalue'];
  2079. $records = sf_query_results(
  2080. "SELECT SQL_CALC_FOUND_ROWS DISTINCT
  2081. ".SFTOPICS.".topic_id, topic_name, topic_slug, ".SFTOPICS.".forum_id, ".sf_zone_datetime('topic_date').", topic_status, topic_status_flag, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  2082. forum_name, forum_slug, post_content
  2083. FROM ".SFTOPICS."
  2084. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  2085. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  2086. WHERE ".SFPOSTS.".user_id = ".$userid.$where2." AND ".SFPOSTS.".post_index=1 ".$ORDER.$LIMIT.";", ARRAY_A);
  2087. } elseif($sfvars['searchinclude'] == 4) {
  2088. # it's a tag search
  2089. $searchtag = sf_create_slug(urldecode($searchvalue), 'tag');
  2090. $WHERE = SFTOPICS.".topic_id IN (SELECT topic_id FROM ".SFTAGMETA." JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  2091. WHERE tag_slug = '".$searchtag."') ";
  2092. $records = sf_query_results(
  2093. "SELECT SQL_CALC_FOUND_ROWS DISTINCT
  2094. ".SFTOPICS.".topic_id, topic_name, topic_slug, ".SFTOPICS.".forum_id, ".sf_zone_datetime('topic_date').", topic_status, topic_status_flag, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  2095. forum_name, forum_slug, post_content
  2096. FROM ".SFTOPICS."
  2097. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  2098. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  2099. WHERE ".$WHERE." AND ".SFPOSTS.".post_index=1 ".$where2.$ORDER.$LIMIT.";", ARRAY_A);
  2100. } else {
  2101. $searchterm = sf_construct_search_term($searchvalue, $sfvars['searchtype']);
  2102. switch($sfvars['searchinclude'])
  2103. {
  2104. case 1:
  2105. $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)) ";
  2106. break;
  2107. case 2:
  2108. $MATCH = "MATCH(".SFPOSTS.".post_content) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE) ";
  2109. break;
  2110. case 3:
  2111. $MATCH = "MATCH(".SFTOPICS.".topic_name) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE) ";
  2112. break;
  2113. }
  2114. $records = sf_query_results(
  2115. "SELECT SQL_CALC_FOUND_ROWS DISTINCT
  2116. ".SFTOPICS.".topic_id, topic_name, topic_slug, ".SFTOPICS.".forum_id, ".sf_zone_datetime('topic_date').", topic_status, topic_status_flag, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  2117. forum_name, forum_slug, post_status
  2118. FROM ".SFTOPICS."
  2119. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  2120. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  2121. WHERE ".$MATCH.$where2.$ORDER.$LIMIT.";", ARRAY_A);
  2122. }
  2123. if($records)
  2124. {
  2125. $sfvars['searchresults'] = $wpdb->get_var("SELECT FOUND_ROWS()");
  2126. $sfdisplay=array();
  2127. $sfdisplay=sf_get_option('sfdisplay');
  2128. # do we need a post tooltip on the topic link? (Not possible on topics user hs posted in type search)
  2129. if($sfdisplay['topics']['posttip'] && $sfvars['searchtype'] != 8 && $current_user->sfaccess)
  2130. {
  2131. for($x=0; $x<count($records); $x++)
  2132. {
  2133. if (sf_can_view_forum($records[$x]['forum_id']))
  2134. {
  2135. $records[$x]['post_tip']=sf_filter_tooltip_display($records[$x]['post_content'], $records[$x]['post_status']);
  2136. $records[$x]['post_content']='';
  2137. }
  2138. }
  2139. }
  2140. }
  2141. return $records;
  2142. }
  2143. # ******************************************************************
  2144. # USER RELATED DB FUNCTIONS
  2145. # ******************************************************************
  2146. # ------------------------------------------------------------------
  2147. # sf_create_member_data()
  2148. #
  2149. # Filter Call
  2150. # On user registration sets up the new 'members' data row
  2151. # $userid: Passed in to filter
  2152. # ------------------------------------------------------------------
  2153. function sf_create_member_data($userid)
  2154. {
  2155. global $wpdb;
  2156. if(!$userid) return '';
  2157. # Grab the data we need
  2158. $sfprofile = sf_get_option('sfprofile');
  2159. $user = $wpdb->get_row("SELECT user_login, display_name FROM ".SFUSERS." WHERE ID=".$userid);
  2160. # Display Name validation
  2161. $display_name = '';
  2162. # use first and last name (or just one if only one available)
  2163. if($sfprofile['nameformat'] == 3)
  2164. {
  2165. $first = get_user_meta($userid, 'first_name', true);
  2166. $last = get_user_meta($userid, 'last_name', true);
  2167. if(!empty($first) || !empty($last))
  2168. {
  2169. $display_name = trim($first.' '.$last);
  2170. }
  2171. }
  2172. # use the login name (which is also the default if 1 or 3 fail)
  2173. if(empty($display_name) || $sfprofile['nameformat'] == 2)
  2174. {
  2175. $display_name = $user->user_login;
  2176. }
  2177. # use the WP recorded display name
  2178. if($sfprofile['nameformat'] == 1)
  2179. {
  2180. $display_name = $user->display_name;
  2181. }
  2182. # use default login name if all else failed - this MUST exist
  2183. if(empty($display_name)) $display_name = $user->user_login;
  2184. $display_name = sf_filter_name_save($display_name);
  2185. $admin = 0;
  2186. $moderator = 0;
  2187. $pm = 0;
  2188. $avatar = 'a:1:{s:8:"uploaded";s:0:"";}';
  2189. $signature = '';
  2190. $posts = -1;
  2191. $lastvisit = current_time('mysql');
  2192. $checktime = current_time('mysql');
  2193. $subscribe = '';
  2194. $buddies = '';
  2195. $watches = '';
  2196. $newposts = '';
  2197. $posts_rated = '';
  2198. $admin_options = '';
  2199. $useropts = array();
  2200. $sfmemberopts = sf_get_option('sfmemberopts');
  2201. if ($sfmemberopts['sfautosub'])
  2202. {
  2203. $useropts['autosubpost'] = 1;
  2204. } else {
  2205. $useropts['autosubpost'] = 0;
  2206. }
  2207. $useropts['hidestatus'] = 0;
  2208. $useropts['timezone'] = sf_get_option('sfzone');
  2209. if(empty($useropts['timezone'])) $useropts['timezone']=0;
  2210. $useropts['editor'] = 1;
  2211. $useropts['pmemail'] = 1;
  2212. $useropts['namesync'] = 1;
  2213. $sfmemberopts = sf_get_option('sfmemberopts');
  2214. if ($sfmemberopts['sfautosub']) $useropts['autosubpost'] = 1;
  2215. $user_options = serialize($useropts);
  2216. # generate feedkey
  2217. $feedkey = sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  2218. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
  2219. mt_rand( 0, 0x0fff ) | 0x4000,
  2220. mt_rand( 0, 0x3fff ) | 0x8000,
  2221. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
  2222. # save initial record
  2223. $sql = "INSERT INTO ".SFMEMBERS."
  2224. (user_id, display_name, admin, moderator, pm, avatar, signature, posts, lastvisit, checktime, subscribe, buddies, watches, newposts, posts_rated, admin_options, user_options, feedkey)
  2225. VALUES
  2226. ({$userid}, '{$display_name}', {$admin}, {$moderator}, {$pm}, '{$avatar}', '{$signature}', {$posts}, '{$lastvisit}', '{$checktime}', '{$subscribe}', '{$buddies}', '{$watches}', '{$newposts}', '{$posts_rated}', '{$admin_options}', '{$user_options}', '{$feedkey}')";
  2227. $wpdb->query($sql);
  2228. # insert new user into usergroup based on wp role
  2229. sf_map_role_to_ug($userid);
  2230. return;
  2231. }
  2232. # ------------------------------------------------------------------
  2233. # sf_update_member_data()
  2234. #
  2235. # Filter Call
  2236. # On user wp profile updates, check if any spf stuff needs updating
  2237. # $userid: Passed in to filter
  2238. # ------------------------------------------------------------------
  2239. function sf_update_member_data($userid)
  2240. {
  2241. global $wpdb;
  2242. if(!$userid) return '';
  2243. # are we syncing display names between WP and SPF?
  2244. $options = sf_get_member_item($userid, 'user_options');
  2245. if ($options['namesync'] == true)
  2246. {
  2247. $display_name = $wpdb->get_var("SELECT display_name FROM ".SFUSERS." WHERE ID=".$userid);
  2248. sf_update_member_item($userid, 'display_name', $display_name);
  2249. }
  2250. return;
  2251. }
  2252. # ------------------------------------------------------------------
  2253. # sf_map_role_to_ug()
  2254. #
  2255. # helper function
  2256. # update usergroup memberships based on wp role
  2257. # $userid: id of user to check memberships
  2258. # ------------------------------------------------------------------
  2259. function sf_map_role_to_ug($userid, $role='')
  2260. {
  2261. # make sure user has been created first since wp role hook fires before create hook
  2262. if (!$userid || !sf_get_member_item($userid, 'user_id')) return '';
  2263. # get the user's wp role
  2264. if (empty($role))
  2265. {
  2266. $user = new WP_User($userid);
  2267. $roles = $user->roles;
  2268. } else {
  2269. $roles = (array) $role;
  2270. }
  2271. # grab the user group for that wp role
  2272. # if the role doesnt have a mapping, use the defaul user group for new members
  2273. foreach ($roles as $role)
  2274. {
  2275. $value = sf_get_sfmeta('default usergroup', $role);
  2276. if (!empty($value))
  2277. {
  2278. $ug = $value[0]['meta_value'];
  2279. } else {
  2280. $value = sf_get_sfmeta('default usergroup', 'sfmembers');
  2281. $ug = $value[0]['meta_value'];
  2282. }
  2283. sfc_add_membership($ug, $userid);
  2284. }
  2285. return;
  2286. }
  2287. # ------------------------------------------------------------------
  2288. # sf_delete_member_data()
  2289. #
  2290. # Filter Call
  2291. # On user deletion remove 'members' data row
  2292. # $userid: Passed in to filter
  2293. # ------------------------------------------------------------------
  2294. function sf_delete_member_data($userid)
  2295. {
  2296. global $wpdb;
  2297. if(!$userid) return '';
  2298. # 1: get users email address
  2299. $user_email = $wpdb->get_var("SELECT user_email FROM ".SFUSERS." WHERE ID=".$userid);
  2300. # 2: get the users display name from members table
  2301. $display_name = sf_get_member_item($userid, 'display_name');
  2302. # 3: Set user name and email to guest name and meail in all of their posts
  2303. $wpdb->query("UPDATE ".SFPOSTS." SET user_id=NULL, guest_name='".$display_name."', guest_email='".$user_email."' WHERE user_id=".$userid);
  2304. # 4: Remove PM messages
  2305. $wpdb->query("DELETE FROM ".SFMESSAGES." WHERE to_id=".$userid." OR from_id=".$userid);
  2306. # 5: Remove subscriptions
  2307. $subs = sf_get_member_item($userid, 'subscribe');
  2308. if (!empty($subs))
  2309. {
  2310. foreach ($subs as $sub)
  2311. {
  2312. sf_remove_subscription($sub, $userid);
  2313. }
  2314. }
  2315. # 6: Remove watches
  2316. $watches = sf_get_member_item($userid, 'watches');
  2317. if (!empty($watches))
  2318. {
  2319. foreach ($watches as $watch)
  2320. {
  2321. sf_remove_watch($watch, $userid);
  2322. }
  2323. }
  2324. # 7: Remove posts rated
  2325. $ratings = sf_get_member_item($userid, 'posts_rated');
  2326. if (!empty($ratings))
  2327. {
  2328. foreach ($ratings as $postid)
  2329. {
  2330. sf_remove_postrated($postid, $userid);
  2331. }
  2332. }
  2333. # 8: Remove from recent members list if present
  2334. sf_remove_newuser($userid);
  2335. # 8: Remove from Members table
  2336. $wpdb->query("DELETE FROM ".SFMEMBERS." WHERE user_id=".$userid);
  2337. # 9: Remove user group memberships
  2338. $wpdb->query("DELETE FROM ".SFMEMBERSHIPS." WHERE user_id=".$userid);
  2339. return;
  2340. }
  2341. # ------------------------------------------------------------------
  2342. # sf_track_logout()
  2343. #
  2344. # Filter Call
  2345. # Sets up the last visited upon user logout
  2346. # ------------------------------------------------------------------
  2347. function sf_track_logout()
  2348. {
  2349. global $wpdb, $current_user;
  2350. # re-use this for updating lastvisit (time at logout)
  2351. sf_set_last_visited($current_user->ID);
  2352. $wpdb->query("DELETE FROM ".SFTRACK." WHERE trackuserid=".$current_user->ID);
  2353. sf_destroy_users_newposts($current_user->ID);
  2354. return;
  2355. }
  2356. # ------------------------------------------------------------------
  2357. # sf_get_user_id_from_display_name()
  2358. #
  2359. # returns the id of a user
  2360. # $display_name: User to lookup
  2361. # ------------------------------------------------------------------
  2362. function sf_get_user_id_from_display_name($display_name)
  2363. {
  2364. global $wpdb;
  2365. if(!$display_name) return '';
  2366. return $wpdb->get_var(
  2367. "SELECT user_id
  2368. FROM ".SFMEMBERS."
  2369. WHERE display_name='".$display_name."'");
  2370. }
  2371. # ------------------------------------------------------------------
  2372. # sf_get_user_id_from_user_login()
  2373. #
  2374. # returns the id of a user
  2375. # $user_login: User to lookup
  2376. # ------------------------------------------------------------------
  2377. function sf_get_user_id_from_user_login($login_name)
  2378. {
  2379. global $wpdb;
  2380. if(!$login_name) return '';
  2381. return $wpdb->get_var(
  2382. "SELECT ID
  2383. FROM ".SFUSERS."
  2384. WHERE user_login='".$login_name."'");
  2385. }
  2386. # ------------------------------------------------------------------
  2387. # sf_is_subscribed()
  2388. #
  2389. # determine if user already subscribed to topic
  2390. # $userid: User being looked up
  2391. # $topicid: Topic subscribed to
  2392. # ------------------------------------------------------------------
  2393. function sf_is_subscribed($userid, $topicid)
  2394. {
  2395. global $wpdb;
  2396. if(!$userid || !$topicid) return '';
  2397. $list = sf_get_member_item($userid, 'subscribe');
  2398. if (empty($list))
  2399. {
  2400. return false;
  2401. }
  2402. $found = false;
  2403. if (in_array($topicid, $list)) $found = true;
  2404. return $found;
  2405. }
  2406. # ------------------------------------------------------------------
  2407. # sf_is_watching()
  2408. #
  2409. # determine if user already watcing a topic
  2410. # $userid: User being looked up
  2411. # $topicid: Topic watching
  2412. # ------------------------------------------------------------------
  2413. function sf_is_watching($userid, $topicid)
  2414. {
  2415. global $wpdb;
  2416. if(!$userid || !$topicid) return '';
  2417. $list = sf_get_member_item($userid, 'watches');
  2418. if (empty($list))
  2419. {
  2420. return false;
  2421. }
  2422. $found = false;
  2423. if (in_array($topicid, $list)) $found = true;
  2424. return $found;
  2425. }
  2426. # ******************************************************************
  2427. # SAVE ITEM FUNCTIONS
  2428. # ******************************************************************
  2429. # ------------------------------------------------------------------
  2430. # sf_save_edited_post()
  2431. #
  2432. # Saves a forum post following an edit in the UI
  2433. # Values in POST variables
  2434. # ------------------------------------------------------------------
  2435. function sf_save_edited_post()
  2436. {
  2437. global $wpdb, $current_user;
  2438. # post content
  2439. $postcontent = $_POST['postitem'];
  2440. sf_process_hook('sf_hook_pre_edit_post_save', array($_POST["pid"], $postcontent, $current_user->ID));
  2441. $postcontent = sf_filter_content_save($postcontent, 'edit');
  2442. # post edit array
  2443. $postedit = array();
  2444. $pedit = stripslashes($_POST['pedit']);
  2445. if(!empty($pedit))
  2446. {
  2447. $postedit = unserialize($pedit);
  2448. }
  2449. $x = count($postedit);
  2450. $edittime = current_time('mysql');
  2451. $postedit[$x]['by'] = sf_filter_name_save($current_user->display_name);
  2452. $postedit[$x]['at'] = strtotime($edittime);
  2453. $postedit = serialize($postedit);
  2454. $sql = "UPDATE ".SFPOSTS." SET post_content='".$postcontent."', post_edit='".$postedit."' WHERE post_id=".sf_esc_int($_POST["pid"]);
  2455. if($wpdb->query($sql) === false)
  2456. {
  2457. update_sfnotice('sfmessage', '1@'.__("Update Failed!", "sforum"));
  2458. } else {
  2459. update_sfnotice('sfmessage', '0@'.__("Updated Post Saved", "sforum"));
  2460. }
  2461. # is it a blog linked topic post edit?
  2462. if(sf_esc_int($_POST['blogpid']) && $_POST['sfedit'])
  2463. {
  2464. $ID = sf_esc_int($_POST['blogpid']);
  2465. $post_content = $postcontent;
  2466. $post = compact('ID', 'post_content');
  2467. wp_update_post($post);
  2468. }
  2469. sf_process_hook('sf_hook_post_edit_post_save', array($_POST["pid"], $postcontent, $current_user->ID));
  2470. return;
  2471. }
  2472. # ------------------------------------------------------------------
  2473. # sf_save_edited_topic()
  2474. #
  2475. # Saves a topic title following an edit in the UI
  2476. # Values in POST variables
  2477. # ------------------------------------------------------------------
  2478. function sf_save_edited_topic()
  2479. {
  2480. global $wpdb, $sfglobals;
  2481. $topicname = sf_filter_title_save($_POST['topicname']);
  2482. if(empty($_POST['topicslug']))
  2483. {
  2484. $topicslug = sf_create_slug(sf_esc_str($_POST['topicname']), 'topic');
  2485. if(empty($topicslug)) $topicslug = 'topic-'.sf_esc_int($_POST['tid']);
  2486. } else {
  2487. $topicslug = sf_esc_str($_POST['topicslug']);
  2488. }
  2489. $sql = 'UPDATE '.SFTOPICS.' SET topic_name="'.$topicname.'", topic_slug="'.$topicslug.'" WHERE topic_id='.sf_esc_int($_POST['tid']);
  2490. if($wpdb->query($sql) === false)
  2491. {
  2492. update_sfnotice('sfmessage', '1@'.__("Update Failed!", "sforum"));
  2493. } else {
  2494. update_sfnotice('sfmessage', '0@'.__("Updated Topic Title Saved", "sforum"));
  2495. }
  2496. return;
  2497. }
  2498. # ------------------------------------------------------------------
  2499. # sf_save_subscription()
  2500. #
  2501. # Saves a user subscription following an edit in the UI
  2502. # $topicid: The topic being subscribed to
  2503. # $userid: The user (passed because used in upgrade flow)
  2504. # $retmessage: True/False: Whether to retrun message (for UI)
  2505. # ------------------------------------------------------------------
  2506. function sf_save_subscription($topicid, $userid, $retmessage)
  2507. {
  2508. global $wpdb, $current_user;
  2509. if(!$userid || !$topicid) return '';
  2510. if (($current_user->guest) || ($current_user->sfsubscriptions == false)) return;
  2511. # is user already subscribed to this topic?
  2512. if (sf_is_subscribed($userid, $topicid))
  2513. {
  2514. if($retmessage)
  2515. {
  2516. update_sfnotice('sfmessage', '1@'.__('You are already subscribed to this topic', "sforum"));
  2517. return;
  2518. }
  2519. }
  2520. # OK -subscribe them to the topic
  2521. $list = $wpdb->get_var("SELECT topic_subs FROM ".SFTOPICS." WHERE topic_id=".$topicid);
  2522. $list = unserialize($list);
  2523. $list[] = $userid;
  2524. $list = serialize($list);
  2525. $wpdb->query("UPDATE ".SFTOPICS." SET topic_subs = '".$list."' WHERE topic_id=".$topicid);
  2526. # plus note the topic against their usermeta record
  2527. $list = sf_get_member_item($userid, 'subscribe');
  2528. $list[] = $topicid;
  2529. sf_update_member_item($userid, 'subscribe', $list);
  2530. if($retmessage)
  2531. {
  2532. update_sfnotice('sfmessage', '0@'.__('Subscription added', "sforum"));
  2533. }
  2534. return;
  2535. }
  2536. # ------------------------------------------------------------------
  2537. # sf_save_watch()
  2538. #
  2539. # Saves a user watch topic request following an edit in the UI
  2540. # $topicid: The topic being watched
  2541. # $userid: The user (passed because used in upgrade flow)
  2542. # $retmessage: True/False: Whether to retrun message (for UI)
  2543. # ------------------------------------------------------------------
  2544. function sf_save_watch($topicid, $userid, $retmessage)
  2545. {
  2546. global $wpdb, $current_user;
  2547. if(!$userid || !$topicid) return '';
  2548. if (($current_user->guest) || ($current_user->sfwatch == false)) return;
  2549. # is user already watching this topic?
  2550. if (sf_is_watching($userid, $topicid))
  2551. {
  2552. if ($retmessage)
  2553. {
  2554. update_sfnotice('sfmessage', '1@'.__('You are already watching this topic', "sforum"));
  2555. return;
  2556. }
  2557. }
  2558. # OK - watch the topic (in members table)
  2559. $list = sf_get_member_item($userid, 'watches');
  2560. $list[] = $topicid;
  2561. sf_update_member_item($userid, 'watches', $list);
  2562. # OK -subscribe them to the topic
  2563. $list = $wpdb->get_var("SELECT topic_watches FROM ".SFTOPICS." WHERE topic_id=".$topicid);
  2564. $list = unserialize($list);
  2565. $list[] = $userid;
  2566. $list = serialize($list);
  2567. $wpdb->query("UPDATE ".SFTOPICS." SET topic_watches = '".$list."' WHERE topic_id=".$topicid);
  2568. if ($retmessage)
  2569. {
  2570. update_sfnotice('sfmessage', '0@'.__('Topic watch added', "sforum"));
  2571. }
  2572. return;
  2573. }
  2574. # ------------------------------------------------------------------
  2575. # sf_end_watch()
  2576. #
  2577. # Update watches from the UI - current user
  2578. # Values in POST variables
  2579. # ------------------------------------------------------------------
  2580. function sf_end_watch()
  2581. {
  2582. global $current_user, $wpdb, $sfglobals;
  2583. $topic = sf_esc_int($_GET['topic']);
  2584. # remove current watch from member
  2585. $list = $sfglobals['member']['watches'];
  2586. if (!empty($list))
  2587. {
  2588. $newlist = '';
  2589. foreach($list as $topicid)
  2590. {
  2591. if ($topic != $topicid)
  2592. {
  2593. $newlist[] = $topicid;
  2594. }
  2595. }
  2596. sf_update_member_item($current_user->ID, 'watches', $newlist);
  2597. }
  2598. #remove the topic watch
  2599. $list = $wpdb->get_var("SELECT topic_watches FROM ".SFTOPICS." WHERE topic_id=".$topic);
  2600. if (!empty($list))
  2601. {
  2602. $newlist = '';
  2603. $list = unserialize($list);
  2604. foreach ($list as $userid)
  2605. {
  2606. if ($current_user->ID != $userid)
  2607. {
  2608. $newlist[] = $userid;
  2609. }
  2610. }
  2611. if ($newlist != '') $newlist = serialize($newlist);
  2612. $list = $wpdb->query("UPDATE ".SFTOPICS." SET topic_watches ='".$newlist."' WHERE topic_id=".$topic);
  2613. }
  2614. update_sfnotice('sfmessage', '0@'.__('Watches Updated', "sforum"));
  2615. return;
  2616. }
  2617. # ------------------------------------------------------------------
  2618. # sf_end_sub()
  2619. #
  2620. # Update watches from the UI - current user
  2621. # Values in POST variables
  2622. # ------------------------------------------------------------------
  2623. function sf_end_sub()
  2624. {
  2625. global $current_user, $wpdb, $sfglobals;
  2626. $topic = sf_esc_int($_GET['topic']);
  2627. # remove current subscription from member
  2628. $list = $sfglobals['member']['subscribe'];
  2629. if (!empty($list))
  2630. {
  2631. $newlist = '';
  2632. foreach($list as $topicid)
  2633. {
  2634. if ($topic != $topicid)
  2635. {
  2636. $newlist[] = $topicid;
  2637. }
  2638. }
  2639. sf_update_member_item($current_user->ID, 'subscribe', $newlist);
  2640. }
  2641. #remove the topic subscription
  2642. $list = $wpdb->get_var("SELECT topic_subs FROM ".SFTOPICS." WHERE topic_id=".$topic);
  2643. if (!empty($list))
  2644. {
  2645. $newlist = '';
  2646. $list = unserialize($list);
  2647. foreach ($list as $userid)
  2648. {
  2649. if ($current_user->ID != $userid)
  2650. {
  2651. $newlist[] = $userid;
  2652. }
  2653. }
  2654. if ($newlist != '') $newlist = serialize($newlist);
  2655. $list = $wpdb->query("UPDATE ".SFTOPICS." SET topic_subs ='".$newlist."' WHERE topic_id=".$topic);
  2656. }
  2657. update_sfnotice('sfmessage', '0@'.__('Subscriptions Updated', "sforum"));
  2658. return;
  2659. }
  2660. # ------------------------------------------------------------------
  2661. # sf_remove_watch($topic, $userid)
  2662. #
  2663. # removes the topic watch for the specified user
  2664. # $topic topic to be removed
  2665. # $userid user to have watch removed
  2666. # ------------------------------------------------------------------
  2667. function sf_remove_watch($topic, $userid)
  2668. {
  2669. global $wpdb, $sfglobals;
  2670. if(!$userid || !$topic) return '';
  2671. # remove current watch from member
  2672. $list = $sfglobals['member']['watches'];
  2673. if (!empty($list))
  2674. {
  2675. $newlist = '';
  2676. foreach($list as $topicid)
  2677. {
  2678. if ($topic != $topicid)
  2679. {
  2680. $newlist[] = $topicid;
  2681. }
  2682. }
  2683. sf_update_member_item($userid, 'watches', $newlist);
  2684. }
  2685. #remove the topic subscription
  2686. $list = $wpdb->get_var("SELECT topic_watches FROM ".SFTOPICS." WHERE topic_id=".$topic);
  2687. if (!empty($list))
  2688. {
  2689. $newlist = '';
  2690. $list = unserialize($list);
  2691. foreach ($list as $user)
  2692. {
  2693. if ($userid != $user)
  2694. {
  2695. $newlist[] = $user;
  2696. }
  2697. }
  2698. if ($newlist != '') $newlist = serialize($newlist);
  2699. $list = $wpdb->query("UPDATE ".SFTOPICS." SET topic_watches ='".$newlist."' WHERE topic_id=".$topic);
  2700. }
  2701. return;
  2702. }
  2703. # ------------------------------------------------------------------
  2704. # sf_remove_user_watches($userid)
  2705. #
  2706. # removes all topic watches for the specified user
  2707. # $userid user to have all watches removed
  2708. # ------------------------------------------------------------------
  2709. function sf_remove_user_watches($userid)
  2710. {
  2711. global $current_user;
  2712. # make sure user is doing this
  2713. if ($userid == $current_user->ID)
  2714. {
  2715. # Remove watches
  2716. $watches = sf_get_member_item($userid, 'watches');
  2717. if (!empty($watches))
  2718. {
  2719. foreach ($watches as $watch)
  2720. {
  2721. sf_remove_watch($watch, $userid);
  2722. }
  2723. }
  2724. }
  2725. }
  2726. # ------------------------------------------------------------------
  2727. # sf_remove_user_subs($userid)
  2728. #
  2729. # removes all topic subs for the specified user
  2730. # $userid user to have subscriptions removed
  2731. # ------------------------------------------------------------------
  2732. function sf_remove_user_subs($userid)
  2733. {
  2734. global $current_user;
  2735. # make sure user is doing this
  2736. if ($userid == $current_user->ID)
  2737. {
  2738. # Remove subscriptions
  2739. $subs = sf_get_member_item($userid, 'subscribe');
  2740. if (!empty($subs))
  2741. {
  2742. foreach ($subs as $sub)
  2743. {
  2744. sf_remove_subscription($sub, $userid);
  2745. }
  2746. }
  2747. }
  2748. }
  2749. # ------------------------------------------------------------------
  2750. # sf_add_postrating_vote()
  2751. #
  2752. # Saves a user watch topic request following an edit in the UI
  2753. # $postid: The post being voted on
  2754. # ------------------------------------------------------------------
  2755. function sf_add_postrating_vote($postid)
  2756. {
  2757. global $current_user, $sfglobals;
  2758. if(!$postid) return '';
  2759. # record the post as voted (in members table)
  2760. $list = $sfglobals['member']['posts_rated'];
  2761. $list[] = $postid;
  2762. sf_update_member_item($current_user->ID, 'posts_rated', $list);
  2763. return;
  2764. }
  2765. # ------------------------------------------------------------------
  2766. # sf_remove_postrated($topic, $userid)
  2767. #
  2768. # removes the post rated id for the specified user
  2769. # $postid postid to be removed
  2770. # $userid user to have watche removed
  2771. # ------------------------------------------------------------------
  2772. function sf_remove_postrated($postid, $userid)
  2773. {
  2774. global $wpdb;
  2775. if(!$userid || !$postid) return '';
  2776. #remove the member id from post rated
  2777. $list = $wpdb->get_var("SELECT members FROM ".SFPOSTRATINGS." WHERE post_id=".$postid);
  2778. if (!empty($list))
  2779. {
  2780. $newlist = null;
  2781. $list = unserialize($list);
  2782. foreach ($list as $user)
  2783. {
  2784. if ($userid != $user)
  2785. {
  2786. $newlist[] = $user;
  2787. }
  2788. }
  2789. if ($newlist) $newlist = serialize($newlist);
  2790. $list = $wpdb->query("UPDATE ".SFPOSTRATINGS." SET members ='".$newlist."' WHERE post_id=".$postid);
  2791. }
  2792. return;
  2793. }
  2794. # ------------------------------------------------------------------
  2795. # sf_move_topic()
  2796. #
  2797. # Move topic from one forum to another
  2798. # Values in POST variables
  2799. # ------------------------------------------------------------------
  2800. function sf_move_topic()
  2801. {
  2802. global $wpdb, $current_user;
  2803. if(!$current_user->sfmovetopics)
  2804. {
  2805. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  2806. return;
  2807. }
  2808. if(empty($_POST['forumid']))
  2809. {
  2810. update_sfnotice('sfmessage', '1@'.__('Destination Forum not Selected', "sforum"));
  2811. return;
  2812. }
  2813. $currentforumid = sf_esc_int($_POST['currentforumid']);
  2814. $currenttopicid = sf_esc_int($_POST['currenttopicid']);
  2815. $targetforumid = sf_esc_int($_POST['forumid']);
  2816. # change topic record to new forum id
  2817. $wpdb->query("UPDATE ".SFTOPICS." SET forum_id = ".$targetforumid." WHERE topic_id=".$currenttopicid);
  2818. if($wpdb === false)
  2819. {
  2820. update_sfnotice('sfmessage', '1@'.__("Topic Move Failed", "sforum"));
  2821. return;
  2822. }
  2823. # check in 'waiting' to see if there is an unread post in there and change forum id if there is
  2824. $wpdb->query("UPDATE ".SFWAITING." SET forum_id = ".$targetforumid." WHERE topic_id=".$currenttopicid);
  2825. # change posts record(s) to new forum
  2826. $wpdb->query("UPDATE ".SFPOSTS." SET forum_id = ".$targetforumid." WHERE topic_id=".$currenttopicid);
  2827. # rebuild forum counts for old and new forums
  2828. sf_build_forum_index($currentforumid);
  2829. sf_build_forum_index($targetforumid);
  2830. # relink to new forum if blog linked topic
  2831. sf_relink_topic($currenttopicid, $currenttopicid, $targetforumid);
  2832. # Ok - do not like doing this but....
  2833. # There seems to have been times when a new post is made to the old forum id so we will now double check...
  2834. $checkposts = $wpdb->get_results("SELECT post_id FROM ".SFPOSTS." WHERE forum_id=".$currentforumid." AND topic_id=".$currenttopicid);
  2835. if($checkposts)
  2836. {
  2837. # made after most were moved
  2838. sf_move_topic();
  2839. } else {
  2840. if($wpdb === false)
  2841. {
  2842. update_sfnotice('sfmessage', '1@'.__("Topic Move Failed", "sforum"));
  2843. } else {
  2844. update_sfnotice('sfmessage', '0@'.__("Topic Moved", "sforum"));
  2845. }
  2846. }
  2847. return;
  2848. }
  2849. # ------------------------------------------------------------------
  2850. # sf_move_post()
  2851. #
  2852. # Move post from one topic and create new topic -same/another forum
  2853. # Values in POST variables
  2854. # ------------------------------------------------------------------
  2855. function sf_move_post()
  2856. {
  2857. global $wpdb, $current_user;
  2858. if(!$current_user->sfmoveposts)
  2859. {
  2860. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  2861. return;
  2862. }
  2863. if(empty($_POST['forumid']))
  2864. {
  2865. update_sfnotice('sfmessage', '1@'.__("Post move abandoned - No Forum selected!", "sforum"));
  2866. return;
  2867. }
  2868. if(empty($_POST['newtopicname']))
  2869. {
  2870. update_sfnotice('sfmessage', '1@'.__("Post move abandoned - No Topic Defined!", "sforum"));
  2871. return;
  2872. }
  2873. # extract data from POST
  2874. $postid = sf_esc_int($_POST['postid']);
  2875. $oldtopicid = sf_esc_int($_POST['oldtopicid']);
  2876. $oldforumid = sf_esc_int($_POST['oldforumid']);
  2877. $newforumid = sf_esc_int($_POST['forumid']);
  2878. $blogpostid = sf_esc_int($_POST['blogpostid']);
  2879. $newtopicname = sf_filter_title_save(trim(($_POST['newtopicname'])));
  2880. # start with creating the new topic
  2881. $newtopicslug = sf_create_slug($newtopicname, 'topic');
  2882. # now create the topic and post records
  2883. $wpdb->query(
  2884. "INSERT INTO ".SFTOPICS."
  2885. (topic_name, topic_slug, topic_date, forum_id, post_count, blog_post_id, post_id)
  2886. VALUES
  2887. ('".$newtopicname."', '".$newtopicslug."', now(), ".$newforumid.", 1, '".$blogpostid."', ".$postid.");");
  2888. if($wpdb === false)
  2889. {
  2890. update_sfnotice('sfmessage', '1@'.__("Post Move Failed", "sforum"));
  2891. return;
  2892. }
  2893. $newtopicid = $wpdb->insert_id;
  2894. # check the topic slug and if empty use the topic id
  2895. if(empty($newtopicslug))
  2896. {
  2897. $newtopicslug = 'topic-'.$newtopicid;
  2898. $thistopic = $wpdb->query("UPDATE ".SFTOPICS." SET topic_slug='".$slug."' WHERE topic_id=".$newtopicid);
  2899. }
  2900. # now check if old topic had just the one post and if so remove it
  2901. $check = $wpdb->get_var("SELECT post_count FROM ".SFTOPICS." WHERE topic_id=".$oldtopicid);
  2902. if($check == 1)
  2903. {
  2904. $wpdb->query("DELETE FROM ".SFTOPICS." WHERE topic_id=".$oldtopicid);
  2905. }
  2906. # update post record
  2907. $wpdb->query(
  2908. "UPDATE ".SFPOSTS."
  2909. SET topic_id=".$newtopicid.", forum_id=".$newforumid.", post_index=1, post_status=0
  2910. WHERE post_id=".$postid);
  2911. # If old topic was in the admin queue then remove it. Assume it's read
  2912. sf_remove_from_waiting(true, $oldtopicid, 0);
  2913. # rebuild forum counts for old and new forums
  2914. sf_build_forum_index($oldforumid);
  2915. sf_build_forum_index($newforumid);
  2916. sf_build_post_index($oldtopicid, sf_get_topic_slug($oldtopicid));
  2917. # relink to new forum if blog linked topic and this was first post
  2918. if($blogpostid)
  2919. {
  2920. sf_relink_topic($oldtopicid, $newtopicid, $newforumid);
  2921. }
  2922. if($wpdb == false)
  2923. {
  2924. update_sfnotice('sfmessage', '1@'.__("Post Move Failed", "sforum"));
  2925. } else {
  2926. update_sfnotice('sfmessage', '0@'.__("Post Moved", "sforum"));
  2927. }
  2928. return;
  2929. }
  2930. # ------------------------------------------------------------------
  2931. # sf_relink_topic()
  2932. #
  2933. # Relinks topic after a move
  2934. # $topicid original topic ID
  2935. # $newtopicid new id if moved post
  2936. # $newforumid new forum id
  2937. # ------------------------------------------------------------------
  2938. function sf_relink_topic($topicid, $newtopicid, $newforumid)
  2939. {
  2940. global $wpdb;
  2941. # Check if the target topic is a linked topic
  2942. $link = $wpdb->get_row("SELECT * FROM ".SFLINKS." WHERE topic_id=".$topicid);
  2943. if($link)
  2944. {
  2945. sf_blog_links_control('update', $link->post_id, $newforumid, $newtopicid, $link->syncedit);
  2946. }
  2947. return;
  2948. }
  2949. function sf_reassign_post()
  2950. {
  2951. global $wpdb, $current_user;
  2952. if (!$current_user->sfreassign)
  2953. {
  2954. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  2955. return;
  2956. }
  2957. $postid = sf_esc_int($_POST['postid']);
  2958. $olduserid = sf_esc_int($_POST['olduserid']);
  2959. $newuserid = sf_esc_int($_POST['newuserid']);
  2960. # transfer the post
  2961. $wpdb->query(
  2962. "UPDATE ".SFPOSTS."
  2963. SET user_id=".$newuserid."
  2964. WHERE post_id=".$postid);
  2965. # update old user post counts
  2966. $count = sf_get_member_item($olduserid, 'posts') - 1;
  2967. sf_update_member_item($olduserid, 'posts', $count);
  2968. # update new user post counts
  2969. $count = sf_get_member_item($newuserid, 'posts') + 1;
  2970. sf_update_member_item($newuserid, 'posts', $count);
  2971. }
  2972. function sf_change_topic_status()
  2973. {
  2974. global $wpdb, $current_user;
  2975. if(!$current_user->sfedit)
  2976. {
  2977. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  2978. return;
  2979. }
  2980. $topicid= sf_esc_int($_POST['id']);
  2981. $statvalue = sf_esc_int($_POST['statvalue']);
  2982. sf_update_topic_status_flag($statvalue, $topicid);
  2983. update_sfnotice('sfmessage', '0@'.__("Topic Status Changed", "sforum"));
  2984. return;
  2985. }
  2986. # ------------------------------------------------------------------
  2987. # sf_update_opened()
  2988. #
  2989. # Updates the number of times a topic is viewed
  2990. # $topicid: The topic being opened for view
  2991. # ------------------------------------------------------------------
  2992. function sf_update_opened($topicid)
  2993. {
  2994. global $wpdb, $sfvars;
  2995. if(empty($topicid)) return;
  2996. $ref=array();
  2997. $ref=explode('/', $_SERVER['HTTP_REFERER']);
  2998. $ref_topic = $ref[count($ref)-3];
  2999. if(substr($ref_topic,0,5) == 'page-') $ref_topic = $ref[count($ref)-4];
  3000. if($ref_topic == $sfvars['topicslug']) return;
  3001. $current=$wpdb->get_var("SELECT topic_opened FROM ".SFTOPICS." WHERE topic_id=".$topicid);
  3002. $current++;
  3003. $wpdb->query("UPDATE ".SFTOPICS." SET topic_opened = ".$current." WHERE topic_id=".$topicid);
  3004. return;
  3005. }
  3006. # ******************************************************************
  3007. # DELETE ITEM FUNCTIONS
  3008. # ******************************************************************
  3009. # ------------------------------------------------------------------
  3010. # sf_delete_topic()
  3011. #
  3012. # Delete a topic and all it;s posts
  3013. # $topicid: The topic being subscribed to
  3014. # $show: True/False: Whether to return message (for UI)
  3015. # ------------------------------------------------------------------
  3016. function sf_delete_topic($topicid, $show=true)
  3017. {
  3018. global $wpdb, $current_user;
  3019. if(!$topicid) return '';
  3020. if (!$current_user->sfdelete && !sf_is_forum_admin($current_user->ID) && !$current_user->sfdeleteown)
  3021. {
  3022. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  3023. return;
  3024. }
  3025. # We need to check for subscriptions on this topic
  3026. $subs = $wpdb->get_var("SELECT topic_subs FROM ".SFTOPICS." WHERE topic_id = ".$topicid);
  3027. # Any subscriptions to remopve from user records?
  3028. if ($subs)
  3029. {
  3030. $userlist = unserialize($subs);
  3031. foreach ($userlist as $user)
  3032. {
  3033. $topiclist = sf_get_member_item($user, 'subscribe');
  3034. if (!empty($topiclist))
  3035. {
  3036. $newlist = '';
  3037. foreach ($topiclist as $topic)
  3038. {
  3039. if ($topic != $topicid) $newlist[] = $topic;
  3040. }
  3041. sf_update_member_item($user, 'subscribe', $newlist);
  3042. }
  3043. }
  3044. }
  3045. # Any watches to remove?
  3046. $watches = $wpdb->get_var("SELECT topic_watches FROM ".SFTOPICS." WHERE topic_id = ".$topicid);
  3047. if ($watches)
  3048. {
  3049. $userlist = unserialize($watches);
  3050. foreach ($userlist as $user)
  3051. {
  3052. $topiclist = sf_get_member_item($user, 'watches');
  3053. if (!empty($topiclist))
  3054. {
  3055. $newlist = '';
  3056. foreach ($topiclist as $topic)
  3057. {
  3058. if ($topic != $topicid) $newlist[] = $topic;
  3059. }
  3060. sf_update_member_item($user, 'watches', $newlist);
  3061. }
  3062. }
  3063. }
  3064. # remove any post ratings in this topic
  3065. $rated = $wpdb->get_results("SELECT ".SFPOSTRATINGS.".post_id, members FROM ".SFPOSTRATINGS."
  3066. JOIN ".SFPOSTS." ON ".SFPOSTRATINGS.".post_id = ".SFPOSTS.".post_id
  3067. WHERE ".SFPOSTS.".topic_id=".$topicid);
  3068. if ($rated)
  3069. {
  3070. foreach ($rated as $post)
  3071. {
  3072. # remove the post rating
  3073. $wpdb->query("DELETE FROM ".SFPOSTRATINGS." WHERE post_id=".$post->post_id);
  3074. # remove the posts rated data from sfmembers table
  3075. $members = unserialize($post->members);
  3076. if ($members)
  3077. {
  3078. # loop through each members that rated a post in this topic and remove the rating
  3079. foreach ($members as $member)
  3080. {
  3081. $ratedposts = $wpdb->get_var("SELECT posts_rated FROM ".SFMEMBERS." WHERE user_id=".$member);
  3082. if ($ratedposts)
  3083. {
  3084. # save any other rated posts only removing posts from this topic
  3085. $new_ratings = '';
  3086. $ratedposts = unserialize($ratedposts);
  3087. foreach ($ratedposts as $ratedpost)
  3088. {
  3089. if ($ratedpost != $post->post_id)
  3090. {
  3091. $new_ratings[] = $ratedpost;
  3092. }
  3093. }
  3094. if ($new_ratings == '')
  3095. {
  3096. $wpdb->query("UPDATE ".SFMEMBERS." SET posts_rated=NULL WHERE user_id=".$member);
  3097. } else {
  3098. $new_ratings = serialize($new_ratings);
  3099. $wpdb->query("UPDATE ".SFMEMBERS." SET posts_rated='".$new_ratings."' WHERE user_id=".$member);
  3100. }
  3101. }
  3102. }
  3103. }
  3104. }
  3105. }
  3106. # check of there is a post link to it?
  3107. $row = $wpdb->get_row("SELECT blog_post_id, forum_id FROM ".SFTOPICS." WHERE topic_id = ".$topicid);
  3108. if($row->blog_post_id != 0)
  3109. {
  3110. # break the link
  3111. include_once(SF_PLUGIN_DIR.'/linking/library/sf-links-support.php');
  3112. sf_blog_links_control('delete', $row->blog_post_id);
  3113. }
  3114. # delete from waiting just in case
  3115. $wpdb->query("DELETE FROM ".SFWAITING." WHERE topic_id=".$topicid);
  3116. # now delete from topic
  3117. $wpdb->query("DELETE FROM ".SFTOPICS." WHERE topic_id=".$topicid);
  3118. if($wpdb === false)
  3119. {
  3120. if($show) update_sfnotice('sfmessage', '1@'.__("Deletion Failed", "sforum"));
  3121. return;
  3122. }
  3123. # topic delete hook
  3124. if(function_exists('sf_hook_topic_delete'))
  3125. {
  3126. # grab the forum id
  3127. $forumid = $wpdb->get_var("SELECT forum_id FROM ".SFTOPICS." WHERE topic_id = ".$topicid);
  3128. sf_process_hook('sf_hook_topic_delete', array($topicid, $forumid));
  3129. }
  3130. # now delete all the posts on the topic
  3131. $wpdb->query("DELETE FROM ".SFPOSTS." WHERE topic_id=".$topicid);
  3132. if($wpdb == false)
  3133. {
  3134. if($show) update_sfnotice('sfmessage', '1@'.__("Deletion of Posts in Topic Failed", "sforum"));
  3135. } else {
  3136. if($show) update_sfnotice('sfmessage', '0@'.__("Topic Deleted", "sforum"));
  3137. }
  3138. # delete from forums topic count
  3139. sf_build_forum_index($row->forum_id);
  3140. return;
  3141. }
  3142. # ------------------------------------------------------------------
  3143. # sf_delete_post()
  3144. #
  3145. # Delete a post
  3146. # $postid: The post to be deleted
  3147. # $topicid: The topic post belongs to
  3148. # $forumid: The forum post belongs to
  3149. # ------------------------------------------------------------------
  3150. function sf_delete_post($postid, $topicid, $forumid, $show=true, $poster=0)
  3151. {
  3152. global $wpdb, $current_user;
  3153. if(!$postid || !$topicid || !$forumid) return '';
  3154. if(!$current_user->sfdelete && !($current_user->sfdeleteown && $current_user->ID == $poster))
  3155. {
  3156. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  3157. return;
  3158. }
  3159. # Check post actually exsists - might be a browsser refresh!
  3160. $target = $wpdb->get_var("SELECT post_id FROM ".SFPOSTS." WHERE post_id=".$postid);
  3161. if(empty($target))
  3162. {
  3163. if($show) update_sfnotice('sfmessage', '0@'.__("Post Already Deleted", "sforum"));
  3164. return;
  3165. }
  3166. # if just one post then remove topic as well
  3167. if(sf_get_posts_count_in_topic($topicid) == 1)
  3168. {
  3169. sf_delete_topic($topicid);
  3170. } else {
  3171. # remove post ratings
  3172. $rated = $wpdb->get_var("SELECT members FROM ".SFPOSTRATINGS." WHERE post_id=".$postid);
  3173. if ($rated)
  3174. {
  3175. $members = unserialize($rated);
  3176. if ($members)
  3177. {
  3178. # loop through each members that rated a post in this topic and remove the rating
  3179. foreach ($members as $member)
  3180. {
  3181. $ratedposts = $wpdb->get_var("SELECT posts_rated FROM ".SFMEMBERS." WHERE user_id=".$member);
  3182. if ($ratedposts)
  3183. {
  3184. # save any other rated posts only removing posts from this topic
  3185. $new_ratings = '';
  3186. $ratedposts = unserialize($ratedposts);
  3187. foreach ($ratedposts as $ratedpost)
  3188. {
  3189. if ($ratedpost != $postid)
  3190. {
  3191. $new_ratings[] = $ratedpost;
  3192. }
  3193. }
  3194. if ($new_ratings == '')
  3195. {
  3196. $wpdb->query("UPDATE ".SFMEMBERS." SET posts_rated=NULL WHERE user_id=".$member);
  3197. } else {
  3198. $new_ratings = serialize($new_ratings);
  3199. $wpdb->query("UPDATE ".SFMEMBERS." SET posts_rated='".$new_ratings."' WHERE user_id=".$member);
  3200. }
  3201. }
  3202. }
  3203. }
  3204. }
  3205. $wpdb->query("DELETE FROM ".SFPOSTRATINGS." WHERE post_id=".$postid);
  3206. $wpdb->query("DELETE FROM ".SFPOSTS." WHERE post_id=".$postid);
  3207. if($wpdb === false)
  3208. {
  3209. if($show) update_sfnotice('sfmessage', '1@'.__("Deletion Failed", "sforum"));
  3210. } else {
  3211. if($show) update_sfnotice('sfmessage', '0@'.__("Post Deleted", "sforum"));
  3212. }
  3213. # re number post index
  3214. sf_build_post_index($topicid, sf_get_topic_slug($topicid));
  3215. sf_build_forum_index($forumid);
  3216. }
  3217. # post delete hook
  3218. sf_process_hook('sf_hook_post_delete', array($postid, $topicid, $forumid));
  3219. # need to look in sfwaiting to see if it's in there...
  3220. sf_remove_from_waiting(true, $topicid, $postid);
  3221. return;
  3222. }
  3223. # ------------------------------------------------------------------
  3224. # sf_remove_subscription()
  3225. #
  3226. # Removes a user subscription following edit or topic delete.
  3227. # $topicid: The topic being unsubscribed from
  3228. # $userid: The user (passed because used in upgrade flow)
  3229. # ------------------------------------------------------------------
  3230. function sf_remove_subscription($topic, $userid)
  3231. {
  3232. global $wpdb, $sfglobals;
  3233. if(!$userid || !$topic) return '';
  3234. # remove current subscription from member
  3235. $list = sf_get_member_item($userid, 'subscribe');
  3236. if (!empty($list))
  3237. {
  3238. $newlist = '';
  3239. foreach ($list as $topicid)
  3240. {
  3241. if ($topic != $topicid)
  3242. {
  3243. $newlist[] = $topicid;
  3244. }
  3245. }
  3246. sf_update_member_item($userid, 'subscribe', $newlist);
  3247. }
  3248. $list = $wpdb->get_var("SELECT topic_subs FROM ".SFTOPICS." WHERE topic_id=".$topic);
  3249. if (!empty($list))
  3250. {
  3251. $newlist = '';
  3252. $list = unserialize($list);
  3253. foreach ($list as $user)
  3254. {
  3255. if ($userid != $user)
  3256. {
  3257. $newlist[] = $user;
  3258. }
  3259. }
  3260. if ($newlist != '') $newlist = serialize($newlist);
  3261. $wpdb->query("UPDATE ".SFTOPICS." SET topic_subs = '".$newlist."' WHERE topic_id=".$topic);
  3262. }
  3263. return;
  3264. }
  3265. # ******************************************************************
  3266. # EDIT TOOL ICONS
  3267. # ******************************************************************
  3268. # ------------------------------------------------------------------
  3269. # sf_icon_toggle()
  3270. #
  3271. # Toggle Tool Icon State
  3272. # ------------------------------------------------------------------
  3273. function sf_icon_toggle()
  3274. {
  3275. global $sfglobals;
  3276. $sfadminsettings = array();
  3277. $sfadminsettings = sf_get_option('sfadminsettings');
  3278. $state = $sfadminsettings['sftools'];
  3279. if ($state ? $state = false : $state = true);
  3280. $sfadminsettings['sftools'] = $state;
  3281. sf_update_option('sfadminsettings', $sfadminsettings);
  3282. $sfglobals['admin']['sftools'] = $state;
  3283. return;
  3284. }
  3285. # ------------------------------------------------------------------
  3286. # sf_lock_topic_toggle()
  3287. #
  3288. # Toggle Topic Lock
  3289. # Topicid: Topic to lock/unlock
  3290. # ------------------------------------------------------------------
  3291. function sf_lock_topic_toggle($topicid)
  3292. {
  3293. global $wpdb, $current_user;
  3294. if(!$topicid) return '';
  3295. if(!$current_user->sflock)
  3296. {
  3297. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  3298. return;
  3299. }
  3300. if($_POST['locktopicaction'].$topicid == get_sfsetting('sfaction')) return;
  3301. $status = $wpdb->get_var("SELECT topic_status FROM ".SFTOPICS." WHERE topic_id=".sf_esc_int($topicid));
  3302. if($status == 1 ? $status=0 : $status=1);
  3303. $wpdb->query("UPDATE ".SFTOPICS." SET topic_status = ".$status." WHERE topic_id=".sf_esc_int($topicid));
  3304. if($wpdb == false)
  3305. {
  3306. update_sfnotice('sfmessage', '1@'.__("Topic Lock Toggle Failed", "sforum"));
  3307. } else {
  3308. update_sfnotice('sfmessage', '0@'.__("Topic Lock Toggled", "sforum"));
  3309. update_sfsetting('sfaction', sf_esc_str($_POST['locktopicaction'].$topicid));
  3310. }
  3311. return;
  3312. }
  3313. # ------------------------------------------------------------------
  3314. # sf_pin_topic_toggle()
  3315. #
  3316. # Toggle Topic Pin
  3317. # Topicid: Topic to pin/unpin
  3318. # ------------------------------------------------------------------
  3319. function sf_pin_topic_toggle($topicid)
  3320. {
  3321. global $wpdb, $current_user;
  3322. if(!$topicid) return '';
  3323. if(!$current_user->sfpintopics)
  3324. {
  3325. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  3326. return;
  3327. }
  3328. if($_POST['pintopicaction'].$topicid == get_sfsetting('sfaction')) return;
  3329. $status = $wpdb->get_var("SELECT topic_pinned FROM ".SFTOPICS." WHERE topic_id=".sf_esc_int($topicid));
  3330. if($status == 1 ? $status=0 : $status=1);
  3331. $wpdb->query("UPDATE ".SFTOPICS." SET topic_pinned = ".$status." WHERE topic_id=".sf_esc_int($topicid));
  3332. if($wpdb == false)
  3333. {
  3334. update_sfnotice('sfmessage', '1@'.__("Topic Pin Toggle Failed", "sforum"));
  3335. } else {
  3336. update_sfnotice('sfmessage', '0@'.__("Topic Pin Toggled", "sforum"));
  3337. update_sfsetting('sfaction', sf_esc_str($_POST['pintopicaction'].$topicid));
  3338. }
  3339. return;
  3340. }
  3341. # ------------------------------------------------------------------
  3342. # sf_pin_post_toggle()
  3343. #
  3344. # Toggle Post Pin
  3345. # postid: Post to pin/unpin
  3346. # ------------------------------------------------------------------
  3347. function sf_pin_post_toggle($postid)
  3348. {
  3349. global $wpdb, $current_user;
  3350. if(!$postid) return '';
  3351. if(!$current_user->sfpinposts)
  3352. {
  3353. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  3354. return;
  3355. }
  3356. if($_POST['pinpostaction'].$postid == get_sfsetting('sfaction')) return;
  3357. $status = $wpdb->get_var("SELECT post_pinned FROM ".SFPOSTS." WHERE post_id=".$postid);
  3358. if($status == 1 ? $status = 0 : $status = 1);
  3359. $wpdb->query("UPDATE ".SFPOSTS." SET post_pinned = ".$status." WHERE post_id=".sf_esc_int($postid));
  3360. if($wpdb == false)
  3361. {
  3362. update_sfnotice('sfmessage', '1@'.__("Post Pin Toggle Failed", "sforum"));
  3363. } else {
  3364. update_sfnotice('sfmessage', '0@'.__("Post Pin Toggled", "sforum"));
  3365. update_sfsetting('sfaction', sf_esc_str($_POST['pinpostaction']).$postid);
  3366. }
  3367. return;
  3368. }
  3369. # ******************************************************************
  3370. # DATA INTEGRITY MANAGEMENT
  3371. # ******************************************************************
  3372. # ------------------------------------------------------------------
  3373. # sf_build_post_index()
  3374. #
  3375. # Rebuilds the post index column (post sequence) and also sets the
  3376. # last post id and post count into the parent topic record
  3377. # $topicid: topic whose posts are being re-indexed
  3378. # $topicslug: slug to check sort order
  3379. # ------------------------------------------------------------------
  3380. function sf_build_post_index($topicid, $topicslug, $returnmsg=false)
  3381. {
  3382. global $wpdb;
  3383. if(!$topicid || !$topicslug) return '';
  3384. # get topic posts is their display order
  3385. $posts=$wpdb->get_results("SELECT post_id, post_index FROM ".SFPOSTS." WHERE topic_id = ".$topicid." ORDER BY post_pinned DESC, post_id ASC");
  3386. if($posts)
  3387. {
  3388. $index = 1;
  3389. foreach($posts as $post)
  3390. {
  3391. # update the post_index for each post to set display order
  3392. $wpdb->query("UPDATE ".SFPOSTS." SET post_index = ".$index." WHERE post_id = ".$post->post_id);
  3393. $index++;
  3394. }
  3395. $lastpost = $post->post_id;
  3396. } else {
  3397. $lastpost = 'NULL';
  3398. }
  3399. # update the topic with the last post id and the post count
  3400. $wpdb->query("UPDATE ".SFTOPICS." SET post_id=".$lastpost.", post_count=".($index-1)." WHERE topic_id=".$topicid);
  3401. if($returnmsg) update_sfnotice('sfmessage', '0@'.__("Verification Complete", "sforum"));
  3402. return;
  3403. }
  3404. # ------------------------------------------------------------------
  3405. # sf_build_forum_index()
  3406. #
  3407. # Rebuilds the topic count and last post id in a forum record
  3408. # $forumid: forum needing updating
  3409. # ------------------------------------------------------------------
  3410. function sf_build_forum_index($forumid, $returnmsg=false)
  3411. {
  3412. global $wpdb;
  3413. if(!$forumid) return '';
  3414. # get the topic and post counts for this forum
  3415. $topiccount = $wpdb->get_var("SELECT COUNT(topic_id) FROM ".SFTOPICS." WHERE forum_id=".$forumid);
  3416. $postcount = $wpdb->get_var("SELECT COUNT(post_id) FROM ".SFPOSTS." WHERE forum_id=".$forumid);
  3417. # get the last post that appeared in a topic within this forum
  3418. $postid = $wpdb->get_var("SELECT post_id FROM ".SFPOSTS." WHERE forum_id = ".$forumid." ORDER BY post_id DESC LIMIT 1");
  3419. if(!$topiccount) $topiccount = 0;
  3420. if(!$postcount) $postcount = 0;
  3421. if(!isset($postid)) $postid = 'NULL';
  3422. # update forum record
  3423. $wpdb->query("UPDATE ".SFFORUMS." SET post_id=".$postid.", post_count=".$postcount.", topic_count=".$topiccount." WHERE forum_id=".$forumid);
  3424. if($returnmsg) update_sfnotice('sfmessage', '0@'.__("Verification Complete", "sforum"));
  3425. return;
  3426. }
  3427. function sf_get_user_memberships($user_id)
  3428. {
  3429. global $wpdb;
  3430. if(!$user_id) return '';
  3431. $sql = "SELECT ".SFMEMBERSHIPS.".usergroup_id, usergroup_name, usergroup_desc
  3432. FROM ".SFMEMBERSHIPS."
  3433. JOIN ".SFUSERGROUPS." ON ".SFUSERGROUPS.".usergroup_id = ".SFMEMBERSHIPS.".usergroup_id
  3434. WHERE user_id=".$user_id;
  3435. return $wpdb->get_results($sql, ARRAY_A);
  3436. }
  3437. function sf_check_membership($usergroup_id, $user_id)
  3438. {
  3439. global $wpdb;
  3440. if(!$usergroup_id || !$user_id) return '';
  3441. $sql = "SELECT usergroup_id
  3442. FROM ".SFMEMBERSHIPS."
  3443. WHERE user_id=".$user_id." AND usergroup_id=".$usergroup_id;
  3444. return $wpdb->get_results($sql, ARRAY_A);
  3445. }
  3446. function sf_get_pm_inbox_new_count($userid)
  3447. {
  3448. global $wpdb, $sfglobals;
  3449. if(!$userid) return '';
  3450. return $wpdb->get_var("SELECT COUNT(*) FROM ".SFMESSAGES." WHERE to_id = ".$userid." AND inbox=1 AND message_status=0");
  3451. }
  3452. function sf_change_topic_tags($topicid, $newtags)
  3453. {
  3454. global $wpdb, $sfglobals;
  3455. if(!$topicid || !$newtags) return '';
  3456. # remove any existing tags for the topic
  3457. $oldtags = $wpdb->get_results("SELECT tag_id FROM ".SFTAGMETA." WHERE topic_id=".$topicid);
  3458. foreach ($oldtags as $oldtag)
  3459. {
  3460. # grab all the tag rows and decrement the tag count
  3461. $tagcount = $wpdb->get_var("SELECT tag_count FROM ".SFTAGS." WHERE tag_id=".$oldtag->tag_id);
  3462. # decrement tag count and delete if it gets to zero or update the new count
  3463. $tagcount--;
  3464. if ($tagcount == 0)
  3465. {
  3466. $wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$oldtag->tag_id); # count is zero so delete
  3467. } else {
  3468. $wpdb->query("UPDATE ".SFTAGS." SET tag_count=".$tagcount." WHERE tag_id=".$oldtag->tag_id); # update count
  3469. }
  3470. # remove all the tag meta entries for the topic
  3471. $wpdb->query("DELETE FROM ".SFTAGMETA." WHERE topic_id=".$topicid);
  3472. }
  3473. # now add in the updated tags
  3474. $tags = trim($newtags);
  3475. $tags = trim($tags, ','); # no extra commas allowed
  3476. $tags = explode(',', $tags);
  3477. $tags = array_unique($tags); # remove any duplicates
  3478. $tags = array_values($tags); # put back in order
  3479. if ($sfglobals['display']['topics']['maxtags'] > 0 && count($tags) > $sfglobals['display']['topics']['maxtags'])
  3480. {
  3481. $tags = array_slice($tags, 0, $sfglobals['display']['topics']['maxtags']); # limit to maxt tags opton
  3482. }
  3483. sfc_add_tags($topicid, $tags);
  3484. }
  3485. function sf_mark_all_read()
  3486. {
  3487. global $wpdb, $current_user;
  3488. # just to be safe, make sure a member called
  3489. if ($current_user->member)
  3490. {
  3491. sf_destroy_users_newposts($current_user->ID);
  3492. }
  3493. }
  3494. # ------------------------------------------------------------------
  3495. # sf_get_blog_title_from_id()
  3496. #
  3497. # Returns the post title of a blog post from the post id
  3498. # $postid: blog post target id
  3499. # ------------------------------------------------------------------
  3500. function sf_get_blog_title_from_id($postid)
  3501. {
  3502. global $wpdb;
  3503. if(!$postid) return '';
  3504. return $wpdb->get_var(
  3505. "SELECT post_title
  3506. FROM ".$wpdb->prefix."posts
  3507. WHERE ID=".$postid);
  3508. }
  3509. # ------------------------------------------------------------------
  3510. # sf_query_results()
  3511. #
  3512. # Returns the query if good - displays error if sql invalid
  3513. # 4.2 Just used on two search queries
  3514. # $sql: query string
  3515. # $type: return type - defaults to OBJECT
  3516. # ------------------------------------------------------------------
  3517. function sf_query_results($sql, $type=OBJECT)
  3518. {
  3519. global $wpdb;
  3520. $wpdb->hide_errors();
  3521. $records = $wpdb->get_results($sql, $type);
  3522. if($wpdb->last_error == '')
  3523. {
  3524. return $records;
  3525. } else {
  3526. update_sfnotice('sfmessage', '1@'.__("Invalid Database Query", "sforum"));
  3527. echo sf_render_queued_message();
  3528. return '';
  3529. }
  3530. }
  3531. ?>