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

/src/site/tmp/install_4a925da139185/admin/plugins/phpbb3/forum.php

https://bitbucket.org/manchas/jrobotz
PHP | 730 lines | 505 code | 92 blank | 133 comment | 64 complexity | ae81523e85e77eaf621af046fbe06ee4 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. * @package JFusion_phpBB3
  4. * @author JFusion development team
  5. * @copyright Copyright (C) 2008 JFusion. All rights reserved.
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  7. */
  8. // no direct access
  9. defined('_JEXEC' ) or die('Restricted access' );
  10. /**
  11. * JFusion Forum Class for phpBB3
  12. * For detailed descriptions on these functions please check the model.abstractforum.php
  13. * @package JFusion_phpBB3
  14. */
  15. class JFusionForum_phpbb3 extends JFusionForum
  16. {
  17. var $joomlaGlobals;
  18. function getJname()
  19. {
  20. return 'phpbb3';
  21. }
  22. function getThreadURL($threadid)
  23. {
  24. return 'viewtopic.php?t=' . $threadid;
  25. }
  26. function getPostURL($threadid, $postid)
  27. {
  28. return 'viewtopic.php?p='.$postid.'#p' . $postid;
  29. }
  30. function getProfileURL($uid)
  31. {
  32. return 'memberlist.php?mode=viewprofile&u='.$uid;
  33. }
  34. function getPrivateMessageURL()
  35. {
  36. return 'ucp.php?i=pm&folder=inbox';
  37. }
  38. function getViewNewMessagesURL()
  39. {
  40. return 'search.php?search_id=newposts';
  41. }
  42. function getAvatar($puser_id)
  43. {
  44. if ($puser_id) {
  45. $dbparams = JFusionFactory::getParams($this->getJname());
  46. $db = JFusionFactory::getDatabase($this->getJname());
  47. $db->setQuery('SELECT user_avatar, user_avatar_type FROM #__users WHERE user_id='.$puser_id);
  48. $db->query();
  49. $result = $db->loadObject();
  50. if (!empty($result)) {
  51. if ($result->user_avatar_type == 1) {
  52. // AVATAR_UPLOAD
  53. $url = $dbparams->get('source_url').'download/file.php?avatar='.$result->user_avatar;
  54. } else if ($result->user_avatar_type == 3) {
  55. // AVATAR_GALLERY
  56. $db->setQuery("SELECT config_value FROM #__config WHERE config_name='avatar_gallery_path'");
  57. $db->query();
  58. $path = $db->loadResult();
  59. if (!empty($path)) {
  60. $url = $dbparams->get('source_url').$path.'/'.$result->user_avatar;
  61. } else {
  62. $url = '';
  63. }
  64. } else if ($result->user_avatar_type == 2) {
  65. // AVATAR REMOTE URL
  66. $url = $result->user_avatar;
  67. } else {
  68. $url = '';
  69. }
  70. return $url;
  71. }
  72. }
  73. return 0;
  74. }
  75. function getPrivateMessageCounts($puser_id)
  76. {
  77. if ($puser_id) {
  78. // read pm counts
  79. $db = JFusionFactory::getDatabase($this->getJname());
  80. // read unread count
  81. $db->setQuery('SELECT COUNT(msg_id)
  82. FROM #__privmsgs_to
  83. WHERE pm_unread = 1
  84. AND folder_id <> -2
  85. AND user_id = '.$puser_id);
  86. $unreadCount = $db->loadResult();
  87. // read total pm count
  88. $db->setQuery('SELECT COUNT(msg_id)
  89. FROM #__privmsgs_to
  90. WHERE folder_id NOT IN (-1, -2)
  91. AND user_id = '.$puser_id);
  92. $totalCount = $db->loadResult();
  93. return array('unread' => $unreadCount, 'total' => $totalCount);
  94. }
  95. return array('unread' => 0, 'total' => 0);
  96. }
  97. function getActivityQuery($usedforums, $result_order, $result_limit)
  98. {
  99. $where = (!empty($usedforums)) ? ' WHERE a.forum_id IN (' . $usedforums .')' : '';
  100. $end = $result_order." LIMIT 0,".$result_limit;
  101. $query = array(
  102. //LAT with first post info
  103. LAT.'0' => "SELECT a.topic_id AS threadid, a.topic_last_post_id AS postid, a.topic_first_poster_name AS username, a.topic_poster AS userid, CASE WHEN b.poster_id = 1 THEN 1 ELSE 0 END AS guest, a.topic_title AS subject, a.topic_time AS dateline, a.forum_id as forum_specific_id FROM `#__topics` as a INNER JOIN `#__posts` as b ON a.topic_first_post_id = b.post_id $where ORDER BY a.topic_last_post_time $end",
  104. //LAT with latest post info
  105. LAT.'1' => "SELECT a.topic_id AS threadid, a.topic_last_post_id AS postid, a.topic_last_poster_name AS username, a.topic_last_poster_id AS userid, CASE WHEN a.topic_last_poster_id = 1 THEN 1 ELSE 0 END AS guest, a.topic_title AS subject, a.topic_last_post_time AS dateline, a.forum_id as forum_specific_id FROM `#__topics` as a INNER JOIN `#__posts` as b ON a.topic_first_post_id = b.post_id $where ORDER BY a.topic_last_post_time $end",
  106. LCT => "SELECT a.topic_id AS threadid, a.topic_first_post_id AS postid, a.topic_first_poster_name AS username, a.topic_poster AS userid, CASE WHEN a.topic_poster = 1 THEN 1 ELSE 0 END AS guest, a.topic_title AS subject, b.post_text AS body, a.topic_time AS dateline, a.forum_id as forum_specific_id FROM `#__topics` as a INNER JOIN `#__posts` as b ON a.topic_first_post_id = b.post_id $where ORDER BY a.topic_time $end",
  107. LCP => "SELECT a.topic_id AS threadid, a.post_id AS postid, CASE WHEN a.poster_id = 1 AND a.post_username!='' THEN a.post_username ELSE b.username END AS username, a.poster_id AS userid, CASE WHEN a.poster_id = 1 THEN 1 ELSE 0 END AS guest, a.post_subject AS subject, a.post_text AS body, a.post_time AS dateline, a.forum_id as forum_specific_id FROM `#__posts` AS a INNER JOIN `#__users` AS b ON a.poster_id = b.user_id $where ORDER BY a.post_time $end"
  108. );
  109. return $query;
  110. }
  111. function getForumList()
  112. {
  113. //get the connection to the db
  114. $db = JFusionFactory::getDatabase($this->getJname());
  115. $query = 'SELECT forum_id as id, forum_name as name FROM #__forums
  116. WHERE forum_type = 1 ORDER BY left_id';
  117. $db->setQuery($query );
  118. //getting the results
  119. return $db->loadObjectList();
  120. }
  121. /************************************************
  122. * Functions For JFusion Discussion Bot Plugin
  123. ***********************************************/
  124. /**
  125. * Checks to see if a thread already exists for the content item and calls the appropriate function
  126. * @param object with discussion bot parameters
  127. * @param object $contentitem object containing content information
  128. * @return array Returns status of actions with errors if any
  129. */
  130. function checkThreadExists(&$dbparams, &$contentitem, &$existingthread, $forumid)
  131. {
  132. $status = array();
  133. $status['debug'] = array();
  134. $status['error'] = array();
  135. //set the timezone to UTC
  136. date_default_timezone_set('UTC');
  137. if(!empty($existingthread))
  138. {
  139. //datetime post was last updated
  140. $postModified = $existingthread->modified;
  141. //datetime content was last updated
  142. $contentModified = strtotime($contentitem->modified);
  143. //check to make sure the thread still exists in the software
  144. $jdb = & JFusionFactory::getDatabase($this->getJname());
  145. $query = "SELECT COUNT(*) FROM #__topics WHERE forum_id = {$existingthread->forumid} AND topic_id = {$existingthread->threadid} AND topic_first_post_id = {$existingthread->postid}";
  146. $jdb->setQuery($query);
  147. if($jdb->loadResult()==0)
  148. {
  149. //the thread no longer exists in the software!! recreate it
  150. $this->createThread($dbparams, $contentitem, $forumid, $status);
  151. if (empty($status['error'])) {
  152. $status['action'] = 'created';
  153. }
  154. return $status;
  155. }
  156. elseif($contentModified > $postModified)
  157. {
  158. //update the post if the content has been updated
  159. $this->updateThread($dbparams, $existingthread, $contentitem, $status);
  160. if (empty($status['error'])) {
  161. $status['action'] = 'updated';
  162. }
  163. return $status;
  164. }
  165. }
  166. else
  167. {
  168. //thread does not exist; create it
  169. $this->createThread($dbparams, $contentitem, $forumid, $status);
  170. if (empty($status['error'])) {
  171. $status['action'] = 'created';
  172. }
  173. return $status;
  174. }
  175. }
  176. /**
  177. * Retrieves thread information
  178. * @param int Id of specific thread
  179. * @return object Returns object with thread information
  180. * return the object with these three items
  181. * $result->forumid
  182. * $result->threadid (yes add it even though it is passed in as it will be needed in other functions)
  183. * $result->postid - this is the id of the first post in the thread
  184. */
  185. function getThread($threadid)
  186. {
  187. $db =& JFusionFactory::getDatabase($this->getJname());
  188. $query = "SELECT topic_id AS threadid, forum_id AS forumid, topic_first_post_id AS postid FROM #__topics WHERE topic_id = $threadid";
  189. $db->setQuery($query);
  190. $results = $db->loadObject();
  191. return $results;
  192. }
  193. /**
  194. * Creates new thread and posts first post
  195. * @param object with discussion bot parameters
  196. * @param object $contentitem object containing content information
  197. * @param int Id of forum to create thread
  198. * @param array $status contains errors and status of actions
  199. */
  200. function createThread(&$dbparams, &$contentitem, $forumid, &$status)
  201. {
  202. //setup some variables
  203. $userid = $dbparams->get("default_userid");
  204. $firstPost = $dbparams->get("first_post");
  205. $jdb =& JFusionFactory::getDatabase($this->getJname());
  206. $subject = trim(strip_tags($contentitem->title));
  207. //set what should be posted as the first post
  208. if($firstPost=="articleLink") {
  209. //create link
  210. $forumText = $dbparams->get("first_post_link_text");
  211. if($dbparams->get("first_post_link_type") == 'image') {
  212. $forumText = "<img src='$forumText'>";
  213. }
  214. $text = $this->prepareText(JFusionFunction::createJoomlaArticleURL($contentitem,$forumText));
  215. } elseif($firstPost=="articleIntro") {
  216. //prepare the text for posting
  217. $text = $this->prepareText($contentitem->introtext)."</br></br>";
  218. //create link
  219. $forumText = $dbparams->get("first_post_link_text");
  220. if($dbparams->get("first_post_link_type") == 'image') {
  221. $forumText = "<img src='$forumText'>";
  222. }
  223. $text .= $this->prepareText(JFusionFunction::createJoomlaArticleURL($contentitem,$forumText));
  224. } else {
  225. //prepare the text for posting
  226. $text = $this->prepareText($contentitem->introtext . $contentitem->fulltext);
  227. }
  228. //the user information
  229. $query = "SELECT username, username_clean, user_colour, user_permissions FROM #__users WHERE user_id = '$userid'";
  230. $jdb->setQuery($query);
  231. $phpbbUser = $jdb->loadObject();
  232. $timestamp = $dbparams->get('use_content_created_date',false) ? strtotime($contentitem->created) : time();
  233. $topic_row = new stdClass();
  234. $topic_row->topic_poster = $userid;
  235. $topic_row->topic_time = $timestamp;
  236. $topic_row->forum_id = $forumid;
  237. $topic_row->icon_id = false;
  238. $topic_row->topic_approved = 1;
  239. $topic_row->topic_title = $subject;
  240. $topic_row->topic_first_poster_name = $phpbbUser->username;
  241. $topic_row->topic_first_poster_colour = $phpbbUser->user_permissions;
  242. $topic_row->topic_type = 0;
  243. $topic_row->topic_time_limit = 0;
  244. $topic_row->topic_attachment = 0;
  245. if(!$jdb->insertObject('#__topics', $topic_row, 'topic_id' )){
  246. $status['error'] = $jdb->stderr();
  247. return;
  248. }
  249. $topicid = $jdb->insertid();
  250. require_once(JPATH_ADMINISTRATOR .DS.'components'.DS.'com_jfusion'.DS.'plugins'.DS.$this->getJname().DS.'bbcode_parser.php');
  251. $parser = new phpbb_bbcode_parser($text, $this->getJname());
  252. $post_row = new stdClass();
  253. $post_row->forum_id = $forumid;
  254. $post_row->topic_id = $topicid;
  255. $post_row->poster_id = $userid;
  256. $post_row->icon_id = 0;
  257. $post_row->poster_ip = $_SERVER["REMOTE_ADDR"];
  258. $post_row->post_time = $timestamp;
  259. $post_row->post_approved = 1;
  260. $post_row->enable_bbcode = 1;
  261. $post_row->enable_smilies = 1;
  262. $post_row->enable_magic_url = 1;
  263. $post_row->enable_sig = 1;
  264. $post_row->post_username = $phpbbUser->username;
  265. $post_row->post_subject = $subject;
  266. $post_row->post_text = $parser->text;
  267. $post_row->post_checksum = md5($parser->text);
  268. $post_row->post_attachment = 0;
  269. $post_row->bbcode_bitfield = $parser->bbcode_bitfield;
  270. $post_row->bbcode_uid = $parser->bbcode_uid;
  271. $post_row->post_postcount = 1;
  272. $post_row->post_edit_locked = 0;
  273. if(!$jdb->insertObject('#__posts', $post_row, 'post_id')) {
  274. $status['error'] = $jdb->stderr();
  275. return;
  276. }
  277. $postid = $jdb->insertid();
  278. $topic_row = new stdClass();
  279. $topic_row->topic_first_post_id = $postid;
  280. $topic_row->topic_last_post_id = $postid;
  281. $topic_row->topic_last_post_time = $timestamp;
  282. $topic_row->topic_last_poster_id = (int) $userid;
  283. $topic_row->topic_last_poster_name = $phpbbUser->username;
  284. $topic_row->topic_last_poster_colour = $phpbbUser->user_colour;
  285. $topic_row->topic_last_post_subject = (string) $subject;
  286. $topic_row->topic_id = $topicid;
  287. if(!$jdb->updateObject('#__topics', $topic_row, 'topic_id' )) {
  288. $status['error'] = $jdb->stderr();
  289. return;
  290. }
  291. $query = "SELECT forum_last_post_time, forum_topics, forum_topics_real, forum_posts FROM #__forums WHERE forum_id = $forumid";
  292. $jdb->setQuery($query);
  293. $num = $jdb->loadObject();
  294. $forum_stats = new stdClass();
  295. if($dbparams->get('use_content_created_date',false)) {
  296. //only update the last post for the topic if it really is newer
  297. $updateLastPost = ($timestamp > $num->forum_last_post_time) ? true : false;
  298. } else {
  299. $updateLastPost = true;
  300. }
  301. if($updateLastPost) {
  302. $forum_stats->forum_last_post_id = $postid;
  303. $forum_stats->forum_last_post_subject = $jdb->Quote($subject);
  304. $forum_stats->forum_last_post_time = $timestamp;
  305. $forum_stats->forum_last_poster_id = (int) $userid;
  306. $forum_stats->forum_last_poster_name = $phpbbUser->username;
  307. $forum_stats->forum_last_poster_colour = $phpbbUser->user_colour;
  308. }
  309. $forum_stats->forum_id = $forumid;
  310. $forum_stats->forum_topics = $num->forum_topics + 1;
  311. $forum_stats->forum_topics_real = $num->forum_topics_real + 1;
  312. $forum_stats->forum_posts = $num->forum_posts + 1;
  313. if(!$jdb->updateObject('#__forums', $forum_stats, 'forum_id' )) {
  314. $status['error'] = $jdb->stderr();
  315. return;
  316. }
  317. //update some stats
  318. $query = "UPDATE #__users SET user_posts = user_posts + 1 WHERE user_id = {$userid}";
  319. $jdb->setQuery($query);
  320. if(!$jdb->query()) {
  321. $status['error'] = $jdb->stderr();
  322. }
  323. $query = 'UPDATE #__config SET config_value = config_value + 1 WHERE config_name = \'num_topics\'';
  324. $jdb->setQuery($query);
  325. if(!$jdb->query()) {
  326. $status['error'] = $jdb->stderr();
  327. }
  328. if(!empty($topicid) && !empty($postid)) {
  329. //save the threadid to the lookup table
  330. JFusionFunction::updateForumLookup($contentitem->id, $forumid, $topicid, $postid, $this->getJname());
  331. }
  332. }
  333. /**
  334. * Returns HTML of a quick reply
  335. * @param $dbparams object with discussion bot parameters
  336. * @param boolean $showGuestInputs toggles whether to show guest inputs or not
  337. * @return string of html
  338. */
  339. function createQuickReply(&$dbparams,$showGuestInputs)
  340. {
  341. $html = '';
  342. if($showGuestInputs) {
  343. $html .= "<table><tr><td>".JText::_('USERNAME') .":</td><td><input name='guest_username' value='' class='inputbox'/></td></tr>";
  344. $question = $dbparams->get('captcha_question');
  345. if(!empty($question)) {
  346. $html .= "<tr><td>$question:</td><td><input name='captcha_answer' value='' class='inputbox'/></td></tr>";
  347. }
  348. $html .= "</table><br>";
  349. }
  350. $html .= "<textarea name='quickReply' class='inputbox'></textarea><br>";
  351. $html .= "<div style='width:100%; text-align:right;'><input type='submit' value='Submit'/></div>";
  352. return $html;
  353. }
  354. /**
  355. * Updates information in a specific thread/post
  356. * @param object with discussion bot parameters
  357. * @param object with existing thread info
  358. * @param object $contentitem object containing content information
  359. * @param array $status contains errors and status of actions
  360. */
  361. function updateThread(&$dbparams, &$existingthread, &$contentitem, &$status)
  362. {
  363. $threadid =& $existingthread->threadid;
  364. $forumid =& $existingthread->forumid;
  365. $postid =& $existingthread->postid;
  366. //setup some variables
  367. $firstPost = $dbparams->get("first_post");
  368. $jdb =& JFusionFactory::getDatabase($this->getJname());
  369. $subject = trim(strip_tags($contentitem->title));
  370. //set what should be posted as the first post
  371. if($firstPost=="articleLink") {
  372. //create link
  373. $forumText = $dbparams->get("first_post_link_text");
  374. if($dbparams->get("first_post_link_type") == 'image') {
  375. $forumText = "<img src='$forumText'>";
  376. }
  377. $text = $this->prepareText(JFusionFunction::createJoomlaArticleURL($contentitem,$forumText));
  378. } elseif($firstPost=="articleIntro") {
  379. //prepare the text for posting
  380. $text = $this->prepareText($contentitem->introtext)."</br></br>";
  381. //create link
  382. $forumText = $dbparams->get("first_post_link_text");
  383. if($dbparams->get("first_post_link_type") == 'image') {
  384. $forumText = "<img src='$forumText'>";
  385. }
  386. $text .= $this->prepareText(JFusionFunction::createJoomlaArticleURL($contentitem,$forumText));
  387. } else {
  388. //prepare the text for posting
  389. $text = $this->prepareText($contentitem->introtext . $contentitem->fulltext);
  390. }
  391. require_once(JPATH_ADMINISTRATOR .DS.'components'.DS.'com_jfusion'.DS.'plugins'.DS.$this->getJname().DS.'bbcode_parser.php');
  392. $parser = new phpbb_bbcode_parser($text, $this->getJname());
  393. $timestamp = time();
  394. $userid = $dbparams->get('default_user');
  395. $query = "SELECT post_edit_count FROM #__posts WHERE post_id = $postid";
  396. $jdb->setQuery($query);
  397. $count = $jdb->loadResult();
  398. $post_row = new stdClass();
  399. $post_row->post_subject = $subject;
  400. $post_row->post_text = $parser->text;
  401. $post_row->post_checksum = md5($parser->text);
  402. $post_row->bbcode_bitfield = $parser->bbcode_bitfield;
  403. $post_row->bbcode_uid = $parser->bbcode_uid;
  404. $post_row->post_edit_time = $timestamp;
  405. $post_row->post_edit_reason = 'JFusion Discussion Bot '. JText::_('UPDATE');
  406. $post_row->post_edit_user = $userid;
  407. $post_row->post_edit_count = $count + 1;
  408. $post_row->post_id = $postid;
  409. if(!$jdb->updateObject('#__posts', $post_row, 'post_id')) {
  410. $status['error'] = $jdb->stderr();
  411. } else {
  412. //update the lookup table
  413. JFusionFunction::updateForumLookup($contentitem->id, $forumid, $threadid, $postid, $this->getJname());
  414. }
  415. }
  416. /**
  417. * Creates a post from the quick reply
  418. * @param object with discussion bot parameters
  419. * @param $ids array with thread id ($ids["threadid"]) and first post id ($ids["postid"])
  420. * @param $contentitem object of content item
  421. * @param $userinfo object info of the forum user
  422. * @return array with status
  423. */
  424. function createPost(&$dbparams, &$ids, &$contentitem, &$userinfo)
  425. {
  426. $status = array();
  427. $status["error"] = false;
  428. if($userinfo->guest) {
  429. $captcha_answer = JRequest::getVar('captcha_answer', '', 'POST');
  430. if($captcha_answer != $dbparams->get('captcha_answer')) {
  431. $status["error"][] = JText::_('CAPTCHA_INCORRECT');
  432. return $status;
  433. } else {
  434. $userinfo->username = JRequest::getVar('guest_username', '', 'POST');
  435. $userinfo->userid = 1;
  436. //check to see if user exists to prevent user hijacking
  437. $JFusionUser = JFusionFactory::getUser($this->getJname());
  438. define('OVERRIDE_IDENTIFIER',3);
  439. $existinguser = $JFusionUser->getUser($userinfo);
  440. if(!empty($existinguser)) {
  441. $status["error"][] = JText::_('USERNAME_IN_USE');
  442. return $status;
  443. }
  444. }
  445. }
  446. //setup some variables
  447. $userid = $userinfo->userid;
  448. $jdb =& JFusionFactory::getDatabase($this->getJname());
  449. $text = JRequest::getVar('quickReply', false, 'POST');
  450. if(!empty($text)) {
  451. $text = $this->prepareText($text);
  452. require_once(JPATH_ADMINISTRATOR .DS.'components'.DS.'com_jfusion'.DS.'plugins'.DS.$this->getJname().DS.'bbcode_parser.php');
  453. $parser = new phpbb_bbcode_parser($text, $this->getJname());
  454. //get some topic information
  455. $query = "SELECT topic_title, topic_replies, topic_replies_real FROM #__topics WHERE topic_id = {$ids->threadid}";
  456. $jdb->setQuery($query);
  457. $topic = $jdb->loadObject();
  458. //the user information
  459. $query = "SELECT username, user_colour, user_permissions FROM #__users WHERE user_id = '$userid'";
  460. $jdb->setQuery($query);
  461. $phpbbUser = $jdb->loadObject();
  462. if($userinfo->guest && !empty($userinfo->username)) {
  463. $phpbbUser->username = $userinfo->username;
  464. }
  465. $timestamp = time();
  466. $post_row = new stdClass();
  467. $post_row->forum_id = $ids->forumid;
  468. $post_row->topic_id = $ids->threadid;
  469. $post_row->poster_id = $userid;
  470. $post_row->icon_id = 0;
  471. $post_row->poster_ip = $_SERVER["REMOTE_ADDR"];
  472. $post_row->post_time = $timestamp;
  473. $post_row->post_approved = 1;
  474. $post_row->enable_bbcode = 1;
  475. $post_row->enable_smilies = 1;
  476. $post_row->enable_magic_url = 1;
  477. $post_row->enable_sig = 1;
  478. $post_row->post_username = $phpbbUser->username;
  479. $post_row->post_subject = "Re: {$topic->topic_title}";
  480. $post_row->post_text = $parser->text;
  481. $post_row->post_checksum = md5($parser->text);
  482. $post_row->post_attachment = 0;
  483. $post_row->bbcode_bitfield = $parser->bbcode_bitfield;
  484. $post_row->bbcode_uid = $parser->bbcode_uid;
  485. $post_row->post_postcount = 1;
  486. $post_row->post_edit_locked = 0;
  487. if(!$jdb->insertObject('#__posts', $post_row, 'post_id')) {
  488. $status['error'] = $jdb->stderr();
  489. return $status;
  490. }
  491. $postid = $jdb->insertid();
  492. $topic_row = new stdClass();
  493. $topic_row->topic_last_post_id = $postid;
  494. $topic_row->topic_last_post_time = $timestamp;
  495. $topic_row->topic_last_poster_id = (int) $userid;
  496. $topic_row->topic_last_poster_name = $phpbbUser->username;
  497. $topic_row->topic_last_poster_colour = $phpbbUser->user_colour;
  498. $topic_row->topic_last_post_subject = '';
  499. $topic_row->topic_replies = $topic->topic_replies + 1;
  500. $topic_row->topic_replies_real = $topic->topic_replies_real + 1;
  501. $topic_row->topic_id = $ids->threadid;
  502. if(!$jdb->updateObject('#__topics', $topic_row, 'topic_id' )) {
  503. $status['error'] = $jdb->stderr();
  504. return $status;
  505. }
  506. $query = "SELECT forum_posts FROM #__forums WHERE forum_id = {$ids->forumid}";
  507. $jdb->setQuery($query);
  508. $num = $jdb->loadObject();
  509. $forum_stats = new stdClass();
  510. $forum_stats->forum_last_post_id = $postid;
  511. $forum_stats->forum_last_post_subject = '';
  512. $forum_stats->forum_last_post_time = $timestamp;
  513. $forum_stats->forum_last_poster_id = (int) $userid;
  514. $forum_stats->forum_last_poster_name = $phpbbUser->username;
  515. $forum_stats->forum_last_poster_colour = $phpbbUser->user_colour;
  516. $forum_stats->forum_posts = $num->forum_posts + 1;
  517. $forum_stats->forum_id = $ids->forumid;
  518. $query = "SELECT forum_topics, forum_topics_real, forum_posts FROM #__forums WHERE forum_id = {$ids->forumid}";
  519. $jdb->setQuery($query);
  520. $num = $jdb->loadObject();
  521. $forum_stats->forum_topics = $num->forum_topics + 1;
  522. $forum_stats->forum_topics_real = $num->forum_topics_real + 1;
  523. $forum_stats->forum_posts = $num->forum_posts + 1;
  524. if(!$jdb->updateObject('#__forums', $forum_stats, 'forum_id' )) {
  525. $status['error'] = $jdb->stderr();
  526. return $status;
  527. }
  528. //update some stats
  529. $query = "UPDATE #__users SET user_posts = user_posts + 1 WHERE user_id = {$userid}";
  530. $jdb->setQuery($query);
  531. if(!$jdb->query()) {
  532. $status['error'] = $jdb->stderr();
  533. }
  534. $query = 'UPDATE #__config SET config_value = config_value + 1 WHERE config_name = \'num_posts\'';
  535. $jdb->setQuery($query);
  536. if(!$jdb->query()) {
  537. $status['error'] = $jdb->stderr();
  538. }
  539. }
  540. return $status;
  541. }
  542. /**
  543. * Prepares text before saving to db or presentint to joomla article
  544. * @param string Text to be modified
  545. * @param $prepareForJoomla boolean to indicate if the text is to be saved to software's db or presented in joomla article
  546. * @return string Modified text
  547. */
  548. function prepareText($text, $prepareForJoomla = false)
  549. {
  550. if($prepareForJoomla===false) {
  551. //first thing is to remove all joomla plugins
  552. preg_match_all('/\{(.*)\}/U',$text,$matches);
  553. //find each thread by the id
  554. foreach($matches[1] AS $plugin) {
  555. //replace plugin with nothing
  556. $text = str_replace('{'.$plugin.'}',"",$text);
  557. }
  558. $text = JFusionFunction::parseCode($text,'bbcode');
  559. //now we have to parse the bbcode into phpBB's crazy format
  560. } else {
  561. //remove phpbb's bbcode uids
  562. $text = preg_replace("#\[(.*?):(.*?)]#si","[$1]",$text);
  563. //decode html entities
  564. $text = html_entity_decode($text);
  565. if(strpos($text,'SMILIES_PATH')!==false) {
  566. //must convert smilies
  567. $db =& JFusionFactory::getDatabase($this->getJname());
  568. $query = "SELECT config_value FROM #__config WHERE config_name = 'smilies_path'";
  569. $db->setQuery($query);
  570. $smilie_path = $db->loadResult();
  571. $params =& JFusionFactory::getParams($this->getJname());
  572. $source_url = $params->get('source_url');
  573. $text = preg_replace('#<!-- s(.*?) --><img src="\{SMILIES_PATH\}\/(.*?)" alt="(.*?)" title="(.*?)" \/>#si',"[img]{$source_url}{$smilie_path}/$2[/img]",$text);
  574. }
  575. //parse bbcode to html
  576. $text = JFusionFunction::parseCode($text,'html');
  577. }
  578. return $text;
  579. }
  580. /**
  581. * Returns an object of columns used in createPostTable()
  582. * Saves from having to repeat the same code over and over for each plugin
  583. * For example:
  584. * $columns->userid = "userid";
  585. * $columns->username = "username";
  586. * $columns->username_clean = "username_clean"; //if applicable for filtered usernames
  587. * $columns->dateline = "dateline";
  588. * $columns->posttext = "pagetext";
  589. * $columns->posttitle = "title";
  590. * $columns->postid = "postid";
  591. * $columns->threadid = "threadid";
  592. * @return object with column names
  593. */
  594. function getDiscussionColumns()
  595. {
  596. $columns = new stdClass();
  597. $columns->userid = "user_id";
  598. $columns->username = "username";
  599. $columns->username_clean = "username_clean";
  600. $columns->dateline = "post_time";
  601. $columns->posttext = "post_text";
  602. $columns->posttitle = "post_subject";
  603. $columns->postid = "post_id";
  604. $columns->threadid = "topic_id";
  605. $columns->guest = "guest";
  606. return $columns;
  607. }
  608. /**
  609. * Retrieves the posts to be displayed in the content item if enabled
  610. * @param object with discussion bot parameters
  611. * @param int Id of thread
  612. * @param int Id of first post which is useful if you do not want the first post to be included in results
  613. * @return array or object Returns retrieved posts
  614. */
  615. function getPosts(&$dbparams, &$existingthread)
  616. {
  617. $threadid =& $existingthread->threadid;
  618. $postid =& $existingthread->postid;
  619. //set the query
  620. $limit_posts = $dbparams->get("limit_posts");
  621. $limit = empty($limit_posts) || trim($limit_posts)==0 ? "" : "LIMIT 0,$limit_posts";
  622. $sort = $dbparams->get("sort_posts");
  623. $where = "WHERE p.topic_id = {$threadid} AND p.post_id != {$postid} AND p.post_approved = 1";
  624. $query = "SELECT p.post_id , CASE WHEN p.poster_id = 1 THEN 1 ELSE 0 END AS guest, CASE WHEN p.poster_id = 1 AND p.post_username!='' THEN p.post_username ELSE u.username END AS username, u.username_clean, u.user_id, p.post_subject, p.post_time, p.post_text, p.topic_id FROM `#__posts` as p INNER JOIN `#__users` as u ON p.poster_id = u.user_id $where ORDER BY p.post_time $sort $limit";
  625. $jdb = & JFusionFactory::getDatabase($this->getJname());
  626. $jdb->setQuery($query);
  627. $posts = $jdb->loadObjectList();
  628. return $posts;
  629. }
  630. function getReplyCount(&$existingthread)
  631. {
  632. $db =& JFusionFactory::getDatabase($this->getJname());
  633. $query = "SELECT topic_replies FROM #__topics WHERE topic_id = {$existingthread->threadid}";
  634. $db->setQuery($query);
  635. $result = $db->loadResult();
  636. return $result;
  637. }
  638. }