Improve admin_export ui and move system export cde to OC_Migrate

This commit is contained in:
Tom Needham 2012-03-15 20:52:43 +00:00
parent c9be325af2
commit 50233d075c
4 changed files with 151 additions and 90 deletions

View File

@ -28,70 +28,22 @@ OC_Util::checkAppEnabled('admin_export');
define('DS', '/'); define('DS', '/');
// Export?
if (isset($_POST['admin_export'])) { if (isset($_POST['admin_export'])) {
$root = OC::$SERVERROOT . "/"; // Create the export zip
$datadir = OC_Config::getValue( 'datadirectory' ); if( !$path = OC_Migrate::createSysExportFile( $_POST['export_type'] ) ){
$zip = new ZipArchive(); // Error
$tempdir = get_temp_dir(); die('error');
$filename = $tempdir . "/owncloud_export_" . date("y-m-d_H-i-s") . ".zip"; } else {
OC_Log::write('admin_export',"Creating export file at: " . $filename,OC_Log::INFO); // Download it
if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
exit("Cannot open <$filename>\n");
}
if (isset($_POST['owncloud_system'])) {
// adding owncloud system files
OC_Log::write('admin_export',"Adding owncloud system files to export",OC_Log::INFO);
zipAddDir($root, $zip, false);
foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dirname) {
zipAddDir($root . $dirname, $zip, true, "/");
}
}
if (isset($_POST['owncloud_config'])) {
// adding owncloud config
// todo: add database export
$dbfile = $tempdir . "/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");
OC_Log::write('admin_export',"Adding owncloud config to export",OC_Log::INFO);
zipAddDir($root . "config/", $zip, true, "/");
}
if (isset($_POST['user_files'])) {
// needs to handle data outside of the default data dir.
// adding user files
$zip->addFile($root . '/data/.htaccess', "data/.htaccess");
$zip->addFile($root . '/data/index.html', "data/index.html");
foreach (OC_User::getUsers() as $i) {
OC_Log::write('admin_export',"Adding owncloud user files of $i to export",OC_Log::INFO);
zipAddDir($datadir . '/' . $i, $zip, true, "/data/");
}
}
$zip->close();
header("Content-Type: application/zip"); header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=" . basename($filename)); header("Content-Disposition: attachment; filename=" . basename($path));
header("Content-Length: " . filesize($filename)); header("Content-Length: " . filesize($path));
@ob_end_clean(); @ob_end_clean();
readfile($filename); readfile($path);
unlink($filename); OC_Migrate::cleanUp( $path );
unlink($dbfile); }
// Import?
} else if( isset($_POST['admin_import']) ){ } else if( isset($_POST['admin_import']) ){
$root = OC::$SERVERROOT . "/"; $root = OC::$SERVERROOT . "/";

View File

@ -2,12 +2,13 @@
<fieldset class="personalblock"> <fieldset class="personalblock">
<legend><strong><?php echo $l->t('Export this ownCloud instance');?></strong></legend> <legend><strong><?php echo $l->t('Export this ownCloud instance');?></strong></legend>
<p><?php echo $l->t('This will create a compressed file that contains the data of this owncloud instance. <p><?php echo $l->t('This will create a compressed file that contains the data of this owncloud instance.
Please choose which components should be included:');?> Please choose the export type:');?>
</p>
<p><input type="checkbox" id="user_files" name="user_files" value="true"><label for="user_files"><?php echo $l->t('User files');?></label><br/>
<input type="checkbox" id="owncloud_system" name="owncloud_system" value="true"><label for="owncloud_system"><?php echo $l->t('ownCloud system files');?></label><br/>
<input type="checkbox" id="owncloud_config" name="owncloud_config" value="true"><label for="owncloud_config"><?php echo $l->t('ownCloud configuration');?></label>
</p> </p>
<h3>What would you like to export?</h3>
<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="submit" name="admin_export" value="<?php echo $l->t('Export'); ?>" /> <input type="submit" name="admin_export" value="<?php echo $l->t('Export'); ?>" />
</fieldset> </fieldset>
</form> </form>

View File

@ -26,7 +26,7 @@ OC_Util::checkAppEnabled('user_migrate');
if (isset($_POST['user_export'])) { if (isset($_POST['user_export'])) {
// Create the export zip // Create the export zip
if( !$path = OC_Migrate::createExportFile() ){ if( !$path = OC_Migrate::createUserExportFile() ){
// Error // Error
die('error'); die('error');
} else { } else {

View File

@ -111,20 +111,120 @@ class OC_Migrate{
} }
/**
* @breif creates an export file for the whole system
* @param optional $exporttype string export type ('instance','system' or 'userfiles')
* @param optional $path string path to zip destination (with trailing slash)
* @return path to the zip or false if there was a problem
*/
static public function createSysExportFile( $exporttype='instance', $path=null ){
// Calculate zip name
$zipname = "owncloud_export_" . date("y-m-d_H-i-s") . ".zip";
// Get the data dir
$datadir = OC_Config::getValue( 'datadirectory' );
// Calculate destination
if( !is_null( $path ) ){
// Path given
// Is a directory?
if( !is_dir( $path ) ){
OC_Log::write('migration', 'Path supplied to createSysExportFile() is not a directory', OC_Log::ERROR);
return false;
}
// Is writeable
if( !is_writeable( $path ) ){
OC_Log::write('migration', 'Path supplied to createSysExportFile() is not writeable', OC_Log::ERROR);
return false;
}
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;
}
// Create the zip object
self::$zip = new ZipArchive;
// Try to create the zip
if( !self::createZip() ){
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);
return false;
}
// Close the zip
if( !self::closeZip() ){
return false;
}
return self::$zippath;
}
/**
* @breif tried to finalise the zip
* @return bool
*/
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);
return false;
} else {
OC_Log::write('migration', 'Created export file for: '.self::$uid, OC_Log::INFO);
return true;
}
}
/** /**
* @breif creates a zip user export * @breif creates a zip user export
* @param optional $uid string user id of the user to export (defaults to current) * @param optional $uid string user id of the user to export (defaults to current)
* @param optional $path string path to folder to create file in (with trailing slash) (defaults to current user's data dir) * @param optional $path string path to folder to create file in (with trailing slash) (defaults to current user's data dir)
* @return false on failure | string path on success * @return false on failure | string path on success
*/ */
static public function createExportFile( $uid=null, $path=null ){ static public function createUserExportFile( $uid=null, $path=null ){
// User passed? // User passed?
$uid = is_null( $uid ) ? OC_User::getUser() : $uid ; $uid = is_null( $uid ) ? OC_User::getUser() : $uid ;
// Is a database user? // Is a database user?
if( !OC_User_Database::userExists( $uid ) ){ if( !OC_User_Database::userExists( $uid ) ){
OC_Log::write('migration', 'User: '.$uid.' is not in the database and so cannot be exported.', OC_Log::ERROR); OC_Log::write('migration', 'User: '.$uid.' is not in the database and so cannot be exported.', OC_Log::ERROR);
return false; return false;
exit();
} }
// Set the uid // Set the uid
self::$uid = $uid; self::$uid = $uid;
@ -140,42 +240,54 @@ class OC_Migrate{
// Path given // Path given
// Is a directory? // Is a directory?
if( !is_dir( $path ) ){ if( !is_dir( $path ) ){
OC_Log::write('migration', 'Path supplied to createExportFile() is not a directory', OC_Log::ERROR); OC_Log::write('migration', 'Path supplied to createUserExportFile() is not a directory', OC_Log::ERROR);
return false; return false;
exit();
} }
// Is writeable // Is writeable
if( !is_writeable( $path ) ){ if( !is_writeable( $path ) ){
OC_Log::write('migration', 'Path supplied to createExportFile() is not writeable', OC_Log::ERROR); OC_Log::write('migration', 'Path supplied to createUserExportFile() is not writeable', OC_Log::ERROR);
return false; return false;
exit();
} }
self::$zippath = $path . $zipname; self::$zippath = $path . $zipname;
} else { } else {
// Save in users data dir // Save in users data dir
self::$zippath = $userdatadir . $zipname; self::$zippath = $userdatadir . $zipname;
} }
if (self::$zip->open(self::$zippath, ZIPARCHIVE::CREATE) !== TRUE) { // Try to create the zip
// TODO ADD LOGGING if( !self::createZip() ){
exit("Cannot open <$filename>\n"); return false;
} }
// Export the app info // Export the app info
$info = json_encode( self::exportAppData() ); $info = json_encode( self::exportAppData() );
file_put_contents( $userdatadir . '/exportinfo.json', $info ); file_put_contents( $userdatadir . '/exportinfo.json', $info );
// Add the data dir to the zip // Add the data dir to the zip
self::addDirToZip( $userdatadir ); self::addDirToZip( $userdatadir );
// All done! // Close the zip
if( !self::$zip->close() ){ if( !self::closeZip() ){
OC_Log::write('migration', 'Failed to save the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR);
return false; return false;
exit();
} else {
OC_Log::write('migration', 'Created export file for: '.self::$uid, OC_Log::INFO);
//return true;
} }
// All good
return self::$zippath; return self::$zippath;
} }
/**
* @breif tries to create the zip
* @return bool
*/
static private function createZip(){
// Check if properties are set
if( !self::$zip || !self::$zippath ){
OC_Log::write('migration', 'createZip() called but $zip and/or $zippath have not been set', OC_Log::ERROR);
return false;
}
if ( self::$zip->open( self::$zippath, ZIPARCHIVE::CREATE ) !== TRUE ) {
OC_Log::write('migration', 'Failed to create the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR);
return false;
} else {
return true;
}
}
/** /**
* @breif adds a directory to the zip object * @breif adds a directory to the zip object
* @param $dir string path of the directory to add * @param $dir string path of the directory to add
@ -235,7 +347,6 @@ class OC_Migrate{
if(!self::$uid){ if(!self::$uid){
OC_Log::write('migration','Tried to import without passing a uid',OC_Log::FATAL); OC_Log::write('migration','Tried to import without passing a uid',OC_Log::FATAL);
return false; return false;
exit();
} }
// Check if the db exists // Check if the db exists
@ -243,18 +354,15 @@ class OC_Migrate{
// Connect to the db // Connect to the db
if(!self::connectDB( $db )){ if(!self::connectDB( $db )){
return false; return false;
exit();
} }
} else { } else {
OC_Log::write('migration','Migration.db not found at: '.$db, OC_Log::FATAL ); OC_Log::write('migration','Migration.db not found at: '.$db, OC_Log::FATAL );
return false; return false;
exit();
} }
if( !is_array( $migrateinfo ) ){ if( !is_array( $migrateinfo ) ){
OC_Log::write('migration','$migrateinfo is not an array', OC_Log::FATAL); OC_Log::write('migration','$migrateinfo is not an array', OC_Log::FATAL);
return false; return false;
exit();
} }
// Set the user id // Set the user id