annotate common.php @ 12:7917bd536187 draft

Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
author Ivo Smits <Ivo@UCIS.nl>
date Wed, 12 Jun 2013 22:22:07 +0200
parents e0807e0b1a67
children cccd73f72bf6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
1 <?php
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
2 /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved.
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
3 Redistribution and use in source and binary forms, with or without modification, are
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
4 permitted provided that the following conditions are met:
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
5
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
6 1. Redistributions of source code must retain the above copyright notice, this list of
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
7 conditions and the following disclaimer.
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
8
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
9 2. Redistributions in binary form must reproduce the above copyright notice, this list
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
10 of conditions and the following disclaimer in the documentation and/or other materials
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
11 provided with the distribution.
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
12
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
14 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
16 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
17 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
18 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
19 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
20 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
21 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
22
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
23 The views and conclusions contained in the software and documentation are those of the
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
24 authors and should not be interpreted as representing official policies, either expressed
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
25 or implied, of Ivo Smits.*/
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
26
12
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
27 $pnewss_hooks = array('article_stored' => array());
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
28
11
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
29 require_once './pdo.php';
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
30 require_once './config.php';
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
31
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
32 function nntp_readline($socket) {
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
33 $line = fgets($socket, 512);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
34 if ($line === FALSE || $line === NULL) return $line;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
35 $line = rtrim($line, "\r\n");
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
36 writelog('R: '.$line);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
37 return $line;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
38 }
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
39 function nntp_writeline_data($socket, $line) {
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
40 if (strlen($line) && $line[0] == '.') $line = '.'.$line;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
41 nntp_writeline($socket, $line);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
42 }
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
43 function nntp_writeline($socket, $line) {
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
44 writelog('W: '.$line);
12
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
45 $line .= "\r\n";
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
46 while (strlen($line)) {
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
47 $written = fwrite($socket, $line);
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
48 if ($written === FALSE || $written <= 0) throw new Exception('Write operation failed');
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
49 $line = substr($line, $written);
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
50 }
11
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
51 }
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
52 function nntp_readlines($socket) {
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
53 $line = nntp_readline($socket);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
54 $lines = array();
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
55 while ($line != '.' && $line !== FALSE && $line !== FALSE) {
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
56 if (strlen($line) && $line[0] == '.') $line = substr($line, 1);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
57 $lines[] = $line;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
58 $line = nntp_readline($socket);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
59 }
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
60 if ($line != '.') throw new Exception('Unexpected end of message');
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
61 return $lines;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
62 }
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
63
12
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
64 function pnewss_call_hooks($hooks, $args) {
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
65 foreach ($hooks as $hook) call_user_func_array($hook, $args);
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
66 }
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
67
11
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
68 function nntp_article_store($lines, $header = array()) {
12
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
69 global $db, $pnewss_hooks;
11
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
70 $headers = array();
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
71 if (!count($header)) {
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
72 while (count($lines)) {
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
73 $line = array_shift($lines);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
74 if (!strlen($line)) break;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
75 $header[] = $line;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
76 }
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
77 }
12
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
78 $headername = NULL;
11
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
79 foreach ($header as $headerid => $line) {
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
80 if (!strlen($line) || $line == '.') throw new Exception('Empty or terminating header line');
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
81 if (strpos($line, "\r") !== FALSE || strpos($line, "\n") !== FALSE || strpos($line, "\0")) throw new Exception('Invalid newline or NUL character in header line');
12
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
82 if (strlen($line) && strlen($headername) && ($line[0] == ' ' || $line[0] == "\t") && isset($headers[$headername])) {
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
83 $headers[$headername] .= ' '.trim($line, " \t");
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
84 unset($header[$headerid]);
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
85 continue;
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
86 }
11
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
87 $parts = explode(': ', $line, 2);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
88 $headername = strtoupper($parts[0]);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
89 switch ($headername) {
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
90 case 'PATH': case 'FROM': case 'NEWSGROUPS': case 'SUBJECT': case 'DATE': case 'MESSAGE-ID': case 'SENDER':
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
91 if (isset($headers[$headername])) throw new Exception('Duplicate header: '.$headername);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
92 $headers[strtoupper($parts[0])] = $parts[1];
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
93 unset($header[$headerid]);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
94 break;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
95 case 'ORGANIZATION': case 'LINES':
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
96 case 'MIME-VERSION': case 'CONTENT-TYPE': case 'CONTENT-TRANSFER-ENCODING': case 'USER-AGENT':
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
97 case 'REFERENCES': case 'REPLY-TO': case 'SENDER': case 'FOLLOWUP-TO': case 'IN-REPLY-TO':
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
98 case 'EXPIRES': case 'CONTROL': case 'DISTRIBUTION': case 'KEYWORDS': case 'SUMMARY':
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
99 break;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
100 case 'NNTP-POSTING-HOST': case 'X-TRACE': case 'XREF': case 'X-COMPLAINTS-TO':
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
101 case 'NNTP-POSTING-DATE':
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
102 unset($header[$headerid]);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
103 $line = NULL;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
104 break;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
105 default:
12
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
106 if ($headername[0] == 'X' && $headername[1] == '-') break;
11
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
107 writelog("Received unknown header $headername");
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
108 }
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
109 }
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
110 foreach ($lines as $line) {
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
111 if ($line == '.') throw new Exception('Terminating body line');
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
112 if (strpos($line, "\r") !== FALSE || strpos($line, "\n") !== FALSE || strpos($line, "\0")) throw new Exception('Invalid newline or NUL character in header line');
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
113 }
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
114 if (!isset($headers['NEWSGROUPS'])) throw new Exception('Missing required Newsgroups: header');
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
115 $newsgroups = array();
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
116 foreach (explode(',', $headers['NEWSGROUPS']) as $groupname) {
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
117 $group = $db->evalRowAssoc('SELECT * FROM `groups` WHERE `name` = ?', $groupname);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
118 if ($group === FALSE) continue;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
119 $newsgroups[] = $group['id'];
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
120 }
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
121 if (!count($newsgroups)) throw new Exception('No known newsgroups listed');
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
122 if (!isset($headers['MESSAGE-ID'])) $headers['MESSAGE-ID'] = '<'.md5(time().rand()).'@pNewss.Core.UCIS.nl>';
12
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
123 if (!isset($headers['DATE'])) $headers['DATE'] = gmdate('r');
11
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
124 $messageid = $headers['MESSAGE-ID'];
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
125 if (strlen($messageid) < 3 || strlen($messageid) > 250 || $messageid[0] != '<' || strpos($messageid, '>') !== strlen($messageid) - 1) throw new Exception('Bad Message-ID');
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
126 $messageid = substr($messageid, 1, -1);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
127 $article = $db->evalRowAssoc('SELECT * FROM `messages` WHERE `messageid` = ?', $messageid);
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
128 if ($article !== FALSE) throw new Exception('Duplicate');
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
129 $headers['PATH'] = 'pNewss.Core.UCIS.nl'.(isset($headers['PATH'])?'!'.$headers['PATH']:'');
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
130 foreach (array('Path', 'From', 'Newsgroups', 'Subject', 'Date', 'Message-ID', 'Sender') as $headername) {
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
131 if (isset($headers[strtoupper($headername)])) $header[] = $headername.': '.$headers[strtoupper($headername)];
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
132 }
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
133 $id = $db->insert('INSERT INTO `messages` (`messageid`, `header`, `body`) VALUES (?, ?, ?)', array($messageid, implode("\r\n", $header), implode("\r\n", $lines)));
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
134 foreach ($newsgroups as $groupid) $db->insert('INSERT INTO `groupmessages` (`group`, `message`) VALUES (?, ?)', array($groupid, $id));
12
7917bd536187 Added hook for new articles, detect send/write failures, fixed handling of multiline headers, add Date header if it doesn't exist, add option to disable peers, fixes for synchronization with INN, added streaming mode support, small fixes
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
135 pnewss_call_hooks($pnewss_hooks['article_stored'], array(array('messageid' => $messageid, 'headers' => $headers, 'body' => $lines, 'dbid' => $id)));
11
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
136 return $messageid;
e0807e0b1a67 Added common.php, updated server synchronization to continue after a connection failure
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
137 }