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
|
|
|
/**
|
|
|
|
* A class with HTTP-related helpers.
|
|
|
|
* Like Debug, this is more a bundle of methods than a class ;-)
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
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
|
|
|
/**
|
|
|
|
* @var int $cache_age
|
|
|
|
*/
|
2012-05-11 01:04:51 +02:00
|
|
|
protected static $cache_age = 0;
|
|
|
|
|
2013-05-10 11:59:20 +02:00
|
|
|
/**
|
|
|
|
* @var timestamp $modification_date
|
|
|
|
*/
|
2012-05-11 01:04:51 +02:00
|
|
|
protected static $modification_date = null;
|
|
|
|
|
2013-05-10 11:59:20 +02:00
|
|
|
/**
|
|
|
|
* @var string $etag
|
|
|
|
*/
|
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
|
|
|
/**
|
2013-05-10 11:59:20 +02:00
|
|
|
* Turns a local system filename into a URL by comparing it to the script
|
|
|
|
* filename.
|
|
|
|
*
|
|
|
|
* @param 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) {
|
|
|
|
if(substr($filename, 0, $slashPos) == substr($_SERVER['SCRIPT_FILENAME'],0,$slashPos)) {
|
|
|
|
$commonLength = $slashPos;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2013-05-10 11:59:20 +02:00
|
|
|
$urlBase = substr(
|
|
|
|
$_SERVER['PHP_SELF'],
|
|
|
|
0,
|
|
|
|
-(strlen($_SERVER['SCRIPT_FILENAME']) - $commonLength)
|
|
|
|
);
|
|
|
|
|
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.
|
|
|
|
// $prefix = str_repeat("../", substr_count(substr($_SERVER[SCRIPT_FILENAME],$commonBaseLength)));
|
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
|
|
|
/**
|
|
|
|
* Turn all relative URLs in the content to absolute URLs
|
|
|
|
*/
|
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 01:59:04 +02:00
|
|
|
//no need to rewrite, if uri has a protocol
|
|
|
|
//(determined here by existence of reserved URI character ":")
|
|
|
|
if(stripos($url, ":") !== false){
|
|
|
|
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
|
|
|
*
|
|
|
|
* Put $URL where you want the URL to appear, however, you can't embed $URL in strings
|
|
|
|
* Some example code:
|
2013-01-23 23:35:27 +01:00
|
|
|
* <ul>
|
|
|
|
* <li><code>'"../../" . $URL'</code></li>
|
|
|
|
* <li><code>'myRewriter($URL)'</code></li>
|
|
|
|
* <li><code>'(substr($URL,0,1)=="/") ? "../" . substr($URL,1) : $URL'</code></li>
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* As of 3.2 $code should be a callable which takes a single parameter and returns
|
|
|
|
* the rewritten URL. e.g.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* function($url) {
|
|
|
|
* return Director::absoluteURL($url, true);
|
|
|
|
* }
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* @return 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)) {
|
|
|
|
Deprecation::notice(3.1, 'HTTP::urlRewriter expects a callable as the second parameter');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace attributes
|
2007-07-19 12:40:28 +02:00
|
|
|
$attribs = array("src","background","a" => "href","link" => "href", "base" => "href");
|
|
|
|
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) {
|
2013-01-23 23:35:27 +01:00
|
|
|
if(is_callable($code)) {
|
|
|
|
$rewritten = $code($matches[2]);
|
|
|
|
} else {
|
|
|
|
// Expose the $URL variable to be used by the $code expression
|
|
|
|
$URL = $matches[2];
|
|
|
|
$rewritten = eval("return ($code);");
|
|
|
|
}
|
|
|
|
return $matches[1] . $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
|
|
|
/**
|
|
|
|
* 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'].
|
2010-10-04 06:16:47 +02:00
|
|
|
* 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 '&'.
|
2010-04-13 05:21:33 +02: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.
|
|
|
|
*
|
|
|
|
* @param String $varname
|
|
|
|
* @param String $varvalue
|
|
|
|
* @param String $currentURL Relative or absolute URL (Optional).
|
2010-10-13 03:48:24 +02:00
|
|
|
* @param String $separator Separator for http_build_query(). (Optional).
|
2010-04-13 05:21:33 +02:00
|
|
|
* @return String Absolute URL
|
|
|
|
*/
|
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;
|
2010-10-13 03:48:24 +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'] . '@' : '@';
|
|
|
|
}
|
|
|
|
|
|
|
|
$host = (isset($parts['host'])) ? $parts['host'] : '';
|
|
|
|
$port = (isset($parts['port']) && $parts['port'] != '') ? ':'.$parts['port'] : '';
|
|
|
|
$path = (isset($parts['path']) && $parts['path'] != '') ? $parts['path'] : '';
|
|
|
|
|
|
|
|
// handle URL params which are existing / new
|
2010-10-13 03:48:24 +02:00
|
|
|
$params = ($params) ? '?' . http_build_query($params, null, $separator) : '';
|
2010-10-13 02:46:12 +02:00
|
|
|
|
2010-10-13 03:08:10 +02:00
|
|
|
// keep fragments (anchors) intact.
|
|
|
|
$fragment = (isset($parts['fragment']) && $parts['fragment'] != '') ? '#'.$parts['fragment'] : '';
|
|
|
|
|
|
|
|
// Recompile URI segments
|
|
|
|
$newUri = $scheme . '://' . $user . $host . $port . $path . $params . $fragment;
|
|
|
|
|
2010-10-13 02:46:12 +02:00
|
|
|
if($isRelative) return Director::makeRelative($newUri);
|
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
|
|
|
|
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);
|
|
|
|
}
|
2009-10-11 02:06:55 +02:00
|
|
|
|
2010-04-13 05:21:33 +02:00
|
|
|
/**
|
|
|
|
* Search for all tags with a specific attribute, then return the value of that attribute in a flat array.
|
|
|
|
*
|
|
|
|
* @param string $content
|
|
|
|
* @param array $attributes an array of tags to attributes, for example "[a] => 'href', [div] => 'id'"
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function findByTagAndAttribute($content, $attributes) {
|
|
|
|
$regexes = array();
|
|
|
|
|
|
|
|
foreach($attributes as $tag => $attribute) {
|
|
|
|
$regexes[] = "/<{$tag} [^>]*$attribute *= *([\"'])(.*?)\\1[^>]*>/i";
|
|
|
|
$regexes[] = "/<{$tag} [^>]*$attribute *= *([^ \"'>]+)/i";
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2010-04-13 05:21:33 +02:00
|
|
|
|
|
|
|
return count($result) ? $result : null;
|
|
|
|
}
|
2009-10-11 02:06:55 +02:00
|
|
|
|
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"));
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
|
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"));
|
|
|
|
}
|
2009-05-20 07:16:00 +02:00
|
|
|
|
2012-05-11 01:33:21 +02:00
|
|
|
/**
|
2012-05-17 23:22:02 +02:00
|
|
|
* Get the MIME type based on a file's extension.
|
|
|
|
*
|
|
|
|
* If the finfo class exists in PHP, and the file actually exists, then use that
|
|
|
|
* extension, otherwise fallback to a list of commonly known MIME types.
|
2012-05-11 04:05:40 +02:00
|
|
|
*
|
2012-05-11 01:33:21 +02:00
|
|
|
* @uses finfo
|
2012-05-11 04:05:40 +02:00
|
|
|
* @param string $filename Relative path to filename from project root, e.g. "mysite/tests/file.csv"
|
|
|
|
* @return string MIME type
|
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
|
|
|
/**
|
|
|
|
* Set the maximum age of this page in web caches, in seconds
|
|
|
|
*/
|
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
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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.
|
2007-09-16 17:53:35 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param SS_HTTPResponse The SS_HTTPResponse object to augment. Omitted the argument or passing a string is
|
|
|
|
* deprecated; in these cases, the headers are output directly.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function add_cache_headers($body = null) {
|
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.
|
|
|
|
if(Director::isDev()) return;
|
2008-10-16 05:02:29 +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
|
|
|
|
|
|
|
// Popuplate $responseHeaders with all the headers that we want to build
|
2008-10-16 05:02:29 +02:00
|
|
|
$responseHeaders = array();
|
|
|
|
if(function_exists('apache_request_headers')) {
|
|
|
|
$requestHeaders = apache_request_headers();
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($requestHeaders['X-Requested-With']) && $requestHeaders['X-Requested-With']=='XMLHttpRequest') {
|
|
|
|
self::$cache_age = 0;
|
|
|
|
}
|
2008-10-16 05:02:29 +02:00
|
|
|
// bdc: now we must check for DUMB IE6:
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($requestHeaders['x-requested-with']) && $requestHeaders['x-requested-with']=='XMLHttpRequest') {
|
|
|
|
self::$cache_age = 0;
|
|
|
|
}
|
2008-10-16 05:02:29 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2008-10-16 05:02:29 +02:00
|
|
|
if(self::$cache_age > 0) {
|
2011-09-29 05:21:32 +02:00
|
|
|
$responseHeaders["Cache-Control"] = "max-age=" . self::$cache_age . ", must-revalidate, no-transform";
|
2008-10-16 05:02:29 +02:00
|
|
|
$responseHeaders["Pragma"] = "";
|
2011-07-07 07:28:58 +02:00
|
|
|
|
2013-02-18 14:41:49 +01:00
|
|
|
// To do: User-Agent should only be added in situations where you *are* actually
|
|
|
|
// varying according to user-agent.
|
2011-09-30 06:14:19 +02:00
|
|
|
$responseHeaders['Vary'] = 'Cookie, X-Forwarded-Protocol, User-Agent, Accept';
|
2011-07-07 07:28:58 +02:00
|
|
|
|
2008-10-16 05:02:29 +02:00
|
|
|
} else {
|
2011-09-29 05:21:32 +02:00
|
|
|
$responseHeaders["Cache-Control"] = "no-cache, max-age=0, must-revalidate, no-transform";
|
2008-10-16 05:02:29 +02:00
|
|
|
}
|
2007-09-16 17:53:35 +02:00
|
|
|
|
2008-10-16 05:02:29 +02:00
|
|
|
if(self::$modification_date && self::$cache_age > 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)
|
|
|
|
// which means that if you log out, you get redirected back to a page which Chrome then checks against
|
|
|
|
// 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.
|
|
|
|
// By also using and etag that includes both the modification date and all the varies
|
|
|
|
// 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));
|
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_PROTOCOL'])) $etagParts[] = $_SERVER['HTTP_X_FORWARDED_PROTOCOL'];
|
|
|
|
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
|
|
|
|
2008-10-16 05:02:29 +02:00
|
|
|
$expires = time() + self::$cache_age;
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if($body) $body->addHeader($k, $v);
|
2008-12-15 02:30:41 +01:00
|
|
|
else if(!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
|
|
|
/**
|
2007-09-16 17:53:35 +02: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)
|
|
|
|
*/
|
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
|
|
|
}
|
2008-12-15 02:30:41 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return static variable cache_age in second
|
|
|
|
*/
|
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
|
|
|
|