/** * Returns headers for file * * @param array $file CDN file array * @return array */ function _get_headers($file, $block_expires = false) { $local_path = $file['local_path']; $mime_type = Util_Mime::get_mime_type($local_path); $last_modified = time(); $link = $file['original_url']; $headers = array('Content-Type' => $mime_type, 'Last-Modified' => Util_Content::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'] = Util_Environment::w3tc_header($this->_config); } $expires_set = false; if (!$block_expires && $this->cache_config[$mime_type]['expires']) { $headers['Expires'] = Util_Content::http_date(time() + $this->cache_config[$mime_type]['lifetime']); $expires_set = true; } 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' => ($expires_set ? '' : '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' => 'private, must-revalidate')); break; case 'cache_maxage': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => ($expires_set ? '' : '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 = is_array($custom_headers) ? $custom_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' => Util_Content::http_date($time))); } if ($this->_config->get_boolean('browsercache.html.expires')) { $headers = array_merge($headers, array('Expires' => Util_Content::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' => Util_Environment::w3tc_header())); } } $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)); } /** * Disable caching for preview mode */ if (Util_Environment::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; }