PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/redirect/code/trunk/plugins/system/redirect.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 95 lines | 50 code | 9 blank | 36 comment | 11 complexity | c12d04aa7fdf8838711bdb9722322a2e MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: redirect.php 580 2011-02-25 05:11:47Z eddieajau $
  4. * @package NewLifeInIT
  5. * @subpackage plgSystemRedirect
  6. * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  8. * @link http://www.theartofjoomla.com
  9. */
  10. defined('JPATH_BASE') or die;
  11. /**
  12. * Plugin class for Redirect.
  13. *
  14. * @package NewLifeInIT
  15. * @subpackage plgSystemRedirect
  16. * @since 1.0
  17. */
  18. class plgSystemRedirect extends JPlugin
  19. {
  20. /**
  21. * Method to handle hard errors and catch 404 not found errors.
  22. *
  23. * @param object JException object.
  24. * @return void
  25. * @since 1.0
  26. */
  27. public static function handleError(&$error)
  28. {
  29. // Get the application object.
  30. $app = JFactory::getApplication();
  31. // Only apply special error handling if the error is a 404 and we aren't in the admin.
  32. if (!$app->isAdmin() and ($error->code == 404)) {
  33. // Get the current requested URI.
  34. $currentURI = JURI::getInstance()->toString(array('scheme', 'host', 'port', 'path', 'query', 'fragment'));
  35. // Attempt to ignore idiots.
  36. if ((strpos($currentURI, 'mosConfig_') !== false) || (strpos($currentURI, '=http://') !== false)) {
  37. JError::customErrorPage($error);
  38. }
  39. // Get the database connection object.
  40. $db = JFactory::getDBO();
  41. // Check to see if a redirect is set for the current URI.
  42. $db->setQuery(
  43. 'SELECT `id`, `new_url`, `published`' .
  44. ' FROM `#__redirect_links`' .
  45. ' WHERE `old_url` = '.$db->quote($currentURI),
  46. 0, 1
  47. );
  48. $link = $db->loadObject();
  49. // If the redirect exist and is published redirect the browser.
  50. if ($link && ($link->published == 1)) {
  51. $app->redirect($link->new_url, null, null, true);
  52. }
  53. // Render the Joomla! error page and add the URI to the database if necessary.
  54. else {
  55. // Add the current URI to the database if not present.
  56. if (empty($link)) {
  57. $now = JFactory::getDate();
  58. $referer = @$_SERVER['HTTP_REFERER'];
  59. $db->setQuery(
  60. 'INSERT INTO `#__redirect_links` (`old_url`, `referer`, `published`, `created_date`, `hits`)' .
  61. ' VALUES ('.$db->quote($currentURI).', '.$db->quote($referer).', 0, '.$db->quote($now->toMySQL()).', 1)'
  62. );
  63. $db->query();
  64. }
  65. // Increase the hit counter if it is present.
  66. else {
  67. $db->setQuery(
  68. 'UPDATE `#__redirect_links`' .
  69. ' SET `hits` = `hits` + 1' .
  70. ' WHERE `id` = '.(int) $link->id
  71. );
  72. $db->query();
  73. }
  74. // Render the Joomla! error page.
  75. JError::customErrorPage($error);
  76. }
  77. }
  78. // We are either in the administrator application or have a non-404 error.
  79. else {
  80. // Render the Joomla! error page.
  81. JError::customErrorPage($error);
  82. }
  83. }
  84. }
  85. // Reset the hard error handling to use the plugin callback.
  86. JError::setErrorHandling(E_ERROR, 'callback', array('plgSystemRedirect', 'handleError'));