]>
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
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.
14 * @var string PHP start tag and comment tag.
16 public static $PHP_HEADER;
18 public function __construct()
20 // The field can't be initialized directly with concatenation before PHP 5.6.
21 self
::$PHP_HEADER = '<?php /*'. PHP_EOL
;
27 function read($filepath)
29 if (! file_exists($filepath) || ! is_readable($filepath)) {
32 $data = file_get_contents($filepath);
33 $data = str_replace(self
::$PHP_HEADER, '', $data);
34 $data = json_decode($data, true);
36 $error = json_last_error();
37 throw new Exception('An error occured while parsing JSON file: error code #'. $error);
45 function write($filepath, $conf)
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(
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.'
62 function getExtension()