diff options
author | Tom Needham <needham.thomas@gmail.com> | 2012-03-13 23:09:43 +0000 |
---|---|---|
committer | Tom Needham <needham.thomas@gmail.com> | 2012-03-13 23:09:43 +0000 |
commit | a310a81053c31205abd6d62491304705b1f565e2 (patch) | |
tree | 5704bb5a96430b8fed99dd9f5cebf2c14b7b80f9 | |
parent | 5a50144a16fa9b5d8caf9ee261e3c4a39eaa04bc (diff) | |
download | nextcloud-server-a310a81053c31205abd6d62491304705b1f565e2.tar.gz nextcloud-server-a310a81053c31205abd6d62491304705b1f565e2.zip |
move zip creation inside OC_Migrate
-rw-r--r-- | apps/user_migrate/settings.php | 77 | ||||
-rw-r--r-- | lib/migrate.php | 112 | ||||
-rw-r--r-- | lib/migrate/provider.php | 5 |
3 files changed, 111 insertions, 83 deletions
diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 5e8ac9c21df..9fbb4da9e56 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -24,72 +24,27 @@ */ OC_Util::checkAppEnabled('user_migrate'); -define('DS', '/'); - if (isset($_POST['user_export'])) { - - // Setup the export - $zip = new ZipArchive(); - $tmp = get_temp_dir(); - $user = OC_User::getUser(); - - $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . $user; - $filename = $userdatadir . '/owncloud_export_' . $user . '_' . date("y-m-d_H-i-s") . ".zip"; - OC_Log::write('user_migrate',"Creating export file at: " . $filename,OC_Log::INFO); - if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) { - exit("Cannot open <$filename>\n"); - } - - // Migrate the app info - $info = json_encode( OC_Migrate::export( $user ) ); - $infofile = $userdatadir . '/exportinfo.json'; - file_put_contents( $infofile, $info ); - - // Add the data dir (which includes migration.db and exportinfo.json) - zipAddDir( $userdatadir, $zip, true, "/" ); - - // Save the zip - $zip->close(); - - // Send the zip - header("Content-Type: application/zip"); - header("Content-Disposition: attachment; filename=" . basename($filename)); - header("Content-Length: " . filesize($filename)); - @ob_end_clean(); - readfile($filename); - // Cleanup - unlink($filename); - OC_Migrate::cleanUp(); - + // Create the export zip + $user = OC_User::getUser(); + $path = OC_Config::getValue( 'datadirectory' ) . '/' . OC_User::getUser() . '/'; + if( OC_Migrate::createExportFile( $user, $path ) ){ + // Download it then + 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(); + } else { + die('error'); + } } if( isset( $_POST['user_import'] ) ){ // TODO }else { - // fill template - $tmpl = new OC_Template('user_migrate', 'settings'); - return $tmpl->fetchPage(); - + $tmpl = new OC_Template('user_migrate', 'settings'); + return $tmpl->fetchPage(); } -function zipAddDir($dir, $zip, $recursive=true, $internalDir='') { - $dirname = basename($dir); - $zip->addEmptyDir($internalDir . $dirname); - $internalDir.=$dirname.='/'; - if ($dirhandle = opendir($dir)) { - while (false !== ( $file = readdir($dirhandle))) { - - if (( $file != '.' ) && ( $file != '..' )) { - - if (is_dir($dir . '/' . $file) && $recursive) { - zipAddDir($dir . '/' . $file, $zip, $recursive, $internalDir); - } elseif (is_file($dir . '/' . $file)) { - $zip->addFile($dir . '/' . $file, $internalDir . $file); - } - } - } - closedir($dirhandle); - } else { - OC_Log::write('user_migrate',"Was not able to open directory: " . $dir,OC_Log::ERROR); - } -} diff --git a/lib/migrate.php b/lib/migrate.php index acc01ec7bbd..b4c4d635ff3 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -36,6 +36,10 @@ class OC_Migrate{ static private $uid=false; // Path to the sqlite db static private $dbpath=false; + // Holds the ZipArchive object + static private $zip=false; + // String path to export + static private $zippath=false; /** * register a new migration provider @@ -47,24 +51,10 @@ class OC_Migrate{ /** * @breif creates a migration.db in the users data dir with their app data in - * @param @uid string userid of the user to export for * @return bool whether operation was successfull */ - public static function export( $uid ){ - - // Only export database users, otherwise we get chaos - if(!OC_User_Database::userExists( $uid )){ - return false; - } - - self::$uid = $uid; - - if(empty(self::$uid)){ - OC_Log::write('migration','Invalid uid passed',OC_Log::FATAL); - return false; - exit(); - } - + private static function exportAppData( ){ + self::connectDB(); $ok = true; $return = array(); @@ -100,7 +90,7 @@ class OC_Migrate{ // Run the import function? if( !$failed ){ - $return['app'][$provider->id]['success'] = $provider->export( $uid ); + $return['app'][$provider->id]['success'] = $provider->export( self::$uid ); } else { $return['app'][$provider->id]['success'] = false; $return['app'][$provider->id]['message'] = 'failed to create the app tables'; @@ -114,7 +104,7 @@ class OC_Migrate{ // Add some general info to the return array - $return['migrateinfo']['uid'] = $uid; + $return['migrateinfo']['uid'] = self::$uid; $return['migrateinfo']['ocversion'] = OC_Util::getVersionString(); return $return; @@ -122,6 +112,88 @@ class OC_Migrate{ } /** + * @breif creates a zip user export + * @param $uid string user id of the user to export + * @param $path string path to folder to create file in (with trailing slash) + * @return bool success + */ + static public function createExportFile( $uid, $path ){ + // Is a directory + if( !is_dir( $path ) ){ + OC_Log::write('migration', 'Path supplied to createExportFile() 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); + return false; + exit(); + } + // 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(); + } + + self::$uid = $uid; + self::$zip = new ZipArchive; + + // Get some info + $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . self::$uid; + self::$zippath = $path . 'owncloud_export_' . self::$uid . '_' . date("y-m-d_H-i-s") . ".zip"; + if ( self::$zip->open( self::$zippath, ZIPARCHIVE::CREATE ) !== TRUE ) { + OC_Log::write('migration','Cannot create a zip file at: '.self::$zippath, OC_Log::ERROR); + return false; + exit(); + } + + // Export the app info + $info = json_encode( self::exportAppData() ); + file_put_contents( $userdatadir . '/exportinfo.json', $info ); + + // Add the data dir (which includes migration.db and exportinfo.json) + 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; + } + + + } + + /** + * @breif adds a directory to the zip object + * @return void + */ + static private function addDirToZip( $dir, $recursive=true, $internalDir='' ){ + $dirname = basename($dir); + self::$zip->addEmptyDir($internalDir . $dirname); + $internalDir.=$dirname.='/'; + if ($dirhandle = opendir($dir)) { + while (false !== ( $file = readdir($dirhandle))) { + if (( $file != '.' ) && ( $file != '..' )) { + if (is_dir($dir . '/' . $file) && $recursive) { + self::addDirToZip($dir . '/' . $file, $recursive, $internalDir); + } elseif (is_file($dir . '/' . $file)) { + self::$zip->addFile($dir . '/' . $file, $internalDir . $file); + } + } + } + closedir($dirhandle); + } else { + OC_Log::write('migration',"Was not able to open directory: " . $dir,OC_Log::ERROR); + } + } + + /** * @breif returns an array of apps that support migration * @return array */ @@ -143,7 +215,7 @@ class OC_Migrate{ * @param $uid optional uid to use * @return bool if the import succedded */ - public static function import( $db, $migrateinfo, $uid=false ){ + public static function importAppData( $db, $migrateinfo, $uid=false ){ if(!self::$uid){ OC_Log::write('migration','Tried to import without passing a uid',OC_Log::FATAL); @@ -467,6 +539,8 @@ class OC_Migrate{ unlink( $userdatadir . '/migration.db' ); // Remove exportinfo.json unlink( $userdatadir . '/exportinfo.json' ); + // Remove the zip + unlink(self::$zippath); return true; } } diff --git a/lib/migrate/provider.php b/lib/migrate/provider.php index 9c03639b7c3..e2e01b3b5aa 100644 --- a/lib/migrate/provider.php +++ b/lib/migrate/provider.php @@ -20,9 +20,8 @@ abstract class OC_Migrate_Provider{ /** * @breif imports data for the app - * @param $data array of data. eg: array('info'=> APPINFO, 'data'=>APPDATA ARRAY) - * @param $info array of info of the source install + * @param $info array of info including exportinfo.json * @return void */ - abstract function import($data,$uid); + abstract function import( $info ); } |