Created
October 8, 2017 17:34
-
-
Save ozh/1b9f80aac785114e43a5e026f1b4166d to your computer and use it in GitHub Desktop.
Alternate bootstrap config class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* YOURLS actions upon instantiating | |
*/ | |
namespace YOURLS\Config; | |
class Init { | |
/** | |
* @param array | |
*/ | |
protected $actions; | |
/** | |
* @param $actions array Array of overridden actions | |
*/ | |
public function __construct($actions = array()) { | |
$this->actions = $this->defaults(); | |
if (is_array($actions)) { | |
$this->actions = array_merge($this->actions, $actions); | |
} | |
} | |
/** | |
* Default actions and behaviors when YOURLS is loaded | |
* | |
* @since 1.7.3 | |
* @return array | |
*/ | |
public function defaults() { | |
return array( | |
/** | |
* Whether to include core function files | |
*/ | |
'include_core_funcs' => true, | |
/** | |
* Whether to include auth function files | |
* By default do not load (let YOURLS decide depending on yourls_is_private() value) | |
*/ | |
'include_auth_funcs' => false, | |
/** | |
* Whether to include auth function files | |
* By default do not load | |
*/ | |
'include_install_upgrade_funcs' => false, | |
/** | |
* Whether to set default time zone | |
*/ | |
'default_timezone' => true, | |
/** | |
* Whether to load default text domain | |
*/ | |
'load_default_textdomain' => true, | |
/** | |
* Whether to check for maintenance mode and maybe die here | |
*/ | |
'check_maintenance_mode' => true, | |
/** | |
* Whether to fix $_REQUEST for IIS | |
*/ | |
'fix_request_uri' => true, | |
/** | |
* Whether to redirect to SSL if needed | |
*/ | |
'redirect_ssl' => true, | |
/** | |
* Whether to include DB engine | |
*/ | |
'include_db' => true, | |
/** | |
* Whether to include cache layer | |
*/ | |
'include_cache' => true, | |
/** | |
* Whether to end instantiating early if YOURLS_FAST_INIT is defined and true | |
*/ | |
'return_if_fast_init' => true, | |
/** | |
* Whether to read all options at once during starting | |
*/ | |
'get_all_options' => true, | |
/** | |
* Whether to register shutdown action | |
*/ | |
'register_shutdown' => true, | |
/** | |
* Whether to trigger action 'init' after core is loaded | |
*/ | |
'core_loaded' => true, | |
/** | |
* Whether to redirect to install procedure if needed | |
*/ | |
'redirect_to_install' => true, | |
/** | |
* Whether to redirect to upgrade procedure if needed | |
*/ | |
'check_if_upgrade_needed' => true, | |
/** | |
* Whether to load all plugins | |
*/ | |
'load_plugins' => true, | |
/** | |
* Whether to trigger the "plugins_loaded" action | |
*/ | |
'plugins_loaded_action' => true, | |
/** | |
* Whether to check if a new version if available | |
*/ | |
'check_new_version' => true, | |
/** | |
* Whether to trigger 'admin_init' if applicable | |
*/ | |
'init_admin' => true, | |
); | |
} | |
/** | |
* @since 1.7.3 | |
*/ | |
public function set_up() { | |
// Include core files | |
if ($this->actions['include_core_funcs'] === true) { | |
$this->include_core_functions(); | |
} | |
// Enforce UTC timezone to suppress PHP warnings -- correct date/time will be managed using the config time offset | |
if ($this->actions['default_timezone'] === true) { | |
date_default_timezone_set('UTC'); | |
} | |
// Load locale | |
if ($this->actions['load_default_textdomain'] === true) { | |
yourls_load_default_textdomain(); | |
} | |
// Check if we are in maintenance mode - if yes, it will die here. | |
if ($this->actions['check_maintenance_mode'] === true) { | |
yourls_check_maintenance_mode(); | |
} | |
// Fix REQUEST_URI for IIS | |
if ($this->actions['fix_request_uri'] === true) { | |
yourls_fix_request_uri(); | |
} | |
// If request for an admin page is http:// and SSL is required, redirect | |
if ($this->actions['redirect_ssl'] === true) { | |
$this->redirect_ssl_if_needed(); | |
} | |
// Create the YOURLS object $ydb that will contain everything we globally need | |
if ($this->actions['include_db'] === true) { | |
$this->include_db_files(); | |
} | |
// Allow early inclusion of a cache layer | |
if ($this->actions['include_cache'] === true) { | |
$this->include_cache_files(); | |
} | |
// Abort initialization here if fast init wanted (for tests/debug/do not use) | |
if ($this->actions['return_if_fast_init'] === true && defined('YOURLS_FAST_INIT') && YOURLS_FAST_INIT){ | |
return; | |
} | |
// Read options right from start | |
if ($this->actions['get_all_options'] === true) { | |
yourls_get_all_options(); | |
} | |
// Register shutdown function | |
if ($this->actions['register_shutdown'] === true) { | |
register_shutdown_function( 'yourls_shutdown' ); | |
} | |
// Core now loaded | |
if ($this->actions['core_loaded'] === true) { | |
yourls_do_action( 'init' ); // plugins can't see this, not loaded yet | |
} | |
// Check if need to redirect to install procedure | |
if ($this->actions['redirect_to_install'] === true) { | |
if (!yourls_is_installed() && !yourls_is_installing()) { | |
yourls_redirect( yourls_admin_url('install.php'), 302 ); | |
} | |
} | |
// Check if upgrade is needed (bypassed if upgrading or installing) | |
if ($this->actions['check_if_upgrade_needed'] === true) { | |
if (!yourls_is_upgrading() && !yourls_is_installing() && yourls_upgrade_is_needed()) { | |
yourls_redirect( yourls_admin_url('upgrade.php'), 302 ); | |
} | |
} | |
// Load all plugins | |
if ($this->actions['load_plugins'] === true) { | |
yourls_load_plugins(); | |
} | |
// Trigger plugin loaded action | |
if ($this->actions['plugins_loaded_action'] === true) { | |
yourls_do_action( 'plugins_loaded' ); | |
} | |
// Is there a new version of YOURLS ? | |
if ($this->actions['check_new_version'] === true) { | |
if (yourls_is_installed() && !yourls_is_upgrading()) { | |
yourls_tell_if_new_version(); | |
} | |
} | |
if ($this->actions['init_admin'] === true) { | |
if (yourls_is_admin()) { | |
yourls_do_action( 'admin_init' ); | |
} | |
} | |
} | |
/** | |
* @since 1.7.3 | |
* @return void | |
*/ | |
public function redirect_ssl_if_needed() { | |
if (yourls_is_admin() && yourls_needs_ssl() && !yourls_is_ssl()) { | |
if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) { | |
yourls_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ) ); | |
} else { | |
yourls_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); | |
} | |
exit(); | |
} | |
} | |
/** | |
* @since 1.7.3 | |
* @return void | |
*/ | |
public function include_db_files() { | |
// Allow drop-in replacement for the DB engine | |
if (file_exists(YOURLS_USERDIR.'/db.php')) { | |
require_once YOURLS_USERDIR.'/db.php'; | |
} else { | |
require_once YOURLS_INC.'/class-mysql.php'; | |
yourls_db_connect(); | |
} | |
} | |
/** | |
* @since 1.7.3 | |
* @return void | |
*/ | |
public function include_cache_files() { | |
if (file_exists(YOURLS_USERDIR.'/cache.php')) { | |
require_once YOURLS_USERDIR.'/cache.php'; | |
} | |
} | |
/** | |
* @since 1.7.3 | |
* @return void | |
*/ | |
public function include_core_functions() { | |
require_once YOURLS_INC.'/version.php'; | |
require_once YOURLS_INC.'/functions.php'; | |
require_once YOURLS_INC.'/functions-plugins.php'; | |
require_once YOURLS_INC.'/functions-formatting.php'; | |
require_once YOURLS_INC.'/functions-api.php'; | |
require_once YOURLS_INC.'/functions-kses.php'; | |
require_once YOURLS_INC.'/functions-l10n.php'; | |
require_once YOURLS_INC.'/functions-compat.php'; | |
require_once YOURLS_INC.'/functions-html.php'; | |
require_once YOURLS_INC.'/functions-http.php'; | |
require_once YOURLS_INC.'/functions-infos.php'; | |
require_once YOURLS_INC.'/functions-deprecated.php'; | |
// Load auth functions if needed | |
if (yourls_is_private() || $this->actions->include_auth_funcs === true) { | |
require_once YOURLS_INC.'/functions-auth.php'; | |
} | |
// Load install & upgrade functions if needed | |
if ($this->actions['include_install_upgrade_funcs'] === true) { | |
require_once YOURLS_INC.'/functions-upgrade.php'; | |
require_once YOURLS_INC.'/functions-install.php'; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Bootstrap YOURLS | |
* | |
* This file initialize everything needed for YOURLS | |
* If you need to bootstrap YOURLS (ie access its functions and features) simply include this file. | |
*/ | |
require __DIR__ . '/vendor/autoload.php'; | |
// Set up YOURLS config | |
$config = new \YOURLS\Config\Config; | |
/* The following require has to be at global level so the variables inside config.php, including user defined if any, | |
* are registered in the global scope. If this require is moved in \YOURLS\Config\Config, $yourls_user_passwords for | |
* instance isn't registered. | |
*/ | |
if (!defined('YOURLS_CONFIGFILE')) { | |
define('YOURLS_CONFIGFILE', $config->find_config()); | |
} | |
require_once YOURLS_CONFIGFILE; | |
$config->define_core_constants(); | |
// Initialize YOURLS with default behaviors | |
$yourls = new \YOURLS\Config\Init(); | |
$yourls->set_up(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment