Fix export for admin and users. Added 3 admin export types

This commit is contained in:
Tom Needham 2012-03-16 21:09:36 +00:00
parent 50233d075c
commit c442a06a02
4 changed files with 90 additions and 67 deletions

View file

@ -40,12 +40,12 @@ if (isset($_POST['admin_export'])) {
header("Content-Disposition: attachment; filename=" . basename($path));
header("Content-Length: " . filesize($path));
@ob_end_clean();
readfile($path);
OC_Migrate::cleanUp( $path );
readfile( $path );
unlink( $path );
}
// Import?
} else if( isset($_POST['admin_import']) ){
/*
$root = OC::$SERVERROOT . "/";
$importname = "owncloud_import_" . date("y-m-d_H-i-s");
@ -85,7 +85,8 @@ if (isset($_POST['admin_export'])) {
exit();
}
OC_DB::replaceDB( get_temp_dir() . '/' . $importname . '/dbexport.xml' );
OC_DB::replaceDB( get_temp_dir() . '/' . $importname . '/dbexport.xml' );
*/
} else {
// fill template
$tmpl = new OC_Template('admin_export', 'settings');

View file

@ -8,7 +8,7 @@
<p>
<input type="radio" name="export_type" value="instance" /> ownCloud instance ( suitable for import )<br />
<input type="radio" name="export_type" value="system" /> ownCloud system files<br />
<input type="radio" name="export_type" value="userfiles" /> Just user files
<input type="radio" name="export_type" value="userfiles" /> Just user files<br />
<input type="submit" name="admin_export" value="<?php echo $l->t('Export'); ?>" />
</fieldset>
</form>

View file

@ -36,7 +36,7 @@ if (isset($_POST['user_export'])) {
header("Content-Length: " . filesize($path));
@ob_end_clean();
readfile($path);
OC_Migrate::cleanUp( $path );
unlink( $path );
}
} if( isset( $_POST['user_import'] ) ){
// TODO

View file

@ -40,6 +40,10 @@ class OC_Migrate{
static private $zip=false;
// String path to export
static private $zippath=false;
// Stores the type of export
static private $exporttype=false;
// Array of temp files to be deleted after zip creation
static private $tmpfiles=array();
/**
* register a new migration provider
@ -138,14 +142,7 @@ class OC_Migrate{
self::$zippath = $path . $zipname;
} else {
// Save in tmp dir
$structure = sys_get_temp_dir() . '/owncloudexports/';
if( !file_exists( $structure ) ){
if ( !mkdir( $structure ) ) {
OC_Log::write('migration', 'Could not create the temporary export at: '.$structure, OC_Log::ERROR);
return false;
}
}
self::$zippath = $structure . $zipname;
self::$zippath = sys_get_temp_dir() . '/' . $zipname;
}
// Create the zip object
self::$zip = new ZipArchive;
@ -154,42 +151,48 @@ class OC_Migrate{
return false;
}
// Handle export types
if( $exporttype == 'instance' ){
// Creates a zip that is compatable with the import function
/*
$dbfile = self:: . "/dbexport.xml";
OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL');
// Now add in *dbname* and *dbtableprefix*
$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 );
// Write the new db export file
file_put_contents( $dbfile, $dbexport );
$zip->addFile($dbfile, "dbexport.xml");
*/
} else if( $exporttype == 'system' ){
// Creates a zip with the owncloud system files
self::addDirToZip( OC::$SERVERROOT . '/', false);
foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dir) {
self::addDirToZip( OC::$SERVERROOT . '/' . $dir, true, "/");
}
} else if ( $exporttype == 'userfiles' ){
// Creates a zip with all of the users files
foreach(OC_User::getUsers() as $user){
self::addDirToZip( $datadir . '/' . $user . '/', true, "/" . $user);
}
} else {
// Invalid export type supplied
OC_Log::write('migration', 'Invalid export type supplied to createSysExportFile() "'.$exporttype.'"', OC_Log::ERROR);
$exporttypes = array( 'userfiles', 'instance', 'system' );
self::$exporttype = in_array( $exporttype, $exporttypes ) ? $exporttype : false;
if( !self::$exporttype ){
OC_Log::write( 'migration', 'Export type: '.$exporttype.' is not supported.', OC_Log::ERROR);
return false;
}
switch( self::$exporttype ){
case 'instance':
// Creates a zip that is compatable with the import function
$dbfile = tempnam( "/tmp", "owncloud_export_data_" );
OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL');
// 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 );
// Write the new db export file
file_put_contents( $dbfile, $dbexport );
self::$zip->addFile( $dbfile, "dbexport.xml" );
// Add user data
foreach(OC_User::getUsers() as $user){
self::addDirToZip( $datadir . '/' . $user . '/', true, "/userdata/" );
}
break;
case 'userfiles':
// Creates a zip with all of the users files
foreach(OC_User::getUsers() as $user){
self::addDirToZip( $datadir . '/' . $user . '/', true, "/" );
}
break;
case 'system':
// Creates a zip with the owncloud system files
self::addDirToZip( OC::$SERVERROOT . '/', false, '/');
foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dir) {
self::addDirToZip( OC::$SERVERROOT . '/' . $dir, true, "/");
}
break;
}
// Add export info
self::addExportInfo();
// Close the zip
if( !self::closeZip() ){
return false;
@ -198,6 +201,30 @@ class OC_Migrate{
}
/**
* @breif adds a json file with infomation on the export to the zips root (used on import)
* @return bool
*/
static private function addExportInfo(){
$info = array(
'ocversion' => OC_Util::getVersion(),
'exporttime' => time(),
'exportedby' => OC_User::getUser(),
'exporttype' => self::$exporttype
);
// Create json
$json = json_encode( $info );
$tmpfile = tempnam("/tmp", "oc_export_info_");
self::$tmpfiles[] = $tmpfile;
if( !file_put_contents( $tmpfile, $json ) ){
return false;
} else {
self::$zip->addFile( $tmpfile, "export_info.json" );
return true;
}
}
/**
* @breif tried to finalise the zip
* @return bool
@ -205,13 +232,25 @@ class OC_Migrate{
static private function closeZip(){
if( !self::$zip->close() ){
OC_Log::write('migration', 'Failed to save the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR);
self::cleanup();
return false;
} else {
OC_Log::write('migration', 'Created export file for: '.self::$uid, OC_Log::INFO);
OC_Log::write('migration', 'Export zip created ok', OC_Log::INFO);
self::cleanup();
return true;
}
}
/**
* @breif cleans up after the zip
*/
static private function cleanup(){
// Delete tmp files
foreach(self::$tmpfiles as $i){
unlink( $i );
}
}
/**
* @breif creates a zip user export
* @param optional $uid string user id of the user to export (defaults to current)
@ -647,22 +686,5 @@ class OC_Migrate{
return $result ? true : false;
}
/**
* @breif removes migration.db and exportinfo.json from the users data dir
* @param optional $path string path to the export zip to delete
* @return void
*/
static public function cleanUp( $path=null ){
$userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . self::$uid;
// Remove migration.db
unlink( $userdatadir . '/migration.db' );
// Remove exportinfo.json
unlink( $userdatadir . '/exportinfo.json' );
// Remove the zip
if( !is_null( $path ) ){
unlink( $path );
}
return true;
}
}