-
Notifications
You must be signed in to change notification settings - Fork 117
/
SpotParser.php
400 lines (315 loc) · 11.4 KB
/
SpotParser.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
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
<?php
require_once "Math/BigInteger.php";
require_once "Crypt/RSA.php";
require_once "settings.php";
class SpotParser {
private $_xmlarray = array();
private $_xmlelement = '';
private function xmlfullStartElement($parser, $name, $attrs)
{
$this->_xmlelement = strtolower($name);
} # xmlfullStartElement
private function xmlfullEndElement($parser, $name)
{
$this->_xmlelement = '';
} # xmlfullEndElement
private function xmlfullCharacterHandler($parser, $data) {
if (isset($this->_xmlarray[$this->_xmlelement])) {
if (!is_array($this->_xmlarray[$this->_xmlelement])) {
$this->_xmlarray[$this->_xmlelement] = array($this->_xmlarray[$this->_xmlelement], $data);
} else {
$this->_xmlarray[$this->_xmlelement][] = $data;
} # else
} else {
$this->_xmlarray[$this->_xmlelement] = $data;
} # else
} # xmlfullCharacterHandler
function parseFull($xml) {
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, array($this, 'xmlfullStartElement'), array('SpotParser', 'xmlfullEndElement'));
xml_set_character_data_handler($xml_parser, array($this, 'xmlfullCharacterHandler'));
if (!xml_parse($xml_parser, $xml, true)) {
$this->_xmlarray = false;
} # if error parsing
xml_parser_free($xml_parser);
# fix the category in the XML array
$this->_xmlarray['category'] = ((int) $this->_xmlarray['category']) - 1;
# and return the parsed XML
return $this->_xmlarray;
} # parseFull()
function parseXover($subj, $from, $messageid, $rsakeys) {
$_ID = 2;
$_CAT = 0;
$_STAMP = 3;
// initialiseer wat variabelen
$spot = array();
// Eerst splitsen we de header string op in enkel de category info e.d.
$tmpHdr = preg_split("(<|>)", $from);
if (count($tmpHdr) < 2) {
return null;
} # if
$tmpHdr = explode("@", $tmpHdr[1]);
$spot['Header'] = $tmpHdr[1];
$spot['Verified'] = false;
$spot['MessageID'] = substr($messageid, 1, strlen($messageid) - 2);
$fields = explode(".", $spot['Header']);
if (count($fields) >= 6) {
$spot['ID'] = $fields[$_ID];
if ($spot['ID'] > 9) {
$spot['Category'] = (substr($fields[$_CAT], 0, 1)) - 1.0;
// extract de posters name
$spot['Poster'] = explode("<", $from);
$spot['Poster'] = Trim($spot['Poster'][0]);
// key id
$spot['KeyID'] = (int) substr($fields[$_CAT], 1, 1);
// groupname
$spot['GroupName'] = 'free.pt';
if ($spot['KeyID'] >= 1) {
$expression = '';
$strInput = substr($fields[$_CAT], 2);
$recentKey = $spot['KeyID'] <> 1;
if ($recentKey) {
if ((strlen($strInput) == 0) || ((strlen($strInput) % 3) != 0)) {
exit;
} # if
$subcatAr = $this->SplitBySizEx($strInput, 3);
foreach($subcatAr as $str) {
if (strlen($str) > 0) {
$expression .= strtolower(substr($str, 0, 1)) . ((int) substr($str, 1)) . "|";
} # if
} # foeeach
$spot['SubCat'] = (int) (substr($subcatAr[0], 1));
} else {
$list = array();
for($i = 0; $i < strlen($strInput); $i++) {
if (($strInput[$i] == 0) && (!is_numeric($strInput[$i])) && (strlen($expression) > 0)) {
$list[] = $expression;
$expression = '';
} # if
$expression .= $strInput[$i];
} # for
$list[] = $expression;
$expression = '';
foreach($list as $str) {
$expression .= strtolower(substr($str, 0, 1)) . substr($str, 1) . "|";
} # foreach
$spot['SubCat'] = (int) (substr($list[0], 1));
} # else if $recentKey
# Break up the subcategories per subcat-type
if (strlen($expression) > 0) {
$subcats = explode('|', $expression);
$spot['SubCatA'] = '';
$spot['SubCatB'] = '';
$spot['SubCatC'] = '';
$spot['SubCatD'] = '';
foreach($subcats as $subcat) {
if (array_search(strtolower(substr($subcat, 0, 1)), array('a','b','c','d')) !== false) {
$spot['SubCat' . strtoupper(substr($subcat, 0, 1))] .= $subcat . '|';
} # if
} # foreach
} # if
if ((strpos($subj, "=?") !== false) && (strpos($subj, "?=") !== false)) {
# Make sure its as simple as possible
$subj = str_replace("?= =?", "?==?", $subj);
$subj = str_replace("\r", "", trim($this->OldEncodingParse($subj)));
$subj = str_replace("\n", "", $subj);
} # if
if ($recentKey) {
if (strpos($subj, "|") !== false) {
$tmp = explode("|", $subj);
$spot['Title'] = trim($tmp[0]);
$spot['Tag'] = trim($tmp[1]);
} else {
$spot['Title'] = trim($subj);
$spot['Tag'] = '';
} # else
} else {
$tmp = explode("|", $subj);
if (count($tmp) <= 1) {
$tmp = array($subj);
} # if
$spot['Tag'] = trim($tmp[count($tmp) - 1]);
# remove the tags from the array
array_pop($tmp);
array_pop($tmp);
$spot['Title'] = trim(implode('|', $tmp));
if ((strpos($spot['Title'], chr(0xc2)) !== false) | (strpos($spot['Title'], chr(0xc3)) !== false)) {
$spot['Title'] = trim($this->OldEncodingParse($spot['Title']));
} # if
} # if recentKey
$spot['Stamp'] = $fields[$_STAMP];
if (((strlen($spot['Title']) != 0) && (strlen($spot['Poster']) != 0)) && (($spot['ID'] >= 1000000) || $recentKey)) {
# Vanaf spot-id 1385910 komen we KeyID's 2 tegen, dus vanaf daar gaan we alle niet-signed posts weigeren.
$mustbeSigned = $recentKey | (!$recentKey & ($spot['ID'] > 1385910));
# FIXME
#
# somehow there is a check that the key is only validated for spots with key id 2 ?
# not sure about the code as it only seems to execute for more than 25000 spots or something?
#
$mustbeSigned = (($mustbeSigned) & ($spot['KeyID'] >= 2));
# and verify the signature it
if ($mustbeSigned) {
$spot['HeaderSign'] = $fields[count($fields) - 1];
if (strlen($spot['HeaderSign']) != 0) {
$spot['WasSigned'] = true;
# This is the string to verify
$toCheck = $spot['Title'] . substr($spot['Header'], 0, strlen($spot['Header']) - strlen($spot['HeaderSign']) - 1) . $spot['Poster'];
# Initialize the public key to verify with
$pubKey['n'] = new Math_BigInteger(base64_decode($rsakeys[$spot['KeyID']]['modulo']), 256);
$pubKey['e'] = new Math_BigInteger(base64_decode($rsakeys[$spot['KeyID']]['exponent']), 256);
# the signature this header is signed with
$signature = base64_decode($this->UnspecialString($spot['HeaderSign']));
# and verify the signature
$rsa = new Crypt_RSA();
$rsa->loadKey($pubKey, CRYPT_RSA_PUBLIC_FORMAT_RAW);
$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
# Supress notice if the signature was invalid
$saveErrorReporting = error_reporting(E_ERROR);
$spot['Verified'] = $rsa->verify($toCheck, $signature);
error_reporting($saveErrorReporting);
} # if
} # if must be signed
else {
$spot['Verified'] = true;
$spot['WasSigned'] = false;
} # if doesnt need to be signed, pretend that it is
} # if
} # if
} # if
} # if
return $spot;
} # parseXover
private function FixPadding($strInput) {
while ((strlen($strInput) % 4) != 0) {
$strInput .= "=";
} # while
return $strInput;
} # FixPadding
private function UnspecialString($strInput) {
$strInput = $this->FixPadding($strInput);
$strInput = str_replace("-s", "/", $strInput);
$strInput = str_replace("-p", "+", $strInput);
return $strInput;
} # UnspecialString
/*private */function UnspecialZipStr($strInput) {
$strInput = str_replace("=C", "\n", $strInput);
$strInput = str_replace("=B", "\r", $strInput);
$strInput = str_replace("=A", "\0", $strInput);
$strInput = str_replace("=D", "=", $strInput);
return $strInput;
} # UnspecialZipstr
private function SplitBySizEx($strInput, $iSize) {
$length = strlen($strInput);
$index = 0;
$tmp = array();
for ($i = 0; ($i + $iSize) <= ($length + $iSize); $i += $iSize) {
$tmp[$index] = substr($strInput, $i, $iSize);
$index++;
} # for
return $tmp;
} # SplitBySizEx
function ParseEncodedWord($inputStr) {
$str = "";
$builder = "";
if (substr($inputStr, 0, 1) !== '=') {
return $inputStr;
} # if
if (substr($inputStr, strlen($inputStr) - 2) !== '?=') {
return $inputStr;
} # if
$name = substr($inputStr, 2, strpos($inputStr, "?", 2) - 2);
if (strtoupper($name) == "UTF8") {
$name = "UTF-8";
} # if
$c = $inputStr[strlen($name) + 3];
$startIndex = strlen($name) + 5;
switch(strtolower($c)) {
case 'q' :
{
while ($startIndex < strlen($input)) {
$ch2 = $strInput[$startIndex];
$chArray = null;
switch($ch2) {
case '=': {
if ($startIndex >= (strlen($input) - 2)) {
$chArray = substr($strInput, $startIndex + 1, 2);
} # if
if ($chArray == null) {
echo "Untested code path!";
$builder .= $chArray . chr(10);
$startIndex += 3;
} # if
continue;
} # case '='
case '?': {
if ($strInput[$startIndex + 1] == '=') {
$startIndex += 2;
} # if
continue;
} # case '?'
} # switch
$builder .= $ch2;
$startIndex++;
} # while
break;
} # case 'q'
case 'b' :
{
$builder .= base64_decode(substr($inputStr, $startIndex, ((strlen($inputStr) - $startIndex) - 2)));
break;
} # case 'b'
} # switch
return $builder;
} # parseEncodedWord
function OldEncodingParse($inputStr) {
$builder = "";
$builder2 = "";
$encodedWord = false;
$num = 0;
while ($num < strlen($inputStr)) {
$bliep = false;
$ch = $inputStr[$num];
switch($ch) {
case '=' :
{
if (($num != (strlen($inputStr) - 1)) && ($inputStr[$num + 1] == '?')) {
$encodedWord = true;
} # if
break;
} # case '='
case '?' :
{
$ch2 = ' ';
if ($num != (strlen($inputStr) - 1)) {
$ch2 = $inputStr[$num+1];
} # if
if ($ch2 != "=") {
break;
} # if
$encodedWord = false;
$builder .= $ch . $ch2;
$builder2 .= $this->ParseEncodedWord($builder);
$builder = "";
$num += 2;
$bliep = true;
continue;
} # case '?'
} # switch
if (!$bliep) {
if ($encodedWord) {
$builder .= $ch;
$num++;
} else {
$builder2 .= $ch;
$num++;
} # else
} # if
} # while
return $builder2;
} # OldEncodingParse
} # class Spot
/*
convert("Two And A half Men S04E20 | shrekNZB", "jjzwlr <57105@13a00b04c01d04.205135897.57105.1297325839.7rQ.0.U9rvXyLMQU2-sZll-szYs2MwwVdVhRb8C-pYSACkNtuweKS6ItvcuXwS7l1t0khesWf>", $settings['rsa_keys']);
echo "<hr>";
convert("Acronis True Image Home 2011 14.0.6696 UK | zinitzio", "zinitzio <57399@43a00b24.371055968.57399.1297361414.hus.0.oqUtT-pzj7699nE-p9PXMQ9Y-poelVdPDMFIjKGP-sDL7xK-sIPiu427ryBSCumgNXZlD>", $settings['rsa_keys']);
*/