]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/config/ConfigPhp.php
Apply PHP Code Beautifier on source code for linter automatic fixes
[github/shaarli/Shaarli.git] / application / config / ConfigPhp.php
1 <?php
2
3 namespace Shaarli\Config;
4
5 /**
6 * Class ConfigPhp (ConfigIO implementation)
7 *
8 * Handle Shaarli's legacy PHP configuration file.
9 * Note: this is only designed to support the transition to JSON configuration.
10 */
11 class ConfigPhp implements ConfigIO
12 {
13 /**
14 * @var array List of config key without group.
15 */
16 public static $ROOT_KEYS = [
17 'login',
18 'hash',
19 'salt',
20 'timezone',
21 'title',
22 'titleLink',
23 'redirector',
24 'disablesessionprotection',
25 'privateLinkByDefault',
26 ];
27
28 /**
29 * Map legacy config keys with the new ones.
30 * If ConfigPhp is used, getting <newkey> will actually look for <legacykey>.
31 * The updater will use this array to transform keys when switching to JSON.
32 *
33 * @var array current key => legacy key.
34 */
35 public static $LEGACY_KEYS_MAPPING = [
36 'credentials.login' => 'login',
37 'credentials.hash' => 'hash',
38 'credentials.salt' => 'salt',
39 'resource.data_dir' => 'config.DATADIR',
40 'resource.config' => 'config.CONFIG_FILE',
41 'resource.datastore' => 'config.DATASTORE',
42 'resource.updates' => 'config.UPDATES_FILE',
43 'resource.log' => 'config.LOG_FILE',
44 'resource.update_check' => 'config.UPDATECHECK_FILENAME',
45 'resource.raintpl_tpl' => 'config.RAINTPL_TPL',
46 'resource.theme' => 'config.theme',
47 'resource.raintpl_tmp' => 'config.RAINTPL_TMP',
48 'resource.thumbnails_cache' => 'config.CACHEDIR',
49 'resource.page_cache' => 'config.PAGECACHE',
50 'resource.ban_file' => 'config.IPBANS_FILENAME',
51 'security.session_protection_disabled' => 'disablesessionprotection',
52 'security.ban_after' => 'config.BAN_AFTER',
53 'security.ban_duration' => 'config.BAN_DURATION',
54 'general.title' => 'title',
55 'general.timezone' => 'timezone',
56 'general.header_link' => 'titleLink',
57 'updates.check_updates' => 'config.ENABLE_UPDATECHECK',
58 'updates.check_updates_branch' => 'config.UPDATECHECK_BRANCH',
59 'updates.check_updates_interval' => 'config.UPDATECHECK_INTERVAL',
60 'privacy.default_private_links' => 'privateLinkByDefault',
61 'feed.rss_permalinks' => 'config.ENABLE_RSS_PERMALINKS',
62 'general.links_per_page' => 'config.LINKS_PER_PAGE',
63 'thumbnail.enable_thumbnails' => 'config.ENABLE_THUMBNAILS',
64 'thumbnail.enable_localcache' => 'config.ENABLE_LOCALCACHE',
65 'general.enabled_plugins' => 'config.ENABLED_PLUGINS',
66 'redirector.url' => 'redirector',
67 'redirector.encode_url' => 'config.REDIRECTOR_URLENCODE',
68 'feed.show_atom' => 'config.SHOW_ATOM',
69 'privacy.hide_public_links' => 'config.HIDE_PUBLIC_LINKS',
70 'privacy.hide_timestamps' => 'config.HIDE_TIMESTAMPS',
71 'security.open_shaarli' => 'config.OPEN_SHAARLI',
72 ];
73
74 /**
75 * @inheritdoc
76 */
77 public function read($filepath)
78 {
79 if (! file_exists($filepath) || ! is_readable($filepath)) {
80 return [];
81 }
82
83 include $filepath;
84
85 $out = [];
86 foreach (self::$ROOT_KEYS as $key) {
87 $out[$key] = isset($GLOBALS[$key]) ? $GLOBALS[$key] : '';
88 }
89 $out['config'] = isset($GLOBALS['config']) ? $GLOBALS['config'] : [];
90 $out['plugins'] = isset($GLOBALS['plugins']) ? $GLOBALS['plugins'] : [];
91 return $out;
92 }
93
94 /**
95 * @inheritdoc
96 */
97 public function write($filepath, $conf)
98 {
99 $configStr = '<?php ' . PHP_EOL;
100 foreach (self::$ROOT_KEYS as $key) {
101 if (isset($conf[$key])) {
102 $configStr .= '$GLOBALS[\'' . $key . '\'] = ' . var_export($conf[$key], true) . ';' . PHP_EOL;
103 }
104 }
105
106 // Store all $conf['config']
107 foreach ($conf['config'] as $key => $value) {
108 $configStr .= '$GLOBALS[\'config\'][\''
109 . $key
110 . '\'] = '
111 . var_export($conf['config'][$key], true) . ';'
112 . PHP_EOL;
113 }
114
115 if (isset($conf['plugins'])) {
116 foreach ($conf['plugins'] as $key => $value) {
117 $configStr .= '$GLOBALS[\'plugins\'][\''
118 . $key
119 . '\'] = '
120 . var_export($conf['plugins'][$key], true) . ';'
121 . PHP_EOL;
122 }
123 }
124
125 if (
126 !file_put_contents($filepath, $configStr)
127 || strcmp(file_get_contents($filepath), $configStr) != 0
128 ) {
129 throw new \Shaarli\Exceptions\IOException(
130 $filepath,
131 t('Shaarli could not create the config file. ' .
132 'Please make sure Shaarli has the right to write in the folder is it installed in.')
133 );
134 }
135 }
136
137 /**
138 * @inheritdoc
139 */
140 public function getExtension()
141 {
142 return '.php';
143 }
144 }