]>
Commit | Line | Data |
---|---|---|
59404d79 A |
1 | <?php |
2 | ||
3 | /** | |
4 | * Class ConfigPhp (ConfigIO implementation) | |
5 | * | |
6 | * Handle Shaarli's legacy PHP configuration file. | |
7 | * Note: this is only designed to support the transition to JSON configuration. | |
8 | */ | |
9 | class ConfigPhp implements ConfigIO | |
10 | { | |
11 | /** | |
12 | * @var array List of config key without group. | |
13 | */ | |
14 | public static $ROOT_KEYS = array( | |
15 | 'login', | |
16 | 'hash', | |
17 | 'salt', | |
18 | 'timezone', | |
19 | 'title', | |
20 | 'titleLink', | |
21 | 'redirector', | |
22 | 'disablesessionprotection', | |
23 | 'privateLinkByDefault', | |
24 | ); | |
25 | ||
26 | /** | |
27 | * @inheritdoc | |
28 | */ | |
29 | function read($filepath) | |
30 | { | |
59404d79 A |
31 | if (! file_exists($filepath) || ! is_readable($filepath)) { |
32 | return array(); | |
33 | } | |
34 | ||
35 | include $filepath; | |
36 | ||
37 | $out = array(); | |
38 | foreach (self::$ROOT_KEYS as $key) { | |
39 | $out[$key] = $GLOBALS[$key]; | |
40 | } | |
41 | $out['config'] = $GLOBALS['config']; | |
42 | $out['plugins'] = !empty($GLOBALS['plugins']) ? $GLOBALS['plugins'] : array(); | |
43 | return $out; | |
44 | } | |
45 | ||
46 | /** | |
47 | * @inheritdoc | |
48 | */ | |
49 | function write($filepath, $conf) | |
50 | { | |
59404d79 A |
51 | $configStr = '<?php '. PHP_EOL; |
52 | foreach (self::$ROOT_KEYS as $key) { | |
53 | if (isset($conf[$key])) { | |
54 | $configStr .= '$GLOBALS[\'' . $key . '\'] = ' . var_export($conf[$key], true) . ';' . PHP_EOL; | |
55 | } | |
56 | } | |
57 | ||
58 | // Store all $conf['config'] | |
59 | foreach ($conf['config'] as $key => $value) { | |
60 | $configStr .= '$GLOBALS[\'config\'][\''. $key .'\'] = '.var_export($conf['config'][$key], true).';'. PHP_EOL; | |
61 | } | |
62 | ||
63 | if (isset($conf['plugins'])) { | |
64 | foreach ($conf['plugins'] as $key => $value) { | |
65 | $configStr .= '$GLOBALS[\'plugins\'][\''. $key .'\'] = '.var_export($conf['plugins'][$key], true).';'. PHP_EOL; | |
66 | } | |
67 | } | |
68 | ||
59404d79 A |
69 | if (!file_put_contents($filepath, $configStr) |
70 | || strcmp(file_get_contents($filepath), $configStr) != 0 | |
71 | ) { | |
72 | throw new IOException( | |
73 | $filepath, | |
74 | 'Shaarli could not create the config file. | |
75 | Please make sure Shaarli has the right to write in the folder is it installed in.' | |
76 | ); | |
77 | } | |
78 | } | |
79 | ||
80 | /** | |
81 | * @inheritdoc | |
82 | */ | |
83 | function getExtension() | |
84 | { | |
85 | return '.php'; | |
86 | } | |
87 | } |