aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/config/ConfigJson.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/config/ConfigJson.php')
-rw-r--r--application/config/ConfigJson.php33
1 files changed, 16 insertions, 17 deletions
diff --git a/application/config/ConfigJson.php b/application/config/ConfigJson.php
index cbafbf6d..94693c86 100644
--- a/application/config/ConfigJson.php
+++ b/application/config/ConfigJson.php
@@ -8,29 +8,15 @@
8class ConfigJson implements ConfigIO 8class ConfigJson implements ConfigIO
9{ 9{
10 /** 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 11 * @inheritdoc
26 */ 12 */
27 function read($filepath) 13 function read($filepath)
28 { 14 {
29 if (! file_exists($filepath) || ! is_readable($filepath)) { 15 if (! is_readable($filepath)) {
30 return array(); 16 return array();
31 } 17 }
32 $data = file_get_contents($filepath); 18 $data = file_get_contents($filepath);
33 $data = str_replace(self::$PHP_HEADER, '', $data); 19 $data = str_replace(self::getPhpHeaders(), '', $data);
34 $data = json_decode($data, true); 20 $data = json_decode($data, true);
35 if ($data === null) { 21 if ($data === null) {
36 $error = json_last_error(); 22 $error = json_last_error();
@@ -46,7 +32,7 @@ class ConfigJson implements ConfigIO
46 { 32 {
47 // JSON_PRETTY_PRINT is available from PHP 5.4. 33 // JSON_PRETTY_PRINT is available from PHP 5.4.
48 $print = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0; 34 $print = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
49 $data = self::$PHP_HEADER . json_encode($conf, $print); 35 $data = self::getPhpHeaders() . json_encode($conf, $print);
50 if (!file_put_contents($filepath, $data)) { 36 if (!file_put_contents($filepath, $data)) {
51 throw new IOException( 37 throw new IOException(
52 $filepath, 38 $filepath,
@@ -63,4 +49,17 @@ class ConfigJson implements ConfigIO
63 { 49 {
64 return '.json.php'; 50 return '.json.php';
65 } 51 }
52
53 /**
54 * The JSON data is wrapped in a PHP file for security purpose.
55 * This way, even if the file is accessible, credentials and configuration won't be exposed.
56 *
57 * Note: this isn't a static field because concatenation isn't supported in field declaration before PHP 5.6.
58 *
59 * @return string PHP start tag and comment tag.
60 */
61 public static function getPhpHeaders()
62 {
63 return '<?php /*'. PHP_EOL;
64 }
66} 65}