forked from Pistolebob/spotweb
/
index.php
executable file
·498 lines (405 loc) · 15.3 KB
/
index.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
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
<?php
error_reporting(E_ALL & ~8192 & ~E_USER_WARNING); # 8192 == E_DEPRECATED maar PHP < 5.3 heeft die niet
require_once "lib/SpotDb.php";
require_once "lib/SpotReq.php";
require_once "SpotParser.php";
require_once "SpotCategories.php";
require_once "SpotNntp.php";
function initialize() {
require_once "settings.php";
$settings = $GLOBALS['settings'];
# we define some preferences, later these could be
# user specific or stored in a cookie or something
$prefs = array('perpage' => 1000);
if (isset($settings['prefs'])) {
$prefs = array_merge($prefs, $settings['prefs']);
} # if
# helper functions for passed variables
$req = new SpotReq();
$req->initialize();
# gather the current page
$GLOBALS['site']['page'] = $req->getDef('page', 'index');
if (array_search($GLOBALS['site']['page'], array('index', 'catsjson', 'getnzb', 'getspot')) === false) {
$GLOBALS['site']['page'] = 'index';
} # if
# and put them in an encompassing site object
$GLOBALS['site']['req'] = $req;
$GLOBALS['site']['settings'] = $settings;
$GLOBALS['site']['prefs'] = $prefs;
$GLOBALS['site']['pagetitle'] = 'SpotWeb - ';
} # initialize()
function openDb() {
extract($GLOBALS['site'], EXTR_REFS);
# fireup the database
try {
$db = new SpotDb($settings['db']);
$db->connect();
}
catch(Exception $x) {
die('Unable to open database: ' . $x->getMessage());
} # catch
$GLOBALS['site']['db'] = $db;
return $db;
} # openDb]
function fixSpotSubCategories($spot) {
$subcatAr = array();
$subcatList = array();
#
# Bij oude-style (?) spots wordt er al een gesplitste array van subcategorieen aangeleverd
# die uiteraard niet compatible is met de nieuwe style van subcategorieen
#
if (!isset($spot['subcata'])) {
if ((!empty($spot['subcat'])) && (is_array($spot['subcat']))) {
$subcatList = $spot['subcat'];
} else {
$subcatList = $spot['sub'];
} # if
# match hoofdcat/subcat-type/subcatvalue
foreach($subcatList as $subcat) {
if (preg_match('/(\d+)([aAbBcCdD])(\d+)/', preg_quote($subcat), $tmpMatches)) {
$subcatAr[] = strtolower($tmpMatches[2]) . ((int) $tmpMatches[3]);
} # if
} # foreach
} else {
#
# als de headers al voorbewerkt zijn (maw: dit is de spotheader listing vanuit de database),
# niks meer aan doen behalve er zelf de category voor gaan zetten
#
$subcatAr = explode("|", $spot['subcata'] . $spot['subcatb'] . $spot['subcatc'] . $spot['subcatd']);
} # elseif
return $subcatAr;
} # func. fixSpotSubCategories
function sabnzbdurl($spot) {
extract($GLOBALS['site'], EXTR_REFS);
# alleen draaien als we gedefinieerd zijn
if ((!isset($settings['sabnzbd'])) | (!isset($settings['sabnzbd']['apikey'])) | (!isset($settings['sabnzbd']['categories']))) {
return '';
} # if
# fix de category
$spot['category'] = (int) $spot['category'];
# find een geschikte category
$category = $settings['sabnzbd']['categories'][$spot['category']]['default'];
foreach($spot['subcatlist'] as $cat) {
if (isset($settings['sabnzbd']['categories'][$spot['category']][$cat])) {
$category = $settings['sabnzbd']['categories'][$spot['category']][$cat];
} # if
} # foreach
# en creeer die sabnzbd url
$tmp = $settings['sabnzbd']['url'];
$tmp = str_replace('$SABNZBDHOST', $settings['sabnzbd']['host'], $tmp);
$tmp = str_replace('$NZBURL', urlencode($settings['sabnzbd']['spotweburl'] . '?page=getnzb&messageid='. $spot['messageid']), $tmp);
$tmp = str_replace('$SPOTTITLE', urlencode($spot['title']), $tmp);
$tmp = str_replace('$SANZBDCAT', $category, $tmp);
$tmp = str_replace('$APIKEY', $settings['sabnzbd']['apikey'], $tmp);
return $tmp;
} # sabnzbdurl
function makesearchurl($spot) {
extract($GLOBALS['site'], EXTR_REFS);
if (!isset($spot['filename'])) {
$tmp = str_replace('$SPOTFNAME', $spot['title'], $settings['search_url']);
} else {
$tmp = str_replace('$SPOTFNAME', $spot['filename'], $settings['search_url']);
} # else
return $tmp;
} # makesearchurl
function loadSpots($start, $sqlFilter) {
extract($GLOBALS['site'], EXTR_REFS);
$spotList = $db->getSpots($start, $prefs['perpage'], $sqlFilter);
$spotCnt = count($spotList);
for ($i = 0; $i < $spotCnt; $i++) {
$spotList[$i]['subcatlist'] = fixSpotSubcategories($spotList[$i]);
if (isset($settings['sabnzbd']['apikey'])) {
$spotList[$i]['sabnzbdurl'] = sabnzbdurl($spotList[$i]);
} # if
$spotList[$i]['searchurl'] = makesearchurl($spotList[$i]);
} # foreach
return $spotList;
} # loadSpots()
function filterToQuery($search) {
extract($GLOBALS['site'], EXTR_REFS);
$filterList = array();
$dyn2search = array();
# dont filter anything
if (empty($search)) {
return '';
} # if
# convert the dynatree list to a list
if (!empty($search['tree'])) {
# explode the dynaList
$dynaList = explode(',', $search['tree']);
# fix de tree variable zodat we dezelfde parameters ondersteunen als de JS
$newTreeQuery = '';
for($i = 0; $i < count($dynaList); $i++) {
# De opgegeven category kan in twee soorten voorkomen:
# cat1_a ==> Alles van cat1, en daar alles van 'a' selecteren
# cat1 ==> Heel cat1 selecteren
#
# Omdat we in deze code de dynatree emuleren, voeren we deze lelijke hack uit.
if ((strlen($dynaList[$i]) == 6) || (strlen($dynaList[$i]) == 4)) {
$hCat = (int) substr($dynaList[$i], 3, 1);
# was een subcategory gespecificeerd?
if (strlen($dynaList[$i]) == 6) {
$subCatSelected = substr($dynaList[$i], 5);
} else {
$subCatSelected = '*';
} # else
#
# creeer een string die alle subcategories bevat
#
# we loopen altijd door alle subcategorieen heen zodat we zowel voor complete category selectie
# als voor enkel subcategory selectie dezelfde code kunnen gebruiken.
#
$tmpStr = '';
foreach(SpotCategories::$_categories[$hCat] as $subCat => $subcatValues) {
if (($subCat == $subCatSelected) || ($subCatSelected == '*')) {
foreach(SpotCategories::$_categories[$hCat][$subCat] as $x => $y) {
$tmpStr .= ",cat" . $hCat . "_" . $subCat . $x;
} # foreach
} # if
} # foreach
$newTreeQuery .= $tmpStr;
} elseif (substr($dynaList[$i], 0, 1) == '!') {
# als het een NOT is, haal hem dan uit de lijst
$newTreeQuery = str_replace(substr($dynaList[$i], 1) . ",", "", $newTreeQuery);
} else {
$newTreeQuery .= "," . $dynaList[$i];
} # else
} # foreach
# explode the dynaList
$search['tree'] = $newTreeQuery;
$dynaList = explode(',', $search['tree']);
# en fix the list
foreach($dynaList as $val) {
if (substr($val, 0, 3) == 'cat') {
# 0e element is hoofdcategory
# 1e element is category
$val = explode('_', (substr($val, 3) . '_'));
$catVal = $val[0];
$subCatIdx = substr($val[1], 0, 1);
$subCatVal = substr($val[1], 1);
if (count($val) >= 3) {
$dyn2search['cat'][$catVal][$subCatIdx][] = $subCatVal;
} # if
} # if
} # foreach
} # if
# Add a list of possible head categories
if ((isset($dyn2search['cat'])) && (is_array($dyn2search['cat']))) {
$filterList = array();
foreach($dyn2search['cat'] as $catid => $cat) {
$catid = (int) $catid;
$tmpStr = "((category = " . $catid . ")";
# Now start adding the sub categories
if ((is_array($cat)) && (!empty($cat))) {
#
# uiteraard is een LIKE query voor category search niet super schaalbaar
# maar omdat deze webapp sowieso niet bedoeld is voor grootschalig gebruik
# moet het meer dan genoeg zijn
#
$subcatItems = array();
foreach($cat as $subcat => $subcatItem) {
$subcatValues = array();
foreach($subcatItem as $subcatValue) {
$subcatValues[] = "(subcat" . $subcat . " LIKE '%" . $subcat . $subcatValue . "|%') ";
} # foreach
# voeg de subfilter values (bv. alle formaten films) samen met een OR
$subcatItems[] = " (" . join(" OR ", $subcatValues) . ") ";
} # foreach subcat
# voeg de category samen met de diverse subcategory filters met een OR, bv. genre: actie, type: divx.
$tmpStr .= " AND (" . join(" AND ", $subcatItems) . ") ";
} # if
# close the opening parenthesis from this category filter
$tmpStr .= ")";
$filterList[] = $tmpStr;
} # foreach
} # if
# Add a list of possible head categories
$textSearch = '';
if ((!empty($search['text'])) && ((isset($search['type'])))) {
$field = 'title';
if ($search['type'] == 'Tag') {
$field = 'tag';
} else if ($search['type'] == 'Poster') {
$field = 'poster';
} # else
$textSearch .= ' (' . $field . " LIKE '%" . $db->safe($search['text']) . "%')";
} # if
if (!empty($filterList)) {
# echo '(' . (join(' OR ', $filterList) . ') ' . (empty($textSearch) ? "" : " AND " . $textSearch));
return '(' . (join(' OR ', $filterList) . ') ' . (empty($textSearch) ? "" : " AND " . $textSearch));
} else {
return $textSearch;
} # if
} # filterToQuery
function template($tpl, $params = array()) {
extract($GLOBALS['site'], EXTR_REFS);
extract($params, EXTR_REFS);
require_once($settings['tpl_path'] . $tpl . '.inc.php');
} # template
function categoriesToJson() {
echo "[";
$hcatList = array();
foreach(SpotCategories::$_head_categories as $hcat_key => $hcat_val) {
$hcatTmp = '{"title": "' . $hcat_val . '", "isFolder": true, "key": "cat' . $hcat_key . '", "children": [' ;
$subcatDesc = array();
foreach(SpotCategories::$_subcat_descriptions[$hcat_key] as $sclist_key => $sclist_desc) {
$subcatTmp = '{"title": "' . $sclist_desc . '", "isFolder": true, "hideCheckbox": true, "key": "cat' . $hcat_key . '_' . $sclist_key . '", "unselectable": false, "children": [';
# echo ".." . $sclist_desc . " <br>";
$catList = array();
foreach(SpotCategories::$_categories[$hcat_key][$sclist_key] as $key => $val) {
if ((strlen($val) != 0) && (strlen($key) != 0)) {
$catList[] = '{"title": "' . $val . '", "icon": false, "key":"'. 'cat' . $hcat_key . '_' . $sclist_key.$key .'"}';
} # if
} # foreach
$subcatTmp .= join(",", $catList);
$subcatDesc[] = $subcatTmp . "]}";
} # foreach
$hcatList[] = $hcatTmp . join(",", $subcatDesc) . "]}";
} # foreach
echo join(",", $hcatList);
echo "]";
} # categoriesToJson
#- main() -#
initialize();
extract($site, EXTR_REFS);
switch($site['page']) {
case 'index' : {
openDb();
$filter = filterToQuery($req->getDef('search', $settings['index_filter']));
$spots = loadSpots(0, $filter);
# zet de page title
$pagetitle .= "overzicht";
#- display stuff -#
template('header');
template('filters', array('search' => $req->getDef('search', array()),
'filters' => $settings['filters']));
template('spots', array('spots' => $spots));
template('footer');
break;
} # case index
case 'catsjson' : {
categoriesToJson();
break;
} # catsjson
case 'getspot' : {
$db = openDb();
$spot = $db->getSpot($req->getDef('messageid', ''));
$spot = $spot[0];
try {
$spotnntp = new SpotNntp($settings['nntp_hdr']['host'],
$settings['nntp_hdr']['enc'],
$settings['nntp_hdr']['port'],
$settings['nntp_hdr']['user'],
$settings['nntp_hdr']['pass']);
$spotnntp->connect();
$header = $spotnntp->getHeader('<' . $spot['messageid'] . '>');
$xml = '';
if ($header !== false) {
foreach($header as $str) {
if (substr($str, 0, 7) == 'X-XML: ') {
$xml .= substr($str, 7);
} # if
} # foreach
} # if
$spotParser = new SpotParser();
$xmlar = $spotParser->parseFull($xml);
$xmlar['messageid'] = $req->getDef('messageid', '');
$xmlar['subcatlist'] = fixSpotSubcategories($xmlar);
$xmlar['sabnzbdurl'] = sabnzbdurl($xmlar);
$xmlar['searchurl'] = makesearchurl($xmlar);
# Vraag een lijst op met alle comments messageid's
$commentList = $db->getCommentRef($xmlar['messageid']);
$comments = array();
foreach($commentList as $comment) {
$tmpAr = array('body' => $spotnntp->getBody('<' . $comment['messageid'] . '>'));
# extract de velden we die we willen hebben
$header = $spotnntp->getHeader('<' . $comment['messageid'] . '>');
foreach($header as $hdr) {
if (substr($hdr, 0, strlen('From: ')) == 'From: ') {
$tmpAr['from'] = trim(substr($hdr, strlen('From: '), strpos($hdr, '<') - 1 - strlen('From: ')));
} # if
if (substr($hdr, 0, strlen('Date: ')) == 'Date: ') {
$tmpAr['date'] = substr($hdr, strlen('Date: '));
} # if
} # foreach
$comments[] = $tmpAr;
} # foreach
# zet de page title
$pagetitle .= "spot: " . $xmlar['title'];
#- display stuff -#
template('header');
template('spotinfo', array('spot' => $xmlar, 'comments' => $comments));
template('footer');
break;
}
catch (Exception $x) {
die($x->getMessage());
} # else
} # getspot
case 'getnzb' : {
$db = openDb();
$spot = $db->getSpot($req->getDef('messageid', ''));
$spot = $spot[0];
try {
$hdr_spotnntp = new SpotNntp($settings['nntp_hdr']['host'],
$settings['nntp_hdr']['enc'],
$settings['nntp_hdr']['port'],
$settings['nntp_hdr']['user'],
$settings['nntp_hdr']['pass']);
if ($settings['nntp_hdr']['host'] == $settings['nntp_nzb']['host']) {
$hdr_spotnntp->connect();
$nzb_spotnntp = $hdr_spotnntp;
} else {
$nzb_spotnntp = new SpotNntp($settings['nntp_nzb']['host'],
$settings['nntp_nzb']['enc'],
$settings['nntp_nzb']['port'],
$settings['nntp_nzb']['user'],
$settings['nntp_nzb']['pass']);
$hdr_spotnntp->connect();
$nzb_spotnntp->connect();
} # else
$header = $hdr_spotnntp->getHeader('<' . $spot['messageid'] . '>');
$xml = '';
if ($header !== false) {
foreach($header as $str) {
if (substr($str, 0, 7) == 'X-XML: ') {
$xml .= substr($str, 7);
} # if
} # foreach
} # if
$spotParser = new SpotParser();
$xmlar = $spotParser->parseFull($xml);
/* Connect to the NZB group */
/* Get the NZB file */
$nzb = false;
if (is_array($xmlar['segment'])) {
foreach($xmlar['segment'] as $seg) {
$nzb .= implode("", $nzb_spotnntp->getBody("<" . $seg . ">"));
} # foreach
} else {
$nzb .= implode("", $nzb_spotnntp->getBody("<" . $xmlar['segment'] . ">"));
} # if
if ($nzb !== false) {
if ($settings['nzb_download_local'] == true)
{
$myFile = $settings['nzb_local_queue_dir'] .$xmlar['title'] . ".nzb";
$fh = fopen($myFile, 'w') or die("Unable to open file");
$stringData = gzinflate($spotParser->unspecialZipStr($nzb));
fwrite($fh, $stringData);
fclose($fh);
echo "NZB toegevoegd aan queue : ".$myFile;
} else {
Header("Content-Type: application/x-nzb");
Header("Content-Disposition: attachment; filename=\"" . $xmlar['title'] . ".nzb\"");
echo gzinflate($spotParser->unspecialZipStr($nzb));
}
} else {
echo "Unable to get NZB file: " . $nzb_spotnntp->getError();
} # else
}
catch(Exception $x) {
die($x->getMessage());
} # catch
break;
} # getnzb
}