aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/config
diff options
context:
space:
mode:
Diffstat (limited to 'application/config')
-rw-r--r--application/config/ConfigIO.php2
-rw-r--r--application/config/ConfigJson.php66
-rw-r--r--application/config/ConfigManager.php7
3 files changed, 69 insertions, 6 deletions
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