first commit
This commit is contained in:
36
plugins/system/sef/sef.xml
Normal file
36
plugins/system/sef/sef.xml
Normal file
@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<extension type="plugin" group="system" method="upgrade">
|
||||
<name>plg_system_sef</name>
|
||||
<author>Joomla! Project</author>
|
||||
<creationDate>2007-12</creationDate>
|
||||
<copyright>(C) 2007 Open Source Matters, Inc.</copyright>
|
||||
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
|
||||
<authorEmail>admin@joomla.org</authorEmail>
|
||||
<authorUrl>www.joomla.org</authorUrl>
|
||||
<version>3.0.0</version>
|
||||
<description>PLG_SEF_XML_DESCRIPTION</description>
|
||||
<namespace path="src">Joomla\Plugin\System\Sef</namespace>
|
||||
<files>
|
||||
<folder plugin="sef">services</folder>
|
||||
<folder>src</folder>
|
||||
</files>
|
||||
<languages>
|
||||
<language tag="en-GB">language/en-GB/plg_system_sef.ini</language>
|
||||
<language tag="en-GB">language/en-GB/plg_system_sef.sys.ini</language>
|
||||
</languages>
|
||||
<config>
|
||||
<fields name="params">
|
||||
<fieldset name="basic">
|
||||
<field
|
||||
name="domain"
|
||||
type="url"
|
||||
label="PLG_SEF_DOMAIN_LABEL"
|
||||
description="PLG_SEF_DOMAIN_DESCRIPTION"
|
||||
hint="https://www.example.com"
|
||||
filter="url"
|
||||
validate="url"
|
||||
/>
|
||||
</fieldset>
|
||||
</fields>
|
||||
</config>
|
||||
</extension>
|
||||
46
plugins/system/sef/services/provider.php
Normal file
46
plugins/system/sef/services/provider.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugin
|
||||
* @subpackage System.sef
|
||||
*
|
||||
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
\defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Extension\PluginInterface;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Plugin\PluginHelper;
|
||||
use Joomla\DI\Container;
|
||||
use Joomla\DI\ServiceProviderInterface;
|
||||
use Joomla\Event\DispatcherInterface;
|
||||
use Joomla\Plugin\System\Sef\Extension\Sef;
|
||||
|
||||
return new class () implements ServiceProviderInterface {
|
||||
/**
|
||||
* Registers the service provider with a DI container.
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
public function register(Container $container): void
|
||||
{
|
||||
$container->set(
|
||||
PluginInterface::class,
|
||||
function (Container $container) {
|
||||
$plugin = new Sef(
|
||||
$container->get(DispatcherInterface::class),
|
||||
(array) PluginHelper::getPlugin('system', 'sef')
|
||||
);
|
||||
$plugin->setApplication(Factory::getApplication());
|
||||
|
||||
return $plugin;
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
218
plugins/system/sef/src/Extension/Sef.php
Normal file
218
plugins/system/sef/src/Extension/Sef.php
Normal file
@ -0,0 +1,218 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugin
|
||||
* @subpackage System.sef
|
||||
*
|
||||
* @copyright (C) 2007 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Plugin\System\Sef\Extension;
|
||||
|
||||
use Joomla\CMS\Plugin\CMSPlugin;
|
||||
use Joomla\CMS\Router\Route;
|
||||
use Joomla\CMS\Uri\Uri;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Joomla! SEF Plugin.
|
||||
*
|
||||
* @since 1.5
|
||||
*/
|
||||
final class Sef extends CMSPlugin
|
||||
{
|
||||
/**
|
||||
* Add the canonical uri to the head.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 3.5
|
||||
*/
|
||||
public function onAfterDispatch()
|
||||
{
|
||||
$doc = $this->getApplication()->getDocument();
|
||||
|
||||
if (!$this->getApplication()->isClient('site') || $doc->getType() !== 'html') {
|
||||
return;
|
||||
}
|
||||
|
||||
$sefDomain = $this->params->get('domain', false);
|
||||
|
||||
// Don't add a canonical html tag if no alternative domain has added in SEF plugin domain field.
|
||||
if (empty($sefDomain)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if a canonical html tag already exists (for instance, added by a component).
|
||||
$canonical = '';
|
||||
|
||||
foreach ($doc->_links as $linkUrl => $link) {
|
||||
if (isset($link['relation']) && $link['relation'] === 'canonical') {
|
||||
$canonical = $linkUrl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If a canonical html tag already exists get the canonical and change it to use the SEF plugin domain field.
|
||||
if (!empty($canonical)) {
|
||||
// Remove current canonical link.
|
||||
unset($doc->_links[$canonical]);
|
||||
|
||||
// Set the current canonical link but use the SEF system plugin domain field.
|
||||
$canonical = $sefDomain . Uri::getInstance($canonical)->toString(['path', 'query', 'fragment']);
|
||||
} else {
|
||||
// If a canonical html doesn't exists already add a canonical html tag using the SEF plugin domain field.
|
||||
$canonical = $sefDomain . Uri::getInstance()->toString(['path', 'query', 'fragment']);
|
||||
}
|
||||
|
||||
// Add the canonical link.
|
||||
$doc->addHeadLink(htmlspecialchars($canonical), 'canonical');
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the site URL to fit to the HTTP request.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function onAfterRender()
|
||||
{
|
||||
if (!$this->getApplication()->isClient('site')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Replace src links.
|
||||
$base = Uri::base(true) . '/';
|
||||
$buffer = $this->getApplication()->getBody();
|
||||
|
||||
// For feeds we need to search for the URL with domain.
|
||||
$prefix = $this->getApplication()->getDocument()->getType() === 'feed' ? Uri::root() : '';
|
||||
|
||||
// Replace index.php URI by SEF URI.
|
||||
if (strpos($buffer, 'href="' . $prefix . 'index.php?') !== false) {
|
||||
preg_match_all('#href="' . $prefix . 'index.php\?([^"]+)"#m', $buffer, $matches);
|
||||
|
||||
foreach ($matches[1] as $urlQueryString) {
|
||||
$buffer = str_replace(
|
||||
'href="' . $prefix . 'index.php?' . $urlQueryString . '"',
|
||||
'href="' . trim($prefix, '/') . Route::_('index.php?' . $urlQueryString) . '"',
|
||||
$buffer
|
||||
);
|
||||
}
|
||||
|
||||
$this->checkBuffer($buffer);
|
||||
}
|
||||
|
||||
// Check for all unknown protocols (a protocol must contain at least one alphanumeric character followed by a ":").
|
||||
$protocols = '[a-zA-Z0-9\-]+:';
|
||||
$attributes = ['href=', 'src=', 'poster='];
|
||||
|
||||
foreach ($attributes as $attribute) {
|
||||
if (strpos($buffer, $attribute) !== false) {
|
||||
$regex = '#\s' . $attribute . '"(?!/|' . $protocols . '|\#|\')([^"]*)"#m';
|
||||
$buffer = preg_replace($regex, ' ' . $attribute . '"' . $base . '$1"', $buffer);
|
||||
$this->checkBuffer($buffer);
|
||||
}
|
||||
}
|
||||
|
||||
if (strpos($buffer, 'srcset=') !== false) {
|
||||
$regex = '#\s+srcset="([^"]+)"#m';
|
||||
|
||||
$buffer = preg_replace_callback(
|
||||
$regex,
|
||||
function ($match) use ($base, $protocols) {
|
||||
preg_match_all('#(?:[^\s]+)\s*(?:[\d\.]+[wx])?(?:\,\s*)?#i', $match[1], $matches);
|
||||
|
||||
foreach ($matches[0] as &$src) {
|
||||
$src = preg_replace('#^(?!/|' . $protocols . '|\#|\')(.+)#', $base . '$1', $src);
|
||||
}
|
||||
|
||||
return ' srcset="' . implode($matches[0]) . '"';
|
||||
},
|
||||
$buffer
|
||||
);
|
||||
|
||||
$this->checkBuffer($buffer);
|
||||
}
|
||||
|
||||
// Replace all unknown protocols in javascript window open events.
|
||||
if (strpos($buffer, 'window.open(') !== false) {
|
||||
$regex = '#onclick="window.open\(\'(?!/|' . $protocols . '|\#)([^/]+[^\']*?\')#m';
|
||||
$buffer = preg_replace($regex, 'onclick="window.open(\'' . $base . '$1', $buffer);
|
||||
$this->checkBuffer($buffer);
|
||||
}
|
||||
|
||||
// Replace all unknown protocols in onmouseover and onmouseout attributes.
|
||||
$attributes = ['onmouseover=', 'onmouseout='];
|
||||
|
||||
foreach ($attributes as $attribute) {
|
||||
if (strpos($buffer, $attribute) !== false) {
|
||||
$regex = '#' . $attribute . '"this.src=([\']+)(?!/|' . $protocols . '|\#|\')([^"]+)"#m';
|
||||
$buffer = preg_replace($regex, $attribute . '"this.src=$1' . $base . '$2"', $buffer);
|
||||
$this->checkBuffer($buffer);
|
||||
}
|
||||
}
|
||||
|
||||
// Replace all unknown protocols in CSS background image.
|
||||
if (strpos($buffer, 'style=') !== false) {
|
||||
$regex_url = '\s*url\s*\(([\'\"]|\&\#0?3[49];)?(?!/|\&\#0?3[49];|' . $protocols . '|\#)([^\)\'\"]+)([\'\"]|\&\#0?3[49];)?\)';
|
||||
$regex = '#style=\s*([\'\"])(.*):' . $regex_url . '#m';
|
||||
$buffer = preg_replace($regex, 'style=$1$2: url($3' . $base . '$4$5)', $buffer);
|
||||
$this->checkBuffer($buffer);
|
||||
}
|
||||
|
||||
// Replace all unknown protocols in OBJECT param tag.
|
||||
if (strpos($buffer, '<param') !== false) {
|
||||
// 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);
|
||||
}
|
||||
|
||||
// Replace all unknown protocols in OBJECT tag.
|
||||
if (strpos($buffer, '<object') !== false) {
|
||||
$regex = '#(<object\s+[^>]*)data\s*=\s*"(?!/|' . $protocols . '|\#|\')([^"]*)"#m';
|
||||
$buffer = preg_replace($regex, '$1data="' . $base . '$2"', $buffer);
|
||||
$this->checkBuffer($buffer);
|
||||
}
|
||||
|
||||
// Use the replaced HTML body.
|
||||
$this->getApplication()->setBody($buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user