]>
Commit | Line | Data |
---|---|---|
59404d79 | 1 | <?php |
53054b2b | 2 | |
3c66e564 | 3 | namespace Shaarli\Config; |
59404d79 A |
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 | */ | |
53054b2b | 16 | public static $ROOT_KEYS = [ |
59404d79 A |
17 | 'login', |
18 | 'hash', | |
19 | 'salt', | |
20 | 'timezone', | |
21 | 'title', | |
22 | 'titleLink', | |
23 | 'redirector', | |
24 | 'disablesessionprotection', | |
25 | 'privateLinkByDefault', | |
53054b2b | 26 | ]; |
59404d79 | 27 | |
da10377b A |
28 | /** |
29 | * Map legacy config keys with the new ones. | |
30 | * If ConfigPhp is used, getting <newkey> will actually look for <legacykey>. | |
bcf056c9 | 31 | * The updater will use this array to transform keys when switching to JSON. |
da10377b A |
32 | * |
33 | * @var array current key => legacy key. | |
34 | */ | |
53054b2b | 35 | public static $LEGACY_KEYS_MAPPING = [ |
da10377b A |
36 | 'credentials.login' => 'login', |
37 | 'credentials.hash' => 'hash', | |
38 | 'credentials.salt' => 'salt', | |
894a3c4b A |
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', | |
adc4aee8 | 46 | 'resource.theme' => 'config.theme', |
894a3c4b A |
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', | |
da10377b A |
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', | |
894a3c4b A |
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', | |
da10377b | 62 | 'general.links_per_page' => 'config.LINKS_PER_PAGE', |
894a3c4b A |
63 | 'thumbnail.enable_thumbnails' => 'config.ENABLE_THUMBNAILS', |
64 | 'thumbnail.enable_localcache' => 'config.ENABLE_LOCALCACHE', | |
da10377b | 65 | 'general.enabled_plugins' => 'config.ENABLED_PLUGINS', |
894a3c4b A |
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', | |
53054b2b | 72 | ]; |
da10377b | 73 | |
59404d79 A |
74 | /** |
75 | * @inheritdoc | |
76 | */ | |
93b1fe54 | 77 | public function read($filepath) |
59404d79 | 78 | { |
59404d79 | 79 | if (! file_exists($filepath) || ! is_readable($filepath)) { |
53054b2b | 80 | return []; |
59404d79 A |
81 | } |
82 | ||
83 | include $filepath; | |
84 | ||
53054b2b | 85 | $out = []; |
59404d79 | 86 | foreach (self::$ROOT_KEYS as $key) { |
cb4ddbe4 | 87 | $out[$key] = isset($GLOBALS[$key]) ? $GLOBALS[$key] : ''; |
59404d79 | 88 | } |
cb4ddbe4 A |
89 | $out['config'] = isset($GLOBALS['config']) ? $GLOBALS['config'] : []; |
90 | $out['plugins'] = isset($GLOBALS['plugins']) ? $GLOBALS['plugins'] : []; | |
59404d79 A |
91 | return $out; |
92 | } | |
93 | ||
94 | /** | |
95 | * @inheritdoc | |
96 | */ | |
93b1fe54 | 97 | public function write($filepath, $conf) |
59404d79 | 98 | { |
53054b2b | 99 | $configStr = '<?php ' . PHP_EOL; |
59404d79 A |
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 | } | |
adc4aee8 | 105 | |
59404d79 A |
106 | // Store all $conf['config'] |
107 | foreach ($conf['config'] as $key => $value) { | |
9d9f6d75 V |
108 | $configStr .= '$GLOBALS[\'config\'][\'' |
109 | . $key | |
53054b2b A |
110 | . '\'] = ' |
111 | . var_export($conf['config'][$key], true) . ';' | |
9d9f6d75 | 112 | . PHP_EOL; |
59404d79 A |
113 | } |
114 | ||
115 | if (isset($conf['plugins'])) { | |
116 | foreach ($conf['plugins'] as $key => $value) { | |
9d9f6d75 V |
117 | $configStr .= '$GLOBALS[\'plugins\'][\'' |
118 | . $key | |
53054b2b A |
119 | . '\'] = ' |
120 | . var_export($conf['plugins'][$key], true) . ';' | |
9d9f6d75 | 121 | . PHP_EOL; |
59404d79 A |
122 | } |
123 | } | |
124 | ||
53054b2b A |
125 | if ( |
126 | !file_put_contents($filepath, $configStr) | |
59404d79 A |
127 | || strcmp(file_get_contents($filepath), $configStr) != 0 |
128 | ) { | |
f3d2f257 | 129 | throw new \Shaarli\Exceptions\IOException( |
59404d79 | 130 | $filepath, |
53054b2b | 131 | t('Shaarli could not create the config file. ' . |
12266213 | 132 | 'Please make sure Shaarli has the right to write in the folder is it installed in.') |
59404d79 A |
133 | ); |
134 | } | |
135 | } | |
136 | ||
137 | /** | |
138 | * @inheritdoc | |
139 | */ | |
93b1fe54 | 140 | public function getExtension() |
59404d79 A |
141 | { |
142 | return '.php'; | |
143 | } | |
144 | } |