<?php

defined('ABSPATH') || exit;
function rocket_clean_exclude_file($file)
{
    if (!$file) {
        return false;
    }
    return wp_parse_url($file, PHP_URL_PATH);
}
function rocket_clean_wildcards($path)
{
    if (!$path) {
        return '';
    }
    $path_components = explode('/', $path);
    $arr = ['.*' => '(.*)', '*' => '(.*)', '(*)' => '(.*)', '(.*)' => '(.*)'];
    foreach ($path_components as &$path_component) {
        $path_component = strtr($path_component, $arr);
    }
    $path = implode('/', $path_components);
    return $path;
}
function rocket_sanitize_css($file)
{
    $file = preg_replace('#\\?.*$#', '', $file);
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    return 'css' === $ext || 'php' === $ext ? trim($file) : false;
}
function rocket_sanitize_js($file)
{
    $file = preg_replace('#\\?.*$#', '', $file);
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    return 'js' === $ext || 'php' === $ext ? trim($file) : false;
}
function rocket_validate_js($file)
{
    if (rocket_is_internal_file($file)) {
        $file = trim($file);
        $file = rocket_clean_exclude_file($file);
        $file = rocket_sanitize_js($file);
        return $file;
    }
    return rocket_remove_url_protocol(esc_url_raw(strtok($file, '?')));
}
function rocket_validate_css($file)
{
    if (rocket_is_internal_file($file)) {
        return rocket_sanitize_css(rocket_clean_exclude_file(trim($file)));
    }
    return rocket_remove_url_protocol(esc_url_raw(strtok($file, '?')));
}
function rocket_is_internal_file($file)
{
    $file_host = wp_parse_url(rocket_add_url_protocol($file), PHP_URL_HOST);
    if (empty($file_host)) {
        return false;
    }
    $hosts = apply_filters('rocket_cdn_hosts', [], ['all', 'css_and_js', 'css', 'js']);
    $hosts[] = wp_parse_url(content_url(), PHP_URL_HOST);
    $langs = get_rocket_i18n_uri();
    if (!empty($langs)) {
        foreach ($langs as $lang) {
            $hosts[] = wp_parse_url($lang, PHP_URL_HOST);
        }
    }
    $hosts = array_unique($hosts);
    if (empty($hosts)) {
        return false;
    }
    return in_array($file_host, $hosts, true);
}
function rocket_sanitize_textarea_field($field, $value)
{
    $fields = ['cache_purge_pages' => ['esc_url', 'rocket_clean_exclude_file', 'rocket_clean_wildcards'], 'cache_reject_cookies' => ['rocket_sanitize_key'], 'cache_reject_ua' => ['rocket_sanitize_ua', 'rocket_clean_wildcards'], 'cache_reject_uri' => ['esc_url', 'rocket_clean_exclude_file', 'rocket_clean_wildcards'], 'cache_query_strings' => ['rocket_sanitize_key'], 'cdn_reject_files' => ['rocket_clean_exclude_file', 'rocket_clean_wildcards'], 'exclude_css' => ['rocket_validate_css', 'rocket_clean_wildcards'], 'exclude_inline_js' => ['sanitize_text_field'], 'exclude_defer_js' => ['sanitize_text_field'], 'exclude_js' => ['rocket_validate_js', 'rocket_clean_wildcards'], 'exclude_lazyload' => ['sanitize_text_field'], 'delay_js_exclusions' => ['sanitize_text_field', 'rocket_clean_wildcards'], 'remove_unused_css_safelist' => ['sanitize_text_field', 'rocket_clean_wildcards']];
    if (!isset($fields[$field])) {
        return null;
    }
    $sanitizations = $fields[$field];
    if (!is_array($value)) {
        $value = explode("\n", $value);
    }
    $value = array_map('trim', $value);
    $value = array_filter($value);
    if (!$value) {
        return [];
    }
    foreach ($sanitizations as $sanitization) {
        $value = array_filter(array_map($sanitization, $value));
    }
    return array_unique($value);
}
function rocket_sanitize_xml($file)
{
    $file = preg_replace('#\\?.*$#', '', $file);
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    return 'xml' === $ext ? trim($file) : false;
}
function rocket_sanitize_key($key)
{
    $key = preg_replace('/[^a-z0-9_\\-]/i', '', $key);
    return $key;
}
function rocket_sanitize_ua($user_agent)
{
    $user_agent = preg_replace('/[^a-z0-9._\\(\\)\\*\\-\\/\\s\\x5c]/i', '', $user_agent);
    return $user_agent;
}
function rocket_remove_url_protocol($url, $no_dots = false)
{
    $url = preg_replace('#^(https?:)?\\/\\/#im', '', $url);
    if (apply_filters('rocket_url_no_dots', $no_dots)) {
        $url = str_replace('.', '_', $url);
    }
    return $url;
}
function rocket_add_url_protocol($url)
{
    if (strpos($url, 'http://') !== false || strpos($url, 'https://') !== false) {
        return $url;
    }
    if (substr($url, 0, 2) !== '//') {
        $url = '//' . $url;
    }
    return set_url_scheme($url);
}
function rocket_set_internal_url_scheme($url)
{
    $tmp_url = set_url_scheme($url);
    if (rocket_extract_url_component($tmp_url, PHP_URL_HOST) === rocket_extract_url_component(home_url(), PHP_URL_HOST)) {
        $url = $tmp_url;
    }
    return $url;
}
function rocket_get_domain($url)
{
    $url = rocket_add_url_protocol(trim($url));
    $url_array = wp_parse_url($url);
    $host = $url_array['host'];
    $match = '/(?P<domain>[a-z0-9][a-z0-9\\-]{1,63}\\.[a-z\\.]{2,' . apply_filters('rocket_get_domain_preg', '6') . '})$/i';
    if (preg_match($match, $host, $regs)) {
        return $regs['domain'];
    }
    return false;
}
function get_rocket_parse_url($url)
{
    if (!is_string($url)) {
        return;
    }
    $encoded_url = preg_replace_callback('%[^:/@?&=#]+%usD', function ($matches) {
        return rawurlencode($matches[0]);
    }, $url);
    $url = wp_parse_url($encoded_url);
    $host = isset($url['host']) ? strtolower(urldecode($url['host'])) : '';
    $path = isset($url['path']) ? urldecode($url['path']) : '';
    $scheme = isset($url['scheme']) ? urldecode($url['scheme']) : '';
    $query = isset($url['query']) ? urldecode($url['query']) : '';
    $fragment = isset($url['fragment']) ? urldecode($url['fragment']) : '';
    return (array) apply_filters('rocket_parse_url', ['host' => $host, 'path' => $path, 'scheme' => $scheme, 'query' => $query, 'fragment' => $fragment]);
}
function rocket_extract_url_component($url, $component)
{
    return _get_component_from_parsed_url_array(wp_parse_url($url), $component);
}
function rocket_realpath($file)
{
    $wrapper = null;
    if (rocket_is_stream($file)) {
        list($wrapper, $file) = explode('://', $file, 2);
    }
    $path = [];
    foreach (explode('/', $file) as $part) {
        if ('' === $part || '.' === $part) {
            continue;
        }
        if ('..' !== $part) {
            array_push($path, $part);
        } elseif (count($path) > 0) {
            array_pop($path);
        }
    }
    $file = join('/', $path);
    if (null !== $wrapper) {
        return $wrapper . '://' . $file;
    }
    $prefix = 'WIN' === strtoupper(substr(PHP_OS, 0, 3)) ? '' : '/';
    return $prefix . $file;
}
function rocket_url_to_path($url, array $zones = array('all'))
{
    $wp_content_dir = rocket_get_constant('WP_CONTENT_DIR');
    $root_dir = trailingslashit(dirname($wp_content_dir));
    $root_url = str_replace(wp_basename($wp_content_dir), '', content_url());
    $url_host = wp_parse_url($url, PHP_URL_HOST);
    if (null === $url_host) {
        $subdir_levels = substr_count(preg_replace('/https?:\\/\\//', '', site_url()), '/');
        $url = trailingslashit(site_url() . str_repeat('/..', $subdir_levels)) . ltrim($url, '/');
    }
    $url = apply_filters('rocket_asset_url', $url, $zones);
    $url = rawurldecode($url);
    $root_url = preg_replace('/^https?:/', '', $root_url);
    $url = preg_replace('/^https?:/', '', $url);
    $file = str_replace($root_url, $root_dir, $url);
    $file = rocket_realpath($file);
    $file = apply_filters('rocket_url_to_path', $file, $url);
    if (!rocket_direct_filesystem()->is_readable($file)) {
        return false;
    }
    return $file;
}
function rocket_get_external_url($target, $query_args = array())
{
    $site_url = WP_ROCKET_WEB_MAIN;
    switch ($target) {
        case 'support':
            $locale = function_exists('get_user_locale') ? get_user_locale() : get_locale();
            $paths = ['default' => 'support', 'fr_FR' => 'fr/support', 'fr_CA' => 'fr/support', 'it_IT' => 'it/supporto', 'de_DE' => 'de/support', 'es_ES' => 'es/soporte'];
            $url = isset($paths[$locale]) ? $paths[$locale] : $paths['default'];
            $url = $site_url . $url . '/';
            break;
        case 'account':
            $locale = function_exists('get_user_locale') ? get_user_locale() : get_locale();
            $paths = ['default' => 'account', 'fr_FR' => 'fr/compte', 'fr_CA' => 'fr/compte', 'it_IT' => 'it/account/', 'de_DE' => 'de/konto/', 'es_ES' => 'es/cuenta/'];
            $url = isset($paths[$locale]) ? $paths[$locale] : $paths['default'];
            $url = $site_url . $url . '/';
            break;
        default:
            $url = $site_url;
    }
    if ($query_args) {
        $url = add_query_arg($query_args, $url);
    }
    return $url;
}