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