<?php
/**
 * Processes an HTML string to inline all linked stylesheets by replacing <link> tags
 * with corresponding <style> tags containing the CSS content.
 * Might be broken, currently disabled in the config
 *
 * @param string $inputString The HTML content containing <link> tags to stylesheets.
 * @return string The modified HTML content with stylesheets inlined within <style> tags.
 */
function inlineLocalStylesFromHref(string $inputString): string
{
    $pattern = '/<link[^>]*?\srel=["\']?stylesheet["\'].*?\shref=["\']?\/(.*?)["\'][^>]*?>/i';

    return preg_replace_callback($pattern, function($match) {
        $href = $match[1];
        $cssFilePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $href;
        $cssContent = file_get_contents($cssFilePath);
        $cssDir = dirname($cssFilePath);

        $cssContent = preg_replace_callback('/url\(["\']?(\/.*?|.*?)["\']?\)/i', function($urlMatch) use ($cssDir) {
            $url = $urlMatch[1];

            // Check if the URL starts with any protocol or //
            if (!preg_match('/^([a-zA-Z]+:)?\/\//', $url)) {
                $absolutePath = $cssDir . '/' . $url;
                $relativePath = ltrim(substr($absolutePath, strlen($_SERVER['DOCUMENT_ROOT'])), '/');
                return 'url("' . $relativePath . '")';
            } else {
                // If the URL starts with a protocol or //, leave it unchanged
                return 'url("' . $url . '")';
            }
        }, $cssContent);

        // Minify the CSS content
        $cssContent = minifyCss($cssContent);

        return "<style>$cssContent</style>";
    }, $inputString);
}
/**
 * Processes an HTML string to inline all external JavaScript files by replacing <script src="..."> tags
 * with <script> tags containing the JavaScript content.
 * Might be broken, currently disabled in the config
 *
 * @param string $inputString The HTML content containing <script src=""> tags.
 * @return string The modified HTML content with external scripts inlined within <script> tags.
 */
function inlineScriptFromSrc(string $inputString): string
{
    $pattern = '/<script.*?src=["\']\/(.*?)["\'].*?>\s*<\/script>/i';

    return preg_replace_callback($pattern, function($match) {
        $src = $match[1];
        $jsContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $src);

        // Minify the JavaScript content
        $jsContent = minifyJs($jsContent);
        return "<script>$jsContent</script>";
    }, $inputString);
}
/**
 * Minifies CSS content by removing comments, unnecessary whitespaces, semicolons, and optimizing other aspects of the stylesheet.
 * Might be broken, currently disabled in the config
 *
 * @param string $css The original CSS content.
 * @return string The minified CSS content.
 */
function minifyCss(string $css): string
{
    // 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);
}
/**
 * Minifies JavaScript content by removing comments, unnecessary whitespaces, and optimizing spaces around operators.
 * Might be broken, currently disabled in the config
 *
 * @param string $js The original JavaScript content.
 * @return string The minified JavaScript content.
 */
function minifyJs(string $js): string
{

    // Remove newlines and tabs
    $js = str_replace("\t", '', $js);

    // Remove spaces around operators
    $js = preg_replace('~\s*([=+\-*/])\s*~', '$1', $js);

    return trim($js);
}