From 27bf34f7be0e7c3c1516cdf6c44877ee77962dca Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 17 Mar 2012 13:30:58 +0000 Subject: [PATCH] Move user import to the admin --- apps/user_migrate/admin.php | 118 ++++++++++++++++++++++ apps/user_migrate/appinfo/app.php | 8 ++ apps/user_migrate/settings.php | 123 +---------------------- apps/user_migrate/templates/admin.php | 9 ++ apps/user_migrate/templates/settings.php | 11 +- 5 files changed, 137 insertions(+), 132 deletions(-) create mode 100644 apps/user_migrate/admin.php create mode 100644 apps/user_migrate/templates/admin.php diff --git a/apps/user_migrate/admin.php b/apps/user_migrate/admin.php new file mode 100644 index 0000000000..56fe887514 --- /dev/null +++ b/apps/user_migrate/admin.php @@ -0,0 +1,118 @@ +. + * + */ +OC_Util::checkAdminUser(); +OC_Util::checkAppEnabled('user_migrate'); + +// Import? +if (isset($_POST['userimport'])) { + + $root = OC::$SERVERROOT . "/"; + $importname = "owncloud_import_" . date("y-m-d_H-i-s"); + + // Save data dir for later + $datadir = OC_Config::getValue( 'datadirectory' ); + + // Copy the uploaded file + $from = $_FILES['owncloud_import']['tmp_name']; + $to = get_temp_dir().'/'.$importname.'.zip'; + if( !move_uploaded_file( $from, $to ) ){ + OC_Log::write('admin_export',"Failed to copy the uploaded file",OC_Log::INFO); + exit(); + } + + // Extract zip + $zip = new ZipArchive(); + if ($zip->open(get_temp_dir().'/'.$importname.'.zip') != TRUE) { + OC_Log::write('admin_export',"Failed to open zip file",OC_Log::INFO); + exit(); + } + $zip->extractTo(get_temp_dir().'/'.$importname.'/'); + $zip->close(); + + $importdir = get_temp_dir() . '/' . $importname; + + // Delete uploaded file + unlink( $importdir . '.zip' ); + + // Find folder + $files = scandir( $importdir ); + unset($files[0]); + unset($files[1]); + + // Get the user + if( count($files) != 1 ){ + OC_Log::write('migration', 'Invalid import file', OC_Log::ERROR); + die('invalid import'); + } + + $user = reset($files); + + // Check for dbexport.xml and export info and data dir + $files = scandir( $importdir . '/' . $user ); + $required = array( 'migration.db', 'exportinfo.json', 'files'); + foreach($required as $require){ + if( !in_array( $require, $files) ){ + OC_Log::write('migration', 'Invlaid import file', OC_Log::ERROR); + die('invalid import'); + } + } + + $migrateinfo = $importdir . '/' . $user . '/exportinfo.json'; + $migrateinfo = json_decode( file_get_contents( $migrateinfo ) ); + $olduid = $migrateinfo->migrateinfo->uid; + + // Check if uid is available + if( OC_User::UserExists( $olduid ) ){ + OC_Log::write('migration','Username exists', OC_Log::ERROR); + die('user exists'); + } + + // Create the user + if( !OC_Migrate::createUser( $olduid, $migrateinfo->migrateinfo->hash ) ){ + OC_Log::write('migration', 'Failed to create the new user', OC_Log::ERROR); + die('coundlt create new user'); + } + + $datadir = OC_Config::getValue( 'datadirectory' ); + // Copy data + if( !copy_r( $importdir . '/files', $datadir . '/' ) ){ + OC_Log::write('migration','Failed to copy user files to destination', OC_Log::ERROR); + die('failed to copy user files'); + } + + // Import user data + if( !OC_Migrate::importUser( $importdir . '/migration.db', $migrateinfo ) ){ + OC_Log::write('migration','Failed to import user data', OC_Log::ERROR); + die('failed to import user data'); + } + + // All done! + die('done'); + +} else { +// fill template + $tmpl = new OC_Template('user_migrate', 'admin'); + return $tmpl->fetchPage(); +} \ No newline at end of file diff --git a/apps/user_migrate/appinfo/app.php b/apps/user_migrate/appinfo/app.php index 4a795a5474..18b97b93df 100644 --- a/apps/user_migrate/appinfo/app.php +++ b/apps/user_migrate/appinfo/app.php @@ -22,5 +22,13 @@ */ OC_APP::registerPersonal('user_migrate','settings'); +OC_APP::registerAdmin('user_migrate','admin'); +// add settings page to navigation +$entry = array( + 'id' => "user_migrate_settings", + 'order'=>1, + 'href' => OC_Helper::linkTo( "user_migrate", "admin.php" ), + 'name' => 'Import' +); ?> \ No newline at end of file diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 3efe9228a1..62f5e3f20d 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -38,129 +38,8 @@ if (isset($_POST['user_export'])) { readfile($path); unlink( $path ); } -} if( isset( $_POST['user_import'] ) ){ - // TODO - $root = OC::$SERVERROOT . "/"; - $importname = "owncloud_import_" . date("y-m-d_H-i-s"); - - // Save data dir for later - $datadir = OC_Config::getValue( 'datadirectory' ); - - // Copy the uploaded file - $from = $_FILES['owncloud_import']['tmp_name']; - $to = get_temp_dir().'/'.$importname.'.zip'; - if( !move_uploaded_file( $from, $to ) ){ - OC_Log::write('admin_export',"Failed to copy the uploaded file",OC_Log::INFO); - exit(); - } - - // Extract zip - $zip = new ZipArchive(); - if ($zip->open(get_temp_dir().'/'.$importname.'.zip') != TRUE) { - OC_Log::write('admin_export',"Failed to open zip file",OC_Log::INFO); - exit(); - } - $zip->extractTo(get_temp_dir().'/'.$importname.'/'); - $zip->close(); - - $importdir = get_temp_dir() . '/' . $importname; - - // Delete uploaded file - unlink( $importdir . '.zip' ); - - // Find folder - $files = scandir( $importdir ); - unset($files[0]); - unset($files[1]); - - // Get the user - if( count($files) != 1 ){ - OC_Log::write('migration', 'Invalid import file', OC_Log::ERROR); - die('invalid import'); - } - - $user = reset($files); - - // Check for dbexport.xml and export info and data dir - $files = scandir( $importdir . '/' . $user ); - $required = array( 'migration.db', 'exportinfo.json', 'files'); - foreach($required as $require){ - if( !in_array( $require, $files) ){ - OC_Log::write('migration', 'Invlaid import file', OC_Log::ERROR); - die('invalid import'); - } - } - - $migrateinfo = $importdir . '/' . $user . '/exportinfo.json'; - $migrateinfo = json_decode( file_get_contents( $migrateinfo ) ); - $olduid = $migrateinfo->migrateinfo->uid; - - // Check if uid is available - if( OC_User::UserExists( $olduid ) ){ - OC_Log::write('migration','Username exists', OC_Log::ERROR); - die('user exists'); - } - - // Create the user - if( !OC_Migrate::createUser( $olduid, $migrateinfo->migrateinfo->hash ) ){ - OC_Log::write('migration', 'Failed to create the new user', OC_Log::ERROR); - die('coundlt create new user'); - } - - $datadir = OC_Config::getValue( 'datadirectory' ); - // Copy data - if( !copy_r( $importdir . '/files', $datadir . '/' ) ){ - OC_Log::write('migration','Failed to copy user files to destination', OC_Log::ERROR); - die('failed to copy user files'); - } - - // Import user data - if( !OC_Migrate::importUser( $importdir . '/migration.db', $migrateinfo ) ){ - OC_Log::write('migration','Failed to import user data', OC_Log::ERROR); - die('failed to import user data'); - } - - // All done! - die('done'); - } else { // fill template $tmpl = new OC_Template('user_migrate', 'settings'); return $tmpl->fetchPage(); -} - -function copy_r( $path, $dest ) - { - if( is_dir($path) ) - { - @mkdir( $dest ); - $objects = scandir($path); - if( sizeof($objects) > 0 ) - { - foreach( $objects as $file ) - { - if( $file == "." || $file == ".." ) - continue; - // go on - if( is_dir( $path.DS.$file ) ) - { - copy_r( $path.DS.$file, $dest.DS.$file ); - } - else - { - copy( $path.DS.$file, $dest.DS.$file ); - } - } - } - return true; - } - elseif( is_file($path) ) - { - return copy($path, $dest); - } - else - { - return false; - } - } - +} \ No newline at end of file diff --git a/apps/user_migrate/templates/admin.php b/apps/user_migrate/templates/admin.php new file mode 100644 index 0000000000..b5a9951841 --- /dev/null +++ b/apps/user_migrate/templates/admin.php @@ -0,0 +1,9 @@ +
+
+ t('Import user account');?> +

+

+

+ +
+
diff --git a/apps/user_migrate/templates/settings.php b/apps/user_migrate/templates/settings.php index 59a27a926d..389de563a6 100644 --- a/apps/user_migrate/templates/settings.php +++ b/apps/user_migrate/templates/settings.php @@ -5,13 +5,4 @@

- -
-
- t('Import user account');?> -

-

-

- -
-
+ \ No newline at end of file