Passed
Push — master ( 545bfd...abc97e )
by Stefan
02:21
created

upload.php (2 issues)

1
<?php
2
3
// Copyright (C) 2014-2015 Universitätsbibliothek Mannheim
4
// See file LICENSE for license details.
5
6
require_once('globals.php');
7
8
if (empty($_FILES)) {
9
    $error = 99;
10
    $filename = 'unknown';
11
} else {
12
    $error = $_FILES['file']['error'];
13
    $filename = $_FILES['file']['name'];
14
}
15
16
if (!is_dir(CONFIG_UPLOAD_DIR)) {
17
    /* Target directory is missing, so create it now. */
18
    mkdir(CONFIG_UPLOAD_DIR, 0755, true);
19
}
20
21
if ($error == UPLOAD_ERR_OK || $error == "downloaded_from_url") {
22
    # All uploaded files are collected in the upload directory.
23
    # If necessary, an index is added to get a unique filename.
24
    $tempFile = $_FILES['file']['tmp_name'];
25
    $targetFile = CONFIG_UPLOAD_DIR . "/$filename";
26
    $index = 0;
27
    $fparts = pathinfo($filename);
28
    $fname = $fparts['filename'];
29
    $ftype = null;
30
    if (isset($fparts['extension'])) {
31
        $ftype = $fparts['extension'];
32
    }
33
    while (file_exists($targetFile)) {
34
        $index++;
35
        if ($ftype) {
36
            $targetFile = CONFIG_UPLOAD_DIR . "/$fname-$index.$ftype";
37
        } else {
38
            $targetFile = CONFIG_UPLOAD_DIR . "/$fname-$index";
39
        }
40
    }
41
    trace("upload '$tempFile' to '$targetFile'");
42
    if (is_uploaded_file($tempFile)) {
43
        move_uploaded_file($tempFile, $targetFile);
44
    } elseif ($error == "downloaded_from_url") {
45
        rename($tempFile, $targetFile);
46
    } else {
47
        trace("upload failed!");
48
    }
49
} else {
50
    // Support localisation.
51
    require_once('i12n.php');
52
53
    $targetFile = CONFIG_UPLOAD_DIR . "/error.html";
54
    $f = fopen($targetFile, 'w');
55
    if ($f) {
0 ignored issues
show
$f is of type resource, thus it always evaluated to false.
Loading history...
56
        switch ($error) {
57
            case UPLOAD_ERR_INI_SIZE:
58
                $message = addslashes(__("This file is too large."));
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

58
                $message = addslashes(/** @scrutinizer ignore-call */ __("This file is too large."));
Loading history...
59
                break;
60
            case UPLOAD_ERR_FORM_SIZE:
61
                $message = addslashes(__("Large files are not supported."));
62
                break;
63
            case UPLOAD_ERR_PARTIAL:
64
                $message = addslashes(__("File was only partially uploaded."));
65
                break;
66
            default:
67
                $message = sprintf(addslashes(__("Error code %s.")), $error);
68
                break;
69
        }
70
        fprintf($f, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"");
71
        fprintf($f, "\"http://www.w3.org/TR/html4/strict.dtd\">");
72
        fprintf($f, "<html>\n");
73
        fprintf($f, "<head>\n");
74
        fprintf($f, "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n");
75
        fprintf($f, "<title>Error</title>\n");
76
        fprintf($f, "</head>\n");
77
        fprintf($f, "<body>\n");
78
        fprintf($f, "<p>\n");
79
        fprintf(
80
            $f,
81
            addslashes(__("File '%s' cannot be shown.")) . "<br>\n%s\n",
82
            $filename,
83
            $message
84
        );
85
        fprintf($f, "</p>\n");
86
        fprintf($f, "</body>\n");
87
        fprintf($f, "</html>\n");
88
        fclose($f);
89
    }
90
    $targetFile = "file:///$targetFile";
91
}
92
93
// create window object and send to nuc
94
95
$dt = new DateTime();
96
$date = $dt->format('Y-m-d H:i:s');
97
98
$window = array(
99
    "id" => "",
100
    "win_id" => "",
101
    "name" => "",
102
    "state" => "",
103
    "file" => $targetFile,
104
    "userid" => "",
105
    "date" => $date);
106
107
//echo "<body onLoad=\"sendToNuc('newWindow=".serialize($window)."')\" /></body>";
108
109
$serializedWindow = serialize($window);
110
111
$sw = urlencode($serializedWindow);
112
// Get cURL resource
113
$curl = curl_init();
114
// Set some options - we are passing in a useragent too here
115
curl_setopt_array($curl, array(
116
                      CURLOPT_RETURNTRANSFER => 1,
117
                      CURLOPT_URL => CONFIG_CONTROL_FILE . '?newWindow=' . $sw,
118
                      CURLOPT_USERAGENT => 'PalMA cURL Request'
119
                               ));
120
// Send the request & save response to $resp
121
$resp = curl_exec($curl);
122
// Close request to clear up some resources
123
curl_close($curl);
124
125
trace("upload closed, result='$resp'");
126