forked from UB-Mannheim/PalMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.php
139 lines (123 loc) · 4.19 KB
/
upload.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
<?php
// Copyright (C) 2014 Universitätsbibliothek Mannheim
// See file LICENSE for license details.
function trace($text) {
error_log("palma: $text");
}
if (file_exists('palma.ini')) {
// Get configuration from ini file.
$conf = parse_ini_file("palma.ini", true);
$url = $conf['path']['control_file'];
$targetPath = $conf['path']['upload_dir'];
} else {
// Guess configuration from global PHP variables.
// TODO: There is no HTTP_REFERER, basename is wrong, so ini file is still needed.
$url = dirname($_SERVER['HTTP_REFERER']) . '/' .
basename($_SERVER['PHP_SELF']);
$targetPath = dirname(__FILE__) . '/uploads';
trace("url=$url");
}
if (empty($_FILES)) {
$error = 99;
$filename = 'unknown';
} else {
$error = $_FILES['file']['error'];
$filename = $_FILES['file']['name'];
}
if (!is_dir($targetPath)) {
/* Target directory is missing, so create it now. */
mkdir($targetPath, 0755);
}
if ($error == UPLOAD_ERR_OK) {
# All uploaded files are collected in the upload directory.
# If necessary, an index is added to get a unique filename.
$tempFile = $_FILES['file']['tmp_name'];
$targetFile = "$targetPath/$filename";
$index = 0;
$fparts = pathinfo($filename);
$fname = $fparts['filename'];
$ftype = null;
if (isset($fparts['extension'])) {
$ftype = $fparts['extension'];
}
while (file_exists($targetFile)) {
$index++;
if ($ftype) {
$targetFile = "$targetPath/$fname-$index.$ftype";
} else {
$targetFile = "$targetPath/$fname-$index";
}
}
trace("upload '$tempFile' to '$targetFile'");
move_uploaded_file($tempFile, $targetFile);
} else {
// Support localisation.
require_once('gettext.php');
$targetFile = "$targetPath/error.html";
$f = fopen($targetFile, 'w');
if ($f) {
switch ($error) {
case UPLOAD_ERR_INI_SIZE:
$message = _("This file is too large.");
break;
case UPLOAD_ERR_FORM_SIZE:
$message = _("Large files are not supported.");
break;
case UPLOAD_ERR_PARTIAL:
$message = _("File was only partially uploaded.");
break;
default:
$message = sprintf(_("Error code %s."), $error);
break;
}
fprintf($f, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"");
fprintf($f, "\"http://www.w3.org/TR/html4/strict.dtd\">");
fprintf($f, "<html>\n");
fprintf($f, "<head>\n");
fprintf($f, "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n");
fprintf($f, "<title>Error</title>\n");
fprintf($f, "</head>\n");
fprintf($f, "<body>\n");
fprintf($f, "<p>\n");
fprintf($f, _("File '%s' cannot be shown.") . "<br>\n%s\n",
$filename, $message);
fprintf($f, "</p>\n");
fprintf($f, "</body>\n");
fprintf($f, "</html>\n");
fclose($f);
}
$targetFile = "file:///$targetFile";
}
// Get information of application for uploaded file.
require_once ('FileHandler.class.php');
$handler = FileHandler::getFileHandler($targetFile);
// create window object and send to nuc
$dt = new DateTime();
$date = $dt->format('Y-m-d H:i:s');
$window = array(
"id" => "",
"win_id" => "",
"name" => "",
"state" => "",
"file" => $targetFile,
"handler" => $handler,
"userid" => "",
"date" => $date
);
//echo "<body onLoad=\"sendToNuc('newWindow=".serialize($window)."')\" /></body>";
$serializedWindow = serialize($window);
$sw = urlencode($serializedWindow);
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url . '?newWindow=' . $sw,
CURLOPT_USERAGENT => 'PalMA cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
trace("upload closed, result='$resp'");
?>