-
Notifications
You must be signed in to change notification settings - Fork 173
/
FileUpload.php
473 lines (421 loc) · 13.6 KB
/
FileUpload.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<?php
/**
* Media uploader.
*
* Handles media upload and update actions received from a front-end ajax call
*
* PHP Version 7
*
* @category Loris
* @package Media
* @author Alex I. <ailea.mcin@gmail.com>
* @license Loris license
* @link https://github.com/aces/Loris-Trunk
*/
if (isset($_GET['action'])) {
$action = $_GET['action'];
if ($action == "getData") {
viewData();
} else if ($action == "upload") {
uploadFile();
} else if ($action == "edit") {
editFile();
}
}
/**
* Handles the media update/edit process
*
* @throws DatabaseException
*
* @return void
*/
function editFile()
{
$db = \NDB_Factory::singleton()->database();
$user =& User::singleton();
if (!$user->hasPermission('media_write')) {
showMediaError("Permission Denied", 403);
exit;
}
// Read JSON from STDIN
$stdin = file_get_contents('php://input');
$req = json_decode($stdin, true);
if (!is_array($req)) {
throw new Exception("Invalid JSON");
}
$idMediaFile = $req['idMediaFile'] ?? '';
if (!$idMediaFile) {
showMediaError("Media ID $idMediaFile not found", 404);
}
$dateTaken = $req['dateTaken'];
checkDateTaken($dateTaken);
$updateValues = [
'date_taken' => $dateTaken,
'comments' => $req['comments'],
'language_id' => $req['language'],
'hide_file' => $req['hideFile'] ? $req['hideFile'] : 0,
];
try {
$db->update('media', $updateValues, ['id' => $idMediaFile]);
} catch (DatabaseException $e) {
showMediaError("Could not update the file. Please try again!", 500);
}
}
/**
* Handles the media upload process
*
* @throws DatabaseException
*
* @return void
*/
function uploadFile()
{
$uploadNotifier = new NDB_Notifier(
"media",
"upload"
);
$db = \NDB_Factory::singleton()->database();
$config = NDB_Config::singleton();
$user =& User::singleton();
if (!$user->hasPermission('media_write')) {
showMediaError("Permission Denied", 403);
exit;
}
// Validate media path and destination folder
$mediaPath = $config->getSetting('mediaPath');
if (!isset($mediaPath)) {
showMediaError(
"Media path not set in LORIS settings! "
. "Please contact your LORIS administrator",
500
);
}
if (!file_exists($mediaPath)) {
showMediaError("Error! The upload folder '$mediaPath' does not exist!", 404);
}
// Process posted data
$pscid = isset($_POST['pscid']) ? $_POST['pscid'] : null;
$visit = isset($_POST['visitLabel']) ? $_POST['visitLabel'] : null;
$instrument = isset($_POST['instrument']) ? $_POST['instrument'] : null;
$dateTaken = isset($_POST['dateTaken']) ? $_POST['dateTaken'] : null;
$comments = isset($_POST['comments']) ? $_POST['comments'] : null;
$language = isset($_POST['language']) ? $_POST['language'] : null;
// If required fields are not set, show an error
if (empty($_FILES)) {
echo showMediaError(
"File could not be uploaded successfully.
Please contact the administrator.",
400
);
}
if (!isset($pscid, $visit)) {
echo showMediaError("Please fill in all required fields!", 400);
return;
}
checkDateTaken($dateTaken);
$fileName = preg_replace('/\s/', '_', $_FILES["file"]["name"]);
// urldecode() necessary to decode double quotes encoded automatically
// by chrome browsers to avoid XSS attacks
$fileName = urldecode($fileName);
$fileType = $_FILES["file"]["type"];
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
if (empty($extension)) {
$response = showMediaError(
"Please make sure your file has a valid extension!",
400,
);
print $response;
return;
}
$userID = $user->getUsername();
$sessionID = $db->pselectOne(
"SELECT s.ID as session_id FROM candidate c " .
"LEFT JOIN session s USING(CandID) WHERE c.PSCID = :v_pscid AND " .
"s.Visit_label = :v_visit_label",
[
'v_pscid' => $pscid,
'v_visit_label' => $visit,
]
);
if (!isset($sessionID) || strlen($sessionID) < 1) {
echo showMediaError(
"Error! A session does not exist for candidate '$pscid'' " .
"and visit label '$visit'.",
404
);
return;
}
// Build insert query
$query = [
'session_id' => $sessionID,
'instrument' => $instrument,
'date_taken' => $dateTaken,
'comments' => $comments,
'file_name' => $fileName,
'file_type' => $fileType,
'data_dir' => $mediaPath,
'uploaded_by' => $userID,
'hide_file' => 0,
'last_modified' => date("Y-m-d H:i:s"),
'language_id' => $language,
];
if (move_uploaded_file($_FILES["file"]["tmp_name"], $mediaPath . $fileName)) {
try {
// Insert or override db record if file_name already exists
$db->unsafeInsertOnDuplicateUpdate('media', $query);
$uploadNotifier->notify(["file" => $fileName]);
$qparam = ['ID' => $sessionID];
$result = $db->pselect(
'SELECT ID, CandID, CenterID, ProjectID, Visit_label
from session
where ID=:ID',
$qparam
)[0];
echo json_encode(
[
'full_name' => $fileName,
'pscid' => $pscid,
'visit_label' => $result['ProjectID'],
'language' => $language,
'instrument' => $instrument,
'site' => $result['CenterID'],
'project' => $result['ProjectID'],
'uploaded_by' => $userID,
'date_taken' => $dateTaken,
'comments' => $comments,
'last_modified' => date("Y-m-d H:i:s"),
'file_type' => $fileType,
'CandID' => $result['CandID'],
'SessionID' => $sessionID,
'fileVisibility' => 0,
]
);
} catch (DatabaseException $e) {
echo showMediaError("Could not upload the file. Please try again!", 500);
}
} else {
echo showMediaError("Could not upload the file. Please try again!", 500);
}
}
/**
* Handles the media view data process
*
* @return void
*/
function viewData()
{
$user =& User::singleton();
if (!$user->hasPermission('media_read')) {
echo showMediaError("Permission denied", 403);
return;
}
echo json_encode(getUploadFields());
}
/**
* Returns a list of fields from database
*
* @return array
* @throws DatabaseException
*/
function getUploadFields()
{
$db = \NDB_Factory::singleton()->database();
$user = \User::singleton();
$config = \NDB_Config::singleton();
$lorisinstance = new \LORIS\LorisInstance(
$db,
$config,
[
__DIR__ . "/../../../project/modules",
__DIR__ . "/../../",
],
);
// Select only candidates that have had visit at user's sites
$qparam = [];
$sessionQuery = "SELECT
c.PSCID, s.Visit_label, s.CenterID, f.Test_name, tn.Full_name
FROM candidate c
LEFT JOIN session s USING (CandID)
LEFT JOIN flag f ON (s.ID=f.SessionID)
LEFT JOIN test_names tn ON (f.Test_name=tn.Test_name)";
if (!$user->hasPermission('access_all_profiles')) {
$sessionQuery .= " WHERE FIND_IN_SET(s.CenterID, :cid) ORDER BY c.PSCID ASC";
$qparam['cid'] = implode(",", $user->getCenterIDs());
} else {
$sessionQuery .= " ORDER BY c.PSCID ASC";
}
$sessionRecords = $db->pselect(
$sessionQuery,
$qparam
);
$instrumentsList = toSelect($sessionRecords, "Test_name", null);
$candidatesList = toSelect($sessionRecords, "PSCID", null);
$visitList = Utility::getVisitList();
$languageList = Utility::getLanguageList();
$startYear = $config->getSetting('startYear');
$endYear = $config->getSetting('endYear');
$allInstruments = \NDB_BVL_Instrument::getInstrumentNamesList($lorisinstance);
// Build array of session data to be used in upload media dropdowns
$sessionData = [];
foreach ($sessionRecords as $record) {
// Populate visits
if (!isset($sessionData[$record["PSCID"]]['visits'])) {
$sessionData[$record["PSCID"]]['visits'] = [];
}
if ($record["Visit_label"] !== null && !in_array(
$record["Visit_label"],
$sessionData[$record["PSCID"]]['visits'],
true
)
) {
$sessionData[$record["PSCID"]]['visits'][$record["Visit_label"]]
= $record["Visit_label"];
}
// Populate instruments
$visit = $record["Visit_label"];
$pscid = $record["PSCID"];
if (!isset($sessionData[$pscid]['instruments'][$visit])) {
$sessionData[$pscid]['instruments'][$visit] = [];
}
if (!isset($sessionData[$pscid]['instruments']['all'])) {
$sessionData[$pscid]['instruments']['all'] = [];
}
if ($record["Test_name"] !== null
&& !in_array(
$record["Test_name"],
$sessionData[$pscid]['instruments'][$visit] ?? [],
true
)
) {
$testname = $record["Test_name"];
$instrumentName = $allInstruments[$testname] ?: $testname;
$sessionData[$pscid]['instruments'][$visit][$testname]
= $instrumentName;
if (!in_array(
$record["Test_name"],
$sessionData[$pscid]['instruments']['all'],
true
)
) {
$sessionData[$pscid]['instruments']['all'][$record["Test_name"]]
= $instrumentName;
}
}
}
// Build media data to be displayed when editing a media file
$mediaData = null;
if (isset($_GET['idMediaFile'])) {
$idMediaFile = $_GET['idMediaFile'];
$mediaData = $db->pselectRow(
"SELECT " .
"m.session_id as sessionID, " .
"c.PSCID as pscid, " .
"Visit_label as visitLabel, " .
"instrument, " .
"CenterID as forSite, " .
"date_taken as dateTaken, " .
"comments, " .
"file_name as fileName, " .
"hide_file as hideFile, " .
"language_id as language," .
"m.id FROM media m LEFT JOIN session s ON m.session_id = s.ID
LEFT JOIN candidate c ON (c.CandID=s.CandID) " .
"WHERE m.id = :mediaId",
['mediaId' => $idMediaFile]
);
}
$result = [
'candidates' => $candidatesList,
'visits' => $visitList,
'instruments' => $instrumentsList,
'mediaData' => $mediaData,
'mediaFiles' => array_values(getFilesList()),
'sessionData' => $sessionData,
'language' => $languageList,
'startYear' => $startYear,
'endYear' => $endYear,
];
return $result;
}
/**
* Utility function to return errors from the server
*
* @param string $message error message to display
* @param int $code The HTTP response code to
* use with the message
*
* @return string
*/
function showMediaError($message, $code) : string
{
if (!isset($message)) {
$message = 'An unknown error occurred!';
}
http_response_code($code);
header('Content-Type: application/json; charset=UTF-8');
return json_encode(['message' => $message]);
}
/**
* Utility function to convert data from database to a
* (select) dropdown friendly format
*
* @param array $options array of options
* @param string $item key
* @param ?string $item2 value
*
* @return array
*/
function toSelect($options, $item, $item2)
{
$selectOptions = [];
$optionsValue = $item;
if (isset($item2)) {
$optionsValue = $item2;
}
foreach (array_keys($options) as $key) {
$selectOptions[$options[$key][$optionsValue]] = $options[$key][$item];
}
return $selectOptions;
}
/**
* Returns an array of (id, file_name) pairs from media table
*
* @return array
* @throws DatabaseException
*/
function getFilesList()
{
$db = \NDB_Factory::singleton()->database();
$fileList = $db->pselect("SELECT id, file_name FROM media", []);
$mediaFiles = [];
foreach ($fileList as $row) {
$mediaFiles[$row['id']] = $row['file_name'];
}
return $mediaFiles;
}
/**
* Checks that a date is not in the future.
*
* @param string $dateTaken The date (in YYYY-MM-DD
* format) to check)
*
* @return void (but may terminate as a side-effect)
*/
function checkDateTaken($dateTaken)
{
if (!empty($dateTaken)) {
$date = date_create_from_format("Y-m-d", $dateTaken);
if ($date === false) {
echo showMediaError("Invalid date: $dateTaken", 400);
return;
}
$now = new DateTime();
$diff = intval(date_diff($date, $now)->format("%R%a"));
if ($diff < 0) {
echo showMediaError(
"Date of administration cannot be in the future",
400,
);
}
}
}