]> source.dussan.org Git - nextcloud-server.git/commitdiff
added `writeConfiguration()` method to OC_CONFIG
authorAldo "xoen" Giambelluca <xoen@xoen.org>
Sun, 11 Jul 2010 20:44:48 +0000 (22:44 +0200)
committerAldo "xoen" Giambelluca <xoen@xoen.org>
Sun, 11 Jul 2010 20:44:48 +0000 (22:44 +0200)
This actually write all the key/value into the config.php.
The code in `writeAdminLisener()` is cleaner.
This is in prevision of new configuration options (e.g. plugin related)

inc/lib_config.php [changed mode: 0755->0644]

old mode 100755 (executable)
new mode 100644 (file)
index 5058f38..af8f030
@@ -109,7 +109,32 @@ class OC_CONFIG{
                        return false;
                }
        }
-       
+
+       /**
+        * Write the configuration to the `config.php` file
+        *
+        * $configuration contains key/value
+        *   - the key is the option name without the 'CONFIG_' prefix
+        *   - the value is a string or a boolean
+        *
+        * @param array $configuration is an associarive array
+        */
+       protected static function saveConfiguration($configuration) {
+               global $SERVERROOT;
+               global $WEBROOT;
+
+               $configContent = '<?php';
+               foreach ( $configuration as $key => $value ) {
+                       if ( is_string($value) ) {
+                               $configContent .= "\n\$CONFIG_$key = '$value';";
+                       } else if ( is_bool($value) ) {
+                               $value = $value ? 'true' : 'false';
+                               $configContent .= "\n\$CONFIG_$key = $value;";
+                       }
+               }
+               $filename = "$SERVERROOT/config/config.php";
+               file_put_contents($filename, $configContent);
+       }
        
        /**
        * lisen for admin configuration changes and write it to the file
@@ -234,42 +259,40 @@ class OC_CONFIG{
                                                        $error.='error while trying to add the admin user to the admin group<br/>';
                                                }
                                        }
-                                       //storedata
-                                       $config='<?php '."\n";
-                                       $config.='$CONFIG_INSTALLED=true;'."\n";
-                                       $config.='$CONFIG_DATADIRECTORY=\''.$_POST['datadirectory']."';\n";
-                                       if(isset($_POST['forcessl'])) $config.='$CONFIG_HTTPFORCESSL=true'.";\n"; else $config.='$CONFIG_HTTPFORCESSL=false'.";\n";
-                                       if(isset($_POST['enablebackup'])) $config.='$CONFIG_ENABLEBACKUP=true'.";\n"; else $config.='$CONFIG_ENABLEBACKUP=false'.";\n";
-                                       if(isset($_POST['enablebackup']) and $_POST['enablebackup']==1){
-                                               $config.='$CONFIG_BACKUPDIRECTORY=\''.$_POST['backupdirectory']."';\n";
-                                       }
-                                       $config.='$CONFIG_DATEFORMAT=\''.$_POST['dateformat']."';\n";
-                                       $config.='$CONFIG_DBTYPE=\''.$dbtype."';\n";
-                                       $config.='$CONFIG_DBNAME=\''.$_POST['dbname']."';\n";
-                                       $config.='$CONFIG_DBTABLEPREFIX=\''.$_POST['dbtableprefix']."';\n";
-                                       if($dbtype!='sqlite'){
-                                               $config.='$CONFIG_DBHOST=\''.$_POST['dbhost']."';\n";
-                                               $config.='$CONFIG_DBUSER=\''.$_POST['dbuser']."';\n";
-                                               $config.='$CONFIG_DBPASSWORD=\''.$_POST['dbpassword']."';\n";
+                                       // Build the configuration array
+                                       $config = array();
+                                       $config['INSTALLED'] = true;
+                                       $config['DATADIRECTORY'] = $_POST['datadirectory'];
+                                       $config['HTTPFORCESSL'] = isset($_POST['forcessl']);
+                                       // Backup configuration
+                                       $config['ENABLEBACKUP'] = isset($_POST['enablebackup']);
+                                       if ( $config['ENABLEBACKUP'] AND (1 == $_POST['enablebackup']) )
+                                               $config['BACKUPDIRECTORY'] = $_POST['backupdirectory'];
+                                       $config['DATEFORMAT'] = $_POST['dateformat'];
+                                       // DB Configuration
+                                       $config['DBTYPE'] = $dbtype;
+                                       $config['DBNAME'] = $_POST['dbname'];
+                                       $config['DBTABLEPREFIX'] = $_POST['dbtableprefix'];
+                                       if ( 'sqlite' != $dbtype ) {
+                                               $config['DBHOST'] = $_POST['dbhost'];
+                                               $config['DBUSER'] = $_POST['dbuser'];
+                                               $config['DBPASSWORD'] = $_POST['dbpassword'];
                                        }
-                                       $config.='?> ';
 
-                                       $filename=$SERVERROOT.'/config/config.php';
-                                       if(empty($error)){
-                                               header("Location: ".$WEBROOT."/");
-                                               try{
-                                                       file_put_contents($filename,$config);
-                                               }catch(Exception $e){
+                                       if( empty($error) ) {
+                                               header("Location: $WEBROOT/");
+                                               try {
+                                                       // Write the configuration array to `/config/config.php`
+                                                       OC_CONFIG::saveConfiguration($config);
+                                               } catch ( Exception $e ) {
                                                        $error.='error while trying to save the configuration file<br/>';
                                                        return $error;
                                                }
-                                       }else{
+                                       } else {
                                                return $error;
                                        }
-
                                }
                                return($error);
-
                        }
                }
        }