]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/Config.php
Plugin system - CORE
[github/shaarli/Shaarli.git] / application / Config.php
old mode 100755 (executable)
new mode 100644 (file)
index ec799d7..c71ef68
-<?php\r
-/**\r
- * Functions related to configuration management.\r
- */\r
-\r
-/**\r
- * Re-write configuration file according to given array.\r
- * Requires mandatory fields listed in $MANDATORY_FIELDS.\r
- *\r
- * @param array $config     contains all configuration fields.\r
- * @param bool  $isLoggedIn true if user is logged in.\r
- *\r
- * @return void\r
- *\r
- * @throws MissingFieldConfigException: a mandatory field has not been provided in $config.\r
- * @throws UnauthorizedConfigException: user is not authorize to change configuration.\r
- * @throws Exception: an error occured while writing the new config file.\r
- */\r
-function writeConfig($config, $isLoggedIn)\r
-{\r
-    // These fields are required in configuration.\r
-    $MANDATORY_FIELDS = array(\r
-        'login', 'hash', 'salt', 'timezone', 'title', 'titleLink',\r
-        'redirector', 'disablesessionprotection', 'privateLinkByDefault'\r
-    );\r
-\r
-    if (!isset($config['config']['CONFIG_FILE'])) {\r
-        throw new MissingFieldConfigException('CONFIG_FILE');\r
-    }\r
-\r
-    // Only logged in user can alter config.\r
-    if (is_file($config['config']['CONFIG_FILE']) && !$isLoggedIn) {\r
-        throw new UnauthorizedConfigException();\r
-    }\r
-\r
-    // Check that all mandatory fields are provided in $config.\r
-    foreach ($MANDATORY_FIELDS as $field) {\r
-        if (!isset($config[$field])) {\r
-            throw new MissingFieldConfigException($field);\r
-        }\r
-    }\r
-\r
-    $configStr = '<?php '. PHP_EOL;\r
-    $configStr .= '$GLOBALS[\'login\'] = '.var_export($config['login'], true).';'. PHP_EOL;\r
-    $configStr .= '$GLOBALS[\'hash\'] = '.var_export($config['hash'], true).';'. PHP_EOL;\r
-    $configStr .= '$GLOBALS[\'salt\'] = '.var_export($config['salt'], true).'; '. PHP_EOL;\r
-    $configStr .= '$GLOBALS[\'timezone\'] = '.var_export($config['timezone'], true).';'. PHP_EOL;\r
-    $configStr .= 'date_default_timezone_set('.var_export($config['timezone'], true).');'. PHP_EOL;\r
-    $configStr .= '$GLOBALS[\'title\'] = '.var_export($config['title'], true).';'. PHP_EOL;\r
-    $configStr .= '$GLOBALS[\'titleLink\'] = '.var_export($config['titleLink'], true).'; '. PHP_EOL;\r
-    $configStr .= '$GLOBALS[\'redirector\'] = '.var_export($config['redirector'], true).'; '. PHP_EOL;\r
-    $configStr .= '$GLOBALS[\'disablesessionprotection\'] = '.var_export($config['disablesessionprotection'], true).'; '. PHP_EOL;\r
-    $configStr .= '$GLOBALS[\'privateLinkByDefault\'] = '.var_export($config['privateLinkByDefault'], true).'; '. PHP_EOL;\r
-\r
-    // Store all $config['config']\r
-    foreach ($config['config'] as $key => $value) {\r
-        $configStr .= '$GLOBALS[\'config\'][\''. $key .'\'] = '.var_export($config['config'][$key], true).';'. PHP_EOL;\r
-    }\r
-    $configStr .= '?>';\r
-\r
-    if (!file_put_contents($config['config']['CONFIG_FILE'], $configStr)\r
-        || strcmp(file_get_contents($config['config']['CONFIG_FILE']), $configStr) != 0\r
-    ) {\r
-        throw new Exception(\r
-            'Shaarli could not create the config file.\r
-            Please make sure Shaarli has the right to write in the folder is it installed in.'\r
-        );\r
-    }\r
-}\r
-\r
-/**\r
- * Milestone 0.9 - shaarli/Shaarli#41: options.php is not supported anymore.\r
- * ==> if user is loggedIn, merge its content with config.php, then delete options.php.\r
- *\r
- * @param array $config     contains all configuration fields.\r
- * @param bool  $isLoggedIn true if user is logged in.\r
- *\r
- * @return void\r
- */\r
-function mergeDeprecatedConfig($config, $isLoggedIn)\r
-{\r
-    $config_file = $config['config']['CONFIG_FILE'];\r
-\r
-    if (is_file($config['config']['DATADIR'].'/options.php') && $isLoggedIn) {\r
-        include $config['config']['DATADIR'].'/options.php';\r
-\r
-        // Load GLOBALS into config\r
-        foreach ($GLOBALS as $key => $value) {\r
-            $config[$key] = $value;\r
-        }\r
-        $config['config']['CONFIG_FILE'] = $config_file;\r
-        writeConfig($config, $isLoggedIn);\r
-\r
-        unlink($config['config']['DATADIR'].'/options.php');\r
-    }\r
-}\r
-\r
-/**\r
- * Exception used if a mandatory field is missing in given configuration.\r
- */\r
-class MissingFieldConfigException extends Exception\r
-{\r
-    public $field;\r
-\r
-    /**\r
-     * Construct exception.\r
-     *\r
-     * @param string $field field name missing.\r
-     */\r
-    public function __construct($field)\r
-    {\r
-        $this->field = $field;\r
-        $this->message = 'Configuration value is required for '. $this->field;\r
-    }\r
-}\r
-\r
-/**\r
- * Exception used if an unauthorized attempt to edit configuration has been made.\r
- */\r
-class UnauthorizedConfigException extends Exception\r
-{\r
-    /**\r
-     * Construct exception.\r
-     */\r
-    public function __construct()\r
-    {\r
-        $this->message = 'You are not authorized to alter config.';\r
-    }\r
-}\r
+<?php
+/**
+ * Functions related to configuration management.
+ */
+
+/**
+ * Re-write configuration file according to given array.
+ * Requires mandatory fields listed in $MANDATORY_FIELDS.
+ *
+ * @param array $config     contains all configuration fields.
+ * @param bool  $isLoggedIn true if user is logged in.
+ *
+ * @return void
+ *
+ * @throws MissingFieldConfigException: a mandatory field has not been provided in $config.
+ * @throws UnauthorizedConfigException: user is not authorize to change configuration.
+ * @throws Exception: an error occured while writing the new config file.
+ */
+function writeConfig($config, $isLoggedIn)
+{
+    // These fields are required in configuration.
+    $MANDATORY_FIELDS = array(
+        'login', 'hash', 'salt', 'timezone', 'title', 'titleLink',
+        'redirector', 'disablesessionprotection', 'privateLinkByDefault'
+    );
+
+    if (!isset($config['config']['CONFIG_FILE'])) {
+        throw new MissingFieldConfigException('CONFIG_FILE');
+    }
+
+    // Only logged in user can alter config.
+    if (is_file($config['config']['CONFIG_FILE']) && !$isLoggedIn) {
+        throw new UnauthorizedConfigException();
+    }
+
+    // Check that all mandatory fields are provided in $config.
+    foreach ($MANDATORY_FIELDS as $field) {
+        if (!isset($config[$field])) {
+            throw new MissingFieldConfigException($field);
+        }
+    }
+
+    $configStr = '<?php '. PHP_EOL;
+    $configStr .= '$GLOBALS[\'login\'] = '.var_export($config['login'], true).';'. PHP_EOL;
+    $configStr .= '$GLOBALS[\'hash\'] = '.var_export($config['hash'], true).';'. PHP_EOL;
+    $configStr .= '$GLOBALS[\'salt\'] = '.var_export($config['salt'], true).'; '. PHP_EOL;
+    $configStr .= '$GLOBALS[\'timezone\'] = '.var_export($config['timezone'], true).';'. PHP_EOL;
+    $configStr .= 'date_default_timezone_set('.var_export($config['timezone'], true).');'. PHP_EOL;
+    $configStr .= '$GLOBALS[\'title\'] = '.var_export($config['title'], true).';'. PHP_EOL;
+    $configStr .= '$GLOBALS[\'titleLink\'] = '.var_export($config['titleLink'], true).'; '. PHP_EOL;
+    $configStr .= '$GLOBALS[\'redirector\'] = '.var_export($config['redirector'], true).'; '. PHP_EOL;
+    $configStr .= '$GLOBALS[\'disablesessionprotection\'] = '.var_export($config['disablesessionprotection'], true).'; '. PHP_EOL;
+    $configStr .= '$GLOBALS[\'privateLinkByDefault\'] = '.var_export($config['privateLinkByDefault'], true).'; '. PHP_EOL;
+
+    // Store all $config['config']
+    foreach ($config['config'] as $key => $value) {
+        $configStr .= '$GLOBALS[\'config\'][\''. $key .'\'] = '.var_export($config['config'][$key], true).';'. PHP_EOL;
+    }
+
+    if (isset($config['plugins'])) {
+        foreach ($config['plugins'] as $key => $value) {
+            $configStr .= '$GLOBALS[\'plugins\'][\''. $key .'\'] = '.var_export($config['plugins'][$key], true).';'. PHP_EOL;
+        }
+    }
+
+    if (!file_put_contents($config['config']['CONFIG_FILE'], $configStr)
+        || strcmp(file_get_contents($config['config']['CONFIG_FILE']), $configStr) != 0
+    ) {
+        throw new Exception(
+            'Shaarli could not create the config file.
+            Please make sure Shaarli has the right to write in the folder is it installed in.'
+        );
+    }
+}
+
+/**
+ * Milestone 0.9 - shaarli/Shaarli#41: options.php is not supported anymore.
+ * ==> if user is loggedIn, merge its content with config.php, then delete options.php.
+ *
+ * @param array $config     contains all configuration fields.
+ * @param bool  $isLoggedIn true if user is logged in.
+ *
+ * @return void
+ */
+function mergeDeprecatedConfig($config, $isLoggedIn)
+{
+    $config_file = $config['config']['CONFIG_FILE'];
+
+    if (is_file($config['config']['DATADIR'].'/options.php') && $isLoggedIn) {
+        include $config['config']['DATADIR'].'/options.php';
+
+        // Load GLOBALS into config
+        foreach ($GLOBALS as $key => $value) {
+            $config[$key] = $value;
+        }
+        $config['config']['CONFIG_FILE'] = $config_file;
+        writeConfig($config, $isLoggedIn);
+
+        unlink($config['config']['DATADIR'].'/options.php');
+    }
+}
+
+/**
+ * Exception used if a mandatory field is missing in given configuration.
+ */
+class MissingFieldConfigException extends Exception
+{
+    public $field;
+
+    /**
+     * Construct exception.
+     *
+     * @param string $field field name missing.
+     */
+    public function __construct($field)
+    {
+        $this->field = $field;
+        $this->message = 'Configuration value is required for '. $this->field;
+    }
+}
+
+/**
+ * Exception used if an unauthorized attempt to edit configuration has been made.
+ */
+class UnauthorizedConfigException extends Exception
+{
+    /**
+     * Construct exception.
+     */
+    public function __construct()
+    {
+        $this->message = 'You are not authorized to alter config.';
+    }
+}