]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/config/ConfigPhp.php
Merge pull request #1345 from ArthurHoaro/changelog-v0.11.1
[github/shaarli/Shaarli.git] / application / config / ConfigPhp.php
CommitLineData
59404d79 1<?php
3c66e564 2namespace 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 */
10class 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>.
bcf056c9 30 * The updater will use this array to transform keys when switching to JSON.
da10377b
A
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) {
cb4ddbe4 86 $out[$key] = isset($GLOBALS[$key]) ? $GLOBALS[$key] : '';
59404d79 87 }
cb4ddbe4
A
88 $out['config'] = isset($GLOBALS['config']) ? $GLOBALS['config'] : [];
89 $out['plugins'] = isset($GLOBALS['plugins']) ? $GLOBALS['plugins'] : [];
59404d79
A
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) {
9d9f6d75
V
107 $configStr .= '$GLOBALS[\'config\'][\''
108 . $key
109 .'\'] = '
110 .var_export($conf['config'][$key], true).';'
111 . PHP_EOL;
59404d79
A
112 }
113
114 if (isset($conf['plugins'])) {
115 foreach ($conf['plugins'] as $key => $value) {
9d9f6d75
V
116 $configStr .= '$GLOBALS[\'plugins\'][\''
117 . $key
118 .'\'] = '
119 .var_export($conf['plugins'][$key], true).';'
120 . PHP_EOL;
59404d79
A
121 }
122 }
123
59404d79
A
124 if (!file_put_contents($filepath, $configStr)
125 || strcmp(file_get_contents($filepath), $configStr) != 0
126 ) {
f3d2f257 127 throw new \Shaarli\Exceptions\IOException(
59404d79 128 $filepath,
12266213
A
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.')
59404d79
A
131 );
132 }
133 }
134
135 /**
136 * @inheritdoc
137 */
93b1fe54 138 public function getExtension()
59404d79
A
139 {
140 return '.php';
141 }
142}