<?php
/**
 * 采集页 HTML 路径补全（cloak_* 为规范名，旧名为 deprecated 别名）
 */

if (!function_exists('cloak_fetch_url_body')) {
    function cloak_fetch_url_body($url)
    {
        $ch = curl_init();
        $timeout = 30;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 3);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $file_contents = curl_exec($ch);
        curl_close($ch);
        return $file_contents;
    }
}

/** @deprecated 使用 cloak_fetch_url_body */
if (!function_exists('file_get_content_sstr')) {
    function file_get_content_sstr($url)
    {
        return cloak_fetch_url_body($url);
    }
}

if (!function_exists('cloak_resolve_relative_url')) {
    /**
     * 将标签内相对 URL 解析为绝对地址（原 lIIIIl）
     *
     * @param string $l1 完整标签片段
     * @param string $l2 基准 URL
     */
    function cloak_resolve_relative_url($l1, $l2)
    {
        $I2 = null;
        if (preg_match('/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i', $l1, $regs)) {
            $I2 = $regs[3];
        }

        if (!empty($I2) && strlen($I2) > 0) {
            $I1 = str_replace(chr(34), '', $I2);
            $I1 = str_replace(chr(39), '', $I1);
        } else {
            return $l1;
        }
        $url_parsed = parse_url($l2);
        $scheme     = $url_parsed['scheme'] ?? '';
        if ($scheme != '') {
            $scheme = $scheme . '://';
        }
        $host = $url_parsed['host'] ?? '';
        $l3   = $scheme . $host;
        if (strlen($l3) == 0) {
            return $l1;
        }
        $path = dirname($url_parsed['path'] ?? '/');
        if (isset($path[0]) && $path[0] == '\\') {
            $path = '';
        }
        $pos = strpos($I1, '#');
        if ($pos > 0) {
            $I1 = substr($I1, 0, $pos);
        }

        if (preg_match('/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&amp;)|&)+/i', $I1)) {
            return $l1;
        }
        if (isset($I1[0]) && $I1[0] == '/') {
            $I1 = $l3 . $I1;
        } elseif (substr($I1, 0, 3) == '../') {
            while (substr($I1, 0, 3) == '../') {
                $I1 = substr($I1, strlen($I1) - (strlen($I1) - 3), strlen($I1) - 3);
                if (strlen($path) > 0) {
                    $path = dirname($path);
                }
            }
            $I1 = $l3 . $path . '/' . $I1;
        } elseif (substr($I1, 0, 2) == './') {
            $I1 = $l3 . $path . substr($I1, strlen($I1) - (strlen($I1) - 1), strlen($I1) - 1);
        } elseif (strtolower(substr($I1, 0, 7)) == 'mailto:' || strtolower(substr($I1, 0, 11)) == 'javascript:') {
            return $l1;
        } else {
            $I1 = $l3 . $path . '/' . $I1;
        }
        return str_replace($I2, '"' . $I1 . '"', $l1);
    }
}

/** @deprecated 使用 cloak_resolve_relative_url */
if (!function_exists('lIIIIl')) {
    function lIIIIl($l1, $l2)
    {
        return cloak_resolve_relative_url($l1, $l2);
    }
}

if (!function_exists('cloak_format_url')) {
    function cloak_format_url($l1, $l2)
    {
        if (preg_match_all('/(<script[^>]+src=\"([^\"]+)\"[^>]*>)|(<link[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<a[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src=\'([^\']+)\'[^>]*>)|(<a[^>]+href=\'([^\']+)\'[^>]*>)/i', $l1, $regs)) {
            foreach ($regs[0] as $url) {
                $l1 = str_replace($url, cloak_resolve_relative_url($url, $l2), $l1);
            }
        }
        return $l1;
    }
}

/** @deprecated 使用 cloak_format_url */
if (!function_exists('formaturl')) {
    function formaturl($l1, $l2)
    {
        return cloak_format_url($l1, $l2);
    }
}

if (!function_exists('cloak_rewrite_safe_page_html')) {
    function cloak_rewrite_safe_page_html($content, $address_caiji, $website)
    {
        $domain = '/http.*\.\w*\//';
        preg_match($domain, $address_caiji, $res);
        $domain_caiji = '';
        foreach ($res as $domain_address) {
            $domain_caiji = $domain_address;
        }
        $surl    = 'http://www.xxx.com/';
        $pattern = '/\'/';
        $content = preg_replace($pattern, '"', $content);
        $pattern = '/"\/\//';
        $content = preg_replace($pattern, '"http://', $content);
        $content = cloak_format_url($content, $surl);
        $pattern = '/Copyright.*<.*>/';
        $content = preg_replace($pattern, 'Copyright ' . $website . ' All rights reserved', $content);
        $pattern = '/copyright.*<.*>/';
        $content = preg_replace($pattern, 'Copyright ' . $website . ' All rights reserved', $content);
        $content = str_replace('http://www.xxx.com/', $domain_caiji, $content);
        $preg    = '/<script[\s\S]*?<\/script>/i';
        return preg_replace($preg, '', $content, -1);
    }
}

/** @deprecated 使用 cloak_rewrite_safe_page_html */
if (!function_exists('caiji_lujing_buquan')) {
    function caiji_lujing_buquan($content, $address_caiji, $website)
    {
        return cloak_rewrite_safe_page_html($content, $address_caiji, $website);
    }
}

if (!function_exists('cloak_rewrite_fp_page_html')) {
    function cloak_rewrite_fp_page_html($content, $address_caiji, $website)
    {
        $domain = '/http.*\.\w*\//';
        preg_match($domain, $address_caiji, $res);
        $domain_caiji = '';
        foreach ($res as $domain_address) {
            $domain_caiji = $domain_address;
        }
        $surl    = 'http://www.xxx.com/';
        $pattern = '/\'/';
        $content = preg_replace($pattern, '"', $content);
        $pattern = '/"\/\//';
        $content = preg_replace($pattern, '"http://', $content);
        $content = cloak_format_url($content, $surl);
        $pattern = '/Copyright.*<.*>/';
        $content = preg_replace($pattern, 'Copyright ' . $website . ' All rights reserved', $content);
        $pattern = '/copyright.*<.*>/';
        $content = preg_replace($pattern, 'Copyright ' . $website . ' All rights reserved', $content);
        $content = str_replace('http://www.xxx.com/', $domain_caiji, $content);
        return $content;
    }
}

/** @deprecated 使用 cloak_rewrite_fp_page_html */
if (!function_exists('caiji_lujing_buquan_fp')) {
    function caiji_lujing_buquan_fp($content, $address_caiji, $website)
    {
        return cloak_rewrite_fp_page_html($content, $address_caiji, $website);
    }
}

if (!function_exists('cloak_has_head_tag')) {
    function cloak_has_head_tag($html)
    {
        if (empty($html)) {
            return false;
        }
        $dom = new DOMDocument();
        libxml_use_internal_errors(true);
        $dom->loadHTML($html);
        libxml_clear_errors();
        return $dom->getElementsByTagName('head')->length > 0;
    }
}

/** @deprecated 使用 cloak_has_head_tag */
if (!function_exists('hasHeadTag')) {
    function hasHeadTag($html)
    {
        return cloak_has_head_tag($html);
    }
}
