PageRenderTime 62ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/components/com_jfusionplugins/vbulletin/hooks.php

http://jfusion.googlecode.com/
PHP | 509 lines | 408 code | 9 blank | 92 comment | 120 complexity | ec55881ac54d9b4fa3e5c3911196181b MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. *
  4. * PHP version 5
  5. *
  6. * @category JFusion
  7. * @package JFusionPlugins
  8. * @subpackage vBulletin
  9. * @author JFusion Team <webmaster@jfusion.org>
  10. * @copyright 2008 JFusion. All rights reserved.
  11. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  12. * @link http://www.jfusion.org
  13. */
  14. //force required variables into global scope
  15. if (!isset($GLOBALS['vbulletin']) && !empty($vbulletin)) {
  16. $GLOBALS["vbulletin"] = & $vbulletin;
  17. }
  18. if (!isset($GLOBALS['db']) && !empty($db)) {
  19. $GLOBALS["db"] = & $db;
  20. }
  21. /**
  22. * Vbulletin hook class
  23. *
  24. * @category JFusion
  25. * @package JFusionPlugins
  26. * @subpackage vBulletin
  27. * @author JFusion Team <webmaster@jfusion.org>
  28. * @copyright 2008 JFusion. All rights reserved.
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  30. * @link http://www.jfusion.org
  31. */
  32. class executeJFusionHook
  33. {
  34. var $vars;
  35. function executeJFusionHook($hook, &$vars)
  36. {
  37. if ($hook != 'init_startup' && !defined('_VBJNAME') && empty($_POST['logintype'])) {
  38. die("JFusion plugins need to be updated. Reinstall desired plugins in JFusion's config for vBulletin.");
  39. }
  40. if (!defined('_JFVB_PLUGIN_VERIFIED') && $hook != 'init_startup' && defined('_VBJNAME') && defined('_JEXEC') && empty($_POST['logintype'])) {
  41. define('_JFVB_PLUGIN_VERIFIED', 1);
  42. if (!JFusionFunction::validPlugin(_VBJNAME)) {
  43. die("JFusion plugin is invalid. Reinstall desired plugins in JFusion's config for vBulletin.");
  44. }
  45. }
  46. //execute the hook
  47. $this->vars = & $vars;
  48. eval('$success = $this->' . $hook . '();');
  49. //if ($success) die('<pre>'.print_r($GLOBALS["vbulletin"]->pluginlist,true)."</pre>");
  50. }
  51. function init_startup()
  52. {
  53. global $vbulletin;
  54. if ($this->vars == "redirect" && !isset($_GET['noredirect']) && !defined('_JEXEC') && !isset($_GET['jfusion'])) {
  55. //only redirect if in the main forum
  56. if (!empty($_SERVER['PHP_SELF'])) {
  57. $s = $_SERVER['PHP_SELF'];
  58. } elseif (!empty($_SERVER['SCRIPT_NAME'])) {
  59. $s = $_SERVER['SCRIPT_NAME'];
  60. } else {
  61. //the current URL cannot be determined so abort redirect
  62. return;
  63. }
  64. $ignore = array($vbulletin->config['Misc']['admincpdir'], 'ajax.php', 'archive', 'attachment.php', 'cron.php', 'image.php', 'inlinemod', 'login.php', 'misc.php', 'mobiquo', $vbulletin->config['Misc']['modcpdir'], 'newattachment.php', 'picture.php', 'printthread.php', 'sendmessage.php');
  65. if (defined('REDIRECT_IGNORE')) {
  66. $custom_files = explode(',', REDIRECT_IGNORE);
  67. if (is_array($custom_files)) {
  68. foreach ($custom_files as $file) {
  69. if (!empty($file)) {
  70. $ignore[] = trim($file);
  71. }
  72. }
  73. }
  74. }
  75. $redirect = true;
  76. foreach ($ignore as $i) {
  77. if (strpos($s, $i) !== false) {
  78. //for sendmessage.php, only redirect if not sending an IM
  79. if ($i == 'sendmessage.php') {
  80. $do = $_GET['do'];
  81. if ($do != 'im') {
  82. continue;
  83. }
  84. }
  85. $redirect = false;
  86. break;
  87. }
  88. }
  89. if ($redirect) {
  90. $filename = basename($s);
  91. $query = $_SERVER["QUERY_STRING"];
  92. if (SEFENABLED) {
  93. if (SEFMODE == 1) {
  94. $url = JOOMLABASEURL . "$filename/";
  95. if (!empty($query)) {
  96. $q = explode('&', $query);
  97. foreach ($q as $k => $v) {
  98. $url.= "$k,$v/";
  99. }
  100. }
  101. if (!empty($query)) {
  102. $queries = explode('&', $query);
  103. foreach ($queries as $q) {
  104. $part = explode('=', $q);
  105. $url.= "$part[0],$part[1]/";
  106. }
  107. }
  108. } else {
  109. $url = JOOMLABASEURL . $filename;
  110. $url.= (empty($query)) ? '' : "?$query";
  111. }
  112. } else {
  113. $url = JOOMLABASEURL . "&jfile={$filename}";
  114. $url.= (empty($query)) ? '' : "&{$query}";
  115. }
  116. header("Location: $url");
  117. exit;
  118. }
  119. }
  120. //add our custom hooks into vbulletin's hook cache
  121. if (!empty($vbulletin->pluginlist) AND is_array($vbulletin->pluginlist)) {
  122. $hooks = $this->getHooks($this->vars);
  123. if (is_array($hooks)) {
  124. foreach ($hooks as $name => $code) {
  125. if (isset($vbulletin->pluginlist[$name])) {
  126. $vbulletin->pluginlist[$name].= "\n$code";
  127. } else {
  128. $vbulletin->pluginlist[$name] = $code;
  129. }
  130. }
  131. }
  132. }
  133. return true;
  134. }
  135. function getHooks($plugin)
  136. {
  137. global $hookFile;
  138. //we need to set up the hooks
  139. if ($plugin == "frameless") {
  140. //retrieve the hooks that jFusion will use to make vB work framelessly
  141. $hookNames = array("album_picture_complete", "global_start", "global_complete", "global_setup_complete", "header_redirect", "logout_process", "member_profileblock_fetch_unwrapped", "redirect_generic", "xml_print_output");
  142. } elseif ($plugin == "duallogin") {
  143. //retrieve the hooks that vBulletin will use to login to Joomla
  144. $hookNames = array("global_setup_complete", "login_verify_success", "logout_process");
  145. define('DUALLOGIN', 1);
  146. } else {
  147. $hookNames = array();
  148. }
  149. $hooks = array();
  150. foreach ($hookNames as $h) {
  151. //certain hooks we want to call directly such as global variables
  152. if ($h == "profile_editoptions_start") {
  153. $hooks[$h] = 'global $stylecount;';
  154. } else {
  155. if ($h == "album_picture_complete") $toPass = '$vars =& $pictureinfo; ';
  156. elseif ($h == "global_complete") $toPass = '$vars =& $output; ';
  157. elseif ($h == "header_redirect") $toPass = '$vars =& $url;';
  158. elseif ($h == "member_profileblock_fetch_unwrapped") $toPass = '$vars =& $prepared;';
  159. elseif ($h == "redirect_generic") $toPass = '$vars = array(); $vars["url"] =& $url; $vars["js_url"] =& $js_url; $vars["formfile"] =& $formfile;';
  160. elseif ($h == "xml_print_output") $toPass = '$vars = & $this->doc;';
  161. else $toPass = '$vars = null;';
  162. $hooks[$h] = 'include_once(\'' . $hookFile . '\'); ' . $toPass . ' $jFusionHook = new executeJFusionHook(\'' . $h . '\',$vars);';
  163. }
  164. }
  165. return $hooks;
  166. }
  167. /**
  168. * HOOK FUNCTIONS
  169. */
  170. function album_picture_complete()
  171. {
  172. global $vbulletin;
  173. $start = strpos($this->vars['pictureurl'], '/picture.php');
  174. $tempURL = $vbulletin->options['bburl'] . substr($this->vars['pictureurl'], $start);
  175. $this->vars['pictureurl'] = $tempURL;
  176. return true;
  177. }
  178. function global_complete()
  179. {
  180. global $vbulletin;
  181. //create cookies to allow direct login into vb frameless
  182. /*
  183. if ($vbulletin->userinfo['userid'] != 0 && empty($vbulletin->GPC[COOKIE_PREFIX . 'userid'])) {
  184. if ($vbulletin->GPC['cookieuser']) {
  185. $expire = 60 * 60 * 24 * 365;
  186. } else {
  187. $expire = 0;
  188. }
  189. JFusionCurl::addCookie(COOKIE_PREFIX . 'userid', $vbulletin->userinfo['userid'], $expire, $vbulletin->options['cookiepath'], $vbulletin->options['cookiedomain'], false, true);
  190. JFusionCurl::addCookie(COOKIE_PREFIX . 'password', md5($vbulletin->userinfo['password'] . COOKIE_SALT), $expire, $vbulletin->options['cookiepath'], $vbulletin->options['cookiedomain'], false, true);
  191. }
  192. */
  193. //we need to update the session table
  194. $vdb = & JFusionFactory::getDatabase(_VBJNAME);
  195. if (!empty($vdb)) {
  196. $vars = & $vbulletin->session->vars;
  197. if ($vbulletin->session->created) {
  198. $bypass = ($vars[bypass]) ? 1 : 0;
  199. $query = "INSERT IGNORE INTO #__session
  200. (sessionhash, userid, host, idhash, lastactivity, location, styleid, languageid, loggedin, inforum, inthread, incalendar, badlocation, useragent, bypass, profileupdate) VALUES
  201. ({$vdb->Quote($vars[dbsessionhash]) },$vars[userid],{$vdb->Quote($vars[host]) },{$vdb->Quote($vars[idhash]) },$vars[lastactivity],{$vdb->Quote($vars[location]) },$vars[styleid],$vars[languageid],
  202. $vars[loggedin],$vars[inforum],$vars[inthread],$vars[incalendar],$vars[badlocation],{$vdb->Quote($vars[useragent]) },$bypass,$vars[profileupdate])";
  203. } else {
  204. $query = "UPDATE #__session SET lastactivity = $vars[lastactivity], inforum = $vars[inforum], inthread = $vars[inthread], incalendar = $vars[incalendar], badlocation = $vars[badlocation]
  205. WHERE sessionhash = {$vdb->Quote($vars[dbsessionhash]) }";
  206. }
  207. $vdb->setQuery($query);
  208. $vdb->query();
  209. }
  210. //we need to perform the shutdown queries that mark PMs read, etc
  211. if (is_array($vbulletin->db->shutdownqueries)) {
  212. foreach ($vbulletin->db->shutdownqueries AS $name => $query) {
  213. if (!empty($query) AND ($name !== 'pmpopup' OR !defined('NOPMPOPUP'))) {
  214. $vdb->setQuery($query);
  215. $vdb->query();
  216. }
  217. }
  218. }
  219. //echo the output and return an exception to allow Joomla to continue
  220. echo trim($this->vars, "\n\r\t.");
  221. Throw new Exception("vBulletin exited.");
  222. }
  223. function global_setup_complete()
  224. {
  225. if (defined('_JEXEC')) {
  226. //If Joomla SEF is enabled, the dash in the logout hash gets converted to a colon which must be corrected
  227. global $vbulletin, $show, $vbsefenabled, $vbsefmode;
  228. $vbulletin->GPC['logouthash'] = str_replace(':', '-', $vbulletin->GPC['logouthash']);
  229. //if sef is enabled, we need to rewrite the nojs link
  230. if ($vbsefenabled == 1) {
  231. if ($vbsefmode == 1) {
  232. $uri = & JURI::getInstance();
  233. $url = $uri->toString();
  234. $show['nojs_link'] = $url;
  235. $show['nojs_link'].= (substr($url, -1) != '/') ? '/nojs,1/' : 'nojs,1/';
  236. } else {
  237. $jfile = (JRequest::getVar('jfile', false)) ? JRequest::getVar('jfile') : 'index.php';
  238. $show['nojs_link'] = "$jfile" . "?nojs=1";
  239. }
  240. }
  241. }
  242. return true;
  243. }
  244. function global_start()
  245. {
  246. //lets rewrite the img urls now while we can
  247. global $stylevar, $vbulletin;
  248. //check for trailing slash
  249. $DS = (substr($vbulletin->options['bburl'], -1) == '/') ? "" : "/";
  250. if(!empty($stylevar)) {
  251. foreach ($stylevar as $k => $v) {
  252. if (strstr($k, 'imgdir') && strstr($v, $vbulletin->options['bburl']) === false && strpos($v, 'http') === false) {
  253. $stylevar[$k] = $vbulletin->options['bburl'] . $DS . $v;
  254. }
  255. }
  256. }
  257. return true;
  258. }
  259. function header_redirect()
  260. {
  261. global $vbsefenabled, $vbsefmode, $baseURL, $integratedURL, $foruminfo, $vbulletin;
  262. //reworks the URL for header redirects ie header('Location: $url');
  263. //if this is a forum link, return without parsing the URL
  264. if (!empty($foruminfo['link']) && (THIS_SCRIPT != 'subscription' || $_REQUEST['do'] != 'removesubscription')) {
  265. return;
  266. }
  267. if (defined('_JFUSION_DEBUG')) {
  268. $debug = array();
  269. $debug['url'] = $this->vars;
  270. $debug['function'] = 'header_redirect';
  271. }
  272. $admincp = & $vbulletin->config['Misc']['admincpdir'];
  273. $modcp = & $vbulletin->config['Misc']['modcp'];
  274. //create direct URL for admincp, modcp, and archive
  275. if (strpos($this->vars, $admincp) !== false || strpos($this->vars, $modcp) !== false || strpos($this->vars, 'archive') !== false) {
  276. if (defined('_JFUSION_DEBUG')) {
  277. $debug['parsed'] = $this->vars;
  278. $_SESSION["jfvbdebug"][] = $debug;
  279. }
  280. if (!empty($vbsefenabled)) {
  281. if ($vbsefmode == 1) {
  282. if (strpos($this->vars, $admincp) !== false) {
  283. $pos = $admincp;
  284. } elseif (strpos($this->vars, $modcp) !== false) {
  285. $pos = $modcp;
  286. } elseif (strpos($this->vars, 'archive') !== false) {
  287. $pos = 'archive';
  288. }
  289. $this->vars = $integratedURL . substr($this->vars, strpos($this->vars, $pos));
  290. } else {
  291. $this->vars = str_replace($baseURL, $integratedURL, $this->vars);
  292. }
  293. } else {
  294. $this->vars = str_replace(JFusionFunction::getJoomlaURL(), $integratedURL, $this->vars);
  295. }
  296. //convert &amp; to & so the redirect is correct
  297. $this->vars = str_replace('&amp;', '&', $this->vars);
  298. return true;
  299. }
  300. //let's make sure the baseURL does not have a / at the end for comparison
  301. $testURL = (substr($baseURL, -1) == '/') ? substr($baseURL, 0, -1) : $baseURL;
  302. if (strpos($this->vars["url"], $testURL) === false) {
  303. $url = basename($this->vars);
  304. if ($vbsefenabled != 1) {
  305. //non sef URls
  306. $url = str_replace("?", "&", $url);
  307. $url = $baseURL . '&jfile=' . $url;
  308. } else {
  309. if ($vbsefmode == 1) {
  310. $url = JFusionFunction::routeURL($url, JRequest::getInt('Itemid'));
  311. } else {
  312. //we can just append both variables
  313. $url = $baseURL . $url;
  314. }
  315. }
  316. $this->vars = $url;
  317. }
  318. if (defined('_JFUSION_DEBUG')) {
  319. $debug['parsed'] = $this->vars;
  320. $_SESSION["jfvbdebug"][] = $debug;
  321. }
  322. //convert &amp; to & so the redirect is correct
  323. $this->vars = str_replace('&amp;', '&', $this->vars);
  324. return true;
  325. }
  326. function login_verify_success()
  327. {
  328. global $vbulletin;
  329. //if JS is enabled, only a hashed form of the password is available
  330. $password = (!empty($vbulletin->GPC['vb_login_password'])) ? $vbulletin->GPC['vb_login_password'] : $vbulletin->GPC['vb_login_md5password'];
  331. if (!empty($password)) {
  332. if (!defined('_JEXEC')) {
  333. $mainframe = $this->startJoomla();
  334. } else {
  335. $mainframe =& JFactory::getApplication('site');
  336. define('_VBULLETIN_JFUSION_HOOK', true);
  337. }
  338. // do the login
  339. global $JFusionActivePlugin;
  340. $JFusionActivePlugin = JFusionFactory::getPluginNodeId(_VBJNAME);
  341. $baseURL = (class_exists('JFusionFunction')) ? JFusionFunction::getJoomlaURL() : JURI::root();
  342. $loginURL = JRoute::_($baseURL . 'index.php?option=com_user&task=login', false);
  343. $credentials = array('username' => $vbulletin->userinfo['username'], 'password' => $password, 'password_salt' => $vbulletin->userinfo['salt']);
  344. $options = array('entry_url' => $loginURL);
  345. //set remember me option
  346. if(!empty($vbulletin->GPC['cookieuser'])) {
  347. $options['remember'] = 1;
  348. }
  349. //creating my own vb security string for check in the function
  350. define('_VB_SECURITY_CHECK', md5('jfusion' . md5($password . $vbulletin->userinfo['salt'])));
  351. $mainframe->login($credentials, $options);
  352. // clean up the joomla session object before continuing
  353. $session = & JFactory::getSession();
  354. $session->close();
  355. }
  356. return true;
  357. }
  358. function logout_process()
  359. {
  360. if (defined('_JEXEC')) {
  361. //we are in frameless mode and need to kill the cookies to prevent getting stuck logged in
  362. global $vbulletin;
  363. JFusionCurl::addCookie(COOKIE_PREFIX . 'userid', 0, 0, $vbulletin->options['cookiepath'], $vbulletin->options['cookiedomain'], false, true);
  364. JFusionCurl::addCookie(COOKIE_PREFIX . 'password', 0, 0, $vbulletin->options['cookiepath'], $vbulletin->options['cookiedomain'], false, true);
  365. //prevent global_complete from recreating the cookies
  366. $vbulletin->userinfo['userid'] = 0;
  367. $vbulletin->userinfo['password'] = 0;
  368. }
  369. if (defined('DUALLOGIN')) {
  370. if (!defined('_JEXEC')) {
  371. $mainframe = $this->startJoomla();
  372. } else {
  373. $mainframe =& JFactory::getApplication('site');
  374. define('_VBULLETIN_JFUSION_HOOK', true);
  375. }
  376. global $JFusionActivePlugin;
  377. $JFusionActivePlugin = JFusionFactory::getPluginNodeId(_VBJNAME);
  378. // logout any joomla users
  379. $mainframe->logout();
  380. // clean up session
  381. $session = & JFactory::getSession();
  382. $session->close();
  383. }
  384. return true;
  385. }
  386. function member_profileblock_fetch_unwrapped()
  387. {
  388. global $vbsefmode, $vbsefenabled, $baseURL;
  389. static $profileurlSet;
  390. if (!empty($this->vars[profileurl]) && $profileurlSet !== true) {
  391. $uid = JRequest::getVar('u');
  392. if ($vbsefenabled && $vbsefmode) {
  393. $this->vars[profileurl] = str_replace("member.php?u=$uid", '', $this->vars[profileurl]);
  394. } else {
  395. $this->vars[profileurl] = $baseURL . "&jfile=member.php&u=$uid";
  396. }
  397. $profileurlSet = true;
  398. }
  399. }
  400. function redirect_generic()
  401. {
  402. global $vbsefenabled, $vbsefmode, $baseURL;
  403. //reworks the URL for generic redirects that use JS or html meta header
  404. if (defined('_JFUSION_DEBUG')) {
  405. $debug = array();
  406. $debug['url'] = $this->vars["url"];
  407. $debug['function'] = 'redirect_generic';
  408. }
  409. //let's make sure the baseURL does not have a / at the end for comparison
  410. $testURL = (substr($baseURL, -1) == '/') ? substr($baseURL, 0, -1) : $baseURL;
  411. if (strpos($this->vars["url"], $testURL) === false) {
  412. $url = basename($this->vars["url"]);
  413. if ($vbsefenabled != 1) {
  414. //non sef URls
  415. $url = str_replace("?", "&", $url);
  416. $url = $baseURL . '&jfile=' . $url;
  417. } else {
  418. if ($vbsefmode == 1) {
  419. $url = JFusionFunction::routeURL($url, JRequest::getInt('Itemid'));
  420. } else {
  421. //we can just append both variables
  422. $url = $baseURL . $url;
  423. }
  424. }
  425. //convert &amp; to & so the redirect is correct
  426. $url = str_replace('&amp;', '&', $url);
  427. $this->vars["url"] = $url;
  428. $this->vars["js_url"] = addslashes_js($this->vars["url"]);
  429. $this->vars["formfile"] = $this->vars["url"];
  430. }
  431. if (defined('_JFUSION_DEBUG')) {
  432. $debug['parsed'] = $this->vars['url'];
  433. $_SESSION["jfvbdebug"][] = $debug;
  434. }
  435. return true;
  436. }
  437. function xml_print_output()
  438. {
  439. if (!defined('_JEXEC')) {
  440. $mainframe = $this->startJoomla();
  441. }
  442. //parse AJAX output
  443. $public = & JFusionFactory::getPublic(_VBJNAME);
  444. $params = & JFusionFactory::getParams(_VBJNAME);
  445. $jdata = new stdClass();
  446. $jdata->body = & $this->vars;
  447. $jdata->Itemid = $params->get("plugin_itemid");
  448. //Get the base URL to the specific JFusion plugin
  449. $jdata->baseURL = JFusionFunction::getPluginURL($jdata->Itemid);
  450. //Get the integrated URL
  451. $jdata->integratedURL = $params->get('source_url');
  452. $public->parseBody($jdata);
  453. }
  454. /**
  455. * This login portion of this script was originally created for phpBB and customized for vBulletin
  456. * Original Copyright:
  457. * Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
  458. * @version $Id: auth_db.php,v 1.24 2007/10/05 12:42:06 acydburn Exp $
  459. * @copyright (c) 2005 phpBB Group
  460. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  461. */
  462. function startJoomla()
  463. {
  464. define('_VBULLETIN_JFUSION_HOOK', true);
  465. define('_JEXEC', true);
  466. define('DS', DIRECTORY_SEPARATOR);
  467. // load joomla libraries
  468. require_once JPATH_BASE . DS . 'includes' . DS . 'defines.php';
  469. require_once JPATH_LIBRARIES . DS . 'loader.php';
  470. jimport('joomla.base.object');
  471. jimport('joomla.factory');
  472. jimport('joomla.filter.filterinput');
  473. jimport('joomla.error.error');
  474. jimport('joomla.event.dispatcher');
  475. jimport('joomla.event.plugin');
  476. jimport('joomla.plugin.helper');
  477. jimport('joomla.utilities.arrayhelper');
  478. jimport('joomla.environment.uri');
  479. jimport('joomla.environment.request');
  480. jimport('joomla.user.user');
  481. jimport('joomla.html.parameter');
  482. // JText cannot be loaded with jimport since it's not in a file called text.php but in methods
  483. JLoader::register('JText', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
  484. JLoader::register('JRoute', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
  485. //include the JFusion factory and function file
  486. $functionFile = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_jfusion' . DS . 'models' . DS . 'model.jfusion.php';
  487. $factoryFile = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_jfusion' . DS . 'models' . DS . 'model.factory.php';
  488. $curlFile = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_jfusion' . DS . 'models' . DS . 'model.curl.php';
  489. if (file_exists($functionFile) && file_exists($factoryFile) && file_exists($curlFile)) {
  490. require_once $factoryFile;
  491. require_once $functionFile;
  492. require_once $curlFile;
  493. }
  494. $mainframe = & JFactory::getApplication('site');
  495. $GLOBALS['mainframe'] = & $mainframe;
  496. return $mainframe;
  497. }
  498. }
  499. ?>