Source for file redirect.php

Documentation is available at redirect.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  System.redirect
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('JPATH_BASE'or die;
  11.  
  12. /**
  13.  * Plugin class for redirect handling.
  14.  *
  15.  * @package     Joomla.Plugin
  16.  * @subpackage  System.redirect
  17.  * @since       1.6
  18.  */
  19. class PlgSystemRedirect extends JPlugin
  20. {
  21.     /**
  22.      * Object Constructor.
  23.      *
  24.      * @access    public
  25.      * @param   object    The object to observe -- event dispatcher.
  26.      * @param   object    The configuration object for the plugin.
  27.      * @return  void 
  28.      * @since   1.6
  29.      */
  30.     public function __construct(&$subject$config)
  31.     {
  32.         parent::__construct($subject$config);
  33.  
  34.         // Set the error handler for E_ERROR to be the class handleError method.
  35.         JError::setErrorHandling(E_ERROR'callback'array('PlgSystemRedirect''handleError'));
  36.         set_exception_handler(array('PlgSystemRedirect''handleError'));
  37.     }
  38.  
  39.     public static function handleError(&$error)
  40.     {
  41.         // Get the application object.
  42.         $app JFactory::getApplication();
  43.  
  44.         // Make sure the error is a 404 and we are not in the administrator.
  45.         if (!$app->isAdmin(and ($error->getCode(== 404))
  46.         {
  47.             // Get the full current URI.
  48.             $uri JUri::getInstance();
  49.             $current $uri->toString(array('scheme''host''port''path''query''fragment'));
  50.  
  51.             // Attempt to ignore idiots.
  52.             if ((strpos($current'mosConfig_'!== false|| (strpos($current'=http://'!== false))
  53.             {
  54.                 // Render the error page.
  55.                 JError::customErrorPage($error);
  56.             }
  57.  
  58.             // See if the current url exists in the database as a redirect.
  59.             $db JFactory::getDbo();
  60.             $query $db->getQuery(true)
  61.                 ->select($db->quoteName('new_url'))
  62.                 ->select($db->quoteName('published'))
  63.                 ->from($db->quoteName('#__redirect_links'))
  64.                 ->where($db->quoteName('old_url'' = ' $db->quote($current));
  65.             $db->setQuery($query01);
  66.             $link $db->loadObject();
  67.  
  68.             // If a redirect exists and is published, permanently redirect.
  69.             if ($link and ($link->published == 1))
  70.             {
  71.                 $app->redirect($link->new_urltrue);
  72.             }
  73.             else
  74.             {
  75.                 $referer empty($_SERVER['HTTP_REFERER']'' $_SERVER['HTTP_REFERER'];
  76.  
  77.                 $db->setQuery('SELECT id FROM ' $db->quoteName('#__redirect_links''  WHERE old_url= ' $db->quote($current));
  78.                 $res $db->loadResult();
  79.                 if (!$res)
  80.                 {
  81.  
  82.                     // If not, add the new url to the database.
  83.                     $columns array(
  84.                         $db->quoteName('old_url'),
  85.                         $db->quoteName('new_url'),
  86.                         $db->quoteName('referer'),
  87.                         $db->quoteName('comment'),
  88.                         $db->quoteName('hits'),
  89.                         $db->quoteName('published'),
  90.                         $db->quoteName('created_date')
  91.                     );
  92.                     $query->clear()
  93.                         ->insert($db->quoteName('#__redirect_links')false)
  94.                         ->columns($columns)
  95.                         ->values(
  96.                             $db->quote($current', ' $db->quote(''.
  97.                                 ' ,' $db->quote($referer', ' $db->quote(''',1,0, ' .
  98.                                 $db->quote(JFactory::getDate()->toSql())
  99.                         );
  100.  
  101.                     $db->setQuery($query);
  102.                     $db->execute();
  103.                 }
  104.                 else
  105.                 {
  106.                     // Existing error url, increase hit counter
  107.                     $query->clear()
  108.                         ->update($db->quoteName('#__redirect_links'))
  109.                         ->set($db->quoteName('hits'' = ' $db->quote('hits'' + 1')
  110.                         ->where('id = ' . (int) $res);
  111.                     $db->setQuery($query);
  112.                     $db->execute();
  113.                 }
  114.                 // Render the error page.
  115.                 JError::customErrorPage($error);
  116.             }
  117.         }
  118.         else
  119.         {
  120.             // Render the error page.
  121.             JError::customErrorPage($error);
  122.         }
  123.     }
  124. }

Documentation generated on Tue, 19 Nov 2013 15:11:38 +0100 by phpDocumentor 1.4.3