forked from php/web-news
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.php
154 lines (134 loc) · 4.32 KB
/
common.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
<?php
require_once 'lib/Web/News/Nntp.php';
require_once 'lib/fMailbox.php';
$NNTP_HOST = 'localhost';
if (getenv('NNTP_HOST')) {
$NNTP_HOST = getenv('NNTP_HOST');
}
define('NNTP_HOST', $NNTP_HOST);
function error($str) {
head("PHP news : error");
echo "<blockquote><strong>Error:</strong> ".to_utf8($str)."</blockquote>\n";
foot();
die();
}
function head($title="PHP news") {
header("Content-type: text/html; charset=utf-8");
echo '<?xml version="1.0"?>' . "\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo htmlspecialchars($title); ?></title>
<link rel="stylesheet" href="/style.css" type="text/css" />
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="header">
<tr>
<td>
<a href="/index.php"><img src="/i/l.gif" width="120" height="67" alt="PHP" /></a>
</td>
<td align="right" valign="bottom">
PHP.net <a href="news://<?php echo $_SERVER['HTTP_HOST']; ?>/" class="top">news server</a> web interface
</td>
</tr>
</table>
<?php
}
function foot() {?>
<hr />
<div class="small">
Written by Jim Winstead. no rights reserved. (<a href="https://git.php.net/?p=web/news.git">source code</a>)
</div>
</body>
</html>
<?php
}
function to_utf8($str, $charset = 'iso-8859-1')
{
$n = iconv($charset , 'utf-8', $str);
if ($n === false) {
return $str;
}
return $n;
}
function decode_header($charset,$encoding,$text) {
if (strtolower($encoding) == "b") {
$text = base64_decode($text);
} else {
$text = quoted_printable_decode($text);
}
return to_utf8($text, $charset);
}
function recode_header($header, $basecharset) {
if (strpos($header, "=?") === false) {
return to_utf8($header, $basecharset);
}
return preg_replace_callback(
"/=\\?(.+?)\\?([qb])\\?(.+?)(\\?=|$)/i",
function ($m) {
return decode_header($m[1], $m[2], $m[3]);
},
$header
);
}
/* Email spam protection (taken from php-bugs-web) */
function spam_protect($txt) {
$translate = array('@' => ' at ', '.' => ' dot ');
/* php.net addresses are not protected! */
if (preg_match('/^(.+)@php\.net/i', $txt)) {
return $txt;
} else {
return strtr($txt, $translate);
}
}
# this turns some common forms of email addresses into mailto: links
function format_author($a, $charset) {
$a = recode_header($a, $charset);
if (preg_match("/^\s*(.+)\s+\\(\"?(.+?)\"?\\)\s*$/",$a,$ar)) {
return "<a href=\"mailto:".htmlspecialchars(urlencode(spam_protect($ar[1])), ENT_QUOTES, "UTF-8")."\" class=\"email fn n\">".str_replace(" ", " ", htmlspecialchars($ar[2], ENT_QUOTES, "UTF-8"))."</a>";
}
if (preg_match("/^\s*\"?(.+?)\"?\s*<(.+)>\s*$/",$a,$ar)) {
return "<a href=\"mailto:".htmlspecialchars(urlencode(spam_protect($ar[2])), ENT_QUOTES, "UTF-8")."\" class=\"email fn n\">".str_replace(" ", " ", htmlspecialchars($ar[1], ENT_QUOTES, "UTF-8"))."</a>";
}
if (strpos("@",$a) !== false) {
$a = spam_protect($a);
return "<a href=\"mailto:".htmlspecialchars(urlencode($a), ENT_QUOTES, "UTF-8")."\" class=\"email fn n\">".htmlspecialchars($a, ENT_QUOTES, "UTF-8")."</a>";
}
return str_replace(" ", " ", htmlspecialchars($a, ENT_QUOTES, "UTF-8"));
}
function format_subject($s, $charset) {
global $article;
$s = recode_header($s, $charset);
if ((($pos = strpos($s, '[PHP')) !== false || ($pos = strpos($s, '[PEAR')) !== false)) {
if (($end_pos = strpos($s, ']', $pos)) !== false) {
$s = ltrim(substr_replace($s, '', $pos, $end_pos - $pos + 1));
}
}
// make this look better on the preview page..
if (strlen($s) > 150 && !isset($article)) {
$s = substr($s, 0, 150) . "...";
} else {
$s = wordwrap($s, 150);
}
return nl2br(htmlspecialchars($s, ENT_QUOTES, "UTF-8"));
}
function format_title($s, $charset) {
global $article;
$s = recode_header($s, $charset);
$s = preg_replace("/^(Re: *)?\[(PHP|PEAR)(-.*)?\] /i", "\\1", $s);
// make this look better on the preview page..
if (strlen($s) > 150 && !isset($article)) {
$s = substr($s, 0, 150) . "...";
} else {
$s = wordwrap($s, 150);
}
return htmlspecialchars($s, ENT_QUOTES, "UTF-8");
}
function format_date($d) {
$d = strtotime($d);
$d = gmdate('r', $d);
return str_replace(" ", " ", $d);
}