PageRenderTime 44ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/manchas/jrobotz
PHP | 784 lines | 568 code | 124 blank | 92 comment | 95 complexity | f5d8713364ccf8c5df3aa39ff4bb9ce5 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. * @package JFusion_vBulletin
  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 vBulletin
  12. * For detailed descriptions on these functions please check the model.abstractforum.php
  13. * @package JFusion_vBulletin
  14. */
  15. class JFusionForum_vbulletin extends JFusionForum
  16. {
  17. var $joomla_globals;
  18. function JFusionForum_vbulletin()
  19. {
  20. //get the params object
  21. $this->params = JFusionFactory::getParams($this->getJname());
  22. }
  23. function vBulletinInit()
  24. {
  25. //only initialize the vb framework if it has not already been done
  26. if(!defined('VB_AREA'))
  27. {
  28. //get the params object
  29. $this->params = JFusionFactory::getParams($this->getJname());
  30. //load the vbulletin framework
  31. define('VB_AREA','External');
  32. define('SKIP_SESSIONCREATE', 1);
  33. define('SKIP_USERINFO', 1);
  34. define('NOPMPOPUP',1);
  35. define('CWD', $this->params->get('source_path'));
  36. if(file_exists(CWD))
  37. {
  38. require_once(CWD.'/includes/init.php');
  39. //force into global scope
  40. $GLOBALS["vbulletin"] =& $vbulletin;
  41. $GLOBALS["db"] =& $vbulletin->db;
  42. return true;
  43. }
  44. else
  45. {
  46. JError::raiseWarning(500, JText::_('SOURCE_PATH_NOT_FOUND'));
  47. return false;
  48. }
  49. }
  50. else
  51. {
  52. return true;
  53. }
  54. }
  55. function getJname()
  56. {
  57. return 'vbulletin';
  58. }
  59. function checkThreadExists(&$dbparams, &$contentitem, &$existingthread, $forumid)
  60. {
  61. $status = array();
  62. $status['debug'] = array();
  63. $status['error'] = array();
  64. //set the timezone to UTC
  65. date_default_timezone_set('UTC');
  66. //backup Joomla's global scope
  67. $this->backupGlobals();
  68. if(!empty($existingthread))
  69. {
  70. //datetime post was last updated
  71. $postModified = $existingthread->modified;
  72. //datetime content was last updated
  73. $contentModified = strtotime($contentitem->modified);
  74. //check to make sure the thread still exists in the software
  75. $jdb = & JFusionFactory::getDatabase($this->getJname());
  76. $query = "SELECT COUNT(*) FROM #__thread WHERE forumid = {$existingthread->forumid} AND threadid = {$existingthread->threadid} AND firstpostid = {$existingthread->postid}";
  77. $jdb->setQuery($query);
  78. if($jdb->loadResult()==0)
  79. {
  80. //the thread no longer exists in the software!! recreate it
  81. $this->createThread($dbparams, $contentitem, $forumid, $status);
  82. if (empty($status['error'])) {
  83. $status['action'] = 'created';
  84. }
  85. //restore Joomla's global scope
  86. $this->restoreGlobals();
  87. return $status;
  88. }
  89. elseif($contentModified > $postModified)
  90. {
  91. //update the post if the content has been updated
  92. $this->updateThread($dbparams, $existingthread, $contentitem, $status);
  93. if (empty($status['error'])) {
  94. $status['action'] = 'updated';
  95. }
  96. //restore Joomla's global scope
  97. $this->restoreGlobals();
  98. return $status;
  99. }
  100. }
  101. else
  102. {
  103. //thread does not exist; create it
  104. $this->createThread($dbparams, $contentitem, $forumid, $status);
  105. if (empty($status['error'])) {
  106. $status['action'] = 'created';
  107. }
  108. //restore Joomla's global scope
  109. $this->restoreGlobals();
  110. return $status;
  111. }
  112. }
  113. function getThread($threadid)
  114. {
  115. $db =& JFusionFactory::getDatabase($this->getJname());
  116. $query = "SELECT threadid, forumid, firstpostid AS postid FROM #__thread WHERE threadid = $threadid";
  117. $db->setQuery($query);
  118. $results = $db->loadObject();
  119. return $results;
  120. }
  121. function createThread(&$dbparams, &$contentitem, $forumid, &$status)
  122. {
  123. //initialize vb framework
  124. if(!$this->vBulletinInit()) return null;
  125. //TODO create error notices if required params are empty
  126. $userid = $dbparams->get("default_userid");
  127. $firstPost = $dbparams->get("first_post");
  128. //strip title of all html characters
  129. $title = trim(strip_tags($contentitem->title));
  130. //set what should be posted as the first post
  131. if($firstPost=="articleLink") {
  132. //create link
  133. $forumText = $dbparams->get("first_post_link_text");
  134. if($dbparams->get("first_post_link_type") == 'image') {
  135. $forumText = "<img src='$forumText'>";
  136. }
  137. $text = $this->prepareText(JFusionFunction::createJoomlaArticleURL($contentitem,$forumText));
  138. } elseif($firstPost=="articleIntro") {
  139. //prepare the text for posting
  140. $text = $this->prepareText($contentitem->introtext)."</br></br>";
  141. //create link
  142. $forumText = $dbparams->get("first_post_link_text");
  143. if($dbparams->get("first_post_link_type") == 'image') {
  144. $forumText = "<img src='$forumText'>";
  145. }
  146. $text .= $this->prepareText(JFusionFunction::createJoomlaArticleURL($contentitem,$forumText));
  147. } else {
  148. //prepare the text for posting
  149. $text = $this->prepareText($contentitem->introtext . $contentitem->fulltext);
  150. }
  151. require_once (CWD . "/includes/functions.php");
  152. $timestamp = $dbparams->get('use_content_created_date',false) ? strtotime($contentitem->created) : time();
  153. $threaddm =& datamanager_init('Thread_FirstPost', $GLOBALS["vbulletin"], ERRTYPE_SILENT, 'threadpost');
  154. $foruminfo = fetch_foruminfo($forumid);
  155. $threaddm->set_info('forum', $foruminfo);
  156. $threaddm->set('forumid', $foruminfo['forumid']);
  157. $threaddm->set('userid', $userid);
  158. $threaddm->set('title', $title);
  159. $threaddm->set('pagetext',trim($text));
  160. $threaddm->set('allowsmilie', 1);
  161. $threaddm->set('ipaddress', $_SERVER["REMOTE_ADDR"]);
  162. $threaddm->set('visible', 1);
  163. $threaddm->set('dateline', $timestamp);
  164. $threaddm->pre_save();
  165. if(!empty($threaddm->errors)){
  166. $status["error"] = array_merge($status["error"], $threaddm->errors);
  167. } else {
  168. $threadid = $threaddm->save();
  169. $postid = $threaddm->fetch_field('firstpostid');
  170. //save the threadid to the lookup table
  171. JFusionFunction::updateForumLookup($contentitem->id, $forumid, $threadid, $postid, $this->getJname());
  172. }
  173. }
  174. function createQuickReply(&$dbparams,$showGuestInputs)
  175. {
  176. $html = '';
  177. if($showGuestInputs) {
  178. $html .= "<table><tr><td>".JText::_('USERNAME') .":</td><td><input name='guest_username' value='' class='inputbox'/></td></tr>";
  179. $question = $dbparams->get('captcha_question');
  180. if(!empty($question)) {
  181. $html .= "<tr><td>$question:</td><td><input name='captcha_answer' value='' class='inputbox'/></td></tr>";
  182. }
  183. $html .= "</table><br>";
  184. }
  185. $html .= "<textarea name='quickReply' class='inputbox'></textarea><br>";
  186. $html .= "<div style='width:100%; text-align:right;'><input type='submit' value='Submit'/></div>";
  187. return $html;
  188. }
  189. function createPost(&$dbparams, &$ids, &$contentitem, &$userinfo)
  190. {
  191. $status = array();
  192. $status["error"] = array();
  193. if($userinfo->guest) {
  194. $captcha_answer = JRequest::getVar('captcha_answer', '', 'POST');
  195. if($captcha_answer != $dbparams->get('captcha_answer')) {
  196. $status["error"][] = JText::_('CAPTCHA_INCORRECT');
  197. return $status;
  198. } else {
  199. $userinfo->username = JRequest::getVar('guest_username', '', 'POST');
  200. $userinfo->userid = 0;
  201. //check to see if user exists to prevent user hijacking
  202. $JFusionUser = JFusionFactory::getUser($this->getJname());
  203. define('OVERRIDE_IDENTIFIER',3);
  204. $existinguser = $JFusionUser->getUser($userinfo);
  205. if(!empty($existinguser)) {
  206. $status["error"][] = JText::_('USERNAME_IN_USE');
  207. return $status;
  208. }
  209. }
  210. }
  211. $guest = $userinfo->guest;
  212. $text = JRequest::getVar('quickReply', false, 'POST');
  213. if(!empty($text)) {
  214. //backup Joomla's global scope
  215. $this->backupGlobals();
  216. $text = $this->prepareText($text);
  217. //initialize the vb framework
  218. if(!$this->vBulletinInit()) return null;
  219. require_once (CWD . "/includes/functions.php");
  220. $threadinfo = verify_id('thread', $ids->threadid, 0, 1);
  221. $foruminfo = fetch_foruminfo($threadinfo['forumid'], false);
  222. $postinfo = array();
  223. $postinfo['threadid'] = $threadinfo['threadid'];
  224. $postinfo['ipaddress'] = $_SERVER["REMOTE_ADDR"];
  225. $postinfo['dateline'] = time();
  226. $postdm =& datamanager_init('Post', $GLOBALS["vbulletin"], ERRTYPE_SILENT, 'threadpost');
  227. $postdm->set_info('forum', $foruminfo);
  228. $postdm->set_info('thread', $threadinfo);
  229. $userinfo = $this->convertUserData($userinfo);
  230. $postdm->set_info('user',$userinfo);
  231. $postdm->set('userid', $userinfo['userid']);
  232. if($guest) {
  233. $postdm->set('username', $userinfo['username']);
  234. }
  235. $postdm->setr('parentid', $ids->postid);
  236. $postdm->setr('threadid', $ids->threadid);
  237. $postdm->setr('pagetext', $text);
  238. $postdm->set('title', "Re: {$this->prepareText($threadinfo['title'])}");
  239. $postdm->set('visible', 1);
  240. $postdm->set('allowsmilie', 1);
  241. $postdm->pre_save();
  242. if(!empty($postdm->errors)){
  243. $status["error"] = array_merge($status["error"], $postdm->errors);
  244. } else {
  245. $id = $postdm->save();
  246. }
  247. //restore Joomla's global scope
  248. $this->restoreGlobals();
  249. return $status;
  250. }
  251. }
  252. function updateThread( &$dbparams, &$existingthread, &$contentitem, &$status)
  253. {
  254. //initialize the vb framework
  255. if(!$this->vBulletinInit()) return null;
  256. $forumid =& $existingthread->forumid;
  257. $threadid =& $existingthread->threadid;
  258. $postid =& $existingthread->postid;
  259. $firstPost = $dbparams->get("first_post");
  260. //strip title of all html characters
  261. $title = trim(strip_tags($contentitem->title));
  262. //set what should be posted as the first post
  263. if($firstPost=="articleLink") {
  264. //create link
  265. $forumText = $dbparams->get("first_post_link_text");
  266. if($dbparams->get("first_post_link_type") == 'image') {
  267. $forumText = "<img src='$forumText'>";
  268. }
  269. $text = $this->prepareText(JFusionFunction::createJoomlaArticleURL($contentitem,$forumText));
  270. } elseif($firstPost=="articleIntro") {
  271. //prepare the text for posting
  272. $text = $this->prepareText($contentitem->introtext)."</br></br>";
  273. //create link
  274. $forumText = $dbparams->get("first_post_link_text");
  275. if($dbparams->get("first_post_link_type") == 'image') {
  276. $forumText = "<img src='$forumText'>";
  277. }
  278. $text .= $this->prepareText(JFusionFunction::createJoomlaArticleURL($contentitem,$forumText));
  279. } else {
  280. //prepare the text for posting
  281. $text = $this->prepareText($contentitem->introtext . $contentitem->fulltext);
  282. }
  283. require_once (CWD . "/includes/functions.php");
  284. $threadinfo = verify_id('thread', $threadid, 0, 1);
  285. $foruminfo = fetch_foruminfo($threadinfo['forumid'], false);
  286. $postinfo = array();
  287. $postinfo['postid'] = $postid;
  288. $postinfo['threadid'] =$threadinfo['threadid'];
  289. $postinfo['ipaddress'] = $_SERVER["REMOTE_ADDR"];
  290. $postinfo['dateline'] = time();
  291. $postdm =& datamanager_init('Post', $GLOBALS["vbulletin"], ERRTYPE_SILENT, 'threadpost');
  292. $postdm->set_existing($postinfo);
  293. $postdm->set_info('forum', $foruminfo);
  294. $postdm->set_info('thread', $threadinfo);
  295. $postdm->setr('pagetext', $text);
  296. $postdm->setr('title',$title);
  297. $postdm->pre_save();
  298. if(!empty($postdm->errors)){
  299. $status["error"] = array_merge($status["error"], $postdm->errors);
  300. } else {
  301. $postdm->save();
  302. //update the lookup table
  303. JFusionFunction::updateForumLookup($contentitem->id, $forumid, $threadid, $postid, $this->getJname());
  304. }
  305. }
  306. function prepareText($text,$prepareForJoomla=false)
  307. {
  308. if($prepareForJoomla===false) {
  309. //first thing is to remove all joomla plugins
  310. preg_match_all('/\{(.*)\}/U',$text,$matches);
  311. //find each thread by the id
  312. foreach($matches[1] AS $plugin) {
  313. //replace plugin with nothing
  314. $text = str_replace('{'.$plugin.'}',"",$text);
  315. }
  316. $text = html_entity_decode($text);
  317. $text = JFusionFunction::parseCode($text,'bbcode',true);
  318. } else {
  319. $text = JFusionFunction::parseCode($text,'html');
  320. }
  321. return $text;
  322. }
  323. function getPosts(&$dbparams, &$existingthread)
  324. {
  325. $threadid =& $existingthread->threadid;
  326. $postid =& $existingthread->postid;
  327. //set the query
  328. $limit_posts = $dbparams->get("limit_posts");
  329. $limit = empty($limit_posts) || trim($limit_posts)==0 ? "" : "LIMIT 0,$limit_posts";
  330. $sort = $dbparams->get("sort_posts");
  331. $where = "WHERE a.threadid = {$threadid} AND a.postid != {$postid} AND a.visible = 1";
  332. $query = "SELECT a.postid , a.username, a.userid, CASE WHEN a.userid = 0 THEN 1 ELSE 0 END AS guest, a.title, a.dateline, a.pagetext, a.threadid FROM `#__post` as a INNER JOIN `#__thread` as b ON a.threadid = b.threadid $where ORDER BY a.dateline $sort $limit";
  333. $jdb = & JFusionFactory::getDatabase($this->getJname());
  334. $jdb->setQuery($query);
  335. $posts = $jdb->loadObjectList();
  336. return $posts;
  337. }
  338. function getReplyCount(&$existingthread)
  339. {
  340. $db =& JFusionFactory::getDatabase($this->getJname());
  341. $query = "SELECT replycount FROM #__thread WHERE threadid = {$existingthread->threadid}";
  342. $db->setQuery($query);
  343. $result = $db->loadResult();
  344. return $result;
  345. }
  346. function getDiscussionColumns()
  347. {
  348. $columns = new stdClass();
  349. $columns->userid = "userid";
  350. $columns->username = "username";
  351. $columns->dateline = "dateline";
  352. $columns->posttext = "pagetext";
  353. $columns->posttitle = "title";
  354. $columns->postid = "postid";
  355. $columns->threadid = "threadid";
  356. $columns->guest = "guest";
  357. return $columns;
  358. }
  359. function getThreadURL($threadid)
  360. {
  361. return 'showthread.php?t=' . $threadid;
  362. }
  363. function getPostURL($threadid, $postid)
  364. {
  365. return 'showthread.php?p='.$postid.'#post' . $postid;
  366. }
  367. function getProfileURL($uid)
  368. {
  369. return 'member.php?u='.$uid;
  370. }
  371. function getPrivateMessageCounts($userid)
  372. {
  373. // initialise some objects
  374. $jdb = & JFusionFactory::getDatabase($this->getJname());
  375. $query = 'SELECT pmtotal,pmunread FROM #__user WHERE userid = '.$userid;
  376. $jdb->setQuery($query);
  377. $vbPMData = $jdb->loadObject();
  378. $pmcount['total'] = $vbPMData->pmtotal;
  379. $pmcount['unread'] = $vbPMData->pmunread;
  380. return $pmcount;
  381. }
  382. function getPrivateMessageURL()
  383. {
  384. return 'private.php';
  385. }
  386. function getViewNewMessagesURL()
  387. {
  388. return 'search.php?do=getnew';
  389. }
  390. function getAvatar($userid)
  391. {
  392. if ($userid) {
  393. $db =& JFusionFactory::getDatabase($this->getJname());
  394. $query = "SELECT varname, value FROM #__setting WHERE varname = 'usefileavatar' OR varname = 'avatarurl'";
  395. $db->setQuery($query);
  396. $settings = $db->loadObjectList();
  397. foreach($settings as $s) {
  398. ${$s->varname} = $s->value;
  399. }
  400. if($usefileavatar) {
  401. //avatars are saved to the filesystem
  402. $query = "SELECT avatarrevision FROM #__user WHERE userid = $userid";
  403. $db->setQuery($query);
  404. $avatarrevision = $db->loadResult();
  405. $url = $this->params->get('source_url').$avatarurl."/avatar{$userid}_{$avatarrevision}.gif";
  406. } else {
  407. //avatars are saved in the database
  408. $url = $this->params->get('source_url').'image.php?u='.$userid .'&amp;dateline='. time() ;
  409. }
  410. return $url;
  411. } else {
  412. return 0;
  413. }
  414. }
  415. function getActivityQuery($usedforums, $result_order, $result_limit)
  416. {
  417. $usedforums = $this->filterForumList($usedforums);
  418. //if no ther were no forums passed, the entire list is called and filtered in filterForumList
  419. //however if for some reason filterForumList fails, set forumid to 0 to prevent anything from showing protecting private forums
  420. $where = (!empty($usedforums)) ? 'WHERE a.forumid IN (' . implode(',',$usedforums) .') AND b.visible = 1' : 'WHERE a.forumid = 0 AND b.visible = 1';
  421. //going to give a large limit here as otherwise large boards will return a very large number of results
  422. //not using $result_limit here as the filtering process takes place after the results are retrieved
  423. $end = $result_order . " LIMIT 0,100";
  424. $query = array(
  425. //LAT with first post info
  426. LAT.'0' => "SELECT a.threadid, a.lastpostid AS postid, b.username, b.userid, CASE WHEN b.userid = 0 THEN 1 ELSE 0 END AS guest, a.title AS subject, b.dateline, a.forumid FROM `#__thread` as a INNER JOIN `#__post` as b ON a.firstpostid = b.postid $where ORDER BY a.lastpost $end",
  427. //LAT with lastest post info
  428. LAT.'1' => "SELECT a.threadid, a.lastpostid AS postid, b.username, b.userid, CASE WHEN b.userid = 0 THEN 1 ELSE 0 END AS guest, a.title AS subject, b.dateline, a.forumid FROM `#__thread` as a INNER JOIN `#__post` as b ON a.lastpostid = b.postid $where ORDER BY a.lastpost $end",
  429. LCT => "SELECT a.threadid, b.postid, b.username, b.userid, CASE WHEN b.userid = 0 THEN 1 ELSE 0 END AS guest, a.title AS subject, b.dateline, b.pagetext AS body, a.forumid FROM `#__thread` as a INNER JOIN `#__post` as b ON a.firstpostid = b.postid $where ORDER BY a.dateline $end",
  430. LCP => "SELECT b.threadid, b.postid, b.username, b.userid, CASE WHEN b.userid = 0 THEN 1 ELSE 0 END AS guest, CASE WHEN b.title = '' THEN CONCAT(\"Re: \",a.title) ELSE b.title END AS subject, b.dateline, b.pagetext AS body, a.forumid FROM `#__thread` as a INNER JOIN `#__post` AS b ON a.threadid = b.threadid $where ORDER BY b.dateline $end"
  431. );
  432. return $query;
  433. }
  434. function getForumList($objectList = true)
  435. {
  436. //get the connection to the db
  437. $db = JFusionFactory::getDatabase($this->getJname());
  438. $query = 'SELECT forumid as id, title_clean as name FROM #__forum ORDER BY forumid';
  439. $db->setQuery($query );
  440. $results = $db->loadObjectList();
  441. if(!$objectList) {
  442. $array = array();
  443. foreach($results as $r) {
  444. $array[] = $r->id;
  445. }
  446. $results = $array;
  447. }
  448. return $results;
  449. }
  450. function getForumPermissions($userid='find')
  451. {
  452. static $forumPerms, $groupPerms;
  453. if(empty($forumPerms)) {
  454. if($userid=='find') {
  455. //get the joomla user
  456. $JoomlaUser =& JFactory::getUser();
  457. //get the vb user
  458. if(!$JoomlaUser->guest) {
  459. $user = JFusionFunction::lookupUser($this->getJname(), $JoomlaUser->id);
  460. if(!empty($user)) {
  461. $userid = $user->userid;
  462. } else {
  463. //oops, something has failed
  464. $userid = 0;
  465. }
  466. } else {
  467. $userid = 0;
  468. }
  469. }
  470. //define some permissions
  471. defined('CAN_VIEW_THREAD_CONTENT') OR define('CAN_VIEW_THREAD_CONTENT',524288);
  472. defined('CAN_VIEW_FORUM') OR define('CAN_VIEW_FORUM',1);
  473. defined('CAN_VIEW_OTHERS_THREADS') OR define('CAN_VIEW_OTHERS_THREADS',2);
  474. defined('CAN_SEARCH_FORUM') OR define('CAN_SEARCH_FORUM',4);
  475. //get the usergroup permissions
  476. $db =& JFusionFactory::getDatabase($this->getJname());
  477. if($userid!=0) {
  478. $query = "SELECT u.usergroupid AS gid, u.membergroupids, g.forumpermissions AS perms FROM #__user AS u INNER JOIN #__usergroup AS g ON u.usergroupid = g.usergroupid WHERE u.userid = '$userid'";
  479. } else {
  480. $query = "SELECT usergroupid AS gid, forumpermissions AS perms FROM #__usergroup WHERE usergroupid = '1'";
  481. }
  482. $db->setQuery($query);
  483. $usergroup = $db->loadObject();
  484. $groupPerms = $usergroup->perms;
  485. //merge the permissions of member groups
  486. if(!empty($usergroup->membergroupids)) {
  487. $membergroups = explode(',',$usergroup->membergroupids);
  488. $query = "SELECT forumpermissions FROM #__usergroup WHERE usergroupid IN ({$usergroup->membergroupids})";
  489. $db->setQuery($query);
  490. $perms = $db->loadObjectList();
  491. foreach($perms as $p) {
  492. //use which ever grants the greatest number of permissions
  493. if($p->forumpermissions > $groupPerms) {
  494. $groupPerms = $p->forumpermissions;
  495. }
  496. }
  497. }
  498. //get custom forum permissions
  499. $query = "SELECT p.forumpermissions, p.forumid, p.usergroupid, f.parentlist, f.childlist FROM #__forumpermission AS p INNER JOIN #__forum AS f ON p.forumid = f.forumid WHERE p.usergroupid = {$usergroup->gid} ORDER BY p.forumid";
  500. $db->setQuery($query);
  501. $perms = $db->loadObjectList('forumid');
  502. $tempPerms = array();
  503. if(is_array($perms)) {
  504. foreach($perms as $p) {
  505. $tempPerms[$p->forumid]['perms'] = $p->forumpermissions;
  506. $tempPerms[$p->forumid]['childlist'] = explode(',',$p->childlist,-1);
  507. $tempPerms[$p->forumid]['parentlist'] = array_reverse(explode(',',$p->parentlist,-1));
  508. }
  509. }
  510. //get custom forum permissions for member groups
  511. if(!empty($membergroups)) {
  512. $query = "SELECT p.forumpermissions, p.forumid, p.usergroupid, f.parentlist, f.childlist FROM #__forumpermission AS p INNER JOIN #__forum AS f ON p.forumid = f.forumid WHERE p.usergroupid IN ({$usergroup->membergroupids}) ORDER BY p.forumid";
  513. $db->setQuery($query);
  514. $perms = $db->loadObjectList('forumid');
  515. foreach($perms as $p) {
  516. if(!isset($tempPerms[$p->forumid])) {
  517. $tempPerms[$p->forumid]['perms'] = 0;
  518. $tempPerms[$p->forumid]['childlist'] = explode(',',$p->childlist,-1);
  519. $tempPerms[$p->forumid]['parentlist'] = array_reverse(explode(',',$p->parentlist,-1));
  520. }
  521. //use which ever grants the greatest number of permissions
  522. if($p->forumpermissions > $tempPerms[$p->forumid]['perms']) {
  523. $tempPerms[$p->forumid]['perms'] = $p->forumpermissions;
  524. }
  525. }
  526. }
  527. $forumPerms = array();
  528. //we need to copy parent's permissions to the children if the child does not have custom permissions
  529. foreach($tempPerms as $id => $attributes) {
  530. if(!array_key_exists($id,$forumPerms)) {
  531. $forumPerms[$id] = $tempPerms[$id]['perms'];
  532. }
  533. $parent = '';
  534. //the permissions are set by the top parent with custom params
  535. foreach($attributes['parentlist'] as $p) {
  536. if(array_key_exists($p,$tempPerms)) {
  537. $parent = $p;
  538. break;
  539. }
  540. }
  541. if(!empty($parent)) {
  542. foreach($attributes['childlist'] AS $c) {
  543. if(!array_key_exists($c,$tempPerms) && array_key_exists($parent,$tempPerms)) {
  544. $forumPerms[$c] = $tempPerms[$parent]['perms'];
  545. }
  546. }
  547. }
  548. }
  549. }
  550. return array($groupPerms, $forumPerms);
  551. }
  552. function filterActivityResults(&$results, $limit=0, $idKey='forumid', $search = false)
  553. {
  554. //get the joomla user
  555. $JoomlaUser =& JFactory::getUser();
  556. //get the vb user
  557. if(!$JoomlaUser->guest) {
  558. $user = JFusionFunction::lookupUser($this->getJname(), $JoomlaUser->id);
  559. if(!empty($user)) {
  560. $userid = $user->userid;
  561. } else {
  562. //oops, something has failed
  563. $userid = 0;
  564. }
  565. } else {
  566. $userid = 0;
  567. }
  568. list($groupPerms, $forumPerms) = $this->getForumPermissions($userid);
  569. //use a counter to keep track of number of results
  570. $counter = 0;
  571. if(is_array($results)) {
  572. foreach($results as $k => $r) {
  573. $forumid = $r->$idKey;
  574. $counter++;
  575. //use the custom forum permissions by default; if they are empty then use the groups permission
  576. $perms = (isset($forumPerms[$forumid])) ? $forumPerms[$forumid] : $groupPerms;
  577. //check permissions
  578. if($search) {
  579. if(!($perms & CAN_SEARCH_FORUM) || !($perms & CAN_VIEW_FORUM) || !($perms & CAN_VIEW_THREAD_CONTENT) || ($r->userid != $userid && !($perms & CAN_VIEW_OTHERS_THREADS))) {
  580. unset($results[$k]);
  581. $counter--;
  582. }
  583. } else {
  584. if(!$perms & CAN_VIEW_FORUM || ($r->userid != $userid && !($perms & CAN_VIEW_OTHERS_THREADS))) {
  585. //user does not have permission to view the forum or another user's thread
  586. unset($results[$k]);
  587. $counter--;
  588. } elseif(!($perms & CAN_VIEW_THREAD_CONTENT)) {
  589. //user cannot view posts within the thread
  590. if(defined('ACTIVITY_MODE') && ACTIVITY_MODE == LCP) {
  591. //in activity module and using latest created post mode so remove the entire post
  592. unset($results[$k]);
  593. $counter--;
  594. } else {
  595. //in activity module and using the latest active topic or latest created topic mode so just empty the post body
  596. $r->body = '';
  597. }
  598. }
  599. }
  600. //if the limit has been met, remove the rest of the results
  601. if(!empty($limit) && $counter == $limit) {
  602. $results = array_slice($results,0,$limit);
  603. break;
  604. }
  605. }
  606. }
  607. }
  608. function filterForumList($forumids)
  609. {
  610. list($groupPerms, $forumPerms) = $this->getForumPermissions();
  611. if(empty($forumids)) {
  612. $forumids = $this->getForumList(false);
  613. } elseif(!is_array($forumids)) {
  614. $forumids = explode(',',$forumids);
  615. }
  616. if(!empty($forumids)) {
  617. if(is_array($forumids)) {
  618. foreach($forumids as $k => $id) {
  619. //use the custom forum permissions by default; if they are empty then use the groups permission
  620. $perms = (isset($forumPerms[$id])) ? $forumPerms[$id] : $groupPerms;
  621. if(!$perms & CAN_VIEW_FORUM) {
  622. //user does not have permission to view the forum
  623. unset($forumids[$k]);
  624. }
  625. }
  626. }
  627. }
  628. if(is_array($forumids)) {
  629. $forumids = array_values($forumids);
  630. }
  631. return $forumids;
  632. }
  633. //convert the existinguser variable into something vbulletin understands
  634. function convertUserData($existinguser)
  635. {
  636. $userinfo = array(
  637. 'userid' => $existinguser->userid,
  638. 'username' => $existinguser->username,
  639. 'email' => $existinguser->email,
  640. 'password' => $existinguser->password
  641. );
  642. return $userinfo;
  643. }
  644. //backs up joomla's global scope
  645. function backupGlobals()
  646. {
  647. $this->joomla_globals = $GLOBALS;
  648. }
  649. //restore joomla's global scope
  650. function restoreGlobals()
  651. {
  652. if(is_array($this->joomla_globals)) {
  653. $GLOBALS = $this->joomla_globals;
  654. $this->joomla_globals = "";
  655. }
  656. //make sure Joomla's db object is still connected
  657. JFusionFunction::reconnectJoomlaDb();
  658. }
  659. }
  660. ?>