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