PageRenderTime 76ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/openfarmtech/weblog-content
PHP | 4078 lines | 2634 code | 590 blank | 854 comment | 527 complexity | 4cee442dba5d7797a932258a26ec28a4 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.0, LGPL-3.0, BSD-3-Clause, GPL-3.0, LGPL-2.1, AGPL-3.0, CC-BY-SA-3.0
  1. <?php
  2. /*
  3. Simple:Press
  4. Main database routines
  5. $LastChangedDate: 2010-08-14 04:52:34 -0700 (Sat, 14 Aug 2010) $
  6. $Rev: 4424 $
  7. */
  8. if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
  9. die('Access Denied');
  10. }
  11. # ******************************************************************
  12. # GROUP/FORUM VIEW AND GENERAL DB FUNCTIONS
  13. # ******************************************************************
  14. # ------------------------------------------------------------------
  15. # sf_get_combined_groups_and_forums($groupid)
  16. #
  17. # Grabs all groups and forums. Note that the group data is repeated.
  18. # Used to populate 'Select Forum Quicklinks' and Front Main page
  19. # of forum (Group/Forum View)
  20. # $groupid: Optional id to display just a single group
  21. # ------------------------------------------------------------------
  22. function sf_get_combined_groups_and_forums($groupid = null)
  23. {
  24. global $wpdb, $session_groups;
  25. If(is_null($groupid) ? $where='' : $where = " WHERE ".SFGROUPS.".group_id=".$groupid." ");
  26. # retrieve group and forum records
  27. $records = $wpdb->get_results(
  28. "SELECT ".SFGROUPS.".group_id, group_name, group_desc, group_rss, group_icon, group_message,
  29. forum_id, forum_name, forum_slug, forum_desc, forum_status, forum_icon, forum_rss_private, post_id, topic_count, parent, children
  30. FROM ".SFGROUPS."
  31. JOIN ".SFFORUMS." ON ".SFGROUPS.".group_id = ".SFFORUMS.".group_id
  32. ".$where."
  33. ORDER BY group_seq, forum_seq;");
  34. # rebuild into an array grabbing permissions on the way
  35. $groups=array();
  36. if($records)
  37. {
  38. # Set initially to Access Denied in case current user can view no forums
  39. $groups[0]['group_id'] = "Access Denied";
  40. $gindex=-1;
  41. $findex=0;
  42. foreach($records as $record)
  43. {
  44. $groupid=$record->group_id;
  45. $forumid=$record->forum_id;
  46. if(sf_can_view_forum($forumid))
  47. {
  48. if($gindex == -1 || $groups[$gindex]['group_id'] != $groupid)
  49. {
  50. $gindex++;
  51. $findex=-1;
  52. $groups[$gindex]['group_id']=$record->group_id;
  53. $groups[$gindex]['group_name']=$record->group_name;
  54. $groups[$gindex]['group_desc']=$record->group_desc;
  55. $groups[$gindex]['group_rss']=$record->group_rss;
  56. $groups[$gindex]['group_icon']=$record->group_icon;
  57. $groups[$gindex]['group_message']=$record->group_message;
  58. }
  59. if(isset($record->forum_id))
  60. {
  61. $groups[$gindex]['forums'][$findex]['forum_id']=$record->forum_id;
  62. $groups[$gindex]['forums'][$findex]['forum_name']=$record->forum_name;
  63. $groups[$gindex]['forums'][$findex]['forum_slug']=$record->forum_slug;
  64. $groups[$gindex]['forums'][$findex]['forum_desc']=$record->forum_desc;
  65. $groups[$gindex]['forums'][$findex]['forum_status']=$record->forum_status;
  66. $groups[$gindex]['forums'][$findex]['forum_icon']=$record->forum_icon;
  67. $groups[$gindex]['forums'][$findex]['forum_rss_private']=$record->forum_rss_private;
  68. $groups[$gindex]['forums'][$findex]['post_id']=$record->post_id;
  69. $groups[$gindex]['forums'][$findex]['topic_count']=$record->topic_count;
  70. $groups[$gindex]['forums'][$findex]['parent']=$record->parent;
  71. $groups[$gindex]['forums'][$findex]['children']=$record->children;
  72. $findex++;
  73. }
  74. }
  75. }
  76. } else {
  77. $records = sf_get_groups_all(false, false);
  78. if($records)
  79. {
  80. foreach($records as $record)
  81. {
  82. $groups[$gindex]['group_id']=$record->group_id;
  83. $groups[$gindex]['group_name']=$record->group_name;
  84. $groups[$gindex]['group_desc']=$record->group_desc;
  85. $groups[$gindex]['group_rss']=$record->group_rss;
  86. $groups[$gindex]['group_icon']=$record->group_icon;
  87. $groups[$gindex]['group_message']=$record->group_message;
  88. $gindex++;
  89. }
  90. }
  91. }
  92. $session_groups = $groups;
  93. return $groups;
  94. }
  95. # ------------------------------------------------------------------
  96. # sf_get_combined_groups_and_forums_bloglink()
  97. #
  98. # Grabs all groups and forums. Soecial cut down version for
  99. # populating the blog link add post drop down
  100. # ------------------------------------------------------------------
  101. function sf_get_combined_groups_and_forums_bloglink()
  102. {
  103. global $wpdb, $current_user;
  104. # retrieve group and forum records
  105. $records = $wpdb->get_results(
  106. "SELECT ".SFGROUPS.".group_id, group_name,
  107. forum_id, forum_name
  108. FROM ".SFGROUPS."
  109. JOIN ".SFFORUMS." ON ".SFGROUPS.".group_id = ".SFFORUMS.".group_id
  110. ".$where."
  111. ORDER BY group_seq, forum_seq;");
  112. # rebuild into an array grabbing permissions on the way
  113. $groups=array();
  114. $gindex=-1;
  115. $findex=0;
  116. if($records)
  117. {
  118. foreach($records as $record)
  119. {
  120. $groupid=$record->group_id;
  121. $forumid=$record->forum_id;
  122. if (sf_user_can($current_user->ID, 'Can create linked topics', $forumid) && sf_user_can($current_user->ID, 'Can start new topics', $forumid))
  123. {
  124. if($gindex == -1 || $groups[$gindex]['group_id'] != $groupid)
  125. {
  126. $gindex++;
  127. $findex=0;
  128. $groups[$gindex]['group_id']=$record->group_id;
  129. $groups[$gindex]['group_name']=$record->group_name;
  130. }
  131. if(isset($record->forum_id))
  132. {
  133. $groups[$gindex]['forums'][$findex]['forum_id']=$record->forum_id;
  134. $groups[$gindex]['forums'][$findex]['forum_name']=$record->forum_name;
  135. $findex++;
  136. }
  137. }
  138. }
  139. }
  140. return $groups;
  141. }
  142. function sf_get_combined_forum_stats($posts)
  143. {
  144. global $wpdb;
  145. $clause='';
  146. if($posts)
  147. {
  148. $pcount = count($posts);
  149. $done = 0;
  150. foreach ($posts as $post)
  151. {
  152. $clause.= "(".SFPOSTS.".post_id=".$post.")";
  153. $done++;
  154. if($done < $pcount) $clause.= " OR ";
  155. }
  156. } else {
  157. $record=array();
  158. $record['topic_count'] = '0';
  159. $record['post_count'] = '0';
  160. $record['udate'] = '';
  161. return $record;
  162. }
  163. $records = $wpdb->get_results(
  164. "SELECT ".SFPOSTS.".post_id, ".SFPOSTS.".topic_id, topic_name, ".SFPOSTS.".forum_id, ".sf_zone_datetime('post_date').",
  165. UNIX_TIMESTAMP(post_date) as udate, guest_name, ".SFPOSTS.".user_id, post_content, post_status, ".SFMEMBERS.".display_name, post_index,
  166. ".SFFORUMS.".topic_count, ".SFFORUMS.".post_count, topic_slug
  167. FROM ".SFPOSTS."
  168. LEFT JOIN ".SFFORUMS." ON ".SFPOSTS.".forum_id = ".SFFORUMS.".forum_id
  169. LEFT JOIN ".SFTOPICS." ON ".SFPOSTS.".topic_id = ".SFTOPICS.".topic_id
  170. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  171. WHERE ".$clause.";", ARRAY_A);
  172. if($records)
  173. {
  174. $sfdisplay=array();
  175. $sfdisplay=sf_get_option('sfdisplay');
  176. $forums = array();
  177. foreach($records as $record)
  178. {
  179. $forums[$record['forum_id']] = $record;
  180. # do we need a post tooltip on the topic link?
  181. if($sfdisplay['topics']['posttip'])
  182. {
  183. $forums[$record['forum_id']]['post_tip']=sf_filter_tooltip_display($record['post_content'], $record['post_status']);
  184. $forums[$record['forum_id']]['post_content']='';
  185. }
  186. }
  187. }
  188. return $forums;
  189. }
  190. # ------------------------------------------------------------------
  191. # sf_get_group_record()
  192. #
  193. # Returns a single group row
  194. # $groupid: group_id of group to return
  195. # $asArray: return as an array if true
  196. # Note: No permission checking is performed
  197. # ------------------------------------------------------------------
  198. function sf_get_group_record($groupid, $asArray=false)
  199. {
  200. global $wpdb;
  201. if(!$groupid) return '';
  202. $sql=(
  203. "SELECT *
  204. FROM ".SFGROUPS."
  205. WHERE group_id=".$groupid.";");
  206. if($asArray) return $wpdb->get_row($sql, ARRAY_A);
  207. return $wpdb->get_row($sql);
  208. }
  209. # ------------------------------------------------------------------
  210. # sf_get_groups_all()
  211. #
  212. # Return ALL group records - no permission checking
  213. # $id_only: Optionsal = return just ids
  214. # $asArray: Optional - return as array
  215. # ------------------------------------------------------------------
  216. function sf_get_groups_all($id_only=false, $asArray=false)
  217. {
  218. global $wpdb;
  219. if($id_only ? $FROM='group_id' : $FROM='*');
  220. $sql=("SELECT ".$FROM." FROM ".SFGROUPS." ORDER BY group_seq");
  221. if($asArray) return $wpdb->get_results($sql, ARRAY_A);
  222. return $wpdb->get_results($sql);
  223. }
  224. # ------------------------------------------------------------------
  225. # sf_group_exists()
  226. #
  227. # Check the existence of a group by id
  228. # $groupid: group to check for
  229. # ------------------------------------------------------------------
  230. function sf_group_exists($groupid)
  231. {
  232. global $wpdb;
  233. if(empty($groupid)) return false;
  234. if($wpdb->get_var(
  235. "SELECT group_name
  236. FROM ".SFGROUPS."
  237. WHERE group_id=".$groupid))
  238. {
  239. return true;
  240. }
  241. return false;
  242. }
  243. # ------------------------------------------------------------------
  244. # sf_get_group_rss_url()
  245. #
  246. # Returns the RSS feed URL for a Group (custom or standard)
  247. # $groupid: group to return
  248. # ------------------------------------------------------------------
  249. function sf_get_group_rss_url($groupid)
  250. {
  251. global $wpdb, $sfglobals;
  252. if (empty($groupid)) return '';
  253. $url = $wpdb->get_var("SELECT group_rss FROM ".SFGROUPS." WHERE group_id=".$groupid);
  254. if (empty($url))
  255. {
  256. $rssopt = sf_get_option('sfrss');
  257. if ($rssopt['sfrssfeedkey'] && isset($sfglobals['member']['feedkey']))
  258. $url = sf_build_qurl('group='.$groupid, 'xfeed=group', 'feedkey='.$sfglobals['member']['feedkey']);
  259. else
  260. $url = sf_build_qurl('group='.$groupid, 'xfeed=group');
  261. }
  262. return $url;
  263. }
  264. # ------------------------------------------------------------------
  265. # sf_get_group_name_from_forum()
  266. #
  267. # Returns the Group Name when only the forum id is known
  268. # $forumid: forum to lookup for group name
  269. # ------------------------------------------------------------------
  270. function sf_get_group_name_from_forum($forumid)
  271. {
  272. global $wpdb;
  273. if(!$forumid) return '';
  274. return $wpdb->get_var(
  275. "SELECT ".SFGROUPS.".group_name
  276. FROM ".SFGROUPS."
  277. JOIN ".SFFORUMS." ON ".SFFORUMS.".group_id = ".SFGROUPS.".group_id
  278. WHERE ".SFFORUMS.".forum_id=".$forumid);
  279. }
  280. # ******************************************************************
  281. # FORUM/TOPIC VIEW AND GENERAL DB FUNCTIONS
  282. # ******************************************************************
  283. # ------------------------------------------------------------------
  284. # sf_get_combined_forums_and_topics($forumid)
  285. #
  286. # Grabs all forums and their topics. Note that the forum data is
  287. # repeated. Used to populate Topic Listing page of topics
  288. # (Forum/Topics View)
  289. # $forumid: forum id to display
  290. # $currentpage: index to paging
  291. # ------------------------------------------------------------------
  292. function sf_get_combined_forums_and_topics($forumid, $currentpage)
  293. {
  294. global $wpdb, $sfvars, $sfglobals;
  295. if(!$forumid) return '';
  296. $sfvars['searchresults'] = 0;
  297. # rebuild into an array
  298. $forums=array();
  299. # Set initially to Access Denied in case current user can view no forums
  300. $forums[0]['forum_id']="Access Denied";
  301. # quick permission check
  302. if(!sf_can_view_forum($forumid)) return $forums;
  303. # some setup vars
  304. $startlimit = 0;
  305. # get post tooltip state
  306. $sfdisplay = array();
  307. $sfdisplay = sf_get_option('sfdisplay');
  308. # how many topics per page?
  309. $tpaged = $sfglobals['display']['topics']['perpage'];
  310. if(!$tpaged) $tpaged=20;
  311. # setup where we are in the topic list (paging)
  312. if($sfvars['searchpage'] == 0)
  313. {
  314. if($currentpage != 1)
  315. {
  316. $startlimit = ((($currentpage-1) * $tpaged));
  317. }
  318. } else {
  319. if($sfvars['searchpage'] == 1)
  320. {
  321. $currentpage = 1;
  322. } else {
  323. $startlimit = ((($sfvars['searchpage']-1) * $tpaged));
  324. }
  325. }
  326. $LIMIT = " LIMIT ".$startlimit.', '.$tpaged;
  327. if($sfvars['searchpage'] == 0)
  328. {
  329. $topicsort = $sfglobals['display']['topics']['sortnewtop'];
  330. if ($topicsort)
  331. {
  332. $ORDER = " ORDER BY topic_pinned DESC, ".SFTOPICS.".post_id DESC";
  333. } else {
  334. $ORDER = " ORDER BY topic_pinned DESC, ".SFTOPICS.".post_id ASC";
  335. }
  336. } else {
  337. $ORDER = " ORDER BY ".SFTOPICS.".post_id DESC";
  338. }
  339. if($sfvars['searchpage'] == 0)
  340. {
  341. # standar forum view
  342. $SELECT = "SELECT ";
  343. $MATCH = "";
  344. $ANDWHERE = "";
  345. } else {
  346. $searchvalue=urldecode($sfvars['searchvalue']);
  347. if(empty($searchvalue))
  348. {
  349. return '';
  350. }
  351. # what sort of search is it?
  352. if($sfvars['searchtype'] == 6) {
  353. # topic status search
  354. $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
  355. $MATCH = "";
  356. $ANDWHERE = " AND topic_status_flag=".$sfvars['searchvalue']." ";
  357. } elseif($sfvars['searchtype'] == 8) {
  358. # members 'posted in' sepcified forum search
  359. $userid = $sfvars['searchvalue'];
  360. $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
  361. $MATCH = "";
  362. $ANDWHERE = " AND ".SFPOSTS.".user_id=".$userid." ";
  363. } elseif($sfvars['searchtype'] == 9) {
  364. # members 'started' in sepcified forum search
  365. $userid = $sfvars['searchvalue'];
  366. $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
  367. $MATCH = "";
  368. $ANDWHERE = " AND ".SFPOSTS.".user_id=".$userid." ";
  369. } elseif($sfvars['searchinclude'] == 4) {
  370. # it's a tag search
  371. $searchtag = sf_create_slug($searchvalue, 'tag');
  372. $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
  373. $MATCH = SFTOPICS.".topic_id IN (SELECT topic_id FROM ".SFTAGMETA." JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  374. WHERE tag_slug = '".$searchtag."' AND forum_id=".$forumid.") AND ";
  375. $ANDWHERE = "";
  376. } else {
  377. # general keyword search
  378. $SELECT = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ";
  379. $searchterm = sf_construct_search_term($searchvalue, $sfvars['searchtype']);
  380. switch($sfvars['searchinclude'])
  381. {
  382. case 1:
  383. $MATCH = "(MATCH(".SFPOSTS.".post_content) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE) OR MATCH(".SFTOPICS.".topic_name) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE)) AND ";
  384. break;
  385. case 2:
  386. $MATCH = "MATCH(".SFPOSTS.".post_content) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE) AND ";
  387. break;
  388. case 3:
  389. $MATCH = "MATCH(".SFTOPICS.".topic_name) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE) AND ";
  390. break;
  391. }
  392. $ANDWHERE = "";
  393. }
  394. }
  395. # retrieve forum and topic records - is a type 8 then we need a separate query (for now)
  396. if($sfvars['searchtype'] == 8) # NO post_content or post_index = 1
  397. {
  398. $records = sf_query_results(
  399. $SELECT.SFFORUMS.".forum_id, forum_slug, forum_name, forum_status, group_id, topic_count, forum_icon, forum_desc, topic_status_set,
  400. parent, children, forum_message,
  401. ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
  402. topic_status, topic_pinned, topic_opened, topic_subs, topic_watches, topic_status_flag,
  403. post_ratings, use_tags, blog_post_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count, ".SFTOPICS.".user_id, post_status
  404. FROM ".SFFORUMS."
  405. JOIN ".SFTOPICS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  406. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  407. WHERE ".$MATCH.SFFORUMS.".forum_id=".$forumid.$ANDWHERE.$ORDER.$LIMIT.";");
  408. } else {
  409. $records = sf_query_results(
  410. $SELECT.SFFORUMS.".forum_id, forum_slug, forum_name, forum_status, group_id, topic_count, forum_icon, forum_desc, topic_status_set,
  411. parent, children, forum_message,
  412. ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
  413. topic_status, topic_pinned, topic_opened, topic_subs, topic_watches, topic_status_flag,
  414. post_ratings, use_tags, blog_post_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count, ".SFTOPICS.".user_id, post_content, post_status
  415. FROM ".SFFORUMS."
  416. JOIN ".SFTOPICS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  417. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  418. WHERE ".$MATCH.SFFORUMS.".forum_id=".$forumid.$ANDWHERE." AND ".SFPOSTS.".post_index=1 ".$ORDER.$LIMIT.";");
  419. }
  420. if($sfvars['searchpage'] && $records)
  421. {
  422. $sfvars['searchresults'] = $wpdb->get_var("SELECT FOUND_ROWS()");
  423. }
  424. $findex=-1;
  425. $tindex=0;
  426. if($records)
  427. {
  428. $topiclist = '';
  429. foreach($records as $record)
  430. {
  431. $forumid=$record->forum_id;
  432. if($findex == -1 || $forums[$findex]['forum_id'] != $forumid)
  433. {
  434. $findex++;
  435. $tindex=0;
  436. $forums[$findex]['forum_id']=$record->forum_id;
  437. $forums[$findex]['forum_slug']=$record->forum_slug;
  438. $forums[$findex]['forum_name']=$record->forum_name;
  439. $forums[$findex]['forum_desc']=$record->forum_desc;
  440. $forums[$findex]['forum_status']=$record->forum_status;
  441. $forums[$findex]['group_id']=$record->group_id;
  442. $forums[$findex]['topic_count']=$record->topic_count;
  443. $forums[$findex]['forum_icon']=$record->forum_icon;
  444. $forums[$findex]['topic_status_set']=$record->topic_status_set;
  445. $forums[$findex]['post_ratings']=$record->post_ratings;
  446. $forums[$findex]['use_tags']=$record->use_tags;
  447. $forums[$findex]['parent']=$record->parent;
  448. $forums[$findex]['children']=$record->children;
  449. $forums[$findex]['forum_message']=$record->forum_message;
  450. }
  451. $forums[$findex]['topics'][$tindex]['topic_id']=$record->topic_id;
  452. $forums[$findex]['topics'][$tindex]['topic_slug']=$record->topic_slug;
  453. $forums[$findex]['topics'][$tindex]['topic_name']=$record->topic_name;
  454. $forums[$findex]['topics'][$tindex]['topic_date']=$record->topic_date;
  455. $forums[$findex]['topics'][$tindex]['topic_status']=$record->topic_status;
  456. $forums[$findex]['topics'][$tindex]['topic_pinned']=$record->topic_pinned;
  457. $forums[$findex]['topics'][$tindex]['topic_opened']=$record->topic_opened;
  458. $forums[$findex]['topics'][$tindex]['topic_subs']=$record->topic_subs;
  459. $forums[$findex]['topics'][$tindex]['topic_watches']=$record->topic_watches;
  460. $forums[$findex]['topics'][$tindex]['topic_status_flag']=$record->topic_status_flag;
  461. $forums[$findex]['topics'][$tindex]['post_ratings']=$record->post_ratings;
  462. $forums[$findex]['topics'][$tindex]['use_tags']=$record->use_tags;
  463. $forums[$findex]['topics'][$tindex]['blog_post_id']=$record->blog_post_id;
  464. $forums[$findex]['topics'][$tindex]['post_id']=$record->post_id;
  465. $forums[$findex]['topics'][$tindex]['post_count']=$record->post_count;
  466. $forums[$findex]['topics'][$tindex]['user_id']=$record->user_id;
  467. # do we need a post tooltip on the topic link?
  468. if($sfdisplay['topics']['posttip'] && $sfvars['searchtype'] != 8)
  469. {
  470. $forums[$findex]['topics'][$tindex]['post_tip'] = sf_filter_tooltip_display($record->post_content, $record->post_status);
  471. } else {
  472. $forums[$findex]['topics'][$tindex]['post_tip']="";
  473. }
  474. # save topic in list for getting tags later
  475. $topiclist[] = $record->topic_id;
  476. $tindex++;
  477. }
  478. # Now get tags in one query for these topics
  479. $topiclist = implode(',', $topiclist);
  480. $sql = "SELECT tag_name, tag_slug, topic_id
  481. FROM ".SFTAGS."
  482. JOIN ".SFTAGMETA." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  483. WHERE topic_id IN (".$topiclist.")
  484. ORDER BY topic_id";
  485. $tags = $wpdb->get_results($sql);
  486. # Now sort the tags into the monsterous forum array
  487. if ($tags)
  488. {
  489. foreach ($forums as $findex => $forum)
  490. {
  491. foreach ($forum['topics'] as $tindex => $topic)
  492. {
  493. if ($topic['use_tags'])
  494. {
  495. $topictags = '';
  496. foreach ($tags as $tag)
  497. {
  498. if ($topic['topic_id'] == $tag->topic_id)
  499. {
  500. # build tag list for this forum/topic
  501. $topictags[] = $tag;
  502. }
  503. }
  504. # save the tags into the master array
  505. $forums[$findex]['topics'][$tindex]['tags'] = $topictags;
  506. }
  507. }
  508. }
  509. }
  510. } else {
  511. $record = sf_get_forum_record($forumid);
  512. if($record)
  513. {
  514. $forums[0]['forum_id']=$record->forum_id;
  515. $forums[0]['forum_slug']=$record->forum_slug;
  516. $forums[0]['forum_name']=$record->forum_name;
  517. $forums[0]['forum_desc']=$record->forum_desc;
  518. $forums[0]['forum_status']=$record->forum_status;
  519. $forums[0]['group_id']=$record->group_id;
  520. $forums[0]['topic_count']=$record->topic_count;
  521. $forums[0]['forum_icon']=$record->forum_icon;
  522. $forums[0]['topic_status_set']=$record->topic_status_set;
  523. $forums[0]['post_ratings']=$record->post_ratings;
  524. $forums[0]['use_tags']=$record->use_tags;
  525. $forums[0]['parent']=$record->parent;
  526. $forums[0]['children']=$record->children;
  527. $forums[0]['forum_message']=$record->forum_message;
  528. }
  529. }
  530. return $forums;
  531. }
  532. function sf_get_subforums($sublist)
  533. {
  534. global $wpdb;
  535. if (!$sublist) return '';
  536. $subforums = unserialize($sublist);
  537. $forumlist = array();
  538. foreach ($subforums as $forumid)
  539. {
  540. if (sf_can_view_forum($forumid)) $forumlist[] = $forumid;
  541. }
  542. if (empty($forumlist)) return '';
  543. $where = " WHERE forum_id IN (" . implode(",", $forumlist) . ")";
  544. $subforums = $wpdb->get_results("SELECT * FROM ".SFFORUMS.$where." ORDER BY forum_seq", ARRAY_A);
  545. return $subforums;
  546. }
  547. # ------------------------------------------------------------------
  548. # sf_get_combined_topic_stats()
  549. #
  550. # Returns the first and last post data for a topic
  551. # $postid: post id from new posts list
  552. # $user_id for checking of user has posted in topic -
  553. # ------------------------------------------------------------------
  554. function sf_get_combined_topic_stats($posts, $userid=0)
  555. {
  556. global $wpdb;
  557. $clause='';
  558. if($posts)
  559. {
  560. $pcount = count($posts);
  561. $done = 0;
  562. foreach ($posts as $topic=>$postindex)
  563. {
  564. $clause.= "(topic_id = ".$topic." AND post_index = 1 OR topic_id = ".$topic." AND post_index=".$postindex.")";
  565. $done++;
  566. if($done < $pcount) $clause.= " OR ";
  567. }
  568. } else {
  569. return;
  570. }
  571. $records = $wpdb->get_results(
  572. "SELECT post_id, topic_id, forum_id, ".sf_zone_datetime('post_date').", UNIX_TIMESTAMP(post_date) as udate, guest_name, ".SFPOSTS.".user_id, post_index, post_status, post_content, ".SFMEMBERS.".display_name
  573. FROM ".SFPOSTS."
  574. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  575. WHERE ".$clause."
  576. ORDER BY post_id ASC;", ARRAY_A);
  577. # If forum view are we looking to see if user has posted in ant of the topics?
  578. if($records && $userid)
  579. {
  580. $clause='';
  581. $pcount = count($posts);
  582. $done = 0;
  583. foreach ($posts as $topic=>$postindex)
  584. {
  585. $clause.= "(topic_id = ".$topic." AND user_id = ".$userid.")";
  586. $done++;
  587. if($done < $pcount) $clause.= " OR ";
  588. }
  589. $userposted = $wpdb->get_results(
  590. "SELECT DISTINCT topic_id, user_id
  591. FROM ".SFPOSTS."
  592. WHERE ".$clause.";");
  593. } else {
  594. $userposted = '';
  595. }
  596. # Now to format and combine results
  597. if ($records)
  598. {
  599. $sfdisplay=array();
  600. $sfdisplay=sf_get_option('sfdisplay');
  601. $topics = array();
  602. foreach ($records as $record)
  603. {
  604. $topics[$record['topic_id']][$record['post_index']] = $record;
  605. # do we need a post tooltip on the topic link?
  606. if($sfdisplay['topics']['posttip'])
  607. {
  608. $topics[$record['topic_id']][$record['post_index']]['post_tip']=sf_filter_tooltip_display($record['post_content'], $record['post_status']);
  609. $topics[$record['topic_id']][$record['post_index']]['post_content']='';
  610. }
  611. }
  612. foreach ($records as $record)
  613. {
  614. $topics[$record['topic_id']]['thisuser'] = false;
  615. if ($userposted)
  616. {
  617. foreach ($userposted as $user)
  618. {
  619. if ($user->topic_id == $record['topic_id'])
  620. {
  621. $topics[$record['topic_id']]['thisuser'] = true;
  622. break;
  623. }
  624. }
  625. }
  626. }
  627. return $topics;
  628. } else {
  629. return '';
  630. }
  631. }
  632. # ******************************************************************
  633. # FORUM/TOPIC VIEW AND GENERAL DB FUNCTIONS
  634. # ******************************************************************
  635. # ------------------------------------------------------------------
  636. # sf_get_watched_topics($currentpagge)
  637. #
  638. # Grabs all watched topics
  639. # $currentpage: index to paging
  640. # ------------------------------------------------------------------
  641. function sf_get_watched_topics($currentpage=0)
  642. {
  643. global $wpdb, $current_user, $sfglobals;
  644. # quick permission check
  645. if (!$current_user->sfwatch) return '';
  646. $limit='';
  647. # get watched topics
  648. $list = $sfglobals['member']['watches'];
  649. if (empty($list)) return '';
  650. # create where clause of watched topics
  651. $where = " WHERE ".SFTOPICS.".topic_id IN (" . implode(",", $list) . ")";
  652. # retrieve watched topic records
  653. $query = "SELECT ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
  654. topic_status, topic_pinned, topic_opened, topic_watches, topic_status_flag,
  655. blog_post_id, ".SFTOPICS.".forum_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  656. forum_slug, forum_name, topic_status_set, post_ratings, group_id, post_content, post_status
  657. FROM ".SFTOPICS."
  658. JOIN ".SFFORUMS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  659. JOIN ".SFPOSTS." ON ".SFTOPICS.".post_id = ".SFPOSTS.".post_id ".
  660. $where." ORDER BY topic_date DESC ".$limit;
  661. $records = $wpdb->get_results($query, ARRAY_A);
  662. if($records)
  663. {
  664. $sfdisplay=array();
  665. $sfdisplay=sf_get_option('sfdisplay');
  666. # do we need a post tooltip on the topic link?
  667. if($sfdisplay['topics']['posttip'])
  668. {
  669. for($x=0; $x<count($records); $x++)
  670. {
  671. $records[$x]['post_tip']=sf_filter_tooltip_display($records[$x]['post_content'], $records[$x]['post_status']);
  672. $records[$x]['post_content']='';
  673. }
  674. }
  675. }
  676. $watched['records'] = $records;
  677. $watched['count'] = count($list);
  678. return $watched;
  679. }
  680. # ------------------------------------------------------------------
  681. # sf_get_subscribed_topics($currentpagge)
  682. #
  683. # Grabs all subscribed topics
  684. # $currentpage: index to paging
  685. # ------------------------------------------------------------------
  686. function sf_get_subscribed_topics($currentpage=0)
  687. {
  688. global $wpdb, $current_user, $sfglobals;
  689. $limit = '';
  690. # quick permission check
  691. if (!$current_user->sfsubscriptions) return '';
  692. # get subscribed topics
  693. $list = $sfglobals['member']['subscribe'];
  694. if (empty($list)) return '';
  695. # create where clause of subscribed topics
  696. $where = " WHERE ".SFTOPICS.".topic_id IN (" . implode(",", $list) . ")";
  697. # retrieve watched topic records
  698. $query = "SELECT ".SFTOPICS.".topic_id, topic_slug, topic_name, ".sf_zone_datetime('topic_date').",
  699. topic_status, topic_pinned, topic_opened, topic_subs, topic_status_flag,
  700. blog_post_id, ".SFTOPICS.".forum_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  701. forum_slug, forum_name, topic_status_set, post_ratings, group_id, post_content, post_status
  702. FROM ".SFTOPICS."
  703. JOIN ".SFFORUMS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  704. JOIN ".SFPOSTS." ON ".SFTOPICS.".post_id = ".SFPOSTS.".post_id ".
  705. $where." ORDER BY topic_date DESC ".$limit;
  706. $records = $wpdb->get_results($query, ARRAY_A);
  707. if($records)
  708. {
  709. $sfdisplay=array();
  710. $sfdisplay=sf_get_option('sfdisplay');
  711. # do we need a post tooltip on the topic link?
  712. if($sfdisplay['topics']['posttip'])
  713. {
  714. for($x=0; $x<count($records); $x++)
  715. {
  716. $records[$x]['post_tip']=sf_filter_tooltip_display($records[$x]['post_content'], $records[$x]['post_status']);
  717. $records[$x]['post_content']='';
  718. }
  719. }
  720. }
  721. $subscribed['records'] = $records;
  722. $subscribed['count'] = count($list);
  723. return $subscribed;
  724. }
  725. # ------------------------------------------------------------------
  726. # sf_get_related_topics($tags)
  727. #
  728. # Grabs related topics based on tags
  729. # ------------------------------------------------------------------
  730. function sf_get_related_topics($tags)
  731. {
  732. global $wpdb;
  733. if(!$tags) return '';
  734. # build list of tags for the topic id
  735. $taglist = '';
  736. foreach ($tags as $tag)
  737. {
  738. if ($taglist == '')
  739. {
  740. $taglist = "('".$tag->tag_slug."'";
  741. } else {
  742. $taglist.= ",'".$tag->tag_slug."'";
  743. }
  744. }
  745. $taglist.= ")";
  746. # now grab the results
  747. $LIMIT = ' LIMIT 10';
  748. $ORDER = ' ORDER BY topic_id DESC';
  749. $WHERE = SFTOPICS.".topic_id IN (SELECT topic_id FROM ".SFTAGMETA." JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  750. WHERE tag_slug IN ".$taglist.")";
  751. $topics = $wpdb->get_results(
  752. "SELECT SQL_CALC_FOUND_ROWS DISTINCT
  753. ".SFTOPICS.".topic_id, topic_name, topic_slug, ".SFTOPICS.".forum_id, forum_name, forum_slug,
  754. ".sf_zone_datetime('topic_date').", topic_status, topic_pinned, topic_opened,
  755. topic_watches, topic_status_flag, blog_post_id, ".SFTOPICS.".post_id, ".SFTOPICS.".post_count,
  756. topic_status_set, post_ratings, group_id
  757. FROM ".SFTOPICS."
  758. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  759. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  760. WHERE ".$WHERE.$ORDER.$LIMIT.";", ARRAY_A);
  761. return $topics;
  762. }
  763. # ------------------------------------------------------------------
  764. # sf_get_memberlists()
  765. #
  766. # Builds viewable member lists for current user
  767. # ------------------------------------------------------------------
  768. function sf_get_memberlists($currentpage, $search)
  769. {
  770. global $wpdb, $current_user, $sfglobals;
  771. $data = '';
  772. $data->records = '';
  773. $data->count = 0;
  774. $sfmemberopts = sf_get_option('sfmemberopts');
  775. if ($current_user->forumadmin || ($sfmemberopts['sfshowmemberlist'] && $current_user->sfmemberlist))
  776. {
  777. # are we limiting member lists to memberships?
  778. $where = '';
  779. $sfmemberopts = sf_get_option('sfmemberopts');
  780. if ($sfmemberopts['sflimitmemberlist'] && !$current_user->forumadmin)
  781. {
  782. # get usergroups user has membership in
  783. $ugs = sf_get_user_memberships($current_user->ID);
  784. #if no usergroup memberships return empty list
  785. if ($ugs)
  786. {
  787. $ug_ids = '';
  788. foreach ($ugs as $ug)
  789. {
  790. $ug_ids[] = $ug['usergroup_id'];
  791. }
  792. # if no visible usergroups, dont return any search results
  793. if (empty($ug_ids)) return $data;
  794. } else {
  795. return $data;
  796. }
  797. # create where clause based on user memberships
  798. $where = " WHERE ".SFPERMISSIONS.".usergroup_id IN (" . implode(",", $ug_ids) . ") ";
  799. }
  800. if ($search != '')
  801. {
  802. if ($where == '') $where = 'WHERE '; else $where .= ' AND ';
  803. $where.= SFMEMBERS.'.display_name LIKE "'.$search.'%"';
  804. }
  805. # how many members per page?
  806. $startlimit = 0;
  807. $tpaged = $sfglobals['display']['topics']['perpage'];
  808. if(!$tpaged) $tpaged=20;
  809. if ($currentpage != 1)
  810. {
  811. $startlimit = ((($currentpage-1) * $tpaged));
  812. }
  813. $limit = " LIMIT ".$startlimit.', '.$tpaged;
  814. # retrieve members list records
  815. $query = "SELECT SQL_CALC_FOUND_ROWS DISTINCT ".SFPERMISSIONS.".usergroup_id, ".SFMEMBERSHIPS.".user_id, ".SFMEMBERS.".display_name, user_email, posts, lastvisit, usergroup_name, usergroup_desc
  816. FROM ".SFPERMISSIONS."
  817. JOIN ".SFUSERGROUPS." ON ".SFUSERGROUPS.".usergroup_id = ".SFPERMISSIONS.".usergroup_id
  818. JOIN ".SFMEMBERSHIPS." ON ".SFMEMBERSHIPS.".usergroup_id = ".SFPERMISSIONS.".usergroup_id
  819. JOIN ".SFMEMBERS." ON ".SFMEMBERS.".user_id = ".SFMEMBERSHIPS.".user_id
  820. JOIN ".SFUSERS." ON ".SFUSERS.".ID = ".SFMEMBERSHIPS.".user_id
  821. ".$where."
  822. ORDER BY usergroup_id, ".SFMEMBERS.".display_name "
  823. .$limit;
  824. $records = $wpdb->get_results($query, ARRAY_A);
  825. $data->records = $records;
  826. $data->count = $wpdb->get_var("SELECT FOUND_ROWS()");
  827. }
  828. return $data;
  829. }
  830. function sf_get_forum_memberships($user_id)
  831. {
  832. global $wpdb, $current_user;
  833. if ($current_user->guest)
  834. {
  835. $value = sf_get_sfmeta('default usergroup', 'sfguests');
  836. $guests = $value[0]['meta_value'];
  837. $sql = "SELECT forum_id
  838. FROM ".SFPERMISSIONS."
  839. WHERE usergroup_id=".$guests;
  840. } else {
  841. $sql = "SELECT forum_id
  842. FROM ".SFPERMISSIONS."
  843. JOIN ".SFMEMBERSHIPS." ON ".SFPERMISSIONS.".usergroup_id = ".SFMEMBERSHIPS.".usergroup_id
  844. WHERE user_id=".$user_id;
  845. }
  846. return $wpdb->get_results($sql);
  847. }
  848. function sf_get_membership_count($usergroup_id)
  849. {
  850. global $wpdb;
  851. if(!$usergroup_id) return '';
  852. $sql = "SELECT COUNT(*)
  853. FROM ".SFMEMBERSHIPS."
  854. WHERE ".SFMEMBERSHIPS.".usergroup_id=".$usergroup_id;
  855. return $wpdb->get_var($sql);
  856. }
  857. # ------------------------------------------------------------------
  858. # sf_get_last_post_in_topic()
  859. #
  860. # Returns post details of the latest post in the requested topic
  861. # $topicid: requested topic
  862. # NOTE: This one remains as used in a template tag
  863. # ------------------------------------------------------------------
  864. function sf_get_last_post_in_topic($topicid)
  865. {
  866. global $wpdb;
  867. if(!$topicid) return '';
  868. return $wpdb->get_row(
  869. "SELECT post_id, topic_id, forum_id, post_status, post_index, ".sf_zone_datetime('post_date').", UNIX_TIMESTAMP(post_date) as udate, guest_name, guest_email, ".SFPOSTS.".user_id, ".SFMEMBERS.".display_name, user_email
  870. FROM ".SFPOSTS."
  871. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  872. LEFT JOIN ".SFUSERS." ON ".SFPOSTS.".user_id = ".SFUSERS.".ID
  873. WHERE topic_id = ".$topicid." AND post_status = 0
  874. ORDER BY post_id DESC LIMIT 1");
  875. }
  876. # ------------------------------------------------------------------
  877. # sf_get_postratings()
  878. #
  879. # Returns post ratings
  880. # $postid: post_id of post to return
  881. # $asArray: return as an array if true
  882. # Note: No permission checking is performed
  883. # ------------------------------------------------------------------
  884. function sf_get_postratings($postid, $asArray=false)
  885. {
  886. global $wpdb;
  887. if(!$postid) return '';
  888. $sql=(
  889. "SELECT *
  890. FROM ".SFPOSTRATINGS."
  891. WHERE post_id=".$postid.";");
  892. if($asArray) return $wpdb->get_row($sql, ARRAY_A);
  893. return $wpdb->get_row($sql);
  894. }
  895. # ------------------------------------------------------------------
  896. # sf_update_postratings()
  897. #
  898. # Upates post ratings
  899. # $postid: post_id
  900. # $count: number of votes
  901. # $sum: ratings sum
  902. # $ips: array of ips voted for guests
  903. # $members: members that have voted
  904. # Note: No permission checking is performed
  905. # ------------------------------------------------------------------
  906. function sf_update_postratings($postid, $count, $sum, $ips, $members)
  907. {
  908. global $wpdb;
  909. if(!$postid) return '';
  910. $sql=(
  911. "UPDATE ".SFPOSTRATINGS."
  912. SET vote_count=$count, ratings_sum=$sum, ips='".$ips."', members='".$members."'
  913. WHERE post_id=".$postid.";");
  914. $wpdb->query($sql);
  915. return;
  916. }
  917. # ------------------------------------------------------------------
  918. # Add post ratings
  919. # $postid: post_id
  920. # $count: number of votes
  921. # $sum: ratings sum
  922. # $ips: array of ips voted for guests
  923. # $members: members that have voted
  924. # Note: No permission checking is performed
  925. # ------------------------------------------------------------------
  926. function sf_add_postratings($postid, $count, $sum, $ips, $members)
  927. {
  928. global $wpdb;
  929. if(!$postid) return '';
  930. $sql=(
  931. "INSERT INTO ".SFPOSTRATINGS." (post_id, vote_count, ratings_sum, ips, members)
  932. VALUES ($postid, $count, $sum, '".$ips."', '".$members."');");
  933. $wpdb->query($sql);
  934. return;
  935. }
  936. # ------------------------------------------------------------------
  937. # sf_get_topic_ratings()
  938. #
  939. # Returns post ratings
  940. # $topicid: post_id of post to return
  941. # Note: No permission checking is performed
  942. # ------------------------------------------------------------------
  943. function sf_get_topic_ratings($topicid)
  944. {
  945. global $wpdb;
  946. if(!$topicid) return '';
  947. $sql = ("
  948. SELECT vote_count, ratings_sum
  949. FROM ".SFPOSTRATINGS."
  950. JOIN ".SFPOSTS." ON ".SFPOSTS.".topic_id = ".$topicid."
  951. WHERE ".SFPOSTRATINGS.".post_id = ".SFPOSTS.".post_id");
  952. return $wpdb->get_results($sql);
  953. }
  954. # ------------------------------------------------------------------
  955. # sf_get_forum_record()
  956. #
  957. # Returns a single forum row
  958. # $forumid: forum_id of forum to return
  959. # Note: No permission checking is performed
  960. # ------------------------------------------------------------------
  961. function sf_get_forum_record($forumid)
  962. {
  963. global $wpdb, $session_forums;
  964. if(!$forumid) return '';
  965. # check if in the session forums cache
  966. if($session_forums)
  967. {
  968. foreach($session_forums as $forum)
  969. {
  970. if($forum->forum_id == $forumid)
  971. {
  972. return $forum;
  973. }
  974. }
  975. }
  976. $sql=(
  977. "SELECT *
  978. FROM ".SFFORUMS."
  979. WHERE forum_id=".$forumid.";");
  980. $thisforum = $wpdb->get_row($sql);
  981. $session_forums[] = $thisforum;
  982. return $thisforum;
  983. }
  984. # ------------------------------------------------------------------
  985. # sf_get_forum_record_from_slug()
  986. #
  987. # Returns a single forum row
  988. # $forumslug: forum_slug of forum to return
  989. # $asArray: return as an array if true
  990. # Note: No permission checking is performed
  991. # ------------------------------------------------------------------
  992. function sf_get_forum_record_from_slug($forumslug, $asArray=false)
  993. {
  994. global $wpdb;
  995. if(!$forumslug) return '';
  996. $sql=(
  997. "SELECT *
  998. FROM ".SFFORUMS."
  999. WHERE forum_slug='".$forumslug."';");
  1000. if($asArray) return $wpdb->get_row($sql, ARRAY_A);
  1001. return $wpdb->get_row($sql);
  1002. }
  1003. # ------------------------------------------------------------------
  1004. # sf_get_group_record_from_slug()
  1005. #
  1006. # Returns a single group and forum row
  1007. # $forumslug: forum_slug of group and forum to return
  1008. # $asArray: return as an array if true
  1009. # Note: No permission checking is performed
  1010. # ------------------------------------------------------------------
  1011. function sf_get_group_record_from_slug($forumslug, $asArray=false)
  1012. {
  1013. global $wpdb;
  1014. if(!$forumslug) return '';
  1015. $sql=(
  1016. "SELECT *
  1017. FROM ".SFFORUMS."
  1018. JOIN ".SFGROUPS." ON ".SFFORUMS.".group_id = ".SFGROUPS.".group_id
  1019. WHERE forum_slug='".$forumslug."';");
  1020. if($asArray) return $wpdb->get_row($sql, ARRAY_A);
  1021. return $wpdb->get_row($sql);
  1022. }
  1023. # ------------------------------------------------------------------
  1024. # sf_get_forums_all()
  1025. #
  1026. # Returns complete recordset of forums
  1027. # $id_only: limit recordset to forum_id and slug only
  1028. # $asArray: return results as an array
  1029. # Note: No permission checking is performed
  1030. # ------------------------------------------------------------------
  1031. function sf_get_forums_all($id_only=false, $asArray=false)
  1032. {
  1033. global $wpdb;
  1034. if($id_only ? $FROM='forum_id, forum_slug' : $FROM='*');
  1035. $sql=("SELECT ".$FROM." FROM ".SFFORUMS." ORDER BY forum_seq");
  1036. if($asArray) return $wpdb->get_results($sql, ARRAY_A);
  1037. return $wpdb->get_results($sql);
  1038. }
  1039. # ------------------------------------------------------------------
  1040. # sf_forum_exists()
  1041. #
  1042. # Check the existence of a forum by id
  1043. # $forumid: forum to check for
  1044. # ------------------------------------------------------------------
  1045. function sf_forum_exists($forumid)
  1046. {
  1047. global $wpdb;
  1048. if(empty($forumid)) return false;
  1049. if($wpdb->get_var(
  1050. "SELECT forum_name
  1051. FROM ".SFFORUMS."
  1052. WHERE forum_id=".$forumid))
  1053. {
  1054. return true;
  1055. }
  1056. return false;
  1057. }
  1058. # ------------------------------------------------------------------
  1059. # sf_forum_locked()
  1060. #
  1061. # Returns the lock status of a forum
  1062. # $forumid: forum to check for
  1063. # ------------------------------------------------------------------
  1064. function sf_forum_locked($forumid)
  1065. {
  1066. global $wpdb;
  1067. return $wpdb->get_var("SELECT forum_status FROM ".SFFORUMS." WHERE forum_id=".$forumid);
  1068. }
  1069. # ------------------------------------------------------------------
  1070. # sf_get_forum_rss_url()
  1071. #
  1072. # Returns the RSS URL for a forum (custom or standard)
  1073. # $forumid: forum to return
  1074. # $forumslug: slug for the url
  1075. # ------------------------------------------------------------------
  1076. function sf_get_forum_rss_url($forumid, $forumslug)
  1077. {
  1078. global $wpdb, $sfglobals;
  1079. if (empty($forumid)) return '';
  1080. $url = $wpdb->get_var("SELECT forum_rss FROM ".SFFORUMS." WHERE forum_id=".$forumid);
  1081. if (empty($url))
  1082. {
  1083. $rssopt = sf_get_option('sfrss');
  1084. if ($rssopt['sfrssfeedkey'])
  1085. $url = sf_build_qurl('forum='.$forumslug, 'xfeed=forum', 'feedkey='.$sfglobals['member']['feedkey']);
  1086. else
  1087. $url = sf_build_qurl('forum='.$forumslug, 'xfeed=forum');
  1088. }
  1089. return $url;
  1090. }
  1091. # ------------------------------------------------------------------
  1092. # sf_get_topic_status_set()
  1093. #
  1094. # Returns the topic status set name for a forum
  1095. # $forumid: forum to return
  1096. # ------------------------------------------------------------------
  1097. function sf_get_topic_status_set($forumid)
  1098. {
  1099. global $wpdb;
  1100. if(empty($forumid)) return '';
  1101. return $wpdb->get_var(
  1102. "SELECT topic_status_set FROM ".SFFORUMS." WHERE forum_id=".$forumid);
  1103. }
  1104. function sf_get_topic_status_from_forum($forumid, $statusflag)
  1105. {
  1106. global $wpdb;
  1107. if(!$forumid) return '';
  1108. $flag='';
  1109. $set=sf_get_topic_status_set($forumid);
  1110. if($set != 0)
  1111. {
  1112. $flag=sf_get_topic_status_flag($set, $statusflag);
  1113. }
  1114. return $flag;
  1115. }
  1116. # ------------------------------------------------------------------
  1117. # sf_find_user_in_topic()
  1118. #
  1119. # Searches a topics posts to see if user has ever posted in it for
  1120. # the forums topic list icon
  1121. # $topicid: topic to search
  1122. # $userid: user to look for
  1123. # %%FUTURE OPTIMISE%%
  1124. # ------------------------------------------------------------------
  1125. function sf_find_user_in_topic($topicid, $userid)
  1126. {
  1127. global $wpdb;
  1128. if(!$topicid || !$userid) return '';
  1129. return $wpdb->get_col(
  1130. "SELECT user_id
  1131. FROM ".SFPOSTS."
  1132. WHERE topic_id=".$topicid."
  1133. AND user_id=".$userid);
  1134. }
  1135. # ------------------------------------------------------------------
  1136. # sf_get_forum_from_topic()
  1137. #
  1138. # returng the firum id when only the topic is known
  1139. # $topicid: topic to search
  1140. # ------------------------------------------------------------------
  1141. function sf_get_forum_from_topic($topicid)
  1142. {
  1143. global $wpdb;
  1144. if(!$topicid) return '';
  1145. return $wpdb->get_var(
  1146. "SELECT forum_id
  1147. FROM ".SFTOPICS."
  1148. WHERE topic_id=".$topicid);
  1149. }
  1150. # ------------------------------------------------------------------
  1151. # sf_get_forum_name()
  1152. #
  1153. # Returns forum name when only the slug is known
  1154. # $forumslug: forum to return
  1155. # ------------------------------------------------------------------
  1156. function sf_get_forum_name($forumslug)
  1157. {
  1158. global $wpdb;
  1159. if(!$forumslug) return '';
  1160. return $wpdb->get_var(
  1161. "SELECT forum_name
  1162. FROM ".SFFORUMS."
  1163. WHERE forum_slug='".$forumslug."'");
  1164. }
  1165. # ------------------------------------------------------------------
  1166. # sf_get_forum_name_from_id()
  1167. #
  1168. # Returns forum name when only the slug is known
  1169. # $forumid: forum to return
  1170. # ------------------------------------------------------------------
  1171. function sf_get_forum_name_from_id($forumid)
  1172. {
  1173. global $wpdb;
  1174. if(!$forumid) return '';
  1175. return $wpdb->get_var(
  1176. "SELECT forum_name
  1177. FROM ".SFFORUMS."
  1178. WHERE forum_id=".$forumid);
  1179. }
  1180. # ------------------------------------------------------------------
  1181. # sf_get_forum_slug()
  1182. #
  1183. # Returns forum slug when only the id is known
  1184. # $forumid: forum to return
  1185. # ------------------------------------------------------------------
  1186. function sf_get_forum_slug($forumid)
  1187. {
  1188. global $wpdb;
  1189. if(!$forumid) return '';
  1190. return $wpdb->get_var(
  1191. "SELECT forum_slug
  1192. FROM ".SFFORUMS."
  1193. WHERE forum_id=".$forumid);
  1194. }
  1195. # ------------------------------------------------------------------
  1196. # sf_get_forum_id()
  1197. #
  1198. # Returns forum id when only the slug is known
  1199. # $forumslug: forum to return
  1200. # ------------------------------------------------------------------
  1201. function sf_get_forum_id($forumslug)
  1202. {
  1203. global $wpdb;
  1204. if(!$forumslug) return '';
  1205. return $wpdb->get_var(
  1206. "SELECT forum_id
  1207. FROM ".SFFORUMS."
  1208. WHERE forum_slug='".$forumslug."'");
  1209. }
  1210. # ------------------------------------------------------------------
  1211. # sf_get_topics_forum_id()
  1212. #
  1213. # Returns forum id when only the topic id is known
  1214. # $topicid: forum to return from topic record
  1215. # ------------------------------------------------------------------
  1216. function sf_get_topics_forum_id($topicid)
  1217. {
  1218. global $wpdb;
  1219. if(!$topicid) return '';
  1220. return $wpdb->get_var(
  1221. "SELECT forum_id
  1222. FROM ".SFTOPICS."
  1223. WHERE topic_id=".$topicid);
  1224. }
  1225. # ******************************************************************
  1226. # TOPIC/POST VIEW AND GENERAL DB FUNCTIONS
  1227. # ******************************************************************
  1228. # ------------------------------------------------------------------
  1229. # sf_get_combined_topics_and_posts()
  1230. #
  1231. # Returns a page of posts for specified topic
  1232. # $topicid: topic and posts to load
  1233. # ------------------------------------------------------------------
  1234. function sf_get_combined_topics_and_posts($topicid)
  1235. {
  1236. global $wpdb, $sfvars, $sfglobals;
  1237. if(!$topicid) return '';
  1238. # sadly have to grab the topic row first because we need obverride sort order if set
  1239. $topic = $wpdb->get_row(
  1240. "SELECT topic_id, topic_slug, ".SFTOPICS.".forum_id, topic_name, ".SFTOPICS.".post_count, topic_subs, topic_watches, topic_status, post_ratings, use_tags, blog_post_id,
  1241. forum_slug, forum_status, topic_status_set, topic_status_flag, user_id, use_tags
  1242. FROM ".SFTOPICS."
  1243. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  1244. WHERE topic_id = ".$topicid.";", ARRAY_A);
  1245. # quick permission check
  1246. if(!sf_can_view_forum($topic['forum_id'])) return '';
  1247. # grab the tags if enabled for this forum
  1248. if ($topic['use_tags'])
  1249. {
  1250. $sql = "SELECT ".SFTAGS.".tag_name, tag_slug
  1251. FROM ".SFTAGMETA."
  1252. JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  1253. WHERE ".SFTAGMETA.".topic_id = ".$topicid;
  1254. $topic['tags'] = $wpdb->get_results($sql);
  1255. }
  1256. # now for the posts
  1257. $ORDER="ASC"; # default
  1258. if($sfglobals['display']['posts']['sortdesc']) $ORDER="DESC"; # global override
  1259. $ppaged=$sfglobals['display']['posts']['perpage'];
  1260. if(!$ppaged) $ppaged=20;
  1261. if($sfvars['page'] == 1 ? $startlimit = 0 : $startlimit = ((($sfvars['page']-1) * $ppaged)));
  1262. $topic['topic_page'] = $sfvars['page'];
  1263. $tpages = ($topic['post_count'] / $ppaged);
  1264. if(!is_int($tpages))
  1265. {
  1266. $tpages = intval($topic['post_count'] / $ppaged) +1;
  1267. }
  1268. $topic['topic_total_pages'] = $tpages;
  1269. $LIMIT = ' LIMIT '.$startlimit.', '.$ppaged;
  1270. $records = $wpdb->get_results(
  1271. "SELECT ".SFPOSTS.".post_id, post_content, ".sf_zone_datetime('post_date').", ".SFPOSTS.".user_id, guest_name, guest_email,
  1272. post_status, post_pinned, post_index, post_edit,
  1273. ".SFMEMBERS.".display_name, admin, posts, signature, avatar, pm,
  1274. user_url, user_email, rating_id, vote_count, ratings_sum, ips, members
  1275. FROM ".SFPOSTS."
  1276. LEFT JOIN ".SFUSERS." ON ".SFPOSTS.".user_id = ".SFUSERS.".ID
  1277. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  1278. LEFT JOIN ".SFPOSTRATINGS." ON ".SFPOSTRATINGS.".post_id = ".SFPOSTS.".post_id
  1279. WHERE topic_id = ".$topicid."
  1280. ORDER BY post_pinned DESC, ".SFPOSTS.".post_id ".$ORDER.$LIMIT, ARRAY_A);
  1281. $topic['posts'] = $records;
  1282. return $topic;
  1283. }
  1284. # ------------------------------------------------------------------
  1285. # sf_get_topic_record()
  1286. #
  1287. # Returns a single topic row
  1288. # $topicid: topic_id of topic to return
  1289. # Note: No permission checking is performed
  1290. # ------------------------------------------------------------------
  1291. function sf_get_topic_record($topicid)
  1292. {
  1293. global $wpdb, $session_topics;
  1294. if(!$topicid) return '';
  1295. # see if in the current session topics cache
  1296. if($session_topics)
  1297. {
  1298. foreach($session_topics as $topic)
  1299. {
  1300. if($topic->topic_id == $topicid)
  1301. {
  1302. return $topic;
  1303. }
  1304. }
  1305. }
  1306. $sql=(
  1307. "SELECT *
  1308. FROM ".SFTOPICS."
  1309. WHERE topic_id=".$topicid.";");
  1310. $thistopic = $wpdb->get_row($sql);
  1311. $session_topics[] = $thistopic;
  1312. return $thistopic;
  1313. }
  1314. # ------------------------------------------------------------------
  1315. # sf_get_topic_record_from_slug()
  1316. #
  1317. # Returns a single topic row
  1318. # $topicslug: topic_slug of topic to return
  1319. # Note: No permission checking is performed
  1320. # ------------------------------------------------------------------
  1321. function sf_get_topic_record_from_slug($topicslug)
  1322. {
  1323. global $wpdb, $session_topics;
  1324. if(!$topicslug) return '';
  1325. # see if in the current session topics cache
  1326. if($session_topics)
  1327. {
  1328. foreach($session_topics as $topic)
  1329. {
  1330. if($topic->topic_slug == $topicslug)
  1331. {
  1332. return $topic;
  1333. }
  1334. }
  1335. }
  1336. $sql=(
  1337. "SELECT *
  1338. FROM ".SFTOPICS."
  1339. WHERE topic_slug='".$topicslug."';");
  1340. $thistopic = $wpdb->get_row($sql);
  1341. $session_topics[] = $thistopic;
  1342. return $thistopic;
  1343. }
  1344. # ------------------------------------------------------------------
  1345. # sf_get_topic_record_from_blogpostid()
  1346. #
  1347. # Returns a single topic row
  1348. # $blogpostid: blog post id to see if any linked topics
  1349. # Note: No permission checking is performed
  1350. # ------------------------------------------------------------------
  1351. function sf_get_topic_record_from_blogpostid($postid)
  1352. {
  1353. global $wpdb, $session_topics;
  1354. if (!$postid) return '';
  1355. $sql = (
  1356. "SELECT *
  1357. FROM ".SFTOPICS."
  1358. WHERE blog_post_id=".$postid);
  1359. $thistopic = $wpdb->get_row($sql);
  1360. return $thistopic;
  1361. }
  1362. # ------------------------------------------------------------------
  1363. # sf_get_topics_all()
  1364. #
  1365. # Returns complete recordset of topics
  1366. # $id_only: limit recordset to topic_id and slug only
  1367. # $asArray: returb list as an aray
  1368. # Note: No permission checking is performed
  1369. # ------------------------------------------------------------------
  1370. function sf_get_topics_all($id_only=false, $asArray=false)
  1371. {
  1372. global $wpdb;
  1373. if($id_only ? $FROM='topic_id, topic_slug' : $FROM='*');
  1374. $sql=("SELECT ".$FROM." FROM ".SFTOPICS);
  1375. if($asArray) return $wpdb->get_results($sql, ARRAY_A);
  1376. return $wpdb->get_results($sql);
  1377. }
  1378. # ------------------------------------------------------------------
  1379. # sf_topic_exists()
  1380. #
  1381. # Check the existence of a topic by id
  1382. # $topicid: forum to check for
  1383. # ------------------------------------------------------------------
  1384. function sf_topic_exists($topicid)
  1385. {
  1386. global $wpdb;
  1387. if(empty($topicid)) return false;
  1388. if($wpdb->get_var(
  1389. "SELECT topic_name
  1390. FROM ".SFTOPICS."
  1391. WHERE topic_id=".$topicid))
  1392. {
  1393. return true;
  1394. }
  1395. return false;
  1396. }
  1397. # ------------------------------------------------------------------
  1398. # sf_get_topic_name()
  1399. #
  1400. # Returns topic name when only the topic slug is known
  1401. # $topicslug: Topic to lookup
  1402. # ------------------------------------------------------------------
  1403. function sf_get_topic_name($topicslug)
  1404. {
  1405. global $wpdb;
  1406. if(!$topicslug) return '';
  1407. return $wpdb->get_var(
  1408. "SELECT topic_name
  1409. FROM ".SFTOPICS."
  1410. WHERE topic_slug='".$topicslug."'");
  1411. }
  1412. # ------------------------------------------------------------------
  1413. # sf_get_topic_slug()
  1414. #
  1415. # Returns topic slug when only the topic id is known
  1416. # $topicid: Topic to lookup
  1417. # ------------------------------------------------------------------
  1418. function sf_get_topic_slug($topicid)
  1419. {
  1420. global $wpdb;
  1421. if(!$topicid) return '';
  1422. return $wpdb->get_var(
  1423. "SELECT topic_slug
  1424. FROM ".SFTOPICS."
  1425. WHERE topic_id=".$topicid);
  1426. }
  1427. # ------------------------------------------------------------------
  1428. # sf_get_topic_id()
  1429. #
  1430. # Returns topic id when only the topic slug is known
  1431. # $topicslug: Topic to lookup
  1432. # ------------------------------------------------------------------
  1433. function sf_get_topic_id($topicslug)
  1434. {
  1435. global $wpdb;
  1436. if(!$topicslug) return '';
  1437. return $wpdb->get_var(
  1438. "SELECT topic_id
  1439. FROM ".SFTOPICS."
  1440. WHERE topic_slug='".$topicslug."'");
  1441. }
  1442. # ------------------------------------------------------------------
  1443. # sf_get_topic_name_from_id()
  1444. #
  1445. # Returns topic name when only the id is known
  1446. # $topicid: topic to return
  1447. # ------------------------------------------------------------------
  1448. function sf_get_topic_name_from_id($topicid)
  1449. {
  1450. global $wpdb;
  1451. if(!$topicid) return '';
  1452. return $wpdb->get_var(
  1453. "SELECT topic_name
  1454. FROM ".SFTOPICS."
  1455. WHERE topic_id=".$topicid);
  1456. }
  1457. # ------------------------------------------------------------------
  1458. # sf_get_slugs_from_postid()
  1459. #
  1460. # Returns forum and topic slugs when only the post id is known
  1461. # $postid: Post to lookup
  1462. # ------------------------------------------------------------------
  1463. function sf_get_slugs_from_postid($postid)
  1464. {
  1465. global $wpdb;
  1466. if(!$postid) return '';
  1467. return $wpdb->get_row(
  1468. "SELECT forum_slug, topic_slug, post_index
  1469. FROM ".SFPOSTS."
  1470. JOIN ".SFFORUMS." ON ".SFPOSTS.".forum_id = ".SFFORUMS.".forum_id
  1471. JOIN ".SFTOPICS." ON ".SFPOSTS.".topic_id = ".SFTOPICS.".topic_id
  1472. WHERE ".SFPOSTS.".post_id=".$postid.";");
  1473. }
  1474. # ------------------------------------------------------------------
  1475. # sf_get_posts_count_in_topic()
  1476. #
  1477. # Returns the post count from topic record
  1478. # $topicid: Topic to lookup
  1479. # ------------------------------------------------------------------
  1480. function sf_get_posts_count_in_topic($topicid)
  1481. {
  1482. global $wpdb;
  1483. if(empty($topicid)) return 0;
  1484. return $wpdb->get_var(
  1485. "SELECT post_count
  1486. FROM ".SFTOPICS."
  1487. WHERE topic_id=".$topicid);
  1488. }
  1489. # ------------------------------------------------------------------
  1490. # sf_get_posts_count_in_linked_topic()
  1491. #
  1492. # Returns the post count of approved posts in topic
  1493. # $topicid: Topic to lookup
  1494. # $hidedupes Hide duplicate comments
  1495. # ------------------------------------------------------------------
  1496. function sf_get_posts_count_in_linked_topic($topicid, $hidedupes)
  1497. {
  1498. global $wpdb;
  1499. if(empty($topicid)) return 0;
  1500. $hide='';
  1501. if($hidedupes) $hide = " AND comment_id IS NULL";
  1502. return $wpdb->get_var(
  1503. "SELECT COUNT(post_id)
  1504. FROM ".SFPOSTS."
  1505. WHERE topic_id=".$topicid." AND post_status=0".$hide);
  1506. }
  1507. function sf_update_topic_status_flag($statvalue, $topicid)
  1508. {
  1509. global $wpdb;
  1510. if(!$statvalue || !$topicid) return '';
  1511. return $wpdb->query("UPDATE ".SFTOPICS." SET topic_status_flag=".$statvalue." WHERE topic_id=".$topicid);
  1512. }
  1513. # ******************************************************************
  1514. # NEW/UNREAD POST VIEWS DB FUNCTIONS
  1515. # ******************************************************************
  1516. # ------------------------------------------------------------------
  1517. # sf_get_admins_queued_posts()
  1518. #
  1519. # Returns the admins new post view
  1520. # ------------------------------------------------------------------
  1521. function sf_get_admins_queued_posts()
  1522. {
  1523. global $wpdb, $sfvars;
  1524. $newposts = '';
  1525. $clause = '';
  1526. $records = $wpdb->get_results(
  1527. "SELECT ".SFWAITING.".forum_id, forum_slug, forum_name, topic_status_set, topic_id, ".SFWAITING.".post_count, ".SFWAITING.".post_id
  1528. FROM ".SFWAITING."
  1529. LEFT JOIN ".SFFORUMS." ON ".SFWAITING.".forum_id = ".SFFORUMS.".forum_id
  1530. ORDER BY forum_id;");
  1531. if($records)
  1532. {
  1533. # now grab all of the post record we are going to need in one query
  1534. $pcount = count($records);
  1535. $done = 0;
  1536. foreach ($records as $record)
  1537. {
  1538. $clause.= "(".SFTOPICS.".topic_id = ".$record->topic_id." AND ".SFPOSTS.".post_id >= ".$record->post_id.")";
  1539. $done++;
  1540. if($done < $pcount) $clause.= " OR ";
  1541. }
  1542. $preparedpostrecords = $wpdb->get_results(
  1543. "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
  1544. FROM ".SFPOSTS."
  1545. LEFT JOIN ".SFTOPICS." ON ".SFPOSTS.".topic_id = ".SFTOPICS.".topic_id
  1546. LEFT JOIN ".SFMEMBERS." ON ".SFPOSTS.".user_id = ".SFMEMBERS.".user_id
  1547. WHERE ".$clause."
  1548. ORDER BY post_id;");
  1549. $sfvars['queue'] = count($preparedpostrecords);
  1550. $newposts = array();
  1551. $findex=-1;
  1552. $pindex=0;
  1553. $tindex=0;
  1554. foreach($records as $record)
  1555. {
  1556. # Check this still has posts in it and they were not removed (it happens)
  1557. $postrecords='';
  1558. foreach($preparedpostrecords as $prepared)
  1559. {
  1560. if($prepared->topic_id == $record->topic_id)
  1561. {
  1562. $postrecords[] = $prepared;
  1563. }
  1564. }
  1565. # So - were they removed? if so donlt add them to the array and remove them from sfwaiting
  1566. if($postrecords == '')
  1567. {
  1568. sf_remove_from_waiting(true, $record->topic_id, $record->post_id);
  1569. continue;
  1570. }
  1571. $forumid=$record->forum_id;
  1572. if($findex == -1 || $newposts[$findex]['forum_id'] != $forumid)
  1573. {
  1574. $findex++;
  1575. $tindex=0;
  1576. $pindex=0;
  1577. $newposts[$findex]['forum_id']=$record->forum_id;
  1578. $newposts[$findex]['forum_name']=$record->forum_name;
  1579. $newposts[$findex]['forum_slug']=$record->forum_slug;
  1580. $newposts[$findex]['topic_status_set']=$record->topic_status_set;
  1581. }
  1582. $newposts[$findex]['topics'][$tindex]['topic_id']=$record->topic_id;
  1583. $newposts[$findex]['topics'][$tindex]['post_id']=$record->post_id;
  1584. # isolate the post records for current topic
  1585. $postrecords='';
  1586. foreach($preparedpostrecords as $prepared)
  1587. {
  1588. if($prepared->topic_id == $record->topic_id)
  1589. {
  1590. $postrecords[] = $prepared;
  1591. }
  1592. }
  1593. if($postrecords)
  1594. {
  1595. $newposts[$findex]['topics'][$tindex]['post_count']=count($postrecords);
  1596. $pindex=0;
  1597. foreach($postrecords as $postrecord)
  1598. {
  1599. $newposts[$findex]['topics'][$tindex]['topic_slug']=$postrecord->topic_slug;
  1600. $newposts[$findex]['topics'][$tindex]['topic_status_flag']=$postrecord->topic_status_flag;
  1601. $newposts[$findex]['topics'][$tindex]['topic_name']=$postrecord->topic_name;
  1602. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['post_id']=$postrecord->post_id;
  1603. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['post_status']=$postrecord->post_status;
  1604. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['post_index']=$postrecord->post_index;
  1605. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['post_content']=$postrecord->post_content;
  1606. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['user_id']=$postrecord->user_id;
  1607. if(empty($postrecord->user_id))
  1608. {
  1609. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['display_name']=$postrecord->guest_name;
  1610. $thisuser = 'Guest';
  1611. } else {
  1612. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['display_name']=$postrecord->display_name;
  1613. $thisuser = 'Member';
  1614. }
  1615. $newposts[$findex]['topics'][$tindex]['posts'][$pindex]['user_type']=$thisuser;
  1616. $pindex++;
  1617. }
  1618. }
  1619. $tindex++;
  1620. }
  1621. }
  1622. # if no new posts then housekeep sfwaiting
  1623. if(!$newposts) $wpdb->query("TRUNCATE ".SFWAITING);
  1624. return $newposts;
  1625. }
  1626. # ------------------------------------------------------------------
  1627. # sf_combined_new_posts_list()
  1628. #
  1629. # Extend new post list to include all required data
  1630. # $posts: Posts array (forumid and topicid)
  1631. # $forumsort Sort list into group/forum sequence
  1632. # ------------------------------------------------------------------
  1633. function sf_combined_new_posts_list($posts, $forumsort=false)
  1634. {
  1635. global $wpdb;
  1636. $clause='';
  1637. $record='';
  1638. if($posts)
  1639. {
  1640. $pcount = count($posts);
  1641. $done = 0;
  1642. foreach ($posts as $post)
  1643. {
  1644. if (sf_can_view_forum($post->forum_id, true))
  1645. {
  1646. if ($clause != '') $clause.= ' OR ';
  1647. $clause.= "(".SFTOPICS.".forum_id=".$post->forum_id." AND ".SFTOPICS.".topic_id=".$post->topic_id.")";
  1648. $done++;
  1649. }
  1650. }
  1651. # just return if nothing visible
  1652. if ($clause == '') return '';
  1653. if($forumsort)
  1654. {
  1655. $groupjoin = " JOIN ".SFGROUPS." ON ".SFFORUMS.".group_id = ".SFGROUPS.".group_id ";
  1656. $orderby = " ORDER BY group_seq, forum_seq";
  1657. $groupname = ', group_name';
  1658. } else {
  1659. $groupjoin = '';
  1660. $orderby = ' ORDER BY post_id DESC';
  1661. $groupname = '';
  1662. }
  1663. $record = $wpdb->get_results(
  1664. "SELECT ".SFFORUMS.".forum_id, forum_name, forum_slug,
  1665. topic_name, topic_slug, ".SFTOPICS.".post_id, ".SFTOPICS.".topic_id, topic_status_set,
  1666. post_status, UNIX_TIMESTAMP(".SFPOSTS.".post_date) as udate, post_index, ".SFPOSTS.".user_id ".$groupname."
  1667. FROM ".SFFORUMS."
  1668. JOIN ".SFTOPICS." ON ".SFFORUMS.".forum_id = ".SFTOPICS.".forum_id
  1669. JOIN ".SFPOSTS." ON ".SFTOPICS.".post_id = ".SFPOSTS.".post_id
  1670. ".$groupjoin."
  1671. WHERE ".$clause.$orderby, ARRAY_A);
  1672. }
  1673. return $record;
  1674. }
  1675. # ------------------------------------------------------------------
  1676. # sf_get_users_new_post_list()
  1677. #
  1678. # Returns recordset of current users new-post-list
  1679. # $limit: limit to x number of records
  1680. # ------------------------------------------------------------------
  1681. function sf_get_users_new_post_list($limit)
  1682. {
  1683. global $current_user, $sfglobals;
  1684. if ($current_user->member)
  1685. {
  1686. $newpostlist = $sfglobals['member']['newposts'];
  1687. if($current_user->newpostlist == false)
  1688. {
  1689. $newpostlist = sf_update_users_newposts($newpostlist);
  1690. }
  1691. # we have a live user so construct SQL if anything in newpostslist
  1692. if ($newpostlist['topics'][0] != 0)
  1693. {
  1694. $wanted = $limit;
  1695. $where = ' WHERE';
  1696. if (count($newpostlist['topics']) < $limit) $limit = count($newpostlist['topics']);
  1697. for ($x=0; $x<$limit; $x++)
  1698. {
  1699. $where.= " topic_id=".$newpostlist['topics'][$x];
  1700. if ($x != $limit-1) $where.= " OR";
  1701. }
  1702. $recordset1 = sf_get_users_new_post_list_db($where, '');
  1703. # try and marry the extra count if not enough to satisfy $limit
  1704. if ($limit < $wanted)
  1705. {
  1706. $limit = " LIMIT ".$wanted;
  1707. $where = " WHERE post_status = 0 ";
  1708. $recordset2 = sf_get_users_new_post_list_db($where, $limit);
  1709. if ($recordset2)
  1710. {
  1711. for ($x=0; $x<count($recordset2); $x++)
  1712. {
  1713. if (!in_array($recordset2[$x]->topic_id, $newpostlist['topics']))
  1714. {
  1715. $recordset1[]=$recordset2[$x];
  1716. }
  1717. if (count($recordset1) == $wanted) break;
  1718. }
  1719. }
  1720. }
  1721. return sf_filter_new_post_list($recordset1);
  1722. }
  1723. }
  1724. # but if not a member, empty post list of members query didnlt reach limit...
  1725. if ($current_user->guest || $newpostlist['topics'][0] == 0)
  1726. {
  1727. $limit = " LIMIT ".$limit;
  1728. $where = " WHERE post_status = 0 ";
  1729. $recordset1 = sf_get_users_new_post_list_db($where, $limit);
  1730. return sf_filter_new_post_list($recordset1);
  1731. }
  1732. }
  1733. # ------------------------------------------------------------------
  1734. # sf_get_users_new_post_list_db()
  1735. #
  1736. # Support: Returns recordset of current users new-post-list
  1737. # $where: Option where clause on topic id
  1738. # $limit: limit to x number of records
  1739. # ------------------------------------------------------------------
  1740. function sf_get_users_new_post_list_db($where, $limit)
  1741. {
  1742. global $wpdb, $current_user;
  1743. $where2 = '';
  1744. if (!$current_user->forumadmin)
  1745. {
  1746. # limit to viewable forums based on permissions
  1747. $forums = sf_get_forum_memberships($current_user->ID);
  1748. if ($forums)
  1749. {
  1750. $forum_ids = '';
  1751. foreach ($forums as $forum)
  1752. {
  1753. if (sf_can_view_forum($forum->forum_id))
  1754. {
  1755. $forum_ids[] = $forum->forum_id;
  1756. }
  1757. }
  1758. } else {
  1759. return '';
  1760. }
  1761. # create where clause based on forums that current user can view
  1762. if ($forum_ids != '')
  1763. {
  1764. if ($where != '')
  1765. {
  1766. $where2 = " AND ";
  1767. } else {
  1768. $where2 = " WHERE ";
  1769. }
  1770. $where2.= SFPOSTS.".forum_id IN (" . implode(",", $forum_ids) . ") = 1 ";
  1771. } else {
  1772. return '';
  1773. }
  1774. }
  1775. $records = $wpdb->get_results(
  1776. "SELECT DISTINCT forum_id, topic_id
  1777. FROM ".SFPOSTS
  1778. .$where.$where2."
  1779. ORDER BY post_id DESC"
  1780. .$limit.";");
  1781. return $records;
  1782. }
  1783. # ------------------------------------------------------------------
  1784. # sf_filter_new_post_list()
  1785. #
  1786. # Support: Returns filtered list that current user has permissions to
  1787. # $recordset: Full list of forum/topics
  1788. # ------------------------------------------------------------------
  1789. function sf_filter_new_post_list($recordset)
  1790. {
  1791. if(!$recordset) return '';
  1792. $rlist = array();
  1793. $x = 0;
  1794. foreach($recordset as $record)
  1795. {
  1796. $rlist[$x]->forum_id=$record->forum_id;
  1797. $rlist[$x]->topic_id=$record->topic_id;
  1798. $x++;
  1799. }
  1800. return $rlist;
  1801. }
  1802. # ******************************************************************
  1803. # NEW POSTS FROM WAITING - DASHBOARD
  1804. # ******************************************************************
  1805. # ------------------------------------------------------------------
  1806. # sf_get_unread_forums()
  1807. #
  1808. # Returns list from the waiting table (Admins queue)
  1809. # ------------------------------------------------------------------
  1810. function sf_get_unread_forums()
  1811. {
  1812. global $wpdb;
  1813. return $wpdb->get_results(
  1814. "SELECT topic_id, ".SFWAITING.".forum_id, forum_slug, forum_name, group_id, ".SFWAITING.".post_count, ".SFWAITING.".post_id, topic_status_set
  1815. FROM ".SFFORUMS."
  1816. JOIN ".SFWAITING." ON ".SFFORUMS.".forum_id = ".SFWAITING.".forum_id
  1817. WHERE ".SFWAITING.".post_count > 0
  1818. ORDER BY forum_id, topic_id");
  1819. }
  1820. # ------------------------------------------------------------------
  1821. # sf_get_awaiting_approval()
  1822. #
  1823. # Count of posts currently awaiting moderation
  1824. # ------------------------------------------------------------------
  1825. function sf_get_awaiting_approval()
  1826. {
  1827. global $wpdb;
  1828. return $wpdb->get_var(
  1829. "SELECT COUNT(post_id) AS cnt
  1830. FROM ".SFPOSTS."
  1831. WHERE post_status=1");
  1832. }
  1833. # ------------------------------------------------------------------
  1834. # sf_topic_in_queue()
  1835. #
  1836. # returns true if the topic is in the admin queue
  1837. # $topicid topic being tested
  1838. # ------------------------------------------------------------------
  1839. function sf_topic_in_queue($topicid)
  1840. {
  1841. global $wpdb;
  1842. if(!$topicid) return '';
  1843. return $wpdb->get_var(
  1844. "SELECT post_count
  1845. FROM ".SFWAITING."
  1846. WHERE topic_id=".$topicid);
  1847. }
  1848. # ******************************************************************
  1849. # STATISTICS VIEWS DB FUNCTIONS
  1850. # ******************************************************************
  1851. # ------------------------------------------------------------------
  1852. # sf_get_online_total()
  1853. #
  1854. # Returns total number of users currently tagged as online
  1855. # ------------------------------------------------------------------
  1856. function sf_get_online_total()
  1857. {
  1858. global $wpdb;
  1859. return $wpdb->get_var("SELECT COUNT(id) FROM ".SFTRACK);
  1860. }
  1861. # ------------------------------------------------------------------
  1862. # sf_get_members_online()
  1863. #
  1864. # Returns list of members currently tagged as online
  1865. # ------------------------------------------------------------------
  1866. function sf_get_members_online()
  1867. {
  1868. global $wpdb;
  1869. return $wpdb->get_results("
  1870. SELECT trackuserid, display_name, forum_id, topic_id FROM ".SFTRACK."
  1871. JOIN ".SFMEMBERS." ON ".SFTRACK.".trackuserid = ".SFMEMBERS.".user_id
  1872. ORDER BY trackuserid");
  1873. }
  1874. # ------------------------------------------------------------------
  1875. # sf_is_online()
  1876. #
  1877. # Returns true if member is currently tagged as online
  1878. # ------------------------------------------------------------------
  1879. function sf_is_online($userid)
  1880. {
  1881. global $wpdb, $session_online;
  1882. if(!$userid) return '';
  1883. if(!isset($session_online))
  1884. {
  1885. $session_online = $wpdb->get_col("SELECT trackuserid FROM ".SFTRACK);
  1886. }
  1887. if(in_array($userid, $session_online)) return true;
  1888. return false;
  1889. }
  1890. # ------------------------------------------------------------------
  1891. # sf_get_stats_counts()
  1892. #
  1893. # Returns stats on group/forum/topic/post count
  1894. # ------------------------------------------------------------------
  1895. function sf_get_stats_counts()
  1896. {
  1897. global $wpdb;
  1898. $cnt->groups = 0;
  1899. $cnt->forums = 0;
  1900. $cnt->topics = 0;
  1901. $cnt->posts = 0;
  1902. $groupid='';
  1903. $forums = $wpdb->get_results("SELECT group_id, forum_id, topic_count, post_count FROM ".SFFORUMS." ORDER BY group_id");
  1904. if($forums)
  1905. {
  1906. foreach($forums as $forum)
  1907. {
  1908. if(sf_can_view_forum($forum->forum_id))
  1909. {
  1910. if($forum->group_id != $groupid)
  1911. {
  1912. $groupid=$forum->group_id;
  1913. $cnt->groups++;
  1914. }
  1915. $cnt->forums++;
  1916. $cnt->topics+=$forum->topic_count;
  1917. $cnt->posts+=$forum->post_count;
  1918. }
  1919. }
  1920. }
  1921. return $cnt;
  1922. }
  1923. # ------------------------------------------------------------------
  1924. # sf_get_post_stats()
  1925. #
  1926. # Returns stats on posts (admins/moderators and members and updates
  1927. # the guest count
  1928. # ------------------------------------------------------------------
  1929. function sf_get_post_stats()
  1930. {
  1931. global $wpdb;
  1932. $sfcontrols = sf_get_option('sfcontrols');
  1933. # if the hour has passed or been reset
  1934. if($sfcontrols['hourflag'] != date('G'))
  1935. {
  1936. # get admins and moderators
  1937. $records1 = $wpdb->get_results("
  1938. SELECT user_id, display_name, posts, admin, moderator
  1939. FROM ".SFMEMBERS."
  1940. WHERE admin=1 OR moderator=1
  1941. ORDER BY admin DESC, moderator DESC, posts DESC;");
  1942. # now get members
  1943. $records2 = $wpdb->get_results("
  1944. SELECT SQL_CALC_FOUND_ROWS user_id, display_name, posts, admin, moderator
  1945. FROM ".SFMEMBERS."
  1946. WHERE admin=0 AND moderator=0
  1947. ORDER BY posts DESC
  1948. LIMIT 0,".$sfcontrols['showtopcount'].";");
  1949. # save the members count
  1950. $sfcontrols['membercount'] = $wpdb->get_var("SELECT FOUND_ROWS()");
  1951. # Finally get guests
  1952. $guests = $wpdb->get_col("
  1953. SELECT DISTINCT guest_name
  1954. FROM ".SFPOSTS."
  1955. WHERE guest_name !='';");
  1956. # save the guest count
  1957. $sfcontrols['guestcount'] = count($guests);
  1958. # merge and save the records
  1959. $stats = array_merge($records1, $records2);
  1960. $sfcontrols['statslist'] = $stats;
  1961. # reset the hour flag and save the controls
  1962. $sfcontrols['hourflag'] = date('G');
  1963. sf_update_option('sfcontrols', $sfcontrols);
  1964. } else {
  1965. $stats = $sfcontrols['statslist'];
  1966. }
  1967. # return results
  1968. return $stats;
  1969. }
  1970. # ------------------------------------------------------------------
  1971. # sf_guests_browsing()
  1972. #
  1973. # Calculates how many guests are browsing current forum or topic
  1974. # ------------------------------------------------------------------
  1975. function sf_guests_browsing()
  1976. {
  1977. global $sfvars, $wpdb;
  1978. $where='';
  1979. if($sfvars['pageview']=='forum') $where = "forum_id=".$sfvars['forumid'];
  1980. if($sfvars['pageview']=='topic') $where = "topic_id=".$sfvars['topicid'];
  1981. if($where=='') return;
  1982. return $wpdb->get_var("
  1983. SELECT COUNT(id)
  1984. FROM ".SFTRACK." WHERE trackuserid=0 AND ".$where);
  1985. }
  1986. # ------------------------------------------------------------------
  1987. # sf_update_max_online()
  1988. #
  1989. # Updates max online setting if exceeded and returns value
  1990. # ------------------------------------------------------------------
  1991. function sf_update_max_online($current)
  1992. {
  1993. $sfcontrols = sf_get_option('sfcontrols');
  1994. $max = $sfcontrols['maxonline'];
  1995. if(empty($max)) $max = 0;
  1996. if($current > $max)
  1997. {
  1998. $sfcontrols['maxonline'] = $current;
  1999. sf_update_option('sfcontrols', $sfcontrols);
  2000. return $current;
  2001. }
  2002. return $max;
  2003. }
  2004. # ******************************************************************
  2005. # FULL TOPIC SEARCH VIEW DB FUNCTIONS
  2006. # ******************************************************************
  2007. # ------------------------------------------------------------------
  2008. # sf_get_combined_full_topic_search()
  2009. #
  2010. # Grabs all forums and their topics in the search result.
  2011. # Also performs the full search for a members postings
  2012. # ------------------------------------------------------------------
  2013. function sf_get_combined_full_topic_search()
  2014. {
  2015. global $wpdb, $sfvars, $current_user, $sfglobals;
  2016. $sfvars['searchresults'] = 0;
  2017. # how many topics per page?
  2018. $tpaged = $sfglobals['display']['topics']['perpage'];
  2019. if(!$tpaged) $tpaged=20;
  2020. if($sfvars['searchpage'] == 1)
  2021. {
  2022. $startlimit = 0;
  2023. } else {
  2024. $startlimit = ((($sfvars['searchpage']-1) * $tpaged));
  2025. }
  2026. $LIMIT = ' LIMIT '.$startlimit.', '.$tpaged;
  2027. $ORDER = ' ORDER BY topic_id DESC';
  2028. # for admins search all forums, for users check permissions
  2029. $where2 = '';
  2030. if (!$current_user->forumadmin)
  2031. {
  2032. # limit to viewable forums based on permissions
  2033. $forums = sf_get_forum_memberships($current_user->ID);
  2034. if ($forums)
  2035. {
  2036. $forum_ids = '';
  2037. foreach ($forums as $forum)
  2038. {
  2039. if (sf_can_view_forum($forum->forum_id))
  2040. {
  2041. $forum_ids[] = $forum->forum_id;
  2042. }
  2043. }
  2044. } else {
  2045. return '';
  2046. }
  2047. # create where clause based on forums that current user can view
  2048. if ($forum_ids != '')
  2049. {
  2050. $where2 = " AND ".SFTOPICS.".forum_id IN (" . implode(",", $forum_ids) . ") ";
  2051. } else {
  2052. return '';
  2053. }
  2054. }
  2055. $searchvalue=urldecode($sfvars['searchvalue']);
  2056. if(empty($searchvalue))
  2057. {
  2058. return '';
  2059. }
  2060. if($sfvars['searchtype'] == 8) {
  2061. # topics user has posted in
  2062. $userid = $sfvars['searchvalue'];
  2063. $records = sf_query_results(
  2064. "SELECT SQL_CALC_FOUND_ROWS DISTINCT
  2065. ".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,
  2066. forum_name, forum_slug
  2067. FROM ".SFTOPICS."
  2068. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  2069. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  2070. WHERE ".SFPOSTS.".user_id = ".$userid.$where2.$ORDER.$LIMIT.";", ARRAY_A);
  2071. } else if($sfvars['searchtype'] == 9) {
  2072. # tooics started by user
  2073. $userid = $sfvars['searchvalue'];
  2074. $records = sf_query_results(
  2075. "SELECT SQL_CALC_FOUND_ROWS DISTINCT
  2076. ".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,
  2077. forum_name, forum_slug, post_content
  2078. FROM ".SFTOPICS."
  2079. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  2080. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  2081. WHERE ".SFPOSTS.".user_id = ".$userid.$where2." AND ".SFPOSTS.".post_index=1 ".$ORDER.$LIMIT.";", ARRAY_A);
  2082. } elseif($sfvars['searchinclude'] == 4) {
  2083. # it's a tag search
  2084. $searchtag = sf_create_slug(urldecode($searchvalue), 'tag');
  2085. $WHERE = SFTOPICS.".topic_id IN (SELECT topic_id FROM ".SFTAGMETA." JOIN ".SFTAGS." ON ".SFTAGMETA.".tag_id = ".SFTAGS.".tag_id
  2086. WHERE tag_slug = '".$searchtag."') ";
  2087. $records = sf_query_results(
  2088. "SELECT SQL_CALC_FOUND_ROWS DISTINCT
  2089. ".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,
  2090. forum_name, forum_slug, post_content
  2091. FROM ".SFTOPICS."
  2092. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  2093. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  2094. WHERE ".$WHERE." AND ".SFPOSTS.".post_index=1 ".$ORDER.$LIMIT.";", ARRAY_A);
  2095. } else {
  2096. $searchterm = sf_construct_search_term($searchvalue, $sfvars['searchtype']);
  2097. switch($sfvars['searchinclude'])
  2098. {
  2099. case 1:
  2100. $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)) ";
  2101. break;
  2102. case 2:
  2103. $MATCH = "MATCH(".SFPOSTS.".post_content) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE) ";
  2104. break;
  2105. case 3:
  2106. $MATCH = "MATCH(".SFTOPICS.".topic_name) AGAINST ('".esc_sql(like_escape($searchterm))."' IN BOOLEAN MODE) ";
  2107. break;
  2108. }
  2109. $records = sf_query_results(
  2110. "SELECT SQL_CALC_FOUND_ROWS DISTINCT
  2111. ".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,
  2112. forum_name, forum_slug, post_content, post_status
  2113. FROM ".SFTOPICS."
  2114. JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id
  2115. JOIN ".SFPOSTS." ON ".SFTOPICS.".topic_id = ".SFPOSTS.".topic_id
  2116. WHERE ".$MATCH.$where2." AND ".SFPOSTS.".post_index=1 ".$ORDER.$LIMIT.";", ARRAY_A);
  2117. }
  2118. if($records)
  2119. {
  2120. $sfvars['searchresults'] = $wpdb->get_var("SELECT FOUND_ROWS()");
  2121. $sfdisplay=array();
  2122. $sfdisplay=sf_get_option('sfdisplay');
  2123. # do we need a post tooltip on the topic link? (Not possible on topics user hs posted in type search)
  2124. if($sfdisplay['topics']['posttip'] && $sfvars['searchtype'] != 8)
  2125. {
  2126. for($x=0; $x<count($records); $x++)
  2127. {
  2128. $records[$x]['post_tip']=sf_filter_tooltip_display($records[$x]['post_content'], $records[$x]['post_status']);
  2129. $records[$x]['post_content']='';
  2130. }
  2131. }
  2132. }
  2133. return $records;
  2134. }
  2135. # ******************************************************************
  2136. # USER RELATED DB FUNCTIONS
  2137. # ******************************************************************
  2138. # ------------------------------------------------------------------
  2139. # sf_create_member_data()
  2140. #
  2141. # Filter Call
  2142. # On user registration sets up the new 'members' data row
  2143. # $userid: Passed in to filter
  2144. # ------------------------------------------------------------------
  2145. function sf_create_member_data($userid)
  2146. {
  2147. global $wpdb;
  2148. if(!$userid) return '';
  2149. # Grab the data we need
  2150. $sfprofile = sf_get_option('sfprofile');
  2151. $user = $wpdb->get_row("SELECT user_login, display_name FROM ".SFUSERS." WHERE ID=".$userid);
  2152. # Display Name validation
  2153. if($sfprofile['nameformat'] == 3)
  2154. {
  2155. $display_name = get_user_meta($userid, 'first_name', true).' '.get_user_meta($userid, 'last_name', true);
  2156. }
  2157. if(empty($display_name) || $sfprofile['nameformat'] == 2)
  2158. {
  2159. $display_name = $user->user_login;
  2160. }
  2161. if($sfprofile['nameformat'] == 1)
  2162. {
  2163. $display_name = $user->display_name;
  2164. }
  2165. if(empty($display_name)) $display_name = $user->user_login;
  2166. $display_name = sf_filter_name_save($display_name);
  2167. $admin = 0;
  2168. $moderator = 0;
  2169. $pm = 0;
  2170. $avatar = 'a:1:{s:8:"uploaded";s:0:"";}';
  2171. $signature = '';
  2172. $posts = -1;
  2173. $lastvisit = current_time('mysql');
  2174. $checktime = current_time('mysql');
  2175. $subscribe = '';
  2176. $buddies = '';
  2177. $watches = '';
  2178. $newposts = '';
  2179. $posts_rated = '';
  2180. $admin_options = '';
  2181. $useropts = array();
  2182. $sfmemberopts = sf_get_option('sfmemberopts');
  2183. if ($sfmemberopts['sfautosub'])
  2184. {
  2185. $useropts['autosubpost'] = 1;
  2186. } else {
  2187. $useropts['autosubpost'] = 0;
  2188. }
  2189. $useropts['hidestatus'] = 0;
  2190. $useropts['timezone'] = sf_get_option('sfzone');
  2191. if(empty($useropts['timezone'])) $useropts['timezone']=0;
  2192. $useropts['editor'] = 1;
  2193. $useropts['pmemail'] = 1;
  2194. $useropts['namesync'] = 1;
  2195. $sfmemberopts = sf_get_option('sfmemberopts');
  2196. if ($sfmemberopts['sfautosub']) $useropts['autosubpost'] = 1;
  2197. $user_options = serialize($useropts);
  2198. # generate feedkey
  2199. $feedkey = sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  2200. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
  2201. mt_rand( 0, 0x0fff ) | 0x4000,
  2202. mt_rand( 0, 0x3fff ) | 0x8000,
  2203. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
  2204. # save initial record
  2205. $sql = "INSERT INTO ".SFMEMBERS."
  2206. (user_id, display_name, admin, moderator, pm, avatar, signature, posts, lastvisit, checktime, subscribe, buddies, watches, newposts, posts_rated, admin_options, user_options, feedkey)
  2207. VALUES
  2208. ({$userid}, '{$display_name}', {$admin}, {$moderator}, {$pm}, '{$avatar}', '{$signature}', {$posts}, '{$lastvisit}', '{$checktime}', '{$subscribe}', '{$buddies}', '{$watches}', '{$newposts}', '{$posts_rated}', '{$admin_options}', '{$user_options}', '{$feedkey}')";
  2209. $wpdb->query($sql);
  2210. # insert new user into usergroup based on wp role
  2211. sf_map_role_to_ug($userid);
  2212. return;
  2213. }
  2214. # ------------------------------------------------------------------
  2215. # sf_update_member_data()
  2216. #
  2217. # Filter Call
  2218. # On user wp profile updates, check if any spf stuff needs updating
  2219. # $userid: Passed in to filter
  2220. # ------------------------------------------------------------------
  2221. function sf_update_member_data($userid)
  2222. {
  2223. global $wpdb;
  2224. if(!$userid) return '';
  2225. # are we syncing display names between WP and SPF?
  2226. $options = sf_get_member_item($userid, 'user_options');
  2227. if ($options['namesync'] == true)
  2228. {
  2229. $display_name = $wpdb->get_var("SELECT display_name FROM ".SFUSERS." WHERE ID=".$userid);
  2230. sf_update_member_item($userid, 'display_name', $display_name);
  2231. }
  2232. return;
  2233. }
  2234. # ------------------------------------------------------------------
  2235. # sf_map_role_to_ug()
  2236. #
  2237. # helper function
  2238. # update usergroup memberships based on wp role
  2239. # $userid: id of user to check memberships
  2240. # ------------------------------------------------------------------
  2241. function sf_map_role_to_ug($userid, $role='')
  2242. {
  2243. # make sure user has been created first since wp role hook fires before create hook
  2244. if (!$userid || !sf_get_member_item($userid, 'user_id')) return '';
  2245. # get the user's wp role
  2246. if (empty($role))
  2247. {
  2248. $user = new WP_User($userid);
  2249. $roles = $user->roles;
  2250. } else {
  2251. $roles = (array) $role;
  2252. }
  2253. # grab the user group for that wp role
  2254. # if the role doesnt have a mapping, use the defaul user group for new members
  2255. foreach ($roles as $role)
  2256. {
  2257. $value = sf_get_sfmeta('default usergroup', $role);
  2258. if (!empty($value))
  2259. {
  2260. $ug = $value[0]['meta_value'];
  2261. } else {
  2262. $value = sf_get_sfmeta('default usergroup', 'sfmembers');
  2263. $ug = $value[0]['meta_value'];
  2264. }
  2265. sfc_add_membership($ug, $userid);
  2266. }
  2267. return;
  2268. }
  2269. # ------------------------------------------------------------------
  2270. # sf_delete_member_data()
  2271. #
  2272. # Filter Call
  2273. # On user deletion remove 'members' data row
  2274. # $userid: Passed in to filter
  2275. # ------------------------------------------------------------------
  2276. function sf_delete_member_data($userid)
  2277. {
  2278. global $wpdb;
  2279. if(!$userid) return '';
  2280. # 1: get users email address
  2281. $user_email = $wpdb->get_var("SELECT user_email FROM ".SFUSERS." WHERE ID=".$userid);
  2282. # 2: get the users display name from members table
  2283. $display_name = sf_get_member_item($userid, 'display_name');
  2284. # 3: Set user name and email to guest name and meail in all of their posts
  2285. $wpdb->query("UPDATE ".SFPOSTS." SET user_id=NULL, guest_name='".$display_name."', guest_email='".$user_email."' WHERE user_id=".$userid);
  2286. # 4: Remove PM messages
  2287. $wpdb->query("DELETE FROM ".SFMESSAGES." WHERE to_id=".$userid." OR from_id=".$userid);
  2288. # 5: Remove subscriptions
  2289. $subs = sf_get_member_item($userid, 'subscribe');
  2290. if (!empty($subs))
  2291. {
  2292. foreach ($subs as $sub)
  2293. {
  2294. sf_remove_subscription($sub, $userid);
  2295. }
  2296. }
  2297. # 6: Remove watches
  2298. $watches = sf_get_member_item($userid, 'watches');
  2299. if (!empty($watches))
  2300. {
  2301. foreach ($watches as $watch)
  2302. {
  2303. sf_remove_watch($watch, $userid);
  2304. }
  2305. }
  2306. # 7: Remove posts rated
  2307. $ratings = sf_get_member_item($userid, 'posts_rated');
  2308. if (!empty($ratings))
  2309. {
  2310. foreach ($ratings as $postid)
  2311. {
  2312. sf_remove_postrated($postid, $userid);
  2313. }
  2314. }
  2315. # 8: Remove from recent members list if present
  2316. sf_remove_newuser($userid);
  2317. # 8: Remove from Members table
  2318. $wpdb->query("DELETE FROM ".SFMEMBERS." WHERE user_id=".$userid);
  2319. # 9: Remove user group memberships
  2320. $wpdb->query("DELETE FROM ".SFMEMBERSHIPS." WHERE user_id=".$userid);
  2321. return;
  2322. }
  2323. # ------------------------------------------------------------------
  2324. # sf_track_logout()
  2325. #
  2326. # Filter Call
  2327. # Sets up the last visited upon user logout
  2328. # ------------------------------------------------------------------
  2329. function sf_track_logout()
  2330. {
  2331. global $wpdb, $current_user;
  2332. # re-use this for updating lastvisit (time at logout)
  2333. sf_set_last_visited($current_user->ID);
  2334. $wpdb->query("DELETE FROM ".SFTRACK." WHERE trackuserid=".$current_user->ID);
  2335. sf_destroy_users_newposts($current_user->ID);
  2336. return;
  2337. }
  2338. # ------------------------------------------------------------------
  2339. # sf_get_user_id()
  2340. #
  2341. # returns the id of a user
  2342. # $display_name: User to lookup
  2343. # ------------------------------------------------------------------
  2344. function sf_get_user_id($display_name)
  2345. {
  2346. global $wpdb;
  2347. if(!$display_name) return '';
  2348. return $wpdb->get_var(
  2349. "SELECT user_id
  2350. FROM ".SFMEMBERS."
  2351. WHERE display_name='".$display_name."'");
  2352. }
  2353. # ------------------------------------------------------------------
  2354. # sf_is_subscribed()
  2355. #
  2356. # determine if user already subscribed to topic
  2357. # $userid: User being looked up
  2358. # $topicid: Topic subscribed to
  2359. # ------------------------------------------------------------------
  2360. function sf_is_subscribed($userid, $topicid)
  2361. {
  2362. global $wpdb;
  2363. if(!$userid || !$topicid) return '';
  2364. $list = sf_get_member_item($userid, 'subscribe');
  2365. if (empty($list))
  2366. {
  2367. return false;
  2368. }
  2369. $found = false;
  2370. if (in_array($topicid, $list)) $found = true;
  2371. return $found;
  2372. }
  2373. # ------------------------------------------------------------------
  2374. # sf_is_watching()
  2375. #
  2376. # determine if user already watcing a topic
  2377. # $userid: User being looked up
  2378. # $topicid: Topic watching
  2379. # ------------------------------------------------------------------
  2380. function sf_is_watching($userid, $topicid)
  2381. {
  2382. global $wpdb;
  2383. if(!$userid || !$topicid) return '';
  2384. $list = sf_get_member_item($userid, 'watches');
  2385. if (empty($list))
  2386. {
  2387. return false;
  2388. }
  2389. $found = false;
  2390. if (in_array($topicid, $list)) $found = true;
  2391. return $found;
  2392. }
  2393. # ******************************************************************
  2394. # SAVE ITEM FUNCTIONS
  2395. # ******************************************************************
  2396. # ------------------------------------------------------------------
  2397. # sf_save_edited_post()
  2398. #
  2399. # Saves a forum post following an edit in the UI
  2400. # Values in POST variables
  2401. # ------------------------------------------------------------------
  2402. function sf_save_edited_post()
  2403. {
  2404. global $wpdb, $current_user;
  2405. # post content
  2406. $postcontent = $_POST['postitem'];
  2407. sf_process_hook('sf_hook_pre_edit_post_save', array($_POST["pid"], $postcontent, $current_user->ID));
  2408. $postcontent = sf_filter_content_save($postcontent, 'edit');
  2409. # post edit array
  2410. $postedit = array();
  2411. $pedit = stripslashes($_POST['pedit']);
  2412. if(!empty($pedit))
  2413. {
  2414. $postedit = unserialize($pedit);
  2415. }
  2416. $x = count($postedit);
  2417. $edittime = current_time('mysql');
  2418. $postedit[$x]['by'] = sf_filter_name_save($current_user->display_name);
  2419. $postedit[$x]['at'] = strtotime($edittime);
  2420. $postedit = serialize($postedit);
  2421. $sql = "UPDATE ".SFPOSTS." SET post_content='".$postcontent."', post_edit='".$postedit."' WHERE post_id=".sf_esc_int($_POST["pid"]);
  2422. if($wpdb->query($sql) === false)
  2423. {
  2424. update_sfnotice('sfmessage', '1@'.__("Update Failed!", "sforum"));
  2425. } else {
  2426. update_sfnotice('sfmessage', '0@'.__("Updated Post Saved", "sforum"));
  2427. }
  2428. # is it a blog linked topic post edit?
  2429. if(sf_esc_int($_POST['blogpid']) && $_POST['sfedit'])
  2430. {
  2431. $ID = sf_esc_int($_POST['blogpid']);
  2432. $post_content = $postcontent;
  2433. $post = compact('ID', 'post_content');
  2434. wp_update_post($post);
  2435. }
  2436. sf_process_hook('sf_hook_post_edit_post_save', array($_POST["pid"], $postcontent, $current_user->ID));
  2437. return;
  2438. }
  2439. # ------------------------------------------------------------------
  2440. # sf_save_edited_topic()
  2441. #
  2442. # Saves a topic title following an edit in the UI
  2443. # Values in POST variables
  2444. # ------------------------------------------------------------------
  2445. function sf_save_edited_topic()
  2446. {
  2447. global $wpdb, $sfglobals;
  2448. $topicname = sf_filter_title_save($_POST['topicname']);
  2449. if(empty($_POST['topicslug']))
  2450. {
  2451. $topicslug = sf_create_slug(sf_esc_str($_POST['topicname']), 'topic');
  2452. if(empty($topicslug)) $topicslug = 'topic-'.sf_esc_int($_POST['tid']);
  2453. } else {
  2454. $topicslug = sf_esc_str($_POST['topicslug']);
  2455. }
  2456. $sql = 'UPDATE '.SFTOPICS.' SET topic_name="'.$topicname.'", topic_slug="'.$topicslug.'" WHERE topic_id='.sf_esc_int($_POST['tid']);
  2457. if($wpdb->query($sql) === false)
  2458. {
  2459. update_sfnotice('sfmessage', '1@'.__("Update Failed!", "sforum"));
  2460. } else {
  2461. update_sfnotice('sfmessage', '0@'.__("Updated Topic Title Saved", "sforum"));
  2462. }
  2463. return;
  2464. }
  2465. # ------------------------------------------------------------------
  2466. # sf_save_subscription()
  2467. #
  2468. # Saves a user subscription following an edit in the UI
  2469. # $topicid: The topic being subscribed to
  2470. # $userid: The user (passed because used in upgrade flow)
  2471. # $retmessage: True/False: Whether to retrun message (for UI)
  2472. # ------------------------------------------------------------------
  2473. function sf_save_subscription($topicid, $userid, $retmessage)
  2474. {
  2475. global $wpdb, $current_user;
  2476. if(!$userid || !$topicid) return '';
  2477. if (($current_user->guest) || ($current_user->sfsubscriptions == false)) return;
  2478. # is user already subscribed to this topic?
  2479. if (sf_is_subscribed($userid, $topicid))
  2480. {
  2481. if($retmessage)
  2482. {
  2483. update_sfnotice('sfmessage', '1@'.__('You are already subscribed to this topic', "sforum"));
  2484. return;
  2485. }
  2486. }
  2487. # OK -subscribe them to the topic
  2488. $list = $wpdb->get_var("SELECT topic_subs FROM ".SFTOPICS." WHERE topic_id=".$topicid);
  2489. $list = unserialize($list);
  2490. $list[] = $userid;
  2491. $list = serialize($list);
  2492. $wpdb->query("UPDATE ".SFTOPICS." SET topic_subs = '".$list."' WHERE topic_id=".$topicid);
  2493. # plus note the topic against their usermeta record
  2494. $list = sf_get_member_item($userid, 'subscribe');
  2495. $list[] = $topicid;
  2496. sf_update_member_item($userid, 'subscribe', $list);
  2497. if($retmessage)
  2498. {
  2499. update_sfnotice('sfmessage', '0@'.__('Subscription added', "sforum"));
  2500. }
  2501. return;
  2502. }
  2503. # ------------------------------------------------------------------
  2504. # sf_save_watch()
  2505. #
  2506. # Saves a user watch topic request following an edit in the UI
  2507. # $topicid: The topic being watched
  2508. # $userid: The user (passed because used in upgrade flow)
  2509. # $retmessage: True/False: Whether to retrun message (for UI)
  2510. # ------------------------------------------------------------------
  2511. function sf_save_watch($topicid, $userid, $retmessage)
  2512. {
  2513. global $wpdb, $current_user;
  2514. if(!$userid || !$topicid) return '';
  2515. if (($current_user->guest) || ($current_user->sfwatch == false)) return;
  2516. # is user already watching this topic?
  2517. if (sf_is_watching($userid, $topicid))
  2518. {
  2519. if ($retmessage)
  2520. {
  2521. update_sfnotice('sfmessage', '1@'.__('You are already watching this topic', "sforum"));
  2522. return;
  2523. }
  2524. }
  2525. # OK - watch the topic (in members table)
  2526. $list = sf_get_member_item($userid, 'watches');
  2527. $list[] = $topicid;
  2528. sf_update_member_item($userid, 'watches', $list);
  2529. # OK -subscribe them to the topic
  2530. $list = $wpdb->get_var("SELECT topic_watches FROM ".SFTOPICS." WHERE topic_id=".$topicid);
  2531. $list = unserialize($list);
  2532. $list[] = $userid;
  2533. $list = serialize($list);
  2534. $wpdb->query("UPDATE ".SFTOPICS." SET topic_watches = '".$list."' WHERE topic_id=".$topicid);
  2535. if ($retmessage)
  2536. {
  2537. update_sfnotice('sfmessage', '0@'.__('Topic watch added', "sforum"));
  2538. }
  2539. return;
  2540. }
  2541. # ------------------------------------------------------------------
  2542. # sf_end_watch()
  2543. #
  2544. # Update watches from the UI - current user
  2545. # Values in POST variables
  2546. # ------------------------------------------------------------------
  2547. function sf_end_watch()
  2548. {
  2549. global $current_user, $wpdb, $sfglobals;
  2550. $topic = sf_esc_int($_GET['topic']);
  2551. # remove current watch from member
  2552. $list = $sfglobals['member']['watches'];
  2553. if (!empty($list))
  2554. {
  2555. $newlist = '';
  2556. foreach($list as $topicid)
  2557. {
  2558. if ($topic != $topicid)
  2559. {
  2560. $newlist[] = $topicid;
  2561. }
  2562. }
  2563. sf_update_member_item($current_user->ID, 'watches', $newlist);
  2564. }
  2565. #remove the topic watch
  2566. $list = $wpdb->get_var("SELECT topic_watches FROM ".SFTOPICS." WHERE topic_id=".$topic);
  2567. if (!empty($list))
  2568. {
  2569. $newlist = '';
  2570. $list = unserialize($list);
  2571. foreach ($list as $userid)
  2572. {
  2573. if ($current_user->ID != $userid)
  2574. {
  2575. $newlist[] = $userid;
  2576. }
  2577. }
  2578. if ($newlist != '') $newlist = serialize($newlist);
  2579. $list = $wpdb->query("UPDATE ".SFTOPICS." SET topic_watches ='".$newlist."' WHERE topic_id=".$topic);
  2580. }
  2581. update_sfnotice('sfmessage', '0@'.__('Watches Updated', "sforum"));
  2582. return;
  2583. }
  2584. # ------------------------------------------------------------------
  2585. # sf_end_sub()
  2586. #
  2587. # Update watches from the UI - current user
  2588. # Values in POST variables
  2589. # ------------------------------------------------------------------
  2590. function sf_end_sub()
  2591. {
  2592. global $current_user, $wpdb, $sfglobals;
  2593. $topic = sf_esc_int($_GET['topic']);
  2594. # remove current subscription from member
  2595. $list = $sfglobals['member']['subscribe'];
  2596. if (!empty($list))
  2597. {
  2598. $newlist = '';
  2599. foreach($list as $topicid)
  2600. {
  2601. if ($topic != $topicid)
  2602. {
  2603. $newlist[] = $topicid;
  2604. }
  2605. }
  2606. sf_update_member_item($current_user->ID, 'subscribe', $newlist);
  2607. }
  2608. #remove the topic subscription
  2609. $list = $wpdb->get_var("SELECT topic_subs FROM ".SFTOPICS." WHERE topic_id=".$topic);
  2610. if (!empty($list))
  2611. {
  2612. $newlist = '';
  2613. $list = unserialize($list);
  2614. foreach ($list as $userid)
  2615. {
  2616. if ($current_user->ID != $userid)
  2617. {
  2618. $newlist[] = $userid;
  2619. }
  2620. }
  2621. if ($newlist != '') $newlist = serialize($newlist);
  2622. $list = $wpdb->query("UPDATE ".SFTOPICS." SET topic_subs ='".$newlist."' WHERE topic_id=".$topic);
  2623. }
  2624. update_sfnotice('sfmessage', '0@'.__('Subscriptions Updated', "sforum"));
  2625. return;
  2626. }
  2627. # ------------------------------------------------------------------
  2628. # sf_remove_watch($topic, $userid)
  2629. #
  2630. # removes the topic watch for the specified user
  2631. # $topic topic to be removed
  2632. # $userid user to have watch removed
  2633. # ------------------------------------------------------------------
  2634. function sf_remove_watch($topic, $userid)
  2635. {
  2636. global $wpdb, $sfglobals;
  2637. if(!$userid || !$topic) return '';
  2638. # remove current watch from member
  2639. $list = $sfglobals['member']['watches'];
  2640. if (!empty($list))
  2641. {
  2642. $newlist = '';
  2643. foreach($list as $topicid)
  2644. {
  2645. if ($topic != $topicid)
  2646. {
  2647. $newlist[] = $topicid;
  2648. }
  2649. }
  2650. sf_update_member_item($userid, 'watches', $newlist);
  2651. }
  2652. #remove the topic subscription
  2653. $list = $wpdb->get_var("SELECT topic_watches FROM ".SFTOPICS." WHERE topic_id=".$topic);
  2654. if (!empty($list))
  2655. {
  2656. $newlist = '';
  2657. $list = unserialize($list);
  2658. foreach ($list as $user)
  2659. {
  2660. if ($userid != $user)
  2661. {
  2662. $newlist[] = $user;
  2663. }
  2664. }
  2665. if ($newlist != '') $newlist = serialize($newlist);
  2666. $list = $wpdb->query("UPDATE ".SFTOPICS." SET topic_watches ='".$newlist."' WHERE topic_id=".$topic);
  2667. }
  2668. return;
  2669. }
  2670. # ------------------------------------------------------------------
  2671. # sf_remove_user_watches($userid)
  2672. #
  2673. # removes all topic watches for the specified user
  2674. # $userid user to have all watches removed
  2675. # ------------------------------------------------------------------
  2676. function sf_remove_user_watches($userid)
  2677. {
  2678. global $current_user;
  2679. # make sure user is doing this
  2680. if ($userid == $current_user->ID)
  2681. {
  2682. # Remove watches
  2683. $watches = sf_get_member_item($userid, 'watches');
  2684. if (!empty($watches))
  2685. {
  2686. foreach ($watches as $watch)
  2687. {
  2688. sf_remove_watch($watch, $userid);
  2689. }
  2690. }
  2691. }
  2692. }
  2693. # ------------------------------------------------------------------
  2694. # sf_remove_user_subs($userid)
  2695. #
  2696. # removes all topic subs for the specified user
  2697. # $userid user to have subscriptions removed
  2698. # ------------------------------------------------------------------
  2699. function sf_remove_user_subs($userid)
  2700. {
  2701. global $current_user;
  2702. # make sure user is doing this
  2703. if ($userid == $current_user->ID)
  2704. {
  2705. # Remove subscriptions
  2706. $subs = sf_get_member_item($userid, 'subscribe');
  2707. if (!empty($subs))
  2708. {
  2709. foreach ($subs as $sub)
  2710. {
  2711. sf_remove_subscription($sub, $userid);
  2712. }
  2713. }
  2714. }
  2715. }
  2716. # ------------------------------------------------------------------
  2717. # sf_add_postrating_vote()
  2718. #
  2719. # Saves a user watch topic request following an edit in the UI
  2720. # $postid: The post being voted on
  2721. # ------------------------------------------------------------------
  2722. function sf_add_postrating_vote($postid)
  2723. {
  2724. global $current_user, $sfglobals;
  2725. if(!$postid) return '';
  2726. # record the post as voted (in members table)
  2727. $list = $sfglobals['member']['posts_rated'];
  2728. $list[] = $postid;
  2729. sf_update_member_item($current_user->ID, 'posts_rated', $list);
  2730. return;
  2731. }
  2732. # ------------------------------------------------------------------
  2733. # sf_remove_postrated($topic, $userid)
  2734. #
  2735. # removes the post rated id for the specified user
  2736. # $postid postid to be removed
  2737. # $userid user to have watche removed
  2738. # ------------------------------------------------------------------
  2739. function sf_remove_postrated($postid, $userid)
  2740. {
  2741. global $wpdb;
  2742. if(!$userid || !$postid) return '';
  2743. #remove the member id from post rated
  2744. $list = $wpdb->get_var("SELECT members FROM ".SFPOSTRATINGS." WHERE post_id=".$postid);
  2745. if (!empty($list))
  2746. {
  2747. $newlist = null;
  2748. $list = unserialize($list);
  2749. foreach ($list as $user)
  2750. {
  2751. if ($userid != $user)
  2752. {
  2753. $newlist[] = $user;
  2754. }
  2755. }
  2756. if ($newlist) $newlist = serialize($newlist);
  2757. $list = $wpdb->query("UPDATE ".SFPOSTRATINGS." SET members ='".$newlist."' WHERE post_id=".$postid);
  2758. }
  2759. return;
  2760. }
  2761. # ------------------------------------------------------------------
  2762. # sf_move_topic()
  2763. #
  2764. # Move topic from one forum to another
  2765. # Values in POST variables
  2766. # ------------------------------------------------------------------
  2767. function sf_move_topic()
  2768. {
  2769. global $wpdb, $current_user;
  2770. if(!$current_user->sfmovetopics)
  2771. {
  2772. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  2773. return;
  2774. }
  2775. if(empty($_POST['forumid']))
  2776. {
  2777. update_sfnotice('sfmessage', '1@'.__('Destination Forum not Selected', "sforum"));
  2778. return;
  2779. }
  2780. $currentforumid = sf_esc_int($_POST['currentforumid']);
  2781. $currenttopicid = sf_esc_int($_POST['currenttopicid']);
  2782. $targetforumid = sf_esc_int($_POST['forumid']);
  2783. # change topic record to new forum id
  2784. $wpdb->query("UPDATE ".SFTOPICS." SET forum_id = ".$targetforumid." WHERE topic_id=".$currenttopicid);
  2785. if($wpdb === false)
  2786. {
  2787. update_sfnotice('sfmessage', '1@'.__("Topic Move Failed", "sforum"));
  2788. return;
  2789. }
  2790. # check in 'waiting' to see if there is an unread post in there and change forum id if there is
  2791. $wpdb->query("UPDATE ".SFWAITING." SET forum_id = ".$targetforumid." WHERE topic_id=".$currenttopicid);
  2792. # change posts record(s) to new forum
  2793. $wpdb->query("UPDATE ".SFPOSTS." SET forum_id = ".$targetforumid." WHERE topic_id=".$currenttopicid);
  2794. # rebuild forum counts for old and new forums
  2795. sf_build_forum_index($currentforumid);
  2796. sf_build_forum_index($targetforumid);
  2797. # relink to new forum if blog linked topic
  2798. sf_relink_topic($currenttopicid, $currenttopicid, $targetforumid);
  2799. # Ok - do not like doing this but....
  2800. # There seems to have been times when a new post is made to the old forum id so we will now double check...
  2801. $checkposts = $wpdb->get_results("SELECT post_id FROM ".SFPOSTS." WHERE forum_id=".$currentforumid." AND topic_id=".$currenttopicid);
  2802. if($checkposts)
  2803. {
  2804. # made after most were moved
  2805. sf_move_topic();
  2806. } else {
  2807. if($wpdb === false)
  2808. {
  2809. update_sfnotice('sfmessage', '1@'.__("Topic Move Failed", "sforum"));
  2810. } else {
  2811. update_sfnotice('sfmessage', '0@'.__("Topic Moved", "sforum"));
  2812. }
  2813. }
  2814. return;
  2815. }
  2816. # ------------------------------------------------------------------
  2817. # sf_move_post()
  2818. #
  2819. # Move post from one topic and create new topic -same/another forum
  2820. # Values in POST variables
  2821. # ------------------------------------------------------------------
  2822. function sf_move_post()
  2823. {
  2824. global $wpdb, $current_user;
  2825. if(!$current_user->sfmoveposts)
  2826. {
  2827. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  2828. return;
  2829. }
  2830. if(empty($_POST['forumid']))
  2831. {
  2832. update_sfnotice('sfmessage', '1@'.__("Post move abandoned - No Forum selected!", "sforum"));
  2833. return;
  2834. }
  2835. if(empty($_POST['newtopicname']))
  2836. {
  2837. update_sfnotice('sfmessage', '1@'.__("Post move abandoned - No Topic Defined!", "sforum"));
  2838. return;
  2839. }
  2840. # extract data from POST
  2841. $postid = sf_esc_int($_POST['postid']);
  2842. $oldtopicid = sf_esc_int($_POST['oldtopicid']);
  2843. $oldforumid = sf_esc_int($_POST['oldforumid']);
  2844. $newforumid = sf_esc_int($_POST['forumid']);
  2845. $blogpostid = sf_esc_int($_POST['blogpostid']);
  2846. $newtopicname = sf_filter_title_save(trim(($_POST['newtopicname'])));
  2847. # start with creating the new topic
  2848. $newtopicslug = sf_create_slug($newtopicname, 'topic');
  2849. # now create the topic and post records
  2850. $wpdb->query(
  2851. "INSERT INTO ".SFTOPICS."
  2852. (topic_name, topic_slug, topic_date, forum_id, post_count, blog_post_id, post_id)
  2853. VALUES
  2854. ('".$newtopicname."', '".$newtopicslug."', now(), ".$newforumid.", 1, '".$blogpostid."', ".$postid.");");
  2855. if($wpdb === false)
  2856. {
  2857. update_sfnotice('sfmessage', '1@'.__("Post Move Failed", "sforum"));
  2858. return;
  2859. }
  2860. $newtopicid = $wpdb->insert_id;
  2861. # check the topic slug and if empty use the topic id
  2862. if(empty($newtopicslug))
  2863. {
  2864. $newtopicslug = 'topic-'.$newtopicid;
  2865. $thistopic = $wpdb->query("UPDATE ".SFTOPICS." SET topic_slug='".$slug."' WHERE topic_id=".$newtopicid);
  2866. }
  2867. # now check if old topic had just the one post and if so remove it
  2868. $check = $wpdb->get_var("SELECT post_count FROM ".SFTOPICS." WHERE topic_id=".$oldtopicid);
  2869. if($check == 1)
  2870. {
  2871. $wpdb->query("DELETE FROM ".SFTOPICS." WHERE topic_id=".$oldtopicid);
  2872. }
  2873. # update post record
  2874. $wpdb->query(
  2875. "UPDATE ".SFPOSTS."
  2876. SET topic_id=".$newtopicid.", forum_id=".$newforumid.", post_index=1, post_status=0
  2877. WHERE post_id=".$postid);
  2878. # If old topic was in the admin queue then remove it. Assume it's read
  2879. sf_remove_from_waiting(true, $oldtopicid, 0);
  2880. # rebuild forum counts for old and new forums
  2881. sf_build_forum_index($oldforumid);
  2882. sf_build_forum_index($newforumid);
  2883. sf_build_post_index($oldtopicid, sf_get_topic_slug($oldtopicid));
  2884. # relink to new forum if blog linked topic
  2885. sf_relink_topic($oldtopicid, $newtopicid, $newforumid);
  2886. if($wpdb == false)
  2887. {
  2888. update_sfnotice('sfmessage', '1@'.__("Post Move Failed", "sforum"));
  2889. } else {
  2890. update_sfnotice('sfmessage', '0@'.__("Post Moved", "sforum"));
  2891. }
  2892. return;
  2893. }
  2894. # ------------------------------------------------------------------
  2895. # sf_relink_topic()
  2896. #
  2897. # Relinks topic after a move
  2898. # $topicid original topic ID
  2899. # $newtopicid new id if moved post
  2900. # $newforumid new forum id
  2901. # ------------------------------------------------------------------
  2902. function sf_relink_topic($topicid, $newtopicid, $newforumid)
  2903. {
  2904. global $wpdb;
  2905. # Check if the target topic is a linked topic
  2906. $link = $wpdb->get_row("SELECT * FROM ".SFLINKS." WHERE topic_id=".$topicid);
  2907. if($link)
  2908. {
  2909. sf_blog_links_control('update', $link->post_id, $newforumid, $newtopicid, $link->syncedit);
  2910. }
  2911. return;
  2912. }
  2913. function sf_reassign_post()
  2914. {
  2915. global $wpdb, $current_user;
  2916. if (!$current_user->sfreassign)
  2917. {
  2918. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  2919. return;
  2920. }
  2921. $postid = sf_esc_int($_POST['postid']);
  2922. $olduserid = sf_esc_int($_POST['olduserid']);
  2923. $newuserid = sf_esc_int($_POST['newuserid']);
  2924. # transfer the post
  2925. $wpdb->query(
  2926. "UPDATE ".SFPOSTS."
  2927. SET user_id=".$newuserid."
  2928. WHERE post_id=".$postid);
  2929. # update old user post counts
  2930. $count = sf_get_member_item($olduserid, 'posts') - 1;
  2931. sf_update_member_item($olduserid, 'posts', $count);
  2932. # update new user post counts
  2933. $count = sf_get_member_item($newuserid, 'posts') + 1;
  2934. sf_update_member_item($newuserid, 'posts', $count);
  2935. }
  2936. function sf_change_topic_status()
  2937. {
  2938. global $wpdb, $current_user;
  2939. if(!$current_user->sfedit)
  2940. {
  2941. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  2942. return;
  2943. }
  2944. $topicid= sf_esc_int($_POST['id']);
  2945. $statvalue = sf_esc_int($_POST['statvalue']);
  2946. sf_update_topic_status_flag($statvalue, $topicid);
  2947. update_sfnotice('sfmessage', '0@'.__("Topic Status Changed", "sforum"));
  2948. return;
  2949. }
  2950. # ------------------------------------------------------------------
  2951. # sf_update_opened()
  2952. #
  2953. # Updates the number of times a topic is viewed
  2954. # $topicid: The topic being opened for view
  2955. # ------------------------------------------------------------------
  2956. function sf_update_opened($topicid)
  2957. {
  2958. global $wpdb, $sfvars;
  2959. if(empty($topicid)) return;
  2960. $ref=array();
  2961. $ref=explode('/', $_SERVER['HTTP_REFERER']);
  2962. $ref_topic = $ref[count($ref)-3];
  2963. if(substr($ref_topic,0,5) == 'page-') $ref_topic = $ref[count($ref)-4];
  2964. if($ref_topic == $sfvars['topicslug']) return;
  2965. $current=$wpdb->get_var("SELECT topic_opened FROM ".SFTOPICS." WHERE topic_id=".$topicid);
  2966. $current++;
  2967. $wpdb->query("UPDATE ".SFTOPICS." SET topic_opened = ".$current." WHERE topic_id=".$topicid);
  2968. return;
  2969. }
  2970. # ******************************************************************
  2971. # DELETE ITEM FUNCTIONS
  2972. # ******************************************************************
  2973. # ------------------------------------------------------------------
  2974. # sf_delete_topic()
  2975. #
  2976. # Delete a topic and all it;s posts
  2977. # $topicid: The topic being subscribed to
  2978. # $show: True/False: Whether to return message (for UI)
  2979. # ------------------------------------------------------------------
  2980. function sf_delete_topic($topicid, $show=true)
  2981. {
  2982. global $wpdb, $current_user;
  2983. if(!$topicid) return '';
  2984. if (!$current_user->sfdelete && !sf_is_forum_admin($current_user->ID) && !$current_user->sfdeleteown)
  2985. {
  2986. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  2987. return;
  2988. }
  2989. # We need to check for subscriptions on this topic
  2990. $subs = $wpdb->get_var("SELECT topic_subs FROM ".SFTOPICS." WHERE topic_id = ".$topicid);
  2991. # Any subscriptions to remopve from user records?
  2992. if ($subs)
  2993. {
  2994. $userlist = unserialize($subs);
  2995. foreach ($userlist as $user)
  2996. {
  2997. $topiclist = sf_get_member_item($user, 'subscribe');
  2998. if (!empty($topiclist))
  2999. {
  3000. $newlist = '';
  3001. foreach ($topiclist as $topic)
  3002. {
  3003. if ($topic != $topicid) $newlist[] = $topic;
  3004. }
  3005. sf_update_member_item($user, 'subscribe', $newlist);
  3006. }
  3007. }
  3008. }
  3009. # Any watches to remove?
  3010. $watches = $wpdb->get_var("SELECT topic_watches FROM ".SFTOPICS." WHERE topic_id = ".$topicid);
  3011. if ($watches)
  3012. {
  3013. $userlist = unserialize($watches);
  3014. foreach ($userlist as $user)
  3015. {
  3016. $topiclist = sf_get_member_item($user, 'watches');
  3017. if (!empty($topiclist))
  3018. {
  3019. $newlist = '';
  3020. foreach ($topiclist as $topic)
  3021. {
  3022. if ($topic != $topicid) $newlist[] = $topic;
  3023. }
  3024. sf_update_member_item($user, 'watches', $newlist);
  3025. }
  3026. }
  3027. }
  3028. # remove any post ratings in this topic
  3029. $rated = $wpdb->get_results("SELECT ".SFPOSTRATINGS.".post_id, members FROM ".SFPOSTRATINGS."
  3030. JOIN ".SFPOSTS." ON ".SFPOSTRATINGS.".post_id = ".SFPOSTS.".post_id
  3031. WHERE ".SFPOSTS.".topic_id=".$topicid);
  3032. if ($rated)
  3033. {
  3034. foreach ($rated as $post)
  3035. {
  3036. # remove the post rating
  3037. $wpdb->query("DELETE FROM ".SFPOSTRATINGS." WHERE post_id=".$post->post_id);
  3038. # remove the posts rated data from sfmembers table
  3039. $members = unserialize($post->members);
  3040. if ($members)
  3041. {
  3042. # loop through each members that rated a post in this topic and remove the rating
  3043. foreach ($members as $member)
  3044. {
  3045. $ratedposts = $wpdb->get_var("SELECT posts_rated FROM ".SFMEMBERS." WHERE user_id=".$member);
  3046. if ($ratedposts)
  3047. {
  3048. # save any other rated posts only removing posts from this topic
  3049. $new_ratings = '';
  3050. $ratedposts = unserialize($ratedposts);
  3051. foreach ($ratedposts as $ratedpost)
  3052. {
  3053. if ($ratedpost != $post->post_id)
  3054. {
  3055. $new_ratings[] = $ratedpost;
  3056. }
  3057. }
  3058. if ($new_ratings == '')
  3059. {
  3060. $wpdb->query("UPDATE ".SFMEMBERS." SET posts_rated=NULL WHERE user_id=".$member);
  3061. } else {
  3062. $new_ratings = serialize($new_ratings);
  3063. $wpdb->query("UPDATE ".SFMEMBERS." SET posts_rated='".$new_ratings."' WHERE user_id=".$member);
  3064. }
  3065. }
  3066. }
  3067. }
  3068. }
  3069. }
  3070. # check of there is a post link to it?
  3071. $row = $wpdb->get_row("SELECT blog_post_id, forum_id FROM ".SFTOPICS." WHERE topic_id = ".$topicid);
  3072. if($row->blog_post_id != 0)
  3073. {
  3074. # break the link
  3075. include_once(SF_PLUGIN_DIR.'/linking/library/sf-links-support.php');
  3076. sf_blog_links_control('delete', $row->blog_post_id);
  3077. }
  3078. # delete from waiting just in case
  3079. $wpdb->query("DELETE FROM ".SFWAITING." WHERE topic_id=".$topicid);
  3080. # now delete from topic
  3081. $wpdb->query("DELETE FROM ".SFTOPICS." WHERE topic_id=".$topicid);
  3082. if($wpdb === false)
  3083. {
  3084. if($show) update_sfnotice('sfmessage', '1@'.__("Deletion Failed", "sforum"));
  3085. return;
  3086. }
  3087. # topic delete hook
  3088. if(function_exists('sf_hook_topic_delete'))
  3089. {
  3090. # grab the forum id
  3091. $forumid = $wpdb->get_var("SELECT forum_id FROM ".SFTOPICS." WHERE topic_id = ".$topicid);
  3092. sf_process_hook('sf_hook_topic_delete', array($topicid, $forumid));
  3093. }
  3094. # now delete all the posts on the topic
  3095. $wpdb->query("DELETE FROM ".SFPOSTS." WHERE topic_id=".$topicid);
  3096. if($wpdb == false)
  3097. {
  3098. if($show) update_sfnotice('sfmessage', '1@'.__("Deletion of Posts in Topic Failed", "sforum"));
  3099. } else {
  3100. if($show) update_sfnotice('sfmessage', '0@'.__("Topic Deleted", "sforum"));
  3101. }
  3102. # delete from forums topic count
  3103. sf_build_forum_index($row->forum_id);
  3104. return;
  3105. }
  3106. # ------------------------------------------------------------------
  3107. # sf_delete_post()
  3108. #
  3109. # Delete a post
  3110. # $postid: The post to be deleted
  3111. # $topicid: The topic post belongs to
  3112. # $forumid: The forum post belongs to
  3113. # ------------------------------------------------------------------
  3114. function sf_delete_post($postid, $topicid, $forumid, $show=true, $poster=0)
  3115. {
  3116. global $wpdb, $current_user;
  3117. if(!$postid || !$topicid || !$forumid) return '';
  3118. if(!$current_user->sfdelete && !($current_user->sfdeleteown && $current_user->ID == $poster))
  3119. {
  3120. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  3121. return;
  3122. }
  3123. # if just one post then remove topic as well
  3124. if(sf_get_posts_count_in_topic($topicid) == 1)
  3125. {
  3126. sf_delete_topic($topicid);
  3127. } else {
  3128. # remove post ratings
  3129. $rated = $wpdb->get_var("SELECT members FROM ".SFPOSTRATINGS." WHERE post_id=".$postid);
  3130. if ($rated)
  3131. {
  3132. $members = unserialize($rated);
  3133. if ($members)
  3134. {
  3135. # loop through each members that rated a post in this topic and remove the rating
  3136. foreach ($members as $member)
  3137. {
  3138. $ratedposts = $wpdb->get_var("SELECT posts_rated FROM ".SFMEMBERS." WHERE user_id=".$member);
  3139. if ($ratedposts)
  3140. {
  3141. # save any other rated posts only removing posts from this topic
  3142. $new_ratings = '';
  3143. $ratedposts = unserialize($ratedposts);
  3144. foreach ($ratedposts as $ratedpost)
  3145. {
  3146. if ($ratedpost != $postid)
  3147. {
  3148. $new_ratings[] = $ratedpost;
  3149. }
  3150. }
  3151. if ($new_ratings == '')
  3152. {
  3153. $wpdb->query("UPDATE ".SFMEMBERS." SET posts_rated=NULL WHERE user_id=".$member);
  3154. } else {
  3155. $new_ratings = serialize($new_ratings);
  3156. $wpdb->query("UPDATE ".SFMEMBERS." SET posts_rated='".$new_ratings."' WHERE user_id=".$member);
  3157. }
  3158. }
  3159. }
  3160. }
  3161. }
  3162. $wpdb->query("DELETE FROM ".SFPOSTRATINGS." WHERE post_id=".$postid);
  3163. $wpdb->query("DELETE FROM ".SFPOSTS." WHERE post_id=".$postid);
  3164. if($wpdb === false)
  3165. {
  3166. if($show) update_sfnotice('sfmessage', '1@'.__("Deletion Failed", "sforum"));
  3167. } else {
  3168. if($show) update_sfnotice('sfmessage', '0@'.__("Post Deleted", "sforum"));
  3169. }
  3170. # re number post index
  3171. sf_build_post_index($topicid, sf_get_topic_slug($topicid));
  3172. sf_build_forum_index($forumid);
  3173. }
  3174. # post delete hook
  3175. sf_process_hook('sf_hook_post_delete', array($postid, $topicid, $forumid));
  3176. # need to look in sfwaiting to see if it's in there...
  3177. sf_remove_from_waiting(true, $topicid, $postid);
  3178. return;
  3179. }
  3180. # ------------------------------------------------------------------
  3181. # sf_remove_subscription()
  3182. #
  3183. # Removes a user subscription following edit or topic delete.
  3184. # $topicid: The topic being unsubscribed from
  3185. # $userid: The user (passed because used in upgrade flow)
  3186. # ------------------------------------------------------------------
  3187. function sf_remove_subscription($topic, $userid)
  3188. {
  3189. global $wpdb, $sfglobals;
  3190. if(!$userid || !$topic) return '';
  3191. # remove current subscription from member
  3192. $list = sf_get_member_item($userid, 'subscribe');
  3193. if (!empty($list))
  3194. {
  3195. $newlist = '';
  3196. foreach ($list as $topicid)
  3197. {
  3198. if ($topic != $topicid)
  3199. {
  3200. $newlist[] = $topicid;
  3201. }
  3202. }
  3203. sf_update_member_item($userid, 'subscribe', $newlist);
  3204. }
  3205. $list = $wpdb->get_var("SELECT topic_subs FROM ".SFTOPICS." WHERE topic_id=".$topic);
  3206. if (!empty($list))
  3207. {
  3208. $newlist = '';
  3209. $list = unserialize($list);
  3210. foreach ($list as $user)
  3211. {
  3212. if ($userid != $user)
  3213. {
  3214. $newlist[] = $user;
  3215. }
  3216. }
  3217. if ($newlist != '') $newlist = serialize($newlist);
  3218. $wpdb->query("UPDATE ".SFTOPICS." SET topic_subs = '".$newlist."' WHERE topic_id=".$topic);
  3219. }
  3220. return;
  3221. }
  3222. # ******************************************************************
  3223. # EDIT TOOL ICONS
  3224. # ******************************************************************
  3225. # ------------------------------------------------------------------
  3226. # sf_icon_toggle()
  3227. #
  3228. # Toggle Tool Icon State
  3229. # ------------------------------------------------------------------
  3230. function sf_icon_toggle()
  3231. {
  3232. global $sfglobals;
  3233. $sfadminsettings = array();
  3234. $sfadminsettings = sf_get_option('sfadminsettings');
  3235. $state = $sfadminsettings['sftools'];
  3236. if ($state ? $state = false : $state = true);
  3237. $sfadminsettings['sftools'] = $state;
  3238. sf_update_option('sfadminsettings', $sfadminsettings);
  3239. $sfglobals['admin']['sftools'] = $state;
  3240. return;
  3241. }
  3242. # ------------------------------------------------------------------
  3243. # sf_lock_topic_toggle()
  3244. #
  3245. # Toggle Topic Lock
  3246. # Topicid: Topic to lock/unlock
  3247. # ------------------------------------------------------------------
  3248. function sf_lock_topic_toggle($topicid)
  3249. {
  3250. global $wpdb, $current_user;
  3251. if(!$topicid) return '';
  3252. if(!$current_user->sflock)
  3253. {
  3254. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  3255. return;
  3256. }
  3257. if($_POST['locktopicaction'].$topicid == get_sfsetting('sfaction')) return;
  3258. $status = $wpdb->get_var("SELECT topic_status FROM ".SFTOPICS." WHERE topic_id=".sf_esc_int($topicid));
  3259. if($status == 1 ? $status=0 : $status=1);
  3260. $wpdb->query("UPDATE ".SFTOPICS." SET topic_status = ".$status." WHERE topic_id=".sf_esc_int($topicid));
  3261. if($wpdb == false)
  3262. {
  3263. update_sfnotice('sfmessage', '1@'.__("Topic Lock Toggle Failed", "sforum"));
  3264. } else {
  3265. update_sfnotice('sfmessage', '0@'.__("Topic Lock Toggled", "sforum"));
  3266. update_sfsetting('sfaction', sf_esc_str($_POST['locktopicaction'].$topicid));
  3267. }
  3268. return;
  3269. }
  3270. # ------------------------------------------------------------------
  3271. # sf_pin_topic_toggle()
  3272. #
  3273. # Toggle Topic Pin
  3274. # Topicid: Topic to pin/unpin
  3275. # ------------------------------------------------------------------
  3276. function sf_pin_topic_toggle($topicid)
  3277. {
  3278. global $wpdb, $current_user;
  3279. if(!$topicid) return '';
  3280. if(!$current_user->sfpintopics)
  3281. {
  3282. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  3283. return;
  3284. }
  3285. if($_POST['pintopicaction'].$topicid == get_sfsetting('sfaction')) return;
  3286. $status = $wpdb->get_var("SELECT topic_pinned FROM ".SFTOPICS." WHERE topic_id=".sf_esc_int($topicid));
  3287. if($status == 1 ? $status=0 : $status=1);
  3288. $wpdb->query("UPDATE ".SFTOPICS." SET topic_pinned = ".$status." WHERE topic_id=".sf_esc_int($topicid));
  3289. if($wpdb == false)
  3290. {
  3291. update_sfnotice('sfmessage', '1@'.__("Topic Pin Toggle Failed", "sforum"));
  3292. } else {
  3293. update_sfnotice('sfmessage', '0@'.__("Topic Pin Toggled", "sforum"));
  3294. update_sfsetting('sfaction', sf_esc_str($_POST['pintopicaction'].$topicid));
  3295. }
  3296. return;
  3297. }
  3298. # ------------------------------------------------------------------
  3299. # sf_pin_post_toggle()
  3300. #
  3301. # Toggle Post Pin
  3302. # postid: Post to pin/unpin
  3303. # ------------------------------------------------------------------
  3304. function sf_pin_post_toggle($postid)
  3305. {
  3306. global $wpdb, $current_user;
  3307. if(!$postid) return '';
  3308. if(!$current_user->sfpinposts)
  3309. {
  3310. update_sfnotice('sfmessage', '1@'.__('Access Denied', "sforum"));
  3311. return;
  3312. }
  3313. if($_POST['pinpostaction'].$postid == get_sfsetting('sfaction')) return;
  3314. $status = $wpdb->get_var("SELECT post_pinned FROM ".SFPOSTS." WHERE post_id=".$postid);
  3315. if($status == 1 ? $status = 0 : $status = 1);
  3316. $wpdb->query("UPDATE ".SFPOSTS." SET post_pinned = ".$status." WHERE post_id=".sf_esc_int($postid));
  3317. if($wpdb == false)
  3318. {
  3319. update_sfnotice('sfmessage', '1@'.__("Post Pin Toggle Failed", "sforum"));
  3320. } else {
  3321. update_sfnotice('sfmessage', '0@'.__("Post Pin Toggled", "sforum"));
  3322. update_sfsetting('sfaction', sf_esc_str($_POST['pinpostaction']).$postid);
  3323. }
  3324. return;
  3325. }
  3326. # ******************************************************************
  3327. # DATA INTEGRITY MANAGEMENT
  3328. # ******************************************************************
  3329. # ------------------------------------------------------------------
  3330. # sf_build_post_index()
  3331. #
  3332. # Rebuilds the post index column (post sequence) and also sets the
  3333. # last post id and post count into the parent topic record
  3334. # $topicid: topic whose posts are being re-indexed
  3335. # $topicslug: slug to check sort order
  3336. # ------------------------------------------------------------------
  3337. function sf_build_post_index($topicid, $topicslug, $returnmsg=false)
  3338. {
  3339. global $wpdb;
  3340. if(!$topicid || !$topicslug) return '';
  3341. # get topic posts is their display order
  3342. $posts=$wpdb->get_results("SELECT post_id, post_index FROM ".SFPOSTS." WHERE topic_id = ".$topicid." ORDER BY post_pinned DESC, post_id ASC");
  3343. if($posts)
  3344. {
  3345. $index = 1;
  3346. foreach($posts as $post)
  3347. {
  3348. # update the post_index for each post to set display order
  3349. $wpdb->query("UPDATE ".SFPOSTS." SET post_index = ".$index." WHERE post_id = ".$post->post_id);
  3350. $index++;
  3351. }
  3352. $lastpost = $post->post_id;
  3353. } else {
  3354. $lastpost = 'NULL';
  3355. }
  3356. # update the topic with the last post id and the post count
  3357. $wpdb->query("UPDATE ".SFTOPICS." SET post_id=".$lastpost.", post_count=".($index-1)." WHERE topic_id=".$topicid);
  3358. if($returnmsg) update_sfnotice('sfmessage', '0@'.__("Verification Complete", "sforum"));
  3359. return;
  3360. }
  3361. # ------------------------------------------------------------------
  3362. # sf_build_forum_index()
  3363. #
  3364. # Rebuilds the topic count and last post id in a forum record
  3365. # $forumid: forum needing updating
  3366. # ------------------------------------------------------------------
  3367. function sf_build_forum_index($forumid, $returnmsg=false)
  3368. {
  3369. global $wpdb;
  3370. if(!$forumid) return '';
  3371. # get the topic and post counts for this forum
  3372. $topiccount = $wpdb->get_var("SELECT COUNT(topic_id) FROM ".SFTOPICS." WHERE forum_id=".$forumid);
  3373. $postcount = $wpdb->get_var("SELECT COUNT(post_id) FROM ".SFPOSTS." WHERE forum_id=".$forumid);
  3374. # get the last post that appeared in a topic within this forum
  3375. $postid = $wpdb->get_var("SELECT post_id FROM ".SFPOSTS." WHERE forum_id = ".$forumid." ORDER BY post_id DESC LIMIT 1");
  3376. if(!$topiccount) $topiccount = 0;
  3377. if(!$postcount) $postcount = 0;
  3378. if(!isset($postid)) $postid = 'NULL';
  3379. # update forum record
  3380. $wpdb->query("UPDATE ".SFFORUMS." SET post_id=".$postid.", post_count=".$postcount.", topic_count=".$topiccount." WHERE forum_id=".$forumid);
  3381. if($returnmsg) update_sfnotice('sfmessage', '0@'.__("Verification Complete", "sforum"));
  3382. return;
  3383. }
  3384. function sf_get_user_memberships($user_id)
  3385. {
  3386. global $wpdb;
  3387. if(!$user_id) return '';
  3388. $sql = "SELECT ".SFMEMBERSHIPS.".usergroup_id, usergroup_name, usergroup_desc
  3389. FROM ".SFMEMBERSHIPS."
  3390. JOIN ".SFUSERGROUPS." ON ".SFUSERGROUPS.".usergroup_id = ".SFMEMBERSHIPS.".usergroup_id
  3391. WHERE user_id=".$user_id;
  3392. return $wpdb->get_results($sql, ARRAY_A);
  3393. }
  3394. function sf_check_membership($usergroup_id, $user_id)
  3395. {
  3396. global $wpdb;
  3397. if(!$usergroup_id || !$user_id) return '';
  3398. $sql = "SELECT usergroup_id
  3399. FROM ".SFMEMBERSHIPS."
  3400. WHERE user_id=".$user_id." AND usergroup_id=".$usergroup_id;
  3401. return $wpdb->get_results($sql, ARRAY_A);
  3402. }
  3403. function sf_get_pm_inbox_new_count($userid)
  3404. {
  3405. global $wpdb, $sfglobals;
  3406. if(!$userid) return '';
  3407. return $wpdb->get_var("SELECT COUNT(*) FROM ".SFMESSAGES." WHERE to_id = ".$userid." AND inbox=1 AND message_status=0");
  3408. }
  3409. function sf_change_topic_tags($topicid, $newtags)
  3410. {
  3411. global $wpdb, $sfglobals;
  3412. if(!$topicid || !$newtags) return '';
  3413. # remove any existing tags for the topic
  3414. $oldtags = $wpdb->get_results("SELECT tag_id FROM ".SFTAGMETA." WHERE topic_id=".$topicid);
  3415. foreach ($oldtags as $oldtag)
  3416. {
  3417. # grab all the tag rows and decrement the tag count
  3418. $tagcount = $wpdb->get_var("SELECT tag_count FROM ".SFTAGS." WHERE tag_id=".$oldtag->tag_id);
  3419. # decrement tag count and delete if it gets to zero or update the new count
  3420. $tagcount--;
  3421. if ($tagcount == 0)
  3422. {
  3423. $wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$oldtag->tag_id); # count is zero so delete
  3424. } else {
  3425. $wpdb->query("UPDATE ".SFTAGS." SET tag_count=".$tagcount." WHERE tag_id=".$oldtag->tag_id); # update count
  3426. }
  3427. # remove all the tag meta entries for the topic
  3428. $wpdb->query("DELETE FROM ".SFTAGMETA." WHERE topic_id=".$topicid);
  3429. }
  3430. # now add in the updated tags
  3431. $tags = trim($newtags);
  3432. $tags = trim($tags, ','); # no extra commas allowed
  3433. $tags = explode(',', $tags);
  3434. $tags = array_unique($tags); # remove any duplicates
  3435. $tags = array_values($tags); # put back in order
  3436. if ($sfglobals['display']['topics']['maxtags'] > 0 && count($tags) > $sfglobals['display']['topics']['maxtags'])
  3437. {
  3438. $tags = array_slice($tags, 0, $sfglobals['display']['topics']['maxtags']); # limit to maxt tags opton
  3439. }
  3440. sfc_add_tags($topicid, $tags);
  3441. }
  3442. function sf_mark_all_read()
  3443. {
  3444. global $wpdb, $current_user;
  3445. # just to be safe, make sure a member called
  3446. if ($current_user->member)
  3447. {
  3448. sf_destroy_users_newposts($current_user->ID);
  3449. }
  3450. }
  3451. # ------------------------------------------------------------------
  3452. # sf_get_blog_title_from_id()
  3453. #
  3454. # Returns the post title of a blog post from the post id
  3455. # $postid: blog post target id
  3456. # ------------------------------------------------------------------
  3457. function sf_get_blog_title_from_id($postid)
  3458. {
  3459. global $wpdb;
  3460. if(!$postid) return '';
  3461. return $wpdb->get_var(
  3462. "SELECT post_title
  3463. FROM ".$wpdb->prefix."posts
  3464. WHERE ID=".$postid);
  3465. }
  3466. # ------------------------------------------------------------------
  3467. # sf_query_results()
  3468. #
  3469. # Returns the query if good - displays error if sql invalid
  3470. # 4.2 Just used on two search queries
  3471. # $sql: query string
  3472. # $type: return type - defaults to OBJECT
  3473. # ------------------------------------------------------------------
  3474. function sf_query_results($sql, $type=OBJECT)
  3475. {
  3476. global $wpdb;
  3477. $wpdb->hide_errors();
  3478. $records = $wpdb->get_results($sql, $type);
  3479. if($wpdb->last_error == '')
  3480. {
  3481. return $records;
  3482. } else {
  3483. update_sfnotice('sfmessage', '1@'.__("Invalid Database Query", "sforum"));
  3484. echo sf_render_queued_message();
  3485. return '';
  3486. }
  3487. }
  3488. ?>