2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2013-05-10 11:59:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* A class with HTTP-related helpers. Like Debug, this is more a bundle of methods than a class.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage misc
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class HTTP {
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2013-05-10 11:59:20 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* @var int
|
2013-05-10 11:59:20 +02:00
|
|
|
*/
|
2012-05-11 01:04:51 +02:00
|
|
|
protected static $cache_age = 0;
|
|
|
|
|
2013-05-10 11:59:20 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* @var int
|
2013-05-10 11:59:20 +02:00
|
|
|
*/
|
2012-05-11 01:04:51 +02:00
|
|
|
protected static $modification_date = null;
|
|
|
|
|
2013-05-10 11:59:20 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* @var string
|
2013-05-10 11:59:20 +02:00
|
|
|
*/
|
2012-05-11 01:04:51 +02:00
|
|
|
protected static $etag = null;
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-07-27 09:17:37 +02:00
|
|
|
* @config
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
|
|
|
* @var bool
|
2015-07-27 09:17:37 +02:00
|
|
|
*/
|
|
|
|
private static $cache_ajax_requests = true;
|
2015-01-29 02:19:08 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Turns a local system filename into a URL by comparing it to the script filename.
|
|
|
|
*
|
|
|
|
* @param string $filename
|
2013-05-10 11:59:20 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function filename2url($filename) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$slashPos = -1;
|
2013-05-10 11:59:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
while(($slashPos = strpos($filename, "/", $slashPos+1)) !== false) {
|
2015-07-27 09:17:37 +02:00
|
|
|
if(substr($filename, 0, $slashPos) == substr($_SERVER['SCRIPT_FILENAME'], 0, $slashPos)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$commonLength = $slashPos;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2013-05-10 11:59:20 +02:00
|
|
|
$urlBase = substr(
|
2014-08-15 08:53:05 +02:00
|
|
|
$_SERVER['PHP_SELF'],
|
|
|
|
0,
|
2013-05-10 11:59:20 +02:00
|
|
|
-(strlen($_SERVER['SCRIPT_FILENAME']) - $commonLength)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$url = $urlBase . substr($filename, $commonLength);
|
2008-10-09 00:53:20 +02:00
|
|
|
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? "https" : "http";
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Count the number of extra folders the script is in.
|
2015-07-27 09:17:37 +02:00
|
|
|
// $prefix = str_repeat("../", substr_count(substr($_SERVER[SCRIPT_FILENAME], $commonBaseLength)));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 11:59:20 +02:00
|
|
|
return "$protocol://". $_SERVER['HTTP_HOST'] . $url;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Turn all relative URLs in the content to absolute URLs.
|
|
|
|
*
|
|
|
|
* @param string $html
|
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function absoluteURLs($html) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$html = str_replace('$CurrentPageURL', $_SERVER['REQUEST_URI'], $html);
|
2013-01-23 23:35:27 +01:00
|
|
|
return HTTP::urlRewriter($html, function($url) {
|
2013-05-20 02:46:44 +02:00
|
|
|
//no need to rewrite, if uri has a protocol (determined here by existence of reserved URI character ":")
|
2015-07-27 09:17:37 +02:00
|
|
|
if(preg_match('/^\w+:/', $url)) {
|
2013-05-20 01:59:04 +02:00
|
|
|
return $url;
|
|
|
|
}
|
2013-01-23 23:35:27 +01:00
|
|
|
return Director::absoluteURL($url, true);
|
|
|
|
});
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2013-01-23 23:35:27 +01:00
|
|
|
/**
|
|
|
|
* Rewrite all the URLs in the given content, evaluating the given string as PHP code.
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* Put $URL where you want the URL to appear, however, you can't embed $URL in strings, for example:
|
2013-01-23 23:35:27 +01:00
|
|
|
* <ul>
|
|
|
|
* <li><code>'"../../" . $URL'</code></li>
|
|
|
|
* <li><code>'myRewriter($URL)'</code></li>
|
2015-07-27 09:17:37 +02:00
|
|
|
* <li><code>'(substr($URL, 0, 1)=="/") ? "../" . substr($URL, 1) : $URL'</code></li>
|
2013-01-23 23:35:27 +01:00
|
|
|
* </ul>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* As of 3.2 $code should be a callable which takes a single parameter and returns the rewritten,
|
|
|
|
* for example:
|
2013-01-23 23:35:27 +01:00
|
|
|
* <code>
|
2014-08-15 08:53:05 +02:00
|
|
|
* function($url) {
|
2013-01-23 23:35:27 +01:00
|
|
|
* return Director::absoluteURL($url, true);
|
|
|
|
* }
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param string $content The HTML to search for links to rewrite.
|
|
|
|
* @param string|callable $code Either a string that can evaluate to an expression to rewrite links
|
|
|
|
* (depreciated), or a callable that takes a single parameter and returns the rewritten URL.
|
|
|
|
*
|
2016-06-03 10:36:02 +02:00
|
|
|
* @return string The content with all links rewritten as per the logic specified in $code.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function urlRewriter($content, $code) {
|
2013-01-23 23:35:27 +01:00
|
|
|
if(!is_callable($code)) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'HTTP::urlRewriter expects a callable as the second parameter');
|
2013-01-23 23:35:27 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-23 23:35:27 +01:00
|
|
|
// Replace attributes
|
2015-07-27 09:17:37 +02:00
|
|
|
$attribs = array("src", "background", "a" => "href", "link" => "href", "base" => "href");
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($attribs as $tag => $attrib) {
|
|
|
|
if(!is_numeric($tag)) $tagPrefix = "$tag ";
|
|
|
|
else $tagPrefix = "";
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2012-12-20 01:40:42 +01:00
|
|
|
$regExps[] = "/(<{$tagPrefix}[^>]*$attrib *= *\")([^\"]*)(\")/i";
|
|
|
|
$regExps[] = "/(<{$tagPrefix}[^>]*$attrib *= *')([^']*)(')/i";
|
|
|
|
$regExps[] = "/(<{$tagPrefix}[^>]*$attrib *= *)([^\"' ]*)( )/i";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-01-23 23:35:27 +01:00
|
|
|
// Replace css styles
|
|
|
|
// @todo - http://www.css3.info/preview/multiple-backgrounds/
|
|
|
|
$styles = array('background-image', 'background', 'list-style-image', 'list-style', 'content');
|
|
|
|
foreach($styles as $style) {
|
|
|
|
$regExps[] = "/($style:[^;]*url *\(\")([^\"]+)(\"\))/i";
|
|
|
|
$regExps[] = "/($style:[^;]*url *\(')([^']+)('\))/i";
|
|
|
|
$regExps[] = "/($style:[^;]*url *\()([^\"\)')]+)(\))/i";
|
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2013-01-23 23:35:27 +01:00
|
|
|
// Callback for regexp replacement
|
2012-12-20 01:40:42 +01:00
|
|
|
$callback = function($matches) use($code) {
|
2016-06-03 10:36:02 +02:00
|
|
|
// Decode HTML attribute
|
|
|
|
$URL = Convert::xml2raw($matches[2]);
|
2013-01-23 23:35:27 +01:00
|
|
|
if(is_callable($code)) {
|
2016-06-03 10:36:02 +02:00
|
|
|
$rewritten = $code($URL);
|
2013-01-23 23:35:27 +01:00
|
|
|
} else {
|
|
|
|
// Expose the $URL variable to be used by the $code expression
|
|
|
|
$rewritten = eval("return ($code);");
|
|
|
|
}
|
2016-06-03 10:36:02 +02:00
|
|
|
return $matches[1] . Convert::raw2xml($rewritten) . $matches[3];
|
2012-12-20 01:40:42 +01:00
|
|
|
};
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2013-01-23 23:35:27 +01:00
|
|
|
// Execute each expression
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($regExps as $regExp) {
|
2012-12-20 01:40:42 +01:00
|
|
|
$content = preg_replace_callback($regExp, $callback, $content);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
|
|
|
return $content;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2010-04-13 05:21:33 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Will try to include a GET parameter for an existing URL, preserving existing parameters and
|
|
|
|
* fragments. If no URL is given, falls back to $_SERVER['REQUEST_URI']. Uses parse_url() to
|
|
|
|
* dissect the URL, and http_build_query() to reconstruct it with the additional parameter.
|
|
|
|
* Converts any '&' (ampersand) URL parameter separators to the more XHTML compliant '&'.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* CAUTION: If the URL is determined to be relative, it is prepended with Director::absoluteBaseURL().
|
|
|
|
* This method will always return an absolute URL because Director::makeRelative() can lead to
|
|
|
|
* inconsistent results.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-10-06 07:28:04 +02:00
|
|
|
* @param string $varname
|
|
|
|
* @param string $varvalue
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param string $currentURL Relative or absolute URL.
|
|
|
|
* @param string $separator Separator for http_build_query().
|
|
|
|
*
|
|
|
|
* @return string
|
2010-04-13 05:21:33 +02:00
|
|
|
*/
|
2010-10-13 03:48:24 +02:00
|
|
|
public static function setGetVar($varname, $varvalue, $currentURL = null, $separator = '&') {
|
2010-04-13 05:21:33 +02:00
|
|
|
$uri = $currentURL ? $currentURL : Director::makeRelative($_SERVER['REQUEST_URI']);
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2010-10-13 02:46:12 +02:00
|
|
|
$isRelative = false;
|
2010-04-13 05:21:33 +02:00
|
|
|
// We need absolute URLs for parse_url()
|
2010-10-13 02:46:12 +02:00
|
|
|
if(Director::is_relative_url($uri)) {
|
|
|
|
$uri = Director::absoluteBaseURL() . $uri;
|
|
|
|
$isRelative = true;
|
|
|
|
}
|
2010-04-13 05:21:33 +02:00
|
|
|
|
|
|
|
// try to parse uri
|
|
|
|
$parts = parse_url($uri);
|
|
|
|
if(!$parts) {
|
|
|
|
throw new InvalidArgumentException("Can't parse URL: " . $uri);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2010-04-13 05:21:33 +02:00
|
|
|
// Parse params and add new variable
|
|
|
|
$params = array();
|
|
|
|
if(isset($parts['query'])) parse_str($parts['query'], $params);
|
|
|
|
$params[$varname] = $varvalue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-13 03:08:10 +02:00
|
|
|
// Generate URI segments and formatting
|
|
|
|
$scheme = (isset($parts['scheme'])) ? $parts['scheme'] : 'http';
|
|
|
|
$user = (isset($parts['user']) && $parts['user'] != '') ? $parts['user'] : '';
|
|
|
|
|
|
|
|
if($user != '') {
|
|
|
|
// format in either user:pass@host.com or user@host.com
|
|
|
|
$user .= (isset($parts['pass']) && $parts['pass'] != '') ? ':' . $parts['pass'] . '@' : '@';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-13 03:08:10 +02:00
|
|
|
$host = (isset($parts['host'])) ? $parts['host'] : '';
|
|
|
|
$port = (isset($parts['port']) && $parts['port'] != '') ? ':'.$parts['port'] : '';
|
|
|
|
$path = (isset($parts['path']) && $parts['path'] != '') ? $parts['path'] : '';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-13 03:08:10 +02:00
|
|
|
// handle URL params which are existing / new
|
2010-10-13 03:48:24 +02:00
|
|
|
$params = ($params) ? '?' . http_build_query($params, null, $separator) : '';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-13 03:08:10 +02:00
|
|
|
// keep fragments (anchors) intact.
|
|
|
|
$fragment = (isset($parts['fragment']) && $parts['fragment'] != '') ? '#'.$parts['fragment'] : '';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// Recompile URI segments
|
2010-10-13 03:08:10 +02:00
|
|
|
$newUri = $scheme . '://' . $user . $host . $port . $path . $params . $fragment;
|
|
|
|
|
2010-10-13 02:46:12 +02:00
|
|
|
if($isRelative) return Director::makeRelative($newUri);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-13 03:08:10 +02:00
|
|
|
return $newUri;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
/**
|
|
|
|
* @param string $varname
|
|
|
|
* @param string $varvalue
|
|
|
|
* @param null|string $currentURL
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function RAW_setGetVar($varname, $varvalue, $currentURL = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$url = self::setGetVar($varname, $varvalue, $currentURL);
|
|
|
|
return Convert::xml2raw($url);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 05:21:33 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Search for all tags with a specific attribute, then return the value of that attribute in a
|
|
|
|
* flat array.
|
2010-04-13 05:21:33 +02:00
|
|
|
*
|
|
|
|
* @param string $content
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param array $attributes An array of tags to attributes, for example "[a] => 'href', [div] => 'id'"
|
|
|
|
*
|
2010-04-13 05:21:33 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function findByTagAndAttribute($content, $attributes) {
|
|
|
|
$regexes = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 05:21:33 +02:00
|
|
|
foreach($attributes as $tag => $attribute) {
|
|
|
|
$regexes[] = "/<{$tag} [^>]*$attribute *= *([\"'])(.*?)\\1[^>]*>/i";
|
|
|
|
$regexes[] = "/<{$tag} [^>]*$attribute *= *([^ \"'>]+)/i";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 05:21:33 +02:00
|
|
|
$result = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 05:21:33 +02:00
|
|
|
if($regexes) foreach($regexes as $regex) {
|
|
|
|
if(preg_match_all($regex, $content, $matches)) {
|
|
|
|
$result = array_merge_recursive($result, (isset($matches[2]) ? $matches[2] : $matches[1]));
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 05:21:33 +02:00
|
|
|
return count($result) ? $result : null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
/**
|
|
|
|
* @param string $content
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function getLinksIn($content) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return self::findByTagAndAttribute($content, array("a" => "href"));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
/**
|
|
|
|
* @param string $content
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function getImagesIn($content) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return self::findByTagAndAttribute($content, array("img" => "src"));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-11 01:33:21 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Get the MIME type based on a file's extension. If the finfo class exists in PHP, and the file
|
|
|
|
* exists relative to the project root, then use that extension, otherwise fallback to a list of
|
|
|
|
* commonly known MIME types.
|
2012-05-17 23:22:02 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param string $filename
|
2012-05-11 04:05:40 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-05-11 01:33:21 +02:00
|
|
|
public static function get_mime_type($filename) {
|
2012-05-13 23:17:47 +02:00
|
|
|
// If the finfo module is compiled into PHP, use it.
|
2012-05-17 23:22:02 +02:00
|
|
|
$path = BASE_PATH . DIRECTORY_SEPARATOR . $filename;
|
|
|
|
if(class_exists('finfo') && file_exists($path)) {
|
2012-05-13 23:17:47 +02:00
|
|
|
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
2012-05-17 23:22:02 +02:00
|
|
|
return $finfo->file($path);
|
2012-05-13 23:17:47 +02:00
|
|
|
}
|
2012-05-17 23:22:02 +02:00
|
|
|
|
2012-05-13 23:17:47 +02:00
|
|
|
// Fallback to use the list from the HTTP.yml configuration and rely on the file extension
|
|
|
|
// to get the file mime-type
|
2012-05-15 00:03:00 +02:00
|
|
|
$ext = File::get_file_extension($filename);
|
2012-05-13 23:17:47 +02:00
|
|
|
// Get the mime-types
|
|
|
|
$mimeTypes = Config::inst()->get('HTTP', 'MimeTypes');
|
2012-05-17 23:22:02 +02:00
|
|
|
|
2012-05-13 23:17:47 +02:00
|
|
|
// The mime type doesn't exist
|
|
|
|
if(!isset($mimeTypes[$ext])) {
|
|
|
|
return 'application/unknown';
|
|
|
|
}
|
2012-05-17 23:22:02 +02:00
|
|
|
|
2012-05-13 23:17:47 +02:00
|
|
|
return $mimeTypes[$ext];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Set the maximum age of this page in web caches, in seconds.
|
|
|
|
*
|
|
|
|
* @param int $age
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_cache_age($age) {
|
2007-07-19 12:40:28 +02:00
|
|
|
self::$cache_age = $age;
|
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
/**
|
|
|
|
* @param string $dateString
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function register_modification_date($dateString) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$timestamp = strtotime($dateString);
|
2007-09-16 17:54:16 +02:00
|
|
|
if($timestamp > self::$modification_date)
|
|
|
|
self::$modification_date = $timestamp;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
/**
|
|
|
|
* @param int $timestamp
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function register_modification_timestamp($timestamp) {
|
2007-09-16 17:53:35 +02:00
|
|
|
if($timestamp > self::$modification_date)
|
|
|
|
self::$modification_date = $timestamp;
|
|
|
|
}
|
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
/**
|
|
|
|
* @param string $etag
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function register_etag($etag) {
|
2007-09-16 17:54:16 +02:00
|
|
|
self::$etag = $etag;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-10-16 05:02:29 +02:00
|
|
|
* Add the appropriate caching headers to the response, including If-Modified-Since / 304 handling.
|
2015-09-01 00:24:03 +02:00
|
|
|
* Note that setting HTTP::$cache_age will overrule any cache headers set by PHP's
|
|
|
|
* session_cache_limiter functionality. It is your responsibility to ensure only cacheable data
|
2015-11-13 23:44:49 +01:00
|
|
|
* is in fact cached, and HTTP::$cache_age isn't set when the HTTP body contains session-specific
|
|
|
|
* content.
|
|
|
|
*
|
|
|
|
* Omitting the $body argument or passing a string is deprecated; in these cases, the headers are
|
|
|
|
* output directly.
|
2007-09-16 17:53:35 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param SS_HTTPResponse $body
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function add_cache_headers($body = null) {
|
2014-04-24 07:18:07 +02:00
|
|
|
$cacheAge = self::$cache_age;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-16 05:02:29 +02:00
|
|
|
// Validate argument
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
if($body && !($body instanceof SS_HTTPResponse)) {
|
|
|
|
user_error("HTTP::add_cache_headers() must be passed an SS_HTTPResponse object", E_USER_WARNING);
|
2008-10-16 05:02:29 +02:00
|
|
|
$body = null;
|
|
|
|
}
|
2011-09-30 06:14:19 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Development sites have frequently changing templates; this can get stuffed up by the code
|
|
|
|
// below.
|
2014-04-24 07:18:07 +02:00
|
|
|
if(Director::isDev()) $cacheAge = 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// The headers have been sent and we don't have an SS_HTTPResponse object to attach things to; no point in
|
|
|
|
// us trying.
|
2008-10-16 05:02:29 +02:00
|
|
|
if(headers_sent() && !$body) return;
|
2011-09-30 06:14:19 +02:00
|
|
|
|
2015-06-20 18:57:18 +02:00
|
|
|
// Populate $responseHeaders with all the headers that we want to build
|
2008-10-16 05:02:29 +02:00
|
|
|
$responseHeaders = array();
|
2015-06-13 02:12:36 +02:00
|
|
|
|
2015-01-29 02:19:08 +01:00
|
|
|
$config = Config::inst();
|
2015-06-13 02:12:36 +02:00
|
|
|
$cacheControlHeaders = Config::inst()->get('HTTP', 'cache_control');
|
|
|
|
|
|
|
|
|
2015-06-20 18:57:18 +02:00
|
|
|
// currently using a config setting to cancel this, seems to be so that the CMS caches ajax requests
|
2015-01-29 02:19:08 +01:00
|
|
|
if(function_exists('apache_request_headers') && $config->get(get_called_class(), 'cache_ajax_requests')) {
|
2015-06-20 18:57:18 +02:00
|
|
|
$requestHeaders = array_change_key_case(apache_request_headers(), CASE_LOWER);
|
2015-06-13 02:12:36 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($requestHeaders['x-requested-with']) && $requestHeaders['x-requested-with']=='XMLHttpRequest') {
|
2014-04-24 07:18:07 +02:00
|
|
|
$cacheAge = 0;
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2008-10-16 05:02:29 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2014-04-24 07:18:07 +02:00
|
|
|
if($cacheAge > 0) {
|
2015-06-13 02:12:36 +02:00
|
|
|
$cacheControlHeaders['max-age'] = self::$cache_age;
|
2015-09-01 00:24:03 +02:00
|
|
|
|
|
|
|
// Set empty pragma to avoid PHP's session_cache_limiter adding conflicting caching information,
|
|
|
|
// defaulting to "nocache" on most PHP configurations (see http://php.net/session_cache_limiter).
|
|
|
|
// Since it's a deprecated HTTP 1.0 option, all modern HTTP clients and proxies should
|
|
|
|
// prefer the caching information indicated through the "Cache-Control" header.
|
2008-10-16 05:02:29 +02:00
|
|
|
$responseHeaders["Pragma"] = "";
|
2011-07-07 07:28:58 +02:00
|
|
|
|
2013-07-05 06:03:51 +02:00
|
|
|
// To do: User-Agent should only be added in situations where you *are* actually
|
2013-02-18 14:41:49 +01:00
|
|
|
// varying according to user-agent.
|
2015-12-08 05:19:24 +01:00
|
|
|
$vary = $config->get('HTTP', 'vary');
|
|
|
|
if ($vary && strlen($vary)) {
|
|
|
|
$responseHeaders['Vary'] = $vary;
|
|
|
|
}
|
2013-07-05 06:03:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-09-23 03:32:55 +02:00
|
|
|
if($body) {
|
|
|
|
// Grab header for checking. Unfortunately HTTPRequest uses a mistyped variant.
|
|
|
|
$contentDisposition = $body->getHeader('Content-disposition');
|
|
|
|
if (!$contentDisposition) $contentDisposition = $body->getHeader('Content-Disposition');
|
|
|
|
}
|
2013-08-23 05:04:54 +02:00
|
|
|
|
|
|
|
if(
|
2013-09-23 03:32:55 +02:00
|
|
|
$body &&
|
2013-08-23 05:04:54 +02:00
|
|
|
Director::is_https() &&
|
2015-03-26 00:17:22 +01:00
|
|
|
isset($_SERVER['HTTP_USER_AGENT']) &&
|
|
|
|
strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')==true &&
|
2013-08-23 05:04:54 +02:00
|
|
|
strstr($contentDisposition, 'attachment;')==true
|
|
|
|
) {
|
|
|
|
// IE6-IE8 have problems saving files when https and no-cache are used
|
|
|
|
// (http://support.microsoft.com/kb/323308)
|
|
|
|
// Note: this is also fixable by ticking "Do not save encrypted pages to disk" in advanced options.
|
2015-06-13 02:12:36 +02:00
|
|
|
$cacheControlHeaders['max-age'] = 3;
|
2015-10-06 07:31:58 +02:00
|
|
|
|
2015-09-01 00:24:03 +02:00
|
|
|
// Set empty pragma to avoid PHP's session_cache_limiter adding conflicting caching information,
|
|
|
|
// defaulting to "nocache" on most PHP configurations (see http://php.net/session_cache_limiter).
|
|
|
|
// Since it's a deprecated HTTP 1.0 option, all modern HTTP clients and proxies should
|
|
|
|
// prefer the caching information indicated through the "Cache-Control" header.
|
2013-08-23 05:04:54 +02:00
|
|
|
$responseHeaders["Pragma"] = "";
|
|
|
|
} else {
|
2015-06-13 02:12:36 +02:00
|
|
|
$cacheControlHeaders['no-cache'] = "true";
|
2015-11-16 04:50:40 +01:00
|
|
|
$cacheControlHeaders['no-store'] = "true";
|
2015-06-13 02:12:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($cacheControlHeaders as $header => $value) {
|
|
|
|
if(is_null($value)) {
|
|
|
|
unset($cacheControlHeaders[$header]);
|
2015-06-20 18:57:18 +02:00
|
|
|
} elseif((is_bool($value) && $value) || $value === "true") {
|
2015-06-13 02:12:36 +02:00
|
|
|
$cacheControlHeaders[$header] = $header;
|
|
|
|
} else {
|
|
|
|
$cacheControlHeaders[$header] = $header."=".$value;
|
2013-08-23 05:04:54 +02:00
|
|
|
}
|
2008-10-16 05:02:29 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2015-06-20 18:57:18 +02:00
|
|
|
$responseHeaders['Cache-Control'] = implode(', ', $cacheControlHeaders);
|
|
|
|
unset($cacheControlHeaders, $header, $value);
|
|
|
|
|
2014-04-24 07:18:07 +02:00
|
|
|
if(self::$modification_date && $cacheAge > 0) {
|
2011-09-30 06:14:19 +02:00
|
|
|
$responseHeaders["Last-Modified"] = self::gmt_date(self::$modification_date);
|
|
|
|
|
2013-02-18 14:41:49 +01:00
|
|
|
// Chrome ignores Varies when redirecting back (http://code.google.com/p/chromium/issues/detail?id=79758)
|
2014-08-15 08:53:05 +02:00
|
|
|
// which means that if you log out, you get redirected back to a page which Chrome then checks against
|
2013-02-18 14:41:49 +01:00
|
|
|
// last-modified (which passes, getting a 304)
|
|
|
|
// when it shouldn't be trying to use that page at all because it's the "logged in" version.
|
2014-08-15 08:53:05 +02:00
|
|
|
// By also using and etag that includes both the modification date and all the varies
|
2013-02-18 14:41:49 +01:00
|
|
|
// values which we also check against we can catch this and not return a 304
|
2011-09-30 06:14:19 +02:00
|
|
|
$etagParts = array(self::$modification_date, serialize($_COOKIE));
|
2014-05-22 08:34:15 +02:00
|
|
|
$etagParts[] = Director::is_https() ? 'https' : 'http';
|
2011-09-30 06:14:19 +02:00
|
|
|
if (isset($_SERVER['HTTP_USER_AGENT'])) $etagParts[] = $_SERVER['HTTP_USER_AGENT'];
|
|
|
|
if (isset($_SERVER['HTTP_ACCEPT'])) $etagParts[] = $_SERVER['HTTP_ACCEPT'];
|
|
|
|
|
|
|
|
$etag = sha1(implode(':', $etagParts));
|
|
|
|
$responseHeaders["ETag"] = $etag;
|
2008-10-16 05:02:29 +02:00
|
|
|
|
|
|
|
// 304 response detection
|
|
|
|
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
|
|
|
|
$ifModifiedSince = strtotime(stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']));
|
2011-09-30 06:14:19 +02:00
|
|
|
|
|
|
|
// As above, only 304 if the last request had all the same varies values
|
|
|
|
// (or the etag isn't passed as part of the request - but with chrome it always is)
|
|
|
|
$matchesEtag = !isset($_SERVER['HTTP_IF_NONE_MATCH']) || $_SERVER['HTTP_IF_NONE_MATCH'] == $etag;
|
|
|
|
|
|
|
|
if($ifModifiedSince >= self::$modification_date && $matchesEtag) {
|
2008-10-16 05:02:29 +02:00
|
|
|
if($body) {
|
|
|
|
$body->setStatusCode(304);
|
|
|
|
$body->setBody('');
|
|
|
|
} else {
|
|
|
|
header('HTTP/1.0 304 Not Modified');
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2014-04-24 07:18:07 +02:00
|
|
|
$expires = time() + $cacheAge;
|
2008-10-16 05:02:29 +02:00
|
|
|
$responseHeaders["Expires"] = self::gmt_date($expires);
|
|
|
|
}
|
2007-09-16 17:54:16 +02:00
|
|
|
|
2008-10-16 05:02:29 +02:00
|
|
|
if(self::$etag) {
|
|
|
|
$responseHeaders['ETag'] = self::$etag;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
// Now that we've generated them, either output them or attach them to the SS_HTTPResponse as appropriate
|
2008-10-16 05:02:29 +02:00
|
|
|
foreach($responseHeaders as $k => $v) {
|
2015-07-12 10:36:39 +02:00
|
|
|
if($body) {
|
|
|
|
// Set the header now if it's not already set.
|
|
|
|
if ($body->getHeader($k) === null) {
|
|
|
|
$body->addHeader($k, $v);
|
|
|
|
}
|
|
|
|
} elseif(!headers_sent()) {
|
|
|
|
header("$k: $v");
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Return an {@link http://www.faqs.org/rfcs/rfc2822 RFC 2822} date in the GMT timezone (a timestamp
|
|
|
|
* is always in GMT: the number of seconds since January 1 1970 00:00:00 GMT)
|
|
|
|
*
|
|
|
|
* @param int $timestamp
|
|
|
|
*
|
|
|
|
* @return string
|
2007-09-16 17:53:35 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function gmt_date($timestamp) {
|
2007-09-16 17:53:35 +02:00
|
|
|
return gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
/**
|
2008-12-15 02:30:41 +01:00
|
|
|
* Return static variable cache_age in second
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
|
|
|
* @return int
|
2008-12-15 02:30:41 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_cache_age() {
|
2008-12-15 02:30:41 +01:00
|
|
|
return self::$cache_age;
|
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|