-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.php
215 lines (194 loc) · 9.06 KB
/
doc.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
<?php // -*- mode: php -*-
// doc -- HotCRP paper download page
// HotCRP is Copyright (c) 2006-2016 Eddie Kohler and Regents of the UC
// Distributed under an MIT-like license; see LICENSE
require_once("src/initweb.php");
function document_error($status, $msg) {
global $Conf;
header("HTTP/1.1 $status");
if (isset($_GET["fn"]))
json_exit(["ok" => false, "error" => $msg ? : "Internal error."]);
else {
$Conf->header("Download", null, actionBar());
$msg && Conf::msg_error($msg);
$Conf->footer();
exit;
}
}
// Determine the intended paper
function document_download() {
global $Conf, $Me;
$documentType = HotCRPDocument::parse_dtype(req("dt"));
if ($documentType === null)
$documentType = req("final") ? DTYPE_FINAL : DTYPE_SUBMISSION;
$attachment_filename = false;
$docid = null;
$filters = [];
if (isset($_GET["p"]))
$paperId = cvtint($_GET["p"]);
else if (isset($_GET["paperId"]))
$paperId = cvtint($_GET["paperId"]);
else {
$s = $orig_s = preg_replace(',\A/*,', "", Navigation::path());
$dtname = null;
$base_dtname = "paper";
if (str_starts_with($s, $Conf->download_prefix))
$s = substr($s, strlen($Conf->download_prefix));
if (preg_match(',\A(?:p|paper|)(\d+)/+(.*)\z,', $s, $m)) {
$paperId = intval($m[1]);
if (preg_match(',\A([^/]+)\.[^/]+\z,', $m[2], $mm))
$dtname = $mm[1];
else if (preg_match(',\A([^/]+)/+(.*)\z,', $m[2], $mm))
list($dtname, $attachment_filename) = array($m[1], $m[2]);
} else if (preg_match(',\A(p|paper|final|)(\d+)-?([-A-Za-z0-9_]*)(?:|\.[^/]+|/+(.*))\z,', $s, $m)) {
list($paperId, $dtname, $attachment_filename) = [intval($m[2]), $m[3], get($m, 4)];
if ($m[1] === "final")
$base_dtname = "final";
} else if (preg_match(',\A([A-Za-z_][-A-Za-z0-9_]*?)?-?(\d+)(?:|\.[^/]+|/+(.*))\z,', $s, $m))
list($paperId, $dtname, $attachment_filename) = [intval($m[2]), $m[1], get($m, 3)];
else if (preg_match(',\A([^/]+?)(?:|\.[^/]+|/+(.*)|)\z,', $s, $m))
list($paperId, $dtname, $attachment_filename) = [-2, $m[1], get($m, 2)];
$documentType = null;
while ($dtname !== null && $documentType === null) {
if ($paperId < 0)
$documentType = $Conf->paper_opts->match_nonpaper($dtname);
else
$documentType = HotCRPDocument::parse_dtype($dtname ? : $base_dtname);
if ($documentType !== null)
break;
$filter = null;
foreach (FileFilter::all_by_name() as $ff)
if (str_ends_with($dtname, "-" . $ff->name) || $dtname === $ff->name) {
$filter = $ff;
break;
}
if (!$filter)
break;
array_unshift($filters, $filter);
$dtname = substr($dtname, 0, strlen($dtname) - strlen($ff->name));
if (str_ends_with($dtname, "-"))
$dtname = substr($dtname, 0, strlen($dtname) - 1);
}
if (is_object($documentType))
$documentType = $documentType->id;
}
if (isset($_GET["filter"])) {
foreach (explode(" ", $_GET["filter"]) as $filtername)
if ($filtername && ($filter = FileFilter::find_by_name($filtername)))
$filters[] = $filter;
}
if ($documentType === null
|| !($o = $Conf->paper_opts->find_document($documentType))
|| ($attachment_filename && !$o->has_attachments())
|| $o->nonpaper !== ($paperId < 0))
document_error("404 Not Found", "Unknown document “" . htmlspecialchars($orig_s) . "”.");
if ($o->nonpaper) {
$prow = new PaperInfo(["paperId" => -2], null, $Conf);
if (($o->visibility === "admin" && !$Me->privChair)
|| ($o->visibility !== "all" && !$Me->isPC))
document_error("403 Forbidden", "You don’t have permission to view this document.");
} else {
$prow = $Conf->paperRow($paperId, $Me, $whyNot);
if (!$prow)
document_error("404 Not Found", whyNotText($whyNot, "view"));
else if (($whyNot = $Me->perm_view_pdf($prow)))
document_error("403 Forbidden", whyNotText($whyNot, "view"));
else if ($documentType > 0
&& !$Me->can_view_paper_option($prow, $documentType, true))
document_error("403 Forbidden", "You don’t have permission to view this document.");
}
// history
if (isset($_GET["fn"]) && $_GET["fn"] === "history") {
$docs = [];
if ($o->has_attachments()) {
if (($oa = $prow->option($documentType)))
$docs = $oa->documents($prow);
} else if (($doc = $prow->document($documentType, 0, true)))
$docs = [$doc];
$pjs = $actives = [];
foreach ($docs as $doc) {
$pj = ["sha1" => Filer::text_sha1($doc), "at" => (int) $doc->timestamp, "mimetype" => $doc->mimetype];
if ($doc->size !== null)
$pj["size"] = (int) $doc->size;
if ($doc->filename)
$pj["filename"] = $doc->filename;
$pj["active"] = true;
$actives[$doc->paperStorageId] = true;
$pjs[] = $pj;
}
if ($Me->can_view_document_history($prow)) {
$result = $Conf->qe("select paperStorageId, paperId, timestamp, mimetype, sha1, filename, infoJson, size from PaperStorage where paperId=? and documentType=? and filterType is null order by paperStorageId desc", $paperId, $documentType);
while (($row = edb_orow($result))) {
if (get($actives, $row->paperStorageId))
continue;
$pj = ["sha1" => Filer::text_sha1($row), "at" => (int) $row->timestamp, "mimetype" => $row->mimetype];
if ($row->size !== null)
$pj["size"] = (int) $row->size;
if ($row->filename)
$pj["filename"] = $row->filename;
$pjs[] = $pj;
}
}
json_exit(["ok" => true, "result" => $pjs]);
}
$want_docid = $request_docid = 0;
if (isset($_GET["version"])) {
$version_sha1 = Filer::binary_sha1($_GET["version"]);
if (!$version_sha1)
document_error("404 Not Found", "No such version.");
$want_docid = $Conf->fetch_ivalue("select max(paperStorageId) from PaperStorage where paperId=? and documentType=? and sha1=? and filterType is null", $paperId, $documentType, $version_sha1);
if ($want_docid !== null && $Me->can_view_document_history($prow))
$request_docid = $want_docid;
}
$doc = null;
if ($attachment_filename) {
$oa = $prow->option($documentType);
foreach ($oa ? $oa->documents($prow) : array() as $xdoc)
if ($xdoc->unique_filename == $attachment_filename)
$doc = $xdoc;
} else
$doc = $prow->document($documentType, $request_docid);
if ($want_docid !== 0 && (!$doc || $doc->paperStorageId != $want_docid))
document_error("404 Not Found", "No such version.");
else if (!$doc)
document_error("404 Not Found", "No such " . ($attachment_filename ? "attachment" : "document") . " “" . htmlspecialchars($orig_s) . "”.");
// pass through filters
foreach ($filters as $filter)
$doc = $filter->apply($doc, $prow) ? : $doc;
// check for contents request
if (isset($_GET["fn"]) && ($_GET["fn"] === "listing" || $_GET["fn"] === "consolidatedlisting")) {
if (!$doc->docclass->is_archive($doc))
json_exit(["ok" => false, "error" => "That file is not an archive."]);
else if (($listing = $doc->docclass->archive_listing($doc)) === false)
json_exit(["ok" => false, "error" => isset($doc->error) ? $doc->error_text : "Internal error."]);
else {
$listing = $doc->docclass->clean_archive_listing($listing);
if ($_GET["fn"] == "consolidatedlisting")
$listing = join(", ", $doc->docclass->consolidate_archive_listing($listing));
json_exit(["ok" => true, "result" => $listing]);
}
}
// check for If-Not-Modified
if ($doc->sha1) {
$ifnonematch = null;
if (function_exists("getallheaders")) {
foreach (getallheaders() as $k => $v)
if (strcasecmp($k, "If-None-Match") == 0)
$ifnonematch = $v;
} else
$ifnonematch = get($_SERVER, "HTTP_IF_NONE_MATCH");
if ($ifnonematch && $ifnonematch === "\"" . Filer::text_sha1($doc) . "\"") {
header("HTTP/1.1 304 Not Modified");
exit;
}
}
// Actually download paper.
session_write_close(); // to allow concurrent clicks
$opts = ["attachment" => cvtint(req("save")) > 0];
if ($doc->sha1 && ($x = req("sha1")) && $x === Filer::text_sha1($doc))
$opts["cacheable"] = true;
if ($Conf->download_documents([$doc], $opts))
exit;
document_error("500 Server Error", null);
}
document_download();