Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'origin/3.0' into 3.1

  • Loading branch information...
commit 541436feb0fa38bb42490e19f228392f08cb8398 2 parents 23d1e8c + 3c6ba1c
@hafriedlander hafriedlander authored
View
230 core/Constants.php
@@ -0,0 +1,230 @@
+<?php
+/**
+ * This file is the Framework constants bootstrap. It will prepare some basic common constants.
+ *
+ * It takes care of:
+ * - Including _ss_environment.php
+ * - Normalisation of $_SERVER values
+ * - Initialisation of necessary constants (mostly paths)
+ *
+ * Initialized constants:
+ * - BASE_URL: Full URL to the webroot, e.g. "http://my-host.com/my-webroot" (no trailing slash).
+ * - BASE_PATH: Absolute path to the webroot, e.g. "/var/www/my-webroot" (no trailing slash).
+ * See Director::baseFolder(). Can be overwritten by Config::inst()->update('Director', 'alternate_base_folder', ).
+ * - TEMP_FOLDER: Absolute path to temporary folder, used for manifest and template caches. Example: "/var/tmp"
+ * See getTempFolder(). No trailing slash.
+ * - MODULES_DIR: Not used at the moment
+ * - MODULES_PATH: Not used at the moment
+ * - THEMES_DIR: Path relative to webroot, e.g. "themes"
+ * - THEMES_PATH: Absolute filepath, e.g. "/var/www/my-webroot/themes"
+ * - FRAMEWORK_DIR: Path relative to webroot, e.g. "framework"
+ * - FRAMEWORK_PATH:Absolute filepath, e.g. "/var/www/my-webroot/framework"
+ * - FRAMEWORK_ADMIN_DIR: Path relative to webroot, e.g. "framework/admin"
+ * - FRAMEWORK_ADMIN_PATH: Absolute filepath, e.g. "/var/www/my-webroot/framework/admin"
+ * - THIRDPARTY_DIR: Path relative to webroot, e.g. "framework/thirdparty"
+ * - THIRDPARTY_PATH: Absolute filepath, e.g. "/var/www/my-webroot/framework/thirdparty"
+ *
+ * @package framework
+ * @subpackage core
+ */
+
+///////////////////////////////////////////////////////////////////////////////
+// ENVIRONMENT CONFIG
+
+/**
+ * Include _ss_environment.php file
+ */
+//define the name of the environment file
+$envFile = '_ss_environment.php';
+//define the dirs to start scanning from (have to add the trailing slash)
+// we're going to check the realpath AND the path as the script sees it
+$dirsToCheck = array(
+ realpath('.'),
+ dirname($_SERVER['SCRIPT_FILENAME'])
+);
+//if they are the same, remove one of them
+if ($dirsToCheck[0] == $dirsToCheck[1]) {
+ unset($dirsToCheck[1]);
+}
+foreach ($dirsToCheck as $dir) {
+ //check this dir and every parent dir (until we hit the base of the drive)
+ // or until we hit a dir we can't read
+ do {
+ //add the trailing slash we need to concatenate properly
+ $dir .= DIRECTORY_SEPARATOR;
+ //if it's readable, go ahead
+ if (@is_readable($dir)) {
+ //if the file exists, then we include it, set relevant vars and break out
+ if (file_exists($dir . $envFile)) {
+ define('SS_ENVIRONMENT_FILE', $dir . $envFile);
+ include_once(SS_ENVIRONMENT_FILE);
+ //break out of BOTH loops because we found the $envFile
+ break(2);
+ }
+ }
+ else {
+ //break out of the while loop, we can't read the dir
+ break;
+ }
+ //go up a directory
+ $dir = dirname($dir);
+ //here we need to check that the path of the last dir and the next one are
+ // not the same, if they are, we have hit the root of the drive
+ } while (dirname($dir) != $dir);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// GLOBALS AND DEFINE SETTING
+
+/**
+ * A blank HTTP_HOST value is used to detect command-line execution.
+ * We update the $_SERVER variable to contain data consistent with the rest of the application.
+ */
+if(!isset($_SERVER['HTTP_HOST'])) {
+ // HTTP_HOST, REQUEST_PORT, SCRIPT_NAME, and PHP_SELF
+ if(isset($_FILE_TO_URL_MAPPING)) {
+ $fullPath = $testPath = realpath($_SERVER['SCRIPT_FILENAME']);
+ while($testPath && $testPath != '/' && !preg_match('/^[A-Z]:\\\\$/', $testPath)) {
+ if(isset($_FILE_TO_URL_MAPPING[$testPath])) {
+ $url = $_FILE_TO_URL_MAPPING[$testPath]
+ . str_replace(DIRECTORY_SEPARATOR, '/', substr($fullPath,strlen($testPath)));
+
+ $components = parse_url($url);
+ $_SERVER['HTTP_HOST'] = $components['host'];
+ if(!empty($components['port'])) $_SERVER['HTTP_HOST'] .= ':' . $components['port'];
+ $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'] = $components['path'];
+ if(!empty($components['port'])) $_SERVER['REQUEST_PORT'] = $components['port'];
+ break;
+ }
+ $testPath = dirname($testPath);
+ }
+ }
+
+ // Everything else
+ $serverDefaults = array(
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ 'HTTP_ACCEPT' => 'text/plain;q=0.5',
+ 'HTTP_ACCEPT_LANGUAGE' => '*;q=0.5',
+ 'HTTP_ACCEPT_ENCODING' => '',
+ 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1;q=0.5',
+ 'SERVER_SIGNATURE' => 'Command-line PHP/' . phpversion(),
+ 'SERVER_SOFTWARE' => 'PHP/' . phpversion(),
+ 'SERVER_ADDR' => '127.0.0.1',
+ 'REMOTE_ADDR' => '127.0.0.1',
+ 'REQUEST_METHOD' => 'GET',
+ 'HTTP_USER_AGENT' => 'CLI',
+ );
+
+ $_SERVER = array_merge($serverDefaults, $_SERVER);
+
+ /**
+ * If we have an HTTP_HOST value, then we're being called from the webserver and there are some things that
+ * need checking
+ */
+} else {
+ /**
+ * Fix magic quotes setting
+ */
+ if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
+ if($_REQUEST) stripslashes_recursively($_REQUEST);
+ if($_GET) stripslashes_recursively($_GET);
+ if($_POST) stripslashes_recursively($_POST);
+ if($_COOKIE) stripslashes_recursively($_COOKIE);
+ // No more magic_quotes!
+ trigger_error('get_magic_quotes_gpc support is being removed from Silverstripe. Please set this to off in ' .
+ ' your php.ini and see http://php.net/manual/en/security.magicquotes.php', E_USER_WARNING);
+ }
+
+ /**
+ * Fix HTTP_HOST from reverse proxies
+ */
+ if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
+ // Get the first host, in case there's multiple separated through commas
+ $_SERVER['HTTP_HOST'] = strtok($_SERVER['HTTP_X_FORWARDED_HOST'], ',');
+ }
+}
+
+/**
+ * Define system paths
+ */
+if(!defined('BASE_PATH')) {
+ // Assuming that this file is framework/core/Core.php we can then determine the base path
+ $candidateBasePath = rtrim(dirname(dirname(dirname(__FILE__))), DIRECTORY_SEPARATOR);
+ // We can't have an empty BASE_PATH. Making it / means that double-slashes occur in places but that's benign.
+ // This likely only happens on chrooted environemnts
+ if($candidateBasePath == '') $candidateBasePath = DIRECTORY_SEPARATOR;
+ define('BASE_PATH', $candidateBasePath);
+}
+if(!defined('BASE_URL')) {
+ // Determine the base URL by comparing SCRIPT_NAME to SCRIPT_FILENAME and getting common elements
+ $path = realpath($_SERVER['SCRIPT_FILENAME']);
+ if(substr($path, 0, strlen(BASE_PATH)) == BASE_PATH) {
+ $urlSegmentToRemove = substr($path, strlen(BASE_PATH));
+ if(substr($_SERVER['SCRIPT_NAME'], -strlen($urlSegmentToRemove)) == $urlSegmentToRemove) {
+ $baseURL = substr($_SERVER['SCRIPT_NAME'], 0, -strlen($urlSegmentToRemove));
+ define('BASE_URL', rtrim($baseURL, DIRECTORY_SEPARATOR));
+ }
+ }
+
+ // If that didn't work, failover to the old syntax. Hopefully this isn't necessary, and maybe
+ // if can be phased out?
+ if(!defined('BASE_URL')) {
+ $dir = (strpos($_SERVER['SCRIPT_NAME'], 'index.php') !== false)
+ ? dirname($_SERVER['SCRIPT_NAME'])
+ : dirname(dirname($_SERVER['SCRIPT_NAME']));
+ define('BASE_URL', rtrim($dir, DIRECTORY_SEPARATOR));
+ }
+}
+define('MODULES_DIR', 'modules');
+define('MODULES_PATH', BASE_PATH . '/' . MODULES_DIR);
+define('THEMES_DIR', 'themes');
+define('THEMES_PATH', BASE_PATH . '/' . THEMES_DIR);
+// Relies on this being in a subdir of the framework.
+// If it isn't, or is symlinked to a folder with a different name, you must define FRAMEWORK_DIR
+if(!defined('FRAMEWORK_DIR')) {
+ define('FRAMEWORK_DIR', basename(dirname(dirname(__FILE__))));
+}
+define('FRAMEWORK_PATH', BASE_PATH . '/' . FRAMEWORK_DIR);
+define('FRAMEWORK_ADMIN_DIR', FRAMEWORK_DIR . '/admin');
+define('FRAMEWORK_ADMIN_PATH', BASE_PATH . '/' . FRAMEWORK_ADMIN_DIR);
+
+// These are all deprecated. Use the FRAMEWORK_ versions instead.
+define('SAPPHIRE_DIR', FRAMEWORK_DIR);
+define('SAPPHIRE_PATH', FRAMEWORK_PATH);
+define('SAPPHIRE_ADMIN_DIR', FRAMEWORK_ADMIN_DIR);
+define('SAPPHIRE_ADMIN_PATH', FRAMEWORK_ADMIN_PATH);
+
+define('THIRDPARTY_DIR', FRAMEWORK_DIR . '/thirdparty');
+define('THIRDPARTY_PATH', BASE_PATH . '/' . THIRDPARTY_DIR);
+
+if(!defined('ASSETS_DIR')) {
+ define('ASSETS_DIR', 'assets');
+}
+define('ASSETS_PATH', BASE_PATH . '/' . ASSETS_DIR);
+
+///////////////////////////////////////////////////////////////////////////////
+// INCLUDES
+
+if(defined('CUSTOM_INCLUDE_PATH')) {
+ $includePath = '.' . PATH_SEPARATOR . CUSTOM_INCLUDE_PATH . PATH_SEPARATOR
+ . FRAMEWORK_PATH . PATH_SEPARATOR
+ . FRAMEWORK_PATH . '/parsers' . PATH_SEPARATOR
+ . THIRDPARTY_PATH . PATH_SEPARATOR
+ . get_include_path();
+} else {
+ $includePath = '.' . PATH_SEPARATOR . FRAMEWORK_PATH . PATH_SEPARATOR
+ . FRAMEWORK_PATH . '/parsers' . PATH_SEPARATOR
+ . THIRDPARTY_PATH . PATH_SEPARATOR
+ . get_include_path();
+}
+
+set_include_path($includePath);
+
+/**
+ * Define the temporary folder if it wasn't defined yet
+ */
+require_once 'core/TempPath.php';
+
+if(!defined('TEMP_FOLDER')) {
+ define('TEMP_FOLDER', getTempFolder(BASE_PATH));
+}
View
232 core/Core.php
@@ -3,243 +3,29 @@
* This file is the Framework bootstrap. It will get your environment ready to call Director::direct().
*
* It takes care of:
- * - Including _ss_environment.php
- * - Normalisation of $_SERVER values
- * - Initialisation of necessary constants (mostly paths)
+ * - Including Constants.php to include _ss_environment and initialise necessary constants
* - Checking of PHP memory limit
* - Including all the files needed to get the manifest built
* - Building and including the manifest
- *
- * Initialized constants:
- * - BASE_URL: Full URL to the webroot, e.g. "http://my-host.com/my-webroot" (no trailing slash).
- * - BASE_PATH: Absolute path to the webroot, e.g. "/var/www/my-webroot" (no trailing slash).
- * See Director::baseFolder(). Can be overwritten by Config::inst()->update('Director', 'alternate_base_folder', ).
- * - TEMP_FOLDER: Absolute path to temporary folder, used for manifest and template caches. Example: "/var/tmp"
- * See getTempFolder(). No trailing slash.
- * - MODULES_DIR: Not used at the moment
- * - MODULES_PATH: Not used at the moment
- * - THEMES_DIR: Path relative to webroot, e.g. "themes"
- * - THEMES_PATH: Absolute filepath, e.g. "/var/www/my-webroot/themes"
- * - FRAMEWORK_DIR: Path relative to webroot, e.g. "framework"
- * - FRAMEWORK_PATH:Absolute filepath, e.g. "/var/www/my-webroot/framework"
- * - FRAMEWORK_ADMIN_DIR: Path relative to webroot, e.g. "framework/admin"
- * - FRAMEWORK_ADMIN_PATH: Absolute filepath, e.g. "/var/www/my-webroot/framework/admin"
- * - THIRDPARTY_DIR: Path relative to webroot, e.g. "framework/thirdparty"
- * - THIRDPARTY_PATH: Absolute filepath, e.g. "/var/www/my-webroot/framework/thirdparty"
- *
+ *
* @todo This file currently contains a lot of bits and pieces, and its various responsibilities should probably be
* moved into different subsystems.
- * @todo A lot of this stuff is very order-independent; for example, the require_once calls have to happen after the
- * defines.'
- * This could be decoupled.
+ * @todo A lot of this stuff is very order-dependent. This could be decoupled.
+ *
* @package framework
* @subpackage core
*/
-///////////////////////////////////////////////////////////////////////////////
-// ENVIRONMENT CONFIG
-
-// ALL errors are reported, including E_STRICT by default *unless* the site is in
-// live mode, where reporting is limited to fatal errors and warnings (see later in this file)
-error_reporting(E_ALL | E_STRICT);
-
/**
- * Include _ss_environment.php file
+ * All errors are reported, including E_STRICT by default *unless* the site is in
+ * live mode, where reporting is limited to fatal errors and warnings (see later in this file)
*/
-//define the name of the environment file
-$envFile = '_ss_environment.php';
-//define the dirs to start scanning from (have to add the trailing slash)
-// we're going to check the realpath AND the path as the script sees it
-$dirsToCheck = array(
- realpath('.'),
- dirname($_SERVER['SCRIPT_FILENAME'])
-);
-//if they are the same, remove one of them
-if ($dirsToCheck[0] == $dirsToCheck[1]) {
- unset($dirsToCheck[1]);
-}
-foreach ($dirsToCheck as $dir) {
- //check this dir and every parent dir (until we hit the base of the drive)
- // or until we hit a dir we can't read
- do {
- //add the trailing slash we need to concatenate properly
- $dir .= DIRECTORY_SEPARATOR;
- //if it's readable, go ahead
- if (@is_readable($dir)) {
- //if the file exists, then we include it, set relevant vars and break out
- if (file_exists($dir . $envFile)) {
- define('SS_ENVIRONMENT_FILE', $dir . $envFile);
- include_once(SS_ENVIRONMENT_FILE);
- //break out of BOTH loops because we found the $envFile
- break(2);
- }
- }
- else {
- //break out of the while loop, we can't read the dir
- break;
- }
- //go up a directory
- $dir = dirname($dir);
- //here we need to check that the path of the last dir and the next one are
- // not the same, if they are, we have hit the root of the drive
- } while (dirname($dir) != $dir);
-}
-
-///////////////////////////////////////////////////////////////////////////////
-// GLOBALS AND DEFINE SETTING
-
-/**
- * A blank HTTP_HOST value is used to detect command-line execution.
- * We update the $_SERVER variable to contain data consistent with the rest of the application.
- */
-if(!isset($_SERVER['HTTP_HOST'])) {
- // HTTP_HOST, REQUEST_PORT, SCRIPT_NAME, and PHP_SELF
- if(isset($_FILE_TO_URL_MAPPING)) {
- $fullPath = $testPath = realpath($_SERVER['SCRIPT_FILENAME']);
- while($testPath && $testPath != '/' && !preg_match('/^[A-Z]:\\\\$/', $testPath)) {
- if(isset($_FILE_TO_URL_MAPPING[$testPath])) {
- $url = $_FILE_TO_URL_MAPPING[$testPath]
- . str_replace(DIRECTORY_SEPARATOR, '/', substr($fullPath,strlen($testPath)));
-
- $components = parse_url($url);
- $_SERVER['HTTP_HOST'] = $components['host'];
- if(!empty($components['port'])) $_SERVER['HTTP_HOST'] .= ':' . $components['port'];
- $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'] = $components['path'];
- if(!empty($components['port'])) $_SERVER['REQUEST_PORT'] = $components['port'];
- break;
- }
- $testPath = dirname($testPath);
- }
- }
-
- // Everything else
- $serverDefaults = array(
- 'SERVER_PROTOCOL' => 'HTTP/1.1',
- 'HTTP_ACCEPT' => 'text/plain;q=0.5',
- 'HTTP_ACCEPT_LANGUAGE' => '*;q=0.5',
- 'HTTP_ACCEPT_ENCODING' => '',
- 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1;q=0.5',
- 'SERVER_SIGNATURE' => 'Command-line PHP/' . phpversion(),
- 'SERVER_SOFTWARE' => 'PHP/' . phpversion(),
- 'SERVER_ADDR' => '127.0.0.1',
- 'REMOTE_ADDR' => '127.0.0.1',
- 'REQUEST_METHOD' => 'GET',
- 'HTTP_USER_AGENT' => 'CLI',
- );
-
- $_SERVER = array_merge($serverDefaults, $_SERVER);
-
-/**
- * If we have an HTTP_HOST value, then we're being called from the webserver and there are some things that
- * need checking
- */
-} else {
- /**
- * Fix magic quotes setting
- */
- if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
- if($_REQUEST) stripslashes_recursively($_REQUEST);
- if($_GET) stripslashes_recursively($_GET);
- if($_POST) stripslashes_recursively($_POST);
- if($_COOKIE) stripslashes_recursively($_COOKIE);
- // No more magic_quotes!
- trigger_error('get_magic_quotes_gpc support is being removed from Silverstripe. Please set this to off in ' .
- ' your php.ini and see http://php.net/manual/en/security.magicquotes.php', E_USER_WARNING);
- }
-
- /**
- * Fix HTTP_HOST from reverse proxies
- */
- if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
- // Get the first host, in case there's multiple separated through commas
- $_SERVER['HTTP_HOST'] = strtok($_SERVER['HTTP_X_FORWARDED_HOST'], ',');
- }
-}
-
-/**
- * Define system paths
- */
-if(!defined('BASE_PATH')) {
- // Assuming that this file is framework/core/Core.php we can then determine the base path
- $candidateBasePath = rtrim(dirname(dirname(dirname(__FILE__))), DIRECTORY_SEPARATOR);
- // We can't have an empty BASE_PATH. Making it / means that double-slashes occur in places but that's benign.
- // This likely only happens on chrooted environemnts
- if($candidateBasePath == '') $candidateBasePath = DIRECTORY_SEPARATOR;
- define('BASE_PATH', $candidateBasePath);
-}
-if(!defined('BASE_URL')) {
- // Determine the base URL by comparing SCRIPT_NAME to SCRIPT_FILENAME and getting common elements
- $path = realpath($_SERVER['SCRIPT_FILENAME']);
- if(substr($path, 0, strlen(BASE_PATH)) == BASE_PATH) {
- $urlSegmentToRemove = substr($path, strlen(BASE_PATH));
- if(substr($_SERVER['SCRIPT_NAME'], -strlen($urlSegmentToRemove)) == $urlSegmentToRemove) {
- $baseURL = substr($_SERVER['SCRIPT_NAME'], 0, -strlen($urlSegmentToRemove));
- define('BASE_URL', rtrim($baseURL, DIRECTORY_SEPARATOR));
- }
- }
-
- // If that didn't work, failover to the old syntax. Hopefully this isn't necessary, and maybe
- // if can be phased out?
- if(!defined('BASE_URL')) {
- $dir = (strpos($_SERVER['SCRIPT_NAME'], 'index.php') !== false)
- ? dirname($_SERVER['SCRIPT_NAME'])
- : dirname(dirname($_SERVER['SCRIPT_NAME']));
- define('BASE_URL', rtrim($dir, DIRECTORY_SEPARATOR));
- }
-}
-define('MODULES_DIR', 'modules');
-define('MODULES_PATH', BASE_PATH . '/' . MODULES_DIR);
-define('THEMES_DIR', 'themes');
-define('THEMES_PATH', BASE_PATH . '/' . THEMES_DIR);
-// Relies on this being in a subdir of the framework.
-// If it isn't, or is symlinked to a folder with a different name, you must define FRAMEWORK_DIR
-if(!defined('FRAMEWORK_DIR')) {
- define('FRAMEWORK_DIR', basename(dirname(dirname(__FILE__))));
-}
-define('FRAMEWORK_PATH', BASE_PATH . '/' . FRAMEWORK_DIR);
-define('FRAMEWORK_ADMIN_DIR', FRAMEWORK_DIR . '/admin');
-define('FRAMEWORK_ADMIN_PATH', BASE_PATH . '/' . FRAMEWORK_ADMIN_DIR);
-
-// These are all deprecated. Use the FRAMEWORK_ versions instead.
-define('SAPPHIRE_DIR', FRAMEWORK_DIR);
-define('SAPPHIRE_PATH', FRAMEWORK_PATH);
-define('SAPPHIRE_ADMIN_DIR', FRAMEWORK_ADMIN_DIR);
-define('SAPPHIRE_ADMIN_PATH', FRAMEWORK_ADMIN_PATH);
-
-define('THIRDPARTY_DIR', FRAMEWORK_DIR . '/thirdparty');
-define('THIRDPARTY_PATH', BASE_PATH . '/' . THIRDPARTY_DIR);
-
-if(!defined('ASSETS_DIR')) {
- define('ASSETS_DIR', 'assets');
-}
-define('ASSETS_PATH', BASE_PATH . '/' . ASSETS_DIR);
-
-///////////////////////////////////////////////////////////////////////////////
-// INCLUDES
-
-if(defined('CUSTOM_INCLUDE_PATH')) {
- $includePath = '.' . PATH_SEPARATOR . CUSTOM_INCLUDE_PATH . PATH_SEPARATOR
- . FRAMEWORK_PATH . PATH_SEPARATOR
- . FRAMEWORK_PATH . '/parsers' . PATH_SEPARATOR
- . THIRDPARTY_PATH . PATH_SEPARATOR
- . get_include_path();
-} else {
- $includePath = '.' . PATH_SEPARATOR . FRAMEWORK_PATH . PATH_SEPARATOR
- . FRAMEWORK_PATH . '/parsers' . PATH_SEPARATOR
- . THIRDPARTY_PATH . PATH_SEPARATOR
- . get_include_path();
-}
-
-set_include_path($includePath);
+error_reporting(E_ALL | E_STRICT);
/**
- * Define the temporary folder if it wasn't defined yet
+ * Include Constants (if it hasn't already been included) to pull in BASE_PATH, etc
*/
-require_once 'core/TempPath.php';
-
-if(!defined('TEMP_FOLDER')) {
- define('TEMP_FOLDER', getTempFolder(BASE_PATH));
-}
+require_once dirname(__FILE__).'/Constants.php';
/**
* Priorities definition. These constants are used in calls to _t() as an optional argument
View
44 core/startup/ErrorControlChain.php
@@ -3,23 +3,20 @@
/**
* Class ErrorControlChain
*
- * Runs a set of steps, optionally suppressing (but recording) any errors (even fatal ones) that occur in each step.
- * If an error does occur, subsequent steps are normally skipped, but can optionally be run anyway
- *
- * Normal errors are suppressed even past the end of the chain. Fatal errors are only suppressed until the end
- * of the chain - the request will then die silently.
- *
- * The exception is if an error occurs and BASE_URL is not yet set - in that case the error is never suppressed.
+ * Runs a set of steps, optionally suppressing uncaught errors or exceptions which would otherwise be fatal that
+ * occur in each step. If an error does occur, subsequent steps are normally skipped, but can optionally be run anyway.
*
* Usage:
*
* $chain = new ErrorControlChain();
- * $chain->then($callback1)->then($callback2)->then(true, $callback3)->execute();
+ * $chain->then($callback1)->then($callback2)->thenIfErrored($callback3)->execute();
*
* WARNING: This class is experimental and designed specifically for use pre-startup in main.php
* It will likely be heavily refactored before the release of 3.2
*/
class ErrorControlChain {
+ public static $fatal_errors = null; // Initialised after class definition
+
protected $error = false;
protected $steps = array();
@@ -28,6 +25,9 @@ class ErrorControlChain {
/** We can't unregister_shutdown_function, so this acts as a flag to enable handling */
protected $handleFatalErrors = false;
+ /** We overload display_errors to hide errors during execution, so we need to remember the original to restore to */
+ protected $originalDisplayErrors = null;
+
public function hasErrored() {
return $this->error;
}
@@ -38,6 +38,7 @@ public function setErrored($error) {
public function setSuppression($suppression) {
$this->suppression = (bool)$suppression;
+ if ($this->handleFatalErrors) ini_set('display_errors', !$suppression);
}
/**
@@ -68,20 +69,14 @@ public function thenAlways($callback) {
return $this->then($callback, null);
}
- public function handleError() {
- if ($this->suppression && defined('BASE_URL')) throw new Exception('Generic Error');
- else return false;
- }
-
protected function lastErrorWasFatal() {
$error = error_get_last();
- return $error && $error['type'] == 1;
+ return $error && ($error['type'] & self::$fatal_errors) != 0;
}
public function handleFatalError() {
- if ($this->handleFatalErrors && $this->suppression && defined('BASE_URL')) {
+ if ($this->handleFatalErrors && $this->suppression) {
if ($this->lastErrorWasFatal()) {
- ob_clean();
$this->error = true;
$this->step();
}
@@ -89,10 +84,12 @@ public function handleFatalError() {
}
public function execute() {
- set_error_handler(array($this, 'handleError'), error_reporting());
register_shutdown_function(array($this, 'handleFatalError'));
$this->handleFatalErrors = true;
+ $this->originalDisplayErrors = ini_get('display_errors');
+ ini_set('display_errors', !$this->suppression);
+
$this->step();
}
@@ -101,13 +98,7 @@ protected function step() {
$step = array_shift($this->steps);
if ($step['onErrorState'] === null || $step['onErrorState'] === $this->error) {
- try {
- call_user_func($step['callback'], $this);
- }
- catch (Exception $e) {
- if ($this->suppression && defined('BASE_URL')) $this->error = true;
- else throw $e;
- }
+ call_user_func($step['callback'], $this);
}
$this->step();
@@ -115,7 +106,10 @@ protected function step() {
else {
// Now clean up
$this->handleFatalErrors = false;
- restore_error_handler();
+ ini_set('display_errors', $this->originalDisplayErrors);
}
}
}
+
+ErrorControlChain::$fatal_errors = E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR;
+if (defined('E_RECOVERABLE_ERROR')) ErrorControlChain::$fatal_errors |= E_RECOVERABLE_ERROR;
View
12 core/startup/ParameterConfirmationToken.php
@@ -16,17 +16,7 @@ class ParameterConfirmationToken {
protected $token = null;
protected function pathForToken($token) {
- if (defined('BASE_PATH')) {
- $basepath = BASE_PATH;
- }
- else {
- $basepath = rtrim(dirname(dirname(dirname(dirname(__FILE__)))), DIRECTORY_SEPARATOR);
- }
-
- require_once(dirname(dirname(__FILE__)).'/TempPath.php');
- $tempfolder = getTempFolder($basepath ? $basepath : DIRECTORY_SEPARATOR);
-
- return $tempfolder.'/token_'.preg_replace('/[^a-z0-9]+/', '', $token);
+ return TEMP_FOLDER.'/token_'.preg_replace('/[^a-z0-9]+/', '', $token);
}
protected function genToken() {
View
112 main.php
@@ -52,6 +52,57 @@
* @see Director::direct()
*/
+/**
+ * Include the defines that set BASE_PATH, etc
+ */
+require_once('core/Constants.php');
+
+// IIS will sometimes generate this.
+if(!empty($_SERVER['HTTP_X_ORIGINAL_URL'])) {
+ $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
+}
+
+/**
+ * Figure out the request URL
+ */
+global $url;
+
+// PHP 5.4's built-in webserver uses this
+if (php_sapi_name() == 'cli-server') {
+ $url = $_SERVER['REQUEST_URI'];
+
+ // Querystring args need to be explicitly parsed
+ if(strpos($url,'?') !== false) {
+ list($url, $query) = explode('?',$url,2);
+ parse_str($query, $_GET);
+ if ($_GET) $_REQUEST = array_merge((array)$_REQUEST, (array)$_GET);
+ }
+
+ // Pass back to the webserver for files that exist
+ if(file_exists(BASE_PATH . $url) && is_file(BASE_PATH . $url)) return false;
+
+ // Apache rewrite rules use this
+} else if (isset($_GET['url'])) {
+ $url = $_GET['url'];
+ // IIS includes get variables in url
+ $i = strpos($url, '?');
+ if($i !== false) {
+ $url = substr($url, 0, $i);
+ }
+
+ // Lighttpd uses this
+} else {
+ if(strpos($_SERVER['REQUEST_URI'],'?') !== false) {
+ list($url, $query) = explode('?', $_SERVER['REQUEST_URI'], 2);
+ parse_str($query, $_GET);
+ if ($_GET) $_REQUEST = array_merge((array)$_REQUEST, (array)$_GET);
+ } else {
+ $url = $_SERVER["REQUEST_URI"];
+ }
+}
+
+// Remove base folders from the URL if webroot is hosted in a subfolder
+if (substr(strtolower($url), 0, strlen(BASE_URL)) == strtolower(BASE_URL)) $url = substr($url, strlen(BASE_URL));
/**
* Include SilverStripe's core code
@@ -63,70 +114,24 @@
$token = new ParameterConfirmationToken('flush');
$chain
- // First, if $_GET['flush'] was set, but no valid token, suppress the flush
->then(function($chain) use ($token){
+ // First, if $_GET['flush'] was set, but no valid token, suppress the flush
if (isset($_GET['flush']) && !$token->tokenProvided()) {
unset($_GET['flush']);
}
else {
$chain->setSuppression(false);
}
- })
- // Then load in core
- ->then(function(){
- require_once('core/Core.php');
- })
- // Then build the URL (even if Core didn't load beyond setting BASE_URL)
- ->thenAlways(function(){
- global $url;
-
- // IIS will sometimes generate this.
- if(!empty($_SERVER['HTTP_X_ORIGINAL_URL'])) {
- $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
- }
-
- // PHP 5.4's built-in webserver uses this
- if (php_sapi_name() == 'cli-server') {
- $url = $_SERVER['REQUEST_URI'];
-
- // Querystring args need to be explicitly parsed
- if(strpos($url,'?') !== false) {
- list($url, $query) = explode('?',$url,2);
- parse_str($query, $_GET);
- if ($_GET) $_REQUEST = array_merge((array)$_REQUEST, (array)$_GET);
- }
-
- // Apache rewrite rules use this
- } else if (isset($_GET['url'])) {
- $url = $_GET['url'];
- // IIS includes get variables in url
- $i = strpos($url, '?');
- if($i !== false) {
- $url = substr($url, 0, $i);
- }
- // Lighttpd uses this
- } else {
- if(strpos($_SERVER['REQUEST_URI'],'?') !== false) {
- list($url, $query) = explode('?', $_SERVER['REQUEST_URI'], 2);
- parse_str($query, $_GET);
- if ($_GET) $_REQUEST = array_merge((array)$_REQUEST, (array)$_GET);
- } else {
- $url = $_SERVER["REQUEST_URI"];
- }
- }
+ // Load in core
+ require_once('core/Core.php');
- // Remove base folders from the URL if webroot is hosted in a subfolder
- if (substr(strtolower($url), 0, strlen(BASE_URL)) == strtolower(BASE_URL)) $url = substr($url, strlen(BASE_URL));
- })
- // Then start up the database
- ->then(function(){
+ // Connect to database
require_once('model/DB.php');
global $databaseConfig;
if ($databaseConfig) DB::connect($databaseConfig);
- })
- // Then if a flush was requested, redirect to it
- ->then(function($chain) use ($token){
+
+ // Then if a flush was requested, redirect to it
if ($token->parameterProvided() && !$token->tokenProvided()) {
// First, check if we're in dev mode, or the database doesn't have any security data
$canFlush = Director::isDev() || !Security::database_is_ready();
@@ -161,9 +166,6 @@
})
->execute();
-// If we're in PHP's built in webserver, pass back to the webserver for files that exist
-if (php_sapi_name() == 'cli-server' && file_exists(BASE_PATH . $url) && is_file(BASE_PATH . $url)) return false;
-
global $databaseConfig;
// Redirect to the installer if no database is selected
View
245 tests/core/startup/ErrorControlChainTest.php
@@ -1,90 +1,241 @@
<?php
+/**
+ * An extension of ErrorControlChain that runs the chain in a subprocess.
+ *
+ * We need this because ErrorControlChain only suppresses uncaught fatal errors, and
+ * that would kill PHPUnit execution
+ */
+class ErrorControlChainTest_Chain extends ErrorControlChain {
+
+ function executeInSubprocess() {
+ // Get the path to the ErrorControlChain class
+ $classpath = SS_ClassLoader::instance()->getItemPath('ErrorControlChain');
+ $suppression = $this->suppression ? 'true' : 'false';
+
+ // Start building a PHP file that will execute the chain
+ $src = '<'."?php
+require_once '$classpath';
+
+\$chain = new ErrorControlChain();
+
+\$chain->setSuppression($suppression);
+
+\$chain
+";
+
+ // For each step, use reflection to pull out the call, stick in the the PHP source we're building
+ foreach ($this->steps as $step) {
+ $func = new ReflectionFunction($step['callback']);
+ $source = file($func->getFileName());
+
+ $start_line = $func->getStartLine() - 1;
+ $end_line = $func->getEndLine();
+ $length = $end_line - $start_line;
+
+ $src .= implode("", array_slice($source, $start_line, $length)) . "\n";
+ }
+
+ // Finally add a line to execute the chain
+ $src .= "->execute();";
+
+ // Now stick it in a temporary file & run it
+ $codepath = TEMP_FOLDER.'/ErrorControlChainTest_'.sha1($src).'.php';
+
+ $null = is_writeable('/dev/null') ? '/dev/null' : 'NUL';
+
+ file_put_contents($codepath, $src);
+ exec("php $codepath 2> $null", $stdout, $errcode);
+ unlink($codepath);
+
+ return array(implode("\n", $stdout), $errcode);
+ }
+}
+
class ErrorControlChainTest extends SapphireTest {
+ function setUp() {
+ // Check we can run PHP at all
+ $null = is_writeable('/dev/null') ? '/dev/null' : 'NUL';
+ exec("php -v 2> $null", $out, $rv);
+
+ if ($rv != 0) {
+ $this->markTestSkipped("Can't run PHP from the command line - is it in your path?");
+ $this->skipTest = true;
+ }
+
+ parent::setUp();
+ }
+
function testErrorSuppression() {
- $chain = new ErrorControlChain();
- $chain
+ // Fatal error
+
+ $chain = new ErrorControlChainTest_Chain();
+
+ list($out, $code) = $chain
->then(function(){
- user_error('This error should be suppressed', E_USER_ERROR);
+ Foo::bar(); // Non-existant class causes fatal error
+ })
+ ->thenIfErrored(function(){
+ echo "Done";
})
- ->execute();
+ ->executeInSubprocess();
- $this->assertTrue($chain->hasErrored());
- }
+ $this->assertEquals('Done', $out);
+
+ // User error
- function testMultipleErrorSuppression() {
- $chain = new ErrorControlChain();
+ $chain = new ErrorControlChainTest_Chain();
- $chain
+ list($out, $code) = $chain
->then(function(){
- user_error('This error should be suppressed', E_USER_ERROR);
+ user_error('Error', E_USER_ERROR);
})
- ->thenAlways(function(){
- user_error('This error should also be suppressed', E_USER_ERROR);
+ ->thenIfErrored(function(){
+ echo "Done";
})
- ->execute();
+ ->executeInSubprocess();
- $this->assertTrue($chain->hasErrored());
- }
+ $this->assertEquals('Done', $out);
- function testExceptionSuppression() {
- $chain = new ErrorControlChain();
+ // Recoverable error
- $chain
+ $chain = new ErrorControlChainTest_Chain();
+
+ list($out, $code) = $chain
->then(function(){
- throw new Exception('This exception should be suppressed');
+ $x = function(ErrorControlChain $foo){ };
+ $x(1); // Calling against type
})
- ->execute();
+ ->thenIfErrored(function(){
+ echo "Done";
+ })
+ ->executeInSubprocess();
- $this->assertTrue($chain->hasErrored());
+ $this->assertEquals('Done', $out);
}
- function testMultipleExceptionSuppression() {
- $chain = new ErrorControlChain();
+ function testExceptionSuppression() {
+ $chain = new ErrorControlChainTest_Chain();
- $chain
+ list($out, $code) = $chain
->then(function(){
throw new Exception('This exception should be suppressed');
})
- ->thenAlways(function(){
- throw new Exception('This exception should also be suppressed');
+ ->thenIfErrored(function(){
+ echo "Done";
})
- ->execute();
+ ->executeInSubprocess();
- $this->assertTrue($chain->hasErrored());
+ $this->assertEquals('Done', $out);
}
function testErrorControl() {
- $preError = $postError = array('then' => false, 'thenIfErrored' => false, 'thenAlways' => false);
+ $chain = new ErrorControlChainTest_Chain();
- $chain = new ErrorControlChain();
-
- $chain
- ->then(function() use (&$preError) { $preError['then'] = true; })
- ->thenIfErrored(function() use (&$preError) { $preError['thenIfErrored'] = true; })
- ->thenAlways(function() use (&$preError) { $preError['thenAlways'] = true; })
+ list($out, $code) = $chain
+ ->then(function() { echo 'preThen,'; })
+ ->thenIfErrored(function() { echo 'preThenIfErrored,'; })
+ ->thenAlways(function() { echo 'preThenAlways,'; })
->then(function(){ user_error('An error', E_USER_ERROR); })
- ->then(function() use (&$postError) { $postError['then'] = true; })
- ->thenIfErrored(function() use (&$postError) { $postError['thenIfErrored'] = true; })
- ->thenAlways(function() use (&$postError) { $postError['thenAlways'] = true; })
+ ->then(function() { echo 'postThen,'; })
+ ->thenIfErrored(function() { echo 'postThenIfErrored,'; })
+ ->thenAlways(function() { echo 'postThenAlways,'; })
- ->execute();
+ ->executeInSubprocess();
$this->assertEquals(
- array('then' => true, 'thenIfErrored' => false, 'thenAlways' => true),
- $preError,
- 'Then and thenAlways callbacks called before error, thenIfErrored callback not called'
+ "preThen,preThenAlways,postThenIfErrored,postThenAlways,",
+ $out
);
+ }
- $this->assertEquals(
- array('then' => false, 'thenIfErrored' => true, 'thenAlways' => true),
- $postError,
- 'thenIfErrored and thenAlways callbacks called after error, then callback not called'
- );
+ function testSuppressionControl() {
+ // Turning off suppression before execution
+
+ $chain = new ErrorControlChainTest_Chain();
+ $chain->setSuppression(false);
+
+ list($out, $code) = $chain
+ ->then(function($chain){
+ Foo::bar(); // Non-existant class causes fatal error
+ })
+ ->executeInSubprocess();
+
+ $this->assertContains("Fatal error: Class 'Foo' not found", $out);
+
+ // Turning off suppression during execution
+
+ $chain = new ErrorControlChainTest_Chain();
+
+ list($out, $code) = $chain
+ ->then(function($chain){
+ $chain->setSuppression(false);
+ Foo::bar(); // Non-existent class causes fatal error
+ })
+ ->executeInSubprocess();
+
+ $this->assertContains("Fatal error: Class 'Foo' not found", $out);
}
+ function testDoesntAffectNonFatalErrors() {
+ $chain = new ErrorControlChainTest_Chain();
+
+ list($out, $code) = $chain
+ ->then(function(){
+ $array = null;
+ if (@$array['key'] !== null) user_error('Error', E_USER_ERROR);
+ })
+ ->then(function(){
+ echo "Good";
+ })
+ ->thenIfErrored(function(){
+ echo "Bad";
+ })
+ ->executeInSubprocess();
+
+ $this->assertContains("Good", $out);
+ }
+
+ function testDoesntAffectCaughtExceptions() {
+ $chain = new ErrorControlChainTest_Chain();
+
+ list($out, $code) = $chain
+ ->then(function(){
+ try {
+ throw new Exception('Error');
+ }
+ catch (Exception $e) {
+ echo "Good";
+ }
+ })
+ ->thenIfErrored(function(){
+ echo "Bad";
+ })
+ ->executeInSubprocess();
+
+ $this->assertContains("Good", $out);
+ }
+
+ function testDoesntAffectHandledErrors() {
+ $chain = new ErrorControlChainTest_Chain();
+
+ list($out, $code) = $chain
+ ->then(function(){
+ set_error_handler(function(){ /* NOP */ });
+ user_error('Error', E_USER_ERROR);
+ })
+ ->then(function(){
+ echo "Good";
+ })
+ ->thenIfErrored(function(){
+ echo "Bad";
+ })
+ ->executeInSubprocess();
+
+ $this->assertContains("Good", $out);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.