]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/config/ConfigPhp.php
namespacing: \Shaarli\Exceptions\IOException
[github/shaarli/Shaarli.git] / application / config / ConfigPhp.php
1 <?php
2 namespace Shaarli\Config;
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
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',
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',
45 'resource.theme' => 'config.theme',
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',
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',
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',
61 'general.links_per_page' => 'config.LINKS_PER_PAGE',
62 'thumbnail.enable_thumbnails' => 'config.ENABLE_THUMBNAILS',
63 'thumbnail.enable_localcache' => 'config.ENABLE_LOCALCACHE',
64 'general.enabled_plugins' => 'config.ENABLED_PLUGINS',
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',
71 );
72
73 /**
74 * @inheritdoc
75 */
76 public function read($filepath)
77 {
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] = isset($GLOBALS[$key]) ? $GLOBALS[$key] : '';
87 }
88 $out['config'] = isset($GLOBALS['config']) ? $GLOBALS['config'] : [];
89 $out['plugins'] = isset($GLOBALS['plugins']) ? $GLOBALS['plugins'] : [];
90 return $out;
91 }
92
93 /**
94 * @inheritdoc
95 */
96 public function write($filepath, $conf)
97 {
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 }
104
105 // Store all $conf['config']
106 foreach ($conf['config'] as $key => $value) {
107 $configStr .= '$GLOBALS[\'config\'][\''
108 . $key
109 .'\'] = '
110 .var_export($conf['config'][$key], true).';'
111 . PHP_EOL;
112 }
113
114 if (isset($conf['plugins'])) {
115 foreach ($conf['plugins'] as $key => $value) {
116 $configStr .= '$GLOBALS[\'plugins\'][\''
117 . $key
118 .'\'] = '
119 .var_export($conf['plugins'][$key], true).';'
120 . PHP_EOL;
121 }
122 }
123
124 if (!file_put_contents($filepath, $configStr)
125 || strcmp(file_get_contents($filepath), $configStr) != 0
126 ) {
127 throw new \Shaarli\Exceptions\IOException(
128 $filepath,
129 t('Shaarli could not create the config file. '.
130 'Please make sure Shaarli has the right to write in the folder is it installed in.')
131 );
132 }
133 }
134
135 /**
136 * @inheritdoc
137 */
138 public function getExtension()
139 {
140 return '.php';
141 }
142 }