2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Director is responsible for processing URLs, and providing environment information.
|
|
|
|
*
|
|
|
|
* The most important part of director is {@link Director::direct()}, which is passed a URL and will execute the appropriate
|
|
|
|
* controller.
|
|
|
|
*
|
|
|
|
* Director also has a number of static methods that provide information about the environment, such as {@link Director::set_environment_type()}.
|
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage control
|
|
|
|
* @see Director::direct(),Director::addRules(),Director::set_environment_type()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class Director {
|
2007-11-10 06:17:30 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static private $urlParams;
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static private $rules = array();
|
2007-11-10 06:17:30 +01:00
|
|
|
|
2009-10-11 02:07:10 +02:00
|
|
|
/**
|
|
|
|
* @var SiteTree
|
|
|
|
*/
|
|
|
|
private static $current_page;
|
|
|
|
|
2009-06-25 10:15:52 +02:00
|
|
|
/**
|
|
|
|
* @deprecated 2.4
|
|
|
|
*/
|
2007-11-10 06:17:30 +01:00
|
|
|
static $siteMode;
|
|
|
|
|
|
|
|
static $alternateBaseFolder;
|
|
|
|
|
|
|
|
static $alternateBaseURL;
|
|
|
|
|
|
|
|
static $dev_servers = array(
|
|
|
|
'localhost',
|
|
|
|
'127.0.0.1'
|
|
|
|
);
|
|
|
|
|
|
|
|
static $test_servers = array();
|
|
|
|
|
|
|
|
static protected $environment_type;
|
|
|
|
|
|
|
|
/**
|
2009-06-25 10:15:52 +02:00
|
|
|
* @deprecated 2.4
|
2007-11-10 06:17:30 +01:00
|
|
|
*/
|
|
|
|
static protected $callbacks;
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function __construct() {
|
|
|
|
if(isset($_GET['debug_profile'])) Profiler::mark("Director", "construct");
|
|
|
|
Session::addToArray('history', substr($_SERVER['REQUEST_URI'], strlen(Director::baseURL())));
|
|
|
|
if(isset($_GET['debug_profile'])) Profiler::unmark("Director", "construct");
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return a URL from this user's navigation history.
|
|
|
|
* @param pagesBack The number of pages back to go. The default, 1, returns the previous
|
|
|
|
* page.
|
|
|
|
*/
|
|
|
|
static function history($pagesBack = 1) {
|
2009-01-08 00:00:54 +01:00
|
|
|
return Session::get('history.' . intval(sizeof(Session::get('history')) - $pagesBack - 1));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Add URL matching rules to the Director.
|
|
|
|
*
|
|
|
|
* The director is responsible for turning URLs into Controller objects. It does thi
|
|
|
|
*
|
|
|
|
* @param $priority The priority of the rules; higher values will get your rule checked first.
|
|
|
|
* We recommend priority 100 for your site's rules. The built-in rules are priority 10, standard modules are priority 50.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function addRules($priority, $rules) {
|
|
|
|
Director::$rules[$priority] = isset(Director::$rules[$priority]) ? array_merge($rules, (array)Director::$rules[$priority]) : $rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Process the given URL, creating the appropriate controller and executing it.
|
|
|
|
*
|
2008-08-09 05:19:54 +02:00
|
|
|
* Request processing is handled as folows:
|
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
|
|
|
* - Director::direct() creates a new SS_HTTPResponse object and passes this to Director::handleRequest().
|
2008-08-09 05:19:54 +02:00
|
|
|
* - Director::handleRequest($request) checks each of the Director rules and identifies a controller to handle this
|
|
|
|
* request.
|
|
|
|
* - Controller::handleRequest($request) is then called. This will find a rule to handle the URL, and call the rule
|
|
|
|
* handling method.
|
2008-10-30 23:03:21 +01:00
|
|
|
* - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method returns a
|
|
|
|
* RequestHandler object.
|
2008-08-09 05:19:54 +02:00
|
|
|
*
|
|
|
|
* In addition to request processing, Director will manage the session, and perform the output of the actual response
|
|
|
|
* to the browser.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* @param $url String, the URL the user is visiting, without the querystring.
|
2008-08-09 05:19:54 +02:00
|
|
|
* @uses handleRequest() rule-lookup logic is handled by this.
|
2008-02-25 03:10:37 +01:00
|
|
|
* @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call.
|
2007-09-14 19:47:45 +02:00
|
|
|
*/
|
2008-12-04 23:38:32 +01:00
|
|
|
static function direct($url) {
|
2009-03-17 23:22:55 +01:00
|
|
|
// Validate $_FILES array before merging it with $_POST
|
|
|
|
foreach($_FILES as $k => $v) {
|
2009-03-18 11:16:15 +01:00
|
|
|
if(is_array($v['tmp_name'])) {
|
|
|
|
foreach($v['tmp_name'] as $tmpFile) {
|
|
|
|
if($tmpFile && !is_uploaded_file($tmpFile)) {
|
|
|
|
user_error("File upload '$k' doesn't appear to be a valid upload", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if($v['tmp_name'] && !is_uploaded_file($v['tmp_name'])) {
|
|
|
|
user_error("File upload '$k' doesn't appear to be a valid upload", E_USER_ERROR);
|
|
|
|
}
|
2009-03-17 23:22:55 +01: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
|
|
|
$req = new SS_HTTPRequest(
|
2008-10-06 16:58:01 +02:00
|
|
|
(isset($_SERVER['X-HTTP-Method-Override'])) ? $_SERVER['X-HTTP-Method-Override'] : $_SERVER['REQUEST_METHOD'],
|
2008-08-09 09:03:24 +02:00
|
|
|
$url,
|
|
|
|
$_GET,
|
|
|
|
array_merge((array)$_POST, (array)$_FILES),
|
|
|
|
@file_get_contents('php://input')
|
|
|
|
);
|
|
|
|
|
|
|
|
// @todo find better way to extract HTTP headers
|
|
|
|
if(isset($_SERVER['HTTP_ACCEPT'])) $req->addHeader("Accept", $_SERVER['HTTP_ACCEPT']);
|
|
|
|
if(isset($_SERVER['CONTENT_TYPE'])) $req->addHeader("Content-Type", $_SERVER['CONTENT_TYPE']);
|
2008-10-14 21:59:01 +02:00
|
|
|
if(isset($_SERVER['HTTP_REFERER'])) $req->addHeader("Referer", $_SERVER['HTTP_REFERER']);
|
2008-08-09 05:19:54 +02:00
|
|
|
|
|
|
|
// Load the session into the controller
|
2009-08-08 05:10:20 +02:00
|
|
|
$session = new Session(isset($_SESSION) ? $_SESSION : null);
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
$result = Director::handleRequest($req, $session);
|
|
|
|
$session->inst_save();
|
|
|
|
|
|
|
|
// Return code for a redirection request
|
|
|
|
if(is_string($result) && substr($result,0,9) == 'redirect:') {
|
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
|
|
|
$response = new SS_HTTPResponse();
|
2008-08-09 05:19:54 +02:00
|
|
|
$response->redirect(substr($result, 9));
|
2008-02-25 03:10:37 +01:00
|
|
|
$response->output();
|
2007-09-16 17:31:44 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// Handle a controller
|
|
|
|
} else if($result) {
|
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($result instanceof SS_HTTPResponse) {
|
2008-08-09 05:19:54 +02:00
|
|
|
$response = $result;
|
|
|
|
|
|
|
|
} else {
|
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
|
|
|
$response = new SS_HTTPResponse();
|
2008-08-09 05:19:54 +02:00
|
|
|
$response->setBody($result);
|
|
|
|
}
|
2008-10-17 02:52:58 +02:00
|
|
|
|
|
|
|
// ?debug_memory=1 will output the number of bytes of memory used for this request
|
|
|
|
if(isset($_REQUEST['debug_memory']) && $_REQUEST['debug_memory']) {
|
2009-04-03 20:40:19 +02:00
|
|
|
Debug::message(sprintf(
|
|
|
|
"Peak memory usage in bytes: %s",
|
|
|
|
number_format(memory_get_peak_usage(),0)
|
|
|
|
));
|
2008-10-17 02:52:58 +02:00
|
|
|
} else {
|
|
|
|
$response->output();
|
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
|
|
|
|
//$controllerObj->getSession()->inst_save();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-08-21 00:39:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a URL request, returning a response object.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* This method is the counterpart of Director::direct() that is used in functional testing. It will execute the URL given,
|
|
|
|
*
|
2008-08-09 09:03:24 +02:00
|
|
|
* @param string $url The URL to visit
|
|
|
|
* @param array $postVars The $_POST & $_FILES variables
|
|
|
|
* @param Session $session The {@link Session} object representing the current session. By passing the same object to multiple
|
2008-02-25 03:10:37 +01:00
|
|
|
* calls of Director::test(), you can simulate a peristed session.
|
2008-10-06 16:58:01 +02:00
|
|
|
* @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if postVars is set, GET otherwise.
|
|
|
|
* Overwritten by $postVars['_method'] if present.
|
2008-08-09 09:03:24 +02:00
|
|
|
* @param string $body The HTTP body
|
|
|
|
* @param array $headers HTTP headers with key-value pairs
|
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
|
|
|
* @return SS_HTTPResponse
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* @uses getControllerForURL() The rule-lookup logic is handled by this.
|
|
|
|
* @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call.
|
2007-08-21 00:39:44 +02:00
|
|
|
*/
|
2008-12-04 23:38:32 +01:00
|
|
|
static function test($url, $postVars = null, $session = null, $httpMethod = null, $body = null, $headers = null) {
|
2008-10-08 05:37:33 +02:00
|
|
|
// These are needed so that calling Director::test() doesnt muck with whoever is calling it.
|
|
|
|
// Really, it's some inapproriate coupling and should be resolved by making less use of statics
|
|
|
|
$oldStage = Versioned::current_stage();
|
2008-12-04 23:38:32 +01:00
|
|
|
$getVars = array();
|
2008-10-08 05:37:33 +02:00
|
|
|
|
2008-10-14 21:55:55 +02:00
|
|
|
if(!$httpMethod) $httpMethod = ($postVars || is_array($postVars)) ? "POST" : "GET";
|
2008-08-09 05:19:54 +02:00
|
|
|
|
|
|
|
if(!$session) $session = new Session(null);
|
2008-08-11 07:26:51 +02:00
|
|
|
|
|
|
|
// Back up the current values of the superglobals
|
|
|
|
$existingRequestVars = $_REQUEST;
|
|
|
|
$existingGetVars = $_GET;
|
|
|
|
$existingPostVars = $_POST;
|
|
|
|
$existingSessionVars = $_SESSION;
|
2008-11-22 04:33:00 +01:00
|
|
|
$existingCookies = $_COOKIE;
|
2009-01-08 00:00:54 +01:00
|
|
|
$existingServer = $_SERVER;
|
2008-11-22 04:33:00 +01:00
|
|
|
$existingCookieReportErrors = Cookie::report_errors();
|
|
|
|
$existingRequirementsBackend = Requirements::backend();
|
2009-01-08 00:00:54 +01:00
|
|
|
|
|
|
|
Cookie::set_report_errors(false);
|
2008-11-22 04:33:00 +01:00
|
|
|
Requirements::set_backend(new Requirements_Backend());
|
2008-08-11 07:26:51 +02:00
|
|
|
|
2009-07-13 06:15:40 +02:00
|
|
|
// Handle absolute URLs
|
|
|
|
if (@parse_url($url, PHP_URL_HOST) != '') {
|
|
|
|
$bits = parse_url($url);
|
|
|
|
$_SERVER['HTTP_HOST'] = $bits['host'];
|
2009-10-16 00:24:29 +02:00
|
|
|
$url = Director::makeRelative($url);
|
2009-07-13 06:15:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$urlWithQuerystring = $url;
|
|
|
|
if(strpos($url, '?') !== false) {
|
|
|
|
list($url, $getVarsEncoded) = explode('?', $url, 2);
|
|
|
|
parse_str($getVarsEncoded, $getVars);
|
|
|
|
}
|
|
|
|
|
2008-08-11 07:26:51 +02:00
|
|
|
// Replace the superglobals with appropriate test values
|
2008-08-11 07:55:31 +02:00
|
|
|
$_REQUEST = array_merge((array)$getVars, (array)$postVars);
|
2008-08-11 07:26:51 +02:00
|
|
|
$_GET = (array)$getVars;
|
2008-08-11 07:55:31 +02:00
|
|
|
$_POST = (array)$postVars;
|
2008-08-11 07:26:51 +02:00
|
|
|
$_SESSION = $session ? $session->inst_getAll() : array();
|
2008-11-22 04:33:00 +01:00
|
|
|
$_COOKIE = array();
|
2009-01-08 00:00:54 +01:00
|
|
|
$_SERVER['REQUEST_URI'] = Director::baseURL() . $urlWithQuerystring;
|
2008-08-11 07:26:51 +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
|
|
|
$req = new SS_HTTPRequest($httpMethod, $url, $getVars, $postVars, $body);
|
2008-08-09 09:03:24 +02:00
|
|
|
if($headers) foreach($headers as $k => $v) $req->addHeader($k, $v);
|
2008-08-09 05:19:54 +02:00
|
|
|
$result = Director::handleRequest($req, $session);
|
2008-08-11 07:55:31 +02:00
|
|
|
|
2008-08-11 07:26:51 +02:00
|
|
|
// Restore the superglobals
|
|
|
|
$_REQUEST = $existingRequestVars;
|
|
|
|
$_GET = $existingGetVars;
|
|
|
|
$_POST = $existingPostVars;
|
2008-10-14 21:59:01 +02:00
|
|
|
$_SESSION = $existingSessionVars;
|
2008-11-22 04:33:00 +01:00
|
|
|
$_COOKIE = $existingCookies;
|
2009-01-08 00:00:54 +01:00
|
|
|
$_SERVER = $existingServer;
|
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
Cookie::set_report_errors($existingCookieReportErrors);
|
|
|
|
Requirements::set_backend($existingRequirementsBackend);
|
2008-10-08 05:37:33 +02:00
|
|
|
|
|
|
|
// These are needed so that calling Director::test() doesnt muck with whoever is calling it.
|
|
|
|
// Really, it's some inapproriate coupling and should be resolved by making less use of statics
|
|
|
|
Versioned::reading_stage($oldStage);
|
2007-08-21 00:39:44 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
return $result;
|
2007-08-21 00:39:44 +02:00
|
|
|
}
|
|
|
|
|
2008-03-03 00:24:10 +01: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
|
|
|
* Handle an HTTP request, defined with a SS_HTTPRequest object.
|
2008-08-11 05:03:52 +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
|
|
|
* @return SS_HTTPResponse|string
|
2008-03-03 00:24:10 +01: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
|
|
|
protected static function handleRequest(SS_HTTPRequest $request, Session $session) {
|
2007-07-19 12:40:28 +02:00
|
|
|
krsort(Director::$rules);
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($_REQUEST['debug'])) Debug::show(Director::$rules);
|
2007-09-14 19:47:45 +02:00
|
|
|
foreach(Director::$rules as $priority => $rules) {
|
2008-08-09 05:19:54 +02:00
|
|
|
foreach($rules as $pattern => $controllerOptions) {
|
|
|
|
if(is_string($controllerOptions)) {
|
|
|
|
if(substr($controllerOptions,0,2) == '->') $controllerOptions = array('Redirect' => substr($controllerOptions,2));
|
|
|
|
else $controllerOptions = array('Controller' => $controllerOptions);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
|
|
|
|
if(($arguments = $request->match($pattern, true)) !== false) {
|
|
|
|
// controllerOptions provide some default arguments
|
|
|
|
$arguments = array_merge($controllerOptions, $arguments);
|
|
|
|
|
|
|
|
// Find the controller name
|
|
|
|
if(isset($arguments['Controller'])) $controller = $arguments['Controller'];
|
|
|
|
|
|
|
|
// Pop additional tokens from the tokeniser if necessary
|
|
|
|
if(isset($controllerOptions['_PopTokeniser'])) {
|
|
|
|
$request->shift($controllerOptions['_PopTokeniser']);
|
|
|
|
}
|
2008-10-14 21:59:01 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// Handle redirections
|
|
|
|
if(isset($arguments['Redirect'])) {
|
|
|
|
return "redirect:" . Director::absoluteURL($arguments['Redirect'], true);
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
Director::$urlParams = $arguments;
|
2008-08-09 05:19:54 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$controllerObj = new $controller();
|
2008-08-09 05:19:54 +02:00
|
|
|
$controllerObj->setSession($session);
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
return $controllerObj->handleRequest($request);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-11 02:07:10 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the urlParam with the given name
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function urlParam($name) {
|
2008-03-11 02:01:54 +01:00
|
|
|
if(isset(Director::$urlParams[$name])) return Director::$urlParams[$name];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-11-10 06:17:30 +01:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
|
|
|
* Returns an array of urlParams
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function urlParams() {
|
|
|
|
return Director::$urlParams;
|
|
|
|
}
|
2009-10-11 02:07:10 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2009-10-11 02:07:10 +02:00
|
|
|
* Return the {@link SiteTree} object that is currently being viewed. If there is no sitetree object to return,
|
|
|
|
* then this will return the current controller.
|
|
|
|
*
|
|
|
|
* @return SiteTree
|
|
|
|
*/
|
|
|
|
public static function get_current_page() {
|
|
|
|
return self::$current_page ? self::$current_page : Controller::curr();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the currently active {@link SiteTree} object that is being used to respond to the request.
|
|
|
|
*
|
|
|
|
* @param SiteTree $page
|
|
|
|
*/
|
|
|
|
public static function set_current_page($page) {
|
|
|
|
self::$current_page = $page;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 2.4 Use {@link Director::get_current_page()}.
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function currentPage() {
|
2009-10-11 02:07:10 +02:00
|
|
|
user_error (
|
|
|
|
'Director::currentPage() is deprecated, please use Director::get_current_page()', E_USER_NOTICE
|
|
|
|
);
|
|
|
|
|
|
|
|
return self::get_current_page();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
|
|
|
* Turns the given URL into an absolute URL.
|
|
|
|
* @todo Document how relativeToSiteBase works
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function absoluteURL($url, $relativeToSiteBase = false) {
|
2008-09-27 19:02:46 +02:00
|
|
|
if(strpos($url,'/') === false && !$relativeToSiteBase) $url = dirname($_SERVER['REQUEST_URI'] . 'x') . '/' . $url;
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(substr($url,0,4) != "http") {
|
|
|
|
if($url[0] != "/") $url = Director::baseURL() . $url;
|
2008-09-30 01:41:50 +02:00
|
|
|
// Sometimes baseURL() can return a full URL instead of just a path
|
|
|
|
if(substr($url,0,4) != "http") $url = self::protocolAndHost() . $url;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the part of the URL, 'http://www.mysite.com'.
|
2008-09-29 14:52:23 +02:00
|
|
|
*
|
2008-11-10 00:25:02 +01:00
|
|
|
* @return boolean|string The domain from the PHP environment. Returns FALSE is this environment variable isn't set.
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function protocolAndHost() {
|
2007-09-25 05:44:07 +02:00
|
|
|
if(self::$alternateBaseURL) {
|
2008-09-30 01:41:50 +02:00
|
|
|
if(preg_match('/^(http[^:]*:\/\/[^\/]+)(\/|$)/', self::$alternateBaseURL, $matches)) {
|
2007-09-25 05:44:07 +02:00
|
|
|
return $matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-09 00:53:20 +02:00
|
|
|
$s = (isset($_SERVER['SSL']) || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off')) ? 's' : '';
|
2008-08-12 04:51:33 +02:00
|
|
|
|
2008-09-29 14:52:23 +02:00
|
|
|
if(isset($_SERVER['HTTP_HOST'])) {
|
|
|
|
return "http$s://" . $_SERVER['HTTP_HOST'];
|
|
|
|
} else {
|
2008-10-02 11:48:56 +02:00
|
|
|
global $_FILE_TO_URL_MAPPING;
|
|
|
|
if(Director::is_cli() && isset($_FILE_TO_URL_MAPPING)) $errorSuggestion = ' You probably want to define '.
|
|
|
|
'an entry in $_FILE_TO_URL_MAPPING that covers "' . Director::baseFolder() . '"';
|
|
|
|
else if(Director::is_cli()) $errorSuggestion = ' You probably want to define $_FILE_TO_URL_MAPPING in '.
|
|
|
|
'your _ss_environment.php as instructed on the "sake" page of the doc.silverstripe.com wiki';
|
|
|
|
else $errorSuggestion = "";
|
|
|
|
|
|
|
|
user_error("Director::protocolAndHost() lacks sufficient information - HTTP_HOST not set.$errorSuggestion", E_USER_WARNING);
|
2008-09-29 14:52:23 +02:00
|
|
|
return false;
|
|
|
|
|
2008-08-12 04:51:33 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Redirect to another page.
|
2007-08-31 02:26:41 +02:00
|
|
|
* - $url can be an absolute URL
|
|
|
|
* - 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.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-04-22 03:45:55 +02:00
|
|
|
static function redirect($url, $code=302) {
|
|
|
|
Controller::curr()->redirect($url, $code);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-08-31 02:26:41 +02:00
|
|
|
/**
|
|
|
|
* Tests whether a redirection has been requested.
|
|
|
|
* @return string If redirect() has been called, it will return the URL redirected to. Otherwise, it will return null;
|
|
|
|
*/
|
|
|
|
static function redirected_to() {
|
2008-02-25 03:10:37 +01:00
|
|
|
return Controller::curr()->redirectedTo();
|
2007-08-31 02:26:41 +02:00
|
|
|
}
|
2007-12-02 22:29:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the HTTP status code
|
|
|
|
*/
|
|
|
|
static function set_status_code($code) {
|
|
|
|
return Controller::curr()->getResponse()->setStatusCode($code);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current HTTP status code
|
|
|
|
*/
|
|
|
|
static function get_status_code() {
|
|
|
|
return Controller::curr()->getResponse()->getStatusCode();
|
|
|
|
}
|
2007-08-31 02:26:41 +02:00
|
|
|
|
2008-10-14 21:59:01 +02:00
|
|
|
/**
|
|
|
|
* @deprecated 2.3 Use Controller->redirectBack()
|
2007-09-16 17:34:05 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function redirectBack() {
|
2008-10-14 21:59:01 +02:00
|
|
|
Controller::curr()->redirectBack();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns a URL to composed of the given segments - usually controller, action, parameter
|
2008-10-16 10:42:44 +02:00
|
|
|
* @deprecated 2.3 Use Controller::join_links()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function link() {
|
|
|
|
$parts = func_get_args();
|
|
|
|
return Director::baseURL() . implode("/",$parts) . (sizeof($parts) > 2 ? "" : "/");
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-03-03 00:24:10 +01:00
|
|
|
* Returns the root URL for the site.
|
2008-09-27 19:02:46 +02:00
|
|
|
* It will be automatically calculated unless it is overridden with {@link setBaseURL()}.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function baseURL() {
|
2008-09-27 19:02:46 +02:00
|
|
|
if(self::$alternateBaseURL) return self::$alternateBaseURL;
|
|
|
|
else {
|
|
|
|
$base = dirname(dirname($_SERVER['SCRIPT_NAME']));
|
2009-03-10 23:08:52 +01:00
|
|
|
if($base == '/' || $base == '/.' || $base == '\\') $baseURL = '/';
|
|
|
|
else $baseURL = $base . '/';
|
|
|
|
|
|
|
|
if(defined('BASE_SCRIPT_URL')) return $baseURL . BASE_SCRIPT_URL;
|
|
|
|
else return $baseURL;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-11-10 06:17:30 +01:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
|
|
|
* Sets the root URL for the website.
|
|
|
|
* If the site isn't accessible from the URL you provide, weird things will happen.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function setBaseURL($baseURL) {
|
|
|
|
self::$alternateBaseURL = $baseURL;
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the root filesystem folder for the site.
|
|
|
|
* It will be automatically calculated unless it is overridden with {@link setBaseFolder()}.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function baseFolder() {
|
|
|
|
if(self::$alternateBaseFolder) return self::$alternateBaseFolder;
|
2008-09-27 19:02:46 +02:00
|
|
|
else return dirname(dirname($_SERVER['SCRIPT_FILENAME']));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-03-03 00:24:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the root folder for the website.
|
|
|
|
* If the site isn't accessible from the folder you provide, weird things will happen.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function setBaseFolder($baseFolder) {
|
|
|
|
self::$alternateBaseFolder = $baseFolder;
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
|
|
|
* Turns an absolute URL or folder into one that's relative to the root of the site.
|
|
|
|
* This is useful when turning a URL into a filesystem reference, or vice versa.
|
2009-03-17 23:22:55 +01:00
|
|
|
*
|
|
|
|
* @todo Implement checking across http/https protocols
|
|
|
|
*
|
|
|
|
* @param string $url Accepts both a URL or a filesystem path
|
|
|
|
* @return string Either a relative URL if the checks succeeded, or the original (possibly absolute) URL.
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function makeRelative($url) {
|
|
|
|
// Allow for the accidental inclusion of a // in the URL
|
|
|
|
$url = ereg_replace('([^:])//','\\1/',$url);
|
2009-03-17 23:22:55 +01:00
|
|
|
$url = trim($url);
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2008-10-02 11:48:56 +02:00
|
|
|
// Only bother comparing the URL to the absolute version if $url looks like a URL.
|
2009-03-17 23:22:55 +01:00
|
|
|
if(preg_match('/^https?[^:]*:\/\//',$url)) {
|
2008-10-02 11:48:56 +02:00
|
|
|
$base1 = self::absoluteBaseURL();
|
|
|
|
if(substr($url,0,strlen($base1)) == $base1) return substr($url,strlen($base1));
|
2009-10-16 00:24:29 +02:00
|
|
|
// Convert http://www.mydomain.com/mysitedir to ''
|
|
|
|
else if(substr($base1,-1)=="/" && $url == substr($base1,0,-1)) return "";
|
2008-10-02 11:48:56 +02:00
|
|
|
}
|
|
|
|
|
2009-03-17 23:22:55 +01:00
|
|
|
// test for base folder, e.g. /var/www
|
2008-10-02 11:48:56 +02:00
|
|
|
$base2 = self::baseFolder();
|
|
|
|
if(substr($url,0,strlen($base2)) == $base2) return substr($url,strlen($base2));
|
|
|
|
|
2009-03-17 23:22:55 +01:00
|
|
|
// Test for relative base url, e.g. mywebsite/ if the full URL is http://localhost/mywebsite/
|
2008-10-02 11:48:56 +02:00
|
|
|
$base3 = self::baseURL();
|
|
|
|
if(substr($url,0,strlen($base3)) == $base3) return substr($url,strlen($base3));
|
2008-05-26 08:21:30 +02:00
|
|
|
|
2009-03-17 23:22:55 +01:00
|
|
|
// Nothing matched, fall back to returning the original URL
|
2007-07-19 12:40:28 +02:00
|
|
|
return $url;
|
|
|
|
}
|
2008-10-12 18:16:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a given path is absolute. Works under both *nix and windows
|
|
|
|
* systems
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function is_absolute($path) {
|
|
|
|
if($path[0] == '/' || $path[0] == '\\') return true;
|
|
|
|
return preg_match('/^[a-zA-Z]:[\\\\\/]/', $path) == 1;
|
|
|
|
}
|
|
|
|
|
2009-02-11 22:07:58 +01:00
|
|
|
/**
|
|
|
|
* Checks if a given URL is absolute (e.g. starts with 'http://' etc.).
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function is_absolute_url($url) {
|
|
|
|
$url = trim($url);
|
|
|
|
// remove all query strings to avoid parse_url choking on URLs like 'test.com?url=http://test.com'
|
2009-03-17 23:22:55 +01:00
|
|
|
$url = preg_replace('/(.*)\?.*/', '$1', $url);
|
2009-02-11 22:07:58 +01:00
|
|
|
$parsed = parse_url($url);
|
|
|
|
return (isset($parsed['scheme']));
|
|
|
|
}
|
|
|
|
|
2009-03-17 23:22:55 +01:00
|
|
|
/**
|
|
|
|
* Checks if a given URL is relative by checking
|
|
|
|
* {@link is_absolute_url()}.
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function is_relative_url($url) {
|
|
|
|
return (!Director::is_absolute_url($url));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the given URL is belonging to this "site",
|
|
|
|
* as defined by {@link makeRelative()} and {@link absoluteBaseUrl()}.
|
|
|
|
* Useful to check before redirecting based on a URL from user submissions
|
|
|
|
* through $_GET or $_POST, and avoid phishing attacks by redirecting
|
|
|
|
* to an attackers server.
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function is_site_url($url) {
|
|
|
|
$relativeUrl = Director::makeRelative($url);
|
|
|
|
return (bool)self::is_relative_url($relativeUrl);
|
|
|
|
}
|
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2008-08-20 08:31:12 +02:00
|
|
|
* Given a filesystem reference relative to the site root, return the full file-system path.
|
2008-04-09 12:59:30 +02:00
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-10-12 18:16:25 +02:00
|
|
|
public static function getAbsFile($file) {
|
|
|
|
return self::is_absolute($file) ? $file : Director::baseFolder() . '/' . $file;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-11-10 06:17:30 +01:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the given file exists.
|
|
|
|
* @param $file Filename specified relative to the site root
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function fileExists($file) {
|
2008-04-06 06:00:43 +02:00
|
|
|
// replace any appended query-strings, e.g. /path/to/foo.php?bar=1 to /path/to/foo.php
|
|
|
|
$file = preg_replace('/([^\?]*)?.*/','$1',$file);
|
2007-07-19 12:40:28 +02:00
|
|
|
return file_exists(Director::getAbsFile($file));
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-03-03 00:24:10 +01:00
|
|
|
* Returns the Absolute URL of the site root.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function absoluteBaseURL() {
|
|
|
|
return Director::absoluteURL(Director::baseURL());
|
|
|
|
}
|
2007-11-10 06:17:30 +01:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the Absolute URL of the site root, embedding the current basic-auth credentials into the URL.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function absoluteBaseURLWithAuth() {
|
2008-08-11 05:03:52 +02:00
|
|
|
$s = "";
|
|
|
|
$login = "";
|
|
|
|
|
|
|
|
if(isset($_SERVER['PHP_AUTH_USER'])) $login = "$_SERVER[PHP_AUTH_USER]:$_SERVER[PHP_AUTH_PW]@";
|
|
|
|
if(isset($_SERVER['SSL']) && $_SERVER['SSL'] != 'Off') $s = "s";
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return "http$s://" . $login . $_SERVER['HTTP_HOST'] . Director::baseURL();
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-03-03 00:24:10 +01:00
|
|
|
* Force the site to run on SSL. To use, call from _config.php.
|
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
* <code>
|
|
|
|
* if(Director::isLive()) Director::forceSSL();
|
|
|
|
* </code>
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function forceSSL() {
|
2008-10-09 00:53:20 +02:00
|
|
|
if((!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off') && !Director::isDev()) {
|
2008-08-20 06:54:24 +02:00
|
|
|
$destURL = str_replace('http:', 'https:', Director::absoluteURL($_SERVER['REQUEST_URI']));
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2008-08-20 06:54:24 +02:00
|
|
|
header("Location: $destURL", true, 301);
|
2007-07-19 12:40:28 +02:00
|
|
|
die("<h1>Your browser is not accepting header redirects</h1><p>Please <a href=\"$destURL\">click here</a>");
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-03-03 00:24:10 +01:00
|
|
|
* Force a redirect to a domain starting with "www."
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function forceWWW() {
|
2009-06-27 15:08:23 +02:00
|
|
|
if(!Director::isDev() && !Director::isTest() && strpos($_SERVER['HTTP_HOST'], 'www') !== 0) {
|
2008-10-09 00:53:20 +02:00
|
|
|
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
|
2008-09-30 05:31:29 +02:00
|
|
|
$destURL = str_replace('https://', 'https://www.', Director::absoluteURL($_SERVER['REQUEST_URI']));
|
|
|
|
} else {
|
|
|
|
$destURL = str_replace('http://', 'http://www.', Director::absoluteURL($_SERVER['REQUEST_URI']));
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2008-10-14 23:06:15 +02:00
|
|
|
header("Location: $destURL", true, 301);
|
2007-07-19 12:40:28 +02:00
|
|
|
die("<h1>Your browser is not accepting header redirects</h1><p>Please <a href=\"$destURL\">click here</a>");
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Checks if the current HTTP-Request is an "Ajax-Request"
|
|
|
|
* by checking for a custom header set by prototype.js or
|
|
|
|
* wether a manually set request-parameter 'ajax' is present.
|
2007-09-14 19:47:45 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
static function is_ajax() {
|
2007-08-21 00:39:44 +02:00
|
|
|
if(Controller::has_curr()) {
|
|
|
|
return Controller::curr()->isAjax();
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
isset($_REQUEST['ajax']) ||
|
|
|
|
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest")
|
|
|
|
);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-11-11 03:54:19 +01:00
|
|
|
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Returns true if this script is being run from the command line rather than the webserver.
|
|
|
|
*
|
2007-11-11 03:54:19 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function is_cli() {
|
2008-11-06 05:51:25 +01:00
|
|
|
return (!isset($_SERVER['HTTP_HOST']) && preg_match('/install\.php/', $_SERVER['SCRIPT_NAME']))
|
|
|
|
|| preg_match('/cli-script\.php/', $_SERVER['SCRIPT_NAME']);
|
2007-11-11 03:54:19 +01:00
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Site mode methods
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-09-16 17:31:44 +02:00
|
|
|
/**
|
2009-06-25 10:15:52 +02:00
|
|
|
* @deprecated 2.4
|
2007-09-16 17:31:44 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function set_site_mode($mode) {
|
2009-06-25 10:15:52 +02:00
|
|
|
user_error (
|
|
|
|
'Director::set_site_mode() is deprecated as the functionality is no longer neccesary.', E_USER_NOTICE
|
|
|
|
);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
Director::$siteMode = $mode;
|
2007-09-14 02:54:02 +02:00
|
|
|
|
2007-11-10 06:17:30 +01:00
|
|
|
if(isset(self::$callbacks[$mode])) {
|
|
|
|
foreach(self::$callbacks[$mode] as $extension) {
|
2007-09-16 17:31:44 +02:00
|
|
|
call_user_func($extension);
|
|
|
|
}
|
2007-09-14 02:54:02 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-11-10 06:17:30 +01:00
|
|
|
|
2008-02-26 02:23:19 +01:00
|
|
|
/**
|
2009-06-25 10:15:52 +02:00
|
|
|
* @deprecated 2.4
|
2008-02-26 02:23:19 +01:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function get_site_mode() {
|
2009-06-25 10:15:52 +02:00
|
|
|
user_error (
|
|
|
|
'Director::set_site_mode() is deprecated as the functionality is no longer neccesary.', E_USER_NOTICE
|
|
|
|
);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return Director::$siteMode;
|
|
|
|
}
|
2007-09-16 17:31:44 +02:00
|
|
|
|
|
|
|
/**
|
2009-06-25 10:15:52 +02:00
|
|
|
* @deprecated 2.4 Use a custom extension on your controller.
|
2007-09-16 17:31:44 +02:00
|
|
|
*/
|
2007-11-10 06:17:30 +01:00
|
|
|
static function add_callback($function, $mode = 'site') {
|
2009-06-25 10:15:52 +02:00
|
|
|
user_error (
|
|
|
|
'Director::add_callback() is deprecated, please use a custom extension on your controller', E_USER_NOTICE
|
|
|
|
);
|
|
|
|
|
2007-11-10 06:17:30 +01:00
|
|
|
self::$callbacks[$mode][] = $function;
|
2007-09-16 17:31:44 +02:00
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Environment type methods
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Set the environment type of the current site.
|
|
|
|
*
|
|
|
|
* Typically, a SilverStripe site have a number of environments:
|
|
|
|
* - development environments, such a copy on your local machine.
|
|
|
|
* - test sites, such as the one you show the client before going live.
|
|
|
|
* - the live site itself.
|
|
|
|
*
|
|
|
|
* The behaviour of these environments often varies slightly. For example, development sites may have errors dumped to the screen,
|
|
|
|
* and order confirmation emails might be sent to the developer instead of the client.
|
|
|
|
*
|
|
|
|
* To help with this, Sapphire support the notion of an environment type. The environment type can be dev, test, or live.
|
|
|
|
*
|
|
|
|
* You can set it explicitly with Director::set_environment_tpye(). Or you can use {@link Director::set_dev_servers()} and {@link Director::set_test_servers()}
|
|
|
|
* to set it implicitly, based on the value of $_SERVER['HTTP_HOST']. If the HTTP_HOST value is one of the servers listed, then
|
|
|
|
* the environment type will be test or dev. Otherwise, the environment type will be live.
|
|
|
|
*
|
|
|
|
* Dev mode can also be forced by putting ?isDev=1 in your URL, which will ask you to log in and then push the site into dev
|
|
|
|
* mode for the remainder of the session. Putting ?isDev=0 onto the URL can turn it back.
|
2009-05-21 02:14:47 +02:00
|
|
|
*
|
|
|
|
* Test mode can also be forced by putting ?isTest=1 in your URL, which will ask you to log in and then push the site into test
|
|
|
|
* mode for the remainder of the session. Putting ?isTest=0 onto the URL can turn it back.
|
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* Generally speaking, these methods will be called from your _config.php file.
|
|
|
|
*
|
|
|
|
* Once the environment type is set, it can be checked with {@link Director::isDev()}, {@link Director::isTest()}, and
|
|
|
|
* {@link Director::isLive()}.
|
|
|
|
*
|
|
|
|
* @param $et string The environment type: dev, test, or live.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function set_environment_type($et) {
|
|
|
|
if($et != 'dev' && $et != 'test' && $et != 'live') {
|
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
|
|
|
SS_Backtrace::backtrace();
|
2008-08-09 06:38:44 +02:00
|
|
|
user_error("Director::set_environment_type passed '$et'. It should be passed dev, test, or live", E_USER_WARNING);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
self::$environment_type = $et;
|
|
|
|
}
|
|
|
|
}
|
2008-10-01 16:40:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Can also be checked with {@link Director::isDev()}, {@link Director::isTest()}, and {@link Director::isLive()}.
|
|
|
|
*
|
|
|
|
* @return string 'dev', 'test' or 'live'
|
|
|
|
*/
|
|
|
|
static function get_environment_type() {
|
|
|
|
if(Director::isLive()) {
|
|
|
|
return 'live';
|
|
|
|
} elseif(Director::isTest()) {
|
|
|
|
return 'test';
|
|
|
|
} elseif(Director::isDev()) {
|
|
|
|
return 'dev';
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Specify HTTP_HOST values that are development environments.
|
|
|
|
* For information about environment types, see {@link Director::set_environment_type()}.
|
|
|
|
* @param $servers array An array of HTTP_HOST values that should be treated as development environments.
|
|
|
|
*/
|
|
|
|
static function set_dev_servers($servers) {
|
|
|
|
Director::$dev_servers = $servers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify HTTP_HOST values that are test environments.
|
|
|
|
* For information about environment types, see {@link Director::set_environment_type()}.
|
|
|
|
* @param $servers array An array of HTTP_HOST values that should be treated as test environments.
|
|
|
|
*/
|
|
|
|
static function set_test_servers($servers) {
|
|
|
|
Director::$test_servers = $servers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function will return true if the site is in a live environment.
|
|
|
|
* For information about environment types, see {@link Director::set_environment_type()}.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function isLive() {
|
|
|
|
return !(Director::isDev() || Director::isTest());
|
|
|
|
}
|
2007-11-10 06:17:30 +01:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* This function will return true if the site is in a development environment.
|
|
|
|
* For information about environment types, see {@link Director::set_environment_type()}.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function isDev() {
|
2009-06-08 01:07:25 +02:00
|
|
|
// This variable is used to supress repetitions of the isDev security message below.
|
|
|
|
static $firstTimeCheckingGetVar = true;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Use ?isDev=1 to get development access on the live server
|
|
|
|
if(isset($_GET['isDev'])) {
|
2008-11-06 05:51:25 +01:00
|
|
|
if(Security::database_is_ready()) {
|
2009-10-27 08:27:22 +01:00
|
|
|
BasicAuth::requireLogin("SilverStripe developer access. Use your CMS login", "ADMIN");
|
2007-07-19 12:40:28 +02:00
|
|
|
$_SESSION['isDev'] = $_GET['isDev'];
|
|
|
|
} else {
|
2009-06-08 01:07:25 +02:00
|
|
|
if($firstTimeCheckingGetVar && DB::connection_attempted()) {
|
|
|
|
echo "<p style=\"padding: 3px; margin: 3px; background-color: orange;
|
|
|
|
color: white; font-weight: bold\">Sorry, you can't use ?isDev=1 until your
|
|
|
|
Member and Group tables database are available. Perhaps your database
|
|
|
|
connection is failing?</p>";
|
|
|
|
$firstTimeCheckingGetVar = false;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-18 02:48:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($_SESSION['isDev']) && $_SESSION['isDev']) return true;
|
2008-11-18 02:48:37 +01:00
|
|
|
|
|
|
|
if(self::$environment_type) return self::$environment_type == 'dev';
|
2007-09-27 23:50:10 +02:00
|
|
|
|
|
|
|
// Check if we are running on one of the development servers
|
2008-11-22 04:33:00 +01:00
|
|
|
if(isset($_SERVER['HTTP_HOST']) && in_array($_SERVER['HTTP_HOST'], Director::$dev_servers)) {
|
2007-09-27 23:50:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-27 23:50:10 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* This function will return true if the site is in a test environment.
|
|
|
|
* For information about environment types, see {@link Director::set_environment_type()}.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function isTest() {
|
2009-05-21 02:14:47 +02:00
|
|
|
// Use ?isTest=1 to get test access on the live server, or explicitly set your environment
|
|
|
|
if(isset($_GET['isTest'])) {
|
|
|
|
if(Security::database_is_ready()) {
|
2009-10-27 08:27:22 +01:00
|
|
|
BasicAuth::requireLogin("SilverStripe developer access. Use your CMS login", "ADMIN");
|
2009-05-21 02:14:47 +02:00
|
|
|
$_SESSION['isTest'] = $_GET['isTest'];
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-11-18 02:48:37 +01:00
|
|
|
if(self::isDev()) return false;
|
|
|
|
|
2007-09-27 23:50:10 +02:00
|
|
|
if(self::$environment_type) {
|
|
|
|
return self::$environment_type == 'test';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are running on one of the test servers
|
2008-11-22 04:33:00 +01:00
|
|
|
if(isset($_SERVER['HTTP_HOST']) && in_array($_SERVER['HTTP_HOST'], Director::$test_servers)) {
|
2007-09-27 23:50:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-04-29 01:40:35 +02:00
|
|
|
?>
|