File: SpotNntp.php

package info (click to toggle)
spotweb 20130826%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,132 kB
  • ctags: 11,281
  • sloc: php: 31,367; xml: 1,009; sh: 148; makefile: 83
file content (570 lines) | stat: -rwxr-xr-x 18,365 bytes parent folder | download | duplicates (2)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
<?php
class SpotNntp {
		private $_server;
		private $_user;
		private $_pass;
		private $_serverenc;
		private $_serverport;
		
		private $_error;
		private $_nntp;
		private $_connected;
		private $_currentgroup;

		private $_spotParser;
		
		function __construct($server) { 
			$error = '';
			
			$this->_connected = false;
			$this->_server = $server['host'];
			$this->_serverenc = $server['enc'];
			$this->_serverport = $server['port'];
			$this->_user = $server['user'];
			$this->_pass = $server['pass'];

			$this->_nntp = new Net_NNTP_Client();
			$this->_spotParser = new SpotParser();
		} # ctor

	
		/*
		 * Select a group as active group
		 */
		function selectGroup($group) {
			$this->connect();

			$this->_currentgroup = $group;
			return $this->_nntp->selectGroup($this->_currentgroup);
		} # selectGroup()
		
		/*
		 * Returns an overview (XOVER) from first id to lastid
		 */
		function getOverview($first, $last) {
			$this->connect();
			return $this->_nntp->getOverview($first . '-' . $last);
		} # getOverview()

		/*
		 * Get a list of messageid's within a range, same as XOVER
		 * but only for messageids
		 */
		function getMessageIdList($first, $last) {
			$this->connect();
			$hdrList = $this->_nntp->getHeaderField('Message-ID', ($first . '-' . $last));
			return $hdrList;
		} # getMessageIdList()
		
		/*
		 * Disconnect from the server if we are connected
		 */
		function quit() {
			if (!$this->_connected) {
				return ;
			} # if
			
			try {
				$this->_nntp->quit();
				$this->_connected = false;
			} 
			catch(Exception $x) {
				// dummy, we dont care about exceptions during quitting time
			} # catch
		} # quit()

		/*
		 * Sends a no-operation to the usenet server to keep the
		 * connection alive
		 */
		function sendNoop() {
			if (!$this->_connected) {
				return ;
			} # if
			
			/* The NNTP protocol has no proper noop command, this will do fine */
			if (!empty($this->_currentgroup)) {
				$this->selectGroup($this->_currentgroup);		
			} # if
		} # sendnoop()

		/*
		 * Post an article to the server, $article should be an 2-element 
		 * array with head and body as elements
		 */
		function post($article) {
			$this->connect();

			// We kunnen niet rechtstreeks post() aanroepen omdat die
			// de autoloader triggered
			$tmpError = $this->_nntp->cmdPost();
			if ($tmpError) {
				return $this->_nntp->cmdPost2($article);
			} else {
				return $tmpError;
			} # else
		} # post()
		
		/*
		 * Returns the header of an messageid
		 */
		function getHeader($msgid) {
			$this->connect();
			return $this->_nntp->getHeader($msgid);
		} # getHeader()

		/*
		 * Returns the body of an messageid
		 */
		function getBody($msgid) {
			$this->connect();
			return $this->_nntp->getBody($msgid);
		} # getBody	()
		
		/*
		 * Connect to the newsserver and authenticate
		 * if necessary
		 */
		function connect() {
			# dummy operation
			if ($this->_connected) {
				return ;
			} # if
			
			# if an empty hostname is provided, abort
			if (empty($this->_server)) {
				throw new NntpException('Servername is empty', -1);
			}  # if 

			# if a portnumber is empty, abort
			if ((!is_numeric($this->_serverport)) || ($this->_serverport < 1)) {
				throw new NntpException('A server port has to be entered', -1);
			}  # if 

			# if the type of SSL is invalid, abort
			if (($this->_serverenc !== false) && (strtolower($this->_serverenc) !== 'ssl') && (strtolower($this->_serverenc) !== 'tls')) {
				throw new NntpException('Invalid encryption method specified', -1);
			}  # if 
			
			$this->_connected = true;

			/* 
			 * Erase username/password so it won't show up in any stacktrace
			 */
			$tmpUser = $this->_user;
			$tmpPass = $this->_pass;
			$this->_user = '*FILTERED*';
			$this->_pass = '*FILTERED*';
			
			try{
				$ret = $this->_nntp->connect($this->_server, $this->_serverenc, $this->_serverport, 10);
				if ($ret === false) {
					throw new NntpException('Error while connecting to server (server did not respond)', -1);
				} # if
				
				if (!empty($tmpUser)) {
					$authed = $this->_nntp->authenticate($tmpUser, $tmpPass);
					
				} # if
			}catch(Exception $x){
				throw new NntpException($x->getMessage(), $x->getCode());
			}
		} # connect()
		
		/*
		 * Returns a full article divided between an
		 * header and body part
		 */
		function getArticle($msgId) {
			$this->connect();
	
			$result = array('header' => array(), 'body' => array());
			
			# Fetch het artikel
			$art = $this->_nntp->getArticle($msgId);
			
			# vervolgens splitsen we het op in een header array en een body array
			$i = 0;
			$lnCount = count($art);
			while( ($i < $lnCount) && ($art[$i] != '')) {
				$result['header'][] = $art[$i];
				$i++;
			} # while
			$i++;

			while($i < $lnCount) {
				$result['body'][] = $art[$i];
				$i++;
			} # while
			
			return $result;
		} # getArticle

		/*
		 * Parse an header and extract specific fields
		 * from it
		 */
		function parseHeader($headerList, $tmpAr) {
			# extract de velden we die we willen hebben
			foreach($headerList as $hdr) {
				$keys = explode(':', $hdr);

				switch($keys[0]) {
					case 'From'				: $tmpAr['fromhdr'] = utf8_encode(trim(substr($hdr, strlen('From: '), strpos($hdr, '<') - 1 - strlen('From: ')))); break;
					case 'Date'				: $tmpAr['stamp'] = strtotime(substr($hdr, strlen('Date: '))); break;
					case 'X-XML' 			: $tmpAr['fullxml'] .= substr($hdr, 7); break;
					case 'X-User-Signature'	: $tmpAr['user-signature'] = $this->_spotParser->unspecialString(substr($hdr, 18)); break;
					case 'X-XML-Signature'	: $tmpAr['xml-signature'] = $this->_spotParser->unspecialString(substr($hdr, 17)); break;
					case 'X-User-Avatar'	: $tmpAr['user-avatar'] .= substr($hdr, 15); break;
					case 'X-User-Key'		: {
							$xml = simplexml_load_string(substr($hdr, 12)); 
							if ($xml !== false) {
								$tmpAr['user-key']['exponent'] = (string) $xml->Exponent;
								$tmpAr['user-key']['modulo'] = (string) $xml->Modulus;
							} # if
							break;
					} # x-user-key
				} # switch
			} # foreach
			
			return $tmpAr;
		} # parseHeader

		/*
		 * Callback function for sorting of comments on date
		 */
		function cbCommentDateSort($a, $b) {
			if ($a['stamp'] == $b['stamp']) {
				return 0;
			} # if
			
			return ($a['stamp'] < $b['stamp']) ? -1 : 1;
		} # cbCommentDateSort
		
		/*
		 * Returns a list of comments
		 */
		function getComments($commentList) {
			$comments = array();
			$spotSigning = Services_Signing_Base::newServiceSigning();
			
			# We extracten elke comment en halen daar de datum en poster uit, inclusief de body
			# als comment text zelf.
			foreach($commentList as $comment) {
				try {
					$commentTpl = array('messageid' => '', 'fromhdr' => '', 'stamp' => 0, 'user-signature' => '', 
										'user-key' => '', 'spotterid' => '', 'verified' => false,
										'user-avatar' => '', 'fullxml' => '');
										
					$tmpAr = array_merge($commentTpl, $this->getArticle('<' . $comment['messageid'] . '>'));
					$tmpAr['messageid'] = $comment['messageid'];
					$tmpAr = array_merge($tmpAr, $this->parseHeader($tmpAr['header'], $tmpAr));

					# Valideer de signature van de XML, deze is gesigned door de user zelf
					$tmpAr['verified'] = $spotSigning->verifyComment($tmpAr);
					if ($tmpAr['verified']) {
						$tmpAr['spotterid'] = $spotSigning->calculateSpotterId($tmpAr['user-key']['modulo']);
					} # if

					# encode de body voor UTF8
					$tmpAr['body'] = array_map('utf8_encode', $tmpAr['body']);

					$comments[] = $tmpAr; 
				} 
				catch(Exception $x) {
					# Soms gaat het ophalen van een comment mis? Raar want deze komen van de XOVER
					# van de server zelf, dus tenzij ze gecancelled worden mag dit niet gebeuren.
					# iig, we negeren de error
					;
				} # catch
			} # foreach

			# sorteer de comments per datum
			usort($comments, array($this, 'cbCommentDateSort'));

			return $comments;
		} # getComments

		
		function getImage($segmentList) {
			$imageContent = '';

			/*
			 * Retrieve all image segments 
			 */
			foreach($segmentList['image']['segment'] as $seg) {
				$imgTmp = implode('', $this->getBody('<' . $seg . '>'));
				$imageContent .= $this->_spotParser->unspecialZipStr($imgTmp);
			} # foreach
			
			return $imageContent;
		} # getImage
		
		function getNzb($segList) {
			$nzb = '';
			
			foreach($segList as $seg) {
				$nzb .= implode('', $this->getBody('<' . $seg . '>'));
			} # foreach

			$nzb = gzinflate($this->_spotParser->unspecialZipStr($nzb));
			return $nzb;
		} # getNzb
		
		/*
		 * Post plain usenet message
		 */
		function postPlainMessage($newsgroup, $message, $additionalHeaders) {
			$header = 'Subject: ' . utf8_decode($message['title']) . "\r\n";
			$header .= 'Newsgroups: ' . $newsgroup . "\r\n";
			$header .= 'Message-ID: <' . $message['newmessageid'] . ">\r\n";
			$header .= "X-Newsreader: SpotWeb v" . SPOTWEB_VERSION . "\r\n";
			$header .= "X-No-Archive: yes\r\n";
			$header .= $additionalHeaders;

			return $this->post(array($header, $message['body']));
		} # postPlainMessage

		/*
		 * Post a signed usenet message, we allow for additional headers
		 * so this function can be used by anything
		 */
		function postSignedMessage($user, $serverPrivKey, $newsgroup, $message, $additionalHeaders) {
			# instantiate necessary objects
			$spotSigning = Services_Signing_Base::newServiceSigning();

			# also by the SpotWeb server 
			$server_signature = $spotSigning->signMessage($serverPrivKey, '<' . $message['newmessageid'] . '>');

			$addHeaders = '';
			
			# Only add the user-signature header if there is none set yet
			if (stripos($additionalHeaders, 'X-User-Signature: ') === false) {
				# sign the messageid
				$user_signature = $spotSigning->signMessage($user['privatekey'], '<' . $message['newmessageid'] . '>');
			
				$addHeaders .= 'X-User-Signature: ' . $this->_spotParser->specialString($user_signature['signature']) . "\r\n";
				$addHeaders .= 'X-User-Key: ' . $spotSigning->pubkeyToXml($user_signature['publickey']) . "\r\n";
			} # if
			
			$addHeaders .= 'X-Server-Signature: ' . $this->_spotParser->specialString($server_signature['signature']) . "\r\n";
			$addHeaders .= 'X-Server-Key: ' . $spotSigning->pubkeyToXml($server_signature['publickey']) . "\r\n";
			$addHeaders .= $additionalHeaders;

			return $this->postPlainMessage($newsgroup, $message, $addHeaders);
		} # postSignedMessage
		
		/*
		 * Post a binary usenet message
		 */
		function postBinaryMessage($user, $newsgroup, $body, $additionalHeaders) {
			$chunkLen = (1024 * 1024);
			$segmentList = array();
			$spotSigning = Services_Signing_Base::newServiceSigning();
			
			/*
			 * Now start posting chunks of the NZB files
			 */
			while(strlen($body) > 0) {
				$message = array();

				/*
				 * Cut of the first piece of the NZB file, and remove it
				 * from the source string
				 */
				$chunk = substr($body, 0, $chunkLen - 1);
				$body = substr($body, $chunkLen - 1);

				/* 
				 * Split the body in parts of 900 characters
				 */
				$message['body'] = chunk_split($this->_spotParser->specialZipstr($chunk), 900);

				/*
				 * Create an unique messageid and store it so we can return it
				 * for the actual Spot creation
				 */
				$message['newmessageid'] = $spotSigning->makeRandomStr(32) . '@spot.net';
				$message['title'] = md5($message['body']);

				$addHeaders = 'From: ' . $user['username'] . " <" . trim($user['username']) . '@spot.net>' . "\r\n";
				$addHeaders .= 'Content-Type: text/plain; charset=ISO-8859-1' . "\r\n";
				$addHeaders .= 'Content-Transfer-Encoding: 8bit' . "\r\n";
				$addHeaders .= $additionalHeaders;

				/* 
				 * Actually post the image
				 */
				$this->postPlainMessage( $newsgroup, $message, $addHeaders);

				$segmentList[] = $message['newmessageid'];
			} # if
			 
			return $segmentList;
		} # postBinaryMessage

		/*
		 * Post a comment to a spot
		 */
		function postComment($user, $serverPrivKey, $newsgroup, $comment) {
			/* 
			 * Create the comment specific headers
			 */
			$addHeaders = 'From: ' . $user['username'] . " <" . trim($user['username']) . '@spot.net>' . "\r\n";
			$addHeaders .= 'References: <' . $comment['inreplyto']. ">\r\n";
			$addHeaders .= 'X-User-Rating: ' . (int) $comment['rating'] . "\r\n";
			
			/*
			 * And add the X-User-Avatar header if user has an avatar specified
			 */
			if (!empty($user['avatar'])) {
				$tmpAvatar = explode("\r\n", chunk_split($user['avatar'], 900));
				
				foreach($tmpAvatar as $avatarChunk) {
					if (strlen(trim($avatarChunk)) > 0) {
						$addHeaders .= 'X-User-Avatar: ' . $avatarChunk . "\r\n";
					} # if
				} # foreach
			} # if

			return $this->postSignedMessage($user, $serverPrivKey, $newsgroup, $comment, $addHeaders);
		} # postComment
		
	
		/*
		 * Posts a spot file and its corresponding image and NZB file (actually done by
		 * helper functions)
		 */
		function postFullSpot($user, $serverPrivKey, $newsgroup, $spot) {
			# instantiate the necessary objects
			$spotSigning = Services_Signing_Base::newServiceSigning();

			/*
			 * Create the spotnet from header part accrdoing to the following structure:
			 *   From: [Nickname] <[PUBLICKEY-MODULO.USERSIGNATURE]@[CAT][KEY-ID][SUBCAT].[SIZE].[RANDOM].[DATE].[CUSTOM-ID].[CUSTOM-VALUE].[SIGNATURE]>
			 */
			$spotHeader = ($spot['category'] + 1) . $spot['key']; // Append the category and keyid
			
			# Process each subcategory and add them to the from header
			foreach($spot['subcatlist'] as $subcat) {
				$spotHeader .= $subcat[0] . str_pad(substr($subcat, 1), 2, '0', STR_PAD_LEFT);
			} # foreach
			
			$spotHeader .= '.' . $spot['filesize'];
			$spotHeader .= '.' . 10; // some kind of magic number?
			$spotHeader .= '.' . time();
			$spotHeader .= '.' . $spotSigning->makeRandomStr(4);
			$spotHeader .= '.' . $spotSigning->makeRandomStr(3);

			# If a tag is given, add it to the subject
			if (strlen(trim($spot['tag'])) > 0) {
				$spot['title'] = $spot['title'] . ' | ' . $spot['tag'];
			} # if
			
			# Create the user-signature
			$user_signature = $spotSigning->signMessage($user['privatekey'], '<' . $spot['newmessageid'] . '>');
			$header = 'X-User-Signature: ' . $this->_spotParser->specialString($user_signature['signature']) . "\r\n";
			$header .= 'X-User-Key: ' . $spotSigning->pubkeyToXml($user_signature['publickey']) . "\r\n";
				
			# sign the header by using the users' key
			$header_signature = $spotSigning->signMessage($user['privatekey'], $spot['title'] . $spotHeader . $spot['poster']);

			# sign the XML with the users' key
			$xml_signature = $spotSigning->signMessage($user['privatekey'], $spot['spotxml']);

			# Extract the users' publickey
			$userPubKey = $spotSigning->getPublicKey($user['privatekey']);
			
			# Create the From header
			$spotnetFrom = $user['username'] . ' <' . 
								$this->_spotParser->specialString($userPubKey['publickey']['modulo']) . 
								'.' . 
								$this->_spotParser->specialString($user_signature['signature']) . '@';
			$header = 'From: ' . $spotnetFrom . $spotHeader . '.' . $this->_spotParser->specialString($header_signature['signature']) . ">\r\n";
			
			# Add the Spotnet XML file, but split it in chunks of 900 characters
			$tmpXml = explode("\r\n", chunk_split($spot['spotxml'], 900));
			foreach($tmpXml as $xmlChunk) {
				if (strlen(trim($xmlChunk)) > 0) {
					$header .= 'X-XML: ' . $xmlChunk . "\r\n";
				} # if
			} # foreach
			$header .= 'X-XML-Signature: ' . $this->_spotParser->specialString($xml_signature['signature']) . "\r\n";

			# post the message
			return $this->postSignedMessage($user, $serverPrivKey, $newsgroup, $spot, $header);
		} # postFullSpot

		/*
		 * Retrieve the fullspot from the NNTP server
		 */
		function getFullSpot($msgId) {
			SpotTiming::start('SpotNntp::' . __FUNCTION__);

			# initialize some variables
			$spotSigning = Services_Signing_Base::newServiceSigning();
			
			$spot = array('fullxml' => '',
						  'user-signature' => '',
						  'user-key' => '',
						  'verified' => false,
						  'messageid' => $msgId,
						  'spotterid' => '',
						  'xml-signature' => '',
						  'moderated' => 0,
						  'user-avatar' => '');
			# Vraag de volledige article header van de spot op
			SpotTiming::start('SpotNntp::' . __FUNCTION__ . '->getHeader()');
			$header = $this->getHeader('<' . $msgId . '>');
			SpotTiming::stop('SpotNntp::' . __FUNCTION__ . '->getHeader()', array($header));

			# Parse de header
			SpotTiming::start('SpotNntp::' . __FUNCTION__ . '->parseHeader()');
			$spot = array_merge($spot, $this->parseHeader($header, $spot));
			SpotTiming::stop('SpotNntp::' . __FUNCTION__ . '->parseHeader()', array($spot));
			
			# Valideer de signature van de XML, deze is gesigned door de user zelf
			SpotTiming::start('SpotNntp::' . __FUNCTION__ . '->verifyFullSpot()');
			$spot['verified'] = $spotSigning->verifyFullSpot($spot);
			SpotTiming::stop('SpotNntp::' . __FUNCTION__ . '->verifyFullSpot()', array($spot));
			
			# als de spot verified is, toon dan de spotterid van deze user
			if ($spot['verified']) {
				$spot['spotterid'] = $spotSigning->calculateSpotterId($spot['user-key']['modulo']);
			} # if	
			
			# Parse nu de XML file, alles wat al gedefinieerd is eerder wordt niet overschreven
			SpotTiming::start('SpotNntp::' . __FUNCTION__ . '->parseFull()');
			$spot = array_merge($this->_spotParser->parseFull($spot['fullxml']), $spot);
			SpotTiming::stop('SpotNntp::' . __FUNCTION__ . '->parseFull()', array($spot));
			
			SpotTiming::stop('SpotNntp::' . __FUNCTION__, array($spot));
			
			return $spot;
		} # getFullSpot 
		
		function reportSpotAsSpam($user, $serverPrivKey, $newsgroup, $report) {
			/*
			 * Create the comment specific headers
			 */
			$addHeaders = 'From: ' . $user['username'] . " <" . trim($user['username']) . '@spot.net>' . "\r\n";
			$addHeaders .= 'References: <' . $report['inreplyto']. ">\r\n";

			return $this->postSignedMessage($user, $serverPrivKey, $newsgroup, $report, $addHeaders);
		} # reportSpotAsSpam
		
		/*
		 * validates wether can succesfully connect to the usenet
		 * server
		 */
		function validateServer() {
			/*
			 * We need to select a group, because authenticatin
			 * is not always entered but sometimes required
			 */
			$this->selectGroup('free.pt');
			
			$this->quit();
		} # validateServer
		
} # class SpotNntp