]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/config/ConfigJson.php
4 * Class ConfigJson (ConfigIO implementation)
6 * Handle Shaarli's JSON configuration file.
8 class ConfigJson
implements ConfigIO
13 function read($filepath)
15 if (! is_readable($filepath)) {
18 $data = file_get_contents($filepath);
19 $data = str_replace(self
::getPhpHeaders(), '', $data);
20 $data = str_replace(self
::getPhpSuffix(), '', $data);
21 $data = json_decode($data, true);
23 $error = json_last_error();
24 throw new Exception('An error occured while parsing JSON file: error code #'. $error);
32 function write($filepath, $conf)
34 // JSON_PRETTY_PRINT is available from PHP 5.4.
35 $print = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT
: 0;
36 $data = self
::getPhpHeaders() . json_encode($conf, $print) . self
::getPhpSuffix();
37 if (!file_put_contents($filepath, $data)) {
38 throw new IOException(
40 'Shaarli could not create the config file.
41 Please make sure Shaarli has the right to write in the folder is it installed in.'
49 function getExtension()
55 * The JSON data is wrapped in a PHP file for security purpose.
56 * This way, even if the file is accessible, credentials and configuration won't be exposed.
58 * Note: this isn't a static field because concatenation isn't supported in field declaration before PHP 5.6.
60 * @return string PHP start tag and comment tag.
62 public static function getPhpHeaders()
64 return '<?php /*'. PHP_EOL
;
68 * Get PHP comment closing tags.
70 * Static method for consistency with getPhpHeaders.
72 * @return string PHP comment closing.
74 public static function getPhpSuffix()
76 return PHP_EOL
. '*/ ?>';