adlerka.top/lib/inliner.php

81 lines
2.4 KiB
PHP
Raw Normal View History

2024-02-04 10:21:56 +01:00
<?php
2024-02-04 10:37:06 +01:00
2024-02-04 10:21:56 +01:00
function inlineLocalStylesFromHref($inputString) {
2024-02-04 10:37:06 +01:00
$pattern = '/<link[^>]*?\srel=["\']?stylesheet["\'].*?\shref=["\']?\/(.*?)["\'][^>]*?>/i';
2024-02-04 10:21:56 +01:00
2024-02-04 10:24:52 +01:00
$outputString = preg_replace_callback($pattern, function($match) {
2024-02-04 10:37:06 +01:00
$href = $match[1];
2024-02-04 10:42:36 +01:00
$cssFilePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $href;
$cssContent = file_get_contents($cssFilePath);
$cssDir = dirname($cssFilePath);
2024-02-04 10:21:56 +01:00
2024-02-04 10:42:36 +01:00
$cssContent = preg_replace_callback('/url\(["\']?(\/.*?|.*?)["\']?\)/i', function($urlMatch) use ($cssDir) {
2024-02-04 10:32:08 +01:00
$url = $urlMatch[1];
2024-02-04 10:42:36 +01:00
$absolutePath = $cssDir . '/' . $url;
$relativePath = ltrim(substr($absolutePath, strlen($_SERVER['DOCUMENT_ROOT'])), '/');
2024-02-04 10:45:44 +01:00
return 'url("' . $relativePath . '")';
2024-02-04 10:32:08 +01:00
}, $cssContent);
2024-02-04 10:45:44 +01:00
// Minify the CSS content
$cssContent = minifyCss($cssContent);
return "<style>{$cssContent}</style>";
2024-02-04 10:21:56 +01:00
}, $inputString);
return $outputString;
}
function inlineScriptFromSrc($inputString) {
2024-02-04 10:24:52 +01:00
$pattern = '/<script.*?src=["\']\/(.*?)["\'].*?>\s*<\/script>/i';
2024-02-04 10:21:56 +01:00
2024-02-04 10:24:52 +01:00
$outputString = preg_replace_callback($pattern, function($match) {
2024-02-04 10:21:56 +01:00
$src = $match[1];
2024-02-04 10:24:52 +01:00
$jsContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $src);
2024-02-04 10:21:56 +01:00
2024-02-04 10:45:44 +01:00
// Minify the JavaScript content
$jsContent = minifyJs($jsContent);
2024-02-04 10:21:56 +01:00
2024-02-04 10:45:44 +01:00
$escapedJsContent = htmlspecialchars($jsContent, ENT_QUOTES, 'UTF-8');
return "<script>{$escapedJsContent}</script>";
2024-02-04 10:21:56 +01:00
}, $inputString);
return $outputString;
2024-02-04 10:45:44 +01:00
}
function minifyCss($css) {
// Remove comments
$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
// Remove whitespace
$css = preg_replace('/\s+/', ' ', $css);
// Remove unnecessary semicolons
$css = str_replace(';}', '}', $css);
// Remove spaces around colons
$css = str_replace(': ', ':', $css);
// Remove spaces around commas
$css = str_replace(', ', ',', $css);
return trim($css);
}
function minifyJs($js) {
// Remove comments
$js = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $js);
// Remove newlines and tabs
$js = str_replace(array("\r\n", "\r", "\n", "\t"), '', $js);
// Remove unnecessary semicolons
$js = str_replace(';}', '}', $js);
// Remove spaces after function keyword
$js = str_replace('function ', 'function', $js);
// Remove spaces around operators
$js = preg_replace('/\s*([=+\-*/])\s*/', '$1', $js);
return trim($js);
2024-02-04 10:21:56 +01:00
}