From 50233d075c47c86a5a26d4f946f8aa09f703cb15 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Thu, 15 Mar 2012 20:52:43 +0000 Subject: [PATCH] Improve admin_export ui and move system export cde to OC_Migrate --- apps/admin_export/settings.php | 78 +++--------- apps/admin_export/templates/settings.php | 11 +- apps/user_migrate/settings.php | 2 +- lib/migrate.php | 150 +++++++++++++++++++---- 4 files changed, 151 insertions(+), 90 deletions(-) diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index 73a4209d3f..9db1d75db9 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -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 = "\n\n " . OC_Config::getValue( "dbname", "owncloud" ); - $dbtableprefixstring = "\n\n " . OC_Config::getValue( "dbtableprefix", "_oc" ); - - $dbexport = str_replace( $dbnamestring, "\n\n *dbname*", $dbexport ); - $dbexport = str_replace( $dbtableprefixstring, "
\n\n *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 . "/"; diff --git a/apps/admin_export/templates/settings.php b/apps/admin_export/templates/settings.php index 9f0845bf55..15a19b7c63 100644 --- a/apps/admin_export/templates/settings.php +++ b/apps/admin_export/templates/settings.php @@ -2,12 +2,13 @@
t('Export this ownCloud instance');?>

t('This will create a compressed file that contains the data of this owncloud instance. - Please choose which components should be included:');?> -

-


-
- + Please choose the export type:');?>

+

What would you like to export?

+

+ ownCloud instance ( suitable for import )
+ ownCloud system files
+ Just user files

diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 04aca51f51..d862ac5a82 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -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 { diff --git a/lib/migrate.php b/lib/migrate.php index 728f15e1f6..8f26ea7ae6 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -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 = "\n\n " . OC_Config::getValue( "dbname", "owncloud" ); + $dbtableprefixstring = "
\n\n " . OC_Config::getValue( "dbtableprefix", "_oc" ); + + $dbexport = str_replace( $dbnamestring, "\n\n *dbname*", $dbexport ); + $dbexport = str_replace( $dbtableprefixstring, "
\n\n *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