nextcloud/apps/user_migrate/admin.php

111 lines
3.4 KiB
PHP
Raw Normal View History

2012-03-17 17:30:58 +04:00
<?php
/**
2012-03-17 17:53:00 +04:00
* ownCloud - user_migrate
2012-03-17 17:30:58 +04:00
*
* @author Tom Needham
* @copyright 2012 Tom Needham tom@owncloud.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
OC_Util::checkAdminUser();
OC_Util::checkAppEnabled('user_migrate');
// Import?
2012-03-17 17:53:00 +04:00
if (isset($_POST['user_import'])) {
2012-03-17 17:30:58 +04:00
$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 ) ){
2012-03-21 00:19:21 +04:00
OC_Log::write( 'user_migrate', "Failed to copy the uploaded file", OC_Log::ERROR );
2012-03-17 17:30:58 +04:00
exit();
}
2012-03-21 00:19:21 +04:00
OC_Migrate::import( $to, 'user', 'newuser' );
die();
2012-03-17 17:30:58 +04:00
// 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, no user included');
2012-03-17 17:30:58 +04:00
}
$olduser = reset($files);
2012-03-17 17:30:58 +04:00
// Check for dbexport.xml and export info and data dir
$files = scandir( $importdir . '/' . $olduser );
$required = array( 'migration.db', 'export_info.json', 'files');
2012-03-17 17:30:58 +04:00
foreach($required as $require){
if( !in_array( $require, $files) ){
OC_Log::write('migration', 'Invlaid import file', OC_Log::ERROR);
die('invalid import');
}
}
$migrateinfo = $importdir . '/' . $olduser . '/export_info.json';
2012-03-17 17:30:58 +04:00
$migrateinfo = json_decode( file_get_contents( $migrateinfo ) );
// Check if uid is available
if( OC_User::UserExists( $olduser ) ){
2012-03-17 17:30:58 +04:00
OC_Log::write('migration','Username exists', OC_Log::ERROR);
die('user exists');
}
// Create the user
if( !OC_Migrate::createUser( $olduser, $migrateinfo->hash ) ){
2012-03-17 17:30:58 +04:00
OC_Log::write('migration', 'Failed to create the new user', OC_Log::ERROR);
die('coundlt create new user');
}
$datadir = OC_Config::getValue( 'datadirectory' );
// Make the new users data dir
$path = $datadir . '/' . $olduser . '/files/';
if( !mkdir( $path, 0755, true ) ){
OC_Log::write('migration','Failed to create users data dir: '.$path, OC_Log::ERROR);
die('failed to create users data dir');
}
2012-03-17 17:30:58 +04:00
// Copy data
if( !copy_r( $importdir . '/' . $olduser . '/files', $datadir . '/' . $olduser . '/files' ) ){
2012-03-17 17:30:58 +04:00
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::importAppData( $importdir . '/' . $olduser . '/migration.db', $migrateinfo ) ){
2012-03-17 17:30:58 +04:00
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();
}