2008-01-21 09:01:30 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2017-01-30 16:33:56 +01:00
|
|
|
* Configure SilverStripe from the environment variables.
|
2012-03-24 04:38:57 +01:00
|
|
|
* Usage: Put "require_once('conf/ConfigureFromEnv.php');" into your _config.php file.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-01-30 16:33:56 +01:00
|
|
|
* If you include this file, you will be able to use the following variables to control
|
2008-01-21 09:01:30 +01:00
|
|
|
* your site.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-21 09:01:30 +01:00
|
|
|
* Your database connection will be set up using these defines:
|
2009-05-27 01:45:54 +02:00
|
|
|
* - SS_DATABASE_CLASS: The database class to use, MySQLDatabase, MSSQLDatabase, etc. defaults to
|
|
|
|
* MySQLDatabase
|
|
|
|
* - SS_DATABASE_SERVER: The database server to use, defaulting to localhost
|
2010-10-13 03:28:54 +02:00
|
|
|
* - SS_DATABASE_USERNAME: The database username (mandatory)
|
2008-01-21 09:01:30 +01:00
|
|
|
* - SS_DATABASE_PASSWORD: The database password (mandatory)
|
2013-03-18 22:51:16 +01:00
|
|
|
* - SS_DATABASE_PORT: The database port
|
2009-05-27 01:45:54 +02:00
|
|
|
* - SS_DATABASE_SUFFIX: A suffix to add to the database name.
|
|
|
|
* - SS_DATABASE_PREFIX: A prefix to add to the database name.
|
2012-03-11 20:53:01 +01:00
|
|
|
* - SS_DATABASE_TIMEZONE: Set the database timezone to something other than the system timezone.
|
2014-08-15 08:53:05 +02:00
|
|
|
* - SS_DATABASE_MEMORY: Use in-memory state if possible. Useful for testing, currently only
|
2013-06-19 13:42:28 +02:00
|
|
|
* supported by the SQLite database adapter.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-11-18 02:48:37 +01:00
|
|
|
* There is one more setting that is intended to be used by people who work on SilverStripe.
|
2012-09-26 23:34:00 +02:00
|
|
|
* - SS_DATABASE_CHOOSE_NAME: Boolean/Int. If set, then the system will choose a default database name for you if
|
|
|
|
* one isn't give in the $database variable. The database name will be "SS_" followed by the name of the folder
|
|
|
|
* into which you have installed SilverStripe. If this is enabled, it means that the phpinstaller will work out of
|
|
|
|
* the box without the installer needing to alter any files. This helps prevent accidental changes to the
|
|
|
|
* environment.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* If SS_DATABASE_CHOOSE_NAME is an integer greater than one, then an ancestor folder will be used for the database
|
|
|
|
* name. This is handy for a site that's hosted from /sites/examplesite/www or /buildbot/allmodules-2.3/build. If
|
|
|
|
* it's 2, the parent folder will be chosen; if it's 3 the grandparent, and so on.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-21 09:01:30 +01:00
|
|
|
* You can configure the environment with this define:
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-21 09:01:30 +01:00
|
|
|
* - SS_ENVIRONMENT_TYPE: The environment type: dev, test or live.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* You can configure the default admin with these defines:
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* - SS_DEFAULT_ADMIN_USERNAME: The username of the default admin - this is a non-database user with administrative
|
2012-09-26 23:34:00 +02:00
|
|
|
* privileges.
|
2008-02-10 23:58:58 +01:00
|
|
|
* - SS_DEFAULT_ADMIN_PASSWORD: The password of the default admin.
|
2009-03-04 04:44:11 +01:00
|
|
|
* - SS_USE_BASIC_AUTH: Protect the site with basic auth (good for test sites)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-21 09:01:30 +01:00
|
|
|
* Email:
|
|
|
|
* - SS_SEND_ALL_EMAILS_TO: If you set this define, all emails will be redirected to this address.
|
2011-03-02 06:23:07 +01:00
|
|
|
* - SS_SEND_ALL_EMAILS_FROM: If you set this define, all emails will be send from this address.
|
2008-01-21 09:01:30 +01:00
|
|
|
*/
|
|
|
|
|
2015-07-24 00:53:41 +02:00
|
|
|
use Monolog\Logger;
|
|
|
|
use Monolog\Handler\StreamHandler;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Email\Email;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Dev\Install\DatabaseAdapterRegistry;
|
|
|
|
use SilverStripe\Security\BasicAuth;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Security;
|
|
|
|
|
2008-11-18 02:48:37 +01:00
|
|
|
global $database;
|
|
|
|
|
|
|
|
// No database provided
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!isset($database) || !$database) {
|
2017-01-30 16:33:56 +01:00
|
|
|
if (!($database = getenv('SS_DATABASE_NAME')) && $chooseName = getenv('SS_DATABASE_CHOOSE_NAME')) {
|
|
|
|
$loopCount = (int)$chooseName;
|
2016-11-29 00:31:16 +01:00
|
|
|
$databaseDir = BASE_PATH;
|
2017-01-30 16:33:56 +01:00
|
|
|
for ($i=0; $i<$loopCount-1; $i++) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$databaseDir = dirname($databaseDir);
|
|
|
|
}
|
2017-01-30 16:33:56 +01:00
|
|
|
$database = getenv('SS_DATABASE_PREFIX') ?: 'SS_';
|
|
|
|
$database .= basename($databaseDir);
|
2016-11-29 00:31:16 +01:00
|
|
|
$database = str_replace('.', '', $database);
|
|
|
|
}
|
2008-11-18 02:48:37 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-01-30 16:33:56 +01:00
|
|
|
if ($dbUser = getenv('SS_DATABASE_USERNAME')) {
|
2016-11-29 00:31:16 +01:00
|
|
|
global $databaseConfig;
|
2017-01-12 01:24:33 +01:00
|
|
|
|
|
|
|
// Checks if the database global is defined (if present, wraps with prefix and suffix)
|
|
|
|
$databaseNameWrapper = function ($name) {
|
|
|
|
if (!$name) {
|
|
|
|
return '';
|
|
|
|
} else {
|
2017-01-30 16:33:56 +01:00
|
|
|
return (getenv('SS_DATABASE_PREFIX') ?: '')
|
2017-01-12 01:24:33 +01:00
|
|
|
. $name
|
2017-01-30 16:33:56 +01:00
|
|
|
. (getenv('SS_DATABASE_SUFFIX') ?: '');
|
2017-01-12 01:24:33 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/** @skipUpgrade */
|
|
|
|
$databaseConfig = array(
|
2017-01-30 16:33:56 +01:00
|
|
|
"type" => getenv('SS_DATABASE_CLASS') ?: 'MySQLDatabase',
|
|
|
|
"server" => getenv('SS_DATABASE_SERVER') ?: 'localhost',
|
|
|
|
"username" => $dbUser,
|
|
|
|
"password" => getenv('SS_DATABASE_PASSWORD'),
|
2017-01-12 01:24:33 +01:00
|
|
|
"database" => $databaseNameWrapper($database),
|
2016-11-29 00:31:16 +01:00
|
|
|
);
|
2012-03-11 20:53:01 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// Set the port if called for
|
2017-01-30 16:33:56 +01:00
|
|
|
if ($dbPort = getenv('SS_DATABASE_PORT')) {
|
|
|
|
$databaseConfig['port'] = $dbPort;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2013-03-18 22:51:16 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// Set the timezone if called for
|
2017-01-30 16:33:56 +01:00
|
|
|
if ($dbTZ = getenv('SS_DATABASE_TIMEZONE')) {
|
|
|
|
$databaseConfig['timezone'] = $dbTZ;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2012-03-11 20:53:01 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// For schema enabled drivers:
|
2017-01-30 16:33:56 +01:00
|
|
|
if ($dbSchema = getenv('SS_DATABASE_SCHEMA')) {
|
|
|
|
$databaseConfig["schema"] = $dbSchema;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2013-06-19 13:42:28 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// For SQlite3 memory databases (mainly for testing purposes)
|
2017-01-30 16:33:56 +01:00
|
|
|
if ($dbMemory = getenv('SS_DATABASE_MEMORY')) {
|
|
|
|
$databaseConfig["memory"] = $dbMemory;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2008-11-18 02:48:37 +01:00
|
|
|
}
|
2008-01-21 09:01:30 +01:00
|
|
|
|
2017-01-30 16:33:56 +01:00
|
|
|
if ($sendAllEmailsTo = getenv('SS_SEND_ALL_EMAILS_TO')) {
|
|
|
|
Email::config()->send_all_emails_to = $sendAllEmailsTo;
|
2008-01-21 09:01:30 +01:00
|
|
|
}
|
2017-01-30 16:33:56 +01:00
|
|
|
if ($sendAllEmailsFrom = getenv('SS_SEND_ALL_EMAILS_FROM')) {
|
|
|
|
Email::config()->send_all_emails_from = $sendAllEmailsFrom;
|
2011-03-02 06:23:07 +01:00
|
|
|
}
|
2008-02-10 23:58:58 +01:00
|
|
|
|
2017-01-30 16:33:56 +01:00
|
|
|
if ($defaultAdminUser = getenv('SS_DEFAULT_ADMIN_USERNAME')) {
|
|
|
|
if (!$defaultAdminPass = getenv('SS_DEFAULT_ADMIN_PASSWORD')) {
|
2016-11-29 00:31:16 +01:00
|
|
|
user_error(
|
2017-01-30 16:33:56 +01:00
|
|
|
"SS_DEFAULT_ADMIN_PASSWORD must be defined in your environment,"
|
2016-11-29 00:31:16 +01:00
|
|
|
. "if SS_DEFAULT_ADMIN_USERNAME is defined. See "
|
|
|
|
. "http://doc.silverstripe.org/framework/en/topics/environment-management for more information",
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
|
|
|
} else {
|
2017-01-30 16:33:56 +01:00
|
|
|
Security::setDefaultAdmin($defaultAdminUser, $defaultAdminPass);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2008-02-10 23:58:58 +01:00
|
|
|
}
|
2017-01-30 16:33:56 +01:00
|
|
|
if ($useBasicAuth = getenv('SS_USE_BASIC_AUTH')) {
|
|
|
|
BasicAuth::config()->entire_site_protected = $useBasicAuth;
|
2009-03-04 04:44:11 +01:00
|
|
|
}
|
2008-08-11 06:41:54 +02:00
|
|
|
|
2017-01-30 16:33:56 +01:00
|
|
|
if ($errorLog = getenv('SS_ERROR_LOG')) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$logger = Injector::inst()->get('Logger');
|
|
|
|
if ($logger instanceof Logger) {
|
2017-01-30 16:33:56 +01:00
|
|
|
$logger->pushHandler(new StreamHandler(BASE_PATH . '/' . $errorLog, Logger::WARNING));
|
2016-11-29 00:31:16 +01:00
|
|
|
} else {
|
|
|
|
user_error("SS_ERROR_LOG setting only works with Monolog, you are using another logger", E_USER_WARNING);
|
|
|
|
}
|
2009-03-04 04:44:11 +01:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
// Allow database adapters to handle their own configuration
|
|
|
|
DatabaseAdapterRegistry::autoconfigure();
|
2017-01-30 16:33:56 +01:00
|
|
|
|
|
|
|
unset(
|
|
|
|
$envType,
|
|
|
|
$chooseName,
|
|
|
|
$loopCount,
|
|
|
|
$databaseDir,
|
|
|
|
$i,
|
|
|
|
$databaseNameWrapper,
|
|
|
|
$dbUser,
|
|
|
|
$dbPort,
|
|
|
|
$dbTZ,
|
|
|
|
$dbSchema,
|
|
|
|
$dbMemory,
|
|
|
|
$sendAllEmailsTo,
|
|
|
|
$sendAllEmailsFrom,
|
|
|
|
$defaultAdminUser,
|
|
|
|
$defaultAdminPass,
|
|
|
|
$useBasicAuth,
|
|
|
|
$errorLog,
|
|
|
|
$logger
|
|
|
|
);
|