summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Needham <needham.thomas@gmail.com>2012-03-16 21:09:36 +0000
committerTom Needham <needham.thomas@gmail.com>2012-03-16 21:09:36 +0000
commitc442a06a0217afa8ff284333b0560aeb87db7a55 (patch)
tree58008384e36a52f90d3dabc8890c54c83b025574
parent50233d075c47c86a5a26d4f946f8aa09f703cb15 (diff)
downloadnextcloud-server-c442a06a0217afa8ff284333b0560aeb87db7a55.tar.gz
nextcloud-server-c442a06a0217afa8ff284333b0560aeb87db7a55.zip
Fix export for admin and users. Added 3 admin export types
-rw-r--r--apps/admin_export/settings.php9
-rw-r--r--apps/admin_export/templates/settings.php2
-rw-r--r--apps/user_migrate/settings.php2
-rw-r--r--lib/migrate.php144
4 files changed, 90 insertions, 67 deletions
diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php
index 9db1d75db96..33fca26630a 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 15a19b7c633..6d848048c45 100644
--- a/apps/admin_export/templates/settings.php
+++ b/apps/admin_export/templates/settings.php
@@ -8,7 +8,7 @@
<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="radio" name="export_type" value="userfiles" /> Just user files<br />
<input type="submit" name="admin_export" value="<?php echo $l->t('Export'); ?>" />
</fieldset>
</form>
diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php
index d862ac5a82a..2d200c0f769 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 8f26ea7ae68..522d8da843c 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 = "<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);
+ $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 = "<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 );
+ 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;
@@ -199,20 +202,56 @@ 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
*/
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)
* @param optional $path string path to folder to create file in (with trailing slash) (defaults to current user's data dir)
@@ -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;
- }
+
}