/redirect/code/trunk/plugins/system/redirect.php
PHP | 95 lines | 50 code | 9 blank | 36 comment | 11 complexity | c12d04aa7fdf8838711bdb9722322a2e MD5 | raw file
- <?php
- /**
- * @version $Id: redirect.php 580 2011-02-25 05:11:47Z eddieajau $
- * @package NewLifeInIT
- * @subpackage plgSystemRedirect
- * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
- * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
- * @link http://www.theartofjoomla.com
- */
- defined('JPATH_BASE') or die;
- /**
- * Plugin class for Redirect.
- *
- * @package NewLifeInIT
- * @subpackage plgSystemRedirect
- * @since 1.0
- */
- class plgSystemRedirect extends JPlugin
- {
- /**
- * Method to handle hard errors and catch 404 not found errors.
- *
- * @param object JException object.
- * @return void
- * @since 1.0
- */
- public static function handleError(&$error)
- {
- // Get the application object.
- $app = JFactory::getApplication();
- // Only apply special error handling if the error is a 404 and we aren't in the admin.
- if (!$app->isAdmin() and ($error->code == 404)) {
- // Get the current requested URI.
- $currentURI = JURI::getInstance()->toString(array('scheme', 'host', 'port', 'path', 'query', 'fragment'));
- // Attempt to ignore idiots.
- if ((strpos($currentURI, 'mosConfig_') !== false) || (strpos($currentURI, '=http://') !== false)) {
- JError::customErrorPage($error);
- }
- // Get the database connection object.
- $db = JFactory::getDBO();
- // Check to see if a redirect is set for the current URI.
- $db->setQuery(
- 'SELECT `id`, `new_url`, `published`' .
- ' FROM `#__redirect_links`' .
- ' WHERE `old_url` = '.$db->quote($currentURI),
- 0, 1
- );
- $link = $db->loadObject();
- // If the redirect exist and is published redirect the browser.
- if ($link && ($link->published == 1)) {
- $app->redirect($link->new_url, null, null, true);
- }
- // Render the Joomla! error page and add the URI to the database if necessary.
- else {
- // Add the current URI to the database if not present.
- if (empty($link)) {
- $now = JFactory::getDate();
- $referer = @$_SERVER['HTTP_REFERER'];
- $db->setQuery(
- 'INSERT INTO `#__redirect_links` (`old_url`, `referer`, `published`, `created_date`, `hits`)' .
- ' VALUES ('.$db->quote($currentURI).', '.$db->quote($referer).', 0, '.$db->quote($now->toMySQL()).', 1)'
- );
- $db->query();
- }
- // Increase the hit counter if it is present.
- else {
- $db->setQuery(
- 'UPDATE `#__redirect_links`' .
- ' SET `hits` = `hits` + 1' .
- ' WHERE `id` = '.(int) $link->id
- );
- $db->query();
- }
- // Render the Joomla! error page.
- JError::customErrorPage($error);
- }
- }
- // We are either in the administrator application or have a non-404 error.
- else {
- // Render the Joomla! error page.
- JError::customErrorPage($error);
- }
- }
- }
- // Reset the hard error handling to use the plugin callback.
- JError::setErrorHandling(E_ERROR, 'callback', array('plgSystemRedirect', 'handleError'));