forked from php/web-news
-
Notifications
You must be signed in to change notification settings - Fork 1
/
article.php
206 lines (175 loc) · 5.65 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
<?php
require 'common.php';
/* Prevents the poor mail server from suffering if it receives a message with many references */
/* (References: <xxx> or In-Reply-To: <xxx>) */
define('REFERENCES_LIMIT', 20);
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 = fMailbox::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'));
start_article($mail, $refsResolved);
$lines = preg_split("@(?<=\r\n|\n)@", $mail['text']);
$insig = 0;
foreach ($lines as $line) {
# fix lines that started with a period and got escaped
if (substr($line,0,2) == "..") {
$line = substr($line,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 && substr($line,0,4) == ">") {
echo "<span class=\"quote\">$line</span>";
} else {
echo $line;
}
}
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";
function start_article($mail, $refsResolved) {
echo " <blockquote>\n";
echo ' <table border="0" cellpadding="2" cellspacing="2" width="100%">' . "\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">';
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">';
$r = explode(",", rtrim($mail['headers']['newsgroups']));
while (list($k,$v) = each($r)) {
echo "<a href=\"/".urlencode($v)."\">".htmlspecialchars($v)."</a> ";
}
echo "</td>\n";
}
echo " </tr>\n";
echo " </table>\n";
echo " </blockquote>\n";
echo " <blockquote>\n";
echo " <pre>\n";
}
// Does not check existence of next, so consider this the super duper fast [broken] version
// Based off navbar() in group.php
function navbar($group, $current) {
$group = htmlspecialchars($group, ENT_QUOTES, "UTF-8");
echo ' <table border="0" cellpadding="2" cellspacing="2" width="100%">' . "\n";
echo ' <tr>' . "\n";
echo ' <th class="nav">';
if ($current > 1) {
echo ' <a href="/' , $group , '/' , ($current-1) , '"><b>« previous</b></a>';
} else {
echo ' ';
}
echo ' </th>' . "\n";
echo ' <th align="center">' . "$group (#$current)</th>\n";
echo ' <th align="right" class="nav">';
echo ' <a href="/' , $group , '/' , ($current+1) , '"><b>next »</b></a>';
echo ' </th>' . "\n";
echo ' </tr>' . "\n";
echo ' </table>' . "\n";
}
navbar($group, $article);
foot();