aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2016-06-09 20:04:02 +0200
committerArthurHoaro <arthur@hoa.ro>2016-06-11 09:30:56 +0200
commit278d9ee2836df7d805845077f26f8cecd16f0f4f (patch)
tree9155cab8890074e83b54efaa649bfa74885d3ab5 /application
parent7f179985b497053c59338667fe49c390aa626ab7 (diff)
downloadShaarli-278d9ee2836df7d805845077f26f8cecd16f0f4f.tar.gz
Shaarli-278d9ee2836df7d805845077f26f8cecd16f0f4f.tar.zst
Shaarli-278d9ee2836df7d805845077f26f8cecd16f0f4f.zip
ConfigManager no longer uses singleton pattern
Diffstat (limited to 'application')
-rw-r--r--application/ApplicationUtils.php7
-rw-r--r--application/PageBuilder.php48
-rw-r--r--application/Updater.php57
-rw-r--r--application/config/ConfigManager.php84
4 files changed, 101 insertions, 95 deletions
diff --git a/application/ApplicationUtils.php b/application/ApplicationUtils.php
index 37deb4b3..c5a157b9 100644
--- a/application/ApplicationUtils.php
+++ b/application/ApplicationUtils.php
@@ -132,12 +132,13 @@ class ApplicationUtils
132 /** 132 /**
133 * Checks Shaarli has the proper access permissions to its resources 133 * Checks Shaarli has the proper access permissions to its resources
134 * 134 *
135 * @param ConfigManager $conf Configuration Manager instance.
136 *
135 * @return array A list of the detected configuration issues 137 * @return array A list of the detected configuration issues
136 */ 138 */
137 public static function checkResourcePermissions() 139 public static function checkResourcePermissions($conf)
138 { 140 {
139 $errors = array(); 141 $errors = array();
140 $conf = ConfigManager::getInstance();
141 142
142 // Check script and template directories are readable 143 // Check script and template directories are readable
143 foreach (array( 144 foreach (array(
@@ -168,7 +169,7 @@ class ApplicationUtils
168 169
169 // Check configuration files are readable and writeable 170 // Check configuration files are readable and writeable
170 foreach (array( 171 foreach (array(
171 $conf->getConfigFile(), 172 $conf->getConfigFileExt(),
172 $conf->get('path.datastore'), 173 $conf->get('path.datastore'),
173 $conf->get('path.ban_file'), 174 $conf->get('path.ban_file'),
174 $conf->get('path.log'), 175 $conf->get('path.log'),
diff --git a/application/PageBuilder.php b/application/PageBuilder.php
index 04454865..843cc0dc 100644
--- a/application/PageBuilder.php
+++ b/application/PageBuilder.php
@@ -15,12 +15,20 @@ class PageBuilder
15 private $tpl; 15 private $tpl;
16 16
17 /** 17 /**
18 * @var ConfigManager $conf Configuration Manager instance.
19 */
20 protected $conf;
21
22 /**
18 * PageBuilder constructor. 23 * PageBuilder constructor.
19 * $tpl is initialized at false for lazy loading. 24 * $tpl is initialized at false for lazy loading.
25 *
26 * @param ConfigManager $conf Configuration Manager instance (reference).
20 */ 27 */
21 function __construct() 28 function __construct(&$conf)
22 { 29 {
23 $this->tpl = false; 30 $this->tpl = false;
31 $this->conf = $conf;
24 } 32 }
25 33
26 /** 34 /**
@@ -29,22 +37,21 @@ class PageBuilder
29 private function initialize() 37 private function initialize()
30 { 38 {
31 $this->tpl = new RainTPL(); 39 $this->tpl = new RainTPL();
32 $conf = ConfigManager::getInstance();
33 40
34 try { 41 try {
35 $version = ApplicationUtils::checkUpdate( 42 $version = ApplicationUtils::checkUpdate(
36 shaarli_version, 43 shaarli_version,
37 $conf->get('path.update_check'), 44 $this->conf->get('path.update_check'),
38 $conf->get('general.check_updates_interval'), 45 $this->conf->get('general.check_updates_interval'),
39 $conf->get('general.check_updates'), 46 $this->conf->get('general.check_updates'),
40 isLoggedIn(), 47 isLoggedIn(),
41 $conf->get('general.check_updates_branch') 48 $this->conf->get('general.check_updates_branch')
42 ); 49 );
43 $this->tpl->assign('newVersion', escape($version)); 50 $this->tpl->assign('newVersion', escape($version));
44 $this->tpl->assign('versionError', ''); 51 $this->tpl->assign('versionError', '');
45 52
46 } catch (Exception $exc) { 53 } catch (Exception $exc) {
47 logm($conf->get('path.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage()); 54 logm($this->conf->get('path.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
48 $this->tpl->assign('newVersion', ''); 55 $this->tpl->assign('newVersion', '');
49 $this->tpl->assign('versionError', escape($exc->getMessage())); 56 $this->tpl->assign('versionError', escape($exc->getMessage()));
50 } 57 }
@@ -63,19 +70,19 @@ class PageBuilder
63 $this->tpl->assign('scripturl', index_url($_SERVER)); 70 $this->tpl->assign('scripturl', index_url($_SERVER));
64 $this->tpl->assign('pagetitle', 'Shaarli'); 71 $this->tpl->assign('pagetitle', 'Shaarli');
65 $this->tpl->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links? 72 $this->tpl->assign('privateonly', !empty($_SESSION['privateonly'])); // Show only private links?
66 if ($conf->exists('general.title')) { 73 if ($this->conf->exists('general.title')) {
67 $this->tpl->assign('pagetitle', $conf->get('general.title')); 74 $this->tpl->assign('pagetitle', $this->conf->get('general.title'));
68 } 75 }
69 if ($conf->exists('general.header_link')) { 76 if ($this->conf->exists('general.header_link')) {
70 $this->tpl->assign('titleLink', $conf->get('general.header_link')); 77 $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
71 } 78 }
72 if ($conf->exists('pagetitle')) { 79 if ($this->conf->exists('pagetitle')) {
73 $this->tpl->assign('pagetitle', $conf->get('pagetitle')); 80 $this->tpl->assign('pagetitle', $this->conf->get('pagetitle'));
74 } 81 }
75 $this->tpl->assign('shaarlititle', $conf->get('title', 'Shaarli')); 82 $this->tpl->assign('shaarlititle', $this->conf->get('title', 'Shaarli'));
76 $this->tpl->assign('openshaarli', $conf->get('extras.open_shaarli', false)); 83 $this->tpl->assign('openshaarli', $this->conf->get('extras.open_shaarli', false));
77 $this->tpl->assign('showatom', $conf->get('extras.show_atom', false)); 84 $this->tpl->assign('showatom', $this->conf->get('extras.show_atom', false));
78 $this->tpl->assign('hide_timestamps', $conf->get('extras.hide_timestamps', false)); 85 $this->tpl->assign('hide_timestamps', $this->conf->get('extras.hide_timestamps', false));
79 if (!empty($GLOBALS['plugin_errors'])) { 86 if (!empty($GLOBALS['plugin_errors'])) {
80 $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']); 87 $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']);
81 } 88 }
@@ -89,7 +96,6 @@ class PageBuilder
89 */ 96 */
90 public function assign($placeholder, $value) 97 public function assign($placeholder, $value)
91 { 98 {
92 // Lazy initialization
93 if ($this->tpl === false) { 99 if ($this->tpl === false) {
94 $this->initialize(); 100 $this->initialize();
95 } 101 }
@@ -105,7 +111,6 @@ class PageBuilder
105 */ 111 */
106 public function assignAll($data) 112 public function assignAll($data)
107 { 113 {
108 // Lazy initialization
109 if ($this->tpl === false) { 114 if ($this->tpl === false) {
110 $this->initialize(); 115 $this->initialize();
111 } 116 }
@@ -117,6 +122,7 @@ class PageBuilder
117 foreach ($data as $key => $value) { 122 foreach ($data as $key => $value) {
118 $this->assign($key, $value); 123 $this->assign($key, $value);
119 } 124 }
125 return true;
120 } 126 }
121 127
122 /** 128 /**
@@ -127,10 +133,10 @@ class PageBuilder
127 */ 133 */
128 public function renderPage($page) 134 public function renderPage($page)
129 { 135 {
130 // Lazy initialization 136 if ($this->tpl === false) {
131 if ($this->tpl===false) {
132 $this->initialize(); 137 $this->initialize();
133 } 138 }
139
134 $this->tpl->draw($page); 140 $this->tpl->draw($page);
135 } 141 }
136 142
diff --git a/application/Updater.php b/application/Updater.php
index db2144fe..b8940e41 100644
--- a/application/Updater.php
+++ b/application/Updater.php
@@ -18,6 +18,11 @@ class Updater
18 protected $linkDB; 18 protected $linkDB;
19 19
20 /** 20 /**
21 * @var ConfigManager $conf Configuration Manager instance.
22 */
23 protected $conf;
24
25 /**
21 * @var bool True if the user is logged in, false otherwise. 26 * @var bool True if the user is logged in, false otherwise.
22 */ 27 */
23 protected $isLoggedIn; 28 protected $isLoggedIn;
@@ -30,14 +35,16 @@ class Updater
30 /** 35 /**
31 * Object constructor. 36 * Object constructor.
32 * 37 *
33 * @param array $doneUpdates Updates which are already done. 38 * @param array $doneUpdates Updates which are already done.
34 * @param LinkDB $linkDB LinkDB instance. 39 * @param LinkDB $linkDB LinkDB instance.
35 * @param boolean $isLoggedIn True if the user is logged in. 40 * @oaram ConfigManager $conf Configuration Manager instance.
41 * @param boolean $isLoggedIn True if the user is logged in.
36 */ 42 */
37 public function __construct($doneUpdates, $linkDB, $isLoggedIn) 43 public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn)
38 { 44 {
39 $this->doneUpdates = $doneUpdates; 45 $this->doneUpdates = $doneUpdates;
40 $this->linkDB = $linkDB; 46 $this->linkDB = $linkDB;
47 $this->conf = $conf;
41 $this->isLoggedIn = $isLoggedIn; 48 $this->isLoggedIn = $isLoggedIn;
42 49
43 // Retrieve all update methods. 50 // Retrieve all update methods.
@@ -107,21 +114,19 @@ class Updater
107 */ 114 */
108 public function updateMethodMergeDeprecatedConfigFile() 115 public function updateMethodMergeDeprecatedConfigFile()
109 { 116 {
110 $conf = ConfigManager::getInstance(); 117 if (is_file($this->conf->get('path.data_dir') . '/options.php')) {
111 118 include $this->conf->get('path.data_dir') . '/options.php';
112 if (is_file($conf->get('path.data_dir') . '/options.php')) {
113 include $conf->get('path.data_dir') . '/options.php';
114 119
115 // Load GLOBALS into config 120 // Load GLOBALS into config
116 $allowedKeys = array_merge(ConfigPhp::$ROOT_KEYS); 121 $allowedKeys = array_merge(ConfigPhp::$ROOT_KEYS);
117 $allowedKeys[] = 'config'; 122 $allowedKeys[] = 'config';
118 foreach ($GLOBALS as $key => $value) { 123 foreach ($GLOBALS as $key => $value) {
119 if (in_array($key, $allowedKeys)) { 124 if (in_array($key, $allowedKeys)) {
120 $conf->set($key, $value); 125 $this->conf->set($key, $value);
121 } 126 }
122 } 127 }
123 $conf->write($this->isLoggedIn); 128 $this->conf->write($this->isLoggedIn);
124 unlink($conf->get('path.data_dir').'/options.php'); 129 unlink($this->conf->get('path.data_dir').'/options.php');
125 } 130 }
126 131
127 return true; 132 return true;
@@ -132,14 +137,13 @@ class Updater
132 */ 137 */
133 public function updateMethodRenameDashTags() 138 public function updateMethodRenameDashTags()
134 { 139 {
135 $conf = ConfigManager::getInstance();
136 $linklist = $this->linkDB->filterSearch(); 140 $linklist = $this->linkDB->filterSearch();
137 foreach ($linklist as $link) { 141 foreach ($linklist as $link) {
138 $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']); 142 $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']);
139 $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); 143 $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true)));
140 $this->linkDB[$link['linkdate']] = $link; 144 $this->linkDB[$link['linkdate']] = $link;
141 } 145 }
142 $this->linkDB->savedb($conf->get('path.page_cache')); 146 $this->linkDB->savedb($this->conf->get('path.page_cache'));
143 return true; 147 return true;
144 } 148 }
145 149
@@ -151,23 +155,21 @@ class Updater
151 */ 155 */
152 public function updateMethodConfigToJson() 156 public function updateMethodConfigToJson()
153 { 157 {
154 $conf = ConfigManager::getInstance();
155
156 // JSON config already exists, nothing to do. 158 // JSON config already exists, nothing to do.
157 if ($conf->getConfigIO() instanceof ConfigJson) { 159 if ($this->conf->getConfigIO() instanceof ConfigJson) {
158 return true; 160 return true;
159 } 161 }
160 162
161 $configPhp = new ConfigPhp(); 163 $configPhp = new ConfigPhp();
162 $configJson = new ConfigJson(); 164 $configJson = new ConfigJson();
163 $oldConfig = $configPhp->read($conf::$CONFIG_FILE . '.php'); 165 $oldConfig = $configPhp->read($this->conf->getConfigFile() . '.php');
164 rename($conf->getConfigFile(), $conf::$CONFIG_FILE . '.save.php'); 166 rename($this->conf->getConfigFileExt(), $this->conf->getConfigFile() . '.save.php');
165 $conf->setConfigIO($configJson); 167 $this->conf->setConfigIO($configJson);
166 $conf->reload(); 168 $this->conf->reload();
167 169
168 $legacyMap = array_flip(ConfigPhp::$LEGACY_KEYS_MAPPING); 170 $legacyMap = array_flip(ConfigPhp::$LEGACY_KEYS_MAPPING);
169 foreach (ConfigPhp::$ROOT_KEYS as $key) { 171 foreach (ConfigPhp::$ROOT_KEYS as $key) {
170 $conf->set($legacyMap[$key], $oldConfig[$key]); 172 $this->conf->set($legacyMap[$key], $oldConfig[$key]);
171 } 173 }
172 174
173 // Set sub config keys (config and plugins) 175 // Set sub config keys (config and plugins)
@@ -179,12 +181,12 @@ class Updater
179 } else { 181 } else {
180 $configKey = $sub .'.'. $key; 182 $configKey = $sub .'.'. $key;
181 } 183 }
182 $conf->set($configKey, $value); 184 $this->conf->set($configKey, $value);
183 } 185 }
184 } 186 }
185 187
186 try{ 188 try{
187 $conf->write($this->isLoggedIn); 189 $this->conf->write($this->isLoggedIn);
188 return true; 190 return true;
189 } catch (IOException $e) { 191 } catch (IOException $e) {
190 error_log($e->getMessage()); 192 error_log($e->getMessage());
@@ -202,12 +204,11 @@ class Updater
202 */ 204 */
203 public function escapeUnescapedConfig() 205 public function escapeUnescapedConfig()
204 { 206 {
205 $conf = ConfigManager::getInstance();
206 try { 207 try {
207 $conf->set('general.title', escape($conf->get('general.title'))); 208 $this->conf->set('general.title', escape($this->conf->get('general.title')));
208 $conf->set('general.header_link', escape($conf->get('general.header_link'))); 209 $this->conf->set('general.header_link', escape($this->conf->get('general.header_link')));
209 $conf->set('extras.redirector', escape($conf->get('extras.redirector'))); 210 $this->conf->set('extras.redirector', escape($this->conf->get('extras.redirector')));
210 $conf->write($this->isLoggedIn); 211 $this->conf->write($this->isLoggedIn);
211 } catch (Exception $e) { 212 } catch (Exception $e) {
212 error_log($e->getMessage()); 213 error_log($e->getMessage());
213 return false; 214 return false;
diff --git a/application/config/ConfigManager.php b/application/config/ConfigManager.php
index c0482cf3..5aafc89d 100644
--- a/application/config/ConfigManager.php
+++ b/application/config/ConfigManager.php
@@ -2,13 +2,13 @@
2 2
3// FIXME! Namespaces... 3// FIXME! Namespaces...
4require_once 'ConfigIO.php'; 4require_once 'ConfigIO.php';
5require_once 'ConfigPhp.php';
6require_once 'ConfigJson.php'; 5require_once 'ConfigJson.php';
6require_once 'ConfigPhp.php';
7 7
8/** 8/**
9 * Class ConfigManager 9 * Class ConfigManager
10 * 10 *
11 * Singleton, manages all Shaarli's settings. 11 * Manages all Shaarli's settings.
12 * See the documentation for more information on settings: 12 * See the documentation for more information on settings:
13 * - doc/Shaarli-configuration.html 13 * - doc/Shaarli-configuration.html
14 * - https://github.com/shaarli/Shaarli/wiki/Shaarli-configuration 14 * - https://github.com/shaarli/Shaarli/wiki/Shaarli-configuration
@@ -16,19 +16,14 @@ require_once 'ConfigJson.php';
16class ConfigManager 16class ConfigManager
17{ 17{
18 /** 18 /**
19 * @var ConfigManager instance. 19 * @var string Flag telling a setting is not found.
20 */ 20 */
21 protected static $instance = null; 21 protected static $NOT_FOUND = 'NOT_FOUND';
22 22
23 /** 23 /**
24 * @var string Config folder. 24 * @var string Config folder.
25 */ 25 */
26 public static $CONFIG_FILE = 'data/config'; 26 protected $configFile;
27
28 /**
29 * @var string Flag telling a setting is not found.
30 */
31 protected static $NOT_FOUND = 'NOT_FOUND';
32 27
33 /** 28 /**
34 * @var array Loaded config array. 29 * @var array Loaded config array.
@@ -41,37 +36,20 @@ class ConfigManager
41 protected $configIO; 36 protected $configIO;
42 37
43 /** 38 /**
44 * Private constructor: new instances not allowed. 39 * Constructor.
45 */ 40 */
46 private function __construct() {} 41 public function __construct($configFile = 'data/config')
47
48 /**
49 * Cloning isn't allowed either.
50 */
51 private function __clone() {}
52
53 /**
54 * Return existing instance of PluginManager, or create it.
55 *
56 * @return ConfigManager instance.
57 */
58 public static function getInstance()
59 { 42 {
60 if (!(self::$instance instanceof self)) { 43 $this->configFile = $configFile;
61 self::$instance = new self(); 44 $this->initialize();
62 self::$instance->initialize();
63 }
64
65 return self::$instance;
66 } 45 }
67 46
68 /** 47 /**
69 * Reset the ConfigManager instance. 48 * Reset the ConfigManager instance.
70 */ 49 */
71 public static function reset() 50 public function reset()
72 { 51 {
73 self::$instance = null; 52 $this->initialize();
74 return self::getInstance();
75 } 53 }
76 54
77 /** 55 /**
@@ -87,10 +65,10 @@ class ConfigManager
87 */ 65 */
88 protected function initialize() 66 protected function initialize()
89 { 67 {
90 if (! file_exists(self::$CONFIG_FILE .'.php')) { 68 if (file_exists($this->configFile . '.php')) {
91 $this->configIO = new ConfigJson();
92 } else {
93 $this->configIO = new ConfigPhp(); 69 $this->configIO = new ConfigPhp();
70 } else {
71 $this->configIO = new ConfigJson();
94 } 72 }
95 $this->load(); 73 $this->load();
96 } 74 }
@@ -100,7 +78,7 @@ class ConfigManager
100 */ 78 */
101 protected function load() 79 protected function load()
102 { 80 {
103 $this->loadedConfig = $this->configIO->read($this->getConfigFile()); 81 $this->loadedConfig = $this->configIO->read($this->getConfigFileExt());
104 $this->setDefaultValues(); 82 $this->setDefaultValues();
105 } 83 }
106 84
@@ -213,7 +191,7 @@ class ConfigManager
213 ); 191 );
214 192
215 // Only logged in user can alter config. 193 // Only logged in user can alter config.
216 if (is_file(self::$CONFIG_FILE) && !$isLoggedIn) { 194 if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
217 throw new UnauthorizedConfigException(); 195 throw new UnauthorizedConfigException();
218 } 196 }
219 197
@@ -224,17 +202,37 @@ class ConfigManager
224 } 202 }
225 } 203 }
226 204
227 return $this->configIO->write($this->getConfigFile(), $this->loadedConfig); 205 return $this->configIO->write($this->getConfigFileExt(), $this->loadedConfig);
228 } 206 }
229 207
230 /** 208 /**
231 * Get the configuration file path. 209 * Set the config file path (without extension).
232 * 210 *
233 * @return string Config file path. 211 * @param string $configFile File path.
212 */
213 public function setConfigFile($configFile)
214 {
215 $this->configFile = $configFile;
216 }
217
218 /**
219 * Return the configuration file path (without extension).
220 *
221 * @return string Config path.
234 */ 222 */
235 public function getConfigFile() 223 public function getConfigFile()
236 { 224 {
237 return self::$CONFIG_FILE . $this->configIO->getExtension(); 225 return $this->configFile;
226 }
227
228 /**
229 * Get the configuration file path with its extension.
230 *
231 * @return string Config file path.
232 */
233 public function getConfigFileExt()
234 {
235 return $this->configFile . $this->configIO->getExtension();
238 } 236 }
239 237
240 /** 238 /**
@@ -302,7 +300,7 @@ class ConfigManager
302 $this->setEmpty('path.page_cache', 'pagecache'); 300 $this->setEmpty('path.page_cache', 'pagecache');
303 301
304 $this->setEmpty('security.ban_after', 4); 302 $this->setEmpty('security.ban_after', 4);
305 $this->setEmpty('security.ban_after', 1800); 303 $this->setEmpty('security.ban_duration', 1800);
306 $this->setEmpty('security.session_protection_disabled', false); 304 $this->setEmpty('security.session_protection_disabled', false);
307 305
308 $this->setEmpty('general.check_updates', false); 306 $this->setEmpty('general.check_updates', false);