/** * Returns headers for file * * @param array $file CDN file array * @return array */ function _get_headers($file) { w3_require_once(W3TC_INC_DIR . '/functions/mime.php'); $local_path = $file['local_path']; $mime_type = w3_get_mime_type($local_path); $last_modified = time(); $link = $file['original_url']; $headers = array('Content-Type' => $mime_type, 'Last-Modified' => w3_http_date($last_modified), 'Access-Control-Allow-Origin' => '*', 'Link' => '<' . $link . '>; rel="canonical"'); if (isset($this->cache_config[$mime_type])) { if ($this->cache_config[$mime_type]['etag']) { $headers['Etag'] = @md5_file($local_path); } if ($this->cache_config[$mime_type]['w3tc']) { $headers['X-Powered-By'] = W3TC_POWERED_BY; } if ($this->cache_config[$mime_type]['expires']) { $headers['Expires'] = w3_http_date(time() + $this->cache_config[$mime_type]['lifetime']); } switch ($this->cache_config[$mime_type]['cache_control']) { case 'cache': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public')); break; case 'cache_public_maxage': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => ($this->cache_config[$mime_type]['expires'] ? '' : 'max-age=' . $this->cache_config[$mime_type]['lifetime'] . ', ') . 'public')); break; case 'cache_validation': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public, must-revalidate, proxy-revalidate')); break; case 'cache_noproxy': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public, must-revalidate')); break; case 'cache_maxage': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => ($this->cache_config[$mime_type]['expires'] ? '' : 'max-age=' . $this->cache_config[$mime_type]['lifetime'] . ', ') . 'public, must-revalidate, proxy-revalidate')); break; case 'no_cache': $headers = array_merge($headers, array('Pragma' => 'no-cache', 'Cache-Control' => 'max-age=0, private, no-store, no-cache, must-revalidate')); break; } } return $headers; }
/** * Sends headers * @param boolean $is_404 * @param string $etag * @param integer $time * @param string $compression * @param array $custom_headers * @return boolean */ function _send_headers($is_404, $time, $etag, $compression, $custom_headers = array()) { $exit = false; $headers = array(); $curr_time = time(); $expires = $time + $this->_lifetime; $max_age = $expires > $curr_time ? $expires - $curr_time : 0; if ($is_404) { /** * Add 404 header */ $headers = array_merge($headers, array('Status' => 'HTTP/1.1 404 Not Found')); } elseif ($this->_check_modified_since($time) || $this->_check_match($etag)) { /** * Add 304 header */ $headers = array_merge($headers, array('Status' => 'HTTP/1.1 304 Not Modified')); /** * Don't send content if it isn't modified */ $exit = true; } /** * Add default headers */ $headers = array_merge($headers, array('Last-Modified' => w3_http_date($time), 'Vary' => 'Cookie')); if ($this->_config->get_boolean('browsercache.enabled')) { if ($this->_config->get_boolean('browsercache.html.expires')) { $headers = array_merge($headers, array('Expires' => w3_http_date($expires))); } if ($this->_config->get_boolean('browsercache.html.cache.control')) { switch ($this->_config->get_string('browsercache.html.cache.policy')) { case 'cache': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public')); break; case 'cache_validation': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public, must-revalidate, proxy-revalidate')); break; case 'cache_noproxy': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public, must-revalidate')); break; case 'cache_maxage': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => sprintf('max-age=%d, public, must-revalidate, proxy-revalidate', $max_age))); break; case 'no_cache': $headers = array_merge($headers, array('Pragma' => 'no-cache', 'Cache-Control' => 'max-age=0, private, no-store, no-cache, must-revalidate')); break; } } if ($this->_config->get_boolean('browsercache.html.etag')) { $headers = array_merge($headers, array('Etag' => $etag)); } if ($this->_config->get_boolean('browsercache.html.w3tc')) { $headers = array_merge($headers, array('X-Powered-By' => W3TC_POWERED_BY)); } } if ($compression) { /** * Add Content-Encoding header */ $headers = array_merge($headers, array('Vary' => 'Accept-Encoding, Cookie', 'Content-Encoding' => $compression)); } /** * Add custom headers */ $headers = array_merge($headers, $custom_headers); /** * Disable caching for preview mode */ if (w3_is_preview_mode()) { $headers = array_merge($headers, array('Pragma' => 'private', 'Cache-Control' => 'private')); } /** * Send headers to client */ $result = $this->_headers($headers); if ($exit) { exit; } return $result; }
/** * Sends headers * @param boolean $is_404 * @param string $etag * @param integer $time * @param string $compression * @param array $custom_headers * @return boolean */ function _send_headers($is_404, $time, $etag, $compression, $custom_headers = array()) { $exit = false; $headers = array(); $curr_time = time(); $bc_lifetime = $this->_config->get_integer('browsercache.html.lifetime'); $expires = (is_null($time) ? $curr_time : $time) + $bc_lifetime; $max_age = $expires > $curr_time ? $expires - $curr_time : 0; if ($is_404) { /** * Add 404 header */ $headers = array_merge($headers, array('Status' => 'HTTP/1.1 404 Not Found')); } elseif (!is_null($time) && $this->_check_modified_since($time) || $this->_check_match($etag)) { /** * Add 304 header */ $headers = array_merge($headers, array('Status' => 'HTTP/1.1 304 Not Modified')); /** * Don't send content if it isn't modified */ $exit = true; } if ($this->_config->get_boolean('browsercache.enabled')) { if ($this->_config->get_boolean('browsercache.html.last_modified')) { $headers = array_merge($headers, array('Last-Modified' => w3_http_date($time))); } if ($this->_config->get_boolean('browsercache.html.expires')) { $headers = array_merge($headers, array('Expires' => w3_http_date($expires))); } if ($this->_config->get_boolean('browsercache.html.cache.control')) { switch ($this->_config->get_string('browsercache.html.cache.policy')) { case 'cache': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public')); break; case 'cache_public_maxage': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => sprintf('max-age=%d, public', $max_age))); break; case 'cache_validation': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public, must-revalidate, proxy-revalidate')); break; case 'cache_noproxy': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'private, must-revalidate')); break; case 'cache_maxage': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => sprintf('max-age=%d, public, must-revalidate, proxy-revalidate', $max_age))); break; case 'no_cache': $headers = array_merge($headers, array('Pragma' => 'no-cache', 'Cache-Control' => 'max-age=0, private, no-store, no-cache, must-revalidate')); break; } } if ($this->_config->get_boolean('browsercache.html.etag')) { $headers = array_merge($headers, array('Etag' => $etag)); } if ($this->_config->get_boolean('browsercache.html.w3tc')) { $headers = array_merge($headers, array('X-Powered-By' => W3TC_POWERED_BY)); } } $vary = ''; //compressed && UAG if ($compression && $this->_get_mobile_group()) { $vary = 'Accept-Encoding,User-Agent,Cookie'; $headers = array_merge($headers, array('Content-Encoding' => $compression)); //compressed } elseif ($compression) { $vary = 'Accept-Encoding'; $headers = array_merge($headers, array('Content-Encoding' => $compression)); //uncompressed && UAG } elseif ($this->_get_mobile_group()) { $vary = 'User-Agent,Cookie'; } //Add Cookie to vary if user logged in and not previously set if (!$this->_check_logged_in() && strpos($vary, 'Cookie') === false) { if ($vary) { $vary .= ',Cookie'; } else { $vary = 'Cookie'; } } /** * Add vary header */ if ($vary) { $headers = array_merge($headers, array('Vary' => $vary)); } /** * Add custom headers */ $headers = array_merge($headers, $custom_headers); /** * Disable caching for preview mode */ if (w3_is_preview_mode()) { $headers = array_merge($headers, array('Pragma' => 'private', 'Cache-Control' => 'private')); } /** * Send headers to client */ $result = $this->_headers($headers); if ($exit) { exit; } return $result; }
/** * Returns headers for file * * @param string $file * @return array */ function _get_headers($file) { $mime_type = w3_get_mime_type($file); $last_modified = time(); $headers = array('Content-Type' => $mime_type, 'Last-Modified' => w3_http_date($last_modified), 'Access-Control-Allow-Origin' => '*'); if (isset($this->cache_config[$mime_type])) { if ($this->cache_config[$mime_type]['etag']) { $headers['Etag'] = @md5_file($file); } if ($this->cache_config[$mime_type]['w3tc']) { $headers['X-Powered-By'] = W3TC_POWERED_BY; } switch ($this->cache_config[$mime_type]['cache_control']) { case 'cache': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public')); break; case 'cache_validation': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public, must-revalidate, proxy-revalidate')); break; case 'cache_noproxy': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public, must-revalidate')); break; case 'cache_maxage': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'max-age=' . $this->cache_config[$mime_type]['lifetime'] . ', public, must-revalidate, proxy-revalidate')); break; case 'no_cache': $headers = array_merge($headers, array('Pragma' => 'no-cache', 'Cache-Control' => 'max-age=0, private, no-store, no-cache, must-revalidate')); break; } } return $headers; }