]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/config/ConfigPhp.php
Replace $GLOBALS configuration with the configuration manager in the whole code base
[github/shaarli/Shaarli.git] / application / config / ConfigPhp.php
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 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 {
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
69 // FIXME!
70 //$configStr .= 'date_default_timezone_set('.var_export($conf['timezone'], true).');'. PHP_EOL;
71
72 if (!file_put_contents($filepath, $configStr)
73 || strcmp(file_get_contents($filepath), $configStr) != 0
74 ) {
75 throw new IOException(
76 $filepath,
77 'Shaarli could not create the config file.
78 Please make sure Shaarli has the right to write in the folder is it installed in.'
79 );
80 }
81 }
82
83 /**
84 * @inheritdoc
85 */
86 function getExtension()
87 {
88 return '.php';
89 }
90 }