From 4a5443c5da07088683e66e72b8503ae4d862510c Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Tue, 22 Apr 2008 01:45:55 +0000 Subject: [PATCH] ENHANCEMENT: Ticket #2382 - Merged patch changing 302 for /home to 301 git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@53177 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/control/ContentController.php | 2 +- core/control/Controller.php | 4 ++-- core/control/Director.php | 4 ++-- core/control/HTTPResponse.php | 16 +++++++++++++--- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/core/control/ContentController.php b/core/control/ContentController.php index 391d69dce..c4139b70e 100644 --- a/core/control/ContentController.php +++ b/core/control/ContentController.php @@ -88,7 +88,7 @@ class ContentController extends Controller { unset($getVars['url']); if($getVars) $url = "?" . http_build_query($getVars); else $url = ""; - Director::redirect($url); + Director::redirect($url, 301); return; } diff --git a/core/control/Controller.php b/core/control/Controller.php index f3706ff8b..a75fb3d07 100644 --- a/core/control/Controller.php +++ b/core/control/Controller.php @@ -509,7 +509,7 @@ class Controller extends ViewableData { * Redirct to the given URL. * It is generally recommended to call Director::redirect() rather than calling this function directly. */ - function redirect($url) { + function redirect($url, $code=302) { if($this->response->getHeader('Location')) { user_error("Already directed to " . $this->response->getHeader('Location') . "; now trying to direct to $url", E_USER_ERROR); } @@ -519,7 +519,7 @@ class Controller extends ViewableData { $url = Director::baseURL() . $url; } - $this->response->redirect($url); + $this->response->redirect($url, $code); } /** diff --git a/core/control/Director.php b/core/control/Director.php index 6acf6968d..e720e5770 100644 --- a/core/control/Director.php +++ b/core/control/Director.php @@ -285,8 +285,8 @@ class Director { * - or it can be a URL relative to the "site base" * - if it is just a word without an slashes, then it redirects to another action on the current controller. */ - static function redirect($url) { - Controller::curr()->redirect($url); + static function redirect($url, $code=302) { + Controller::curr()->redirect($url, $code); } /** diff --git a/core/control/HTTPResponse.php b/core/control/HTTPResponse.php index 10da9cd77..84bd384c6 100644 --- a/core/control/HTTPResponse.php +++ b/core/control/HTTPResponse.php @@ -53,6 +53,15 @@ class HTTPResponse extends Object { 505 => 'HTTP Version Not Supported', ); + protected static $redirect_codes = array( + 301, + 302, + 303, + 304, + 305, + 307 + ); + protected $statusCode = 200; protected $headers = array(); protected $body = null; @@ -91,8 +100,9 @@ class HTTPResponse extends Object { } } - function redirect($dest) { - $this->statusCode = 302; + function redirect($dest, $code=302) { + if(!in_array($code, self::$redirect_codes)) $code = 302; + $this->statusCode = $code; $this->headers['Location'] = $dest; } @@ -100,7 +110,7 @@ class HTTPResponse extends Object { * Send this HTTPReponse to the browser */ function output() { - if($this->statusCode == 302 && headers_sent($file, $line)) { + if(in_array($this->statusCode, self::$redirect_codes) && headers_sent($file, $line)) { $url = $this->headers['Location']; echo "

Redirecting to $url... (output started on $file, line $line)