nextcloud/apps/user_migrate/settings.php

96 lines
2.9 KiB
PHP
Raw Normal View History

<?php
/**
* ownCloud - user_migrate
*
* @author Thomas Schmidt
* @copyright 2011 Thomas Schmidt tom@opensuse.org
* @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::checkAppEnabled('user_migrate');
define('DS', '/');
if (isset($_POST['user_export'])) {
// Setup the export
$zip = new ZipArchive();
$tmp = get_temp_dir();
$user = OC_User::getUser();
2012-03-13 21:27:47 +04:00
$userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . $user;
$filename = $userdatadir . '/owncloud_export_' . $user . '_' . date("y-m-d_H-i-s") . ".zip";
OC_Log::write('user_migrate',"Creating export file at: " . $filename,OC_Log::INFO);
if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
exit("Cannot open <$filename>\n");
}
// Migrate the app info
2012-03-13 21:04:49 +04:00
$info = json_encode( OC_Migrate::export( $user ) );
2012-03-13 21:27:47 +04:00
$infofile = $userdatadir . '/exportinfo.json';
2012-03-13 21:08:20 +04:00
file_put_contents( $infofile, $info );
2012-03-13 21:08:20 +04:00
// Add the data dir (which includes migration.db and exportinfo.json)
2012-03-13 21:27:47 +04:00
zipAddDir( $userdatadir, $zip, true, "/" );
// Save the zip
$zip->close();
// Send the zip
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=" . basename($filename));
header("Content-Length: " . filesize($filename));
@ob_end_clean();
readfile($filename);
// Cleanup
unlink($filename);
2012-03-13 21:18:42 +04:00
OC_Migrate::cleanUp();
} if( isset( $_POST['user_import'] ) ){
// TODO
}else {
// fill template
$tmpl = new OC_Template('user_migrate', 'settings');
return $tmpl->fetchPage();
}
function zipAddDir($dir, $zip, $recursive=true, $internalDir='') {
$dirname = basename($dir);
$zip->addEmptyDir($internalDir . $dirname);
$internalDir.=$dirname.='/';
if ($dirhandle = opendir($dir)) {
while (false !== ( $file = readdir($dirhandle))) {
if (( $file != '.' ) && ( $file != '..' )) {
if (is_dir($dir . '/' . $file) && $recursive) {
zipAddDir($dir . '/' . $file, $zip, $recursive, $internalDir);
} elseif (is_file($dir . '/' . $file)) {
$zip->addFile($dir . '/' . $file, $internalDir . $file);
}
}
}
closedir($dirhandle);
} else {
OC_Log::write('user_migrate',"Was not able to open directory: " . $dir,OC_Log::ERROR);
}
}