Issues (25)

upload.php (2 issues)

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

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