forked from elabftw/elabftw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.php
More file actions
259 lines (220 loc) · 9.19 KB
/
Copy pathdelete.php
File metadata and controls
259 lines (220 loc) · 9.19 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?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/>. *
* *
********************************************************************************/
// delete.php
// This page is called with POST requests containing an id and a type.
require_once 'inc/common.php';
// Check id is valid and assign it to $id
if (isset($_POST['id']) && is_pos_int($_POST['id'])) {
$id = $_POST['id'];
} else {
die();
}
// Item switch
if (isset($_POST['type']) && !empty($_POST['type'])) {
switch ($_POST['type']) {
// EXPERIMENTS
case 'exp':
// check if we can delete experiments
if (((get_config('deletable_xp') == '0') &&
!$_SESSION['is_admin']) ||
!is_owned_by_user($id, 'experiments', $_SESSION['userid'])) {
$msg_arr[] = "You don't have the rights to delete this experiments.";
$_SESSION['errors'] = $msg_arr;
die();
} else {
// delete the experiment
$sql = "DELETE FROM experiments WHERE id = :id";
$req = $pdo->prepare($sql);
$req->execute(array(
'id' => $id
));
// delete associated tags
$sql = "DELETE FROM experiments_tags WHERE item_id = :id";
$req = $pdo->prepare($sql);
$req->execute(array(
'id' => $id
));
// delete associated files
$sql = "DELETE FROM uploads WHERE item_id = :id AND type = :type";
$req = $pdo->prepare($sql);
$req->execute(array(
'id' => $id,
'type' => 'exp'
));
// delete associated links
$delete_sql = "DELETE FROM experiments_links WHERE item_id = :item_id";
$delete_req = $pdo->prepare($delete_sql);
$result = $delete_req->execute(array(
'item_id' => $id
));
// delete associated experiments comments
$sql = "DELETE FROM experiments_comments WHERE exp_id = :id";
$req = $pdo->prepare($sql);
$req->execute(array(
'id' => $id
));
$msg_arr[] = "Experiment was deleted successfully.";
$_SESSION['infos'] = $msg_arr;
die();
}
break;
// DELETE EXPERIMENTS TEMPLATES
case 'tpl':
$delete_sql = "DELETE FROM experiments_templates WHERE id = :id";
$delete_req = $pdo->prepare($delete_sql);
$result = $delete_req->execute(array(
'id' => $id
));
if ($result) {
$msg_arr[] = "Template was deleted successfully.";
$_SESSION['infos'] = $msg_arr;
}
break;
// DELETE EXPERIMENT COMMENT
case 'expcomment':
// this is called by deleteThisAndReload
// it reloads part of the page, so no need to put session['infos']
$delete_sql = "DELETE FROM experiments_comments WHERE id = :id";
$delete_req = $pdo->prepare($delete_sql);
$result = $delete_req->execute(array(
'id' => $id
));
break;
// DELETE ITEM
case 'item':
// to store the outcome of sql
$result = array();
// delete the database item
$sql = "DELETE FROM items WHERE id = :id";
$req = $pdo->prepare($sql);
$result[] = $req->execute(array(
'id' => $id
));
// delete associated tags
$sql = "DELETE FROM items_tags WHERE item_id = :id";
$req = $pdo->prepare($sql);
$result[] = $req->execute(array(
'id' => $id
));
// delete associated files
$sql = "DELETE FROM uploads WHERE item_id = :id AND type = :type";
$req = $pdo->prepare($sql);
$result[] = $req->execute(array(
'id' => $id,
'type' => 'items'
));
// delete links of this item in experiments with this item linked
// get all experiments with that item linked
$sql = "SELECT id FROM experiments_links WHERE link_id = :link_id";
$req = $pdo->prepare($sql);
$result[] = $req->execute(array(
'link_id' => $id
));
while ($links = $req->fetch()) {
$delete_sql = "DELETE FROM experiments_links WHERE id=".$links['id'];
$delete_req = $pdo->prepare($delete_sql);
$result[] = $delete_req->execute();
}
// test if there was an error somewhere
if (in_array(false, $result)) {
$msg_arr[] = "There was an error during the deletion.";
$_SESSION['errors'] = $msg_arr;
} else {
$msg_arr[] = "Item was deleted successfully.";
$_SESSION['infos'] = $msg_arr;
}
break;
// DELETE ITEMS TYPES
case 'item_type':
$sql = "DELETE FROM items_types WHERE id = :id";
$req = $pdo->prepare($sql);
$result = $req->execute(array(
'id' => $id
));
if ($result) {
$msg_arr[] = "Item type was deleted successfully.";
$_SESSION['infos'] = $msg_arr;
}
break;
// DELETE LINKS
case 'link':
$result = false;
if (is_pos_int($_POST['item_id'])) {
$item_id = $_POST['item_id'];
} else {
die();
}
if (is_owned_by_user($item_id, 'experiments', $_SESSION['userid'])) {
$delete_sql = "DELETE FROM experiments_links WHERE id= :id";
$delete_req = $pdo->prepare($delete_sql);
$result = $delete_req->execute(array(
'id' => $id
));
}
if ($result) {
$msg_arr[] = "Item type was deleted successfully.";
$_SESSION['infos'] = $msg_arr;
}
break;
// DELETE TAGS
case 'exptag':
if (is_pos_int($_POST['item_id'])) {
$item_id = $_POST['item_id'];
} else {
die();
}
if (is_owned_by_user($item_id, 'experiments', $_SESSION['userid'])) {
$delete_sql = "DELETE FROM experiments_tags WHERE id = :id";
$delete_req = $pdo->prepare($delete_sql);
$delete_req->execute(array(
'id' => $id
));
}
break;
case 'itemtag':
$delete_sql = "DELETE FROM items_tags WHERE id = :id";
$delete_req = $pdo->prepare($delete_sql);
$delete_req->execute(array(
'id' => $id
));
break;
case 'status':
// normally there is no experiments left with this status
$delete_sql = "DELETE FROM status WHERE id = :id";
$delete_req = $pdo->prepare($delete_sql);
$delete_req->execute(array(
'id' => $id
));
break;
// END
default:
$err_flag = true;
}
} else {
$err_flag = true;
}
if (isset($err_flag)) {
die('Nope.');
}