]>
Commit | Line | Data |
---|---|---|
59404d79 A |
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 | ||
da10377b A |
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', | |
894a3c4b A |
37 | 'resource.data_dir' => 'config.DATADIR', |
38 | 'resource.config' => 'config.CONFIG_FILE', | |
39 | 'resource.datastore' => 'config.DATASTORE', | |
40 | 'resource.updates' => 'config.UPDATES_FILE', | |
41 | 'resource.log' => 'config.LOG_FILE', | |
42 | 'resource.update_check' => 'config.UPDATECHECK_FILENAME', | |
43 | 'resource.raintpl_tpl' => 'config.RAINTPL_TPL', | |
44 | 'resource.raintpl_tmp' => 'config.RAINTPL_TMP', | |
45 | 'resource.thumbnails_cache' => 'config.CACHEDIR', | |
46 | 'resource.page_cache' => 'config.PAGECACHE', | |
47 | 'resource.ban_file' => 'config.IPBANS_FILENAME', | |
da10377b A |
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', | |
894a3c4b A |
54 | 'updates.check_updates' => 'config.ENABLE_UPDATECHECK', |
55 | 'updates.check_updates_branch' => 'config.UPDATECHECK_BRANCH', | |
56 | 'updates.check_updates_interval' => 'config.UPDATECHECK_INTERVAL', | |
57 | 'privacy.default_private_links' => 'privateLinkByDefault', | |
58 | 'feed.rss_permalinks' => 'config.ENABLE_RSS_PERMALINKS', | |
da10377b | 59 | 'general.links_per_page' => 'config.LINKS_PER_PAGE', |
894a3c4b A |
60 | 'thumbnail.enable_thumbnails' => 'config.ENABLE_THUMBNAILS', |
61 | 'thumbnail.enable_localcache' => 'config.ENABLE_LOCALCACHE', | |
da10377b | 62 | 'general.enabled_plugins' => 'config.ENABLED_PLUGINS', |
894a3c4b A |
63 | 'redirector.url' => 'redirector', |
64 | 'redirector.encode_url' => 'config.REDIRECTOR_URLENCODE', | |
65 | 'feed.show_atom' => 'config.SHOW_ATOM', | |
66 | 'privacy.hide_public_links' => 'config.HIDE_PUBLIC_LINKS', | |
67 | 'privacy.hide_timestamps' => 'config.HIDE_TIMESTAMPS', | |
68 | 'security.open_shaarli' => 'config.OPEN_SHAARLI', | |
da10377b A |
69 | ); |
70 | ||
59404d79 A |
71 | /** |
72 | * @inheritdoc | |
73 | */ | |
74 | function read($filepath) | |
75 | { | |
59404d79 A |
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 | { | |
59404d79 A |
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 | ||
59404d79 A |
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 | } |