2012-02-03 20:32:06 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-03-19 20:44:20 +00:00
|
|
|
* provides an interface to migrate users and whole ownclouds
|
2012-02-03 20:32:06 +00:00
|
|
|
*/
|
|
|
|
class OC_Migrate{
|
2012-04-14 12:31:48 +00:00
|
|
|
|
|
|
|
|
2012-03-13 16:21:17 +00:00
|
|
|
// Array of OC_Migration_Provider objects
|
2012-02-03 21:00:12 +00:00
|
|
|
static private $providers=array();
|
2012-03-13 16:21:17 +00:00
|
|
|
// User id of the user to import/export
|
2012-03-10 18:18:58 +00:00
|
|
|
static private $uid=false;
|
2012-03-13 23:09:43 +00:00
|
|
|
// Holds the ZipArchive object
|
|
|
|
static private $zip=false;
|
2012-03-16 21:09:36 +00:00
|
|
|
// Stores the type of export
|
|
|
|
static private $exporttype=false;
|
2012-03-19 20:44:20 +00:00
|
|
|
// Holds the db object
|
2013-11-22 14:53:56 +00:00
|
|
|
static private $migration_database=false;
|
2012-03-19 20:44:20 +00:00
|
|
|
// Path to the sqlite db
|
|
|
|
static private $dbpath=false;
|
|
|
|
// Holds the path to the zip file
|
|
|
|
static private $zippath=false;
|
|
|
|
// Holds the OC_Migration_Content object
|
|
|
|
static private $content=false;
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-02-03 20:32:06 +00:00
|
|
|
/**
|
|
|
|
* register a new migration provider
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param OC_Migration_Provider $provider
|
2012-02-03 20:32:06 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function registerProvider($provider) {
|
2012-02-03 20:32:06 +00:00
|
|
|
self::$providers[]=$provider;
|
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* finds and loads the providers
|
2012-03-17 16:25:14 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
static private function findProviders() {
|
2012-03-17 16:25:14 +00:00
|
|
|
// Find the providers
|
|
|
|
$apps = OC_App::getAllApps();
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($apps as $app) {
|
2012-06-27 20:43:33 +00:00
|
|
|
$path = OC_App::getAppPath($app) . '/appinfo/migrate.php';
|
2012-09-07 13:22:01 +00:00
|
|
|
if( file_exists( $path ) ) {
|
2014-08-28 10:34:29 +00:00
|
|
|
include_once $path;
|
2012-04-14 12:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 16:25:14 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-19 20:44:20 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* exports a user, or owncloud instance
|
2014-04-21 13:44:54 +00:00
|
|
|
* @param string $uid user id of user to export if export type is user, defaults to current
|
|
|
|
* @param string $type type of export, defualts to user
|
|
|
|
* @param string $path path to zip output folder
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return string on error, path to zip on success
|
2012-03-19 20:44:20 +00:00
|
|
|
*/
|
2012-12-14 22:04:42 +00:00
|
|
|
public static function export( $uid=null, $type='user', $path=null ) {
|
2012-03-19 20:44:20 +00:00
|
|
|
$datadir = OC_Config::getValue( 'datadirectory' );
|
2012-12-14 22:16:32 +00:00
|
|
|
// Validate export type
|
|
|
|
$types = array( 'user', 'instance', 'system', 'userfiles' );
|
|
|
|
if( !in_array( $type, $types ) ) {
|
|
|
|
OC_Log::write( 'migration', 'Invalid export type', OC_Log::ERROR );
|
|
|
|
return json_encode( array( 'success' => false ) );
|
|
|
|
}
|
|
|
|
self::$exporttype = $type;
|
|
|
|
// Userid?
|
|
|
|
if( self::$exporttype == 'user' ) {
|
|
|
|
// Check user exists
|
|
|
|
self::$uid = is_null($uid) ? OC_User::getUser() : $uid;
|
|
|
|
if(!OC_User::userExists(self::$uid)) {
|
|
|
|
return json_encode( array( 'success' => false) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Calculate zipname
|
|
|
|
if( self::$exporttype == 'user' ) {
|
|
|
|
$zipname = 'oc_export_' . self::$uid . '_' . date("y-m-d_H-i-s") . '.zip';
|
|
|
|
} else {
|
|
|
|
$zipname = 'oc_export_' . self::$exporttype . '_' . date("y-m-d_H-i-s") . '.zip';
|
|
|
|
}
|
|
|
|
// Calculate path
|
|
|
|
if( self::$exporttype == 'user' ) {
|
|
|
|
self::$zippath = $datadir . '/' . self::$uid . '/' . $zipname;
|
|
|
|
} else {
|
|
|
|
if( !is_null( $path ) ) {
|
|
|
|
// Validate custom path
|
|
|
|
if( !file_exists( $path ) || !is_writeable( $path ) ) {
|
|
|
|
OC_Log::write( 'migration', 'Path supplied is invalid.', OC_Log::ERROR );
|
|
|
|
return json_encode( array( 'success' => false ) );
|
|
|
|
}
|
|
|
|
self::$zippath = $path . $zipname;
|
|
|
|
} else {
|
|
|
|
// Default path
|
|
|
|
self::$zippath = get_temp_dir() . '/' . $zipname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create the zip object
|
|
|
|
if( !self::createZip() ) {
|
|
|
|
return json_encode( array( 'success' => false ) );
|
|
|
|
}
|
|
|
|
// Do the export
|
|
|
|
self::findProviders();
|
|
|
|
$exportdata = array();
|
|
|
|
switch( self::$exporttype ) {
|
|
|
|
case 'user':
|
|
|
|
// Connect to the db
|
|
|
|
self::$dbpath = $datadir . '/' . self::$uid . '/migration.db';
|
|
|
|
if( !self::connectDB() ) {
|
|
|
|
return json_encode( array( 'success' => false ) );
|
|
|
|
}
|
2013-11-22 14:53:56 +00:00
|
|
|
self::$content = new OC_Migration_Content( self::$zip, self::$migration_database );
|
2012-12-14 22:16:32 +00:00
|
|
|
// Export the app info
|
|
|
|
$exportdata = self::exportAppData();
|
2012-03-19 20:44:20 +00:00
|
|
|
// Add the data dir to the zip
|
2012-08-27 11:21:26 +00:00
|
|
|
self::$content->addDir(OC_User::getHome(self::$uid), true, '/' );
|
2012-12-14 22:16:32 +00:00
|
|
|
break;
|
|
|
|
case 'instance':
|
|
|
|
self::$content = new OC_Migration_Content( self::$zip );
|
2012-03-19 20:44:20 +00:00
|
|
|
// Creates a zip that is compatable with the import function
|
2012-05-30 12:14:32 +00:00
|
|
|
$dbfile = tempnam( get_temp_dir(), "owncloud_export_data_" );
|
2012-03-19 20:44:20 +00:00
|
|
|
OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL');
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-19 20:44:20 +00:00
|
|
|
// Now add in *dbname* and *dbprefix*
|
|
|
|
$dbexport = file_get_contents( $dbfile );
|
|
|
|
$dbnamestring = "<database>\n\n <name>" . OC_Config::getValue( "dbname", "owncloud" );
|
|
|
|
$dbtableprefixstring = "<table>\n\n <name>" . OC_Config::getValue( "dbtableprefix", "oc_" );
|
|
|
|
$dbexport = str_replace( $dbnamestring, "<database>\n\n <name>*dbname*", $dbexport );
|
|
|
|
$dbexport = str_replace( $dbtableprefixstring, "<table>\n\n <name>*dbprefix*", $dbexport );
|
|
|
|
// Add the export to the zip
|
|
|
|
self::$content->addFromString( $dbexport, "dbexport.xml" );
|
|
|
|
// Add user data
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach(OC_User::getUsers() as $user) {
|
2012-08-27 11:21:26 +00:00
|
|
|
self::$content->addDir(OC_User::getHome($user), true, "/userdata/" );
|
2012-03-19 20:44:20 +00:00
|
|
|
}
|
2012-12-14 22:16:32 +00:00
|
|
|
break;
|
2012-03-19 20:44:20 +00:00
|
|
|
case 'userfiles':
|
|
|
|
self::$content = new OC_Migration_Content( self::$zip );
|
|
|
|
// Creates a zip with all of the users files
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach(OC_User::getUsers() as $user) {
|
2012-08-27 11:21:26 +00:00
|
|
|
self::$content->addDir(OC_User::getHome($user), true, "/" );
|
2012-03-19 20:44:20 +00:00
|
|
|
}
|
2012-12-14 22:16:32 +00:00
|
|
|
break;
|
2012-03-19 20:44:20 +00:00
|
|
|
case 'system':
|
|
|
|
self::$content = new OC_Migration_Content( self::$zip );
|
|
|
|
// Creates a zip with the owncloud system files
|
|
|
|
self::$content->addDir( OC::$SERVERROOT . '/', false, '/');
|
2013-02-11 16:44:02 +00:00
|
|
|
foreach (array(
|
|
|
|
".git",
|
|
|
|
"3rdparty",
|
|
|
|
"apps",
|
|
|
|
"core",
|
|
|
|
"files",
|
|
|
|
"l10n",
|
|
|
|
"lib",
|
|
|
|
"ocs",
|
|
|
|
"search",
|
|
|
|
"settings",
|
|
|
|
"tests"
|
|
|
|
) as $dir) {
|
2012-12-14 22:16:32 +00:00
|
|
|
self::$content->addDir( OC::$SERVERROOT . '/' . $dir, true, "/");
|
2012-03-19 20:44:20 +00:00
|
|
|
}
|
2012-12-14 22:16:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( !$info = self::getExportInfo( $exportdata ) ) {
|
|
|
|
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 json_encode( array( 'success' => false ) );
|
|
|
|
}
|
|
|
|
return json_encode( array( 'success' => true, 'data' => self::$zippath ) );
|
2013-02-09 16:27:57 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-20 20:19:21 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* imports a user, or owncloud instance
|
2014-04-21 13:44:54 +00:00
|
|
|
* @param string $path path to zip
|
|
|
|
* @param string $type type of import (user or instance)
|
|
|
|
* @param string|null|int $uid userid of new user
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function import( $path, $type='user', $uid=null ) {
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-03-20 20:19:21 +00:00
|
|
|
$datadir = OC_Config::getValue( 'datadirectory' );
|
|
|
|
// Extract the zip
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !$extractpath = self::extractZip( $path ) ) {
|
2012-04-14 12:31:48 +00:00
|
|
|
return json_encode( array( 'success' => false ) );
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
|
|
|
// Get export_info.json
|
|
|
|
$scan = scandir( $extractpath );
|
|
|
|
// Check for export_info.json
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !in_array( 'export_info.json', $scan ) ) {
|
2012-11-06 20:59:59 +00:00
|
|
|
OC_Log::write( 'migration', 'Invalid import file, export_info.json not found', OC_Log::ERROR );
|
2012-04-14 12:31:48 +00:00
|
|
|
return json_encode( array( 'success' => false ) );
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
|
|
|
$json = json_decode( file_get_contents( $extractpath . 'export_info.json' ) );
|
2012-09-07 13:22:01 +00:00
|
|
|
if( $json->exporttype != $type ) {
|
2012-03-20 20:32:01 +00:00
|
|
|
OC_Log::write( 'migration', 'Invalid import file', OC_Log::ERROR );
|
2012-04-14 12:31:48 +00:00
|
|
|
return json_encode( array( 'success' => false ) );
|
2012-03-20 20:32:01 +00:00
|
|
|
}
|
|
|
|
self::$exporttype = $type;
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-06-15 23:14:40 +00:00
|
|
|
$currentuser = OC_User::getUser();
|
|
|
|
|
2012-03-20 20:19:21 +00:00
|
|
|
// Have we got a user if type is user
|
2012-09-07 13:22:01 +00:00
|
|
|
if( self::$exporttype == 'user' ) {
|
2012-06-15 23:14:40 +00:00
|
|
|
self::$uid = !is_null($uid) ? $uid : $currentuser;
|
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-06-15 23:14:40 +00:00
|
|
|
// We need to be an admin if we are not importing our own data
|
2012-09-07 13:22:01 +00:00
|
|
|
if(($type == 'user' && self::$uid != $currentuser) || $type != 'user' ) {
|
2013-01-14 18:45:17 +00:00
|
|
|
if( !OC_User::isAdminUser($currentuser)) {
|
2012-06-15 23:14:40 +00:00
|
|
|
// Naughty.
|
|
|
|
OC_Log::write( 'migration', 'Import not permitted.', OC_Log::ERROR );
|
2012-08-29 06:38:33 +00:00
|
|
|
return json_encode( array( 'success' => false ) );
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-20 20:19:21 +00:00
|
|
|
// Handle export types
|
2012-09-07 13:22:01 +00:00
|
|
|
switch( self::$exporttype ) {
|
2012-03-20 20:19:21 +00:00
|
|
|
case 'user':
|
|
|
|
// Check user availability
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !OC_User::userExists( self::$uid ) ) {
|
2012-06-15 23:14:40 +00:00
|
|
|
OC_Log::write( 'migration', 'User doesn\'t exist', OC_Log::ERROR );
|
2012-04-14 12:31:48 +00:00
|
|
|
return json_encode( array( 'success' => false ) );
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
2013-03-11 15:21:26 +00:00
|
|
|
|
|
|
|
// Check if the username is valid
|
|
|
|
if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $json->exporteduser )) {
|
|
|
|
OC_Log::write( 'migration', 'Username is not valid', OC_Log::ERROR );
|
|
|
|
return json_encode( array( 'success' => false ) );
|
|
|
|
}
|
|
|
|
|
2012-03-20 20:19:21 +00:00
|
|
|
// Copy data
|
2012-11-06 20:59:59 +00:00
|
|
|
$userfolder = $extractpath . $json->exporteduser;
|
|
|
|
$newuserfolder = $datadir . '/' . self::$uid;
|
|
|
|
foreach(scandir($userfolder) as $file){
|
2013-12-20 11:22:49 +00:00
|
|
|
if($file !== '.' && $file !== '..' && is_dir($userfolder.'/'.$file)) {
|
2013-03-11 15:21:26 +00:00
|
|
|
$file = str_replace(array('/', '\\'), '', $file);
|
|
|
|
|
2012-11-06 20:59:59 +00:00
|
|
|
// Then copy the folder over
|
2012-11-06 23:49:25 +00:00
|
|
|
OC_Helper::copyr($userfolder.'/'.$file, $newuserfolder.'/'.$file);
|
2012-11-06 20:59:59 +00:00
|
|
|
}
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
// Import user app data
|
2012-11-09 23:53:28 +00:00
|
|
|
if(file_exists($extractpath . $json->exporteduser . '/migration.db')) {
|
2013-02-11 16:44:02 +00:00
|
|
|
if( !$appsimported = self::importAppData( $extractpath . $json->exporteduser . '/migration.db',
|
|
|
|
$json,
|
|
|
|
self::$uid ) ) {
|
2012-11-06 23:49:25 +00:00
|
|
|
return json_encode( array( 'success' => false ) );
|
|
|
|
}
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
|
|
|
// All done!
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !self::unlink_r( $extractpath ) ) {
|
2012-04-14 12:31:48 +00:00
|
|
|
OC_Log::write( 'migration', 'Failed to delete the extracted zip', OC_Log::ERROR );
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
2012-04-07 21:55:16 +00:00
|
|
|
return json_encode( array( 'success' => true, 'data' => $appsimported ) );
|
2012-12-14 22:16:32 +00:00
|
|
|
break;
|
2012-03-20 20:19:21 +00:00
|
|
|
case 'instance':
|
2012-04-01 00:25:47 +00:00
|
|
|
/*
|
2012-04-07 17:27:09 +00:00
|
|
|
* EXPERIMENTAL
|
2012-03-20 20:19:21 +00:00
|
|
|
// Check for new data dir and dbexport before doing anything
|
|
|
|
// TODO
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-20 20:19:21 +00:00
|
|
|
// Delete current data folder.
|
|
|
|
OC_Log::write( 'migration', "Deleting current data dir", OC_Log::INFO );
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !self::unlink_r( $datadir, false ) ) {
|
2012-03-20 20:19:21 +00:00
|
|
|
OC_Log::write( 'migration', 'Failed to delete the current data dir', OC_Log::ERROR );
|
2012-04-14 12:31:48 +00:00
|
|
|
return json_encode( array( 'success' => false ) );
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-20 20:19:21 +00:00
|
|
|
// Copy over data
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !self::copy_r( $extractpath . 'userdata', $datadir ) ) {
|
2012-03-20 20:19:21 +00:00
|
|
|
OC_Log::write( 'migration', 'Failed to copy over data directory', OC_Log::ERROR );
|
2012-04-14 12:31:48 +00:00
|
|
|
return json_encode( array( 'success' => false ) );
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-20 20:19:21 +00:00
|
|
|
// Import the db
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !OC_DB::replaceDB( $extractpath . 'dbexport.xml' ) ) {
|
2012-04-14 12:31:48 +00:00
|
|
|
return json_encode( array( 'success' => false ) );
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
|
|
|
// Done
|
2012-06-30 10:57:47 +00:00
|
|
|
return json_encode( array( 'success' => true ) );
|
2012-04-01 00:25:47 +00:00
|
|
|
*/
|
2012-12-14 22:16:32 +00:00
|
|
|
break;
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-20 20:19:21 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* recursively deletes a directory
|
2014-04-21 13:44:54 +00:00
|
|
|
* @param string $dir path of dir to delete
|
|
|
|
* @param bool $deleteRootToo delete the root directory
|
2012-03-20 20:19:21 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private static function unlink_r( $dir, $deleteRootToo=true ) {
|
|
|
|
if( !$dh = @opendir( $dir ) ) {
|
2012-04-14 12:31:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
while (false !== ($obj = readdir($dh))) {
|
2012-04-14 12:31:48 +00:00
|
|
|
if($obj == '.' || $obj == '..') {
|
|
|
|
continue;
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if (!@unlink($dir . '/' . $obj)) {
|
2012-04-14 12:31:48 +00:00
|
|
|
self::unlink_r($dir.'/'.$obj, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dh);
|
|
|
|
if ( $deleteRootToo ) {
|
|
|
|
@rmdir($dir);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-20 20:19:21 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* tries to extract the import zip
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param string $path path to the zip
|
2012-03-20 20:19:21 +00:00
|
|
|
* @return string path to extract location (with a trailing slash) or false on failure
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
static private function extractZip( $path ) {
|
2012-03-20 20:19:21 +00:00
|
|
|
self::$zip = new ZipArchive;
|
|
|
|
// Validate path
|
2012-12-14 22:16:32 +00:00
|
|
|
if( !file_exists( $path ) ) {
|
|
|
|
OC_Log::write( 'migration', 'Zip not found', OC_Log::ERROR );
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-23 06:01:09 +00:00
|
|
|
if ( self::$zip->open( $path ) != true ) {
|
2012-03-20 20:19:21 +00:00
|
|
|
OC_Log::write( 'migration', "Failed to open zip file", OC_Log::ERROR );
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
$to = get_temp_dir() . '/oc_import_' . self::$exporttype . '_' . date("y-m-d_H-i-s") . '/';
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !self::$zip->extractTo( $to ) ) {
|
2012-04-14 12:31:48 +00:00
|
|
|
return false;
|
2012-03-20 20:19:21 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
self::$zip->close();
|
2012-03-20 20:19:21 +00:00
|
|
|
return $to;
|
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-02-03 20:32:06 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* creates a migration.db in the users data dir with their app data in
|
2012-03-09 23:33:11 +00:00
|
|
|
* @return bool whether operation was successfull
|
2012-02-03 20:32:06 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private static function exportAppData( ) {
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-19 20:44:20 +00:00
|
|
|
$success = true;
|
2012-03-10 18:18:58 +00:00
|
|
|
$return = array();
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-09 23:33:11 +00:00
|
|
|
// Foreach provider
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach( self::$providers as $provider ) {
|
2012-05-10 23:06:53 +00:00
|
|
|
// Check if the app is enabled
|
2012-09-07 13:22:01 +00:00
|
|
|
if( OC_App::isEnabled( $provider->getID() ) ) {
|
2012-05-10 23:06:53 +00:00
|
|
|
$success = true;
|
|
|
|
// Does this app use the database?
|
2012-09-07 13:22:01 +00:00
|
|
|
if( file_exists( OC_App::getAppPath($provider->getID()).'/appinfo/database.xml' ) ) {
|
2012-05-10 23:06:53 +00:00
|
|
|
// Create some app tables
|
|
|
|
$tables = self::createAppTables( $provider->getID() );
|
2012-09-07 13:22:01 +00:00
|
|
|
if( is_array( $tables ) ) {
|
2012-05-10 23:06:53 +00:00
|
|
|
// Save the table names
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($tables as $table) {
|
2012-05-10 23:06:53 +00:00
|
|
|
$return['apps'][$provider->getID()]['tables'][] = $table;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// It failed to create the tables
|
|
|
|
$success = false;
|
2012-04-14 12:31:48 +00:00
|
|
|
}
|
2012-05-10 23:06:53 +00:00
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-05-10 23:06:53 +00:00
|
|
|
// Run the export function?
|
2012-09-07 13:22:01 +00:00
|
|
|
if( $success ) {
|
2012-05-10 23:06:53 +00:00
|
|
|
// Set the provider properties
|
|
|
|
$provider->setData( self::$uid, self::$content );
|
|
|
|
$return['apps'][$provider->getID()]['success'] = $provider->export();
|
2012-03-12 18:40:14 +00:00
|
|
|
} else {
|
2012-05-10 23:06:53 +00:00
|
|
|
$return['apps'][$provider->getID()]['success'] = false;
|
|
|
|
$return['apps'][$provider->getID()]['message'] = 'failed to create the app tables';
|
2012-04-14 12:31:48 +00:00
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-05-10 23:06:53 +00:00
|
|
|
// Now add some app info the the return array
|
|
|
|
$appinfo = OC_App::getAppInfo( $provider->getID() );
|
|
|
|
$return['apps'][$provider->getID()]['version'] = OC_App::getAppVersion($provider->getID());
|
2012-03-10 18:18:58 +00:00
|
|
|
}
|
2012-03-09 23:33:11 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-10 18:18:58 +00:00
|
|
|
return $return;
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-09 23:33:11 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
|
|
|
|
2012-03-16 21:09:36 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* generates json containing export info, and merges any data supplied
|
2014-04-21 13:44:54 +00:00
|
|
|
* @param array $array of data to include in the returned json
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return string
|
2012-03-19 20:44:20 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
static private function getExportInfo( $array=array() ) {
|
2012-03-16 21:09:36 +00:00
|
|
|
$info = array(
|
|
|
|
'ocversion' => OC_Util::getVersion(),
|
|
|
|
'exporttime' => time(),
|
|
|
|
'exportedby' => OC_User::getUser(),
|
2012-06-30 10:57:47 +00:00
|
|
|
'exporttype' => self::$exporttype,
|
|
|
|
'exporteduser' => self::$uid
|
2012-03-16 21:09:36 +00:00
|
|
|
);
|
2012-06-30 10:57:47 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !is_array( $array ) ) {
|
2012-04-14 12:31:48 +00:00
|
|
|
OC_Log::write( 'migration', 'Supplied $array was not an array in getExportInfo()', OC_Log::ERROR );
|
2012-03-19 20:44:20 +00:00
|
|
|
}
|
2012-03-17 13:53:00 +00:00
|
|
|
// Merge in other data
|
2012-03-19 20:44:20 +00:00
|
|
|
$info = array_merge( $info, (array)$array );
|
2012-03-16 21:09:36 +00:00
|
|
|
// Create json
|
|
|
|
$json = json_encode( $info );
|
2012-03-20 20:19:21 +00:00
|
|
|
return $json;
|
2012-03-16 21:09:36 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-13 23:09:43 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* connects to migration.db, or creates if not found
|
2014-04-21 13:44:54 +00:00
|
|
|
* @param string $path to migration.db, defaults to user data dir
|
2012-03-19 20:44:20 +00:00
|
|
|
* @return bool whether the operation was successful
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
static private function connectDB( $path=null ) {
|
2012-03-19 20:44:20 +00:00
|
|
|
// Has the dbpath been set?
|
|
|
|
self::$dbpath = !is_null( $path ) ? $path : self::$dbpath;
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !self::$dbpath ) {
|
2012-03-19 20:44:20 +00:00
|
|
|
OC_Log::write( 'migration', 'connectDB() was called without dbpath being set', OC_Log::ERROR );
|
2012-04-14 12:31:48 +00:00
|
|
|
return false;
|
2012-03-13 23:09:43 +00:00
|
|
|
}
|
2012-03-19 20:44:20 +00:00
|
|
|
// Already connected
|
2013-11-22 14:53:56 +00:00
|
|
|
if(!self::$migration_database) {
|
2012-03-19 20:44:20 +00:00
|
|
|
$datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" );
|
2013-11-22 14:53:56 +00:00
|
|
|
$connectionParams = array(
|
|
|
|
'path' => self::$dbpath,
|
|
|
|
'driver' => 'pdo_sqlite',
|
2012-03-19 20:44:20 +00:00
|
|
|
);
|
2013-12-20 11:09:19 +00:00
|
|
|
$connectionParams['adapter'] = '\OC\DB\AdapterSqlite';
|
|
|
|
$connectionParams['wrapperClass'] = 'OC\DB\Connection';
|
|
|
|
$connectionParams['tablePrefix'] = '';
|
2012-03-19 20:44:20 +00:00
|
|
|
|
|
|
|
// Try to establish connection
|
2013-11-22 14:53:56 +00:00
|
|
|
self::$migration_database = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
|
2012-03-13 23:09:43 +00:00
|
|
|
}
|
2012-03-19 20:44:20 +00:00
|
|
|
return true;
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-19 20:44:20 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-19 20:44:20 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* creates the tables in migration.db from an apps database.xml
|
2014-04-21 13:44:54 +00:00
|
|
|
* @param string $appid id of the app
|
2012-03-19 20:44:20 +00:00
|
|
|
* @return bool whether the operation was successful
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
static private function createAppTables( $appid ) {
|
2013-11-22 14:53:56 +00:00
|
|
|
$schema_manager = new OC\DB\MDB2SchemaManager(self::$migration_database);
|
2012-04-14 12:31:48 +00:00
|
|
|
|
|
|
|
// There is a database.xml file
|
2012-06-27 20:43:33 +00:00
|
|
|
$content = file_get_contents(OC_App::getAppPath($appid) . '/appinfo/database.xml' );
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-19 20:44:20 +00:00
|
|
|
$file2 = 'static://db_scheme';
|
|
|
|
// TODO get the relative path to migration.db from the data dir
|
|
|
|
// For now just cheat
|
|
|
|
$path = pathinfo( self::$dbpath );
|
|
|
|
$content = str_replace( '*dbname*', self::$uid.'/migration', $content );
|
|
|
|
$content = str_replace( '*dbprefix*', '', $content );
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-19 20:44:20 +00:00
|
|
|
$xml = new SimpleXMLElement($content);
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($xml->table as $table) {
|
2012-04-14 12:31:48 +00:00
|
|
|
$tables[] = (string)$table->name;
|
|
|
|
}
|
|
|
|
|
2012-03-19 20:44:20 +00:00
|
|
|
file_put_contents( $file2, $content );
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-19 20:44:20 +00:00
|
|
|
// Try to create tables
|
2013-11-22 14:53:56 +00:00
|
|
|
try {
|
|
|
|
$schema_manager->createDbFromStructure($file2);
|
|
|
|
} catch(Exception $e) {
|
|
|
|
unlink( $file2 );
|
2012-03-19 20:44:20 +00:00
|
|
|
OC_Log::write( 'migration', 'Failed to create tables for: '.$appid, OC_Log::FATAL );
|
2013-11-22 14:53:56 +00:00
|
|
|
OC_Log::write( 'migration', $e->getMessage(), OC_Log::FATAL );
|
2012-03-19 20:44:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-22 14:53:56 +00:00
|
|
|
return $tables;
|
2012-03-19 20:44:20 +00:00
|
|
|
}
|
|
|
|
|
2012-03-15 20:52:43 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* tries to create the zip
|
2012-03-15 20:52:43 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
static private function createZip() {
|
2012-03-20 20:19:21 +00:00
|
|
|
self::$zip = new ZipArchive;
|
2012-03-15 20:52:43 +00:00
|
|
|
// Check if properties are set
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !self::$zippath ) {
|
2012-03-15 20:52:43 +00:00
|
|
|
OC_Log::write('migration', 'createZip() called but $zip and/or $zippath have not been set', OC_Log::ERROR);
|
2012-04-14 12:31:48 +00:00
|
|
|
return false;
|
2012-03-15 20:52:43 +00:00
|
|
|
}
|
2012-10-23 06:01:09 +00:00
|
|
|
if ( self::$zip->open( self::$zippath, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE ) !== true ) {
|
2013-02-11 16:44:02 +00:00
|
|
|
OC_Log::write('migration',
|
|
|
|
'Failed to create the zip with error: '.self::$zip->getStatusString(),
|
|
|
|
OC_Log::ERROR);
|
2012-03-15 20:52:43 +00:00
|
|
|
return false;
|
2012-12-14 22:16:32 +00:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-15 20:52:43 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-13 21:28:53 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* returns an array of apps that support migration
|
2012-03-13 21:28:53 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
static public function getApps() {
|
2012-03-13 21:28:53 +00:00
|
|
|
$allapps = OC_App::getAllApps();
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($allapps as $app) {
|
2012-06-22 20:58:28 +00:00
|
|
|
$path = self::getAppPath($app) . '/lib/migrate.php';
|
2012-09-07 13:22:01 +00:00
|
|
|
if( file_exists( $path ) ) {
|
2012-03-13 21:28:53 +00:00
|
|
|
$supportsmigration[] = $app;
|
2012-04-14 12:31:48 +00:00
|
|
|
}
|
2012-03-13 21:28:53 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
return $supportsmigration;
|
2012-03-13 21:28:53 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-09 23:33:11 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* imports a new user
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param string $db string path to migration.db
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param object $info object of migration info
|
2014-04-21 13:44:54 +00:00
|
|
|
* @param string|null|int $uid uid to use
|
2014-05-11 17:13:51 +00:00
|
|
|
* @return array an array of apps with import statuses, or false on failure.
|
2012-03-09 23:33:11 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function importAppData( $db, $info, $uid=null ) {
|
2012-03-12 21:41:32 +00:00
|
|
|
// Check if the db exists
|
2012-09-07 13:22:01 +00:00
|
|
|
if( file_exists( $db ) ) {
|
2012-03-12 21:41:32 +00:00
|
|
|
// Connect to the db
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!self::connectDB( $db )) {
|
2012-11-04 17:28:29 +00:00
|
|
|
OC_Log::write('migration', 'Failed to connect to migration.db', OC_Log::ERROR);
|
2012-03-12 21:41:32 +00:00
|
|
|
return false;
|
2012-04-14 12:31:48 +00:00
|
|
|
}
|
2012-03-12 21:41:32 +00:00
|
|
|
} else {
|
2012-11-04 10:10:46 +00:00
|
|
|
OC_Log::write('migration', 'Migration.db not found at: '.$db, OC_Log::FATAL );
|
2012-03-12 21:41:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-17 16:25:14 +00:00
|
|
|
// Find providers
|
|
|
|
self::findProviders();
|
|
|
|
|
|
|
|
// Generate importinfo array
|
2012-04-14 12:31:48 +00:00
|
|
|
$importinfo = array(
|
2012-03-17 16:25:14 +00:00
|
|
|
'olduid' => $info->exporteduser,
|
|
|
|
'newuid' => self::$uid
|
|
|
|
);
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach( self::$providers as $provider) {
|
2012-03-12 21:41:32 +00:00
|
|
|
// Is the app in the export?
|
2012-03-19 20:44:20 +00:00
|
|
|
$id = $provider->getID();
|
2012-09-07 13:22:01 +00:00
|
|
|
if( isset( $info->apps->$id ) ) {
|
2012-03-27 21:21:14 +00:00
|
|
|
// Is the app installed
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !OC_App::isEnabled( $id ) ) {
|
2013-02-11 16:44:02 +00:00
|
|
|
OC_Log::write( 'migration',
|
|
|
|
'App: ' . $id . ' is not installed, can\'t import data.',
|
|
|
|
OC_Log::INFO );
|
2012-04-14 12:31:48 +00:00
|
|
|
$appsstatus[$id] = 'notsupported';
|
2012-03-27 21:21:14 +00:00
|
|
|
} else {
|
|
|
|
// Did it succeed on export?
|
2012-09-07 13:22:01 +00:00
|
|
|
if( $info->apps->$id->success ) {
|
2012-03-27 21:21:14 +00:00
|
|
|
// Give the provider the content object
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !self::connectDB( $db ) ) {
|
2012-04-14 12:31:48 +00:00
|
|
|
return false;
|
2012-03-27 21:21:14 +00:00
|
|
|
}
|
2013-11-22 14:53:56 +00:00
|
|
|
$content = new OC_Migration_Content( self::$zip, self::$migration_database );
|
2012-03-27 21:21:14 +00:00
|
|
|
$provider->setData( self::$uid, $content, $info );
|
|
|
|
// Then do the import
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !$appsstatus[$id] = $provider->import( $info->apps->$id, $importinfo ) ) {
|
2012-03-27 21:21:14 +00:00
|
|
|
// Failed to import app
|
2013-02-11 16:44:02 +00:00
|
|
|
OC_Log::write( 'migration',
|
|
|
|
'Failed to import app data for user: ' . self::$uid . ' for app: ' . $id,
|
|
|
|
OC_Log::ERROR );
|
2012-03-27 21:21:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Add to failed list
|
2012-04-14 12:31:48 +00:00
|
|
|
$appsstatus[$id] = false;
|
2012-03-19 20:44:20 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-09 23:33:11 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-27 21:21:14 +00:00
|
|
|
return $appsstatus;
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-09 23:33:11 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2014-04-21 13:44:54 +00:00
|
|
|
/**
|
|
|
|
* creates a new user in the database
|
|
|
|
* @param string $uid user_id of the user to be created
|
|
|
|
* @param string $hash hash of the user to be created
|
2012-03-19 20:44:20 +00:00
|
|
|
* @return bool result of user creation
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function createUser( $uid, $hash ) {
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-09 23:33:11 +00:00
|
|
|
// Check if userid exists
|
2012-09-07 13:22:01 +00:00
|
|
|
if(OC_User::userExists( $uid )) {
|
2012-03-09 23:33:11 +00:00
|
|
|
return false;
|
2012-03-03 17:30:21 +00:00
|
|
|
}
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-09 23:33:11 +00:00
|
|
|
// Create the user
|
|
|
|
$query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" );
|
2012-03-17 16:25:14 +00:00
|
|
|
$result = $query->execute( array( $uid, $hash));
|
2012-09-07 13:22:01 +00:00
|
|
|
if( !$result ) {
|
2013-01-06 22:30:17 +00:00
|
|
|
OC_Log::write('migration', 'Failed to create the new user "'.$uid."", OC_Log::ERROR);
|
2012-03-12 21:41:32 +00:00
|
|
|
}
|
2012-03-09 23:33:11 +00:00
|
|
|
return $result ? true : false;
|
2012-04-14 12:31:48 +00:00
|
|
|
|
2012-03-09 23:33:11 +00:00
|
|
|
}
|
2012-03-16 21:09:36 +00:00
|
|
|
|
2012-02-03 20:32:06 +00:00
|
|
|
}
|