/** * Uncompresses a minify auto filename into an array of files. * @param $compressed * @param $type * @return array */ function uncompress_minify_files($compressed, $type) { $no_type_files = array(); $compressed = basename($compressed, '.' . $type); $uncompressed = $this->_uncompress(base64_decode(strtr($compressed, '-_', '+/'))); $exploded = explode(',', $uncompressed); $replacements = $this->_minify_path_replacements(); foreach ($exploded as $file) { if (!w3_is_url($file)) { $prefix = substr($file, 0, 1); $after_pre = substr($file, 1, 1); if (isset($replacements[$prefix]) && $after_pre == '/') { $file = $replacements[$prefix] . substr($file, 1); $no_type_files[] = $file; } else { $no_type_files[] = $file; } } else { $no_type_files[] = $file; } } $files = array(); foreach ($no_type_files as $no_type_file) { $file = !w3_is_url($no_type_file) ? $no_type_file . '.' . $type : $no_type_file; $verified = false; if (w3_is_url($file)) { $external = $this->_config->get_array('minify.cache.files'); foreach ($external as $ext) { if (preg_match('#' . w3_get_url_regexp($ext) . '#', $file) && !$verified) { $verified = true; } } if (!$verified) { $this->error(sprintf('Remote file not in external files/libraries list: "%s"', $file)); } } elseif (strpos($file, '..') != false || strpos($file, '//') !== false || strpos($file, '\\') !== false && strtoupper(substr(PHP_OS, 0, 3)) != 'WIN' || preg_match('/(?:^|[^\\.])\\.\\//', $file) || !preg_match('/^[a-zA-Z0-9_.\\/-]|[\\\\]+$/', $file)) { $verified = false; $this->error(sprintf('File path invalid: "%s"', $file)); } else { $verified = true; } if ($verified) { $files[] = $file; } } return $files; }
/** * URL file filter * * @param string $file * @return bool */ function _filter_files($file) { static $external; $ext = strrchr($file, '.'); if ($ext != '.js' && $ext != '.css') { return false; } if (!isset($external)) { $external = $this->_config->get_array('minify.cache.files'); } foreach ($external as $ext) { if (preg_match('#' . w3_get_url_regexp($ext) . '#', $file)) { return true; } } if (w3_is_url($file)) { return false; } $path = w3_get_document_root() . '/' . $file; if (!file_exists($path)) { return false; } return true; }
/** * Set up groups of files as sources * * @param array $options controller and Minify options * @return array Minify options * */ public function setupSources($options) { // filter controller options $cOptions = array_merge(array('allowDirs' => '//', 'groupsOnly' => false, 'groups' => array(), 'maxFiles' => 100), isset($options['minApp']) ? $options['minApp'] : array()); unset($options['minApp']); $sources = array(); if (isset($_GET['g'])) { // try groups if (!isset($cOptions['groups'][$_GET['g']])) { $this->log("A group configuration for \"{$_GET['g']}\" was not set"); return $options; } $files = $cOptions['groups'][$_GET['g']]; // if $files is a single object, casting will break it if (is_object($files)) { $files = array($files); } elseif (!is_array($files)) { $files = (array) $files; } foreach ($files as $file) { if ($file instanceof Minify_Source) { $sources[] = $file; continue; } if (0 === strpos($file, '//')) { $file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1); } $realPath = realpath($file); if (is_file($realPath)) { $sources[] = new Minify_Source(array('filepath' => $realPath)); } else { $this->log("The path \"{$realPath}\" could not be found (or was not a file)"); continue; } } } elseif (!$cOptions['groupsOnly'] && isset($_GET['f'])) { $config = w3_instance('W3_Config'); $external = $config->get_array('minify.cache.files'); $files = $_GET['f']; $temp_files = array(); $external_files = 0; foreach ($files as $file) { if (!is_string($file)) { $url = $file->minifyOptions['prependRelativePath']; $verified = false; foreach ($external as $ext) { if (preg_match('#' . w3_get_url_regexp($ext) . '#', $url) && !$verified) { $verified = true; } } if (!$verified) { $this->log("GET['f'] param part invalid, not in accepted external files list: \"{$url}\""); return $options; } $external_files++; } else { $temp_files[] = $file; } } if ($temp_files) { $imploded = implode(',', $temp_files); if (!preg_match('/^[^,]+\\.(css|js)(?:,[^,]+\\.\\1)*$/', $imploded) || strpos($imploded, '//') !== false || strpos($imploded, '\\') !== false || preg_match('/(?:^|[^\\.])\\.\\//', $imploded)) { $this->log("GET['f'] param part invalid: \"{$imploded}\""); return $options; } } if (count($files) > $cOptions['maxFiles'] || count($files) - $external_files != count(array_unique($temp_files))) { $this->log("Too many or duplicate files specified: \"" . implode(', ', $temp_files) . "\""); return $options; } if (!empty($_GET['b'])) { // check for validity if (preg_match('@^[^/]+(?:/[^/]+)*$@', $_GET['b']) && false === strpos($_GET['b'], '..') && $_GET['b'] !== '.') { // valid base $base = "/{$_GET['b']}/"; } else { $this->log("GET['b'] param invalid: \"{$_GET['b']}\""); return $options; } } else { $base = '/'; } $allowDirs = array(); foreach ((array) $cOptions['allowDirs'] as $allowDir) { $allowDirs[] = realpath(str_replace('//', $_SERVER['DOCUMENT_ROOT'] . '/', $allowDir)); } foreach ($files as $file) { if ($file instanceof Minify_Source) { $sources[] = $file; continue; } $path = $_SERVER['DOCUMENT_ROOT'] . $base . $file; $file = realpath($path); if (false === $file) { $this->log("Path \"{$path}\" failed realpath()"); return $options; } elseif (!parent::_fileIsSafe($file, $allowDirs)) { $this->log("Path \"{$path}\" failed Minify_Controller_Base::_fileIsSafe()"); return $options; } else { $sources[] = new Minify_Source(array('filepath' => $file)); } } } if ($sources) { $this->sources = $sources; } else { $this->log("No sources to serve"); } return $options; }
/** * Returns home url regexp * * @return string */ function w3_get_home_url_regexp() { $home_url = w3_get_home_url(); $regexp = w3_get_url_regexp($home_url); return $regexp; }
/** * Gets regexp for minified files * * @return string */ function _minify_url_regexp($filename_mask) { $minify_base_url = w3_filename_to_url(w3_cache_blog_dir('minify')); $matches = null; if (!preg_match('~((https?://)?([^/]+))(.+)~i', $minify_base_url, $matches)) { return ''; } $protocol_domain_regexp = w3_get_url_regexp($matches[1]); $path_regexp = w3_preg_quote($matches[4]); $regexp = '(' . '(' . $protocol_domain_regexp . ')?' . '(' . $path_regexp . $filename_mask . ')' . ')'; return $regexp; }
/** * URL file filter * * @param string $file * @return bool */ public function is_file_for_minification($file) { static $external; $ext = strrchr($file, '.'); if ($ext != '.js' && $ext != '.css') { return false; } if (!isset($external)) { $external = $this->config->get_array('minify.cache.files'); } foreach ($external as $ext) { if (preg_match('#' . w3_get_url_regexp($ext) . '#', $file)) { return true; } } if (w3_is_url($file)) { return false; } /** @var W3_MinifyFileTool $file_tool */ $file_tool = w3_instance('W3_MinifyFileTool'); $file_tool->setDocumentRoot(w3_get_document_root()); if (!$file_tool->fileExists($file)) { return false; } return true; }
/** * @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; }