2013-07-22 03:52:00 +02:00
|
|
|
<?php
|
2017-04-13 03:33:29 +02:00
|
|
|
|
2017-10-16 05:43:12 +02:00
|
|
|
use SilverStripe\Core\Environment;
|
|
|
|
use SilverStripe\Core\EnvironmentLoader;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Core\TempFolder;
|
2017-04-13 03:33:29 +02:00
|
|
|
|
2013-07-22 03:52:00 +02:00
|
|
|
/**
|
|
|
|
* This file is the Framework constants bootstrap. It will prepare some basic common constants.
|
|
|
|
*
|
|
|
|
* It takes care of:
|
|
|
|
* - 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).
|
2017-07-17 07:59:40 +02:00
|
|
|
* See Director::baseFolder(). Can be overwritten by Config::modify()->set(Director::class, 'alternate_base_folder', ).
|
2017-10-09 01:41:34 +02:00
|
|
|
* - TEMP_PATH: Absolute path to temporary folder, used for manifest and template caches. Example: "/var/tmp"
|
2013-07-22 03:52:00 +02:00
|
|
|
* See getTempFolder(). No trailing slash.
|
2017-08-23 04:20:39 +02:00
|
|
|
* - ASSETS_DIR: Dir for assets folder. e.g. "assets"
|
|
|
|
* - ASSETS_PATH: Full path to assets folder. e.g. "/var/www/my-webroot/assets"
|
2013-07-22 03:52:00 +02:00
|
|
|
* - 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"
|
|
|
|
* - THIRDPARTY_DIR: Path relative to webroot, e.g. "framework/thirdparty"
|
|
|
|
* - THIRDPARTY_PATH: Absolute filepath, e.g. "/var/www/my-webroot/framework/thirdparty"
|
|
|
|
*/
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
require_once __DIR__ . '/functions.php';
|
|
|
|
|
2013-07-22 03:52:00 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ENVIRONMENT CONFIG
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define system paths
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!defined('BASE_PATH')) {
|
2017-04-13 03:33:29 +02:00
|
|
|
define('BASE_PATH', call_user_func(function () {
|
|
|
|
// Determine BASE_PATH based on the composer autoloader
|
|
|
|
foreach (debug_backtrace() as $backtraceItem) {
|
|
|
|
if (isset($backtraceItem['file']) && preg_match(
|
|
|
|
'#^(?<base>.*)(/|\\\\)vendor(/|\\\\)composer(/|\\\\)autoload_real.php#',
|
|
|
|
$backtraceItem['file'],
|
|
|
|
$matches
|
|
|
|
)) {
|
|
|
|
return realpath($matches['base']) ?: DIRECTORY_SEPARATOR;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2016-09-12 05:13:55 +02:00
|
|
|
|
2017-04-13 03:33:29 +02:00
|
|
|
// Determine BASE_PATH by assuming that this file is framework/src/Core/Constants.php
|
|
|
|
// we can then determine the base path
|
|
|
|
$candidateBasePath = rtrim(dirname(dirname(dirname(__DIR__))), 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
|
|
|
|
return $candidateBasePath ?: DIRECTORY_SEPARATOR;
|
|
|
|
}));
|
2013-07-22 03:52:00 +02:00
|
|
|
}
|
2017-01-30 16:33:56 +01:00
|
|
|
|
|
|
|
// Allow a first class env var to be set that disables .env file loading
|
2017-10-16 05:43:12 +02:00
|
|
|
if (!Environment::getEnv('SS_IGNORE_DOT_ENV')) {
|
2017-06-22 12:50:45 +02:00
|
|
|
call_user_func(function () {
|
2017-10-16 05:43:12 +02:00
|
|
|
$loader = new EnvironmentLoader();
|
2017-06-22 12:50:45 +02:00
|
|
|
foreach ([BASE_PATH, dirname(BASE_PATH)] as $path) {
|
2017-10-16 05:43:12 +02:00
|
|
|
// Stop searching after first `.env` file is loaded
|
|
|
|
$dotEnvFile = $path . DIRECTORY_SEPARATOR . '.env';
|
|
|
|
if ($loader->loadFile($dotEnvFile)) {
|
|
|
|
break;
|
2017-06-22 12:50:45 +02:00
|
|
|
}
|
2017-01-30 16:33:56 +01:00
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
});
|
2017-01-30 16:33:56 +01:00
|
|
|
}
|
|
|
|
|
2017-09-20 00:07:32 +02:00
|
|
|
// Validate SS_BASE_URL is absolute
|
2017-10-16 05:43:12 +02:00
|
|
|
if (Environment::getEnv('SS_BASE_URL') && !preg_match('#^(\w+:)?//.*#', Environment::getEnv('SS_BASE_URL'))) {
|
2017-09-20 00:07:32 +02:00
|
|
|
call_user_func(function () {
|
2017-10-16 05:43:12 +02:00
|
|
|
$base = Environment::getEnv('SS_BASE_URL');
|
2017-09-20 00:07:32 +02:00
|
|
|
user_error(
|
|
|
|
"SS_BASE_URL should be an absolute url with protocol "
|
|
|
|
. "(http://$base) or without protocol (//$base)",
|
|
|
|
E_USER_WARNING
|
|
|
|
);
|
|
|
|
// Treat as protocol-less absolute url
|
|
|
|
$base = '//' . $base;
|
2017-10-16 05:43:12 +02:00
|
|
|
Environment::setEnv('SS_BASE_URL', $base);
|
2017-09-20 00:07:32 +02:00
|
|
|
});
|
|
|
|
}
|
2017-05-30 13:18:47 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!defined('BASE_URL')) {
|
2017-04-13 03:33:29 +02:00
|
|
|
define('BASE_URL', call_user_func(function () {
|
2017-08-24 02:58:04 +02:00
|
|
|
// Prefer explicitly provided SS_BASE_URL
|
2017-10-16 05:43:12 +02:00
|
|
|
$base = Environment::getEnv('SS_BASE_URL');
|
2017-08-24 02:58:04 +02:00
|
|
|
if ($base) {
|
|
|
|
// Strip relative path from SS_BASE_URL
|
|
|
|
return rtrim(parse_url($base, PHP_URL_PATH), '/');
|
|
|
|
}
|
|
|
|
|
2017-04-13 03:33:29 +02:00
|
|
|
// Determine the base URL by comparing SCRIPT_NAME to SCRIPT_FILENAME and getting common elements
|
|
|
|
// This tends not to work on CLI
|
|
|
|
$path = realpath($_SERVER['SCRIPT_FILENAME']);
|
|
|
|
if (substr($path, 0, strlen(BASE_PATH)) == BASE_PATH) {
|
2017-10-12 15:54:27 +02:00
|
|
|
$urlSegmentToRemove = str_replace('\\', '/', substr($path, strlen(BASE_PATH)));
|
2017-04-13 03:33:29 +02:00
|
|
|
if (substr($_SERVER['SCRIPT_NAME'], -strlen($urlSegmentToRemove)) == $urlSegmentToRemove) {
|
|
|
|
$baseURL = substr($_SERVER['SCRIPT_NAME'], 0, -strlen($urlSegmentToRemove));
|
2017-08-23 04:48:28 +02:00
|
|
|
// ltrim('.'), normalise slashes to '/', and rtrim('/')
|
|
|
|
return rtrim(str_replace('\\', '/', ltrim($baseURL, '.')), '/');
|
2017-04-13 03:33:29 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2013-07-22 03:52:00 +02:00
|
|
|
|
2017-04-13 03:33:29 +02:00
|
|
|
// Assume no base_url
|
|
|
|
return '';
|
|
|
|
}));
|
2013-07-22 03:52:00 +02:00
|
|
|
}
|
2017-04-13 03:33:29 +02:00
|
|
|
|
2013-07-22 03:52:00 +02:00
|
|
|
define('THEMES_DIR', 'themes');
|
2017-04-13 03:33:29 +02:00
|
|
|
define('THEMES_PATH', BASE_PATH . DIRECTORY_SEPARATOR . THEMES_DIR);
|
|
|
|
|
2013-07-22 03:52:00 +02:00
|
|
|
// 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
|
2016-09-12 05:13:55 +02:00
|
|
|
|
2016-11-01 04:13:06 +01:00
|
|
|
define('FRAMEWORK_PATH', realpath(__DIR__ . '/../../'));
|
2017-04-13 03:33:29 +02:00
|
|
|
if (strpos(FRAMEWORK_PATH, BASE_PATH) !== 0) {
|
2016-11-29 00:31:16 +01:00
|
|
|
throw new Exception("Path error: FRAMEWORK_PATH " . FRAMEWORK_PATH . " not within BASE_PATH " . BASE_PATH);
|
2013-07-22 03:52:00 +02:00
|
|
|
}
|
2017-04-13 03:33:29 +02:00
|
|
|
define('FRAMEWORK_DIR', trim(substr(FRAMEWORK_PATH, strlen(BASE_PATH)), DIRECTORY_SEPARATOR));
|
|
|
|
define('THIRDPARTY_DIR', FRAMEWORK_DIR ? (FRAMEWORK_DIR . '/thirdparty') : 'thirdparty');
|
|
|
|
define('THIRDPARTY_PATH', FRAMEWORK_PATH . DIRECTORY_SEPARATOR . 'thirdparty');
|
2013-07-24 02:09:44 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!defined('ASSETS_DIR')) {
|
|
|
|
define('ASSETS_DIR', 'assets');
|
2013-07-24 02:09:44 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!defined('ASSETS_PATH')) {
|
2017-04-13 03:33:29 +02:00
|
|
|
define('ASSETS_PATH', BASE_PATH . DIRECTORY_SEPARATOR . ASSETS_DIR);
|
2013-10-31 23:25:19 +01:00
|
|
|
}
|
2013-07-22 03:52:00 +02:00
|
|
|
|
2016-11-01 03:45:56 +01:00
|
|
|
// Custom include path - deprecated
|
2016-11-29 00:31:16 +01:00
|
|
|
if (defined('CUSTOM_INCLUDE_PATH')) {
|
2018-01-16 19:39:30 +01:00
|
|
|
set_include_path(CUSTOM_INCLUDE_PATH . PATH_SEPARATOR . get_include_path());
|
2013-07-22 03:52:00 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Define the temporary folder if it wasn't defined yet
|
2017-10-09 01:41:34 +02:00
|
|
|
if (!defined('TEMP_PATH')) {
|
|
|
|
if (defined('TEMP_FOLDER')) {
|
|
|
|
define('TEMP_PATH', TEMP_FOLDER);
|
|
|
|
} else {
|
|
|
|
define('TEMP_PATH', TempFolder::getTempFolder(BASE_PATH));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define the temporary folder for backwards compatibility
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!defined('TEMP_FOLDER')) {
|
2017-10-09 01:41:34 +02:00
|
|
|
define('TEMP_FOLDER', TEMP_PATH);
|
2013-07-22 03:52:00 +02:00
|
|
|
}
|