From 684e662a58b02bde225e44d3677987b6fc3adf0b Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 18 May 2016 21:48:24 +0200 Subject: Replace $GLOBALS configuration with the configuration manager in the whole code base --- index.php | 443 ++++++++++++++++++++++++++++---------------------------------- 1 file changed, 200 insertions(+), 243 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index 7465c41f..780dc581 100644 --- a/index.php +++ b/index.php @@ -22,114 +22,13 @@ if (date_default_timezone_get() == '') { date_default_timezone_set('UTC'); } -/* ----------------------------------------------------------------------------- - * Hardcoded parameters - * You should not touch any code below (or at your own risks!) - * (These parameters can be overwritten by editing the file /data/config.php) - * ----------------------------------------------------------------------------- - */ - -/* - * Shaarli directories & configuration files - */ -// Data subdirectory -$GLOBALS['config']['DATADIR'] = 'data'; - -// Main configuration file -$GLOBALS['config']['CONFIG_FILE'] = $GLOBALS['config']['DATADIR'].'/config.php'; - -// Link datastore -$GLOBALS['config']['DATASTORE'] = $GLOBALS['config']['DATADIR'].'/datastore.php'; - -// Banned IPs -$GLOBALS['config']['IPBANS_FILENAME'] = $GLOBALS['config']['DATADIR'].'/ipbans.php'; - -// Processed updates file. -$GLOBALS['config']['UPDATES_FILE'] = $GLOBALS['config']['DATADIR'].'/updates.txt'; - -// Access log -$GLOBALS['config']['LOG_FILE'] = $GLOBALS['config']['DATADIR'].'/log.txt'; - -// For updates check of Shaarli -$GLOBALS['config']['UPDATECHECK_FILENAME'] = $GLOBALS['config']['DATADIR'].'/lastupdatecheck.txt'; - -// Set ENABLE_UPDATECHECK to disabled by default. -$GLOBALS['config']['ENABLE_UPDATECHECK'] = false; - -// RainTPL cache directory (keep the trailing slash!) -$GLOBALS['config']['RAINTPL_TMP'] = 'tmp/'; -// Raintpl template directory (keep the trailing slash!) -$GLOBALS['config']['RAINTPL_TPL'] = 'tpl/'; - -// Thumbnail cache directory -$GLOBALS['config']['CACHEDIR'] = 'cache'; - -// Atom & RSS feed cache directory -$GLOBALS['config']['PAGECACHE'] = 'pagecache'; - -/* - * Global configuration - */ -// Ban IP after this many failures -$GLOBALS['config']['BAN_AFTER'] = 4; -// Ban duration for IP address after login failures (in seconds) -$GLOBALS['config']['BAN_DURATION'] = 1800; - -// Feed options -// Enable RSS permalinks by default. -// This corresponds to the default behavior of shaarli before this was added as an option. -$GLOBALS['config']['ENABLE_RSS_PERMALINKS'] = true; -// If true, an extra "ATOM feed" button will be displayed in the toolbar -$GLOBALS['config']['SHOW_ATOM'] = false; - -// Link display options -$GLOBALS['config']['HIDE_PUBLIC_LINKS'] = false; -$GLOBALS['config']['HIDE_TIMESTAMPS'] = false; -$GLOBALS['config']['LINKS_PER_PAGE'] = 20; - -// Open Shaarli (true): anyone can add/edit/delete links without having to login -$GLOBALS['config']['OPEN_SHAARLI'] = false; - -// Thumbnails -// Display thumbnails in links -$GLOBALS['config']['ENABLE_THUMBNAILS'] = true; -// Store thumbnails in a local cache -$GLOBALS['config']['ENABLE_LOCALCACHE'] = true; - -// Update check frequency for Shaarli. 86400 seconds=24 hours -$GLOBALS['config']['UPDATECHECK_BRANCH'] = 'stable'; -$GLOBALS['config']['UPDATECHECK_INTERVAL'] = 86400; - -$GLOBALS['config']['REDIRECTOR_URLENCODE'] = true; - -/* - * Plugin configuration - * - * Warning: order matters! - * - * These settings may be be overriden in: - * - data/config.php - * - each plugin's configuration file - */ -//$GLOBALS['config']['ENABLED_PLUGINS'] = array( -// 'qrcode', 'archiveorg', 'readityourself', 'demo_plugin', 'playvideos', -// 'wallabag', 'markdown', 'addlink_toolbar', -//); -$GLOBALS['config']['ENABLED_PLUGINS'] = array('qrcode'); - -// Initialize plugin parameters array. -$GLOBALS['plugins'] = array(); - -// PubSubHubbub support. Put an empty string to disable, or put your hub url here to enable. -$GLOBALS['config']['PUBSUBHUB_URL'] = ''; - /* * PHP configuration */ define('shaarli_version', '0.7.0'); // http://server.com/x/shaarli --> /shaarli/ -define('WEB_PATH', substr($_SERVER["REQUEST_URI"], 0, 1+strrpos($_SERVER["REQUEST_URI"], '/', 0))); +define('WEB_PATH', substr($_SERVER['REQUEST_URI'], 0, 1+strrpos($_SERVER['REQUEST_URI'], '/', 0))); // High execution time in case of problematic imports/exports. ini_set('max_input_time','60'); @@ -144,12 +43,6 @@ error_reporting(E_ALL^E_WARNING); // See all errors (for debugging only) //error_reporting(-1); -/* - * User configuration - */ -if (is_file($GLOBALS['config']['CONFIG_FILE'])) { - require_once $GLOBALS['config']['CONFIG_FILE']; -} // Shaarli library require_once 'application/ApplicationUtils.php'; @@ -166,10 +59,12 @@ require_once 'application/PageBuilder.php'; require_once 'application/TimeZone.php'; require_once 'application/Url.php'; require_once 'application/Utils.php'; -require_once 'application/Config.php'; +require_once 'application/config/ConfigManager.php'; +require_once 'application/config/ConfigPlugin.php'; require_once 'application/PluginManager.php'; require_once 'application/Router.php'; require_once 'application/Updater.php'; +require_once 'inc/rain.tpl.class.php'; // Ensure the PHP version is supported try { @@ -210,16 +105,16 @@ if (isset($_COOKIE['shaarli']) && !is_session_id_valid($_COOKIE['shaarli'])) { $_COOKIE['shaarli'] = session_id(); } -include "inc/rain.tpl.class.php"; //include Rain TPL -raintpl::$tpl_dir = $GLOBALS['config']['RAINTPL_TPL']; // template directory -raintpl::$cache_dir = $GLOBALS['config']['RAINTPL_TMP']; // cache directory +$conf = ConfigManager::getInstance(); + +RainTPL::$tpl_dir = $conf->get('config.RAINTPL_TPL'); // template directory +RainTPL::$cache_dir = $conf->get('config.RAINTPL_TMP'); // cache directory $pluginManager = PluginManager::getInstance(); -$pluginManager->load($GLOBALS['config']['ENABLED_PLUGINS']); +$pluginManager->load($conf->get('config.ENABLED_PLUGINS')); ob_start(); // Output buffering for the page cache. - // In case stupid admin has left magic_quotes enabled in php.ini: if (get_magic_quotes_gpc()) { @@ -236,17 +131,25 @@ header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); // Handling of old config file which do not have the new parameters. -if (empty($GLOBALS['title'])) $GLOBALS['title']='Shared links on '.escape(index_url($_SERVER)); -if (empty($GLOBALS['timezone'])) $GLOBALS['timezone']=date_default_timezone_get(); -if (empty($GLOBALS['redirector'])) $GLOBALS['redirector']=''; -if (empty($GLOBALS['disablesessionprotection'])) $GLOBALS['disablesessionprotection']=false; -if (empty($GLOBALS['privateLinkByDefault'])) $GLOBALS['privateLinkByDefault']=false; -if (empty($GLOBALS['titleLink'])) $GLOBALS['titleLink']='?'; -// I really need to rewrite Shaarli with a proper configuation manager. - -if (! is_file($GLOBALS['config']['CONFIG_FILE'])) { +if (! $conf->exists('title')) { + $conf->set('title', 'Shared links on '. escape(index_url($_SERVER))); +} +if (! $conf->exists('timezone')) { + $conf->set('timezone', date_default_timezone_get()); +} +if (! $conf->exists('disablesessionprotection')) { + $conf->set('disablesessionprotection', false); +} +if (! $conf->exists('privateLinkByDefault')) { + $conf->set('privateLinkByDefault', false); +} +if (! $conf->exists('titleLink')) { + $conf->set('titleLink', '?'); +} + +if (! is_file($conf->getConfigFile())) { // Ensure Shaarli has proper access to its resources - $errors = ApplicationUtils::checkResourcePermissions($GLOBALS['config']); + $errors = ApplicationUtils::checkResourcePermissions(); if ($errors != array()) { $message = '

Insufficient permissions: