download.php 4.23 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage export
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
12
13
 *
 */

define('INTERNAL', 1);
require(dirname(dirname(__FILE__)) . '/init.php');
14
require_once(get_config('docroot') . '/lib/htmloutput.php');
15
16

// Download the export file if it's been generated
17
18
19
$downloadfile = param_variable('file', null);
if ($downloadfile) {
    $exportfile = get_config('dataroot') . 'export/' . $USER->get('id') . '/' . $downloadfile;
20
21
22
    $SESSION->set('exportdata', '');
    $SESSION->set('exportfile', '');
    require_once('file.php');
23
    serve_file($exportfile, basename($exportfile), 'application/x-zip', array('lifetime' => 0, 'forcedownload' => true));
24
25
26
    exit;
}

27
28
29
30
if (function_exists('apache_setenv')) {
    // Turn off gzip if it's on, because it prevents output from being flushed
    apache_setenv('no-gzip', 1);
}
31

32
if (!$exportdata = $SESSION->get('exportdata')) {
33
    redirect('/export/index.php');
34
35
36
}
$SESSION->set('exportdata', '');

37
$stylesheets = array_reverse($THEME->get_url('style/style.css', true));
38
print_export_head($stylesheets);
39
flush();
40
41
42
43
44
45

/**
 * Outputs enough HTML to make a pretty error message in the iframe
 *
 * @param string $message The message to display to the user
 */
46
47
function export_iframe_die($message, $link=null) {
    print_export_iframe_die($message, $link);
48
49
50
51
    exit;
}

/**
Aaron Wells's avatar
Aaron Wells committed
52
 * Registered as the progress report handler for the export. Streams updates
53
54
55
56
57
58
 * back to the browser
 *
 * @param int $percent   How far complete the export is
 * @param string $status A human-readable string describing the current step
 */
function export_iframe_progress_handler($percent, $status) {
59
    print_iframe_progress_handler($percent, $status);
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    flush();
}


// Bail if we don't have enough data to do an export
if (!isset($exportdata['format'])
    || !isset($exportdata['what'])
    || !isset($exportdata['views'])) {
    export_iframe_die(get_string('unabletogenerateexport', 'export'));
    exit;
}

safe_require('export', $exportdata['format']);
$user = new User();
$user->find_by_id($USER->get('id'));
$class = generate_class_name('export', $exportdata['format']);

switch($exportdata['what']) {
case 'all':
79
    $exporter = new $class($user, PluginExport::EXPORT_ALL_VIEWS_COLLECTIONS, PluginExport::EXPORT_ALL_ARTEFACTS, 'export_iframe_progress_handler');
80
81
82
83
    break;
case 'views':
    $exporter = new $class($user, $exportdata['views'], PluginExport::EXPORT_ARTEFACTS_FOR_VIEWS, 'export_iframe_progress_handler');
    break;
84
case 'collections':
85
    $exporter = new $class($user, $exportdata['views'], PluginExport::EXPORT_LIST_OF_COLLECTIONS, 'export_iframe_progress_handler');
86
    break;
87
88
89
90
default:
    export_iframe_die(get_string('unabletoexportportfoliousingoptions', 'export'));
}

91
$exporter->includefeedback = $exportdata['includefeedback'];
92
93
94
95
96
97
// Get an estimate of how big the unzipped export file would be
// so we can check that we have enough disk space for it
$space = $exporter->is_diskspace_available();
if (!$space) {
    export_iframe_die(get_string('exportfiletoobig', 'mahara'), get_config('wwwroot') . 'view/index.php');
}
98

99
100
101
try {
    $zipfile = $exporter->export();
} catch (SystemException $e) {
102
    export_iframe_die($e->getMessage(), get_config('wwwroot') . 'view/index.php');
103
}
104

Aaron Wells's avatar
Aaron Wells committed
105
106
// Store the filename in the session, and redirect the iframe to it to trigger
// the download. Here it would be nice to trigger the download for everyone,
107
108
// but alas this is not possible for people without javascript.
$SESSION->set('exportfile', $exporter->get('exportdir') . $zipfile);
109
$filepath = str_replace(get_config('dataroot') . 'export/' . $USER->get('id') . '/', '', $exporter->get('exportdir') . $zipfile);
110
$continueurl = 'download.php';
111
$continueurljs = get_config('wwwroot') . 'export/index.php';
112
113
114
115
116
117
118
119
$result = $SESSION->get('messages');
if (empty($result)) {
    $strexport   = get_string('exportgeneratedsuccessfully1', 'export');
}
else {
    $SESSION->clear('messages');
    $strexport   = get_string('exportgeneratedwitherrors', 'export');
}
120
print_export_footer($strexport, $continueurl, $continueurljs, $result, get_config('wwwroot') . 'export/download.php?file=' . $filepath);