diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index 9db1d75db9..33fca26630 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -40,12 +40,12 @@ if (isset($_POST['admin_export'])) { header("Content-Disposition: attachment; filename=" . basename($path)); header("Content-Length: " . filesize($path)); @ob_end_clean(); - readfile($path); - OC_Migrate::cleanUp( $path ); + readfile( $path ); + unlink( $path ); } // Import? } else if( isset($_POST['admin_import']) ){ - + /* $root = OC::$SERVERROOT . "/"; $importname = "owncloud_import_" . date("y-m-d_H-i-s"); @@ -85,7 +85,8 @@ if (isset($_POST['admin_export'])) { exit(); } - OC_DB::replaceDB( get_temp_dir() . '/' . $importname . '/dbexport.xml' ); + OC_DB::replaceDB( get_temp_dir() . '/' . $importname . '/dbexport.xml' ); + */ } else { // fill template $tmpl = new OC_Template('admin_export', 'settings'); diff --git a/apps/admin_export/templates/settings.php b/apps/admin_export/templates/settings.php index 15a19b7c63..6d848048c4 100644 --- a/apps/admin_export/templates/settings.php +++ b/apps/admin_export/templates/settings.php @@ -8,7 +8,7 @@

ownCloud instance ( suitable for import )
ownCloud system files
- Just user files + Just user files
diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index d862ac5a82..2d200c0f76 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -36,7 +36,7 @@ if (isset($_POST['user_export'])) { header("Content-Length: " . filesize($path)); @ob_end_clean(); readfile($path); - OC_Migrate::cleanUp( $path ); + unlink( $path ); } } if( isset( $_POST['user_import'] ) ){ // TODO diff --git a/lib/migrate.php b/lib/migrate.php index 8f26ea7ae6..522d8da843 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -40,6 +40,10 @@ class OC_Migrate{ static private $zip=false; // String path to export static private $zippath=false; + // Stores the type of export + static private $exporttype=false; + // Array of temp files to be deleted after zip creation + static private $tmpfiles=array(); /** * register a new migration provider @@ -138,14 +142,7 @@ class OC_Migrate{ 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; + self::$zippath = sys_get_temp_dir() . '/' . $zipname; } // Create the zip object self::$zip = new ZipArchive; @@ -154,42 +151,48 @@ class OC_Migrate{ 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); + $exporttypes = array( 'userfiles', 'instance', 'system' ); + self::$exporttype = in_array( $exporttype, $exporttypes ) ? $exporttype : false; + if( !self::$exporttype ){ + OC_Log::write( 'migration', 'Export type: '.$exporttype.' is not supported.', OC_Log::ERROR); return false; } + switch( self::$exporttype ){ + case 'instance': + // Creates a zip that is compatable with the import function + $dbfile = tempnam( "/tmp", "owncloud_export_data_" ); + OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL'); + + // Now add in *dbname* and *dbprefix* + $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 ); + self::$zip->addFile( $dbfile, "dbexport.xml" ); + // Add user data + foreach(OC_User::getUsers() as $user){ + self::addDirToZip( $datadir . '/' . $user . '/', true, "/userdata/" ); + } + break; + case 'userfiles': + // Creates a zip with all of the users files + foreach(OC_User::getUsers() as $user){ + self::addDirToZip( $datadir . '/' . $user . '/', true, "/" ); + } + break; + case '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, "/"); + } + break; + } + // Add export info + self::addExportInfo(); // Close the zip if( !self::closeZip() ){ return false; @@ -198,6 +201,30 @@ class OC_Migrate{ } + /** + * @breif adds a json file with infomation on the export to the zips root (used on import) + * @return bool + */ + static private function addExportInfo(){ + $info = array( + 'ocversion' => OC_Util::getVersion(), + 'exporttime' => time(), + 'exportedby' => OC_User::getUser(), + 'exporttype' => self::$exporttype + ); + // Create json + $json = json_encode( $info ); + $tmpfile = tempnam("/tmp", "oc_export_info_"); + self::$tmpfiles[] = $tmpfile; + if( !file_put_contents( $tmpfile, $json ) ){ + return false; + } else { + self::$zip->addFile( $tmpfile, "export_info.json" ); + return true; + } + } + + /** * @breif tried to finalise the zip * @return bool @@ -205,13 +232,25 @@ class OC_Migrate{ 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); + self::cleanup(); return false; } else { - OC_Log::write('migration', 'Created export file for: '.self::$uid, OC_Log::INFO); + OC_Log::write('migration', 'Export zip created ok', OC_Log::INFO); + self::cleanup(); return true; } } + /** + * @breif cleans up after the zip + */ + static private function cleanup(){ + // Delete tmp files + foreach(self::$tmpfiles as $i){ + unlink( $i ); + } + } + /** * @breif creates a zip user export * @param optional $uid string user id of the user to export (defaults to current) @@ -647,22 +686,5 @@ class OC_Migrate{ return $result ? true : false; } - - /** - * @breif removes migration.db and exportinfo.json from the users data dir - * @param optional $path string path to the export zip to delete - * @return void - */ - static public function cleanUp( $path=null ){ - $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . self::$uid; - // Remove migration.db - unlink( $userdatadir . '/migration.db' ); - // Remove exportinfo.json - unlink( $userdatadir . '/exportinfo.json' ); - // Remove the zip - if( !is_null( $path ) ){ - unlink( $path ); - } - return true; - } + }