- md5
- 74430becaa9e630b84b83001c506ca91
- sha1
- bc6b3f2eed2973941c27868f485be8d53fe244f5
- sha256
- 24978ac7c8a610723fe051b786b2e574f69af6219ab396481de11776a5616f06
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
<?php
/**
* @package Joomla.Plugin
* @subpackage System.sef
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* Joomla! SEF Plugin.
*
* @since 1.5
*/
class PlgSystemSef extends JPlugin
{
/**
* Add the canonical uri to the head.
*
* @return void
*
* @since 3.0
*/
public function onAfterRoute()
{
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
if ($app->getName() != 'site' || $doc->getType() !== 'html')
{
return;
}
$router = $app::getRouter();
$uri = JUri::getInstance();
$domain = $this->params->get('domain');
if ($domain === null || $domain === '')
{
$domain = $uri->toString(array('scheme', 'host', 'port'));
}
$link = $domain . JRoute::_('index.php?' . http_build_query($router->getVars()), false);
if ($uri->toString() !== $link)
{
$doc->addHeadLink(htmlspecialchars($link), 'canonical');
}
}
/**
* Convert the site URL to fit to the HTTP request.
*
* @return void
*/
public function onAfterRender()
{
$app = JFactory::getApplication();
if ($app->getName() != 'site' || $app->get('sef') == '0')
{
return true;
}
// Replace src links.
$base = JUri::base(true) . '/';
$buffer = $app->getBody();
$regex = '#href="index.php\?([^"]*)#m';
$buffer = preg_replace_callback($regex, array('PlgSystemSef', 'route'), $buffer);
$this->checkBuffer($buffer);
// Check for all unknown protocals (a protocol must contain at least one alpahnumeric character followed by a ":").
$protocols = '[a-zA-Z0-9\-]+:';
$regex = '#\s+(src|href|poster)="(?!/|' . $protocols . '|\#|\')([^"]*)"#m';
$buffer = preg_replace($regex, " $1=\"$base\$2\"", $buffer);
$this->checkBuffer($buffer);
$regex = '#(onclick="window.open\(\')(?!/|' . $protocols . '|\#)([^/]+[^\']*?\')#m';
$buffer = preg_replace($regex, '$1' . $base . '$2', $buffer);
$this->checkBuffer($buffer);
// ONMOUSEOVER / ONMOUSEOUT
$regex = '#(onmouseover|onmouseout)="this.src=([\']+)(?!/|' . $protocols . '|\#|\')([^"]+)"#m';
$buffer = preg_replace($regex, '$1="this.src=$2' . $base . '$3$4"', $buffer);
$this->checkBuffer($buffer);
// Background image.
$regex = '#style\s*=\s*[\'\"](.*):\s*url\s*\([\'\"]?(?!/|' . $protocols . '|\#)([^\)\'\"]+)[\'\"]?\)#m';
$buffer = preg_replace($regex, 'style="$1: url(\'' . $base . '$2$3\')', $buffer);
$this->checkBuffer($buffer);
// OBJECT <param name="xx", value="yy"> -- fix it only inside the <param> tag.
$regex = '#(<param\s+)name\s*=\s*"(movie|src|url)"[^>]\s*value\s*=\s*"(?!/|' . $protocols . '|\#|\')([^"]*)"#m';
$buffer = preg_replace($regex, '$1name="$2" value="' . $base . '$3"', $buffer);
$this->checkBuffer($buffer);
// OBJECT <param value="xx", name="yy"> -- fix it only inside the <param> tag.
$regex = '#(<param\s+[^>]*)value\s*=\s*"(?!/|' . $protocols . '|\#|\')([^"]*)"\s*name\s*=\s*"(movie|src|url)"#m';
$buffer = preg_replace($regex, '<param value="' . $base . '$2" name="$3"', $buffer);
$this->checkBuffer($buffer);
// OBJECT data="xx" attribute -- fix it only in the object tag.
$regex = '#(<object\s+[^>]*)data\s*=\s*"(?!/|' . $protocols . '|\#|\')([^"]*)"#m';
$buffer = preg_replace($regex, '$1data="' . $base . '$2"$3', $buffer);
$this->checkBuffer($buffer);
$app->setBody($buffer);
return true;
}
/**
* Check the buffer.
*
* @param string $buffer Buffer to be checked.
*
* @return void
*/
private function checkBuffer($buffer)
{
if ($buffer === null)
{
switch (preg_last_error())
{
case PREG_BACKTRACK_LIMIT_ERROR:
$message = "PHP regular expression limit reached (pcre.backtrack_limit)";
break;
case PREG_RECURSION_LIMIT_ERROR:
$message = "PHP regular expression limit reached (pcre.recursion_limit)";
break;
case PREG_BAD_UTF8_ERROR:
$message = "Bad UTF8 passed to PCRE function";
break;
default:
$message = "Unknown PCRE error calling PCRE function";
}
throw new RuntimeException($message);
}
}
/**
* Replace the matched tags.
*
* @param array &$matches An array of matches (see preg_match_all).
*
* @return string
*/
protected static function route(&$matches)
{
$url = $matches[1];
$url = str_replace('&', '&', $url);
$route = JRoute::_('index.php?' . $url);
return 'href="' . $route;
}
}
The same content is in 12 releases, Joomla 3.4.2-rc to 3.5.0-beta. See them all, or every version of this file.