/** * Remove script tags from the source * * @param string $content * @param array $files * @return void */ function remove_scripts(&$content, $files) { $regexps = array(); $home_url_regexp = w3_get_home_url_regexp(); $path = ''; if (w3_is_network() && !w3_is_subdomain_install()) { $path = ltrim(w3_get_home_path(), '/'); } foreach ($files as $file) { if ($path && strpos($file, $path) === 0) { $file = substr($file, strlen($path)); } $this->replaced_scripts[] = $file; if (w3_is_url($file) && !preg_match('~' . $home_url_regexp . '~i', $file)) { // external JS files $regexps[] = w3_preg_quote($file); } else { // local JS files $file = ltrim($file, '/'); if (ltrim(w3_get_site_path(), '/') && strpos($file, ltrim(w3_get_site_path(), '/')) === 0) { $file = str_replace(ltrim(w3_get_site_path(), '/'), '', $file); } $file = ltrim(preg_replace('~' . $home_url_regexp . '~i', '', $file), '/\\'); $regexps[] = '(' . $home_url_regexp . ')?/?' . w3_preg_quote($file); } } foreach ($regexps as $regexp) { $content = preg_replace('~<script\\s+[^<>]*src=["\']?' . $regexp . '["\']?[^<>]*>\\s*</script>~Uis', '', $content); } }
/** * Generate rules related to prevent for media 404 error by WP * * @return string */ function generate_rules_no404wp_nginx() { $cssjs_types = $this->_get_cssjs_types(); $html_types = $this->_get_html_types(); $other_types = $this->_get_other_types(); $extensions = array_merge(array_keys($cssjs_types), array_keys($html_types), array_keys($other_types)); $permalink_structure = get_option('permalink_structure'); $permalink_structure_ext = ltrim(strrchr($permalink_structure, '.'), '.'); if ($permalink_structure_ext != '') { foreach ($extensions as $index => $extension) { if (strstr($extension, $permalink_structure_ext) !== false) { $extensions[$index] = preg_replace('~\\|?' . w3_preg_quote($permalink_structure_ext) . '\\|?~', '', $extension); } } } $exceptions = $this->_config->get_array('browsercache.no404wp.exceptions'); $rules = ''; $rules .= W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP . "\n"; $rules .= "if (-f \$request_filename) {\n"; $rules .= " break;\n"; $rules .= "}\n"; $rules .= "if (-d \$request_filename) {\n"; $rules .= " break;\n"; $rules .= "}\n"; if (count($exceptions)) { $rules .= "if (\$request_uri ~ \"(" . implode('|', $exceptions) . ")\") {\n"; $rules .= " break;\n"; $rules .= "}\n"; } $rules .= "if (\$request_uri ~* \\.(" . implode('|', $extensions) . ")\$) {\n"; $rules .= " return 404;\n"; $rules .= "}\n"; $rules .= W3TC_MARKER_END_BROWSERCACHE_NO404WP . "\n"; return $rules; }
/** * Remove script tags from the source * * @param string $content * @param array $files * @return void */ function remove_scripts(&$content, $files) { $regexps = array(); $domain_url_regexp = w3_get_domain_url_regexp(); foreach ($files as $file) { $this->replaced_scripts[] = $file; if (w3_is_url($file) && !preg_match('~' . $domain_url_regexp . '~i', $file)) { // external JS files $regexps[] = w3_preg_quote($file); } else { // local JS files $file = ltrim(preg_replace('~' . $domain_url_regexp . '~i', '', $file), '/\\'); $regexps[] = '(' . $domain_url_regexp . ')?/?' . w3_preg_quote($file); } } foreach ($regexps as $regexp) { $content = preg_replace('~<script\\s+[^<>]*src=["\']?' . $regexp . '["\']?[^<>]*>\\s*</script>~Uis', '', $content); } }
/** * Rewrite a file relative URI as root relative * * <code> * Minify_CSS_UriRewriter::rewriteRelative( * '../img/hello.gif' * , '/home/user/www/css' // path of CSS file * , '/home/user/www' // doc root * ); * // returns '/img/hello.gif' * * // example where static files are stored in a symlinked directory * Minify_CSS_UriRewriter::rewriteRelative( * 'hello.gif' * , '/var/staticFiles/theme' * , '/home/user/www' * , array('/home/user/www/static' => '/var/staticFiles') * ); * // returns '/static/theme/hello.gif' * </code> * * @param string $uri file relative URI * * @param string $realCurrentDir realpath of the current file's directory. * * @param string $realDocRoot realpath of the site document root. * * @param array $symlinks (default = array()) If the file is stored in * a symlink-ed directory, provide an array of link paths to * real target paths, where the link paths "appear" to be within the document * root. E.g.: * <code> * array('/home/foo/www/not/real/path' => '/real/target/path') // unix * array('C:\\htdocs\\not\\real' => 'D:\\real\\target\\path') // Windows * </code> * * @return string */ private static function _rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks = array()) { if ('/' === $uri[0]) { // root-relative return $uri; } // prepend path with current dir separator (OS-independent) $path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR); self::$debugText .= "file-relative URI : {$uri}\n" . "path prepended : {$path}\n"; // "unresolve" a symlink back to doc root foreach ($symlinks as $link => $target) { if (0 === strpos($path, $target)) { // replace $target with $link $path = $link . substr($path, strlen($target)); self::$debugText .= "symlink unresolved : {$path}\n"; break; } } // strip doc root $path = substr($path, strlen($realDocRoot)); self::$debugText .= "docroot stripped : {$path}\n"; // fix to root-relative URI $uri = strtr($path, '/\\', '//'); // remove /./ and /../ where possible $uri = str_replace('/./', '/', $uri); // inspired by patch from Oleg Cherniy do { $uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, 1, $changed); } while ($changed); self::$debugText .= "traversals removed : {$uri}\n\n"; $uri = preg_replace('~^' . w3_preg_quote(w3_get_base_path()) . '~', w3_get_site_path(), $uri); return $uri; }
/** * Check if rules exist * * @param string $rules * @param string $start * @param string $end * @return int */ function w3_has_rules($rules, $start, $end) { return preg_match('~' . w3_preg_quote($start) . "\n.*?" . w3_preg_quote($end) . "\n*~s", $rules); }
/** * Translates remote file to local file * * @param string $file * @return string */ function w3_translate_file($file) { if (!w3_is_url($file)) { $file = '/' . ltrim($file, '/'); $regexp = '~^' . w3_preg_quote(w3_get_site_path()) . '~'; $file = preg_replace($regexp, w3_get_base_path(), $file); $file = ltrim($file, '/'); } return $file; }
/** * Returns regexp by mask * * @param string $mask * @return string */ function get_regexp_by_mask($mask) { $mask = trim($mask); $mask = w3_preg_quote($mask); $mask = str_replace(array('\\*', '\\?', ';'), array('@ASTERISK@', '@QUESTION@', '|'), $mask); $regexp = str_replace(array('@ASTERISK@', '@QUESTION@'), array('[^\\?\\*:\\|"<>]*', '[^\\?\\*:\\|"<>]'), $mask); return $regexp; }
/** * Cleans scripts * * @param string $content * @return string */ function clean_scripts($content) { $regexps = array(); $groups = $this->_config->get_array('minify.js.groups'); $domain_url_regexp = w3_get_domain_url_regexp(); foreach ($groups as $group => $locations) { foreach ((array) $locations as $location => $config) { if (!empty($config['files'])) { foreach ((array) $config['files'] as $file) { if (w3_is_url($file) && !preg_match('~' . $domain_url_regexp . '~i', $file)) { // external JS files $regexps[] = w3_preg_quote($file); } else { // local JS files $file = ltrim(preg_replace('~' . $domain_url_regexp . '~i', '', $file), '/\\'); $regexps[] = '(' . $domain_url_regexp . ')?/?' . w3_preg_quote($file); } } } } } foreach ($regexps as $regexp) { $content = preg_replace('~<script\\s+[^<>]*src=["\']?' . $regexp . '["\']?[^<>]*>\\s*</script>~is', '', $content); } return $content; }
/** * Returns domain url regexp * * @return string */ function w3_get_domain_url_regexp() { $domain_url = w3_get_domain_url(); $domain = preg_replace('~https?://~i', '', $domain_url); $regexp = 'https?://' . w3_preg_quote($domain); return $regexp; }
/** * Returns regexp by mask * * @param string $mask * @return string */ function get_regexp_by_mask($mask) { $regexp = str_replace(array('\\*', '\\?', '\\[', '\\]', ';'), array('[^\\s"\'>]*', '[^\\s"\'>]', '[', ']', '|'), w3_preg_quote($mask)); return $regexp; }
/** * @param $domain_url_regexp * @param $baseurl * @param $upload_info * @param $regexps * @return array */ private function make_uploads_regexes($domain_url_regexp, $baseurl, $upload_info, $regexps) { if (preg_match('~' . $domain_url_regexp . '~i', $baseurl)) { $regexps[] = '~(["\'(])\\s*((' . $domain_url_regexp . ')?(' . w3_preg_quote($upload_info['baseurlpath']) . '([^"\')>]+)))~'; } else { $parsed = @parse_url($baseurl); $upload_url_domain_regexp = isset($parsed['host']) ? w3_get_url_regexp($parsed['scheme'] . '://' . $parsed['host']) : $domain_url_regexp; $baseurlpath = isset($parsed['path']) ? rtrim($parsed['path'], '/') : ''; if ($baseurlpath) { $regexps[] = '~(["\'])\\s*((' . $upload_url_domain_regexp . ')?(' . w3_preg_quote($baseurlpath) . '([^"\'>]+)))~'; } else { $regexps[] = '~(["\'])\\s*((' . $upload_url_domain_regexp . ')(([^"\'>]+)))~'; } } return $regexps; }