forked from elabftw/elabftw
/
make_zip.php
207 lines (190 loc) · 9.12 KB
/
make_zip.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/********************************************************************************
* *
* Copyright 2012 Nicolas CARPi (nicolas.carpi@gmail.com) *
* http://www.elabftw.net/ *
* *
********************************************************************************/
/********************************************************************************
* This file is part of eLabFTW. *
* *
* eLabFTW is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* eLabFTW 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 eLabFTW. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
require_once 'inc/common.php';
require_once ELAB_ROOT . 'inc/locale.php';
$page_title = _('Make zip archive');
$selected_menu = null;
require_once ELAB_ROOT . 'inc/head.php';
require_once ELAB_ROOT . 'inc/info_box.php';
require_once ELAB_ROOT . 'vendor/autoload.php';
// Test if there is zip
if (!class_exists('ZipArchive')) {
die("<p>You are missing the ZipArchive class in php. Uncomment the line extension=zip.so in /etc/php/php.ini.</p>");
}
// init some var
$zdate = '';
$clean_title = '';
$files_to_delete = array();
// Switch exp/items just for the table to search in sql requests
if ($_GET['type'] === 'experiments') {
$table = 'experiments';
} elseif ($_GET['type'] === 'items') {
$table = 'items';
} else {
die(_("The type parameter is not valid."));
}
// CREATE URL
$url = 'https://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'];
// Check id is valid and assign it to $id
if (isset($_GET['id']) && !empty($_GET['id'])) {
$id_arr = explode(" ", $_GET['id']);
// BEGIN ZIP
// name of the downloadable file
$zipname = kdate() . ".export.elabftw";
$zipfile = 'uploads/export/' . $zipname . "-" . hash("sha512", uniqid(rand(), true)) . ".zip";
$zip = new ZipArchive;
$res = $zip->open($zipfile, ZipArchive::CREATE);
if ($res === true) {
foreach ($id_arr as $id) {
// MAIN LOOP
////////////////
// SQL to get info on the item we are zipping
if ($table == 'experiments') {
$sql = "SELECT * FROM experiments WHERE id = :id LIMIT 1";
} else {
$sql = "SELECT items.*,
items_types.name AS items_typesname
FROM items
LEFT JOIN items_types ON (items.type = items_types.id)
WHERE items.id = :id LIMIT 1";
}
$req = $pdo->prepare($sql);
$req->bindParam(':id', $id, PDO::PARAM_INT);
$req->execute();
$zipped = $req->fetch();
$title = stripslashes($zipped['title']);
// make a title without special char for folder inside .zip
$clean_title = preg_replace('/[^A-Za-z0-9]/', '_', $title);
$zdate = $zipped['date'];
// name of the folder
// folder begin with date for experiments
if ($table == 'experiments') {
$folder = $zdate . "-" . $clean_title;
} else { // items
$itemtype = $zipped['items_typesname'];
$folder = $itemtype . " - " . $clean_title;
}
$body = stripslashes($zipped['body']);
$req->closeCursor();
// SQL to get firstname + lastname
$sql = "SELECT firstname,lastname FROM users WHERE userid = " . $_SESSION['userid'];
$req = $pdo->prepare($sql);
$req->execute();
$users = $req->fetch();
$firstname = $users['firstname'];
$lastname = $users['lastname'];
// SQL to get tags
$sql = "SELECT tag FROM " . $table . "_tags WHERE item_id = $id";
$req = $pdo->prepare($sql);
$req->execute();
$tags = null;
while ($data = $req->fetch()) {
$tags .= stripslashes($data['tag']) . ' ';
}
// SQL to get filesattached (of the right type)
$sql = "SELECT * FROM uploads WHERE item_id = :id AND type = :type";
$req = $pdo->prepare($sql);
$req->bindParam(':id', $id);
$req->bindParam(':type', $table);
$req->execute();
$real_name = array();
$long_name = array();
$comment = array();
while ($uploads = $req->fetch()) {
$real_name[] = $uploads['real_name'];
$long_name[] = $uploads['long_name'];
$comment[] = $uploads['comment'];
}
// files attached ?
$filenb = count($real_name);
if ($filenb > 0) {
for ($i = 0; $i < $filenb; $i++) {
// add files to archive
$zip->addFile('uploads/' . $long_name[$i], $folder . "/" . $real_name[$i]);
}
}
// add PDF to archive
$pdf = new \Elabftw\Elabftw\MakePdf($id, $table);
$mpdf = new mPDF();
$mpdf->SetAuthor($pdf->author);
$mpdf->SetTitle($pdf->title);
$mpdf->SetSubject('eLabFTW pdf');
$mpdf->SetKeywords($pdf->tags);
$mpdf->SetCreator('www.elabftw.net');
$mpdf->WriteHTML($pdf->content);
$mpdf->Output($pdf->getPath(), 'F');
$zip->addFile($pdf->getPath(), $folder . '/' . $pdf->getFileName());
// add CSV file to archive
$csvpath = make_unique_csv($id, $table);
$zip->addFile($csvpath, $folder . "/" . $clean_title . ".csv");
// add the MANIFEST file that lists the files in archive
$manifest = "";
for ($i = 0; $i < $zip->numFiles; $i++) {
$manifest .= $zip->getNameIndex($i) . "\n";
}
// add info about the creator + timestamp
$manifest .= "Zip archive created by " . $users['firstname'] . " " . $users['lastname'] . " on " . date('Y.m.d') . " at " . date('H:i:s') . ".\n";
$manifest .= "~~~\neLabFTW - Free open source lab manager - http://www.elabftw.net\n";
// fix utf8
$manifest = utf8_encode($manifest);
$manifest = "\xEF\xBB\xBF" . $manifest;
$manifestpath = 'uploads/export/manifest-' . uniqid();
$tf = fopen($manifestpath, 'w+');
fwrite($tf, $manifest);
fclose($tf);
$zip->addFile($manifestpath, $folder . "/MANIFEST");
// add the path of the files to be deleted in the files_to_delete array
// (csv, MANIFEST and pdf)
$files_to_delete[] = $csvpath;
$files_to_delete[] = $manifestpath;
$files_to_delete[] = $pdf->getPath();
} // end foreach
// close the archive
$zip->close();
// now clean up
// we need to do that after $zip->close() or it doesn't work
foreach ($files_to_delete as $file) {
unlink($file);
}
// PAGE BEGIN
echo "<div class='well' style='margin-top:20px'>";
// Get the title if there is only one experiment in the zip
if (count($id_arr) === 1) {
$zipname = $zdate . "-" . $clean_title;
}
// Display download link (with attribute type=zip for download.php)
echo "<p>" . _('Your ZIP archive is ready:') . "<br>
<img src='img/download.png' alt='download' />
<a href='app/download.php?f=".$zipfile . "&name=" . $zipname . ".zip&type=zip' target='_blank'>
".$zipname . ".zip</a>
<span class='filesize'>(".format_bytes(filesize($zipfile)) . ")</span></p>";
} else {
echo sprintf(_("There was an unexpected problem! Please %sopen an issue on GitHub%s if you think this is a bug."), "<a href='https://github.com/elabftw/elabftw/issues/'>", "</a>");
}
echo "</div>";
require_once 'inc/footer.php';
} else {
die(_("The id parameter is not valid!"));
}