-
Notifications
You must be signed in to change notification settings - Fork 20
/
article.php
292 lines (258 loc) · 9.43 KB
/
article.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
require 'common.php';
require 'lib/ThreadTree.php';
if (isset($_GET['article'])) {
$article = (int)$_GET['article'];
} else {
error("No article specified");
}
if (isset($_GET['group'])) {
$group = preg_replace('@[^A-Za-z0-9.-]@', '', $_GET['group']);
} else {
$group = false;
}
try {
$nntpClient = new \Web\News\Nntp($NNTP_HOST);
$message = $nntpClient->readArticle($article, $group);
if ($message === null) {
error('No article found');
}
$mail = \Flourish\Mailbox::parseMessage($message);
$rawReferences = [];
if (!empty($mail['headers']['references'])) {
$rawReferences = $mail['headers']['references'];
} elseif (!empty($mail['headers']['in-reply-to'])) {
$rawReferences = $mail['headers']['in-reply-to'];
}
$references = [];
foreach ($rawReferences as $ref) {
$matches = [];
if (preg_match_all('/\<(.*?)\>/', $ref, $matches)) {
foreach ($matches[0] as $match) {
$references[] = $match;
}
}
}
$refsResolved = [];
$refCount = 0;
foreach ($references as $messageId) {
if (!$messageId) {
continue;
}
if ($refCount >= REFERENCES_LIMIT) {
break;
}
$refsResolved[] = $nntpClient->xpath($messageId);
$refCount++;
}
} catch (Exception $e) {
error($e->getMessage());
}
head("{$group}: " . format_title($mail['headers']['subject'], 'utf-8'));
echo '<nav class="secondary-nav">';
echo ' <ul class="breadcrumbs">';
echo ' <li class="breadcrumbs-item"><a class="breadcrumbs-item-link" href="/">PHP Mailing Lists</a></li>';
echo ' <li class="breadcrumbs-item"><a class="breadcrumbs-item-link" href="/' .
htmlspecialchars($group, ENT_QUOTES, "UTF-8") . '">' .
htmlspecialchars($group, ENT_QUOTES, "UTF-8") . '</a></li>';
echo ' <li class="breadcrumbs-item"><a class="breadcrumbs-item-link" href="/' .
htmlspecialchars($group, ENT_QUOTES, "UTF-8") . '/' . $article . '">' .
format_title($mail['headers']['subject']) . '</a></li>';
echo ' </ul>';
echo '</nav>';
echo '<section class="content">';
echo '<h1>' . format_subject($mail['headers']['subject'], 'utf-8') . "</h1>\n";
echo " <blockquote>\n";
echo ' <table class="standard">' . "\n";
# from
echo ' <tr class="vcard">' . "\n";
echo ' <td class="headerlabel">From:</td>' . "\n";
echo ' <td class="headervalue">' . format_author($mail['headers']['from']['raw'], 'utf-8') . "</td>\n";
# date
echo ' <td class="headerlabel">Date:</td>' . "\n";
echo ' <td class="headervalue">' . format_date($mail['headers']['date']) . "</td>\n";
echo " </tr>\n";
# subject
echo ' <tr>' . "\n";
echo ' <td class="headerlabel">Subject:</td>' . "\n";
echo ' <td class="headervalue" colspan="3">' . format_subject($mail['headers']['subject'], 'utf-8') . "</td>\n";
echo " </tr>\n";
echo " <tr>\n";
# references
if (!empty($refsResolved)) {
echo ' <td class="headerlabel">References:</td>' . "\n";
echo ' <td class="headervalue" ' . (empty($mail['headers']['newsgroups']) ? 'colspan="3"' : null) . '>';
foreach ($refsResolved as $k => $ref) {
echo "<a href=\"/" . urlencode($ref['group']) . '/' . urlencode($ref['articleId']) . "\">" .
($k + 1) . "</a> ";
}
echo "</td>\n";
}
# groups
if (!empty($mail['headers']['newsgroups'])) {
echo ' <td class="headerlabel">Groups:</td>' . "\n";
echo ' <td class="headervalue" ' . (empty($refsResolved) ? 'colspan="3"' : null) . '>';
$r = explode(",", rtrim($mail['headers']['newsgroups']));
foreach ($r as $v) {
echo "<a href=\"/" . urlencode($v) . "\">" . htmlspecialchars($v) . "</a> ";
}
echo "</td>\n";
}
echo " </tr>\n";
# email to request archived copy
$request_address = get_list_address($group) . '+get-' . $article . '@lists.php.net';
echo ' <tr>' . "\n";
echo ' <td class="headerlabel">Request:</td>' . "\n";
echo ' <td class="headervalue" colspan="3">Send a blank email to <a href="mailto:' . clean($request_address) . '">' . clean($request_address) . "</a> to get a copy of this message</td>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " </table>\n";
echo " </blockquote>\n";
echo " <blockquote>\n";
echo " <pre>\n";
/*
* If there was no text part of the message, see what we can do about creating
* one from a text/html part, or just inject a note that there was no text to
* avoid further errors.
*/
if (!array_key_exists('text', $mail)) {
if (array_key_exists('html', $mail)) {
/*
* This just aggressively strips out all tags. For the examples at
* hand, this looked okay-ish. Better than nothing, at least, and
* should be totally safe because all of the text get re-encoded
* later.
*/
// This makes HTML from Fastmail retain paragraph breaks
$text = preg_replace('#<div><br></div>#', "\n\n", $mail['html']);
// And this avoids extra linebreaks from another example (Android?)
$text = preg_replace("#\n<br>\n#", "\n", $text);
$mail['text'] = html_entity_decode(strip_tags($text), encoding: 'UTF-8');
} else {
$mail['text'] = "> There was no text content in this message.";
}
}
$lines = preg_split("@(?<=\r\n|\n)@", $mail['text']);
$insig = $is_commit = $is_diff = 0;
foreach ($lines as $line) {
# fix lines that started with a period and got escaped
if (substr($line, 0, 2) == "..") {
$line = substr($line, 1);
}
# Notice commit messages so we can highlight the diffs
if (str_starts_with($line, 'Commit: https://github.com/php')) {
$is_commit = 1;
}
# this is some amazingly simplistic code to color quotes/signatures
# differently, and turn links into real links. it actually appears
# to work fairly well, but could easily be made more sophistimicated.
/* NOQUOTES? Why? It creates invalid HTML: http:"x */
$line = htmlentities($line, ENT_QUOTES, "utf-8");
$line = preg_replace(
"/((mailto|https?|ftp|nntp|news):.+?)(>|\\s|\\)|\\.\\s|$)/",
"<a href=\"\\1\">\\1</a>\\3",
$line
);
if (!$insig && ($line == "-- \r\n" || $line == "--\r\n")) {
echo "<span class=\"signature\">";
$insig = 1;
}
if (!$insig && $is_commit && preg_match('/^[-+]/', $line, $m)) {
$is_diff = 1;
echo '<span class="' . ($m[0] == '+' ? 'added' : 'removed') . '">';
}
if (!$insig && preg_match('/^((\w*?> ?)+)/', $line, $m)) {
$level = substr_count($m[1], '>') % 4;
echo "<span class=\"quote$level\">", wordwrap($line, 90, "\n" . $m[1]), "</span>";
} else {
echo wordwrap($line, 90);
}
if ($is_diff) {
$is_diff = 0;
echo '</span>';
}
}
if ($insig) {
echo "</span>";
$insig = 0;
}
echo "<br><br>";
if (!empty($mail['attachment'])) {
foreach ($mail['attachment'] as $mimecount => $attachment) {
$mimetype = $attachment['mimetype'];
$name = $attachment['filename'];
if ($mimetype == 'text/plain') {
echo htmlspecialchars($attachment['data']);
continue;
}
if (!empty($attachment['description'])) {
$description = trim($attachment['description']) . " ";
} else {
$description = '';
}
$description .= $name;
$link_desc = "[$mimetype]";
if (strlen($description)) {
$link_desc .= " " . $description;
}
$dl_link = "/getpart.php?group=$group&article=$article&part=$mimecount";
$link_desc = htmlspecialchars($link_desc, ENT_QUOTES, 'UTF-8');
/* Attachment filename and mimetype might contain malicious characters */
printf(
'Attachment: <a href="%s">%s</a><br />' . "\n",
$dl_link,
htmlspecialchars($link_desc)
);
}
}
echo " </pre>\n";
echo " </blockquote>\n";
try {
$overview = $nntpClient->getThreadOverview($group, $article);
$threads = new \PhpWeb\ThreadTree($overview['articles']);
?>
<blockquote>
<h2>
Thread (<?= sprintf("%d message%s", $count = $threads->count(), $count > 1 ? 's' : '') ?>)
</h2>
<div class="responsive-table">
<table class="standard">
<thead>
<tr>
<th>#</th>
<th>Subject</th>
<th>Author</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php $threads->printRows($group, 'utf8'); ?>
</tbody>
</table>
</div>
</blockquote>
<?php
} catch (\Throwable $t) {
// We don't care if there's no thread. (There should be, though.)
}
// Does not check existence of next, so consider this the super duper fast [broken] version
// Based off navbar() in group.php
$group = htmlspecialchars($group, ENT_QUOTES, "UTF-8");
$current = $article;
echo ' <table class="standard">' . "\n";
echo ' <tr>' . "\n";
echo ' <th class="nav">';
if ($current > 1) {
echo ' <a href="/' , $group , '/' , ($current - 1) , '"><b>« <span>previous</span></b></a>';
} else {
echo ' ';
}
echo ' </th>' . "\n";
echo ' <th class="align-center">' . "$group (#$current)</th>\n";
echo ' <th class="nav align-right">';
echo ' <a href="/' , $group , '/' , ($current + 1) , '"><b><span>next</span> »</b></a>';
echo ' </th>' . "\n";
echo ' </tr>' . "\n";
echo ' </table>' . "\n";
echo '</section>';
foot();