2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Control;
|
|
|
|
|
2016-07-25 00:20:06 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Config\Configurable;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Dev\Debug;
|
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\ORM\ArrayLib;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataModel;
|
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
use SilverStripe\View\Requirements_Backend;
|
|
|
|
use SilverStripe\View\TemplateGlobalProvider;
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Director is responsible for processing URLs, and providing environment information.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* The most important part of director is {@link Director::direct()}, which is passed a URL and will
|
|
|
|
* execute the appropriate controller.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* Director also has a number of static methods that provide information about the environment, such as
|
2013-03-21 19:48:54 +01:00
|
|
|
* {@link Director::$environment_type}.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @see Director::direct()
|
|
|
|
* @see Director::$rules
|
|
|
|
* @see Director::$environment_type
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-02-11 03:08:39 +01:00
|
|
|
class Director implements TemplateGlobalProvider {
|
2016-08-19 00:51:35 +02:00
|
|
|
use Configurable;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-12 06:11:30 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Specifies this url is relative to the base.
|
|
|
|
*
|
|
|
|
* @var string
|
2015-06-12 06:11:30 +02:00
|
|
|
*/
|
|
|
|
const BASE = 'BASE';
|
|
|
|
|
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Specifies this url is relative to the site root.
|
|
|
|
*
|
|
|
|
* @var string
|
2015-06-12 06:11:30 +02:00
|
|
|
*/
|
|
|
|
const ROOT = 'ROOT';
|
|
|
|
|
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* specifies this url is relative to the current request.
|
|
|
|
*
|
|
|
|
* @var string
|
2015-06-12 06:11:30 +02:00
|
|
|
*/
|
|
|
|
const REQUEST = 'REQUEST';
|
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static private $urlParams;
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static private $rules = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:10 +02:00
|
|
|
/**
|
|
|
|
* @var SiteTree
|
|
|
|
*/
|
|
|
|
private static $current_page;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $alternate_base_folder;
|
2007-11-10 06:17:30 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $dev_servers = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $test_servers = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-15 23:49:04 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Setting this explicitly specifies the protocol ("http" or "https") used, overriding the normal
|
|
|
|
* behaviour of Director::is_https introspecting it from the request. False values imply default
|
|
|
|
* introspection.
|
2013-10-15 23:49:04 +02:00
|
|
|
*
|
|
|
|
* @config
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
|
|
|
* @var string
|
2013-10-15 23:49:04 +02:00
|
|
|
*/
|
|
|
|
private static $alternate_protocol;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $alternate_base_url;
|
2013-11-05 10:15:14 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $environment_type;
|
2007-11-10 06:17:30 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Process the given URL, creating the appropriate controller and executing it.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-19 05:47:11 +02:00
|
|
|
* Request processing is handled as follows:
|
2016-09-09 08:43:05 +02:00
|
|
|
* - Director::direct() creates a new HTTPResponse object and passes this to
|
2015-11-13 23:44:49 +01:00
|
|
|
* Director::handleRequest().
|
|
|
|
* - 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.
|
|
|
|
* - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method
|
|
|
|
* returns a RequestHandler object.
|
|
|
|
*
|
|
|
|
* In addition to request processing, Director will manage the session, and perform the output of
|
|
|
|
* the actual response to the browser.
|
|
|
|
*
|
2008-08-09 05:19:54 +02:00
|
|
|
* @uses handleRequest() rule-lookup logic is handled by this.
|
2016-08-19 00:51:35 +02:00
|
|
|
* @uses Controller::handleRequest() This handles the page logic for a Director::direct() call.
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param string $url
|
|
|
|
* @param DataModel $model
|
2016-09-09 08:43:05 +02:00
|
|
|
* @throws HTTPResponse_Exception
|
2007-09-14 19:47:45 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function direct($url, DataModel $model) {
|
2009-03-17 23:22:55 +01:00
|
|
|
// Validate $_FILES array before merging it with $_POST
|
|
|
|
foreach($_FILES as $k => $v) {
|
2016-04-07 03:13:31 +02:00
|
|
|
if (is_array($v['tmp_name'])) {
|
2010-12-14 01:15:30 +01:00
|
|
|
$v = ArrayLib::array_values_recursive($v['tmp_name']);
|
|
|
|
foreach($v as $tmpFile) {
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($tmpFile && !is_uploaded_file($tmpFile)) {
|
2009-03-18 11:16:15 +01:00
|
|
|
user_error("File upload '$k' doesn't appear to be a valid upload", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($v['tmp_name'] && !is_uploaded_file($v['tmp_name'])) {
|
2009-03-18 11:16:15 +01:00
|
|
|
user_error("File upload '$k' doesn't appear to be a valid upload", E_USER_ERROR);
|
|
|
|
}
|
2009-03-17 23:22:55 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$req = new HTTPRequest(
|
2012-09-26 23:34:00 +02:00
|
|
|
(isset($_SERVER['X-HTTP-Method-Override']))
|
2014-08-15 08:53:05 +02:00
|
|
|
? $_SERVER['X-HTTP-Method-Override']
|
2012-09-26 23:34:00 +02:00
|
|
|
: $_SERVER['REQUEST_METHOD'],
|
2014-08-15 08:53:05 +02:00
|
|
|
$url,
|
|
|
|
$_GET,
|
2015-10-06 07:28:08 +02:00
|
|
|
ArrayLib::array_merge_recursive((array) $_POST, (array) $_FILES),
|
2008-08-09 09:03:24 +02:00
|
|
|
@file_get_contents('php://input')
|
|
|
|
);
|
2011-03-19 00:38:30 +01:00
|
|
|
|
2012-02-12 19:38:21 +01:00
|
|
|
$headers = self::extract_request_headers($_SERVER);
|
2011-03-19 00:38:30 +01:00
|
|
|
foreach ($headers as $header => $value) {
|
|
|
|
$req->addHeader($header, $value);
|
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
|
2014-06-19 00:38:26 +02:00
|
|
|
// Initiate an empty session - doesn't initialize an actual PHP session until saved (see below)
|
2016-08-19 00:51:35 +02:00
|
|
|
$session = Session::create(isset($_SESSION) ? $_SESSION : array());
|
2014-06-19 00:38:26 +02:00
|
|
|
|
2012-05-03 14:44:36 +02:00
|
|
|
// Only resume a session if its not started already, and a session identifier exists
|
2016-04-07 03:13:31 +02:00
|
|
|
if (!isset($_SESSION) && Session::request_contains_session_id()) {
|
2014-06-19 00:38:26 +02:00
|
|
|
$session->inst_start();
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2012-05-09 14:26:29 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$output = RequestProcessor::singleton()->preRequest($req, $session, $model);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
if ($output === false) {
|
|
|
|
// @TODO Need to NOT proceed with the request in an elegant manner
|
2016-09-09 08:43:05 +02:00
|
|
|
throw new HTTPResponse_Exception(_t('Director.INVALID_REQUEST', 'Invalid request'), 400);
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
$result = Director::handleRequest($req, $session, $model);
|
2012-05-03 14:44:36 +02:00
|
|
|
|
2014-06-19 00:38:26 +02:00
|
|
|
// Save session data. Note that inst_save() will start/resume the session if required.
|
2008-08-09 05:19:54 +02:00
|
|
|
$session->inst_save();
|
|
|
|
|
|
|
|
// Return code for a redirection request
|
2016-04-07 03:13:31 +02:00
|
|
|
if (is_string($result) && substr($result, 0, 9) == 'redirect:') {
|
2013-09-16 10:11:28 +02:00
|
|
|
$url = substr($result, 9);
|
|
|
|
|
2016-04-07 03:13:31 +02:00
|
|
|
if (Director::is_cli()) {
|
2013-09-16 10:11:28 +02:00
|
|
|
// on cli, follow SilverStripe redirects automatically
|
2016-08-19 00:51:35 +02:00
|
|
|
Director::direct(
|
2014-08-15 08:53:05 +02:00
|
|
|
str_replace(Director::absoluteBaseURL(), '', $url),
|
2013-09-16 10:11:28 +02:00
|
|
|
DataModel::inst()
|
|
|
|
);
|
2016-08-19 00:51:35 +02:00
|
|
|
return;
|
2013-09-16 10:11:28 +02:00
|
|
|
} else {
|
2016-09-09 08:43:05 +02:00
|
|
|
$response = new HTTPResponse();
|
2013-09-16 10:11:28 +02:00
|
|
|
$response->redirect($url);
|
2016-08-19 00:51:35 +02:00
|
|
|
$res = RequestProcessor::singleton()->postRequest($req, $response, $model);
|
2013-09-16 10:11:28 +02:00
|
|
|
|
2014-03-04 05:43:17 +01:00
|
|
|
if ($res !== false) {
|
|
|
|
$response->output();
|
|
|
|
}
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
// Handle a controller
|
2016-04-07 03:13:31 +02:00
|
|
|
} elseif ($result) {
|
2016-09-09 08:43:05 +02:00
|
|
|
if ($result instanceof HTTPResponse) {
|
2008-08-09 05:19:54 +02:00
|
|
|
$response = $result;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
} else {
|
2016-09-09 08:43:05 +02:00
|
|
|
$response = new HTTPResponse();
|
2008-08-09 05:19:54 +02:00
|
|
|
$response->setBody($result);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$res = RequestProcessor::singleton()->postRequest($req, $response, $model);
|
2012-05-09 14:26:29 +02:00
|
|
|
if ($res !== false) {
|
2016-08-19 00:51:35 +02:00
|
|
|
$response->output();
|
2008-10-17 02:52:58 +02:00
|
|
|
} else {
|
2012-05-09 14:26:29 +02:00
|
|
|
// @TODO Proper response here.
|
2016-09-09 08:43:05 +02:00
|
|
|
throw new HTTPResponse_Exception("Invalid response");
|
2008-10-17 02:52:58 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
|
|
|
|
//$controllerObj->getSession()->inst_save();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-21 00:39:44 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Test a URL request, returning a response object. This method is the counterpart of
|
|
|
|
* Director::direct() that is used in functional testing. It will execute the URL given, and
|
2016-09-09 08:43:05 +02:00
|
|
|
* return the result as an HTTPResponse object.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @uses Controller::handleRequest() Handles the page logic for a Director::direct() call.
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
|
|
|
* @param string $url The URL to visit.
|
|
|
|
* @param array $postVars The $_POST & $_FILES variables.
|
|
|
|
* @param array|Session $session The {@link Session} object representing the current session.
|
|
|
|
* By passing the same object to multiple calls of Director::test(), you can simulate a persisted
|
|
|
|
* session.
|
|
|
|
* @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.
|
|
|
|
* @param string $body The HTTP body.
|
|
|
|
* @param array $headers HTTP headers with key-value pairs.
|
|
|
|
* @param array|Cookie_Backend $cookies to populate $_COOKIE.
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request.
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @return HTTPResponse
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @throws HTTPResponse_Exception
|
2007-08-21 00:39:44 +02:00
|
|
|
*/
|
2014-09-26 06:01:23 +02:00
|
|
|
public static function test($url, $postVars = null, $session = array(), $httpMethod = null, $body = null,
|
|
|
|
$headers = array(), $cookies = array(), &$request = null) {
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2013-03-27 14:33:04 +01:00
|
|
|
Config::nest();
|
2014-09-26 06:01:23 +02:00
|
|
|
Injector::nest();
|
2013-03-27 14:33:04 +01:00
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
// These are needed so that calling Director::test() does not muck with whoever is calling it.
|
|
|
|
// Really, it's some inappropriate coupling and should be resolved by making less use of statics.
|
2016-05-25 07:09:29 +02:00
|
|
|
$oldReadingMode = Versioned::get_reading_mode();
|
2008-12-04 23:38:32 +01:00
|
|
|
$getVars = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-04-07 03:13:31 +02:00
|
|
|
if (!$httpMethod) $httpMethod = ($postVars || is_array($postVars)) ? "POST" : "GET";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if (!$session) $session = Injector::inst()->create('SilverStripe\\Control\\Session', array());
|
2014-09-26 06:01:23 +02:00
|
|
|
$cookieJar = $cookies instanceof Cookie_Backend
|
|
|
|
? $cookies
|
2016-08-19 00:51:35 +02:00
|
|
|
: Injector::inst()->createWithArgs('SilverStripe\\Control\\Cookie_Backend', array($cookies ?: array()));
|
2008-08-11 07:26:51 +02:00
|
|
|
|
|
|
|
// Back up the current values of the superglobals
|
2010-10-15 05:08:23 +02:00
|
|
|
$existingRequestVars = isset($_REQUEST) ? $_REQUEST : array();
|
2014-08-15 08:53:05 +02:00
|
|
|
$existingGetVars = isset($_GET) ? $_GET : array();
|
2010-10-15 05:08:23 +02:00
|
|
|
$existingPostVars = isset($_POST) ? $_POST : array();
|
2010-10-15 02:34:18 +02:00
|
|
|
$existingSessionVars = isset($_SESSION) ? $_SESSION : array();
|
2010-10-15 05:08:23 +02:00
|
|
|
$existingCookies = isset($_COOKIE) ? $_COOKIE : array();
|
|
|
|
$existingServer = isset($_SERVER) ? $_SERVER : array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
$existingRequirementsBackend = Requirements::backend();
|
2009-01-08 00:00:54 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
Cookie::config()->update('report_errors', false);
|
|
|
|
Requirements::set_backend(Requirements_Backend::create());
|
2008-08-11 07:26:51 +02:00
|
|
|
|
2014-09-26 06:01:23 +02:00
|
|
|
// Set callback to invoke prior to return
|
|
|
|
$onCleanup = function() use(
|
|
|
|
$existingRequestVars, $existingGetVars, $existingPostVars, $existingSessionVars,
|
2016-05-25 07:09:29 +02:00
|
|
|
$existingCookies, $existingServer, $existingRequirementsBackend, $oldReadingMode
|
2014-09-26 06:01:23 +02:00
|
|
|
) {
|
2015-11-13 23:44:49 +01:00
|
|
|
// Restore the super globals
|
2014-09-26 06:01:23 +02:00
|
|
|
$_REQUEST = $existingRequestVars;
|
|
|
|
$_GET = $existingGetVars;
|
|
|
|
$_POST = $existingPostVars;
|
|
|
|
$_SESSION = $existingSessionVars;
|
|
|
|
$_COOKIE = $existingCookies;
|
|
|
|
$_SERVER = $existingServer;
|
|
|
|
|
|
|
|
Requirements::set_backend($existingRequirementsBackend);
|
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
// These are needed so that calling Director::test() does not muck with whoever is calling it.
|
2014-09-26 06:01:23 +02:00
|
|
|
// Really, it's some inappropriate coupling and should be resolved by making less use of statics
|
2016-05-25 07:09:29 +02:00
|
|
|
Versioned::set_reading_mode($oldReadingMode);
|
2014-09-26 06:01:23 +02:00
|
|
|
|
|
|
|
Injector::unnest(); // Restore old CookieJar, etc
|
|
|
|
Config::unnest();
|
|
|
|
};
|
|
|
|
|
2014-02-26 13:04:47 +01:00
|
|
|
if (strpos($url, '#') !== false) {
|
|
|
|
$url = substr($url, 0, strpos($url, '#'));
|
|
|
|
}
|
|
|
|
|
2009-07-13 06:15:40 +02:00
|
|
|
// Handle absolute URLs
|
2014-03-04 23:47:02 +01:00
|
|
|
if (parse_url($url, PHP_URL_HOST)) {
|
2009-07-13 06:15:40 +02:00
|
|
|
$bits = parse_url($url);
|
2015-11-13 23:44:49 +01:00
|
|
|
// If a port is mentioned in the absolute URL, be sure to add that into the HTTP host
|
2016-04-07 03:13:31 +02:00
|
|
|
if (isset($bits['port'])) {
|
2014-02-16 23:09:29 +01:00
|
|
|
$_SERVER['HTTP_HOST'] = $bits['host'].':'.$bits['port'];
|
|
|
|
} else {
|
|
|
|
$_SERVER['HTTP_HOST'] = $bits['host'];
|
|
|
|
}
|
2009-07-13 06:15:40 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 04:45:44 +02:00
|
|
|
// Ensure URL is properly made relative.
|
|
|
|
// Example: url passed is "/ss31/my-page" (prefixed with BASE_URL), this should be changed to "my-page"
|
|
|
|
$url = self::makeRelative($url);
|
|
|
|
|
2009-07-13 06:15:40 +02:00
|
|
|
$urlWithQuerystring = $url;
|
2016-04-07 03:13:31 +02:00
|
|
|
if (strpos($url, '?') !== false) {
|
2009-07-13 06:15:40 +02:00
|
|
|
list($url, $getVarsEncoded) = explode('?', $url, 2);
|
|
|
|
parse_str($getVarsEncoded, $getVars);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
// Replace the super globals with appropriate test values
|
2015-10-06 07:28:08 +02:00
|
|
|
$_REQUEST = ArrayLib::array_merge_recursive((array) $getVars, (array) $postVars);
|
|
|
|
$_GET = (array) $getVars;
|
|
|
|
$_POST = (array) $postVars;
|
2008-08-11 07:26:51 +02:00
|
|
|
$_SESSION = $session ? $session->inst_getAll() : array();
|
2014-09-26 06:01:23 +02:00
|
|
|
$_COOKIE = $cookieJar->getAll(false);
|
2016-08-19 00:51:35 +02:00
|
|
|
Injector::inst()->registerService($cookieJar, 'SilverStripe\\Control\\Cookie_Backend');
|
2009-01-08 00:00:54 +01:00
|
|
|
$_SERVER['REQUEST_URI'] = Director::baseURL() . $urlWithQuerystring;
|
2008-08-11 07:26:51 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$request = new HTTPRequest($httpMethod, $url, $getVars, $postVars, $body);
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($headers) {
|
|
|
|
foreach($headers as $k => $v) {
|
|
|
|
$request->addHeader($k, $v);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// Pre-request filtering
|
2013-10-14 22:53:34 +02:00
|
|
|
// @see issue #2517
|
|
|
|
$model = DataModel::inst();
|
2016-08-19 00:51:35 +02:00
|
|
|
$output = Injector::inst()->get('SilverStripe\\Control\\RequestProcessor')->preRequest($request, $session, $model);
|
2013-10-14 22:53:34 +02:00
|
|
|
if ($output === false) {
|
2014-09-26 06:01:23 +02:00
|
|
|
$onCleanup();
|
2016-09-09 08:43:05 +02:00
|
|
|
throw new HTTPResponse_Exception(_t('Director.INVALID_REQUEST', 'Invalid request'), 400);
|
2013-10-14 22:53:34 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
// TODO: Pass in the DataModel
|
2013-10-14 22:53:34 +02:00
|
|
|
$result = Director::handleRequest($request, $session, $model);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
// Ensure that the result is an HTTPResponse object
|
2016-04-07 03:13:31 +02:00
|
|
|
if (is_string($result)) {
|
|
|
|
if (substr($result, 0, 9) == 'redirect:') {
|
2016-09-09 08:43:05 +02:00
|
|
|
$response = new HTTPResponse();
|
2013-02-25 04:27:44 +01:00
|
|
|
$response->redirect(substr($result, 9));
|
|
|
|
$result = $response;
|
|
|
|
} else {
|
2016-09-09 08:43:05 +02:00
|
|
|
$result = new HTTPResponse($result);
|
2013-02-25 04:27:44 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$output = Injector::inst()->get('SilverStripe\\Control\\RequestProcessor')->postRequest($request, $result, $model);
|
2013-10-14 22:53:34 +02:00
|
|
|
if ($output === false) {
|
2014-09-26 06:01:23 +02:00
|
|
|
$onCleanup();
|
2016-09-09 08:43:05 +02:00
|
|
|
throw new HTTPResponse_Exception("Invalid response");
|
2013-10-14 22:53:34 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-09-26 06:01:23 +02:00
|
|
|
// Return valid response
|
|
|
|
$onCleanup();
|
2008-08-09 05:19:54 +02:00
|
|
|
return $result;
|
2007-08-21 00:39:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2016-09-09 08:43:05 +02:00
|
|
|
* Handle an HTTP request, defined with a HTTPRequest object.
|
2008-08-11 05:03:52 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @skipUpgrade
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param Session $session
|
|
|
|
* @param DataModel $model
|
2016-09-09 08:43:05 +02:00
|
|
|
* @return HTTPResponse|string
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2016-09-09 08:43:05 +02:00
|
|
|
protected static function handleRequest(HTTPRequest $request, Session $session, DataModel $model) {
|
2016-08-19 00:51:35 +02:00
|
|
|
$rules = Director::config()->get('rules');
|
2012-05-19 04:36:48 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if (isset($_REQUEST['debug'])) {
|
|
|
|
Debug::show($rules);
|
|
|
|
}
|
2012-05-19 04:36:48 +02:00
|
|
|
|
|
|
|
foreach($rules as $pattern => $controllerOptions) {
|
2016-07-19 11:58:35 +02:00
|
|
|
if(is_string($controllerOptions)) {
|
2016-04-07 03:13:31 +02:00
|
|
|
if (substr($controllerOptions, 0, 2) == '->') {
|
|
|
|
$controllerOptions = array('Redirect' => substr($controllerOptions, 2));
|
2012-09-26 23:34:00 +02:00
|
|
|
} else {
|
|
|
|
$controllerOptions = array('Controller' => $controllerOptions);
|
|
|
|
}
|
2012-05-19 04:36:48 +02:00
|
|
|
}
|
|
|
|
|
2016-04-07 03:13:31 +02:00
|
|
|
if (($arguments = $request->match($pattern, true)) !== false) {
|
2012-08-27 00:56:59 +02:00
|
|
|
$request->setRouteParams($controllerOptions);
|
2012-05-19 04:36:48 +02:00
|
|
|
// controllerOptions provide some default arguments
|
|
|
|
$arguments = array_merge($controllerOptions, $arguments);
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
// Pop additional tokens from the tokenizer if necessary
|
2016-04-07 03:13:31 +02:00
|
|
|
if (isset($controllerOptions['_PopTokeniser'])) {
|
2012-05-19 04:36:48 +02:00
|
|
|
$request->shift($controllerOptions['_PopTokeniser']);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-10-14 21:59:01 +02:00
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
// Handle redirection
|
2016-04-07 03:13:31 +02:00
|
|
|
if (isset($arguments['Redirect'])) {
|
2012-05-19 04:36:48 +02:00
|
|
|
return "redirect:" . Director::absoluteURL($arguments['Redirect'], true);
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2012-05-19 04:36:48 +02:00
|
|
|
} else {
|
2016-08-19 00:51:35 +02:00
|
|
|
// Find the controller name
|
|
|
|
$controller = $arguments['Controller'];
|
2012-05-19 04:36:48 +02:00
|
|
|
Director::$urlParams = $arguments;
|
|
|
|
$controllerObj = Injector::inst()->create($controller);
|
|
|
|
$controllerObj->setSession($session);
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2012-05-19 04:36:48 +02:00
|
|
|
try {
|
|
|
|
$result = $controllerObj->handleRequest($request, $model);
|
2016-09-09 08:43:05 +02:00
|
|
|
} catch(HTTPResponse_Exception $responseException) {
|
2012-05-19 04:36:48 +02:00
|
|
|
$result = $responseException->getResponse();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-09-09 08:43:05 +02:00
|
|
|
if (!is_object($result) || $result instanceof HTTPResponse) {
|
2016-08-19 00:51:35 +02:00
|
|
|
return $result;
|
|
|
|
}
|
2012-05-19 04:36:48 +02:00
|
|
|
|
|
|
|
user_error("Bad result from url " . $request->getURL() . " handled by " .
|
|
|
|
get_class($controllerObj)." controller: ".get_class($result), E_USER_WARNING);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-06 09:19:03 +02:00
|
|
|
// No URL rules matched, so return a 404 error.
|
2016-09-09 08:43:05 +02:00
|
|
|
return new HTTPResponse('No URL rule was matched', 404);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 04:53:11 +02:00
|
|
|
/**
|
|
|
|
* Set url parameters (should only be called internally by RequestHandler->handleRequest()).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param array $params
|
2010-10-15 04:53:11 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function setUrlParams($params) {
|
2010-10-15 04:53:11 +02:00
|
|
|
Director::$urlParams = $params;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +01: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.
|
2009-10-11 02:07:10 +02:00
|
|
|
*
|
2016-07-25 00:20:06 +02:00
|
|
|
* @return SiteTree|Controller
|
2009-10-11 02:07:10 +02:00
|
|
|
*/
|
|
|
|
public static function get_current_page() {
|
|
|
|
return self::$current_page ? self::$current_page : Controller::curr();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:10 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Turns the given URL into an absolute URL. By default non-site root relative urls will be
|
|
|
|
* evaluated relative to the current base_url.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param string $url URL To transform to absolute.
|
2015-06-12 06:11:30 +02:00
|
|
|
* @param string $relativeParent Method to use for evaluating relative urls.
|
|
|
|
* Either one of BASE (baseurl), ROOT (site root), or REQUEST (requested page).
|
|
|
|
* Defaults to BASE, which is the same behaviour as template url resolution.
|
2015-11-13 23:44:49 +01:00
|
|
|
* Ignored if the url is absolute or site root.
|
|
|
|
*
|
|
|
|
* @return string
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2015-06-12 06:11:30 +02:00
|
|
|
public static function absoluteURL($url, $relativeParent = self::BASE) {
|
2016-04-07 03:13:31 +02:00
|
|
|
if (is_bool($relativeParent)) {
|
2015-06-12 06:11:30 +02:00
|
|
|
// Deprecate old boolean second parameter
|
|
|
|
Deprecation::notice('5.0', 'Director::absoluteURL takes an explicit parent for relative url');
|
|
|
|
$relativeParent = $relativeParent ? self::BASE : self::REQUEST;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-12 06:11:30 +02:00
|
|
|
// Check if there is already a protocol given
|
2016-04-07 03:13:31 +02:00
|
|
|
if (preg_match('/^http(s?):\/\//', $url)) {
|
2015-06-12 06:11:30 +02:00
|
|
|
return $url;
|
2014-07-31 00:22:37 +02:00
|
|
|
}
|
2015-06-12 06:11:30 +02:00
|
|
|
|
|
|
|
// Absolute urls without protocol are added
|
|
|
|
// E.g. //google.com -> http://google.com
|
2016-04-07 03:13:31 +02:00
|
|
|
if (strpos($url, '//') === 0) {
|
2015-06-12 06:11:30 +02:00
|
|
|
return self::protocol() . substr($url, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine method for mapping the parent to this relative url
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($relativeParent === self::ROOT || self::is_root_relative_url($url)) {
|
2015-06-12 06:11:30 +02:00
|
|
|
// Root relative urls always should be evaluated relative to the root
|
|
|
|
$parent = self::protocolAndHost();
|
|
|
|
|
2016-04-07 03:13:31 +02:00
|
|
|
} elseif ($relativeParent === self::REQUEST) {
|
2015-06-12 06:11:30 +02:00
|
|
|
// Request relative urls rely on the REQUEST_URI param (old default behaviour)
|
2016-04-07 03:13:31 +02:00
|
|
|
if (!isset($_SERVER['REQUEST_URI'])) {
|
2015-06-12 06:11:30 +02:00
|
|
|
return false;
|
2014-07-31 00:22:37 +02:00
|
|
|
}
|
2015-06-12 06:11:30 +02:00
|
|
|
$parent = dirname($_SERVER['REQUEST_URI'] . 'x');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Default to respecting site base_url
|
|
|
|
$parent = self::absoluteBaseURL();
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2015-06-12 06:11:30 +02:00
|
|
|
// Map empty urls to relative slash and join to base
|
2016-04-07 03:13:31 +02:00
|
|
|
if (empty($url) || $url === '.' || $url === './') {
|
2015-06-12 06:11:30 +02:00
|
|
|
$url = '/';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2015-06-12 06:11:30 +02:00
|
|
|
return Controller::join_links($parent, $url);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Returns the domain part of the URL 'http://www.mysite.com'. Returns FALSE is this environment
|
|
|
|
* variable isn't set.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @return bool|string
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function protocolAndHost() {
|
2016-08-19 00:51:35 +02:00
|
|
|
$alternate = Config::inst()->get('SilverStripe\\Control\\Director', 'alternate_base_url');
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($alternate) {
|
|
|
|
if (preg_match('/^(http[^:]*:\/\/[^\/]+)(\/|$)/', $alternate, $matches)) {
|
2007-09-25 05:44:07 +02:00
|
|
|
return $matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 03:13:31 +02:00
|
|
|
if (isset($_SERVER['HTTP_HOST'])) {
|
2010-10-15 05:09:32 +02:00
|
|
|
return Director::protocol() . $_SERVER['HTTP_HOST'];
|
2008-09-29 14:52:23 +02:00
|
|
|
} else {
|
2008-10-02 11:48:56 +02:00
|
|
|
global $_FILE_TO_URL_MAPPING;
|
2016-04-07 03:13:31 +02:00
|
|
|
if (Director::is_cli() && isset($_FILE_TO_URL_MAPPING)) {
|
|
|
|
$errorSuggestion = ' You probably want to define ' .
|
2008-10-02 11:48:56 +02:00
|
|
|
'an entry in $_FILE_TO_URL_MAPPING that covers "' . Director::baseFolder() . '"';
|
2016-04-07 03:13:31 +02:00
|
|
|
} elseif (Director::is_cli()) {
|
|
|
|
$errorSuggestion = ' You probably want to define $_FILE_TO_URL_MAPPING in ' .
|
2008-10-02 11:48:56 +02:00
|
|
|
'your _ss_environment.php as instructed on the "sake" page of the doc.silverstripe.com wiki';
|
2016-04-07 03:13:31 +02:00
|
|
|
} else {
|
|
|
|
$errorSuggestion = "";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
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;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-12 04:51:33 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2010-10-15 05:09:32 +02:00
|
|
|
/**
|
2013-05-10 12:03:54 +02:00
|
|
|
* Return the current protocol that the site is running under.
|
2010-10-15 05:09:32 +02:00
|
|
|
*
|
2013-05-10 12:03:54 +02:00
|
|
|
* @return string
|
2010-10-15 05:09:32 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function protocol() {
|
2013-05-10 12:03:54 +02:00
|
|
|
return (self::is_https()) ? 'https://' : 'http://';
|
2014-01-12 06:05:09 +01:00
|
|
|
}
|
2013-05-10 12:03:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether the site is running as under HTTPS.
|
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @return bool
|
2013-05-10 12:03:54 +02:00
|
|
|
*/
|
|
|
|
public static function is_https() {
|
2016-02-17 11:19:19 +01:00
|
|
|
// See https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
|
2016-04-07 03:13:31 +02:00
|
|
|
// See https://support.microsoft.com/en-us/kb/307347
|
2016-02-17 11:19:19 +01:00
|
|
|
$headerOverride = false;
|
2016-04-07 03:13:31 +02:00
|
|
|
if (TRUSTED_PROXY) {
|
2016-02-17 11:19:19 +01:00
|
|
|
$headers = (defined('SS_TRUSTED_PROXY_PROTOCOL_HEADER')) ? array(SS_TRUSTED_PROXY_PROTOCOL_HEADER) : null;
|
2016-04-07 03:13:31 +02:00
|
|
|
if (!$headers) {
|
2016-02-17 11:19:19 +01:00
|
|
|
// Backwards compatible defaults
|
|
|
|
$headers = array('HTTP_X_FORWARDED_PROTO', 'HTTP_X_FORWARDED_PROTOCOL', 'HTTP_FRONT_END_HTTPS');
|
|
|
|
}
|
|
|
|
foreach($headers as $header) {
|
|
|
|
$headerCompareVal = ($header === 'HTTP_FRONT_END_HTTPS' ? 'on' : 'https');
|
2016-04-07 03:13:31 +02:00
|
|
|
if (!empty($_SERVER[$header]) && strtolower($_SERVER[$header]) == $headerCompareVal) {
|
2016-02-17 11:19:19 +01:00
|
|
|
$headerOverride = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($protocol = Config::inst()->get('SilverStripe\\Control\\Director', 'alternate_protocol')) {
|
2016-04-07 03:13:31 +02:00
|
|
|
return ($protocol == 'https');
|
|
|
|
} elseif ($headerOverride) {
|
|
|
|
return true;
|
|
|
|
} elseif ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off')) {
|
|
|
|
return true;
|
|
|
|
} elseif (isset($_SERVER['SSL'])) {
|
|
|
|
return true;
|
2014-05-22 08:34:15 +02:00
|
|
|
} else {
|
2016-04-07 03:13:31 +02:00
|
|
|
return false;
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2010-10-15 05:09:32 +02:00
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Returns the root URL for the site. It will be automatically calculated unless it is overridden
|
|
|
|
* with {@link setBaseURL()}.
|
2013-05-10 12:03:54 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function baseURL() {
|
2016-08-19 00:51:35 +02:00
|
|
|
$alternate = Config::inst()->get('SilverStripe\\Control\\Director', 'alternate_base_url');
|
2013-05-10 12:03:54 +02:00
|
|
|
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($alternate) {
|
2013-03-21 19:48:54 +01:00
|
|
|
return $alternate;
|
|
|
|
} else {
|
2010-04-13 01:15:04 +02:00
|
|
|
$base = BASE_URL;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($base == '/' || $base == '/.' || $base == '\\') {
|
2013-05-10 12:03:54 +02:00
|
|
|
$baseURL = '/';
|
|
|
|
} else {
|
|
|
|
$baseURL = $base . '/';
|
2014-01-12 06:05:09 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-04-07 03:13:31 +02:00
|
|
|
if (defined('BASE_SCRIPT_URL')) {
|
2013-05-10 12:03:54 +02:00
|
|
|
return $baseURL . BASE_SCRIPT_URL;
|
2014-01-12 06:05:09 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 12:03:54 +02:00
|
|
|
return $baseURL;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Sets the root URL for the website. If the site isn't accessible from the URL you provide,
|
|
|
|
* weird things will happen.
|
|
|
|
*
|
|
|
|
* @deprecated 4.0 Use the "Director.alternate_base_url" config setting instead.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param string $baseURL
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function setBaseURL($baseURL) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Director.alternate_base_url" config setting instead');
|
2016-08-19 00:51:35 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_url', $baseURL);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Returns the root filesystem folder for the site. It will be automatically calculated unless
|
|
|
|
* it is overridden with {@link setBaseFolder()}.
|
|
|
|
*
|
|
|
|
* @return string
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function baseFolder() {
|
2016-08-19 00:51:35 +02:00
|
|
|
$alternate = Config::inst()->get('SilverStripe\\Control\\Director', 'alternate_base_folder');
|
2013-03-21 19:48:54 +01:00
|
|
|
return ($alternate) ? $alternate : BASE_PATH;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-03-03 00:24:10 +01:00
|
|
|
|
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Sets the root folder for the website. If the site isn't accessible from the folder you provide,
|
|
|
|
* weird things will happen.
|
|
|
|
*
|
|
|
|
* @deprecated 4.0 Use the "Director.alternate_base_folder" config setting instead.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param string $baseFolder
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function setBaseFolder($baseFolder) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Director.alternate_base_folder" config setting instead');
|
2016-08-19 00:51:35 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_folder', $baseFolder);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param string $url Accepts both a URL or a filesystem path.
|
|
|
|
*
|
|
|
|
* @return string
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2012-07-01 12:07:07 +02:00
|
|
|
public static function makeRelative($url) {
|
|
|
|
// Allow for the accidental inclusion whitespace and // in the URL
|
|
|
|
$url = trim(preg_replace('#([^:])//#', '\\1/', $url));
|
|
|
|
|
2015-06-12 06:11:30 +02:00
|
|
|
$base1 = self::absoluteBaseURL();
|
2012-07-01 12:07:07 +02:00
|
|
|
$baseDomain = substr($base1, strlen(self::protocol()));
|
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.
|
2016-04-07 03:13:31 +02:00
|
|
|
if (preg_match('/^https?[^:]*:\/\//', $url, $matches)) {
|
2012-07-01 12:07:07 +02:00
|
|
|
$urlProtocol = $matches[0];
|
|
|
|
$urlWithoutProtocol = substr($url, strlen($urlProtocol));
|
|
|
|
|
2010-10-19 03:25:50 +02:00
|
|
|
// If we are already looking at baseURL, return '' (substr will return false)
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($url == $base1) {
|
2012-07-01 12:07:07 +02:00
|
|
|
return '';
|
2016-04-07 03:13:31 +02:00
|
|
|
} elseif (substr($url, 0, strlen($base1)) == $base1) {
|
|
|
|
return substr($url, strlen($base1));
|
|
|
|
} elseif (substr($base1, -1) == "/" && $url == substr($base1, 0, -1)) {
|
|
|
|
// Convert http://www.mydomain.com/mysitedir to ''
|
2012-07-01 12:07:07 +02:00
|
|
|
return "";
|
2016-04-07 03:13:31 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-04-07 03:13:31 +02:00
|
|
|
if (substr($urlWithoutProtocol, 0, strlen($baseDomain)) == $baseDomain) {
|
|
|
|
return substr($urlWithoutProtocol, strlen($baseDomain));
|
2012-07-01 12:07:07 +02:00
|
|
|
}
|
2008-10-02 11:48:56 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +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();
|
2016-04-07 03:13:31 +02:00
|
|
|
if (substr($url, 0, strlen($base2)) == $base2) return substr($url, strlen($base2));
|
2008-10-02 11:48:56 +02:00
|
|
|
|
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();
|
2016-04-07 03:13:31 +02:00
|
|
|
if (substr($url, 0, strlen($base3)) == $base3) {
|
|
|
|
return substr($url, strlen($base3));
|
2012-07-01 12:07:07 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-07-01 12:07:07 +02:00
|
|
|
// Test for relative base url, e.g mywebsite/ if the full url is localhost/myswebsite
|
2016-04-07 03:13:31 +02:00
|
|
|
if (substr($url, 0, strlen($baseDomain)) == $baseDomain) {
|
2012-07-01 12:07:07 +02:00
|
|
|
return substr($url, strlen($baseDomain));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-12 18:16:25 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Returns true if a given path is absolute. Works under both *nix and windows systems.
|
2008-10-12 18:16:25 +02:00
|
|
|
*
|
|
|
|
* @param string $path
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
2008-10-12 18:16:25 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function is_absolute($path) {
|
2016-04-07 03:13:31 +02:00
|
|
|
if (empty($path)) return false;
|
|
|
|
if ($path[0] == '/' || $path[0] == '\\') return true;
|
2008-10-12 18:16:25 +02:00
|
|
|
return preg_match('/^[a-zA-Z]:[\\\\\/]/', $path) == 1;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-12 06:11:30 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Determine if the url is root relative (i.e. starts with /, but not with //) SilverStripe
|
|
|
|
* considers root relative urls as a subset of relative urls.
|
2015-06-12 06:11:30 +02:00
|
|
|
*
|
|
|
|
* @param string $url
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
2015-06-12 06:11:30 +02:00
|
|
|
*/
|
|
|
|
public static function is_root_relative_url($url) {
|
|
|
|
return strpos($url, '/') === 0 && strpos($url, '//') !== 0;
|
|
|
|
}
|
|
|
|
|
2009-02-11 22:07:58 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Checks if a given URL is absolute (e.g. starts with 'http://' etc.). URLs beginning with "//"
|
|
|
|
* are treated as absolute, as browsers take this to mean the same protocol as currently being used.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-04 11:55:40 +02:00
|
|
|
* Note: Can't solely rely on PHP's parse_url() , since it is not intended to work with relative URLs
|
|
|
|
* or for security purposes. filter_var($url, FILTER_VALIDATE_URL) has similar problems.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-02-11 22:07:58 +01:00
|
|
|
* @param string $url
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
2009-02-11 22:07:58 +01:00
|
|
|
*/
|
|
|
|
public static function is_absolute_url($url) {
|
2012-06-29 07:14:28 +02:00
|
|
|
// Strip off the query and fragment parts of the URL before checking
|
2016-04-07 03:13:31 +02:00
|
|
|
if (($queryPosition = strpos($url, '?')) !== false) {
|
2012-06-29 07:14:28 +02:00
|
|
|
$url = substr($url, 0, $queryPosition-1);
|
|
|
|
}
|
2016-04-07 03:13:31 +02:00
|
|
|
if (($hashPosition = strpos($url, '#')) !== false) {
|
2012-06-29 07:14:28 +02:00
|
|
|
$url = substr($url, 0, $hashPosition-1);
|
|
|
|
}
|
2012-05-04 11:55:40 +02:00
|
|
|
$colonPosition = strpos($url, ':');
|
2012-06-29 07:14:28 +02:00
|
|
|
$slashPosition = strpos($url, '/');
|
|
|
|
return (
|
|
|
|
// Base check for existence of a host on a compliant URL
|
|
|
|
parse_url($url, PHP_URL_HOST)
|
|
|
|
// Check for more than one leading slash without a protocol.
|
2015-06-12 06:11:30 +02:00
|
|
|
// While not a RFC compliant absolute URL, it is completed to a valid URL by some browsers,
|
|
|
|
// and hence a potential security risk. Single leading slashes are not an issue though.
|
2013-05-04 01:07:25 +02:00
|
|
|
|| preg_match('%^\s*/{2,}%', $url)
|
2012-06-29 07:14:28 +02:00
|
|
|
|| (
|
|
|
|
// If a colon is found, check if it's part of a valid scheme definition
|
|
|
|
// (meaning its not preceded by a slash).
|
2014-08-15 08:53:05 +02:00
|
|
|
$colonPosition !== FALSE
|
2012-06-29 07:14:28 +02:00
|
|
|
&& ($slashPosition === FALSE || $colonPosition < $slashPosition)
|
|
|
|
)
|
|
|
|
);
|
2009-02-11 22:07:58 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-17 23:22:55 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Checks if a given URL is relative (or root relative) by checking {@link is_absolute_url()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-17 23:22:55 +01:00
|
|
|
* @param string $url
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
2009-03-17 23:22:55 +01:00
|
|
|
*/
|
|
|
|
public static function is_relative_url($url) {
|
2015-06-12 06:11:30 +02:00
|
|
|
return !static::is_absolute_url($url);
|
2009-03-17 23:22:55 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-17 23:22:55 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Checks if the given URL is belonging to this "site" (not an external link). That's the case if
|
|
|
|
* the URL is relative, as defined by {@link is_relative_url()}, or if the host matches
|
|
|
|
* {@link protocolAndHost()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-17 23:22:55 +01:00
|
|
|
* @param string $url
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
2009-03-17 23:22:55 +01:00
|
|
|
*/
|
|
|
|
public static function is_site_url($url) {
|
2012-05-04 11:55:40 +02:00
|
|
|
$urlHost = parse_url($url, PHP_URL_HOST);
|
|
|
|
$actualHost = parse_url(self::protocolAndHost(), PHP_URL_HOST);
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($urlHost && $actualHost && $urlHost == $actualHost) {
|
2012-05-04 11:55:40 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return self::is_relative_url($url);
|
|
|
|
}
|
2009-03-17 23:22:55 +01:00
|
|
|
}
|
2011-03-19 00:38:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a $_SERVER data array and extracts HTTP request headers.
|
|
|
|
*
|
2015-10-06 07:28:04 +02:00
|
|
|
* @param array $server
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
2011-03-19 00:38:30 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function extract_request_headers(array $server) {
|
2011-03-19 00:38:30 +01:00
|
|
|
$headers = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-19 00:38:30 +01:00
|
|
|
foreach($server as $key => $value) {
|
2016-04-07 03:13:31 +02:00
|
|
|
if (substr($key, 0, 5) == 'HTTP_') {
|
2011-03-19 00:38:30 +01:00
|
|
|
$key = substr($key, 5);
|
|
|
|
$key = strtolower(str_replace('_', ' ', $key));
|
|
|
|
$key = str_replace(' ', '-', ucwords($key));
|
|
|
|
$headers[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-04-07 03:13:31 +02:00
|
|
|
if (isset($server['CONTENT_TYPE'])) $headers['Content-Type'] = $server['CONTENT_TYPE'];
|
|
|
|
if (isset($server['CONTENT_LENGTH'])) $headers['Content-Length'] = $server['CONTENT_LENGTH'];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-19 00:38:30 +01:00
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
|
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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-04-09 12:59:30 +02:00
|
|
|
* @param string $file
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
2008-04-09 12:59:30 +02:00
|
|
|
* @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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Returns true if the given file exists. Filename should be relative to the site root.
|
|
|
|
*
|
|
|
|
* @param $file
|
|
|
|
*
|
|
|
|
* @return bool
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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
|
2016-04-07 03:13:31 +02:00
|
|
|
$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.
|
2015-06-12 06:11:30 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-12-08 12:20:20 +01:00
|
|
|
public static function absoluteBaseURL() {
|
2015-06-12 06:11:30 +02:00
|
|
|
return self::absoluteURL(
|
|
|
|
self::baseURL(),
|
|
|
|
self::ROOT
|
|
|
|
);
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Returns the Absolute URL of the site root, embedding the current basic-auth credentials into
|
|
|
|
* the URL.
|
|
|
|
*
|
|
|
|
* @return string
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2012-12-08 12:20:20 +01:00
|
|
|
public static function absoluteBaseURLWithAuth() {
|
2008-08-11 05:03:52 +02:00
|
|
|
$login = "";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if (isset($_SERVER['PHP_AUTH_USER'])) {
|
|
|
|
$login = "$_SERVER[PHP_AUTH_USER]:$_SERVER[PHP_AUTH_PW]@";
|
|
|
|
}
|
2010-10-15 05:09:32 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
return Director::protocol() . $login . $_SERVER['HTTP_HOST'] . Director::baseURL();
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2013-07-05 06:03:51 +02:00
|
|
|
/**
|
|
|
|
* Skip any further processing and immediately respond with a redirect to the passed URL.
|
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param string $destURL
|
2013-07-05 06:03:51 +02:00
|
|
|
*/
|
|
|
|
protected static function force_redirect($destURL) {
|
2016-09-09 08:43:05 +02:00
|
|
|
$response = new HTTPResponse();
|
2015-03-20 05:30:37 +01:00
|
|
|
$response->redirect($destURL, 301);
|
2013-07-05 06:03:51 +02:00
|
|
|
|
|
|
|
HTTP::add_cache_headers($response);
|
|
|
|
|
|
|
|
// TODO: Use an exception - ATM we can be called from _config.php, before Director#handleRequest's try block
|
|
|
|
$response->output();
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-19 02:32:42 +02:00
|
|
|
* Force the site to run on SSL.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-19 02:32:42 +02:00
|
|
|
* To use, call from _config.php. For example:
|
2008-03-03 00:24:10 +01:00
|
|
|
* <code>
|
2016-04-07 03:13:31 +02:00
|
|
|
* if (Director::isLive()) Director::forceSSL();
|
2008-03-03 00:24:10 +01:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-19 02:32:42 +02:00
|
|
|
* If you don't want your entire site to be on SSL, you can pass an array of PCRE regular expression
|
|
|
|
* patterns for matching relative URLs. For example:
|
|
|
|
* <code>
|
2016-04-07 03:13:31 +02:00
|
|
|
* if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/'));
|
2010-10-19 02:32:42 +02:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-04-03 05:53:58 +02:00
|
|
|
* If you want certain parts of your site protected under a different domain, you can specify
|
|
|
|
* the domain as an argument:
|
|
|
|
* <code>
|
2016-04-07 03:13:31 +02:00
|
|
|
* if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/'), 'secure.mysite.com');
|
2013-04-03 05:53:58 +02:00
|
|
|
* </code>
|
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* Note that the session data will be lost when moving from HTTP to HTTPS. It is your responsibility
|
|
|
|
* to ensure that this won't cause usability problems.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-19 02:32:42 +02:00
|
|
|
* CAUTION: This does not respect the site environment mode. You should check this
|
|
|
|
* as per the above examples using Director::isLive() or Director::isTest() for example.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @param array $patterns Array of regex patterns to match URLs that should be HTTPS.
|
|
|
|
* @param string $secureDomain Secure domain to redirect to. Defaults to the current domain.
|
|
|
|
*
|
|
|
|
* @return bool|string String of URL when unit tests running, boolean FALSE if patterns don't match request URI.
|
2010-10-19 02:32:42 +02:00
|
|
|
*/
|
2013-04-03 05:53:58 +02:00
|
|
|
public static function forceSSL($patterns = null, $secureDomain = null) {
|
2016-08-19 00:51:35 +02:00
|
|
|
// Calling from the command-line?
|
|
|
|
if (!isset($_SERVER['REQUEST_URI'])) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:42 +02:00
|
|
|
$matched = false;
|
|
|
|
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($patterns) {
|
2010-10-19 02:32:42 +02:00
|
|
|
$relativeURL = self::makeRelative(Director::absoluteURL($_SERVER['REQUEST_URI']));
|
2013-04-03 05:53:58 +02:00
|
|
|
|
|
|
|
// protect portions of the site based on the pattern
|
2010-10-19 02:32:42 +02:00
|
|
|
foreach($patterns as $pattern) {
|
2016-04-07 03:13:31 +02:00
|
|
|
if (preg_match($pattern, $relativeURL)) {
|
2010-10-19 02:32:42 +02:00
|
|
|
$matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// protect the entire site
|
|
|
|
$matched = true;
|
|
|
|
}
|
|
|
|
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($matched && !self::is_https()) {
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2013-04-03 05:53:58 +02:00
|
|
|
// if an domain is specified, redirect to that instead of the current domain
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($secureDomain) {
|
2013-04-03 05:53:58 +02:00
|
|
|
$url = 'https://' . $secureDomain . $_SERVER['REQUEST_URI'];
|
|
|
|
} else {
|
|
|
|
$url = $_SERVER['REQUEST_URI'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$destURL = str_replace('http:', 'https:', Director::absoluteURL($url));
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2010-10-19 02:50:37 +02:00
|
|
|
// This coupling to SapphireTest is necessary to test the destination URL and to not interfere with tests
|
2016-08-19 00:51:35 +02:00
|
|
|
if (class_exists('SilverStripe\\Dev\\SapphireTest', false) && SapphireTest::is_running_test()) {
|
2010-10-19 02:32:42 +02:00
|
|
|
return $destURL;
|
|
|
|
} else {
|
2013-07-05 06:03:51 +02:00
|
|
|
self::force_redirect($destURL);
|
2016-08-19 00:51:35 +02:00
|
|
|
return true;
|
2010-10-19 02:32:42 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
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-03-03 00:24:10 +01:00
|
|
|
* Force a redirect to a domain starting with "www."
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function forceWWW() {
|
2016-04-07 03:13:31 +02:00
|
|
|
if (!Director::isDev() && !Director::isTest() && strpos($_SERVER['HTTP_HOST'], 'www') !== 0) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$destURL = str_replace(Director::protocol(), Director::protocol() . 'www.',
|
2012-09-26 23:34:00 +02:00
|
|
|
Director::absoluteURL($_SERVER['REQUEST_URI']));
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2013-07-05 06:03:51 +02:00
|
|
|
self::force_redirect($destURL);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Checks if the current HTTP-Request is an "Ajax-Request" by checking for a custom header set by
|
|
|
|
* jQuery or whether a manually set request-parameter 'ajax' is present.
|
2007-09-14 19:47:45 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function is_ajax() {
|
2016-04-07 03:13:31 +02:00
|
|
|
if (Controller::has_curr()) {
|
2012-04-05 14:44:42 +02:00
|
|
|
return Controller::curr()->getRequest()->isAjax();
|
2007-08-21 00:39:44 +02:00
|
|
|
} 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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-11-11 03:54:19 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Returns true if this script is being run from the command line rather than the web server.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @return bool
|
2007-11-11 03:54:19 +01:00
|
|
|
*/
|
|
|
|
public static function is_cli() {
|
2010-04-13 01:15:04 +02:00
|
|
|
return (php_sapi_name() == "cli");
|
2007-11-11 03:54:19 +01: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
|
|
|
* Set the environment type of the current site.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Typically, a SilverStripe site have a number of environments:
|
2015-11-13 23:44:49 +01:00
|
|
|
* - 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* To help with this, SilverStripe supports the notion of an environment type. The environment
|
|
|
|
* type can be dev, test, or live.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* You can set it explicitly with {@link Director::set_environment_type()}. Or you can use
|
|
|
|
* {@link Director::$dev_servers} and {@link Director::$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.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01: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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* Generally speaking, these methods will be called from your _config.php file.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* Once the environment type is set, it can be checked with {@link Director::isDev()},
|
|
|
|
* {@link Director::isTest()}, and {@link Director::isLive()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "Director.environment_type" config setting instead
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
|
|
|
* @param $et string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_environment_type($et) {
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($et != 'dev' && $et != 'test' && $et != 'live') {
|
2012-09-26 23:34:00 +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 {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "Director.environment_type" config setting instead');
|
2016-08-19 00:51:35 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Control\\Director', 'environment_type', $et);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-01 16:40:29 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Can also be checked with {@link Director::isDev()}, {@link Director::isTest()}, and
|
|
|
|
* {@link Director::isLive()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @return bool|string
|
2008-10-01 16:40:29 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_environment_type() {
|
2016-04-07 03:13:31 +02:00
|
|
|
if (Director::isLive()) {
|
2008-10-01 16:40:29 +02:00
|
|
|
return 'live';
|
2016-04-07 03:13:31 +02:00
|
|
|
} elseif (Director::isTest()) {
|
2008-10-01 16:40:29 +02:00
|
|
|
return 'test';
|
2016-04-07 03:13:31 +02:00
|
|
|
} elseif (Director::isDev()) {
|
2008-10-01 16:40:29 +02:00
|
|
|
return 'dev';
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2015-11-13 23:44:49 +01:00
|
|
|
/**
|
|
|
|
* This function will return true if the site is in a live environment. For information about
|
|
|
|
* environment types, see {@link Director::set_environment_type()}.
|
|
|
|
*
|
|
|
|
* @return bool
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2014-07-01 09:07:13 +02:00
|
|
|
public static function isLive() {
|
|
|
|
return !(Director::isDev() || Director::isTest());
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +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()}.
|
|
|
|
*
|
|
|
|
* @return bool
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2014-07-01 09:07:13 +02:00
|
|
|
public static function isDev() {
|
|
|
|
// Check session
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($env = self::session_environment()) return $env === 'dev';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
// Check config
|
2016-08-19 00:51:35 +02:00
|
|
|
if (Config::inst()->get('SilverStripe\\Control\\Director', 'environment_type') === 'dev') return true;
|
2013-03-21 19:48:54 +01:00
|
|
|
|
|
|
|
// Check if we are running on one of the test servers
|
2016-08-19 00:51:35 +02:00
|
|
|
$devServers = (array)Config::inst()->get('SilverStripe\\Control\\Director', 'dev_servers');
|
2016-04-07 03:13:31 +02:00
|
|
|
if (isset($_SERVER['HTTP_HOST']) && in_array($_SERVER['HTTP_HOST'], $devServers)) {
|
2013-03-21 19:48:54 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-09 02:15:34 +01:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +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()}.
|
|
|
|
*
|
|
|
|
* @return bool
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2014-07-01 09:07:13 +02:00
|
|
|
public static function isTest() {
|
|
|
|
// In case of isDev and isTest both being set, dev has higher priority
|
2016-04-07 03:13:31 +02:00
|
|
|
if (self::isDev()) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
// Check saved session
|
2016-04-07 03:13:31 +02:00
|
|
|
if ($env = self::session_environment()) return $env === 'test';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
// Check config
|
2016-08-19 00:51:35 +02:00
|
|
|
if (Config::inst()->get('SilverStripe\\Control\\Director', 'environment_type') === 'test') return true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-27 23:50:10 +02:00
|
|
|
// Check if we are running on one of the test servers
|
2016-08-19 00:51:35 +02:00
|
|
|
$testServers = (array)Config::inst()->get('SilverStripe\\Control\\Director', 'test_servers');
|
2016-04-07 03:13:31 +02:00
|
|
|
if (isset($_SERVER['HTTP_HOST']) && in_array($_SERVER['HTTP_HOST'], $testServers)) {
|
2007-09-27 23:50:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-27 23:50:10 +02:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Check or update any temporary environment specified in the session.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-11-13 23:44:49 +01:00
|
|
|
* @return null|string
|
2014-07-01 09:07:13 +02:00
|
|
|
*/
|
|
|
|
protected static function session_environment() {
|
|
|
|
// Set session from querystring
|
2016-04-07 03:13:31 +02:00
|
|
|
if (isset($_GET['isDev'])) {
|
|
|
|
if (isset($_SESSION)) {
|
2014-07-01 09:07:13 +02:00
|
|
|
unset($_SESSION['isTest']); // In case we are changing from test mode
|
|
|
|
$_SESSION['isDev'] = $_GET['isDev'];
|
|
|
|
}
|
|
|
|
return 'dev';
|
2016-04-07 03:13:31 +02:00
|
|
|
} elseif (isset($_GET['isTest'])) {
|
|
|
|
if (isset($_SESSION)) {
|
2014-07-01 09:07:13 +02:00
|
|
|
unset($_SESSION['isDev']); // In case we are changing from dev mode
|
|
|
|
$_SESSION['isTest'] = $_GET['isTest'];
|
|
|
|
}
|
|
|
|
return 'test';
|
|
|
|
}
|
|
|
|
// Check session
|
2016-04-07 03:13:31 +02:00
|
|
|
if (isset($_SESSION['isDev']) && $_SESSION['isDev']) {
|
2014-07-01 09:07:13 +02:00
|
|
|
return 'dev';
|
2016-04-07 03:13:31 +02:00
|
|
|
} elseif (isset($_SESSION['isTest']) && $_SESSION['isTest']) {
|
2014-07-01 09:07:13 +02:00
|
|
|
return 'test';
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-02-11 03:08:39 +01:00
|
|
|
/**
|
2015-11-13 23:44:49 +01:00
|
|
|
* Returns an array of strings of the method names of methods on the call that should be exposed
|
2012-02-11 03:08:39 +01:00
|
|
|
* as global variables in the templates.
|
2015-11-13 23:44:49 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2012-02-11 03:08:39 +01:00
|
|
|
*/
|
2012-02-21 01:36:34 +01:00
|
|
|
public static function get_template_global_variables() {
|
2012-02-11 03:08:39 +01:00
|
|
|
return array(
|
|
|
|
'absoluteBaseURL',
|
|
|
|
'baseURL',
|
|
|
|
'is_ajax',
|
|
|
|
'isAjax' => 'is_ajax',
|
|
|
|
'BaseHref' => 'absoluteBaseURL', //@deprecated 3.0
|
|
|
|
);
|
|
|
|
}
|
2012-02-27 22:14:02 +01:00
|
|
|
}
|