Skip to content

Instantly share code, notes, and snippets.

@leevigraham
Last active November 27, 2020 19:26
Show Gist options
  • Save leevigraham/448166 to your computer and use it in GitHub Desktop.
Save leevigraham/448166 to your computer and use it in GitHub Desktop.
<?php
/**
* Custom configuration bootstrap file for ExpressionEngine
*
* Place config.php in your site root
* Add require(realpath(dirname(__FILE__) . '/../../config_bootstrap.php')); to the bottom of system/expressionengine/config/config.php
* Add require(realpath(dirname(__FILE__) . '/../../config_bootstrap.php')); to the bottom of system/expressionengine/config/database.php
* If you have moved your site root you'll need to update the require_once path
*
* Also includes custom DB configuration file based on your environment
*
* Posiible DB configuration options
*
* $env_db_config['hostname'] = "";
* $env_db_config['username'] = "";
* $env_db_config['password'] = "";
* $env_db_config['database'] = "";
*
* @author Leevi Graham <http://leevigraham.com>
* @link http://expressionengine.com/index.php?affiliate=leevigraham&page=wiki/EE_2_Config_Overrides/
* @link http://eeinsider.com/blog/eeci-2010-how-erskine-rolls-with-ee/ - Hat tip to: Erskine from EECI2010 Preso
* @version 1.10
*
* == Changelog ==
*
* Version 1.1
* - Changed 'gv_' to 'global:'
* - Added {global:cm_subscriber_list_slug} for campaignmonitor.com integration
* - Added {global:google_analytics_key} for Google Analytics integration
* - Make $_POST array available as global vars with 'post:' prefix
* - Make $_GET array available as global vars with 'get:' prefix
* - Added more inline commenting
* - Swapped order of system config and global vars
*
* Version 1.2
* - Removed $_GET and $_POST parsing. You should use Mo Variables instead. https://github.com/rsanchez/mo_variables
*
* Version 1.3
* - Added encryption key
*
* Version 1.4
* - Updated NSM .htaccess path. v1.1.0 of the addon requires the config setting to be an array
*
* Version 1.5
* - Added global:404_entry_id
*
* Version 1.6
* - Added SSL support for site url
*
* Version 1.7
* - Added placeholder config for upload directories
*
* Version 1.8
* - Added $_GET['debug'] param to help with support
*
* Version 1.9
* - Added $config['third_party'] param to ease updating
* - Added $config['base_url']
* - Moved webmaster_email config into dev environment
*
* Version 1.10
* - Added changed $config['third_party'] to $config['third_party_path']
* - Added $config['path_third_themes']
* - Added $config['url_third_themes']
*/
/**
* config_bootstrap.php is included multiple times during the request
* PHP Only allows constants to be defined once so we check first
*/
if(!defined('NSM_ENV')) {
// Set your system folder
define('NSM_SYSTEM_FOLDER', 'ee-admin');
// Define the server name, basepath and site_url
// These can all be defined using the server request and filepath
define('NSM_SERVER_NAME', $_SERVER['SERVER_NAME']);
define('NSM_BASEPATH', dirname(__FILE__));
define('NSM_SITE_URL', "//".NSM_SERVER_NAME);
/**
* Based on the requested server we can determine the environment
* The following conditionals check the server name for specific strings
* We use NSM_ENV to define variables latter on in the bootstrap process
*
* Examples:
* http://local.my-site.com defines NSM_ENV as local
* http://dev.my-site.com defines NSM_ENV as development
*
* strstr() — Find the first occurrence of a string
* @see http://php.net/manual/en/function.strstr.php
*/
if ( strstr( NSM_SERVER_NAME, 'local.' ) ) define('NSM_ENV', 'local');
elseif( strstr( NSM_SERVER_NAME, 'dev.' ) ) define('NSM_ENV', 'development');
elseif( strstr( NSM_SERVER_NAME, 'stage.' ) ) define('NSM_ENV', 'staging');
elseif( strstr( NSM_SERVER_NAME, 'mobi.' ) ) define('NSM_ENV', 'mobile');
// Fallback to production settings
else define('NSM_ENV', 'production');
// Not sure what environment the request is?
// Add ?debug_config_bootstrap to the end of the URL
if(isset($_GET['debug_config_bootstrap'])) {
die('The current environment is: '.NSM_ENV);
}
}
/*
* Create placeholder arrays for environment settings
* Environment settings override the global settings (also defined in config_bootstrap.php) and
* the standard config.php settings
*/
$env_config = array();
$env_db_config = array();
$env_global_vars = array();
// Comparing NSM_ENV which environment specific configuration will we load?
// Local configuration:
if ('local' === NSM_ENV) {
// Environment DB configuration
$env_db_config = array(
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
);
// Environment config variables
// Overrides anything in $env_config
$env_config = array();
// Environment global vars
// These will be available in youre templates
// 'global:' is not required, it's just a naming convention we use @Newism
// Example: 'global:cm_subscriber_list_slug' will be available in templates as {global:cm_subscriber_list_slug}
// Overrides anything in $default_global_vars
$env_global_vars = array(
'global:cm_subscriber_list_slug' => ''
);
}
elseif('development' === NSM_ENV) {
$env_db_config = array(
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
);
$env_config = array();
$env_global_vars = array();
}
elseif('staging' === NSM_ENV) {
$env_db_config = array(
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
);
$env_config = array();
$env_global_vars = array();
}
elseif('mobile' === NSM_ENV) {
$env_db_config = array(
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
);
$env_config = array(
// Custom mobile templates?
'tmpl_file_basepath' => NSM_BASEPATH . '/templates_mobile/'
);
$env_global_vars = array();
}
elseif('production' === NSM_ENV) {
$env_db_config = array(
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
);
$env_config = array();
$env_global_vars = array();
}
// Config bootsrap... GO!
if(isset($config)) {
/**
* Custom global variables
*
* This is a bit sucky as they are pulled straight from the $assign_to_config array.
* See EE_Config.php around line 90 or search for: 'global $assign_to_config;'
* Output the global vars in your template with:
* <?php $EE = get_instance(); print('<pre><code>'.print_r($EE->config->_global_vars, TRUE) . '</code></pre>'); ?>
*/
$default_global_vars = array(
// General - Set the production environment so we can test / show / hide components
'global:env' => NSM_ENV,
// Tag parameters - Short hand tag params
'global:param_disable_default' => 'disable="categories|pagination|member_data"',
'global:param_disable_all' => 'disable="categories|custom_fields|member_data|pagination"',
'global:param_cache_param' => 'cache="yes" refresh="10"',
'-global:param_cache_param' => '-cache="yes" refresh="10"', // disable by adding a '-' to the front of the global
// Date and time - Short hand date and time
'global:date_time' => '%g:%i %a',
'global:date_short' => '%F %d, %Y',
'global:date_full' => '%F %d %Y, %g:%i %a',
/**
* Theme - URL to theme assets
* Example: <script src="{global:theme_url}/js/libs/modernizr-1.6.min.js"></script>
*/
'global:theme_url' => NSM_SITE_URL . '/themes/site_themes/default',
/**
* CampaignMonitor - Slug for CM signup forms
* Example: <form action="http://newism.createsend.com/t/y/s/{global:cm_subscriber_list_slug}/" method="post">...</form>
*/
'global:cm_subscriber_list_slug' => false,
/**
* Google Analytics Key
* Example:
* <script type="text/javascript">
* var _gaq = _gaq || [];
* _gaq.push(['_setAccount', 'UA-{global:google_analytics_key}']);
* _gaq.push(['_trackPageview']);
* </script>
*/
'global:google_analytics_key' => false,
// Store the entry_id for the 404 page
'global:404_entry_id' => '2',
);
// Make this global so we can add some of the config variables here
global $assign_to_config;
if(!isset($assign_to_config['global_vars'])) {
$assign_to_config['global_vars'] = array();
}
$assign_to_config['global_vars'] = array_merge($assign_to_config['global_vars'], $default_global_vars, $env_global_vars);
/**
* Config. This shouldn't have to be changed if you're using the Newism EE2 template.
*
* All the values below override the default config.php configuration.
* Setting a value below will also restrict users from changing it in the CP
* If a user does save a configuration value in the CP:
* - it will be written to config.php
* - the user will see the value below in the CP
*/
$default_config = array(
// General preferences
'is_system_on' => 'y',
'license_number' => '',
'site_index' => '',
'admin_session_type' => 'cs',
'new_version_check' => 'y',
'doc_url' => 'http://expressionengine.com/user_guide/',
'site_url' => NSM_SITE_URL,
'base_url' => NSM_SITE_URL,
'cp_url' => NSM_SITE_URL.'/'.NSM_SYSTEM_FOLDER.'/index.php',
// Set this so we can use query strings
// 'uri_protocol' => 'PATH_INFO',
// http://ellislab.com/expressionengine/user-guide/general/hidden_configuration_variables.html#third-party-path
'third_party_path' => NSM_BASEPATH . '/third_party/',
// http://ellislab.com/expressionengine/user-guide/general/hidden_configuration_variables.html#path-third-themes
'path_third_themes' => NSM_BASEPATH . '/content/themes/third_party/',
// http://ellislab.com/expressionengine/user-guide/general/hidden_configuration_variables.html#url-third-themes
'url_third_themes' => NSM_SITE_URL . '/themes/third_party/',
// Datbase preferences
'db_debug' => 'n',
'pconnect' => 'n',
'enable_db_caching' => 'n',
// Site preferences
// Some of these preferences might actually need to be set in the index.php files.
// Not sure which ones yet, I'll figure that out when I have my first MSM site.
'is_site_on' => 'y',
'site_404' => 'site/four04',
// Localization preferences
'server_timezone' => 'UP10',
'server_offset' => FALSE,
'time_format' => 'eu',
'daylight_savings' => 'n',
'honor_entry_dst' => 'y',
// Channel preferences
'use_category_name' => 'y',
'word_separator' => 'dash',
'reserved_category_word' => 'category',
// Template preferences
'strict_urls' => 'y',
'save_tmpl_files' => 'y',
'save_tmpl_revisions' => 'y',
'tmpl_file_basepath' => NSM_BASEPATH . '/templates/',
// Theme preferences
'theme_folder_path' => NSM_BASEPATH . '/content/themes/',
'theme_folder_url' => NSM_SITE_URL . '/themes/',
// Tracking preferences
'enable_online_user_tracking' => 'n',
'dynamic_tracking_disabling' => '500',
'enable_hit_tracking' => 'n',
'enable_entry_view_tracking' => 'n',
'log_referrers' => 'n',
// Messaging preferences
'prv_msg_upload_path' => NSM_BASEPATH . '/content/uploads/member/pm_attachments',
'enable_emoticons' => 'n',
// Member preferences
'allow_registration' => 'n',
// Create a random string for the member profile trigger
'profile_trigger' => '--sdjhkj2lffgrerfvmdkndkfisolmfmsd' . time(),
// Member avatar confiuration
'enable_avatars' => 'n',
'avatar_path' => NSM_BASEPATH . '/content/uploads/member/avatars/',
'avatar_url' => NSM_SITE_URL . '/uploads/member/avatars/',
'avatar_max_height' => 100,
'avatar_max_width' => 100,
'avatar_max_kb' => 100,
// Member photo configuration
'enable_photos' => 'n',
'photo_path' => NSM_BASEPATH . '/content/uploads/member/photos/',
'photo_url' => NSM_SITE_URL . '/uploads/member/photos/',
'photo_max_height' => 200,
'photo_max_width' => 200,
'photo_max_kb' => 200,
// Member signature confiuration
'sig_allow_img_upload' => 'n',
'sig_img_path' => NSM_BASEPATH . '/content/uploads/member/signature_attachments/',
'sig_img_url' => NSM_SITE_URL . '/uploads/member/signature_attachments/',
'sig_img_max_height' => 80,
'sig_img_max_width' => 480,
'sig_img_max_kb' => 30,
'sig_maxlength' => 500,
// Captcha settings
'captcha_font' => 'y',
'captcha_rand' => 'y',
'captcha_require_members' => 'n',
'captcha_path' => NSM_BASEPATH . '/content/'.NSM_SYSTEM_FOLDER.'/images/captchas/',
'captcha_url' => NSM_SITE_URL.'/'.NSM_SYSTEM_FOLDER.'/images/captchas/',
// Encryption / Session key
'encryption_key' => '',
// File Upload config.
// @see: http://expressionengine.com/user_guide/general/hidden_configuration_variables.html#upload-preferences
//'upload_preferences' => array(
// 1 => array( // ID of upload destination
// 'name' => 'Staging Image Uploads', // Display name in control panel
// 'server_path' => '/home/user/example.com/staging/images/uploads/', // Server path to upload directory
// 'url' => 'http://staging.example.com/images/uploads/' // URL of upload directory
// )
// ),
// NSM htaccess Generator Configuration
// @see: http://ee-garage.com/nsm-htaccess-generator
// 'nsm_htaccess_generator_path' => array(NSM_BASEPATH . "/content/.htaccess"),
// NSM Gravatar Configuration
// @see: https://github.com/newism/nsm.gravatar.ee_addon
// 'nsm_htaccess_generator_path' => array(
// 'size' => '200',
// 'default' => NSM_SITE_URL . '/uploads/member/avatars/default.png',
// 'force_default' => 'n',
// 'rating' => 'pg',
// 'secure' => 'n'
// )
);
// Build the new config object
$config = array_merge($config, $default_config, $env_config);
}
// DB bootsrap... GO!
if(isset($db['expressionengine']))
{
$default_db_config = array("cachedir" => APPPATH . "cache/db_cache/");
$db['expressionengine'] = array_merge($db['expressionengine'], $default_db_config, $env_db_config);
}
@andyhoman
Copy link

Just a quick heads up Leevi. I had to change the file upload code to:

'upload_preferences' => array(
    1 => array(                                                                                 // ID of upload destination
        'name'        => 'Staging Image Uploads',                          // Display name in control panel
        'server_path' => '/home/user/example.com/staging/images/uploads/', // Server path to upload directory
        'url'         => 'http://staging.example.com/images/uploads/'      // URL of upload directory
    )
),

Otherwise it killed the script.

@wssrstrm
Copy link

wssrstrm commented Feb 7, 2012

I'm wondering something pretty basic. Where do you set the db info for the production db if not in this file? Do you just set it normally through the expression engine install on the production server itself and use this file to identify the dbs for the other environments?

@leevigraham
Copy link
Author

@andyhoman sorted!

@ginkelb
Copy link

ginkelb commented Mar 1, 2012

@wssrstrm The db info for the production environment are entered near line 123 of the script. It woudn't be very handy if you ended up with configuration data in multiple files.

@leevigraham The NSM_SITE_URL near line 62 ends up WITH a trailing /. All other variables based off of this like theme_folder_url use a / between NSM_SITE_URL and the rest of the variable. Ending up with url.com//themes/. Is this intentional?

@leevigraham
Copy link
Author

@ginkelb I'll update the NSM_SITE_URL now.

@elliotlewis
Copy link

I think an important config is missing (excellent bootstrap btw).

http://expressionengine.com/wiki/EE_2_Config_Overrides
[base_url] => http://local.ee2.template/

site_url($uri = '') from ->helper('url') in Config.php as part of CI uses base_url to define paths.

This caught me out when developing a module using site_url() for the 'action' part of a form_declaration. I'm not sure why site_url doesn't use 'site_url' from the config array, maybe it's a hangover from CI and EE uses 'site_url'.

Easy fix, I've included after
'site_url' => NSM_SITE_URL,
'base_url' => NSM_SITE_URL

@leevigraham
Copy link
Author

  • Version 1.9
  • - Added $config['third_party'] param to ease updating
    
  • - Added $config['base_url']
    
  • - Moved webmaster_email config into dev environment
    

@alexroper
Copy link

missing a comma after:

'base_url' => NSM_SITE_URL

@jakemauer
Copy link

@alexroper wish I would've see your comment an hour ago. I was tearing my hair out!

Copy link

ghost commented Sep 21, 2012

@alexroper Just found out about the comma

@alexroper
Copy link

@leevigraham EE v2.5.3 adds a couple other hidden config variables for third-party themes:

'path_third_themes' => NSM_BASEPATH . '/content/themes/third_party/',
'url_third_themes' => NSM_SITE_URL . '/themes/third_party/',

@alexroper
Copy link

@leevigraham Also, the 'third_party' variable should be changed to 'third_party_path':

'third_party_path' => NSM_BASEPATH . '/third_party/',

@leevigraham
Copy link
Author

@alexroper took me a while but I've updated based on your comments

@philfreo
Copy link

@leevigraham you've got syntax errors now on Line 154 and 177 (double braces)

@susansan
Copy link

line 369 should be:

$env_config['upload_preferences'] = array(

@sharq88
Copy link

sharq88 commented Apr 25, 2014

@leevigraham Line 7,8 should be:

  • require_once instead of require (see line 9)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment