]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/config/ConfigPhp.php
Rename configuration keys and fix GLOBALS in templates
[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 * Map legacy config keys with the new ones.
28 * If ConfigPhp is used, getting <newkey> will actually look for <legacykey>.
29 * The Updater will use this array to transform keys when switching to JSON.
30 *
31 * @var array current key => legacy key.
32 */
33 public static $LEGACY_KEYS_MAPPING = array(
34 'credentials.login' => 'login',
35 'credentials.hash' => 'hash',
36 'credentials.salt' => 'salt',
37 'path.data_dir' => 'config.DATADIR',
38 'path.config' => 'config.CONFIG_FILE',
39 'path.datastore' => 'config.DATASTORE',
40 'path.updates' => 'config.UPDATES_FILE',
41 'path.log' => 'config.LOG_FILE',
42 'path.update_check' => 'config.UPDATECHECK_FILENAME',
43 'path.raintpl_tpl' => 'config.RAINTPL_TPL',
44 'path.raintpl_tmp' => 'config.RAINTPL_TMP',
45 'path.thumbnails_cache' => 'config.CACHEDIR',
46 'path.page_cache' => 'config.PAGECACHE',
47 'path.ban_file' => 'config.IPBANS_FILENAME',
48 'security.session_protection_disabled' => 'disablesessionprotection',
49 'security.ban_after' => 'config.BAN_AFTER',
50 'security.ban_duration' => 'config.BAN_DURATION',
51 'general.title' => 'title',
52 'general.timezone' => 'timezone',
53 'general.header_link' => 'titleLink',
54 'general.check_updates' => 'config.ENABLE_UPDATECHECK',
55 'general.check_updates_branch' => 'config.UPDATECHECK_BRANCH',
56 'general.check_updates_interval' => 'config.UPDATECHECK_INTERVAL',
57 'general.default_private_links' => 'privateLinkByDefault',
58 'general.rss_permalinks' => 'config.ENABLE_RSS_PERMALINKS',
59 'general.links_per_page' => 'config.LINKS_PER_PAGE',
60 'general.enable_thumbnails' => 'config.ENABLE_THUMBNAILS',
61 'general.enable_localcache' => 'config.ENABLE_LOCALCACHE',
62 'general.enabled_plugins' => 'config.ENABLED_PLUGINS',
63 'extras.redirector' => 'redirector',
64 'extras.redirector_encode_url' => 'config.REDIRECTOR_URLENCODE',
65 'extras.show_atom' => 'config.SHOW_ATOM',
66 'extras.hide_public_links' => 'config.HIDE_PUBLIC_LINKS',
67 'extras.hide_timestamps' => 'config.HIDE_TIMESTAMPS',
68 'extras.open_shaarli' => 'config.OPEN_SHAARLI',
69 );
70
71 /**
72 * @inheritdoc
73 */
74 function read($filepath)
75 {
76 if (! file_exists($filepath) || ! is_readable($filepath)) {
77 return array();
78 }
79
80 include $filepath;
81
82 $out = array();
83 foreach (self::$ROOT_KEYS as $key) {
84 $out[$key] = $GLOBALS[$key];
85 }
86 $out['config'] = $GLOBALS['config'];
87 $out['plugins'] = !empty($GLOBALS['plugins']) ? $GLOBALS['plugins'] : array();
88 return $out;
89 }
90
91 /**
92 * @inheritdoc
93 */
94 function write($filepath, $conf)
95 {
96 $configStr = '<?php '. PHP_EOL;
97 foreach (self::$ROOT_KEYS as $key) {
98 if (isset($conf[$key])) {
99 $configStr .= '$GLOBALS[\'' . $key . '\'] = ' . var_export($conf[$key], true) . ';' . PHP_EOL;
100 }
101 }
102
103 // Store all $conf['config']
104 foreach ($conf['config'] as $key => $value) {
105 $configStr .= '$GLOBALS[\'config\'][\''. $key .'\'] = '.var_export($conf['config'][$key], true).';'. PHP_EOL;
106 }
107
108 if (isset($conf['plugins'])) {
109 foreach ($conf['plugins'] as $key => $value) {
110 $configStr .= '$GLOBALS[\'plugins\'][\''. $key .'\'] = '.var_export($conf['plugins'][$key], true).';'. PHP_EOL;
111 }
112 }
113
114 if (!file_put_contents($filepath, $configStr)
115 || strcmp(file_get_contents($filepath), $configStr) != 0
116 ) {
117 throw new IOException(
118 $filepath,
119 'Shaarli could not create the config file.
120 Please make sure Shaarli has the right to write in the folder is it installed in.'
121 );
122 }
123 }
124
125 /**
126 * @inheritdoc
127 */
128 function getExtension()
129 {
130 return '.php';
131 }
132 }