| Server IP : 46.105.57.169 / Your IP : 216.73.216.144 Web Server : Apache System : Linux webd003.cluster120.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64 User : maitricfuz ( 93378) PHP Version : 8.4.10 Disable Function : _dyuweyrj4,_dyuweyrj4r,dl MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/maitricfuz/www/saint-martin-lg/plugins/system/jtaldef/src/Service/ |
Upload File : |
<?php
/**
* Automatic local download external files
*
* @package Joomla.Plugin
* @subpackage System.Jtaldef
*
* @author Guido De Gobbis <support@joomtools.de>
* @copyright JoomTools.de - All rights reserved.
* @license GNU General Public License version 3 or later
*/
namespace JoomTools\Plugin\System\Jtaldef\Service;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
use JoomTools\Plugin\System\Jtaldef\Helper\JtaldefHelper;
/**
* Download and save external fonts like Google Fonts
*
* @since 2.0.0
*/
class ParseCss
{
/**
* Description
*
* @param string $value Link to content or the content itself to parse.
* @param boolean $isPath True if it is a path to a local file or false for content like <style/>.
*
* @return boolean|string False if no font info is set in the query else the local path to the css file.
* @throws \Exception
*
* @since 2.0.0
*/
public function getNewFileContent($value, $isPath = true)
{
$file = null;
$content = $value;
if ($isPath) {
$file = JPATH_ROOT . '/' . $value;
if (file_exists($file)) {
$content = file_get_contents($file);
}
if (empty($content) || !is_string($content)) {
return false;
}
}
$content = JtaldefHelper::cleanContent($content);
$matches = $this->getFontImports($content);
if (empty($matches) || $matches['onlyInternal']) {
return false;
}
unset($matches['onlyInternal']);
foreach ($matches as $imports) {
$localFontUrl = false;
$service = JtaldefHelper::getServiceByLink($imports['fontUrl']);
if ($service) {
$localFontUrl = JtaldefHelper::getNewFileContentLink($imports['fontUrl'], $service);
}
if (!$service) {
$localFontUrl = JtaldefHelper::replaceRelativeToAbsolutePath($imports['fontUrl'], $file);
}
if ($localFontUrl !== false) {
$newImport = "@import '" . $localFontUrl . "';";
$imports['search'] = array_unique($imports['search'], SORT_REGULAR);
$content = $newImport . PHP_EOL . str_replace($imports['search'], '', $content);
}
}
$content = $this->replaceRelativePath($content, $file);
return $content;
}
/**
* Search for font-face blocks to parse local relative paths
*
* @param string $content The content to be scanned
* @param string $file Absolute path to the actual file
*
* @return string
*
* @since 2.0.0
*/
public function replaceRelativePath($content, $file)
{
$replacements = [];
// Check for CSS with images to replace the path with the new one
if (preg_match_all('#url\(([^)]+?)\)#Us', $content, $paths, PREG_SET_ORDER)) {
foreach ($paths as $path) {
$regex = ['"', "'"];
$path[1] = trim(str_replace($regex, '', $path[1]));
if (empty($path[1]) || JtaldefHelper::isExternalUrl($path[1])) {
continue;
}
$newPath = JtaldefHelper::replaceRelativeToAbsolutePath($path[1], $file);
if (false === $newPath) {
continue;
}
$replacements['search'][md5($path[1])] = $path[1];
$replacements['replace'][md5($path[1])] = $newPath;
}
if (!empty($replacements)) {
$content = str_replace($replacements['search'], $replacements['replace'], $content);
}
}
return $content;
}
/**
* Search for fonts imports
*
* @param string $content The content to be scanned
*
* @return array
*
* @since 2.0.0
*/
public function getFontImports($content)
{
$return = [];
$onlyInternal = true;
// Check for Google Font imports - benchmarked regex
if (preg_match_all('#(?<match>@import\s+(?:[^;]+?)?((?:url\()?([("\'])(?<url>[^)("\']+?)(?:\)|\\3\)?));)#Um', $content, $imports, PREG_SET_ORDER)) {
foreach ($imports as $match) {
$fontUrl = str_replace(['url(', ')', '"', "'"], '', $match['url']);
$fontUrl = trim($fontUrl);
$fontUrlId = md5($fontUrl);
$fontUrl = JtaldefHelper::normalizeUrl($fontUrl);
$return[$fontUrlId]['fontUrl'] = $fontUrl;
$return[$fontUrlId]['search'][] = $match['match'];
if (JtaldefHelper::isExternalUrl($fontUrl)) {
$onlyInternal = false;
}
}
$return['onlyInternal'] = $onlyInternal;
}
return $return;
}
}