]>
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 | { | |
31 | $filepath .= $this->getExtension(); | |
32 | if (! file_exists($filepath) || ! is_readable($filepath)) { | |
33 | return array(); | |
34 | } | |
35 | ||
36 | include $filepath; | |
37 | ||
38 | $out = array(); | |
39 | foreach (self::$ROOT_KEYS as $key) { | |
40 | $out[$key] = $GLOBALS[$key]; | |
41 | } | |
42 | $out['config'] = $GLOBALS['config']; | |
43 | $out['plugins'] = !empty($GLOBALS['plugins']) ? $GLOBALS['plugins'] : array(); | |
44 | return $out; | |
45 | } | |
46 | ||
47 | /** | |
48 | * @inheritdoc | |
49 | */ | |
50 | function write($filepath, $conf) | |
51 | { | |
52 | $filepath .= $this->getExtension(); | |
53 | ||
54 | $configStr = '<?php '. PHP_EOL; | |
55 | foreach (self::$ROOT_KEYS as $key) { | |
56 | if (isset($conf[$key])) { | |
57 | $configStr .= '$GLOBALS[\'' . $key . '\'] = ' . var_export($conf[$key], true) . ';' . PHP_EOL; | |
58 | } | |
59 | } | |
60 | ||
61 | // Store all $conf['config'] | |
62 | foreach ($conf['config'] as $key => $value) { | |
63 | $configStr .= '$GLOBALS[\'config\'][\''. $key .'\'] = '.var_export($conf['config'][$key], true).';'. PHP_EOL; | |
64 | } | |
65 | ||
66 | if (isset($conf['plugins'])) { | |
67 | foreach ($conf['plugins'] as $key => $value) { | |
68 | $configStr .= '$GLOBALS[\'plugins\'][\''. $key .'\'] = '.var_export($conf['plugins'][$key], true).';'. PHP_EOL; | |
69 | } | |
70 | } | |
71 | ||
72 | // FIXME! | |
73 | //$configStr .= 'date_default_timezone_set('.var_export($conf['timezone'], true).');'. PHP_EOL; | |
74 | ||
75 | if (!file_put_contents($filepath, $configStr) | |
76 | || strcmp(file_get_contents($filepath), $configStr) != 0 | |
77 | ) { | |
78 | throw new IOException( | |
79 | $filepath, | |
80 | 'Shaarli could not create the config file. | |
81 | Please make sure Shaarli has the right to write in the folder is it installed in.' | |
82 | ); | |
83 | } | |
84 | } | |
85 | ||
86 | /** | |
87 | * @inheritdoc | |
88 | */ | |
89 | function getExtension() | |
90 | { | |
91 | return '.php'; | |
92 | } | |
93 | } |