]> source.dussan.org Git - nextcloud-server.git/commitdiff
Improve admin_export ui and move system export cde to OC_Migrate
authorTom Needham <needham.thomas@gmail.com>
Thu, 15 Mar 2012 20:52:43 +0000 (20:52 +0000)
committerTom Needham <needham.thomas@gmail.com>
Thu, 15 Mar 2012 20:52:43 +0000 (20:52 +0000)
apps/admin_export/settings.php
apps/admin_export/templates/settings.php
apps/user_migrate/settings.php
lib/migrate.php

index 73a4209d3f847e60ec832a8d1ce001c948104f3e..9db1d75db9612a0e5ff42b3779247769870a837e 100644 (file)
@@ -28,70 +28,22 @@ OC_Util::checkAppEnabled('admin_export');
 define('DS', '/');
 
 
-
+// Export?
 if (isset($_POST['admin_export'])) {
-    $root = OC::$SERVERROOT . "/";
-    $datadir = OC_Config::getValue( 'datadirectory'  );
-    $zip = new ZipArchive();
-    $tempdir = get_temp_dir();
-    $filename = $tempdir . "/owncloud_export_" . date("y-m-d_H-i-s") . ".zip";
-    OC_Log::write('admin_export',"Creating export file at: " . $filename,OC_Log::INFO);
-    if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
-               exit("Cannot open <$filename>\n");
-    }
-
-    if (isset($_POST['owncloud_system'])) {
-               // adding owncloud system files
-               OC_Log::write('admin_export',"Adding owncloud system files to export",OC_Log::INFO);
-               zipAddDir($root, $zip, false);
-               foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dirname) {
-                   zipAddDir($root . $dirname, $zip, true, "/");
-               }
-    }
-
-    if (isset($_POST['owncloud_config'])) {
-       // adding owncloud config
-       // todo: add database export
-       $dbfile = $tempdir . "/dbexport.xml";
-       OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL');
-       
-       // Now add in *dbname* and *dbtableprefix*
-       $dbexport = file_get_contents( $dbfile );
-       
-       $dbnamestring = "<database>\n\n <name>" . OC_Config::getValue( "dbname", "owncloud" );
-       $dbtableprefixstring = "<table>\n\n  <name>" . OC_Config::getValue( "dbtableprefix", "_oc" );
-       
-       $dbexport = str_replace( $dbnamestring, "<database>\n\n <name>*dbname*", $dbexport );
-       $dbexport = str_replace( $dbtableprefixstring, "<table>\n\n  <name>*dbprefix*", $dbexport );
-       
-       // Write the new db export file
-       file_put_contents( $dbfile, $dbexport );
-       
-       $zip->addFile($dbfile, "dbexport.xml");
-
-       OC_Log::write('admin_export',"Adding owncloud config to export",OC_Log::INFO);
-       zipAddDir($root . "config/", $zip, true, "/");
-    }
-
-    if (isset($_POST['user_files'])) {
-           // needs to handle data outside of the default data dir.
-               // adding user files
-               $zip->addFile($root . '/data/.htaccess', "data/.htaccess");
-               $zip->addFile($root . '/data/index.html', "data/index.html");
-               foreach (OC_User::getUsers() as $i) {
-                       OC_Log::write('admin_export',"Adding owncloud user files of $i to export",OC_Log::INFO);
-                   zipAddDir($datadir . '/' . $i, $zip, true, "/data/");
-               }
-    }
-
-    $zip->close();
-    header("Content-Type: application/zip");
-    header("Content-Disposition: attachment; filename=" . basename($filename));
-    header("Content-Length: " . filesize($filename));
-    @ob_end_clean();
-    readfile($filename);
-    unlink($filename);
-    unlink($dbfile);
+       // Create the export zip
+       if( !$path = OC_Migrate::createSysExportFile( $_POST['export_type'] ) ){
+               // Error
+               die('error');   
+       } else {
+               // Download it
+               header("Content-Type: application/zip");
+               header("Content-Disposition: attachment; filename=" . basename($path));
+               header("Content-Length: " . filesize($path));
+               @ob_end_clean();
+               readfile($path);
+               OC_Migrate::cleanUp( $path );   
+       }
+// Import?
 } else if( isset($_POST['admin_import']) ){
        
        $root = OC::$SERVERROOT . "/";
index 9f0845bf55f233198b91bc90d9d5854e5de24e2a..15a19b7c633d9a31e828b7afd1919fcb686cbe5d 100644 (file)
@@ -2,12 +2,13 @@
     <fieldset class="personalblock">
         <legend><strong><?php echo $l->t('Export this ownCloud instance');?></strong></legend>
         <p><?php echo $l->t('This will create a compressed file that contains the data of this owncloud instance.
-            Please choose which components should be included:');?>
-        </p>
-        <p><input type="checkbox" id="user_files" name="user_files" value="true"><label for="user_files"><?php echo $l->t('User files');?></label><br/>
-            <input type="checkbox" id="owncloud_system" name="owncloud_system" value="true"><label for="owncloud_system"><?php echo $l->t('ownCloud system files');?></label><br/>
-            <input type="checkbox" id="owncloud_config" name="owncloud_config" value="true"><label for="owncloud_config"><?php echo $l->t('ownCloud configuration');?></label>
+            Please choose the export type:');?>
         </p>
+        <h3>What would you like to export?</h3>
+        <p>
+        <input type="radio" name="export_type" value="instance" /> ownCloud instance ( suitable for import )<br />
+               <input type="radio" name="export_type" value="system" /> ownCloud system files<br />
+               <input type="radio" name="export_type" value="userfiles" /> Just user files
         <input type="submit" name="admin_export" value="<?php echo $l->t('Export'); ?>" />
     </fieldset>
 </form>
index 04aca51f5185dce3c90428138ad44779f797c97b..d862ac5a82a44c98e496d2ef07348802a85b76f1 100644 (file)
@@ -26,7 +26,7 @@ OC_Util::checkAppEnabled('user_migrate');
 
 if (isset($_POST['user_export'])) {
        // Create the export zip
-       if( !$path = OC_Migrate::createExportFile() ){
+       if( !$path = OC_Migrate::createUserExportFile() ){
                // Error
                die('error');   
        } else {
index 728f15e1f6d9e19bf54174009d01faeb285db7d8..8f26ea7ae68c11b263ea3c244f76f6714dbd09c0 100644 (file)
@@ -111,20 +111,120 @@ class OC_Migrate{
                
        }
        
+       /**
+       * @breif creates an export file for the whole system
+       * @param optional $exporttype string export type ('instance','system' or 'userfiles')
+       * @param optional $path string path to zip destination (with trailing slash)
+       * @return path to the zip or false if there was a problem
+       */
+       static public function createSysExportFile( $exporttype='instance', $path=null ){
+               // Calculate zip name
+               $zipname = "owncloud_export_" . date("y-m-d_H-i-s") . ".zip";
+               // Get the data dir
+               $datadir = OC_Config::getValue( 'datadirectory' );
+               // Calculate destination
+               if( !is_null( $path ) ){
+                       // Path given 
+                       // Is a directory?
+                       if( !is_dir( $path ) ){
+                               OC_Log::write('migration', 'Path supplied to createSysExportFile() is not a directory', OC_Log::ERROR);
+                               return false;
+                       }       
+                       // Is writeable
+                       if( !is_writeable( $path ) ){
+                               OC_Log::write('migration', 'Path supplied to createSysExportFile() is not writeable', OC_Log::ERROR);   
+                               return false;
+                       }
+                       self::$zippath = $path . $zipname;
+               } else {
+                       // Save in tmp dir
+                       $structure = sys_get_temp_dir() . '/owncloudexports/';
+                       if( !file_exists( $structure ) ){
+                               if ( !mkdir( $structure ) ) {
+                                       OC_Log::write('migration', 'Could not create the temporary export at: '.$structure, OC_Log::ERROR);
+                                       return false;
+                               }
+                       }
+                       self::$zippath = $structure . $zipname;
+               }       
+               // Create the zip object
+               self::$zip = new ZipArchive;
+               // Try to create the zip
+           if( !self::createZip() ){
+               return false;
+           }
+               // Handle export types
+               if( $exporttype == 'instance' ){
+                       // Creates a zip that is compatable with the import function
+                       /*      
+                       $dbfile = self:: . "/dbexport.xml";
+                       OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL');
+                       
+                       // Now add in *dbname* and *dbtableprefix*
+                       $dbexport = file_get_contents( $dbfile );
+                       
+                       $dbnamestring = "<database>\n\n <name>" . OC_Config::getValue( "dbname", "owncloud" );
+                       $dbtableprefixstring = "<table>\n\n  <name>" . OC_Config::getValue( "dbtableprefix", "_oc" );
+                       
+                       $dbexport = str_replace( $dbnamestring, "<database>\n\n <name>*dbname*", $dbexport );
+                       $dbexport = str_replace( $dbtableprefixstring, "<table>\n\n  <name>*dbprefix*", $dbexport );
+                       
+                       // Write the new db export file
+                       file_put_contents( $dbfile, $dbexport );
+                       
+                       $zip->addFile($dbfile, "dbexport.xml");
+                       */
+               } else if( $exporttype == 'system' ){
+                       // Creates a zip with the owncloud system files
+                       self::addDirToZip( OC::$SERVERROOT . '/', false);
+                       foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dir) {
+                       self::addDirToZip( OC::$SERVERROOT . '/' . $dir, true, "/");
+                       }
+               } else if ( $exporttype == 'userfiles' ){
+                       // Creates a zip with all of the users files
+                       foreach(OC_User::getUsers() as $user){
+                               self::addDirToZip( $datadir . '/' . $user . '/', true, "/" . $user);    
+                       }
+               } else {
+                       // Invalid export type supplied
+                       OC_Log::write('migration', 'Invalid export type supplied to createSysExportFile() "'.$exporttype.'"', OC_Log::ERROR);
+                       return false;   
+               }
+               // Close the zip
+               if( !self::closeZip() ){
+                       return false;   
+               }
+               return self::$zippath;
+               
+       }
+       
+       /**
+       * @breif tried to finalise the zip
+       * @return bool
+       */
+       static private function closeZip(){
+               if( !self::$zip->close() ){
+                       OC_Log::write('migration', 'Failed to save the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR);
+                       return false;
+               } else {
+                       OC_Log::write('migration', 'Created export file for: '.self::$uid, OC_Log::INFO);
+                       return true;    
+               }       
+       }
+       
        /**
        * @breif creates a zip user export
        * @param optional $uid string user id of the user to export (defaults to current)
        * @param optional $path string path to folder to create file in (with trailing slash) (defaults to current user's data dir)
        * @return false on failure | string path on success
        */
-       static public function createExportFile( $uid=null, $path=null ){
+       static public function createUserExportFile( $uid=null, $path=null ){
                // User passed?
                $uid = is_null( $uid ) ? OC_User::getUser() : $uid ;
                // Is a database user?
                if( !OC_User_Database::userExists( $uid ) ){
                        OC_Log::write('migration', 'User: '.$uid.' is not in the database and so cannot be exported.', OC_Log::ERROR);
                        return false;   
-                       exit();
                }
                // Set the uid
                self::$uid = $uid;
@@ -140,41 +240,53 @@ class OC_Migrate{
                        // Path given 
                        // Is a directory?
                        if( !is_dir( $path ) ){
-                               OC_Log::write('migration', 'Path supplied to createExportFile() is not a directory', OC_Log::ERROR);
+                               OC_Log::write('migration', 'Path supplied to createUserExportFile() is not a directory', OC_Log::ERROR);
                                return false;
-                               exit(); 
                        }       
                        // Is writeable
                        if( !is_writeable( $path ) ){
-                               OC_Log::write('migration', 'Path supplied to createExportFile() is not writeable', OC_Log::ERROR);      
+                               OC_Log::write('migration', 'Path supplied to createUserExportFile() is not writeable', OC_Log::ERROR);  
                                return false;
-                               exit();
                        }
                        self::$zippath = $path . $zipname;
                } else {
                        // Save in users data dir
                        self::$zippath = $userdatadir . $zipname;
                }
-           if (self::$zip->open(self::$zippath, ZIPARCHIVE::CREATE) !== TRUE) {
-                       // TODO ADD LOGGING
-                       exit("Cannot open <$filename>\n");
+               // Try to create the zip
+           if( !self::createZip() ){
+               return false;
            }
            // Export the app info
                $info = json_encode( self::exportAppData() );
                file_put_contents( $userdatadir . '/exportinfo.json', $info );
                // Add the data dir to the zip
                self::addDirToZip( $userdatadir );
-           // All done!
-               if( !self::$zip->close() ){
-                       OC_Log::write('migration', 'Failed to save the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR);
-                       return false;
-                       exit();
-               } else {
-                       OC_Log::write('migration', 'Created export file for: '.self::$uid, OC_Log::INFO);
-                       //return true;  
+           // Close the zip
+               if( !self::closeZip() ){
+                       return false;   
                }
+               // All good
            return self::$zippath;
        } 
+       
+       /**
+       * @breif tries to create the zip
+       * @return bool
+       */
+       static private function createZip(){
+               // Check if properties are set
+               if( !self::$zip || !self::$zippath ){
+                       OC_Log::write('migration', 'createZip() called but $zip and/or $zippath have not been set', OC_Log::ERROR);
+                       return false;   
+               }
+               if ( self::$zip->open( self::$zippath, ZIPARCHIVE::CREATE ) !== TRUE ) {
+                       OC_Log::write('migration', 'Failed to create the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR);
+                       return false;
+           } else {
+               return true;    
+           }   
+       }
                
        /**
        * @breif adds a directory to the zip object
@@ -235,7 +347,6 @@ class OC_Migrate{
                if(!self::$uid){
                        OC_Log::write('migration','Tried to import without passing a uid',OC_Log::FATAL);
                        return false;
-                       exit(); 
                }
                
                // Check if the db exists
@@ -243,18 +354,15 @@ class OC_Migrate{
                        // Connect to the db
                        if(!self::connectDB( $db )){
                                return false;
-                               exit(); 
                        }       
                } else {
                        OC_Log::write('migration','Migration.db not found at: '.$db, OC_Log::FATAL );   
                        return false;
-                       exit();
                }
                
                if( !is_array( $migrateinfo ) ){
                        OC_Log::write('migration','$migrateinfo is not an array', OC_Log::FATAL);
                        return false;
-                       exit();
                }
                
                // Set the user id