Passed
Push — master ( 5c87ed...e4bb53 )
by Stefan
04:05
created

getObjectFromDB()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.439
c 0
b 0
f 0
cc 5
eloc 21
nc 5
nop 1
1
<?php
2
/* 
3
 *******************************************************************************
4
 * Copyright 2011-2017 DANTE Ltd. and GÉANT on behalf of the GN3, GN3+, GN4-1 
5
 * and GN4-2 consortia
6
 *
7
 * License: see the web/copyright.php file in the file structure
8
 *******************************************************************************
9
 */
10
?>
11
<?php
12
13
require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/config/_config.php");
14
15
/**
16
 * retrieves a binary object from the database and pushes it out to the browser
17
 * @param string $id
18
 * @return void in case of error - otherwise, sends the content directly to browser and never returns
19
 */
20
function getObjectFromDB($id) {
21
22
    // check if data is public for this blob call
23
    
24
    $blob = \web\lib\admin\UIElements::getBlobFromDB($id, TRUE);
25
    $finalBlob = base64_decode($blob);
0 ignored issues
show
Bug introduced by
$blob of type false is incompatible with the type string expected by parameter $data of base64_decode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
    $finalBlob = base64_decode(/** @scrutinizer ignore-type */ $blob);
Loading history...
26
27
    if ($finalBlob === FALSE) {
28
        return;
29
    }
30
31
    // Set data type and caching for 30 days
32
    $info = new finfo();
33
    $filetype = $info->buffer($finalBlob, FILEINFO_MIME_TYPE);
34
    header("Content-type: " . $filetype);
35
36
    switch ($filetype) {
37
        case "text/rtf": // fall-through, same treatment
38
        case "application/rtf":
39
            header("Content-Disposition: attachment; filename='download.rtf'");
40
            break;
41
        case "text/plain":
42
            header("Content-Disposition: attachment; filename='download.txt'");
43
            break;
44
        default:
45
            // do nothing special with the Content-Disposition header
46
    }
47
48
    header("Cache-Control: must-revalidate");
49
    $offset = 60 * 60 * 24 * 30;
50
    // gmdate can't possibly fail, because it operates on time() and an integer offset
51
    
52
    $ExpStr = "Expires: " . /** @scrutinizer ignore-type */ gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
53
    header($ExpStr);
54
55
    //  Print out the image
56
    echo $finalBlob;
57
}
58
59
$validator = new \web\lib\common\InputValidation();
60
if (isset($_GET["id"]) && $validator->databaseReference($_GET["id"])) {
61
    getObjectFromDB($_GET["id"]);
62
} else {
63
    echo "No valid ID";
64
}
65