summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Needham <needham.thomas@gmail.com>2012-03-16 22:54:37 +0000
committerTom Needham <needham.thomas@gmail.com>2012-03-16 22:54:37 +0000
commit222bb2303fb0825b4e279024ff723db84e23153d (patch)
tree2bad45ca5f3a4f6e0ca56f109eb1e7e34515346b
parent5332c319a2140563478d83047d9f717c0d3e179f (diff)
downloadnextcloud-server-222bb2303fb0825b4e279024ff723db84e23153d.tar.gz
nextcloud-server-222bb2303fb0825b4e279024ff723db84e23153d.zip
Added prototype of user import
-rw-r--r--apps/admin_export/settings.php4
-rw-r--r--apps/admin_export/templates/settings.php2
-rw-r--r--apps/user_migrate/settings.php117
3 files changed, 120 insertions, 3 deletions
diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php
index 33fca26630a..e7de74f7588 100644
--- a/apps/admin_export/settings.php
+++ b/apps/admin_export/settings.php
@@ -45,7 +45,7 @@ if (isset($_POST['admin_export'])) {
}
// Import?
} else if( isset($_POST['admin_import']) ){
- /*
+
$root = OC::$SERVERROOT . "/";
$importname = "owncloud_import_" . date("y-m-d_H-i-s");
@@ -86,7 +86,7 @@ if (isset($_POST['admin_export'])) {
}
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 6d848048c45..36eec84d489 100644
--- a/apps/admin_export/templates/settings.php
+++ b/apps/admin_export/templates/settings.php
@@ -14,7 +14,7 @@
</form>
<form id="import" action="#" method="post" enctype="multipart/form-data">
<fieldset class="personalblock">
- <legend><strong><?php echo $l->t('Import an ownCloud instance THIS WILL DELETE ALL CURRENT OWNCLOUD DATA');?></strong></legend>
+ <legend><strong><?php echo $l->t('Import an ownCloud instance. THIS WILL DELETE ALL CURRENT OWNCLOUD DATA');?></strong></legend>
<p><?php echo $l->t('All current ownCloud data will be replaced by the ownCloud instance that is uploaded.');?>
</p>
<p><input type="file" id="owncloud_import" name="owncloud_import"><label for="owncloud_import"><?php echo $l->t('ownCloud Export Zip File');?></label>
diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php
index 2d200c0f769..3efe9228a13 100644
--- a/apps/user_migrate/settings.php
+++ b/apps/user_migrate/settings.php
@@ -40,10 +40,127 @@ if (isset($_POST['user_export'])) {
}
} 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;
+ }
+ }