-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport.php
72 lines (63 loc) · 2.1 KB
/
export.php
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
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
set_error_handler(function ($severity, $message, $file, $line) {
if (error_reporting() & $severity) {
throw new ErrorException($message, 0, $severity, $file, $line);
}
});
function writeCsv($query, $csvPath)
{
if (is_file($csvPath)) {
return;
}
$csv = fopen($csvPath, 'w');
ob_start(function ($buffer) use ($csv) {
fwrite($csv, $buffer);
return '';
});
$_SERVER['QUERY_STRING'] = rawurlencode("$query");
require(__DIR__ . '/csv.php');
ob_end_flush();
fclose($csv);
}
$dir = sys_get_temp_dir() . '/' . uniqid('export-', true);
mkdir($dir);
foreach (glob(__DIR__ . '/*-dashboards/*.weave') as $dashboard) {
$relativePath = basename(dirname($dashboard)) . '/' . basename($dashboard);
$newPath = $dir . '/' . $relativePath;
if (! is_dir(dirname($newPath))) {
mkdir(dirname($newPath));
}
copy($dashboard, $newPath);
$zip = new ZipArchive();
$zip->open($newPath);
$json = json_decode($zip->getFromName('weave-json/history.json'), true);
foreach ($json['currentState'] as & $entry) {
if ('weavejs.data.source.CSVDataSource' !== $entry['className']) {
continue;
}
$url = $entry['sessionState']['url'];
if (strpos($url, 'csv.php?') !== 0) {
continue;
}
$query = substr($url, 8);
$csvPath = $dir . '/' . md5($query) . '.csv';
writeCsv($query, $csvPath);
$entry['sessionState']['url'] = basename($csvPath);
}
$zip->addFromString('weave-json/history.json', json_encode($json));
$zip->close();
}
copy(__DIR__ . '/weave.html', "$dir/weave.html");
exec('cp -R ' . escapeshellarg(__DIR__ . '/weave') . ' ' . escapeshellarg("$dir/weave"));
ob_start();
$statisticsOnly = true;
require(__DIR__ . '/index.php');
$indexHtml = ob_get_clean();
file_put_contents("$dir/index.html", $indexHtml);
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename=export.zip');
chdir($dir);
passthru('zip -qr - .');
exec('rm -Rf ' . escapeshellarg($dir));