2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Director is responsible for processing the URL
|
2007-09-14 19:47:45 +02:00
|
|
|
* Director is the first step in the "execution pipeline". It parses the URL, matching it to
|
2007-07-19 12:40:28 +02:00
|
|
|
* one of a number of patterns, and determines the controller, action and any argument to be
|
|
|
|
* used. It then runs the controller, which will finally run the viewer and/or perform processing
|
|
|
|
* steps.
|
|
|
|
*/
|
|
|
|
class Director {
|
|
|
|
static private $urlSegment;
|
|
|
|
static private $urlParams;
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static private $rules = array();
|
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) {
|
|
|
|
return Session::get('history.' . sizeof(Session::get('history')) - $pagesBack - 1);
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Add new rules
|
|
|
|
*/
|
|
|
|
static function addRules($priority, $rules) {
|
|
|
|
Director::$rules[$priority] = isset(Director::$rules[$priority]) ? array_merge($rules, (array)Director::$rules[$priority]) : $rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the given URL, creating the appropriate controller and executing it
|
2007-09-14 19:47:45 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function direct($url) {
|
|
|
|
if(isset($_GET['debug_profile'])) Profiler::mark("Director","direct");
|
|
|
|
$controllerObj = Director::getControllerForURL($url);
|
2007-08-17 07:45:15 +02:00
|
|
|
|
|
|
|
// Load the session into the controller
|
|
|
|
$controllerObj->setSession(new Session($_SESSION));
|
2007-08-17 05:09:46 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_string($controllerObj) && substr($controllerObj,0,9) == 'redirect:') {
|
|
|
|
Director::redirect(substr($controllerObj, 9));
|
2007-08-17 05:09:46 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
} else if($controllerObj) {
|
2007-08-17 05:09:46 +02:00
|
|
|
$response = $controllerObj->run(array_merge((array)$_GET, (array)$_POST, (array)$_FILES));
|
2007-08-17 07:45:15 +02:00
|
|
|
|
|
|
|
// Save the updated session back
|
|
|
|
$_SESSION = $controllerObj->getSession()->inst_getAll();
|
2007-09-16 17:31:44 +02:00
|
|
|
|
|
|
|
if(isset($_GET['debug_profile'])) Profiler::mark("Outputting to browser");
|
2007-08-17 05:09:46 +02:00
|
|
|
$response->output();
|
2007-09-16 17:31:44 +02:00
|
|
|
if(isset($_GET['debug_profile'])) Profiler::unmark("Outputting to browser");
|
2007-08-17 05:09:46 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
if(isset($_GET['debug_profile'])) Profiler::unmark("Director","direct");
|
|
|
|
}
|
2007-08-21 00:39:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a URL request, returning a response object.
|
|
|
|
* @param $url The URL to visit
|
|
|
|
* @param $post The $_POST & $_FILES variables
|
|
|
|
* @param $session The {@link Session} object representing the current session.
|
|
|
|
*/
|
|
|
|
function test($url, $post = null, $session = null) {
|
|
|
|
$getVars = array();
|
|
|
|
if(strpos($url,'?') !== false) {
|
|
|
|
list($url, $getVarsEncoded) = explode('?', $url, 2);
|
|
|
|
parse_str($getVarsEncoded, $getVars);
|
|
|
|
}
|
|
|
|
|
|
|
|
$controllerObj = Director::getControllerForURL($url);
|
|
|
|
|
|
|
|
// Load the session into the controller
|
|
|
|
$controllerObj->setSession($session ? $session : new Session(null));
|
|
|
|
|
|
|
|
if(is_string($controllerObj) && substr($controllerObj,0,9) == 'redirect:') {
|
|
|
|
user_error("Redirection not implemented in Director::test", E_USER_ERROR);
|
|
|
|
|
|
|
|
} else if($controllerObj) {
|
|
|
|
$response = $controllerObj->run( array_merge($getVars, (array)$post) );
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
static function getControllerForURL($url) {
|
|
|
|
if(isset($_GET['debug_profile'])) Profiler::mark("Director","getControllerForURL");
|
|
|
|
$url = preg_replace( array( '/\/+/','/^\//', '/\/$/'),array('/','',''),$url);
|
|
|
|
$urlParts = split('/+', $url);
|
2007-09-14 19:47:45 +02:00
|
|
|
|
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) {
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($rules as $pattern => $controller) {
|
|
|
|
$patternParts = explode('/', $pattern);
|
|
|
|
$matched = true;
|
|
|
|
$arguments = array();
|
|
|
|
foreach($patternParts as $i => $part) {
|
|
|
|
$part = trim($part);
|
|
|
|
if(isset($part[0]) && $part[0] == '$') {
|
|
|
|
$arguments[substr($part,1)] = isset($urlParts[$i]) ? $urlParts[$i] : null;
|
|
|
|
if($part == '$Controller' && !class_exists($arguments['Controller'])) {
|
|
|
|
$matched = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if(!isset($urlParts[$i]) || $urlParts[$i] != $part) {
|
|
|
|
$matched = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($matched) {
|
|
|
|
|
|
|
|
if(substr($controller,0,2) == '->') {
|
|
|
|
if($_REQUEST['debug'] == 1) Debug::message("Redirecting to $controller");
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($_GET['debug_profile'])) Profiler::unmark("Director","getControllerForURL");
|
|
|
|
return "redirect:" . Director::absoluteURL(substr($controller,2), true);
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
if(isset($arguments['Controller']) && $controller == "*") {
|
|
|
|
$controller = $arguments['Controller'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($_REQUEST['debug'])) Debug::message("Using controller $controller");
|
|
|
|
if(isset($arguments['Action'])) {
|
|
|
|
$arguments['Action'] = str_replace('-','',$arguments['Action']);
|
|
|
|
}
|
|
|
|
if(isset($arguments['Action']) && ClassInfo::exists($controller.'_'.$arguments['Action']))
|
2007-09-14 19:47:45 +02:00
|
|
|
$controller = $controller.'_'.$arguments['Action'];
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
Director::$urlParams = $arguments;
|
|
|
|
$controllerObj = new $controller();
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$controllerObj->setURLParams($arguments);
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($arguments['URLSegment'])) self::$urlSegment = $arguments['URLSegment'] . "/";
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($_GET['debug_profile'])) Profiler::unmark("Director","getControllerForURL");
|
|
|
|
return $controllerObj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static function urlParam($name) {
|
|
|
|
return Director::$urlParams[$name];
|
|
|
|
}
|
|
|
|
static function urlParams() {
|
|
|
|
return Director::$urlParams;
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static function currentPage() {
|
|
|
|
if(isset(Director::$urlParams['URLSegment'])) {
|
|
|
|
$SQL_urlSegment = Convert::raw2sql(Director::$urlParams['URLSegment']);
|
|
|
|
return DataObject::get_one("SiteTree", "URLSegment = '$SQL_urlSegment'");
|
|
|
|
} else {
|
|
|
|
return Controller::currentController();
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static function absoluteURL($url, $relativeToSiteBase = false) {
|
|
|
|
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;
|
|
|
|
$url = self::protocolAndHost() . $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function protocolAndHost() {
|
|
|
|
$s = (isset($_SERVER['SSL']) || isset($_SERVER['HTTPS'])) ? 's' : '';
|
|
|
|
return "http$s://" . $_SERVER['HTTP_HOST'];
|
|
|
|
}
|
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
|
|
|
*/
|
|
|
|
static function redirect($url) {
|
2007-08-17 05:09:46 +02:00
|
|
|
Controller::curr()->redirect($url);
|
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() {
|
|
|
|
Controller::curr()->redirectedTo();
|
|
|
|
}
|
|
|
|
|
2007-09-16 17:34:05 +02:00
|
|
|
/*
|
|
|
|
* Redirect back
|
|
|
|
*
|
|
|
|
* Uses either the HTTP_REFERER or a manually set request-variable called
|
|
|
|
* _REDIRECT_BACK_URL.
|
|
|
|
* This variable is needed in scenarios where not HTTP-Referer is sent (
|
|
|
|
* e.g when calling a page by location.href in IE).
|
|
|
|
* If none of the two variables is available, it will redirect to the base
|
|
|
|
* URL (see {@link baseURL()}).
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function redirectBack() {
|
2007-09-16 17:34:05 +02:00
|
|
|
$url = self::baseURL();
|
|
|
|
|
|
|
|
if(isset($_REQUEST['_REDIRECT_BACK_URL'])) {
|
|
|
|
$url = $_REQUEST['_REDIRECT_BACK_URL'];
|
|
|
|
} else if(isset($_SERVER['HTTP_REFERER'])) {
|
|
|
|
$_SERVER['HTTP_REFERER'];
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
Director::redirect($url);
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static function currentURLSegment() {
|
|
|
|
return Director::$urlSegment;
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Returns a URL for the given controller
|
|
|
|
*/
|
|
|
|
static $alternateBaseURL;
|
|
|
|
static function baseURL() {
|
|
|
|
if(self::$alternateBaseURL) return self::$alternateBaseURL;
|
|
|
|
else {
|
|
|
|
$base = dirname(dirname($_SERVER['SCRIPT_NAME']));
|
|
|
|
if($base == '/' || $base == '\\') return '/';
|
|
|
|
else return $base . '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static function setBaseURL($baseURL) {
|
|
|
|
self::$alternateBaseURL = $baseURL;
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $alternateBaseFolder;
|
|
|
|
static function baseFolder() {
|
|
|
|
if(self::$alternateBaseFolder) return self::$alternateBaseFolder;
|
|
|
|
else return dirname(dirname($_SERVER['SCRIPT_FILENAME']));
|
|
|
|
}
|
|
|
|
static function setBaseFolder($baseFolder) {
|
|
|
|
self::$alternateBaseFolder = $baseFolder;
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static function makeRelative($url) {
|
|
|
|
$base1 = self::absoluteBaseURL();
|
|
|
|
$base2 = self::baseFolder();
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Allow for the accidental inclusion of a // in the URL
|
|
|
|
$url = ereg_replace('([^:])//','\\1/',$url);
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(substr($url,0,strlen($base1)) == $base1) return substr($url,strlen($base1));
|
|
|
|
if(substr($url,0,strlen($base2)) == $base2) return substr($url,strlen($base2));
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function getAbsURL($url) {
|
|
|
|
return Director::baseURL() . '/' . $url;
|
|
|
|
}
|
|
|
|
static function getAbsFile($file) {
|
|
|
|
if($file[0] == '/') return $file;
|
|
|
|
return Director::baseFolder() . '/' . $file;
|
|
|
|
}
|
|
|
|
static function fileExists($file) {
|
|
|
|
return file_exists(Director::getAbsFile($file));
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the Absolute URL for the given controller
|
|
|
|
*/
|
|
|
|
static function absoluteBaseURL() {
|
|
|
|
return Director::absoluteURL(Director::baseURL());
|
|
|
|
}
|
|
|
|
static function absoluteBaseURLWithAuth() {
|
|
|
|
if($_SERVER['PHP_AUTH_USER'])
|
|
|
|
$login = "$_SERVER[PHP_AUTH_USER]:$_SERVER[PHP_AUTH_PW]@";
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($_SERVER['SSL']) $s = "s";
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Force the site to run on SSL. To use, call from _config.php
|
|
|
|
*/
|
|
|
|
static function forceSSL() {
|
|
|
|
if(!isset($_SERVER['HTTPS']) && !Director::isDev()){
|
|
|
|
$destURL = str_replace('http:','https:',Director::absoluteURL($_SERVER['REQUEST_URI']));
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
header("Location: $destURL");
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Force a redirect to www.domain
|
|
|
|
*/
|
|
|
|
static function forceWWW() {
|
|
|
|
if(!Director::isDev() && !Director::isTest() && strpos( $_SERVER['SERVER_NAME'], 'www') !== 0 ){
|
|
|
|
if( $_SERVER['HTTPS'] )
|
|
|
|
$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
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
header("Location: $destURL");
|
|
|
|
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-09-14 19:47:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $siteMode;
|
2007-09-14 02:54:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the site mode (if it is the public site or the cms),
|
|
|
|
* and runs registered modules.
|
|
|
|
*/
|
2007-09-16 17:31:44 +02:00
|
|
|
static protected $mode_additions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the site mode (if it is the public site or the cms),
|
|
|
|
* and runs registered modules.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function set_site_mode($mode) {
|
|
|
|
Director::$siteMode = $mode;
|
2007-09-14 02:54:02 +02:00
|
|
|
|
2007-09-16 17:31:44 +02:00
|
|
|
if(isset(self::$mode_additions[$mode])) {
|
|
|
|
foreach(self::$mode_additions[$mode] as $extension) {
|
|
|
|
call_user_func($extension);
|
|
|
|
}
|
2007-09-14 02:54:02 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
static function get_site_mode() {
|
|
|
|
return Director::$siteMode;
|
|
|
|
}
|
2007-09-16 17:31:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows a module to register with the director to be run once
|
|
|
|
* the controller is instantiated. The optional 'mode' parameter
|
|
|
|
* can be either 'site' or 'cms', as those are the two values currently
|
|
|
|
* set by controllers. The callback function will be run at the
|
|
|
|
* initialization of the relavant controller.
|
|
|
|
*/
|
|
|
|
static function extendSite($function, $mode='site') {
|
|
|
|
self::$mode_additions[$mode][] = $function;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static protected $environment_type;
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Force the environment type to be dev, test or live.
|
|
|
|
* This will affect the results of isLive, isDev, and isTest
|
|
|
|
*/
|
|
|
|
static function set_environment_type($et) {
|
|
|
|
if($et != 'dev' && $et != 'test' && $et != 'live') {
|
|
|
|
user_error("Director::set_environment_type passed '$et'. It should be passed dev, test, or live");
|
|
|
|
} else {
|
|
|
|
self::$environment_type = $et;
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static function isLive() {
|
|
|
|
return !(Director::isDev() || Director::isTest());
|
|
|
|
}
|
|
|
|
static function isDev() {
|
|
|
|
if(self::$environment_type) return self::$environment_type == 'dev';
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Use ?isDev=1 to get development access on the live server
|
|
|
|
if(isset($_GET['isDev'])) {
|
|
|
|
if(ClassInfo::ready()) {
|
|
|
|
BasicAuth::requireLogin("SilverStripe developer access. Use your CMS login", "ADMIN");
|
|
|
|
$_SESSION['isDev'] = $_GET['isDev'];
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($_SESSION['isDev']) && $_SESSION['isDev']) return true;
|
2007-09-14 19:47:45 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Anything running on localhost is automatically dev
|
|
|
|
if($_SERVER['SERVER_ADDR'] == '0.0.0.0' || $_SERVER['REMOTE_ADDR'] == $_SERVER['SERVER_ADDR']) return true;
|
|
|
|
|
|
|
|
$testURL = ".$_SERVER[HTTP_HOST].";
|
|
|
|
return (strpos($testURL, '.test.') === false) && (strpos($testURL, '.dev.') !== false) ;
|
|
|
|
}
|
|
|
|
static function isTest() {
|
|
|
|
if(self::$environment_type) return self::$environment_type == 'test';
|
2007-09-14 19:47:45 +02:00
|
|
|
|
|
|
|
$testURL = ".$_SERVER[HTTP_HOST].";
|
2007-07-19 12:40:28 +02:00
|
|
|
return (strpos($testURL, '.test.') !== false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo These functions are deprecated, let's use isLive isDev and isTest instead.
|
|
|
|
*/
|
|
|
|
function isDevMode() { return self::isDev(); }
|
|
|
|
function isTestMode() { return self::isTest(); }
|
|
|
|
function isLiveMode() { return self::isLive(); }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|