From c0869887cf91bf17421a9163069584d29ec5ed49 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 7 Apr 2012 21:55:16 +0000 Subject: [PATCH] Return JSON for import and export methods of OC_Migrate --- apps/bookmarks/appinfo/migrate.php | 1 - apps/user_migrate/admin.php | 6 ++--- apps/user_migrate/ajax/export.php | 35 +++++++++++++------------ lib/migrate.php | 42 +++++++++++++++--------------- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/apps/bookmarks/appinfo/migrate.php b/apps/bookmarks/appinfo/migrate.php index 603a8b72d3..c1251e816e 100644 --- a/apps/bookmarks/appinfo/migrate.php +++ b/apps/bookmarks/appinfo/migrate.php @@ -3,7 +3,6 @@ class OC_Migration_Provider_Bookmarks extends OC_Migration_Provider{ // Create the xml for the user supplied function export( ){ - OC_Log::write('migration','starting export for bookmarks',OC_Log::INFO); $options = array( 'table'=>'bookmarks', 'matchcol'=>'user_id', diff --git a/apps/user_migrate/admin.php b/apps/user_migrate/admin.php index 510c54abe3..0160753af2 100644 --- a/apps/user_migrate/admin.php +++ b/apps/user_migrate/admin.php @@ -42,15 +42,15 @@ if (isset($_POST['user_import'])) { $tmpl->assign('error',$error); return $tmpl->fetchPage(); } - - if( !$appsstatus = OC_Migrate::import( $to, 'user' ) ){ + $response = json_decode( OC_Migrate::import( $to, 'user' ) ); + if( !$response->success ){ $error = array('error'=>'There was an error while importing the user!','hint'=>'Please check the logs for a more detailed explaination'); $tmpl = new OC_Template('user_migrate', 'admin'); $tmpl->assign('error',$error); return $tmpl->fetchPage(); } else { // Check import status - foreach( $appsstatus as $app => $status ){ + foreach( $response->data as $app => $status ){ if( $status != 'true' ){ // It failed for some reason if( $status == 'notsupported' ){ diff --git a/apps/user_migrate/ajax/export.php b/apps/user_migrate/ajax/export.php index fac96577fa..86745d6b16 100644 --- a/apps/user_migrate/ajax/export.php +++ b/apps/user_migrate/ajax/export.php @@ -28,28 +28,29 @@ OC_JSON::checkLoggedIn(); OC_Util::checkAppEnabled('user_migrate'); // Which operation if( $_GET['operation']=='create' ){ -$uid = !empty( $_POST['uid'] ) ? $_POST['uid'] : OC_User::getUser(); -if( $uid != OC_User::getUser() ){ - // Needs to be admin to export someone elses account - OC_JSON::error(); + $uid = !empty( $_POST['uid'] ) ? $_POST['uid'] : OC_User::getUser(); + if( $uid != OC_User::getUser() ){ + // Needs to be admin to export someone elses account + OC_JSON::error(); + die(); + } + // Create the export zip + $response = json_decode( OC_Migrate::export( $uid ) ); + if( !$response->success ){ + // Error + OC_JSON::error(); + die(); + } else { + // Save path in session + $_SESSION['ocuserexportpath'] = $response->data; + } + OC_JSON::success(); die(); -} -// Create the export zip -if( !$path = OC_Migrate::export( $uid ) ){ - // Error - OC_JSON::error(); - die(); -} else { - // Save path in session - $_SESSION['ocuserexportpath'] = $path; -} -OC_JSON::success(); -die(); } else if( $_GET['operation']=='download' ){ // Download the export $path = isset( $_SESSION['ocuserexportpath'] ) ? $_SESSION['ocuserexportpath'] : false; if( !$path ){ - die(); + OC_JSON::error(); } header("Content-Type: application/zip"); header("Content-Disposition: attachment; filename=" . basename($path)); diff --git a/lib/migrate.php b/lib/migrate.php index 22c0560691..6734c805fc 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -84,7 +84,7 @@ class OC_Migrate{ $types = array( 'user', 'instance', 'system', 'userfiles' ); if( !in_array( $type, $types ) ){ OC_Log::write( 'migration', 'Invalid export type', OC_Log::ERROR ); - return false; + return json_encode( array( array( 'success' => false ) ) ); } self::$exporttype = $type; // Userid? @@ -93,7 +93,7 @@ class OC_Migrate{ if( !is_null($uid) ){ 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; + return json_encode( array( 'success' => false ) ); } self::$uid = $uid; } else { @@ -114,7 +114,7 @@ class OC_Migrate{ // Validate custom path if( !file_exists( $path ) || !is_writeable( $path ) ){ OC_Log::write( 'migration', 'Path supplied is invalid.', OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } self::$zippath = $path . $zipname; } else { @@ -124,7 +124,7 @@ class OC_Migrate{ } // Create the zip object if( !self::createZip() ){ - return false; + return json_encode( array( 'success' => false ) ); } // Do the export self::findProviders(); @@ -134,7 +134,7 @@ class OC_Migrate{ // Connect to the db self::$dbpath = $datadir . '/' . self::$uid . '/migration.db'; if( !self::connectDB() ){ - return false; + return json_encode( array( 'success' => false ) ); } self::$content = new OC_Migration_Content( self::$zip, self::$MDB2 ); // Export the app info @@ -178,14 +178,14 @@ class OC_Migrate{ break; } if( !$info = self::getExportInfo( $exportdata ) ){ - return false; + return json_encode( array( 'success' => false ) ); } // Add the export info json to the export zip self::$content->addFromString( $info, 'export_info.json' ); if( !self::$content->finish() ){ - return false; + return json_encode( array( 'success' => false ) ); } - return self::$zippath; + return json_encode( array( 'success' => true, 'data' => self::$zippath ) ); } /** @@ -199,19 +199,19 @@ class OC_Migrate{ $datadir = OC_Config::getValue( 'datadirectory' ); // Extract the zip if( !$extractpath = self::extractZip( $path ) ){ - return false; + return json_encode( array( 'success' => false ) ); } // Get export_info.json $scan = scandir( $extractpath ); // Check for export_info.json if( !in_array( 'export_info.json', $scan ) ){ OC_Log::write( 'migration', 'Invalid import file, export_info.json note found', OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } $json = json_decode( file_get_contents( $extractpath . 'export_info.json' ) ); if( $json->exporttype != $type ){ OC_Log::write( 'migration', 'Invalid import file', OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } self::$exporttype = $type; @@ -230,31 +230,31 @@ class OC_Migrate{ // Check user availability if( OC_User::userExists( self::$uid ) ){ OC_Log::write( 'migration', 'User already exists', OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } // Create the user if( !self::createUser( self::$uid, $json->hash ) ){ - return false; + return json_encode( array( 'success' => false ) ); } // Make the new users data dir $path = $datadir . '/' . self::$uid . '/files/'; if( !mkdir( $path, 0755, true ) ){ OC_Log::write( 'migration', 'Failed to create users data dir: '.$path, OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } // Copy data if( !self::copy_r( $extractpath . $json->exporteduser . '/files', $datadir . '/' . self::$uid . '/files' ) ){ - return false; + return json_encode( array( 'success' => false ) ); } // Import user app data if( !$appsimported = self::importAppData( $extractpath . $json->exporteduser . '/migration.db', $json, self::$uid ) ){ - return false; + return json_encode( array( 'success' => false ) ); } // All done! if( !self::unlink_r( $extractpath ) ){ OC_Log::write( 'migration', 'Failed to delete the extracted zip', OC_Log::ERROR ); } - return $appsimported; + return json_encode( array( 'success' => true, 'data' => $appsimported ) ); break; case 'instance': /* @@ -266,21 +266,21 @@ class OC_Migrate{ OC_Log::write( 'migration', "Deleting current data dir", OC_Log::INFO ); if( !self::unlink_r( $datadir, false ) ){ OC_Log::write( 'migration', 'Failed to delete the current data dir', OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } // Copy over data if( !self::copy_r( $extractpath . 'userdata', $datadir ) ){ OC_Log::write( 'migration', 'Failed to copy over data directory', OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } // Import the db if( !OC_DB::replaceDB( $extractpath . 'dbexport.xml' ) ){ - return false; + return json_encode( array( 'success' => false ) ); } // Done - return true; + return json_encode( 'success' => true ); */ break; }