]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/config/ConfigPhp.php
Merge pull request #732 from ArthurHoaro/feature/theme-manager
[github/shaarli/Shaarli.git] / application / config / ConfigPhp.php
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
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',
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.theme' => 'config.theme',
45 'resource.raintpl_tmp' => 'config.RAINTPL_TMP',
46 'resource.thumbnails_cache' => 'config.CACHEDIR',
47 'resource.page_cache' => 'config.PAGECACHE',
48 'resource.ban_file' => 'config.IPBANS_FILENAME',
49 'security.session_protection_disabled' => 'disablesessionprotection',
50 'security.ban_after' => 'config.BAN_AFTER',
51 'security.ban_duration' => 'config.BAN_DURATION',
52 'general.title' => 'title',
53 'general.timezone' => 'timezone',
54 'general.header_link' => 'titleLink',
55 'updates.check_updates' => 'config.ENABLE_UPDATECHECK',
56 'updates.check_updates_branch' => 'config.UPDATECHECK_BRANCH',
57 'updates.check_updates_interval' => 'config.UPDATECHECK_INTERVAL',
58 'privacy.default_private_links' => 'privateLinkByDefault',
59 'feed.rss_permalinks' => 'config.ENABLE_RSS_PERMALINKS',
60 'general.links_per_page' => 'config.LINKS_PER_PAGE',
61 'thumbnail.enable_thumbnails' => 'config.ENABLE_THUMBNAILS',
62 'thumbnail.enable_localcache' => 'config.ENABLE_LOCALCACHE',
63 'general.enabled_plugins' => 'config.ENABLED_PLUGINS',
64 'redirector.url' => 'redirector',
65 'redirector.encode_url' => 'config.REDIRECTOR_URLENCODE',
66 'feed.show_atom' => 'config.SHOW_ATOM',
67 'privacy.hide_public_links' => 'config.HIDE_PUBLIC_LINKS',
68 'privacy.hide_timestamps' => 'config.HIDE_TIMESTAMPS',
69 'security.open_shaarli' => 'config.OPEN_SHAARLI',
70 );
71
72 /**
73 * @inheritdoc
74 */
75 public function read($filepath)
76 {
77 if (! file_exists($filepath) || ! is_readable($filepath)) {
78 return array();
79 }
80
81 include $filepath;
82
83 $out = array();
84 foreach (self::$ROOT_KEYS as $key) {
85 $out[$key] = $GLOBALS[$key];
86 }
87 $out['config'] = $GLOBALS['config'];
88 $out['plugins'] = !empty($GLOBALS['plugins']) ? $GLOBALS['plugins'] : array();
89 return $out;
90 }
91
92 /**
93 * @inheritdoc
94 */
95 public function write($filepath, $conf)
96 {
97 $configStr = '<?php '. PHP_EOL;
98 foreach (self::$ROOT_KEYS as $key) {
99 if (isset($conf[$key])) {
100 $configStr .= '$GLOBALS[\'' . $key . '\'] = ' . var_export($conf[$key], true) . ';' . PHP_EOL;
101 }
102 }
103
104 // Store all $conf['config']
105 foreach ($conf['config'] as $key => $value) {
106 $configStr .= '$GLOBALS[\'config\'][\''. $key .'\'] = '.var_export($conf['config'][$key], true).';'. PHP_EOL;
107 }
108
109 if (isset($conf['plugins'])) {
110 foreach ($conf['plugins'] as $key => $value) {
111 $configStr .= '$GLOBALS[\'plugins\'][\''. $key .'\'] = '.var_export($conf['plugins'][$key], true).';'. PHP_EOL;
112 }
113 }
114
115 if (!file_put_contents($filepath, $configStr)
116 || strcmp(file_get_contents($filepath), $configStr) != 0
117 ) {
118 throw new IOException(
119 $filepath,
120 'Shaarli could not create the config file.
121 Please make sure Shaarli has the right to write in the folder is it installed in.'
122 );
123 }
124 }
125
126 /**
127 * @inheritdoc
128 */
129 public function getExtension()
130 {
131 return '.php';
132 }
133 }