summaryrefslogtreecommitdiffstats
path: root/apps/user_migrate/settings.php
blob: c1121f2ddf5d5aef3819b01171ce6182e11a3453 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php

/**
 * ownCloud - user_migrate
 *
 * @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');


if (isset($_POST['user_migrate'])) {
	// Looks like they want to migrate
    $root = OC::$SERVERROOT . "/";
    $user = OC_User::getUser();
    $zip = new ZipArchive();
    $tempdir = get_temp_dir();
    $filename = $tempdir . "/" . $user . "_export_" . date("y-m-d_H-i-s") . ".zip";
    OC_Log::write('user_migrate',"Creating user export file at: " . $filename,OC_Log::INFO);
    if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
		exit("Cannot open <$filename>\n");
    }

	// Does the user want to include their files?
    if (isset($_POST['user_files'])) {
	    // needs to handle data outside of the default data dir.
		// adding user files
		OC_Log::write('user_migrate',"Adding owncloud user files of $user to export",OC_Log::INFO);
		zipAddDir($root . "data/" . $user, $zip, true, "files/");
    }
    
    // Does the user want their app data?
    if (isset($_POST['user_appdata'])) {
		// adding owncloud system files
		OC_Log::write('user_migrate',"Adding app data to user export file",OC_Log::INFO);
		// Call to OC_Migrate for the xml file.
		
		// Create migration.db
		OC_Migrate::export(OC_User::getUser());
		// Add export db to zip
		$zip->addFile($root.'data/'.$user.'/migration.db', "migration.db");
		
    }

    $zip->close();

    //header("Content-Type: application/zip");
    //header("Content-Disposition: attachment; filename=" . basename($filename));
    //header("Content-Length: " . filesize($filename));
    //readfile($filename);
    //unlink($filename);
    
} 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('admin_export',"Was not able to open directory: " . $dir,OC_Log::ERROR);
    }
}