-
Notifications
You must be signed in to change notification settings - Fork 117
/
index.php
416 lines (335 loc) · 12.1 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
<?php
require_once "db.php";
require_once "req.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['perpage'] = 1000;
# helper functions for passed variables
$req = new Req();
$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;
} # initialize()
function openDb() {
extract($GLOBALS['site'], EXTR_REFS);
# fireup the database
$db = new db($settings['db']);
$GLOBALS['site']['db'] = $db;
return $db;
} # openDb]
function sabnzbdurl($spot) {
extract($GLOBALS['site'], EXTR_REFS);
# fix de category
$spot['category'] = (int) $spot['category'];
# find een geschikte category
$category = $settings['sabnzbd']['categories'][$spot['category']]['default'];
# voeg de subcategorieen samen en splits ze dan op een pipe
if (isset($spot['subcata'])) {
$subcatAr = explode("|", $spot['subcata'] . $spot['subcatb'] . $spot['subcatc'] . $spot['subcatd']);
} else {
$subcatAr = array();
foreach($spot['sub'] as $subcat) {
$subcatAr[] = substr($subcat, 2, 1) . ((int) substr($subcat, 3));
} # foreach
} # else
foreach($subcatAr 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++) {
if (isset($settings['sabnzbd'])) {
$spotList[$i]['sabnzbdurl'] = sabnzbdurl($spotList[$i]);
} # if
$spotList[$i]['searchurl'] = makesearchurl($spotList[$i]);
} # foreach
return $spotList;
} # loadSpots()
function filterToQuery($search, $dynatree) {
extract($GLOBALS['site'], EXTR_REFS);
$filterList = array();
# dont filter anything
if (empty($search) && (empty($dynatree))) {
return '';
}
# convert the dynatree list to a list
$dyn2search = array();
if (!empty($dynatree)) {
$dynaList = explode(',', $dynatree);
foreach($dynaList as $val) {
if (substr($val, 0, 3) == 'cat') {
$val = explode('_', (substr($val, 3) . '_'));
if (count($val) >= 3) {
$dyn2search['cat'][$val[0]][] = 'a' . $val[1];
} else {
$dyn2search['cat'][$val[0]][] = $val[0];
}
} # if
} # foreach
} # if
# merge the actual search array and the categories selected in the
# tree
$search = array_merge($search, $dyn2search);
# Add a list of possible head categories
if ((isset($search['cat'])) && ((is_array($search['cat'])))) {
$filterList = array();
foreach($search['cat'] as $catid => $cat) {
$catid = (int) $catid;
$tmpStr = "(";
$tmpStr .= "(category = " . $catid . ")";
# Now start adding the sub categories
if ((is_array($cat)) && (!empty($cat))) {
$subcatStr = " AND (";
$subcatCounter = 0;
#
# 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
#
foreach($cat as $subcat) {
# split up the subcat
$operator = ' LIKE ';
$seloper = ' OR ';
# OR or AND ?
if (substr($subcat, 0, 1) == 'a') {
$seloper = ' AND ';
} # if
$subcat = substr($subcat, 1);
# NOT for this subcategory?
if (substr($subcat, 0, 1) == '!') {
$subcat = substr($subcat, 1);
$operator = ' NOT LIKE ';
} # if
# extract the category type
$catType = substr($subcat, 0, 1);
$subcat = ((int) (substr($subcat, 1))) . '|';
if (array_search($catType, array('a','b','c','d')) !== false) {
if ($subcatCounter == 0) {
$seloper = '';
} # if
$subcatStr .= $seloper . "subcat" . $catType . $operator . "'%" . $catType . $subcat . "%'";
$subcatCounter++;
} # if
} # foreach subcat
$subcatStr .= ")";
} # if
if ($subcatCounter > 0) {
$tmpStr .= $subcatStr;
} # 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, "unselectable": true, "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']),
$req->getDef('dynatree-select', array()));
$spots = loadSpots(0, $filter);
#- display stuff -#
template('header');
template('filters', array('search' => $req->getDef('search', array())));
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];
$spotnntp = new SpotNntp($settings['nntp_hdr']['host'],
$settings['nntp_hdr']['enc'],
$settings['nntp_hdr']['port'],
$settings['nntp_hdr']['user'],
$settings['nntp_hdr']['pass']);
if ($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['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
#- display stuff -#
template('header');
template('spotinfo', array('spot' => $xmlar, 'comments' => $comments));
template('footer');
break;
} else {
die("Unable to connect to NNTP server");
} # else
} # getspot
case 'getnzb' : {
$db = openDb();
$spot = $db->getSpot(Req::getDef('messageid', ''));
$spot = $spot[0];
$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']) {
$connected = ($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']);
$connected = (($hdr_spotnntp->connect()) && ($nzb_spotnntp->connect()));
} # else
if ($connected) {
$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) {
$tmp = $nzb_spotnntp->getBody("<" . $seg . ">");
if ($tmp !== false) {
$nzb .= implode("", $tmp);
} else {
break;
} #else
} # foreach
} else {
$tmp = $nzb_spotnntp->getBody("<" . $xmlar['segment'] . ">");
if ($tmp !== false) {
$nzb .= implode("", $tmp);
} # if
} # if
if ($nzb !== false) {
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
} # if
break;
} # getnzb
}