PageRenderTime 69ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_jfusionplugins/vbulletin/helper.php

http://jfusion.googlecode.com/
PHP | 286 lines | 185 code | 23 blank | 78 comment | 21 complexity | 3710fed7b7da6fd007cc7743fe76db40 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. // no direct access
  15. defined('_JEXEC') or die('Restricted access');
  16. /**
  17. * JFusion Helper Class for vBulletin
  18. *
  19. * @category JFusion
  20. * @package JFusionPlugins
  21. * @subpackage vBulletin
  22. * @author JFusion Team <webmaster@jfusion.org>
  23. * @copyright 2008 JFusion. All rights reserved.
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  25. * @link http://www.jfusion.org
  26. */
  27. class JFusionHelper_vbulletin
  28. {
  29. var $vb_data;
  30. var $backup;
  31. /**
  32. *
  33. * @return unknown_type
  34. */
  35. function JFusionHelper_vbulletin()
  36. {
  37. $this->params = & JFusionFactory::getParams($this->getJname());
  38. }
  39. /**
  40. * Returns the name for this plugin
  41. *
  42. * @return string
  43. */
  44. function getJname()
  45. {
  46. return 'vbulletin';
  47. }
  48. /**
  49. * Initializes the vBulletin framework
  50. *
  51. * @return boolean true on successful initialization
  52. */
  53. function vBulletinInit()
  54. {
  55. //only initialize the vb framework if it has not already been done
  56. if (!defined('VB_AREA')) {
  57. //load the vbulletin framework
  58. define('VB_AREA', 'JFusion');
  59. define('VB_ENTRY', 'JFusion');
  60. define('THIS_SCRIPT', 'JFusion');
  61. define('SKIP_SESSIONCREATE', 1);
  62. define('DIE_QUIETLY', 1);
  63. define('SKIP_USERINFO', 1);
  64. define('NOPMPOPUP', 1);
  65. define('CWD', $this->params->get('source_path'));
  66. $phrasegroups = array('postbit');
  67. $specialtemplates = array();
  68. $globaltemplates = array(
  69. 'bbcode_code_printable',
  70. 'bbcode_html_printable',
  71. 'bbcode_php_printable',
  72. 'bbcode_quote_printable',
  73. 'postbit_attachment',
  74. 'postbit_attachmentimage',
  75. 'postbit_attachmentthumbnail',
  76. 'postbit_external',
  77. );
  78. $actiontemplates = array();
  79. global $vbulletin;
  80. if (file_exists(CWD)) {
  81. require_once CWD . '/includes/init.php';
  82. $this->vb_data = $vbulletin;
  83. //force into global scope
  84. $GLOBALS["vbulletin"] = $vbulletin;
  85. $vbulletin->db->query_first("USE `" . $this->params->get('database_name') . "`");
  86. $GLOBALS["db"] = $vbulletin->db;
  87. } else {
  88. JError::raiseWarning(500, JText::_('SOURCE_PATH_NOT_FOUND'));
  89. return false;
  90. }
  91. } elseif (defined('VB_AREA') && VB_AREA == 'JFusion') {
  92. $this->vb_data->db->query_first("USE `" . $this->params->get('database_name') . "`");
  93. if (empty($GLOBALS['vbulletin'])) {
  94. $GLOBALS['vbulletin'] = $this->vb_data;
  95. }
  96. if (empty($GLOBALS['db'])) {
  97. $GLOBALS['db'] = $this->vb_data->db;
  98. }
  99. } elseif (defined('VB_AREA')) {
  100. //vb is calling up JFusion so load the $vbulletin global
  101. global $vbulletin;
  102. if (!empty($vbulletin)) {
  103. $this->vb_data = $vbulletin;
  104. } else {
  105. //these is for sure going to lead to something bad so let's die now
  106. die('vB JFusion Integration Fatal Error - Please contact the site administrator!');
  107. }
  108. } else {
  109. return false;
  110. }
  111. return true;
  112. }
  113. /**
  114. * Convert the existinguser variable into something vbulletin understands
  115. *
  116. * @param $existinguser object with existing vb userinfo
  117. *
  118. * @return array
  119. */
  120. function convertUserData($existinguser)
  121. {
  122. $userinfo = array('userid' => $existinguser->userid, 'username' => $existinguser->username, 'email' => $existinguser->email, 'password' => $existinguser->password);
  123. return $userinfo;
  124. }
  125. /**
  126. * Backs up Joomla's various Joomla variables before calling vBulletin's data managers
  127. */
  128. function backupJoomla()
  129. {
  130. $this->backup['globals'] = $GLOBALS;
  131. //let's take special precautions for Itemid
  132. $this->backup['itemid'] = JRequest::getInt('Itemid', 0);
  133. }
  134. /**
  135. * Restores Joomla's various Joomla variables after calling vBulletin's data managers
  136. */
  137. function restoreJoomla()
  138. {
  139. //restore Joomla's autoload function
  140. spl_autoload_register('__autoload');
  141. if (isset($this->backup['globals'])) {
  142. $GLOBALS = $this->backup['globals'];
  143. }
  144. if (isset($this->backup['itemid'])) {
  145. JRequest::setVar('Itemid', $this->backup['itemid']);
  146. global $Itemid;
  147. $Itemid = $this->backup['itemid'];
  148. }
  149. $this->backup = array();
  150. //make sure Joomla's db object is still connected
  151. JFusionFunction::reconnectJoomlaDb();
  152. }
  153. /**
  154. * Obtains the version of the integrated vbulletin
  155. *
  156. * @return string Version number
  157. */
  158. function getVersion()
  159. {
  160. static $jfusion_vb_version;
  161. if(empty($jfusion_vb_version)) {
  162. $db =& JFusionFactory::getDatabase($this->getJname());
  163. $q = "SELECT value FROM #__setting WHERE varname = 'templateversion'";
  164. $db->setQuery($q);
  165. $jfusion_vb_version = $db->loadResult();
  166. }
  167. return $jfusion_vb_version;
  168. }
  169. /*
  170. * Creates a basic vB SEF url for vB 4
  171. *
  172. * @param (array) $uri Array with uri pieces
  173. * @param (string) $type Type of url eg. forum, thread, member
  174. *
  175. * @return string $url Appropriate URL
  176. */
  177. function getVbURL($url, $type = false)
  178. {
  179. $params = & JFusionFactory::getParams($this->getJname());
  180. $allow_sef = $params->get("allow_sef", 1);
  181. $vbversion = & $this->getVersion();
  182. if (!empty($allow_sef) && (int) substr($vbversion, 0, 1) > 3) {
  183. $db = & JFusionFactory::getDatabase($this->getJname());
  184. if (!defined('JFVB_FRIENDLYURL')) {
  185. $query = "SELECT value FROM #__setting WHERE varname = 'friendlyurl'";
  186. $db->setQuery($query);
  187. $sefmode = $db->loadResult();
  188. define('JFVB_FRIENDLYURL', (int) $sefmode);
  189. }
  190. $uri = new JURI($url);
  191. switch ($type) {
  192. case 'members':
  193. $query = 'SELECT username FROM #__user WHERE userid = ' . $uri->getVar('u');
  194. $db->setQuery($query);
  195. $username = $db->loadResult();
  196. $this->cleanForVbURL($username);
  197. $vburi = $uri->getVar('u') . '-' . $username;
  198. break;
  199. case 'threads':
  200. $query = 'SELECT title FROM #__thread WHERE threadid = ' . $uri->getVar('t');
  201. $db->setQuery($query);
  202. $title = $db->loadResult();
  203. $this->cleanForVbURL($title);
  204. $vburi = $uri->getVar('t') . '-' . $title;
  205. break;
  206. case 'post':
  207. $pid = $uri->getVar('p');
  208. $tid = $uri->getVar('t');
  209. if (empty($tid)) {
  210. $query = 'SELECT threadid FROM #__post WHERE postid = ' . $pid;
  211. $db->setQuery($query);
  212. $tid = $db->loadResult();
  213. $query = 'SELECT title FROM #__thread WHERE threadid = ' . $tid;
  214. $db->setQuery($query);
  215. $title = $db->loadResult();
  216. $this->cleanForVbURL($title);
  217. }
  218. $vburi = $tid . '-' . $title;
  219. $uri->setVar('viewfull', 1);
  220. $type = 'threads';
  221. break;
  222. case 'forums':
  223. default:
  224. //bad type so return url unaltered
  225. return $url;
  226. break;
  227. }
  228. $query = $uri->getQuery();
  229. $fragment = $uri->getFragment();
  230. if ($fragment) {
  231. $fragment = '#' . $fragment;
  232. }
  233. switch (JFVB_FRIENDLYURL) {
  234. case 1:
  235. $sef_url = $uri->getPath() . '?' . $vburi . ($query ? '&' . $query : '') . $fragment;
  236. break;
  237. case 2:
  238. $sef_url = $uri->getPath() . '/' . $vburi . ($query ? '?' . $query : '') . $fragment;
  239. break;
  240. case 3:
  241. $sef_url = $type . '/' . $vburi . ($query ? '?' . $query : '') . $fragment;
  242. break;
  243. case 0:
  244. default:
  245. $sef_url = $url;
  246. break;
  247. }
  248. return $sef_url;
  249. } else {
  250. return $url;
  251. }
  252. }
  253. /**
  254. * Prepares text for a vB SEF URL
  255. *
  256. * @param &$string text to be cleaned
  257. */
  258. function cleanForVbURL(&$string)
  259. {
  260. $string = preg_replace('*([\s$+,/:=\?@"\'<>%{}|\\^~[\]`\r\n\t\x00-\x1f\x7f]|(?(?<!&)#|#(?![0-9]+;))|&(?!#[0-9]+;)|(?<!&#\d|&#\d{2}|&#\d{3}|&#\d{4}|&#\d{5});)*s', '-', strip_tags($string));
  261. $string = trim(preg_replace('#-+#', '-', $string), '-');
  262. }
  263. }