PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/site/tmp/install_4a925da139185/admin/models/model.jfusion.php

https://bitbucket.org/manchas/jrobotz
PHP | 980 lines | 675 code | 127 blank | 178 comment | 151 complexity | cdd3b6dc92b2bb23639637515d8b440e MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. * @package JFusion
  4. * @subpackage Models
  5. * @author JFusion development team
  6. * @copyright Copyright (C) 2008 JFusion. All rights reserved.
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  8. */
  9. // no direct access
  10. defined('_JEXEC' ) or die('Restricted access' );
  11. /**
  12. * Class for general JFusion functions
  13. * @package JFusion
  14. */
  15. class JFusionFunction{
  16. /**
  17. * Returns the JFusion plugin name of the software that is currently the master of user management
  18. * @param string $jname Name of master JFusion plugin
  19. */
  20. function getMaster()
  21. {
  22. static $jfusion_master;
  23. if (!isset($jfusion_master )) {
  24. $db = & JFactory::getDBO();
  25. $query = 'SELECT * from #__jfusion WHERE master = 1 and status = 1';
  26. $db->setQuery($query );
  27. $jfusion_master = $db->loadObject();
  28. if ($jfusion_master) {
  29. return $jfusion_master;
  30. }
  31. } else {
  32. return $jfusion_master;
  33. }
  34. }
  35. /**
  36. * Returns the JFusion plugin name of the software that are currently the slaves of user management
  37. * @param string $jname Name of master JFusion plugin
  38. */
  39. function getSlaves()
  40. {
  41. static $jfusion_slaves;
  42. if (!isset($jfusion_slaves )) {
  43. $db = & JFactory::getDBO();
  44. $query = 'SELECT * from #__jfusion WHERE slave = 1 and status = 1';
  45. $db->setQuery($query );
  46. $jfusion_slaves = $db->loadObjectList();
  47. }
  48. return $jfusion_slaves;
  49. }
  50. /**
  51. * By default, returns the JFusion plugin of the software that is currently the slave of user management, minus the joomla_int plugin.
  52. * If activity, search, or discussion is passed in, returns the plugins with that feature enabled
  53. * @param array $jname Array list of slave JFusion plugin names
  54. */
  55. function getPlugins($criteria = 'slave')
  56. {
  57. switch ($criteria) {
  58. case 'slave':
  59. static $slave_plugins;
  60. case 'activity':
  61. static $activity_plugins;
  62. break;
  63. case 'search':
  64. static $search_plugins;
  65. break;
  66. case 'discussion':
  67. static $discussion_plugins;
  68. break;
  69. default:
  70. return;
  71. }
  72. $plugins = $criteria."_plugins";
  73. if(empty($$plugins)) {
  74. $query = "SELECT * FROM #__jfusion WHERE ($criteria = 1 AND status = 1 AND name NOT LIKE 'joomla_int')";
  75. $db = & JFactory::getDBO();
  76. $db->setQuery($query);
  77. $$plugins = $db->loadObjectList ();
  78. }
  79. return $$plugins;
  80. }
  81. /**
  82. * Saves the posted JFusion component variables
  83. * @param string $jname name of the JFusion plugin used
  84. * @param array $post Array of JFusion plugin parameters posted to the JFusion component
  85. * @return true|false returns true if succesful and false if an error occurred
  86. */
  87. function saveParameters($jname, $post)
  88. {
  89. //serialize the $post to allow storage in a SQL field
  90. $serialized = base64_encode(serialize($post));
  91. //set the current parameters in the jfusion table
  92. $db = & JFactory::getDBO();
  93. $query = 'UPDATE #__jfusion SET params = ' . $db->Quote($serialized) .' WHERE name = ' . $db->Quote($jname);
  94. $db->setQuery($query );
  95. if (!$db->query()) {
  96. //there was an error saving the parameters
  97. JError::raiseWarning(0,$db->stderr());
  98. $result = false;
  99. return $result;
  100. }
  101. //reset the params instance for this plugin
  102. JFusionFactory::getParams($jname,true);
  103. $result = true;
  104. return $result;
  105. }
  106. /**
  107. * Checks to see if the JFusion plugins are installed and enabled
  108. */
  109. function checkPlugin()
  110. {
  111. $userPlugin = true;
  112. $authPlugin = true;
  113. if (!JFusionFunction::isPluginInstalled('jfusion','authentication', false)) {
  114. JError::raiseWarning(0,JText::_('FUSION_MISSING_AUTH'));
  115. $authPlugin = false;
  116. }
  117. if (!JFusionFunction::isPluginInstalled('jfusion','user', false)) {
  118. JError::raiseWarning(0,JText::_('FUSION_MISSING_USER'));
  119. $userPlugin = false;
  120. }
  121. if ($authPlugin && $userPlugin) {
  122. $jAuth = JFusionFunction::isPluginInstalled('jfusion','user',true);
  123. $jUser = JFusionFunction::isPluginInstalled('jfusion','authentication',true);
  124. if (!$jAuth) {
  125. JError::raiseNotice(0,JText::_('FUSION_READY_TO_USE_AUTH'));
  126. }
  127. if (!$jUser) {
  128. JError::raiseNotice(0,JText::_('FUSION_READY_TO_USE_USER'));
  129. }
  130. }
  131. }
  132. /**
  133. * Checks to see if the configuration of a Jfusion plugin is valid and stores the result in the JFusion table
  134. * @param string $jname name of the JFusion plugin used
  135. */
  136. /**
  137. * Tests if a plugin is installed with the specified name, where folder is the type (e.g. user)
  138. * @param string $element element name of the plugin
  139. * @param string $folder folder name of the plugin
  140. * @param integer $testPublished Variable to determine if the function should test to see if the plugin is published
  141. * @return true|false returns true if succesful and false if an error occurred
  142. */
  143. function isPluginInstalled($element,$folder, $testPublished)
  144. {
  145. $db =& JFactory::getDBO();
  146. $query = 'SELECT published FROM #__plugins WHERE element=' . $db->Quote($element) . ' AND folder=' . $db->Quote($folder);
  147. $db->setQuery($query);
  148. $result = $db->loadObject();
  149. if ($result) {
  150. if ($testPublished) {
  151. return($result->published == 1);
  152. } else {
  153. $result = true;
  154. return $result;
  155. }
  156. } else {
  157. $result = false;
  158. return $result;
  159. }
  160. }
  161. /**
  162. * Creates a JFusion Joomla compatible URL
  163. * @param $url string url to be parsed
  164. * @param $itemid string itemid of the JFusion menu item or the name of the plugin for direct link
  165. * @param $route boolean optional switch to send url through JRoute::_() (true by default)
  166. * @param $xhtml boolean optional switch to turn & into &amp; if $route is true (true by default)
  167. * @return Parsed URL
  168. */
  169. function routeURL($url, $itemid, $route = true, $xhtml = true)
  170. {
  171. if(!is_numeric($itemid)){
  172. //we need to create direct link to the plugin
  173. $params = JFusionFactory::getParams($itemid);
  174. $url = $params->get('source_url') . $url;
  175. return $url;
  176. } else {
  177. //we need to create link to a joomla itemid
  178. $u =& JURI::getInstance( $url );
  179. $u->setVar('jfile', $u->getPath());
  180. $u->setVar('option', 'com_jfusion');
  181. $u->setVar('Itemid', $itemid);
  182. $query = $u->getQuery(false);
  183. $fragment = $u->getFragment();
  184. if( isset( $fragment ) ) $query .= '#'.$fragment;
  185. if ($route) {
  186. $url = JRoute::_('index.php?'.$query, $xhtml);
  187. } else {
  188. $url = 'index.php?'.$query;
  189. }
  190. return $url;
  191. }
  192. }
  193. /**
  194. * Returns either the Joomla wrapper URL or the full URL directly to the forum
  195. * @param string $url relative path to a webpage of the integrated software
  196. * @param string $jname name of the JFusion plugin used
  197. * @return string full URL to the filename passed to this function
  198. */
  199. function createURL($url, $jname, $view, $itemid='')
  200. {
  201. if(!empty($itemid)){
  202. //use the itemid only to identify plugin name and view type
  203. $base_url = 'index.php?option=com_jfusion&amp;Itemid=' . $itemid;
  204. } else {
  205. $base_url = 'index.php?option=com_jfusion&amp;Itemid=-1&amp;view=' . $view . '&amp;jname=' . $jname;
  206. }
  207. if ($view == 'direct') {
  208. $params = JFusionFactory::getParams($jname);
  209. $url = $params->get('source_url') . $url;
  210. return $url;
  211. } elseif ($view == 'wrapper') {
  212. //use base64_encode to encode the URL for passing. But, base64_code uses / which throws off SEF urls. Thus slashes
  213. //must be translated into something base64_encode will not generate and something that will not get changed by Joomla or Apache.
  214. $url = $base_url . '&amp;wrap='. str_replace("/","_slash_",base64_encode($url));
  215. $url = JRoute::_($url);
  216. return $url;
  217. } elseif ($view == 'frameless'){
  218. //split the filename from the query
  219. $parts = explode('?', $url);
  220. if (isset($parts[1])) {
  221. $base_url .= '&amp;jfile=' . $parts[0] . '&amp;' . $parts[1];
  222. } else {
  223. $base_url .= '&amp;jfile=' . $parts[0];
  224. }
  225. $url = JRoute::_($base_url);
  226. return $url;
  227. }
  228. }
  229. /**
  230. * Updates the JFusion user lookup table during login
  231. * @param object $userinfo object containing the userdata
  232. * @param string $joomla_id The Joomla ID of the user
  233. * @param string $jname name of the JFusion plugin used
  234. * @param boolean $delete deletes an entry from the table
  235. */
  236. function updateLookup($userinfo, $joomla_id, $jname = '', $delete = false)
  237. {
  238. $db =& JFactory::getDBO();
  239. //we don't need to update the lookup for internal joomla unless deleting a user
  240. if($jname=='joomla_int'){
  241. if($delete) {
  242. //Delete old user data in the lookup table
  243. $query = 'DELETE FROM #__jfusion_users WHERE id =' . $joomla_id . ' OR username = ' . $db->Quote($userinfo->username) . ' OR LOWER(username) = ' . strtolower($db->Quote($userinfo->email));
  244. $db->setQuery($query);
  245. if (!$db->query()) {
  246. JError::raiseWarning(0,$db->stderr());
  247. }
  248. //Delete old user data in the lookup table
  249. $query = 'DELETE FROM #__jfusion_users_plugin WHERE id =' . $joomla_id . ' OR username = ' . $db->Quote($userinfo->username) . ' OR LOWER(username) = ' . strtolower($db->Quote($userinfo->email));
  250. $db->setQuery($query);
  251. if (!$db->query()) {
  252. JError::raiseWarning(0,$db->stderr());
  253. }
  254. }
  255. return;
  256. }
  257. //check to see if we have been given a joomla id
  258. if(empty($joomla_id)) {
  259. $query = "SELECT id FROM #__users WHERE username = ".$db->Quote($userinfo->username);
  260. $db->setQuery($query);
  261. $joomla_id = $db->loadResult();
  262. if(empty($joomla_id)) {
  263. return;
  264. }
  265. }
  266. if(empty($jname)) {
  267. $queries = array();
  268. //we need to update each master/slave
  269. $query = "SELECT name FROM #__jfusion WHERE master = 1 OR slave = 1";
  270. $db->setQuery($query);
  271. $jnames = $db->loadObjectList();
  272. foreach($jnames as $jname) {
  273. if($jname!="joomla_int") {
  274. $user =& JFusionFactory::getUser($jname->name);
  275. $puserinfo = $user->getUser($userinfo);
  276. if($delete) {
  277. $queries[] = "(id = $joomla_id AND jname = ".$db->Quote($jname->name).")";
  278. } else {
  279. $queries[] = "(".$db->Quote($puserinfo->userid).",".$db->Quote($puserinfo->username).", $joomla_id, ".$db->Quote($jname->name).")";
  280. }
  281. unset($user);
  282. unset($puserinfo);
  283. }
  284. }
  285. if(!empty($queries)){
  286. if($delete) {
  287. $query = "DELETE FROM #__jfusion_users_plugin WHERE ".implode(' OR ',$queries);
  288. } else {
  289. $query = "REPLACE INTO #__jfusion_users_plugin (userid,username,id,jname) VALUES (". implode(',',$queries) . ")";
  290. }
  291. $db->setQuery($query);
  292. if(!$db->query()) {
  293. JError::raiseWarning(0,$db->stderr());
  294. }
  295. }
  296. } else {
  297. if($delete) {
  298. $query = "DELETE FROM #__jfusion_users_plugin WHERE id = $joomla_id AND jname = '$jname'";
  299. } else {
  300. $query = "REPLACE INTO #__jfusion_users_plugin (userid,username,id,jname) VALUES ({$db->Quote($userinfo->userid)},{$db->Quote($userinfo->username)},$joomla_id,{$db->Quote($jname)})";
  301. }
  302. $db->setQuery($query);
  303. if(!$db->query()) {
  304. JError::raiseWarning(0,$db->stderr());
  305. }
  306. }
  307. }
  308. /**
  309. * Returns the userinfo data for JFusion plugin based on the userid
  310. * @param string $jname name of the JFusion plugin used
  311. * @param string $userid The ID of the user
  312. * @param boolean $isJoomlaId if true, returns the userinfo data based on Joomla, otherwise the plugin
  313. * @param string $username If the userid is that of the plugin's, we need the username to find the user in case there is no record in the lookup table
  314. * @return object database Returns the userinfo as a Joomla database object
  315. **/
  316. function lookupUser($jname, $userid, $isJoomlaId = true, $username = '')
  317. {
  318. $column = ($isJoomlaId) ? 'a.id' : 'a.userid';
  319. $db =& JFactory::getDBO();
  320. $query = 'SELECT a.*, b.email FROM #__jfusion_users_plugin AS a INNER JOIN #__users AS b ON a.id = b.id WHERE '.$column.' = ' . $db->Quote($userid) . ' AND a.jname = ' . $db->Quote($jname);
  321. $db->setQuery($query);
  322. $result = $db->loadObject();
  323. //for some reason this user is not in the database so let's find them
  324. if(empty($result)) {
  325. if($isJoomlaId) {
  326. //we have a joomla id so let's setup a temp $userinfo
  327. $query = "SELECT username, email FROM #__users WHERE id = $userid";
  328. $db->setQuery($query);
  329. $userinfo = $db->loadResult();
  330. $joomla_id = $userid;
  331. } else {
  332. //we have a plugin's id so we need to find Joomla's id then setup a temp $userinfo
  333. //first try JFusion's user table
  334. $query = "SELECT a.id, a.email FROM #__users AS a INNER JOIN #__jfusion_users AS b WHERE b.username = ".$db->Quote($username);
  335. $db->setQuery($query);
  336. $userinfo = $db->loadObject();
  337. //not created by JFusion so let's check the Joomla table directly
  338. if(empty($userinfo)) {
  339. $query = "SELECT id, email FROM #__users WHERE username = ".$db->Quote($username);
  340. $db->setQuery($query);
  341. $userinfo = $db->loadObject();
  342. if(!empty($userinfo)) {
  343. //we have a user
  344. $userinfo->username = $username;
  345. $joomla_id = $userinfo->id;
  346. }
  347. }
  348. }
  349. if(!empty($userinfo) && !empty($joomla_id) && !empty($jname)) {
  350. //get the plugin's userinfo - specifically we need the userid which it will provide
  351. $user =& JFusionFactory::getUser($jname);
  352. $existinguser = $user->getUser($userinfo);
  353. if(!empty($existinguser)) {
  354. //update the lookup table with the new acquired info
  355. JFusionFunction::updateLookup($existinguser, $joomla_id, $jname);
  356. //return the results
  357. $result = new stdClass();
  358. $result->userid = $existinguser->userid;
  359. $result->username = $existinguser->username;
  360. $result->id = $joomla_id;
  361. $result->jname = $jname;
  362. } else {
  363. //the user does not exist in the software which means they were probably a guest or deleted from the integrated software
  364. //we can't create the user as we have no password
  365. $result = new stdClass();
  366. $result->userid = '0';
  367. $result->username = $userinfo->username;
  368. $result->id = $joomla_id;
  369. $result->jname = $jname;
  370. }
  371. }
  372. }
  373. return $result;
  374. }
  375. /**
  376. * Checks to see if a JFusion plugin is properly configured
  377. * @param string $jname name of the JFusion plugin used
  378. * @return bolean returns true if plugin is correctly configured
  379. */
  380. function validPlugin($jname)
  381. {
  382. $db =& JFactory::getDBO();
  383. $query = 'SELECT status FROM #__jfusion WHERE name =' . $db->Quote($jname);
  384. $db->setQuery($query);
  385. $result = $db->loadResult();
  386. if ($result == '1') {
  387. $result = true;
  388. return $result;
  389. } else {
  390. $result = false;
  391. return $result;
  392. }
  393. }
  394. function removeUser($userinfo)
  395. {
  396. //Delete old user data in the lookup table
  397. $db =& JFactory::getDBO();
  398. $query = 'DELETE FROM #__jfusion_users WHERE id =' . $userinfo->id . ' OR username =' . $db->Quote($userinfo->username) . ' OR LOWER(username) = ' . strtolower($db->Quote($userinfo->email));
  399. $db->setQuery($query);
  400. if(!$db->query()) {
  401. JError::raiseWarning(0,$db->stderr());
  402. }
  403. $query = 'DELETE FROM #__jfusion_users_plugin WHERE id =' . $userinfo->id ;
  404. $db->setQuery($query);
  405. if(!$db->query()) {
  406. JError::raiseWarning(0,$db->stderr());
  407. }
  408. }
  409. function addCookie($name, $value, $expires_time, $cookiepath, $cookiedomain, $httponly)
  410. {
  411. if($expires_time != 0) {
  412. $expires = time() + intval($expires_time);
  413. } else {
  414. $expires = 0;
  415. }
  416. // Versions of PHP prior to 5.2 do not support HttpOnly cookies and IE is buggy when specifying a blank domain so set the cookie manually
  417. $cookie = "Set-Cookie: {$name}=".urlencode($value);
  418. if ($expires > 0) {
  419. $cookie .= "; expires=".gmdate('D, d-M-Y H:i:s \\G\\M\\T', $expires);
  420. }
  421. if (!empty($cookiepath)) {
  422. $cookie .= "; path={$cookiepath}";
  423. }
  424. if (!empty($cookiedomain)) {
  425. $cookie .= "; domain={$cookiedomain}";
  426. }
  427. if ($httponly == true) {
  428. $cookie .= "; HttpOnly";
  429. }
  430. header($cookie, false);
  431. //$document = JFactory::getDocument();
  432. //$document->addCustomTag($cookie);
  433. }
  434. function raiseWarning($type, $warning, $jerror){
  435. if(is_array($warning)){
  436. foreach ($warning as $warningtext){
  437. if ($jerror){
  438. JError::raiseWarning('500', $type . ': '. $warningtext);
  439. } else {
  440. echo $type . ': '. $warningtext . '<br/>';
  441. }
  442. }
  443. } else {
  444. if ($jerror) {
  445. JError::raiseWarning('500', $type .': '. $warning);
  446. } else {
  447. echo $type . ': '. $warning . '<br/>';
  448. }
  449. }
  450. }
  451. function phpinfo_array(){
  452. //get the phpinfo and parse it into an array
  453. ob_start();
  454. phpinfo();
  455. $phpinfo = array('phpinfo' => array());
  456. if(preg_match_all('#(?:<h2>(?:<a name=".*?">)?(.*?)(?:</a>)?</h2>)|(?:<tr(?: class=".*?")?><t[hd](?: class=".*?")?>(.*?)\s*</t[hd]>(?:<t[hd](?: class=".*?")?>(.*?)\s*</t[hd]>(?:<t[hd](?: class=".*?")?>(.*?)\s*</t[hd]>)?)?</tr>)#s', ob_get_clean(), $matches, PREG_SET_ORDER))
  457. foreach($matches as $match)
  458. if(strlen($match[1]))
  459. $phpinfo[$match[1]] = array();
  460. elseif(isset($match[3]))
  461. $phpinfo[end(array_keys($phpinfo))][$match[2]] = isset($match[4]) ? array($match[3], $match[4]) : $match[3];
  462. else
  463. $phpinfo[end(array_keys($phpinfo))][] = $match[2];
  464. return $phpinfo;
  465. }
  466. function displayDonate(){
  467. ?>
  468. <table class="adminform">
  469. <tr>
  470. <td style="width: 90%;"><font size="3"><b><?php echo JText::_('DONATION_MESSAGE'); ?></b></font></td>
  471. <td style="width: 10%; text-align: right;">
  472. <form action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_donations" /> <input type="hidden"
  473. name="business" value="webmaster@jfusion.org" /> <input type="hidden" name="item_name" value="jfusion.org" /> <input type="hidden"
  474. name="no_shipping" value="0" /> <input type="hidden" name="no_note" value="1" /> <input type="hidden" name="currency_code" value="AUD" /> <input
  475. type="hidden" name="tax" value="0" /> <input type="hidden" name="lc" value="AU" /> <input type="hidden" name="bn" value="PP-DonationsBF" /> <input
  476. type="image" src="components/com_jfusion/images/donate.png" name="submit" alt="PayPal donation button." /> <img alt="" border="0"
  477. src="https://www.paypal.com/en_AU/i/scr/pixel.gif" width="1" height="1" /></form>
  478. </td>
  479. </tr>
  480. </table>
  481. <?php
  482. }
  483. /**
  484. * Updates the forum lookup table
  485. * @param $contentid
  486. * @param $threadid
  487. * @param $postid
  488. * @param $jname
  489. */
  490. function updateForumLookup($contentid, $forumid, $threadid, $postid, $jname)
  491. {
  492. $fdb =& JFactory::getDBO();
  493. $modified = time();
  494. $query = "REPLACE INTO #__jfusion_forum_plugin SET
  495. contentid = $contentid,
  496. forumid = $forumid,
  497. threadid = $threadid,
  498. postid = $postid,
  499. modified = '$modified',
  500. jname = '$jname'";
  501. $fdb->setQuery($query);
  502. $fdb->query();
  503. }
  504. /**
  505. * Creates the URL of a Joomla article
  506. * @param $contentitem
  507. * @param $text string to place as the link
  508. * @return link
  509. */
  510. function createJoomlaArticleURL(&$contentitem,$text,$jname='')
  511. {
  512. $itemid = JRequest::getVar('Itemid');
  513. //get the slugs
  514. $db =& JFactory::getDBO();
  515. $query = 'SELECT CASE WHEN CHAR_LENGTH(alias) THEN CONCAT_WS(\':\', id, alias) ELSE id END as slug FROM #__content WHERE id = ' . $contentitem->id;
  516. $db->setQuery($query);
  517. $slug = $db->loadResult();
  518. $article_url = 'index.php?option=com_content&view=article&id=' . $slug;
  519. if(!empty($contentitem->catid)) {
  520. $query = 'SELECT CASE WHEN CHAR_LENGTH(alias) THEN CONCAT_WS(\':\', id, alias) ELSE id END as slug FROM #__categories WHERE id = ' . $contentitem->catid;
  521. $db->setQuery($query);
  522. $cat_slug = $db->loadResult();
  523. $article_url .= '&catid='.$cat_slug;
  524. }
  525. if(!empty($itemid)) {
  526. $article_url .= '&Itemid='.$itemid;
  527. }
  528. //setup JRoute to use JFusion's router
  529. $app = JApplication::getInstance('site');
  530. $router = &$app->getRouter();
  531. $uri = $router->build($article_url);
  532. $article_url = $uri->toString();
  533. $joomla_url = JFusionFunction::getJoomlaURL();
  534. if(!strpos($article_url,'?')){
  535. $article_url .= '/';
  536. }
  537. $juri = new JURI($joomla_url);
  538. $path = $juri->getPath();
  539. if($path != '/'){
  540. $article_url = str_replace($path,'',$baseURL);
  541. }
  542. if (substr($joomla_url, -1) == '/') {
  543. if ($article_url[0] == '/') {
  544. $article_url = substr($joomla_url,0,-1) . $article_url;
  545. } else {
  546. $article_url = $joomla_url . $article_url;
  547. }
  548. } else {
  549. if ($article_url[0] == '/') {
  550. $article_url = $joomla_url . $article_url;
  551. } else {
  552. $article_url = $joomla_url . '/' . $article_url;
  553. }
  554. }
  555. //remove /administrator from path
  556. $article_url = str_replace('/administrator','',$article_url);
  557. $article_url = JRoute::_($article_url);
  558. $link = "<a href='".$article_url."'>$text</a>";
  559. return $link;
  560. }
  561. /**
  562. * Pasrses text from bbcode to html or html to bbcode
  563. * @param $text
  564. * @param $to string with what to conver the text to; bbcode or html
  565. * @param $stripAllHtml boolean if $to==bbcode, strips all unsupported html from text
  566. * @param $morePatten array $morePatten[0] startsearch, $morePatten[1] startreplace, $morePatten[2] endsearch, $morePatten[3] endreplace, strips all unsupported html from text *
  567. * @return string with converted text
  568. */
  569. function parseCode($text, $to, $stripAllHtml = false,$morePatten=null)
  570. {
  571. if($to=='html') {
  572. //entities must be decoded to prevent encoding already encoded entities
  573. $text = html_entity_decode($text);
  574. if(!class_exists('BBCode_Parser')) require_once("parsers/nbbc.php");
  575. $bbcode = new BBCode_Parser;
  576. $bbcode->SetSmileyURL(JFusionFunction::getJoomlaURL().'administrator/components/com_jfusion/models/parsers/smileys');
  577. $text = $bbcode->Parse($text);
  578. //must decode again to display entities properly
  579. $text = html_entity_decode($text);
  580. } elseif($to=='bbcode') {
  581. static $search, $replace;
  582. if(!is_array($search)) {
  583. $search = $replace = array();
  584. $search[] = "#<(blockquote|cite)[^>]*>(.*?)<\/\\1>#si";
  585. $replace[] = "[quote]$2[/quote]";
  586. $search[] = "#<ol[^>]*>(.*?)<\/ol>#si";
  587. $replace[] = "[list=1]$1[/list]";
  588. $search[] = "#<ul[^>]*>(.*?)<\/ul>#si";
  589. $replace[] = "[list]$1[/list]";
  590. $search[] = "#<li[^>]*>(.*?)<\/li>#si";
  591. $replace[] = "[*]$1";
  592. $search[] = "#<img [^>]*src=['|\"](?!\w{0,10}://)(.*?)['|\"][^>]*>#sie";
  593. $replace[] = "'[img]'.JRoute::_(JFusionFunction::getJoomlaURL().\"$1\").'[/img]'";
  594. $search[] = "#<img [^>]*src=['|\"](.*?)['|\"][^>]*>#sim";
  595. $replace[] = "[img]$1[/img]";
  596. $search[] = "#<a [^>]*href=['|\"]mailto:(.*?)['|\"][^>]*>(.*?)<\/a>#si";
  597. $replace[] = "[email=$1]$2[/email]";
  598. $search[] = "#<a [^>]*href=['|\"](?!\w{0,10}://|\#)(.*?)['|\"][^>]*>(.*?)</a>#sie";
  599. $replace[] = "'[url='.JRoute::_(JFusionFunction::getJoomlaURL().\"$1\").']$2[/url]'";
  600. $search[] = "#<a [^>]*href=['|\"](.*?)['|\"][^>]*>(.*?)<\/a>#si";
  601. $replace[] = "[url=$1]$2[/url]";
  602. $search[] = "#<(b|i|u)>(.*?)<\/\\1>#si";
  603. $replace[] = "[$1]$2[/$1]";
  604. $search[] = "#<font [^>]*color=['|\"](.*?)['|\"][^>]*>(.*?)<\/font>#si";
  605. $replace[] = "[color=$1]$2[/color]";
  606. $search[] = "#<p>(.*?)<\/p>#si";
  607. $replace[] = "$1\n\n";
  608. }
  609. $searchNS = $replaceNS = array();
  610. //convert anything between code, html, or php tags to html entities to prevent conversion
  611. $searchNS[] = "#<(code|pre)[^>]*>(.*?)<\/\\1>#sie";
  612. $replaceNS[] = "'[code]'.htmlspecialchars($2, ENT_QUOTES, UTF-8).'[/code]'";
  613. if ( is_array($morePatten) && isset($morePatten[0]) && isset($morePatten[1]) ) {
  614. $searchNS = array_merge($searchNS, $morePatten[0]);
  615. $replaceNS = array_merge($replaceNS, $morePatten[1]);
  616. }
  617. $searchNS = array_merge($searchNS, $search);
  618. $replaceNS = array_merge($replaceNS, $replace);
  619. if ( is_array($morePatten) && isset($morePatten[2]) && isset($morePatten[3]) ) {
  620. $searchNS = array_merge($searchNS, $morePatten[2]);
  621. $replaceNS = array_merge($replaceNS, $morePatten[3]);
  622. }
  623. //decode html entities that we converted for code and pre tags
  624. $searchNS[] = "#\[code\](.*?)\[\/code\]#sie";
  625. $replaceNS[] = "'[code]'.htmlspecialchars_decode($1,ENT_QUOTES).'[/code]'";
  626. $text = str_ireplace(array("<br />","<br>","<br/>"), "\n", $text);
  627. $text = preg_replace($searchNS,$replaceNS,$text);
  628. $text = preg_replace( '/\p{Z}/u', ' ', $text );
  629. if($stripAllHtml) { $text = strip_tags($text); }
  630. }
  631. return $text;
  632. }
  633. /**
  634. * Reconnects Joomla DB if it gets disconnected
  635. */
  636. function reconnectJoomlaDb()
  637. {
  638. //check to see if the Joomla database is still connnected
  639. $db = & JFactory::getDBO();
  640. jimport('joomla.database.database');
  641. jimport( 'joomla.database.table' );
  642. $conf =& JFactory::getConfig();
  643. $database = $conf->getValue('config.db');
  644. if (!$db->connected()) {
  645. //joomla connection needs to be re-established
  646. $host = $conf->getValue('config.host');
  647. $user = $conf->getValue('config.user');
  648. $password = $conf->getValue('config.password');
  649. $prefix = $conf->getValue('config.dbprefix');
  650. $dbtype = $conf->getValue('config.dbtype');
  651. if($dbtype == 'mysqli'){
  652. // Unlike mysql_connect(), mysqli_connect() takes the port and socket
  653. // as separate arguments. Therefore, we have to extract them from the
  654. // host string.
  655. $port = NULL;
  656. $socket = NULL;
  657. $targetSlot = substr( strstr( $host, ":" ), 1 );
  658. if (!empty( $targetSlot )) {
  659. // Get the port number or socket name
  660. if (is_numeric( $targetSlot ))
  661. $port = $targetSlot;
  662. else
  663. $socket = $targetSlot;
  664. // Extract the host name only
  665. $host = substr( $host, 0, strlen( $host ) - (strlen( $targetSlot ) + 1) );
  666. // This will take care of the following notation: ":3306"
  667. if($host == '')
  668. $host = 'localhost';
  669. }
  670. // connect to the server
  671. if (!($db->_resource = @mysqli_connect($host, $user, $password, NULL, $port, $socket))) {
  672. $db->_errorNum = 2;
  673. $db->_errorMsg = 'Could not connect to MySQL';
  674. die ('could not reconnect to the Joomla database');
  675. }
  676. } else {
  677. // connect using mysql
  678. if (!($db->_resource = @mysql_connect( $host, $user, $password, false ))) {
  679. $db->_errorNum = 2;
  680. $db->_errorMsg = 'Could not connect to MySQL';
  681. die ('could not reconnect to the Joomla database');
  682. }
  683. }
  684. }
  685. if (!$db->select($database)) {
  686. $db->_errorNum = 2;
  687. $db->_errorMsg = 'Could not sellect to MySQL';
  688. die ('could not sellect to the Joomla database');
  689. }
  690. //add utf8 support
  691. $db->Execute('SET names \'utf8\'');
  692. //legacy $database must be restored
  693. if(JPluginHelper::getPlugin('system','legacy')) {
  694. $GLOBALS['database'] =& $db;
  695. }
  696. }
  697. /**
  698. * Retrieves the URL to a userprofile of a Joomla supported component
  699. * @param $software string name of the software
  700. * @param $uid int userid of the user
  701. * @return string URL
  702. */
  703. function getAltProfileURL($software, $uid, $isPluginUid = false, $jname = '', $username = '')
  704. {
  705. $db = & JFactory::getDBO();
  706. if($isPluginUid && !empty($jname)) {
  707. $userlookup = JFusionFunction::lookupUser($jname,$uid,false,$username);
  708. if(!empty($userlookup)) {
  709. $uid = $userlookup->id;
  710. } else {
  711. return '';
  712. }
  713. }
  714. if(!empty($uid)){
  715. if($software=="cb") {
  716. $query = "SELECT id FROM #__menu WHERE type = 'component' AND link LIKE '%com_comprofiler%' LIMIT 1";
  717. $db->setQuery($query);
  718. $itemid = $db->loadResult();
  719. $url = 'index.php?option=com_comprofiler&task=userProfile&Itemid='.$itemid.'&user='.$uid;
  720. } elseif($software=="jomsocial") {
  721. $query = "SELECT id FROM #__menu WHERE type = 'component' AND link LIKE '%com_community%' LIMIT 1";
  722. $db->setQuery($query);
  723. $itemid = $db->loadResult();
  724. $url = 'index.php?option=com_community&view=profile&Itemid='.$itemid.'&userid='.$uid;
  725. } elseif($software=="joomunity") {
  726. $query = "SELECT id FROM #__menu WHERE type = 'component' AND link LIKE '%com_joomunity%' LIMIT 1";
  727. $db->setQuery($query);
  728. $itemid = $db->loadResult();
  729. $url = 'index.php?option=com_joomunity&Itemid='.$itemid.'&cmd=Profile.View.'.$uid;
  730. } else {
  731. $url = false;
  732. }
  733. } else {
  734. $url = false;
  735. }
  736. return $url;
  737. }
  738. /**
  739. * Retrieves the source of the avatar for a Joomla supported component
  740. * @param $software
  741. * @param $uid
  742. * @param $isPluginUid boolean if true, look up the Joomla id in the look up table
  743. * @param $jname needed if $isPluginId = true
  744. * @return unknown_type
  745. */
  746. function getAltAvatar($software, $uid, $isPluginUid = false, $jname = '', $username = '')
  747. {
  748. $db = & JFactory::getDBO();
  749. if($isPluginUid && !empty($jname)) {
  750. $userlookup = JFusionFunction::lookupUser($jname,$uid,false,$username);
  751. if(!empty($userlookup)) {
  752. $uid = $userlookup->id;
  753. } else {
  754. //no user was found
  755. $avatar = 'components/com_comprofiler/plugin/templates/default/images/avatar/nophoto_n.png';
  756. return $avatar;
  757. }
  758. }
  759. if($software=="cb") {
  760. $query = "SELECT avatar FROM #__comprofiler WHERE user_id = '$uid'";
  761. $db->setQuery($query);
  762. $result = $db->loadResult();
  763. if(!empty($result)) {
  764. $avatar = "images/comprofiler".DS.$result;
  765. } else {
  766. $avatar = 'components/com_comprofiler/plugin/templates/default/images/avatar/nophoto_n.png';
  767. }
  768. } elseif($software=="jomsocial") {
  769. $query = "SELECT avatar FROM #__community_users WHERE userid = '$uid'";
  770. $db->setQuery($query);
  771. $result = $db->loadResult();
  772. if(!empty($result)) {
  773. $avatar = $result;
  774. } else {
  775. $avatar = 'components/com_community/assets/default_thumb.jpg';
  776. }
  777. } elseif($software=="joomunity") {
  778. $query = "SELECT user_picture FROM #__joom_users WHERE user_id = '$uid'";
  779. $db->setQuery($query);
  780. $result = $db->loadResult();
  781. $avatar = 'components/com_joomunity/files/avatars/'.$result;
  782. } elseif($software=="gravatar") {
  783. $query = "SELECT email FROM #__users WHERE id = '$uid'";
  784. $db->setQuery($query);
  785. $email = $db->loadResult();
  786. $avatar = "http://www.gravatar.com/avatar.php?gravatar_id=".md5( strtolower($email) )."&size=40";
  787. } else {
  788. $avatar = 'components/com_jfusion/images/noavatar.png';
  789. }
  790. return $avatar;
  791. }
  792. /**
  793. * Gets the source_url from the joomla_int plugin
  794. * @return Joomla's source URL
  795. */
  796. function getJoomlaURL()
  797. {
  798. static $joomla_source_url;
  799. if(empty($joomla_source_url)) {
  800. $params =& JFusionFactory::getParams('joomla_int');
  801. $joomla_source_url = $params->get('source_url');
  802. }
  803. return $joomla_source_url;
  804. }
  805. /**
  806. * Gets the base url of a specific menu item
  807. * @param $itemid int id of the menu item
  808. * @return parsed base URL of the menu item
  809. */
  810. function getPluginURL($itemid)
  811. {
  812. $joomla_url = JFusionFunction::getJoomlaURL();
  813. $baseURL = JRoute::_('index.php?option=com_jfusion&Itemid=' . $itemid);
  814. if(!strpos($baseURL,'?')){
  815. $baseURL = preg_replace('#\.[\w]{3,4}\z#is','',$baseURL);
  816. if(substr($baseURL, -1) != '/')
  817. {
  818. $baseURL .= '/';
  819. }
  820. }
  821. $juri = new JURI($joomla_url);
  822. $path = $juri->getPath();
  823. if($path != '/'){
  824. $baseURL = str_replace($path,'',$baseURL);
  825. }
  826. if (substr($joomla_url, -1) == '/') {
  827. if ($baseURL[0] == '/') {
  828. $baseURL = substr($joomla_url,0,-1) . $baseURL;
  829. } else {
  830. $baseURL = $joomla_url . $baseURL;
  831. }
  832. } else {
  833. if ($baseURL[0] == '/') {
  834. $baseURL = $joomla_url . $baseURL;
  835. } else {
  836. $baseURL = $joomla_url . '/' . $baseURL;
  837. }
  838. }
  839. //let's clean up the URL here before passing it
  840. $baseURL = str_replace('&amp;','&',$baseURL);
  841. return $baseURL;
  842. }
  843. function anonymizeUserinfo(&$userinfo)
  844. {
  845. $userinfo->password_clear = '******';
  846. if(isset($userinfo->password)){
  847. $userinfo->password = substr($userinfo->password,0,6) .'********';
  848. }
  849. if(isset($userinfo->password_salt)){
  850. $userinfo->password_salt = substr($userinfo->password_salt,0,4) .'*****';
  851. };
  852. }
  853. }