diff --git a/application/ApplicationUtils.php b/application/ApplicationUtils.php index 37deb4b..c5a157b 100644 --- a/application/ApplicationUtils.php +++ b/application/ApplicationUtils.php @@ -132,12 +132,13 @@ class ApplicationUtils /** * Checks Shaarli has the proper access permissions to its resources * + * @param ConfigManager $conf Configuration Manager instance. + * * @return array A list of the detected configuration issues */ - public static function checkResourcePermissions() + public static function checkResourcePermissions($conf) { $errors = array(); - $conf = ConfigManager::getInstance(); // Check script and template directories are readable foreach (array( @@ -168,7 +169,7 @@ class ApplicationUtils // Check configuration files are readable and writeable foreach (array( - $conf->getConfigFile(), + $conf->getConfigFileExt(), $conf->get('path.datastore'), $conf->get('path.ban_file'), $conf->get('path.log'), diff --git a/application/PageBuilder.php b/application/PageBuilder.php index 0445486..843cc0d 100644 --- a/application/PageBuilder.php +++ b/application/PageBuilder.php @@ -14,13 +14,21 @@ class PageBuilder */ private $tpl; + /** + * @var ConfigManager $conf Configuration Manager instance. + */ + protected $conf; + /** * PageBuilder constructor. * $tpl is initialized at false for lazy loading. + * + * @param ConfigManager $conf Configuration Manager instance (reference). */ - function __construct() + function __construct(&$conf) { $this->tpl = false; + $this->conf = $conf; } /** @@ -29,22 +37,21 @@ class PageBuilder private function initialize() { $this->tpl = new RainTPL(); - $conf = ConfigManager::getInstance(); try { $version = ApplicationUtils::checkUpdate( shaarli_version, - $conf->get('path.update_check'), - $conf->get('general.check_updates_interval'), - $conf->get('general.check_updates'), + $this->conf->get('path.update_check'), + $this->conf->get('general.check_updates_interval'), + $this->conf->get('general.check_updates'), isLoggedIn(), - $conf->get('general.check_updates_branch') + $this->conf->get('general.check_updates_branch') ); $this->tpl->assign('newVersion', escape($version)); $this->tpl->assign('versionError', ''); } catch (Exception $exc) { - logm($conf->get('path.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage()); + logm($this->conf->get('path.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage()); $this->tpl->assign('newVersion', ''); $this->tpl->assign('versionError', escape($exc->getMessage())); } @@ -63,19 +70,19 @@ class PageBuilder $this->tpl->assign('scripturl', index_url($_SERVER)); $this->tpl->assign('pagetitle', 'Shaarli'); $this->tpl->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links? - if ($conf->exists('general.title')) { - $this->tpl->assign('pagetitle', $conf->get('general.title')); + if ($this->conf->exists('general.title')) { + $this->tpl->assign('pagetitle', $this->conf->get('general.title')); } - if ($conf->exists('general.header_link')) { - $this->tpl->assign('titleLink', $conf->get('general.header_link')); + if ($this->conf->exists('general.header_link')) { + $this->tpl->assign('titleLink', $this->conf->get('general.header_link')); } - if ($conf->exists('pagetitle')) { - $this->tpl->assign('pagetitle', $conf->get('pagetitle')); + if ($this->conf->exists('pagetitle')) { + $this->tpl->assign('pagetitle', $this->conf->get('pagetitle')); } - $this->tpl->assign('shaarlititle', $conf->get('title', 'Shaarli')); - $this->tpl->assign('openshaarli', $conf->get('extras.open_shaarli', false)); - $this->tpl->assign('showatom', $conf->get('extras.show_atom', false)); - $this->tpl->assign('hide_timestamps', $conf->get('extras.hide_timestamps', false)); + $this->tpl->assign('shaarlititle', $this->conf->get('title', 'Shaarli')); + $this->tpl->assign('openshaarli', $this->conf->get('extras.open_shaarli', false)); + $this->tpl->assign('showatom', $this->conf->get('extras.show_atom', false)); + $this->tpl->assign('hide_timestamps', $this->conf->get('extras.hide_timestamps', false)); if (!empty($GLOBALS['plugin_errors'])) { $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']); } @@ -89,7 +96,6 @@ class PageBuilder */ public function assign($placeholder, $value) { - // Lazy initialization if ($this->tpl === false) { $this->initialize(); } @@ -105,7 +111,6 @@ class PageBuilder */ public function assignAll($data) { - // Lazy initialization if ($this->tpl === false) { $this->initialize(); } @@ -117,6 +122,7 @@ class PageBuilder foreach ($data as $key => $value) { $this->assign($key, $value); } + return true; } /** @@ -127,10 +133,10 @@ class PageBuilder */ public function renderPage($page) { - // Lazy initialization - if ($this->tpl===false) { + if ($this->tpl === false) { $this->initialize(); } + $this->tpl->draw($page); } diff --git a/application/Updater.php b/application/Updater.php index db2144f..b8940e4 100644 --- a/application/Updater.php +++ b/application/Updater.php @@ -17,6 +17,11 @@ class Updater */ protected $linkDB; + /** + * @var ConfigManager $conf Configuration Manager instance. + */ + protected $conf; + /** * @var bool True if the user is logged in, false otherwise. */ @@ -30,14 +35,16 @@ class Updater /** * Object constructor. * - * @param array $doneUpdates Updates which are already done. - * @param LinkDB $linkDB LinkDB instance. - * @param boolean $isLoggedIn True if the user is logged in. + * @param array $doneUpdates Updates which are already done. + * @param LinkDB $linkDB LinkDB instance. + * @oaram ConfigManager $conf Configuration Manager instance. + * @param boolean $isLoggedIn True if the user is logged in. */ - public function __construct($doneUpdates, $linkDB, $isLoggedIn) + public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn) { $this->doneUpdates = $doneUpdates; $this->linkDB = $linkDB; + $this->conf = $conf; $this->isLoggedIn = $isLoggedIn; // Retrieve all update methods. @@ -107,21 +114,19 @@ class Updater */ public function updateMethodMergeDeprecatedConfigFile() { - $conf = ConfigManager::getInstance(); - - if (is_file($conf->get('path.data_dir') . '/options.php')) { - include $conf->get('path.data_dir') . '/options.php'; + if (is_file($this->conf->get('path.data_dir') . '/options.php')) { + include $this->conf->get('path.data_dir') . '/options.php'; // Load GLOBALS into config $allowedKeys = array_merge(ConfigPhp::$ROOT_KEYS); $allowedKeys[] = 'config'; foreach ($GLOBALS as $key => $value) { if (in_array($key, $allowedKeys)) { - $conf->set($key, $value); + $this->conf->set($key, $value); } } - $conf->write($this->isLoggedIn); - unlink($conf->get('path.data_dir').'/options.php'); + $this->conf->write($this->isLoggedIn); + unlink($this->conf->get('path.data_dir').'/options.php'); } return true; @@ -132,14 +137,13 @@ class Updater */ public function updateMethodRenameDashTags() { - $conf = ConfigManager::getInstance(); $linklist = $this->linkDB->filterSearch(); foreach ($linklist as $link) { $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']); $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); $this->linkDB[$link['linkdate']] = $link; } - $this->linkDB->savedb($conf->get('path.page_cache')); + $this->linkDB->savedb($this->conf->get('path.page_cache')); return true; } @@ -151,23 +155,21 @@ class Updater */ public function updateMethodConfigToJson() { - $conf = ConfigManager::getInstance(); - // JSON config already exists, nothing to do. - if ($conf->getConfigIO() instanceof ConfigJson) { + if ($this->conf->getConfigIO() instanceof ConfigJson) { return true; } $configPhp = new ConfigPhp(); $configJson = new ConfigJson(); - $oldConfig = $configPhp->read($conf::$CONFIG_FILE . '.php'); - rename($conf->getConfigFile(), $conf::$CONFIG_FILE . '.save.php'); - $conf->setConfigIO($configJson); - $conf->reload(); + $oldConfig = $configPhp->read($this->conf->getConfigFile() . '.php'); + rename($this->conf->getConfigFileExt(), $this->conf->getConfigFile() . '.save.php'); + $this->conf->setConfigIO($configJson); + $this->conf->reload(); $legacyMap = array_flip(ConfigPhp::$LEGACY_KEYS_MAPPING); foreach (ConfigPhp::$ROOT_KEYS as $key) { - $conf->set($legacyMap[$key], $oldConfig[$key]); + $this->conf->set($legacyMap[$key], $oldConfig[$key]); } // Set sub config keys (config and plugins) @@ -179,12 +181,12 @@ class Updater } else { $configKey = $sub .'.'. $key; } - $conf->set($configKey, $value); + $this->conf->set($configKey, $value); } } try{ - $conf->write($this->isLoggedIn); + $this->conf->write($this->isLoggedIn); return true; } catch (IOException $e) { error_log($e->getMessage()); @@ -202,12 +204,11 @@ class Updater */ public function escapeUnescapedConfig() { - $conf = ConfigManager::getInstance(); try { - $conf->set('general.title', escape($conf->get('general.title'))); - $conf->set('general.header_link', escape($conf->get('general.header_link'))); - $conf->set('extras.redirector', escape($conf->get('extras.redirector'))); - $conf->write($this->isLoggedIn); + $this->conf->set('general.title', escape($this->conf->get('general.title'))); + $this->conf->set('general.header_link', escape($this->conf->get('general.header_link'))); + $this->conf->set('extras.redirector', escape($this->conf->get('extras.redirector'))); + $this->conf->write($this->isLoggedIn); } catch (Exception $e) { error_log($e->getMessage()); return false; diff --git a/application/config/ConfigManager.php b/application/config/ConfigManager.php index c0482cf..5aafc89 100644 --- a/application/config/ConfigManager.php +++ b/application/config/ConfigManager.php @@ -2,13 +2,13 @@ // FIXME! Namespaces... require_once 'ConfigIO.php'; -require_once 'ConfigPhp.php'; require_once 'ConfigJson.php'; +require_once 'ConfigPhp.php'; /** * Class ConfigManager * - * Singleton, manages all Shaarli's settings. + * Manages all Shaarli's settings. * See the documentation for more information on settings: * - doc/Shaarli-configuration.html * - https://github.com/shaarli/Shaarli/wiki/Shaarli-configuration @@ -16,19 +16,14 @@ require_once 'ConfigJson.php'; class ConfigManager { /** - * @var ConfigManager instance. + * @var string Flag telling a setting is not found. */ - protected static $instance = null; + protected static $NOT_FOUND = 'NOT_FOUND'; /** * @var string Config folder. */ - public static $CONFIG_FILE = 'data/config'; - - /** - * @var string Flag telling a setting is not found. - */ - protected static $NOT_FOUND = 'NOT_FOUND'; + protected $configFile; /** * @var array Loaded config array. @@ -41,37 +36,20 @@ class ConfigManager protected $configIO; /** - * Private constructor: new instances not allowed. + * Constructor. */ - private function __construct() {} - - /** - * Cloning isn't allowed either. - */ - private function __clone() {} - - /** - * Return existing instance of PluginManager, or create it. - * - * @return ConfigManager instance. - */ - public static function getInstance() + public function __construct($configFile = 'data/config') { - if (!(self::$instance instanceof self)) { - self::$instance = new self(); - self::$instance->initialize(); - } - - return self::$instance; + $this->configFile = $configFile; + $this->initialize(); } /** * Reset the ConfigManager instance. */ - public static function reset() + public function reset() { - self::$instance = null; - return self::getInstance(); + $this->initialize(); } /** @@ -87,10 +65,10 @@ class ConfigManager */ protected function initialize() { - if (! file_exists(self::$CONFIG_FILE .'.php')) { - $this->configIO = new ConfigJson(); - } else { + if (file_exists($this->configFile . '.php')) { $this->configIO = new ConfigPhp(); + } else { + $this->configIO = new ConfigJson(); } $this->load(); } @@ -100,7 +78,7 @@ class ConfigManager */ protected function load() { - $this->loadedConfig = $this->configIO->read($this->getConfigFile()); + $this->loadedConfig = $this->configIO->read($this->getConfigFileExt()); $this->setDefaultValues(); } @@ -213,7 +191,7 @@ class ConfigManager ); // Only logged in user can alter config. - if (is_file(self::$CONFIG_FILE) && !$isLoggedIn) { + if (is_file($this->getConfigFileExt()) && !$isLoggedIn) { throw new UnauthorizedConfigException(); } @@ -224,17 +202,37 @@ class ConfigManager } } - return $this->configIO->write($this->getConfigFile(), $this->loadedConfig); + return $this->configIO->write($this->getConfigFileExt(), $this->loadedConfig); } /** - * Get the configuration file path. + * Set the config file path (without extension). * - * @return string Config file path. + * @param string $configFile File path. + */ + public function setConfigFile($configFile) + { + $this->configFile = $configFile; + } + + /** + * Return the configuration file path (without extension). + * + * @return string Config path. */ public function getConfigFile() { - return self::$CONFIG_FILE . $this->configIO->getExtension(); + return $this->configFile; + } + + /** + * Get the configuration file path with its extension. + * + * @return string Config file path. + */ + public function getConfigFileExt() + { + return $this->configFile . $this->configIO->getExtension(); } /** @@ -302,7 +300,7 @@ class ConfigManager $this->setEmpty('path.page_cache', 'pagecache'); $this->setEmpty('security.ban_after', 4); - $this->setEmpty('security.ban_after', 1800); + $this->setEmpty('security.ban_duration', 1800); $this->setEmpty('security.session_protection_disabled', false); $this->setEmpty('general.check_updates', false); diff --git a/index.php b/index.php index ac4a680..d061f91 100644 --- a/index.php +++ b/index.php @@ -48,6 +48,8 @@ error_reporting(E_ALL^E_WARNING); require_once 'application/ApplicationUtils.php'; require_once 'application/Cache.php'; require_once 'application/CachedPage.php'; +require_once 'application/config/ConfigManager.php'; +require_once 'application/config/ConfigPlugin.php'; require_once 'application/FeedBuilder.php'; require_once 'application/FileUtils.php'; require_once 'application/HttpUtils.php'; @@ -59,8 +61,6 @@ require_once 'application/PageBuilder.php'; require_once 'application/TimeZone.php'; require_once 'application/Url.php'; require_once 'application/Utils.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'; @@ -105,13 +105,13 @@ if (isset($_COOKIE['shaarli']) && !is_session_id_valid($_COOKIE['shaarli'])) { $_COOKIE['shaarli'] = session_id(); } -$conf = ConfigManager::getInstance(); +$conf = new ConfigManager(); $conf->setEmpty('general.timezone', date_default_timezone_get()); $conf->setEmpty('general.title', 'Shared links on '. escape(index_url($_SERVER))); RainTPL::$tpl_dir = $conf->get('path.raintpl_tpl'); // template directory RainTPL::$cache_dir = $conf->get('path.raintpl_tmp'); // cache directory -$pluginManager = PluginManager::getInstance(); +$pluginManager = new PluginManager($conf); $pluginManager->load($conf->get('general.enabled_plugins')); date_default_timezone_set($conf->get('general.timezone', 'UTC')); @@ -133,9 +133,9 @@ header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); -if (! is_file($conf->getConfigFile())) { +if (! is_file($conf->getConfigFileExt())) { // Ensure Shaarli has proper access to its resources - $errors = ApplicationUtils::checkResourcePermissions(); + $errors = ApplicationUtils::checkResourcePermissions($conf); if ($errors != array()) { $message = '

Insufficient permissions: