forked from elabftw/elabftw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_zip.php
More file actions
161 lines (148 loc) · 6.95 KB
/
Copy pathmake_zip.php
File metadata and controls
161 lines (148 loc) · 6.95 KB
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
<?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 'inc/head.php';
$page_title = 'Make zip';
require_once 'inc/menu.php';
require_once 'inc/info_box.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>");
}
// 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('bad type');
}
// 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);
$date = $zipped['date'];
// name of the folder
// folder begin with date for experiments
if ($table == 'experiments') {
$folder = $date."-".$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
$pdfname = make_pdf($id, $table, 'uploads/export');
$zip->addFile('uploads/export/'.$pdfname, $folder."/".$pdfname);
// delete file
//unlink('uploads/export/'.$pdfname);
} // end foreach
$zip->close();
// PAGE BEGIN
echo "<div class='item'>";
// Get the title if there is only one experiment in the zip
if (count($id_arr) === 1) {
$zipname = $date."-".$clean_title;
}
// Display download link (with attribute type=zip for download.php)
echo "<p>Your ZIP archive is ready :<br />
<a href='download.php?f=".$zipfile."&name=".$zipname.".zip&type=zip' target='_blank'>
<img src='themes/".$_SESSION['prefs']['theme']."/img/download.png' alt='download' />
".$zipname.".zip</a>
<span class='filesize'>(".format_bytes(filesize($zipfile)).")</span></p>";
echo "</div>";
} else {
echo 'Archive creation failed :(';
}
require_once 'inc/footer.php';
} else {
die("The id parameter in the URL isn't a valid experiment ID");
}