PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/system/backlink.php

https://bitbucket.org/StasPiv/playzone
PHP | 529 lines | 396 code | 61 blank | 72 comment | 135 complexity | ad85f415e2222e2fd1530aae5b15f96b MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: backlink.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla
  5. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  6. * @license GNU/GPL, see LICENSE.php
  7. * Joomla! is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. // no direct access
  14. defined('_JEXEC') or die('Restricted access');
  15. jimport('joomla.application.menu');
  16. jimport( 'joomla.plugin.plugin' );
  17. class plgSystemBacklink extends JPlugin
  18. {
  19. var $_db = null;
  20. /**
  21. * Constructor
  22. *
  23. * For php4 compatability we must not use the __constructor as a constructor for plugins
  24. * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
  25. * This causes problems with cross-referencing necessary for the observer design pattern.
  26. *
  27. * @access protected
  28. * @param object $subject The object to observe
  29. * @param array $config An array that holds the plugin configuration
  30. * @since 1.0
  31. */
  32. function plgSystemBacklink(& $subject, $config)
  33. {
  34. $this->_db = JFactory::getDBO();
  35. parent :: __construct($subject, $config);
  36. }
  37. function onAfterInitialise()
  38. {
  39. global $mainframe;
  40. if ($mainframe->isAdmin()) {
  41. return; // Dont run in admin
  42. }
  43. $sef = $this->params->get('sef', 1);
  44. $url = $this->params->get('url', 1);
  45. $legacysef = $this->params->get('legacysef', 1);
  46. if (!$sef && !$url && !$legacysef)
  47. return; // None of the options enabled, bail!
  48. // Grab the system as early as possible, we're going to terminate it potentially
  49. // Case 1: Query string match (shouldn't need this but its here anyway)
  50. if ($url && isset ($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) && strpos($_SERVER['QUERY_STRING'], '&')) {
  51. $query_string = $_SERVER['QUERY_STRING'];
  52. $this->_lookup($query_string);
  53. }
  54. // Case 2: SEF or similar match
  55. if ($sef && isset ($_SERVER['SCRIPT_NAME']) && isset ($_SERVER['REQUEST_URI'])) {
  56. $part = str_replace('index.php', '', $_SERVER['SCRIPT_NAME']);
  57. if($part != '/') {
  58. $search = str_replace($part, '', $_SERVER['REQUEST_URI']);
  59. } else {
  60. $search = ltrim($_SERVER['REQUEST_URI'],'/');
  61. }
  62. $this->_lookup($search);
  63. }
  64. // Case 3: Old school core sef; used to backlink
  65. // Enable only if:
  66. // 1: SEF is enabled
  67. // 2: Legacy SEF Plugin Param is set
  68. // 3: And there is no backlink
  69. if ($mainframe->getCfg('sef')
  70. && $legacysef
  71. && !strstr($_SERVER['REQUEST_URI'],'nobacklink')
  72. && !strlen($_SERVER['QUERY_STRING'])) {
  73. $this->_legacysef();
  74. }
  75. }
  76. function _lookup($searchstring)
  77. {
  78. // return blank strings and just index.php on its own...
  79. if (!strlen($searchstring) || $searchstring == trim('index.php',' ?')) {
  80. return;
  81. }
  82. $sef = $this->params->get('sef', 1);
  83. $url = $this->params->get('url', 1);
  84. if (!$sef && !$url) {
  85. return; // Neither option enabled, bail!
  86. }
  87. $query = 'SELECT * FROM #__migration_backlinks WHERE ';
  88. $where = Array ();
  89. $search = $this->_db->Quote( $this->_db->getEscaped( $searchstring, true ).'%', false );
  90. if ($url) {
  91. $where[] = 'url LIKE ' . $search;
  92. }
  93. if ($sef) {
  94. $where[] = 'sefurl LIKE ' . $search;
  95. }
  96. $query .= implode(' OR ', $where);
  97. $this->_db->setQuery($query);
  98. $results = $this->_db->loadAssocList();
  99. if (count($results)) {
  100. // Get the first one...
  101. $this->_redirect($results[0]['itemid'], $results[0]['name'], $results[0]['newurl']);
  102. }
  103. }
  104. function _redirect($Itemid, $name, $url = null)
  105. {
  106. global $mainframe;
  107. if (!strlen($url))
  108. {
  109. $menu = & JSite :: getMenu();
  110. $item = $menu->getItem($Itemid);
  111. //$url = $item->link;
  112. switch ($item->type)
  113. {
  114. case 'url' :
  115. if ((strpos($item->link, 'index.php?') !== false) && (strpos($item->link, 'Itemid=') === false)) {
  116. $url = $item->link . '&amp;Itemid=' . $item->id;
  117. } else {
  118. $url = $item->link;
  119. }
  120. break;
  121. default :
  122. $url = 'index.php?Itemid=' . $item->id;
  123. //$url = $item->link . '&Itemid='.$item->id;
  124. break;
  125. }
  126. $url = JRoute :: _($url);
  127. //$url = JURI :: base() . $url; // was $surl with third option of below being url and second being surl
  128. $name = $item->name;
  129. }
  130. // Check we're not redirecting to ourselves
  131. if(!stristr($url,$_SERVER['REQUEST_URI']) && !stristr($url,$_SERVER['SCRIPT_NAME'].'/'.$_SERVER['QUERY_STRING'])) {
  132. return;
  133. }
  134. $name = $name ? $name : "Unknown";
  135. header('Location: ' . str_replace('&amp;','&',$url), true, '301'); // redirect and kill of and &amp;
  136. jexit(JText :: sprintf('"%s" has moved to <a href="%s">%s</a>. Click the link if your browser does not redirect you automatically.', $name, $url, $url));
  137. }
  138. function _legacysef()
  139. {
  140. $mosConfig_absolute_path = JPATH_SITE;
  141. $mosConfig_live_site = JURI :: base();
  142. $url_array = explode('/', $_SERVER['REQUEST_URI']);
  143. if (in_array('content', $url_array))
  144. {
  145. /**
  146. * Content
  147. * http://www.domain.com/$option/$task/$sectionid/$id/$Itemid/$limit/$limitstart
  148. */
  149. $uri = explode('content/', $_SERVER['REQUEST_URI']);
  150. $option = 'com_content';
  151. $_GET['option'] = $option;
  152. $_REQUEST['option'] = $option;
  153. $pos = array_search('content', $url_array);
  154. // language hook for content
  155. $lang = '';
  156. foreach ($url_array as $key => $value)
  157. {
  158. if (!strcasecmp(substr($value, 0, 5), 'lang,'))
  159. {
  160. $temp = explode(',', $value);
  161. if (isset ($temp[0]) && $temp[0] != '' && isset ($temp[1]) && $temp[1] != '')
  162. {
  163. $_GET['lang'] = $temp[1];
  164. $_REQUEST['lang'] = $temp[1];
  165. $lang = $temp[1];
  166. }
  167. unset ($url_array[$key]);
  168. }
  169. }
  170. if (isset ($url_array[$pos +8]) && $url_array[$pos +8] != '' && in_array('category', $url_array) && (strpos($url_array[$pos +5], 'order,') !== false) && (strpos($url_array[$pos +6], 'filter,') !== false))
  171. {
  172. // $option/$task/$sectionid/$id/$Itemid/$order/$filter/$limit/$limitstart
  173. $task = $url_array[$pos +1];
  174. $sectionid = $url_array[$pos +2];
  175. $id = $url_array[$pos +3];
  176. $Itemid = $url_array[$pos +4];
  177. $order = str_replace('order,', '', $url_array[$pos +5]);
  178. $filter = str_replace('filter,', '', $url_array[$pos +6]);
  179. $limit = $url_array[$pos +7];
  180. $limitstart = $url_array[$pos +8];
  181. // pass data onto global variables
  182. $_GET['task'] = $task;
  183. $_REQUEST['task'] = $task;
  184. $_GET['sectionid'] = $sectionid;
  185. $_REQUEST['sectionid'] = $sectionid;
  186. $_GET['id'] = $id;
  187. $_REQUEST['id'] = $id;
  188. $_GET['Itemid'] = $Itemid;
  189. $_REQUEST['Itemid'] = $Itemid;
  190. $_GET['order'] = $order;
  191. $_REQUEST['order'] = $order;
  192. $_GET['filter'] = $filter;
  193. $_REQUEST['filter'] = $filter;
  194. $_GET['limit'] = $limit;
  195. $_REQUEST['limit'] = $limit;
  196. $_GET['limitstart'] = $limitstart;
  197. $_REQUEST['limitstart'] = $limitstart;
  198. $QUERY_STRING = "option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid&order=$order&filter=$filter&limit=$limit&limitstart=$limitstart";
  199. }
  200. else if (isset ($url_array[$pos +7]) && $url_array[$pos +7] != '' && $url_array[$pos +5] > 1000 && (in_array('archivecategory', $url_array) || in_array('archivesection', $url_array)))
  201. {
  202. // $option/$task/$id/$limit/$limitstart/year/month/module
  203. $task = $url_array[$pos +1];
  204. $id = $url_array[$pos +2];
  205. $limit = $url_array[$pos +3];
  206. $limitstart = $url_array[$pos +4];
  207. $year = $url_array[$pos +5];
  208. $month = $url_array[$pos +6];
  209. $module = $url_array[$pos +7];
  210. // pass data onto global variables
  211. $_GET['task'] = $task;
  212. $_REQUEST['task'] = $task;
  213. $_GET['id'] = $id;
  214. $_REQUEST['id'] = $id;
  215. $_GET['limit'] = $limit;
  216. $_REQUEST['limit'] = $limit;
  217. $_GET['limitstart'] = $limitstart;
  218. $_REQUEST['limitstart'] = $limitstart;
  219. $_GET['year'] = $year;
  220. $_REQUEST['year'] = $year;
  221. $_GET['month'] = $month;
  222. $_REQUEST['month'] = $month;
  223. $_GET['module'] = $module;
  224. $_REQUEST['module'] = $module;
  225. $QUERY_STRING = "option=com_content&task=$task&id=$id&limit=$limit&limitstart=$limitstart&year=$year&month=$month&module=$module";
  226. }
  227. else if (isset ($url_array[$pos +7]) && $url_array[$pos +7] != '' && $url_array[$pos +6] > 1000 && (in_array('archivecategory', $url_array) || in_array('archivesection', $url_array)))
  228. {
  229. // $option/$task/$id/$Itemid/$limit/$limitstart/year/month
  230. $task = $url_array[$pos +1];
  231. $id = $url_array[$pos +2];
  232. $Itemid = $url_array[$pos +3];
  233. $limit = $url_array[$pos +4];
  234. $limitstart = $url_array[$pos +5];
  235. $year = $url_array[$pos +6];
  236. $month = $url_array[$pos +7];
  237. // pass data onto global variables
  238. $_GET['task'] = $task;
  239. $_REQUEST['task'] = $task;
  240. $_GET['id'] = $id;
  241. $_REQUEST['id'] = $id;
  242. $_GET['Itemid'] = $Itemid;
  243. $_REQUEST['Itemid'] = $Itemid;
  244. $_GET['limit'] = $limit;
  245. $_REQUEST['limit'] = $limit;
  246. $_GET['limitstart'] = $limitstart;
  247. $_REQUEST['limitstart'] = $limitstart;
  248. $_GET['year'] = $year;
  249. $_REQUEST['year'] = $year;
  250. $_GET['month'] = $month;
  251. $_REQUEST['month'] = $month;
  252. $QUERY_STRING = "option=com_content&task=$task&id=$id&Itemid=$Itemid&limit=$limit&limitstart=$limitstart&year=$year&month=$month";
  253. }
  254. else if (isset ($url_array[$pos +7]) && $url_array[$pos +7] != '' && in_array('category', $url_array) && (strpos($url_array[$pos +5], 'order,') !== false))
  255. {
  256. // $option/$task/$sectionid/$id/$Itemid/$order/$limit/$limitstart
  257. $task = $url_array[$pos +1];
  258. $sectionid = $url_array[$pos +2];
  259. $id = $url_array[$pos +3];
  260. $Itemid = $url_array[$pos +4];
  261. $order = str_replace('order,', '', $url_array[$pos +5]);
  262. $limit = $url_array[$pos +6];
  263. $limitstart = $url_array[$pos +7];
  264. // pass data onto global variables
  265. $_GET['task'] = $task;
  266. $_REQUEST['task'] = $task;
  267. $_GET['sectionid'] = $sectionid;
  268. $_REQUEST['sectionid'] = $sectionid;
  269. $_GET['id'] = $id;
  270. $_REQUEST['id'] = $id;
  271. $_GET['Itemid'] = $Itemid;
  272. $_REQUEST['Itemid'] = $Itemid;
  273. $_GET['order'] = $order;
  274. $_REQUEST['order'] = $order;
  275. $_GET['limit'] = $limit;
  276. $_REQUEST['limit'] = $limit;
  277. $_GET['limitstart'] = $limitstart;
  278. $_REQUEST['limitstart'] = $limitstart;
  279. $QUERY_STRING = "option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid&order=$order&limit=$limit&limitstart=$limitstart";
  280. }
  281. else if (isset ($url_array[$pos +6]) && $url_array[$pos +6] != '')
  282. {
  283. // $option/$task/$sectionid/$id/$Itemid/$limit/$limitstart
  284. $task = $url_array[$pos +1];
  285. $sectionid = $url_array[$pos +2];
  286. $id = $url_array[$pos +3];
  287. $Itemid = $url_array[$pos +4];
  288. $limit = $url_array[$pos +5];
  289. $limitstart = $url_array[$pos +6];
  290. // pass data onto global variables
  291. $_GET['task'] = $task;
  292. $_REQUEST['task'] = $task;
  293. $_GET['sectionid'] = $sectionid;
  294. $_REQUEST['sectionid'] = $sectionid;
  295. $_GET['id'] = $id;
  296. $_REQUEST['id'] = $id;
  297. $_GET['Itemid'] = $Itemid;
  298. $_REQUEST['Itemid'] = $Itemid;
  299. $_GET['limit'] = $limit;
  300. $_REQUEST['limit'] = $limit;
  301. $_GET['limitstart'] = $limitstart;
  302. $_REQUEST['limitstart'] = $limitstart;
  303. $QUERY_STRING = "option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid&limit=$limit&limitstart=$limitstart";
  304. }
  305. else if (isset ($url_array[$pos +5]) && $url_array[$pos +5] != '')
  306. {
  307. // $option/$task/$id/$Itemid/$limit/$limitstart
  308. $task = $url_array[$pos +1];
  309. $id = $url_array[$pos +2];
  310. $Itemid = $url_array[$pos +3];
  311. $limit = $url_array[$pos +4];
  312. $limitstart = $url_array[$pos +5];
  313. // pass data onto global variables
  314. $_GET['task'] = $task;
  315. $_REQUEST['task'] = $task;
  316. $_GET['id'] = $id;
  317. $_REQUEST['id'] = $id;
  318. $_GET['Itemid'] = $Itemid;
  319. $_REQUEST['Itemid'] = $Itemid;
  320. $_GET['limit'] = $limit;
  321. $_REQUEST['limit'] = $limit;
  322. $_GET['limitstart'] = $limitstart;
  323. $_REQUEST['limitstart'] = $limitstart;
  324. $QUERY_STRING = "option=com_content&task=$task&id=$id&Itemid=$Itemid&limit=$limit&limitstart=$limitstart";
  325. }
  326. else if (isset ($url_array[$pos +4]) && $url_array[$pos +4] != '' && (in_array('archivecategory', $url_array) || in_array('archivesection', $url_array)))
  327. {
  328. // $option/$task/$year/$month/$module
  329. $task = $url_array[$pos +1];
  330. $year = $url_array[$pos +2];
  331. $month = $url_array[$pos +3];
  332. $module = $url_array[$pos +4];
  333. // pass data onto global variables
  334. $_GET['task'] = $task;
  335. $_REQUEST['task'] = $task;
  336. $_GET['year'] = $year;
  337. $_REQUEST['year'] = $year;
  338. $_GET['month'] = $month;
  339. $_REQUEST['month'] = $month;
  340. $_GET['module'] = $module;
  341. $_REQUEST['module'] = $module;
  342. $QUERY_STRING = "option=com_content&task=$task&year=$year&month=$month&module=$module";
  343. }
  344. else if (!(isset ($url_array[$pos +5]) && $url_array[$pos +5] != '') && isset ($url_array[$pos +4]) && $url_array[$pos +4] != '')
  345. {
  346. // $option/$task/$sectionid/$id/$Itemid
  347. $task = $url_array[$pos +1];
  348. $sectionid = $url_array[$pos +2];
  349. $id = $url_array[$pos +3];
  350. $Itemid = $url_array[$pos +4];
  351. // pass data onto global variables
  352. $_GET['task'] = $task;
  353. $_REQUEST['task'] = $task;
  354. $_GET['sectionid'] = $sectionid;
  355. $_REQUEST['sectionid'] = $sectionid;
  356. $_GET['id'] = $id;
  357. $_REQUEST['id'] = $id;
  358. $_GET['Itemid'] = $Itemid;
  359. $_REQUEST['Itemid'] = $Itemid;
  360. $QUERY_STRING = "option=com_content&task=$task&sectionid=$sectionid&id=$id&Itemid=$Itemid";
  361. }
  362. else if (!(isset ($url_array[$pos +4]) && $url_array[$pos +4] != '') && (isset ($url_array[$pos +3]) && $url_array[$pos +3] != ''))
  363. {
  364. // $option/$task/$id/$Itemid
  365. $task = $url_array[$pos +1];
  366. $id = $url_array[$pos +2];
  367. $Itemid = $url_array[$pos +3];
  368. // pass data onto global variables
  369. $_GET['task'] = $task;
  370. $_REQUEST['task'] = $task;
  371. $_GET['id'] = $id;
  372. $_REQUEST['id'] = $id;
  373. $_GET['Itemid'] = $Itemid;
  374. $_REQUEST['Itemid'] = $Itemid;
  375. $QUERY_STRING = "option=com_content&task=$task&id=$id&Itemid=$Itemid";
  376. }
  377. else if (!(isset ($url_array[$pos +3]) && $url_array[$pos +3] != '') && (isset ($url_array[$pos +2]) && $url_array[$pos +2] != ''))
  378. {
  379. // $option/$task/$id
  380. $task = $url_array[$pos +1];
  381. $id = $url_array[$pos +2];
  382. // pass data onto global variables
  383. $_GET['task'] = $task;
  384. $_REQUEST['task'] = $task;
  385. $_GET['id'] = $id;
  386. $_REQUEST['id'] = $id;
  387. $QUERY_STRING = "option=com_content&task=$task&id=$id";
  388. }
  389. else if (!(isset ($url_array[$pos +2]) && $url_array[$pos +2] != '') && (isset ($url_array[$pos +1]) && $url_array[$pos +1] != ''))
  390. {
  391. // $option/$task
  392. $task = $url_array[$pos +1];
  393. $_GET['task'] = $task;
  394. $_REQUEST['task'] = $task;
  395. $QUERY_STRING = 'option=com_content&task=' . $task;
  396. }
  397. if ($lang != '') {
  398. $QUERY_STRING .= '&amp;lang=' . $lang;
  399. }
  400. $_SERVER['QUERY_STRING'] = $QUERY_STRING;
  401. $REQUEST_URI = $uri[0] . 'index.php?' . $QUERY_STRING;
  402. $_SERVER['REQUEST_URI'] = $REQUEST_URI;
  403. }
  404. else if (in_array('component', $url_array))
  405. {
  406. $name = 'component';
  407. /*
  408. Components
  409. http://www.domain.com/component/$name,$value
  410. */
  411. $uri = explode('component/', $_SERVER['REQUEST_URI']);
  412. $uri_array = explode('/', $uri[1]);
  413. $QUERY_STRING = '';
  414. // needed for check if component exists
  415. $path = $mosConfig_absolute_path . '/components';
  416. $dirlist = array ();
  417. if (is_dir($path))
  418. {
  419. $base = opendir($path);
  420. while (false !== ($dir = readdir($base)))
  421. {
  422. if ($dir !== '.' && $dir !== '..' && is_dir($path . '/' . $dir) && strtolower($dir) !== 'cvs' && strtolower($dir) !== '.svn') {
  423. $dirlist[] = $dir;
  424. }
  425. }
  426. closedir($base);
  427. }
  428. foreach ($uri_array as $value)
  429. {
  430. $temp = explode(',', $value);
  431. if (isset ($temp[0]) && $temp[0] != '' && isset ($temp[1]) && $temp[1] != '')
  432. {
  433. $_GET[$temp[0]] = $temp[1];
  434. $_REQUEST[$temp[0]] = $temp[1];
  435. // check to ensure component actually exists
  436. if ($temp[0] == 'option')
  437. {
  438. $check = '';
  439. if (count($dirlist)) {
  440. foreach ($dirlist as $dir) {
  441. if ($temp[1] == $dir) {
  442. $check = 1;
  443. break;
  444. }
  445. }
  446. }
  447. // redirect to 404 page if no component found to match url
  448. if (!$check)
  449. {
  450. header('HTTP/1.0 404 Not Found');
  451. require_once ($mosConfig_absolute_path . '/templates/404.php');
  452. exit (404);
  453. }
  454. }
  455. if ($QUERY_STRING == '') {
  456. $QUERY_STRING .= "$temp[0]=$temp[1]";
  457. } else {
  458. $QUERY_STRING .= "&$temp[0]=$temp[1]";
  459. }
  460. }
  461. }
  462. $_SERVER['QUERY_STRING'] = $QUERY_STRING;
  463. $REQUEST_URI = $uri[0] . 'index.php?' . $QUERY_STRING;
  464. $_SERVER['REQUEST_URI'] = $REQUEST_URI;
  465. }
  466. // let this go through and the rest of the system should handle it properly
  467. }
  468. }