/** * @param array $spec options * * 'isPublic': (bool) if false, the Cache-Control header will contain * "private", allowing only browser caching. (default false) * * 'lastModifiedTime': (int) if given, both ETag AND Last-Modified headers * will be sent with content. This is recommended. * * 'encoding': (string) if set, the header "Vary: Accept-Encoding" will * always be sent and a truncated version of the encoding will be appended * to the ETag. E.g. "pub123456;gz". This will also trigger a more lenient * checking of the client's If-None-Match header, as the encoding portion of * the ETag will be stripped before comparison. * * 'contentHash': (string) if given, only the ETag header can be sent with * content (only HTTP1.1 clients can conditionally GET). The given string * should be short with no quote characters and always change when the * resource changes (recommend md5()). This is not needed/used if * lastModifiedTime is given. * * 'eTag': (string) if given, this will be used as the ETag header rather * than values based on lastModifiedTime or contentHash. Also the encoding * string will not be appended to the given value as described above. * * 'invalidate': (bool) if true, the client cache will be considered invalid * without testing. Effectively this disables conditional GET. * (default false) * * 'maxAge': (int) if given, this will set the Cache-Control max-age in * seconds, and also set the Expires header to the equivalent GMT date. * After the max-age period has passed, the browser will again send a * conditional GET to revalidate its cache. */ public function __construct($spec) { if (isset($spec['cacheHeaders']) && is_array($spec['cacheHeaders'])) { $this->_cacheHeaders = $spec['cacheHeaders']; } $scope = $this->_cacheHeaders['cacheheaders_enabled'] && $this->_cacheHeaders['cacheheaders'] != 'no_cache' ? 'public' : 'private'; $maxAge = 0; $this->_headers['Pragma'] = $scope; // backwards compatibility (can be removed later) if (isset($spec['setExpires']) && is_numeric($spec['setExpires']) && !isset($spec['maxAge'])) { $spec['maxAge'] = $spec['setExpires'] - $_SERVER['REQUEST_TIME']; } if (isset($spec['maxAge']) && $this->_cacheHeaders['expires_enabled'] && $spec['maxAge']) { $maxAge = $spec['maxAge']; $this->_headers['Expires'] = self::gmtDate($_SERVER['REQUEST_TIME'] + $spec['maxAge']); } $etagAppend = ''; if (isset($spec['encoding'])) { $this->_stripEtag = true; $this->_headers['Vary'] = 'Accept-Encoding'; if ('' !== $spec['encoding']) { if (0 === strpos($spec['encoding'], 'x-')) { $spec['encoding'] = substr($spec['encoding'], 2); } $etagAppend = ';' . substr($spec['encoding'], 0, 2); } } if (isset($spec['lastModifiedTime'])) { $this->_setLastModified($spec['lastModifiedTime']); if (isset($spec['eTag'])) { // Use it $this->_setEtag($spec['eTag'], $scope); } else { // base both headers on time $this->_setEtag($spec['lastModifiedTime'] . $etagAppend, $scope); } } elseif (isset($spec['eTag'])) { // Use it $this->_setEtag($spec['eTag'], $scope); } elseif (isset($spec['contentHash'])) { // Use the hash as the ETag $this->_setEtag($spec['contentHash'] . $etagAppend, $scope); } if ($this->_cacheHeaders['cacheheaders_enabled']) { switch ($this->_cacheHeaders['cacheheaders']) { case 'cache': $this->_headers['Cache-Control'] = 'public'; break; case 'cache_public_maxage': $this->_headers['Cache-Control'] = "max-age={$maxAge}, public"; break; case 'cache_validation': $this->_headers['Cache-Control'] = 'public, must-revalidate, proxy-revalidate'; break; case 'cache_noproxy': $this->_headers['Cache-Control'] = 'private, must-revalidate'; break; case 'cache_maxage': $this->_headers['Cache-Control'] = "max-age={$maxAge}, {$scope}, must-revalidate, proxy-revalidate"; break; case 'no_cache': $this->_headers['Cache-Control'] = 'max-age=0, private, no-store, no-cache, must-revalidate'; break; } } /** * Disable caching for preview mode */ if (\W3TC\Util_Environment::is_preview_mode()) { $this->_headers = array_merge($this->_headers, array('Pragma' => 'private', 'Cache-Control' => 'private')); } // invalidate cache if disabled, otherwise check $this->cacheIsValid = isset($spec['invalidate']) && $spec['invalidate'] ? false : $this->_isCacheValid(); }
/** * Set up groups of files as sources * * @param array $options controller and Minify options * * @return array Minify options */ public function setupSources($options) { // PHP insecure by default: realpath() and other FS functions can't handle null bytes. foreach (array('g', 'b', 'f') as $key) { if (isset($_GET[$key])) { $_GET[$key] = str_replace("", '', (string) $_GET[$key]); } } // filter controller options $cOptions = array_merge(array('allowDirs' => '//', 'groupsOnly' => false, 'groups' => array(), 'noMinPattern' => '@[-\\.]min\\.(?:js|css)$@i'), isset($options['minApp']) ? $options['minApp'] : array()); unset($options['minApp']); $sources = array(); $this->selectionId = ''; $firstMissingResource = null; if (isset($_GET['g'])) { // add group(s) $this->selectionId .= 'g=' . $_GET['g']; $keys = explode(',', $_GET['g']); if ($keys != array_unique($keys)) { $this->log("Duplicate group key found."); return $options; } $keys = explode(',', $_GET['g']); foreach ($keys as $key) { if (!isset($cOptions['groups'][$key])) { $this->log("A group configuration for \"{$key}\" was not found"); return $options; } $files = $cOptions['groups'][$key]; // 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 = \W3TC\Util_Environment::realpath($file); if ($realpath && is_file($realpath)) { $sources[] = $this->_getFileSource($realpath, $cOptions); } else { $this->log("The path \"{$file}\" (realpath \"{$realpath}\") could not be found (or was not a file)"); if (null === $firstMissingResource) { $firstMissingResource = basename($file); continue; } else { $secondMissingResource = basename($file); $this->log("More than one file was missing: '{$firstMissingResource}', '{$secondMissingResource}'"); return $options; } } } if ($sources) { try { $this->checkType($sources[0]); } catch (Exception $e) { $this->log($e->getMessage()); return $options; } } } } if (!$cOptions['groupsOnly'] && isset($_GET['f_array'])) { $files = $_GET['f_array']; $ext = $_GET['ext']; 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 param 'b' invalid (see MinApp.php line 84)"); return $options; } } else { $base = '/'; } $allowDirs = array(); foreach ((array) $cOptions['allowDirs'] as $allowDir) { $allowDirs[] = \W3TC\Util_Environment::realpath(str_replace('//', $_SERVER['DOCUMENT_ROOT'] . '/', $allowDir)); } $basenames = array(); // just for cache id foreach ($files as $file) { if ($file instanceof Minify_Source) { $sources[] = $file; continue; } $uri = $base . $file; $path = $_SERVER['DOCUMENT_ROOT'] . $uri; $realpath = \W3TC\Util_Environment::realpath($path); if (false === $realpath || !is_file($realpath)) { $this->log("The path \"{$path}\" (realpath \"{$realpath}\") could not be found (or was not a file)"); if (null === $firstMissingResource) { $firstMissingResource = $uri; continue; } else { $secondMissingResource = $uri; $this->log("More than one file was missing: '{$firstMissingResource}', '{$secondMissingResource}`'"); return $options; } } try { parent::checkNotHidden($realpath); parent::checkAllowDirs($realpath, $allowDirs, $uri); } catch (Exception $e) { $this->log($e->getMessage()); return $options; } $sources[] = $this->_getFileSource($realpath, $cOptions); $basenames[] = basename($realpath, $ext); } if ($this->selectionId) { $this->selectionId .= '_f='; } $this->selectionId .= implode(',', $basenames) . $ext; } if ($sources) { if (null !== $firstMissingResource) { array_unshift($sources, new Minify_Source(array('id' => 'missingFile', 'lastModified' => 0, 'content' => "/* Minify: at least one missing file. See " . Minify0_Minify::URL_DEBUG . " */\n", 'minifier' => ''))); } $this->sources = $sources; } else { $this->log("No sources to serve"); } return $options; }
/** * Determine the client's best encoding method from the HTTP Accept-Encoding * header. * * If no Accept-Encoding header is set, or the browser is IE before v6 SP2, * this will return ('', ''), the "identity" encoding. * * A syntax-aware scan is done of the Accept-Encoding, so the method must * be non 0. The methods are favored in order of gzip, deflate, then * compress. Deflate is always smallest and generally faster, but is * rarely sent by servers, so client support could be buggier. * * @param bool $allowCompress allow the older compress encoding * * @param bool $allowDeflate allow the more recent deflate encoding * * @return array two values, 1st is the actual encoding method, 2nd is the * alias of that method to use in the Content-Encoding header (some browsers * call gzip "x-gzip" etc.) */ public static function getAcceptedEncoding($allowCompress = true, $allowDeflate = true) { // @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) || \W3TC\Util_Environment::is_zlib_enabled() || headers_sent() || self::isBuggyIe()) { return array('', ''); } $ae = $_SERVER['HTTP_ACCEPT_ENCODING']; // gzip checks (quick) if (0 === strpos($ae, 'gzip,') || 0 === strpos($ae, 'deflate, gzip,')) { if (function_exists('gzencode')) { return array('gzip', 'gzip'); } } // gzip checks (slow) if (preg_match('@(?:^|,)\\s*((?:x-)?gzip)\\s*(?:$|,|;\\s*q=(?:0\\.|1))@', $ae, $m)) { return array('gzip', $m[1]); } return array('', ''); }
} if (!defined('W3TC_DIR')) { define('W3TC_DIR', (defined('WP_PLUGIN_DIR') ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins') . '/w3-total-cache'); } /** * Abort W3TC loading if WordPress is upgrading */ if (!@is_dir(W3TC_DIR) || !file_exists(W3TC_DIR . '/w3-total-cache-api.php')) { if (!defined('WP_ADMIN')) { // lets don't show error on front end require_once ABSPATH . WPINC . '/wp-db.php'; } else { echo sprintf('<strong>W3 Total Cache Error:</strong> some files appear to be missing or out of place. Please re-install plugin or remove <strong>%s</strong>. <br />', __FILE__); } } else { require_once W3TC_DIR . '/w3-total-cache-api.php'; // no caching during activation $is_installing = defined('WP_INSTALLING') && WP_INSTALLING; $config = \W3TC\Dispatcher::config(); if (!$is_installing && $config->get_boolean('dbcache.enabled') || \W3TC\Util_Environment::is_dbcluster()) { if (defined('DB_TYPE')) { $db_driver_path = sprintf('%s/Db/%s.php', W3TC_LIB_DIR, DB_TYPE); if (file_exists($db_driver_path)) { require_once $db_driver_path; } else { die(sprintf('<strong>W3 Total Cache Error:</strong> database driver doesn\'t exist: %s.', $db_driver_path)); } } $GLOBALS['wpdb'] = \W3TC\DbCache_Wpdb::instance(); } }
/** * @param array $m * * @return string */ private static function _processUriCB($m) { // $m matched either '/@import\\s+([\'"])(.*?)[\'"]/' or '/url\\(\\s*([^\\)\\s]+)\\s*\\)/' $isImport = $m[0][0] === '@'; // determine URI and the quote character (if any) if ($isImport) { $quoteChar = $m[1]; $uri = $m[2]; } else { // $m[1] is either quoted or not $quoteChar = $m[1][0] === "'" || $m[1][0] === '"' ? $m[1][0] : ''; $uri = $quoteChar === '' ? $m[1] : substr($m[1], 1, strlen($m[1]) - 2); } // analyze URI if ('/' !== $uri[0] && false === strpos($uri, '//') && 0 !== strpos($uri, 'data:')) { // URI is file-relative: rewrite depending on options if (self::$_prependPath === null) { $uri = self::rewriteRelative($uri, self::$_currentDir, self::$_docRoot, self::$_symlinks); if (self::$_prependAbsolutePath) { $prependAbsolutePath = self::$_prependAbsolutePath; } elseif (self::$_prependAbsolutePathCallback) { $prependAbsolutePath = call_user_func(self::$_prependAbsolutePathCallback, $uri); } else { $prependAbsolutePath = ''; } if ($prependAbsolutePath) { $uri = rtrim($prependAbsolutePath, '/') . $uri; } } else { if (!\W3TC\Util_Environment::is_url(self::$_prependPath)) { $uri = self::$_prependPath . $uri; if ($uri[0] === '/') { $root = ''; $rootRelative = $uri; $uri = $root . self::removeDots($rootRelative); } elseif (preg_match('@^((https?\\:)?//([^/]+))/@', $uri, $m) && false !== strpos($m[3], '.')) { $root = $m[1]; $rootRelative = substr($uri, strlen($root)); $uri = $root . self::removeDots($rootRelative); } } else { $parse_url = @parse_url(self::$_prependPath); if ($parse_url && isset($parse_url['host'])) { $scheme = $parse_url['scheme']; $host = $parse_url['host']; $port = isset($parse_url['port']) && $parse_url['port'] != 80 ? ':' . (int) $parse_url['port'] : ''; $path = !empty($parse_url['path']) ? $parse_url['path'] : '/'; $dir_css = preg_replace('~[^/]+$~', '', $path); $dir_obj = preg_replace('~[^/]+$~', '', $uri); $dir = ltrim(strpos($dir_obj, '/') === 0 ? \W3TC\Util_Environment::realpath($dir_obj) : \W3TC\Util_Environment::realpath($dir_css . $dir_obj), '/'); $file = basename($uri); $scheme_dot = empty($scheme) ? '' : $scheme . ':'; $uri = sprintf('%s//%s%s/%s/%s', $scheme_dot, $host, $port, $dir, $file); } } } if (self::$_browserCacheId && count(self::$_browserCacheExtensions)) { $matches = null; if (preg_match('~\\.([a-z-_]+)(\\?.*)?$~', $uri, $matches)) { $extension = $matches[1]; $query = isset($matches[2]) ? $matches[2] : ''; if ($extension && in_array($extension, self::$_browserCacheExtensions)) { $uri = \W3TC\Util_Environment::remove_query($uri); $uri .= ($query ? '&' : '?') . self::$_browserCacheId; } } } } return $isImport ? "@import {$quoteChar}{$uri}{$quoteChar}" : "url({$quoteChar}{$uri}{$quoteChar})"; }