aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2016-05-29 12:32:14 +0200
committerArthurHoaro <arthur@hoa.ro>2016-06-11 09:30:56 +0200
commitb74b96bfbd0b778ac50fd17f5e107c51435b1678 (patch)
treefd2debc510c2c51e9b75e2081a31a10e9c02ad06 /application
parent684e662a58b02bde225e44d3677987b6fc3adf0b (diff)
downloadShaarli-b74b96bfbd0b778ac50fd17f5e107c51435b1678.tar.gz
Shaarli-b74b96bfbd0b778ac50fd17f5e107c51435b1678.tar.zst
Shaarli-b74b96bfbd0b778ac50fd17f5e107c51435b1678.zip
Adds ConfigJson which handle the configuration in JSON format.
Also use the Updater to make the transition
Diffstat (limited to 'application')
-rw-r--r--application/PageBuilder.php1
-rw-r--r--application/Updater.php43
-rw-r--r--application/config/ConfigIO.php2
-rw-r--r--application/config/ConfigJson.php66
-rw-r--r--application/config/ConfigManager.php7
5 files changed, 112 insertions, 7 deletions
diff --git a/application/PageBuilder.php b/application/PageBuilder.php
index cf13c714..1d3ba9e8 100644
--- a/application/PageBuilder.php
+++ b/application/PageBuilder.php
@@ -75,6 +75,7 @@ class PageBuilder
75 $this->tpl->assign('shaarlititle', $conf->get('title', 'Shaarli')); 75 $this->tpl->assign('shaarlititle', $conf->get('title', 'Shaarli'));
76 $this->tpl->assign('openshaarli', $conf->get('config.OPEN_SHAARLI', false)); 76 $this->tpl->assign('openshaarli', $conf->get('config.OPEN_SHAARLI', false));
77 $this->tpl->assign('showatom', $conf->get('config.SHOW_ATOM', false)); 77 $this->tpl->assign('showatom', $conf->get('config.SHOW_ATOM', false));
78 $this->tpl->assign('hide_timestamps', $conf->get('config.HIDE_TIMESTAMPS', false));
78 // FIXME! Globals 79 // FIXME! Globals
79 if (!empty($GLOBALS['plugin_errors'])) { 80 if (!empty($GLOBALS['plugin_errors'])) {
80 $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']); 81 $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']);
diff --git a/application/Updater.php b/application/Updater.php
index 6b92af3d..8552850c 100644
--- a/application/Updater.php
+++ b/application/Updater.php
@@ -142,6 +142,48 @@ class Updater
142 $this->linkDB->savedb($conf->get('config.PAGECACHE')); 142 $this->linkDB->savedb($conf->get('config.PAGECACHE'));
143 return true; 143 return true;
144 } 144 }
145
146 /**
147 * Move old configuration in PHP to the new config system in JSON format.
148 *
149 * Will rename 'config.php' into 'config.save.php' and create 'config.json'.
150 */
151 public function updateMethodConfigToJson()
152 {
153 $conf = ConfigManager::getInstance();
154
155 // JSON config already exists, nothing to do.
156 if ($conf->getConfigIO() instanceof ConfigJson) {
157 return true;
158 }
159
160 $configPhp = new ConfigPhp();
161 $configJson = new ConfigJson();
162 $oldConfig = $configPhp->read($conf::$CONFIG_FILE . '.php');
163 rename($conf->getConfigFile(), $conf::$CONFIG_FILE . '.save.php');
164 $conf->setConfigIO($configJson);
165 $conf->reload();
166
167 foreach (ConfigPhp::$ROOT_KEYS as $key) {
168 $conf->set($key, $oldConfig[$key]);
169 }
170
171 // Set sub config keys (config and plugins)
172 $subConfig = array('config', 'plugins');
173 foreach ($subConfig as $sub) {
174 foreach ($oldConfig[$sub] as $key => $value) {
175 $conf->set($sub .'.'. $key, $value);
176 }
177 }
178
179 try{
180 $conf->write($this->isLoggedIn);
181 return true;
182 } catch (IOException $e) {
183 error_log($e->getMessage());
184 return false;
185 }
186 }
145} 187}
146 188
147/** 189/**
@@ -199,7 +241,6 @@ class UpdaterException extends Exception
199 } 241 }
200} 242}
201 243
202
203/** 244/**
204 * Read the updates file, and return already done updates. 245 * Read the updates file, and return already done updates.
205 * 246 *
diff --git a/application/config/ConfigIO.php b/application/config/ConfigIO.php
index 4b1c9901..2b68fe6a 100644
--- a/application/config/ConfigIO.php
+++ b/application/config/ConfigIO.php
@@ -21,8 +21,6 @@ interface ConfigIO
21 * 21 *
22 * @param string $filepath Config file absolute path. 22 * @param string $filepath Config file absolute path.
23 * @param array $conf All configuration in an array. 23 * @param array $conf All configuration in an array.
24 *
25 * @return bool True if the configuration has been successfully written, false otherwise.
26 */ 24 */
27 function write($filepath, $conf); 25 function write($filepath, $conf);
28 26
diff --git a/application/config/ConfigJson.php b/application/config/ConfigJson.php
new file mode 100644
index 00000000..cbafbf6d
--- /dev/null
+++ b/application/config/ConfigJson.php
@@ -0,0 +1,66 @@
1<?php
2
3/**
4 * Class ConfigJson (ConfigIO implementation)
5 *
6 * Handle Shaarli's JSON configuration file.
7 */
8class ConfigJson implements ConfigIO
9{
10 /**
11 * The JSON data is wrapped in a PHP file for security purpose.
12 * This way, even if the file is accessible, credentials and configuration won't be exposed.
13 *
14 * @var string PHP start tag and comment tag.
15 */
16 public static $PHP_HEADER;
17
18 public function __construct()
19 {
20 // The field can't be initialized directly with concatenation before PHP 5.6.
21 self::$PHP_HEADER = '<?php /*'. PHP_EOL;
22 }
23
24 /**
25 * @inheritdoc
26 */
27 function read($filepath)
28 {
29 if (! file_exists($filepath) || ! is_readable($filepath)) {
30 return array();
31 }
32 $data = file_get_contents($filepath);
33 $data = str_replace(self::$PHP_HEADER, '', $data);
34 $data = json_decode($data, true);
35 if ($data === null) {
36 $error = json_last_error();
37 throw new Exception('An error occured while parsing JSON file: error code #'. $error);
38 }
39 return $data;
40 }
41
42 /**
43 * @inheritdoc
44 */
45 function write($filepath, $conf)
46 {
47 // JSON_PRETTY_PRINT is available from PHP 5.4.
48 $print = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
49 $data = self::$PHP_HEADER . json_encode($conf, $print);
50 if (!file_put_contents($filepath, $data)) {
51 throw new IOException(
52 $filepath,
53 'Shaarli could not create the config file.
54 Please make sure Shaarli has the right to write in the folder is it installed in.'
55 );
56 }
57 }
58
59 /**
60 * @inheritdoc
61 */
62 function getExtension()
63 {
64 return '.json.php';
65 }
66}
diff --git a/application/config/ConfigManager.php b/application/config/ConfigManager.php
index 212aac05..70456737 100644
--- a/application/config/ConfigManager.php
+++ b/application/config/ConfigManager.php
@@ -3,7 +3,7 @@
3// FIXME! Namespaces... 3// FIXME! Namespaces...
4require_once 'ConfigIO.php'; 4require_once 'ConfigIO.php';
5require_once 'ConfigPhp.php'; 5require_once 'ConfigPhp.php';
6#require_once 'ConfigJson.php'; 6require_once 'ConfigJson.php';
7 7
8/** 8/**
9 * Class ConfigManager 9 * Class ConfigManager
@@ -84,12 +84,11 @@ class ConfigManager
84 */ 84 */
85 protected function initialize() 85 protected function initialize()
86 { 86 {
87 /*if (! file_exists(self::$CONFIG_FILE .'.php')) { 87 if (! file_exists(self::$CONFIG_FILE .'.php')) {
88 $this->configIO = new ConfigJson(); 88 $this->configIO = new ConfigJson();
89 } else { 89 } else {
90 $this->configIO = new ConfigPhp(); 90 $this->configIO = new ConfigPhp();
91 }*/ 91 }
92 $this->configIO = new ConfigPhp();
93 $this->load(); 92 $this->load();
94 } 93 }
95 94