]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Config.php
0b01b524686a0523e429d3da3199c95fe788ff98
[github/shaarli/Shaarli.git] / application / Config.php
1 <?php
2 /**
3 * Functions related to configuration management.
4 */
5
6 /**
7 * Re-write configuration file according to given array.
8 * Requires mandatory fields listed in $MANDATORY_FIELDS.
9 *
10 * @param array $config contains all configuration fields.
11 * @param bool $isLoggedIn true if user is logged in.
12 *
13 * @return void
14 *
15 * @throws MissingFieldConfigException: a mandatory field has not been provided in $config.
16 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
17 * @throws Exception: an error occured while writing the new config file.
18 */
19 function writeConfig($config, $isLoggedIn)
20 {
21 // These fields are required in configuration.
22 $MANDATORY_FIELDS = [
23 'login', 'hash', 'salt', 'timezone', 'title', 'titleLink',
24 'redirector', 'disablesessionprotection', 'privateLinkByDefault'
25 ];
26
27 if (!isset($config['config']['CONFIG_FILE'])) {
28 throw new MissingFieldConfigException('CONFIG_FILE');
29 }
30
31 // Only logged in user can alter config.
32 if (is_file($config['config']['CONFIG_FILE']) && !$isLoggedIn) {
33 throw new UnauthorizedConfigException();
34 }
35
36 // Check that all mandatory fields are provided in $config.
37 foreach ($MANDATORY_FIELDS as $field) {
38 if (!isset($config[$field])) {
39 throw new MissingFieldConfigException($field);
40 }
41 }
42
43 $configStr = '<?php '. PHP_EOL;
44 $configStr .= '$GLOBALS[\'login\'] = '.var_export($config['login'], true).';'. PHP_EOL;
45 $configStr .= '$GLOBALS[\'hash\'] = '.var_export($config['hash'], true).';'. PHP_EOL;
46 $configStr .= '$GLOBALS[\'salt\'] = '.var_export($config['salt'], true).'; '. PHP_EOL;
47 $configStr .= '$GLOBALS[\'timezone\'] = '.var_export($config['timezone'], true).';'. PHP_EOL;
48 $configStr .= 'date_default_timezone_set('.var_export($config['timezone'], true).');'. PHP_EOL;
49 $configStr .= '$GLOBALS[\'title\'] = '.var_export($config['title'], true).';'. PHP_EOL;
50 $configStr .= '$GLOBALS[\'titleLink\'] = '.var_export($config['titleLink'], true).'; '. PHP_EOL;
51 $configStr .= '$GLOBALS[\'redirector\'] = '.var_export($config['redirector'], true).'; '. PHP_EOL;
52 $configStr .= '$GLOBALS[\'disablesessionprotection\'] = '.var_export($config['disablesessionprotection'], true).'; '. PHP_EOL;
53 $configStr .= '$GLOBALS[\'privateLinkByDefault\'] = '.var_export($config['privateLinkByDefault'], true).'; '. PHP_EOL;
54
55 // Store all $config['config']
56 foreach ($config['config'] as $key => $value) {
57 $configStr .= '$GLOBALS[\'config\'][\''. $key .'\'] = '.var_export($config['config'][$key], true).';'. PHP_EOL;
58 }
59 $configStr .= '?>';
60
61 if (!file_put_contents($config['config']['CONFIG_FILE'], $configStr)
62 || strcmp(file_get_contents($config['config']['CONFIG_FILE']), $configStr) != 0
63 ) {
64 throw new Exception(
65 'Shaarli could not create the config file.
66 Please make sure Shaarli has the right to write in the folder is it installed in.'
67 );
68 }
69 }
70
71 /**
72 * Milestone 0.9 - shaarli/Shaarli#41: options.php is not supported anymore.
73 * ==> if user is loggedIn, merge its content with config.php, then delete options.php.
74 *
75 * @param array $config contains all configuration fields.
76 * @param bool $isLoggedIn true if user is logged in.
77 *
78 * @return void
79 */
80 function mergeDeprecatedConfig($config, $isLoggedIn)
81 {
82 $config_file = $config['config']['CONFIG_FILE'];
83
84 if (is_file($config['config']['DATADIR'].'/options.php') && $isLoggedIn) {
85 include $config['config']['DATADIR'].'/options.php';
86
87 // Load GLOBALS into config
88 foreach ($GLOBALS as $key => $value) {
89 $config[$key] = $value;
90 }
91 $config['config']['CONFIG_FILE'] = $config_file;
92 writeConfig($config, $isLoggedIn);
93
94 unlink($config['config']['DATADIR'].'/options.php');
95 }
96 }
97
98 /**
99 * Exception used if a mandatory field is missing in given configuration.
100 */
101 class MissingFieldConfigException extends Exception
102 {
103 public $field;
104
105 /**
106 * Construct exception.
107 *
108 * @param string $field field name missing.
109 */
110 public function __construct($field)
111 {
112 $this->field = $field;
113 $this->message = 'Configuration value is required for '. $this->field;
114 }
115 }
116
117 /**
118 * Exception used if an unauthorized attempt to edit configuration has been made.
119 */
120 class UnauthorizedConfigException extends Exception
121 {
122 /**
123 * Construct exception.
124 */
125 public function __construct()
126 {
127 $this->message = 'You are not authorized to alter config.';
128 }
129 }