/
SpotPage_newznabapi.php
1164 lines (1022 loc) · 48.3 KB
/
SpotPage_newznabapi.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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class SpotPage_newznabapi extends SpotPage_Abs
{
private $_params;
public function __construct(Dao_Factory $daoFactory, Services_Settings_Container $settings, array $currentSession, array $params)
{
parent::__construct($daoFactory, $settings, $currentSession);
$this->_params = $params;
}
// ctor
/**
* Generic render function, the actual processing happens elsewhere
* in this class.
*/
public function render()
{
// Don't let this output be cached
$this->sendExpireHeaders(true);
/*
* Determine the output type
*/
if ($this->_params['o'] == 'json') {
$outputtype = 'json';
} else {
$outputtype = 'xml';
} // else
/*
* CAPS function is used to query the server for supported features and the protocol version and other
* meta data relevant to the implementation. This function doesn't require the client to provide any
* login information but can be executed out of "login session".
*/
if ($this->_params['t'] == 'caps' || $this->_params['t'] == 'c') {
$this->caps($outputtype);
return;
} // if
// Make sure the user has permissions to retrieve the index
$this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_view_spots_index, '');
/*
* Main switch statement, determines what actually has to be done
*/
switch ($this->_params['t']) {
case '': $this->showApiError(200);
break;
case 'search':
case 's':
case 'tvsearch':
case 't':
case 'music':
case 'movie':
case 'm': $this->search($outputtype);
break;
case 'd':
case 'details': $this->spotDetails($outputtype);
break;
case 'g':
case 'get': $this->getNzb();
break;
default: $this->showApiError(202);
} // switch
}
// render()
/**
* Search the spotweb database for a specific piece of information.
*
* @param $outputtype
*/
public function search($outputtype)
{
// Check users' permissions
$this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_perform_search, '');
$searchParams = [];
$tvInfo = new Dto_MediaInformation();
$tvInfo->setTitle('');
$tvInfo->setValid(true);
/**
* Now determine what type of information we are searching for using sabnzbd.
*/
if ($this->_params['t'] == 't' || $this->_params['t'] == 'tvsearch') {
$found = false;
// First search on tvmazeid if present
if (($found == false) and ($this->_params['tvmazeid'] != '')) {
if (!preg_match('/^[0-9]{1,6}$/', $this->_params['tvmazeid'])) {
$this->showApiError(201);
return;
} // if
/*
* Actually retrieve the information from TVMaze, based on the
* TVmaze ID passed by the API
*/
$svcMediaInfoTvmaze = new Services_MediaInformation_Tvmaze($this->_daoFactory->getCacheDao());
$svcMediaInfoTvmaze->setSearchid($this->_params['tvmazeid']);
$svcMediaInfoTvmaze->setSearchName('tvmaze'); // Indicate tvmazeid usage
$tvInfo = $svcMediaInfoTvmaze->retrieveInfo();
$found = $tvInfo->isValid();
}
// second search on rid (rageid) if present
if (($found == false) and ($this->_params['rid'] != '')) {
// validate input
if (!preg_match('/^[0-9]{1,6}$/', $this->_params['rid'])) {
$this->showApiError(201);
return;
} // if
/*
* Actually retrieve the information from TVMaze as long as TVrage is down, based on the
* tvrage passed by the API
*/
$svcMediaInfoTvmaze = new Services_MediaInformation_Tvmaze($this->_daoFactory->getCacheDao());
$svcMediaInfoTvmaze->setSearchid($this->_params['rid']);
$svcMediaInfoTvmaze->setSearchName('tvrage'); // Indicate tvmazeid usage
$tvInfo = $svcMediaInfoTvmaze->retrieveInfo();
$found = $tvInfo->isValid();
}
// third search on q (showname) if present
if (($found == false) and ($this->_params['q'] != '')) {
$tvInfo = new Dto_MediaInformation();
$tvInfo->setTitle($this->_params['q']);
$tvInfo->setValid(true);
$found = true;
}
// fourth, no search information present, set emtpy
if ($found == false) {
if ((!empty($this->_params['tvmazeid'])) or (!empty($this->_params['rid']))) {
$this->showApiError(300);
return;
}
}
/*
* Try to parse the season parameter. This can be either in the form of S1, S01, 1, 2012, etc.
* we try to standardize all these types of season definitions into one format.
*/
$episodeSearch = '';
$seasonSearch = '';
if (preg_match('/^[sS][0-9]{1,2}$/', $this->_params['season']) ||
preg_match('/^[0-9]{1,4}$/', $this->_params['season'])) {
/*
* Did we get passed a 4 digit season (most likely a year), or a
* two digit season?
*/
if (strlen($this->_params['season']) < 3) {
if (is_numeric($this->_params['season'])) {
$seasonSearch = 'S'.str_pad($this->_params['season'], 2, '0', STR_PAD_LEFT);
} else {
$seasonSearch = $this->_params['season'];
} // else
} else {
$seasonSearch = $this->_params['season'].' ';
} // else
} elseif ($this->_params['season'] != '') {
$this->showApiError(201);
return;
} // if
/*
* And try to add an episode parameter, basically the same set of rules
* as for the season
*/
$title = $tvInfo->getTitle();
if (preg_match('/^[eE][0-9]{1,2}$/', $this->_params['ep']) ||
preg_match('/^[0-9]{1,2}$/', $this->_params['ep']) ||
preg_match('/^[0-9]{1,2}\/[0-9]{1,2}$/', $this->_params['ep'])) {
if (is_numeric($this->_params['ep'])) {
$episodeSearch .= 'E'.str_pad($this->_params['ep'], 2, '0', STR_PAD_LEFT);
} else {
$episodeSearch .= $this->_params['ep'];
} // else
} elseif ($this->_params['ep'] != '') {
$this->showApiError(201);
return;
} // if
/*
* The + operator is supported both by PostgreSQL and MySQL's FTS
*
* We search both for S04E17 and S04 E17 (with a space)
*/
if (!empty($title)) {
if (!empty($seasonSearch)) {
if (!empty($episodeSearch)) {
$searchParams['value'][] = 'Titel:=:AND:+"'.$tvInfo->getTitle().'"';
$searchParams['value'][] = 'Titel:=:DEF:'.$seasonSearch.' '.$episodeSearch;
} else {
// Complete season search, add wildcard character to season
if (empty($this->_params['noalt']) or $this->_params['noalt'] != '1') {
$searchParams['value'][] = 'Titel:=:OR:+"'.$title.'" +"Seizoen '.(int) $this->_params['season'].'"';
$searchParams['value'][] = 'Titel:=:OR:+"'.$title.'" +"Season '.(int) $this->_params['season'].'"';
}
$searchParams['value'][] = 'Titel:=:OR:+"'.$title.'" +'.$seasonSearch.'*';
}
} else {
$searchParams['value'][] = 'Titel:=:OR:+"'.$tvInfo->getTitle().'" +'.$episodeSearch;
}
}
if (empty($this->_params['cat'])) {
$this->_params['cat'] = 5000;
}
} elseif ($this->_params['t'] == 'music') {
if (empty($this->_params['cat'])) {
$this->_params['cat'] = 3000;
}
if (!empty($this->_params['artist'])) {
$searchParams['value'][] = 'Titel:=:DEF:"'.$this->_params['artist'].'"';
} // if
} elseif ($this->_params['t'] == 'm' || $this->_params['t'] == 'movie') {
/*
* Query by IMDB id
*/
if (!empty($this->_params['imdbid'])) {
// validate input
if (!preg_match('/^[0-9]{1,8}$/', $this->_params['imdbid'])) {
$this->showApiError(201);
return;
} // if
/*
* Actually retrieve the information from imdb, based on the
* imdbid passed by the API
*/
$svcMediaInfoImdb = new Services_MediaInformation_Imdb($this->_daoFactory->getCacheDao());
$svcMediaInfoImdb->setSearchid($this->_params['imdbid']);
$imdbInfo = $svcMediaInfoImdb->retrieveInfo();
if (!$imdbInfo->isValid()) {
$this->showApiError(301);
return;
} // if
/* Extract the release date from the IMDB info page */
if ($imdbInfo->getReleaseYear() != null) {
$movieReleaseDate = '+('.$imdbInfo->getReleaseYear().')';
} else {
$movieReleaseDate = '';
} // else
/*
* Add movie title to the query
*/
$searchParams['value'][] = 'Titel:=:OR:+"'.$imdbInfo->getTitle().'" '.$movieReleaseDate;
// imdb sometimes returns the title translated, if so, pass the translated title as well, bu only if noalt <> 1
if (empty($this->_params['noalt']) or $this->_params['noalt'] != '1') {
if ($imdbInfo->getAlternateTitle() != null) {
$searchParams['value'][] = 'Title:=:OR:+"'.$imdbInfo->getAlternateTitle().'" '.$movieReleaseDate;
} // if
} // if
} // if
/*
* Free search query
*/
if (!empty($this->_params['q'])) {
$searchTerm = str_replace(' ', ' +', $this->_params['q']);
$searchParams['value'][] = 'Titel:=:OR:+'.$searchTerm;
}
/*
* List movies category by default
*/
if (empty($this->_params['cat'])) {
$this->_params['cat'] = 2000;
}
} elseif (!empty($this->_params['q'])) {
$searchTerm = str_replace(' ', ' +', $this->_params['q']);
$searchParams['value'][] = 'Titel:=:OR:+'.$searchTerm;
} // elseif
/*
* When a user added a maximum age for queries, convert it to
* a Spotweb query as well
*/
if ($this->_params['maxage'] != '' && is_numeric($this->_params['maxage'])) {
$searchParams['value'][] = 'date:>:DEF:-'.$this->_params['maxage'].'days';
} // if
/*
* We combine the "newznabapi" categories, with a custom extension for
* categories so we can filter deeper than the newznab API can per default
*/
$tmpCat = [];
foreach (explode(',', $this->_params['cat']) as $category) {
$tmpCat[] = $this->nabcat2spotcat($category);
} // foreach
$searchParams['tree'] = implode(',', $tmpCat).','.$this->_params['spotcat'];
/*
* Do not retrieve spots with a filesize of zero (these are very old spots,
* which have no NZB linked to it) as they are useless for a API consumer
*/
$searchParams['value'][] = 'filesize:>:DEF:0';
if (!empty($this->_params['poster'])) {
$searchParams['value'][] = 'Poster:=:'.$this->_params['poster'];
}
/*
* Gather the preference of the results per page and use it in this
* system as well when no value is explicitly provided
*/
if ((!empty($this->_params['limit'])) &&
is_numeric($this->_params['limit']) &&
($this->_params['limit'] <= 500)) {
$limit = $this->_params['limit'];
} else {
$limit = $this->_currentSession['user']['prefs']['perpage'];
} // else
if ((!empty($this->_params['offset'])) && is_numeric($this->_params['offset'])) {
$pageNr = $this->_params['offset'];
} else {
$pageNr = 0;
} // else
/*
* We get a bunch of query parameters, so now change this to the actual
* search query the user requested including the required sorting
*/
$svcUserFilter = new Services_User_Filters($this->_daoFactory, $this->_settings);
$svcSearchQp = new Services_Search_QueryParser($this->_daoFactory->getConnection());
$parsedSearch = $svcSearchQp->filterToQuery(
$searchParams,
[
'field' => 'stamp',
'direction' => 'DESC',
],
$this->_currentSession,
$svcUserFilter->getIndexFilter($this->_currentSession['user']['userid'])
);
/*
* Actually fetch the spots, we always perform
* this action even when the watchlist is editted
*/
$svcProvSpotList = new Services_Providers_SpotList($this->_daoFactory->getSpotDao());
$spotsTmp = $svcProvSpotList->fetchSpotList(
$this->_currentSession['user']['userid'],
$pageNr,
$limit,
$parsedSearch
);
$this->showResults($spotsTmp, $pageNr * $limit, $outputtype);
}
// search
/*
* Actually create the XML or JSON output from the search
* results
*/
public function showResults($spots, $offset, $outputtype)
{
$nzbhandling = $this->_currentSession['user']['prefs']['nzbhandling'];
if ($outputtype == 'json') {
$doc = [];
foreach ($spots['list'] as $spot) {
$data = [];
$data['ID'] = $spot['messageid'];
$data['name'] = html_entity_decode($spot['title'], ENT_QUOTES, 'UTF-8');
$data['size'] = $spot['filesize'];
$data['adddate'] = date('Y-m-d H:i:s', $spot['stamp']);
$data['guid'] = $spot['messageid'];
$data['fromname'] = $spot['poster'];
$data['completion'] = 100;
$cat = '';
$nabCat = explode('|', $this->Cat2NewznabCat($spot['category'], $spot['subcata'], $spot['subcatz']));
if ($nabCat[0] != '' && is_numeric($nabCat[0])) {
$data['categoryID'] = $nabCat[0];
if ($cat != '') {
$cat .= ',';
}
$cat .= implode(',', $nabCat);
} // if
$data['comments'] = $spot['commentcount'];
$data['category_name'] = SpotCategories::HeadCat2Desc($spot['category']).': '.SpotCategories::Cat2ShortDesc($spot['category'], $spot['subcata']);
$data['category_ids'] = $cat;
if (empty($doc)) {
$data['_totalrows'] = count($spots['list']);
}
$doc[] = $data;
} // foreach
echo json_encode($doc);
} else {
// Create XML
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$rss = $doc->createElement('rss');
$rss->setAttribute('version', '2.0');
$rss->setAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom');
$rss->setAttribute('xmlns:newznab', 'http://www.newznab.com/DTD/2010/feeds/attributes/');
$doc->appendChild($rss);
$atomSelfLink = $doc->createElement('atom:link');
$atomSelfLink->setAttribute('href', $this->_settings->/** @scrutinizer ignore-call */get('spotweburl').'api');
$atomSelfLink->setAttribute('rel', 'self');
$atomSelfLink->setAttribute('type', 'application/rss+xml');
$channel = $doc->createElement('channel');
$channel->appendChild($atomSelfLink);
$channel->appendChild($doc->createElement('title', 'Spotweb Index'));
$channel->appendChild($doc->createElement('description', 'Spotweb Index API Results'));
$channel->appendChild($doc->createElement('link', $this->_settings->/** @scrutinizer ignore-call */get('spotweburl')));
$channel->appendChild($doc->createElement('language', 'en-gb'));
$channel->appendChild($doc->createElement('webMaster', $this->_currentSession['user']['mail'].' ('.$this->_currentSession['user']['firstname'].' '.$this->_currentSession['user']['lastname'].')'));
$channel->appendChild($doc->createElement('category', ''));
$rss->appendChild($channel);
$image = $doc->createElement('image');
$image->appendChild($doc->createElement('url', $this->_settings->/** @scrutinizer ignore-call */ get('spotweburl').'images/spotnet.gif'));
$image->appendChild($doc->createElement('title', 'Spotweb Index'));
$image->appendChild($doc->createElement('link', $this->_settings->/** @scrutinizer ignore-call */ get('spotweburl')));
$image->appendChild($doc->createElement('description', 'SpotWeb Index API Results'));
$channel->appendChild($image);
$newznabResponse = $doc->createElement('newznab:response');
$newznabResponse->setAttribute('offset', $offset);
$newznabResponse->setAttribute('total', count($spots['list']));
$channel->appendChild($newznabResponse);
foreach ($spots['list'] as $spot) {
$spot = $this->_tplHelper->formatSpotHeader($spot);
$nzbUrl = $this->_tplHelper->makeBaseUrl('full').'api?t=g&id='.$spot['messageid'].$this->_tplHelper->makeApiRequestString();
if ($this->_params['del'] == '1' && $this->_spotSec->allowed(SpotSecurity::spotsec_keep_own_watchlist, '')) {
$nzbUrl .= '&del=1';
} // if
$guid = $doc->createElement('guid', $spot['messageid']);
$guid->setAttribute('isPermaLink', 'false');
$item = $doc->createElement('item');
$item->appendChild($doc->createElement('title', htmlspecialchars(html_entity_decode($spot['title'], ENT_QUOTES, 'UTF-8'), ENT_XHTML, 'UTF-8')));
$item->appendChild($guid);
$item->appendChild($doc->createElement('link', $nzbUrl));
$item->appendChild($doc->createElement('pubDate', date('r', $spot['stamp'])));
$item->appendChild($doc->createElement('category', SpotCategories::HeadCat2Desc($spot['category']).' > '.SpotCategories::Cat2ShortDesc($spot['category'], $spot['subcata'])));
$channel->appendChild($item);
$enclosure = $doc->createElement('enclosure');
$enclosure->setAttribute('url', html_entity_decode($nzbUrl));
$enclosure->setAttribute('length', $spot['filesize']);
switch ($nzbhandling['prepare_action']) {
case 'zip': $enclosure->setAttribute('type', 'application/zip');
break;
default: $enclosure->setAttribute('type', 'application/x-nzb');
} // switch
$item->appendChild($enclosure);
$nabCat = explode('|', $this->Cat2NewznabCat($spot['category'], $spot['subcata'], $spot['subcatz']));
if ($nabCat[0] != '' && is_numeric($nabCat[0])) {
$attr = $doc->createElement('newznab:attr');
$attr->setAttribute('name', 'category');
$attr->setAttribute('value', $nabCat[0]);
$item->appendChild($attr);
$attr = $doc->createElement('newznab:attr');
$attr->setAttribute('name', 'category');
$attr->setAttribute('value', $nabCat[1]);
$item->appendChild($attr);
}
if (!empty($spot['subcatc'])) {
$nabCat = explode('|', $spot['subcatc']);
$count = 0;
$subs = [];
if (in_array('c2', $nabCat) === true || in_array('c1', $nabCat) === true || in_array('c6', $nabCat) === true) {
$subs[$count] = 'dutch';
$count += 1;
} // if
if (in_array('c3', $nabCat) === true || in_array('c4', $nabCat) === true || in_array('c7', $nabCat) === true) {
$subs[$count] = 'english';
$count += 1;
} // if
if ($count != 0) {
$attr = $doc->createElement('newznab:attr');
$attr->setAttribute('name', 'subs');
$attr->setAttribute('value', implode(',', $subs));
$item->appendChild($attr);
}
} // if
$attr = $doc->createElement('newznab:attr');
$attr->setAttribute('name', 'size');
$attr->setAttribute('value', $spot['filesize']);
$item->appendChild($attr);
if ($this->_params['extended'] != '0') {
$attr = $doc->createElement('newznab:attr');
$attr->setAttribute('name', 'poster');
$attr->setAttribute('value', $spot['poster'].'@spot.net');
$item->appendChild($attr);
$attr = $doc->createElement('newznab:attr');
$attr->setAttribute('name', 'comments');
$attr->setAttribute('value', $spot['commentcount']);
$item->appendChild($attr);
} // if
if ($this->_tplHelper->isSpotNew($spot)) {
$attr = $doc->createElement('new', 'true');
$item->appendChild($attr);
} else {
$attr = $doc->createElement('new', 'false');
$item->appendChild($attr);
}
if (!empty($spot['hasbeenseen'])) {
$attr = $doc->createElement('seen', 'true');
$item->appendChild($attr);
} else {
$attr = $doc->createElement('seen', 'false');
$item->appendChild($attr);
}
if (!empty($spot['hasbeendownloaded'])) {
$attr = $doc->createElement('downloaded', 'true');
$item->appendChild($attr);
} else {
$attr = $doc->createElement('downloaded', 'false');
$item->appendChild($attr);
}
} // foreach
$this->sendContentTypeHeader('xml');
echo $doc->saveXML();
}
}
// showResults
public function spotDetails($outputtype)
{
if (empty($this->_params['messageid'])) {
$this->showApiError(200);
return;
} // if
// Make sure the specific permissions are implemented
$this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_view_spotdetail, '');
// spot ophalen
try {
$fullSpot = $this->_tplHelper->getFullSpot($this->_params['messageid'], true);
} catch (Exception $x) {
$this->showApiError(302);
return;
} // catch
$nzbhandling = $this->_currentSession['user']['prefs']['nzbhandling'];
/*
* Ugly @ operator, but we cannot reliably fix the library for the moment
*/
$spot = @$this->_tplHelper->formatSpot($fullSpot);
if ($outputtype == 'json') {
$doc = [];
$doc['ID'] = $spot['id'];
$doc['name'] = $spot['title'];
$doc['size'] = $spot['filesize'];
$doc['adddate'] = date('Y-m-d H:i:s', $spot['stamp']);
$doc['guid'] = $spot['messageid'];
$doc['fromname'] = $spot['poster'];
$doc['completion'] = 100;
$doc['description'] = json_encode($spot['description']);
$cat = '';
if (!empty($spot['subcatz'])) {
$nabCat = explode('|', $this->Cat2NewznabCat($spot['category'], $spot['subcatz'], $spot['subcata']));
if ($nabCat[0] != '' && is_numeric($nabCat[0])) {
$cat = $nabCat[0];
} // if
} // if
$nabCat = explode('|', $this->Cat2NewznabCat($spot['category'], $spot['subcata'], $spot['subcatz']));
if ($nabCat[0] != '' && is_numeric($nabCat[0])) {
$doc['categoryID'] = $nabCat[0];
$cat .= implode(',', $nabCat);
} // if
$nabCat = explode('|', $this->Cat2NewznabCat($spot['category'], $spot['subcatb'], $spot['subcatz']));
if ($nabCat[0] != '' && is_numeric($nabCat[0])) {
$cat .= ','.$nabCat[0];
} // if
$doc['comments'] = $spot['commentcount'];
$doc['category_name'] = SpotCategories::HeadCat2Desc($spot['category']).': '.SpotCategories::Cat2ShortDesc($spot['category'], $spot['subcata']);
$doc['category_ids'] = $cat;
echo json_encode($doc);
} else {
$nzbUrl = $this->_tplHelper->makeBaseUrl('full').'api?t=g&id='.$spot['messageid'].$this->_tplHelper->makeApiRequestString();
// Opbouwen XML
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$rss = $doc->createElement('rss');
$rss->setAttribute('version', '2.0');
$rss->setAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom');
$rss->setAttribute('xmlns:newznab', 'http://www.newznab.com/DTD/2010/feeds/attributes/');
$rss->setAttribute('encoding', 'utf-8');
$doc->appendChild($rss);
$channel = $doc->createElement('channel');
$channel->appendChild($doc->createElement('title', 'Spotweb'));
$channel->appendChild($doc->createElement('language', 'nl'));
$channel->appendChild($doc->createElement('description', 'Spotweb Index Api Detail'));
$channel->appendChild($doc->createElement('link', $this->_settings->/** @scrutinizer ignore-call */ get('spotweburl')));
$channel->appendChild($doc->createElement('webMaster', $this->_currentSession['user']['mail'].' ('.$this->_currentSession['user']['firstname'].' '.$this->_currentSession['user']['lastname'].')'));
$channel->appendChild($doc->createElement('category', ''));
$rss->appendChild($channel);
$image = $doc->createElement('image');
$image->appendChild($doc->createElement('url', $this->_tplHelper->makeImageUrl($spot, 300, 300)));
$image->appendChild($doc->createElement('title', 'Spotweb Index'));
$image->appendChild($doc->createElement('link', $this->_settings->/** @scrutinizer ignore-call */ get('spotweburl')));
$image->appendChild($doc->createElement('description', 'Visit Spotweb Index'));
$channel->appendChild($image);
$poster = (empty($spot['spotterid'])) ? $spot['poster'] : $spot['poster'].' ('.$spot['spotterid'].')';
$guid = $doc->createElement('guid', $spot['messageid']);
$guid->setAttribute('isPermaLink', 'false');
$description = $doc->createElement('description');
$descriptionCdata = $doc->createCDATASection($spot['description'].'<br /><font color="#ca0000">Door: '.$poster.'</font>');
$description->appendChild($descriptionCdata);
$item = $doc->createElement('item');
$item->appendChild($doc->createElement('title', htmlspecialchars($spot['title'], ENT_QUOTES, 'UTF-8')));
$item->appendChild($guid);
$item->appendChild($doc->createElement('link', $nzbUrl));
$item->appendChild($doc->createElement('pubDate', date('r', $spot['stamp'])));
$item->appendChild($doc->createElement('category', SpotCategories::HeadCat2Desc($spot['category']).' > '.SpotCategories::Cat2ShortDesc($spot['category'], $spot['subcata'])));
$item->appendChild($description);
$channel->appendChild($item);
$enclosure = $doc->createElement('enclosure');
$enclosure->setAttribute('url', html_entity_decode($nzbUrl));
$enclosure->setAttribute('length', $spot['filesize']);
switch ($nzbhandling['prepare_action']) {
case 'zip': $enclosure->setAttribute('type', 'application/zip');
break;
default: $enclosure->setAttribute('type', 'application/x-nzb');
} // switch
$item->appendChild($enclosure);
$nabCat = explode('|', $this->Cat2NewznabCat($spot['category'], $spot['subcata'], $spot['subcatz']));
if ($nabCat[0] != '' && is_numeric($nabCat[0])) {
$attr = $doc->createElement('newznab:attr');
$attr->setAttribute('name', 'category');
$attr->setAttribute('value', $nabCat[0]);
$item->appendChild($attr);
$attr = $doc->createElement('newznab:attr');
$attr->setAttribute('name', 'category');
$attr->setAttribute('value', $nabCat[1]);
$item->appendChild($attr);
} // if
$attr = $doc->createElement('newznab:attr');
$attr->setAttribute('name', 'size');
$attr->setAttribute('value', $spot['filesize']);
$item->appendChild($attr);
$attr = $doc->createElement('newznab:attr');
$attr->setAttribute('name', 'poster');
$attr->setAttribute('value', $spot['poster'].'@spot.net ('.$spot['poster'].')');
$item->appendChild($attr);
$attr = $doc->createElement('newznab:attr');
$attr->setAttribute('name', 'comments');
$attr->setAttribute('value', $spot['commentcount']);
$item->appendChild($attr);
$this->sendContentTypeHeader('xml');
echo $doc->saveXML();
} // if
}
// spotDetails
public function getNzb()
{
if ($this->_params['del'] == '1' && $this->_spotSec->allowed(SpotSecurity::spotsec_keep_own_watchlist, '')) {
$spot = $this->_db->getFullSpot($this->_params['messageid'], $this->_currentSession['user']['userid']);
if ($spot['watchstamp'] !== null) {
$this->_db->removeFromWatchList($this->_params['messageid'], $this->_currentSession['user']['userid']);
$spotsNotifications = new SpotNotifications($this->_db, $this->_settings, $this->_currentSession);
$spotsNotifications->sendWatchlistHandled('remove', $this->_params['messageid']);
} // if
} // if
header('Location: '.$this->_tplHelper->makeBaseUrl('full').'?page=getnzb&action=display&messageid='.$this->_params['messageid'].html_entity_decode($this->_tplHelper->makeApiRequestString()));
}
// getNzb
public function caps($outputtype)
{
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$caps = $doc->createElement('caps');
$doc->appendChild($caps);
$server = $doc->createElement('server');
$server->setAttribute('version', '0.1');
$server->setAttribute('title', 'Spotweb');
$server->setAttribute('strapline', 'Spotweb API Index');
$server->setAttribute('email', $this->_currentSession['user']['mail'].' ('.$this->_currentSession['user']['firstname'].' '.$this->_currentSession['user']['lastname'].')');
$server->setAttribute('url', $this->_settings->/** @scrutinizer ignore-call */ get('spotweburl'));
$server->setAttribute('image', $this->_settings->/** @scrutinizer ignore-call */ get('spotweburl').'images/spotnet.gif');
$server->setAttribute('type', 'Spotweb');
$caps->appendChild($server);
$limits = $doc->createElement('limits');
$limits->setAttribute('max', '500');
$limits->setAttribute('default', $this->_currentSession['user']['prefs']['perpage']);
$caps->appendChild($limits);
if (($this->_settings->get('retention') > 0) && ($this->_settings->get('retentiontype') == 'everything')) {
$ret = $doc->createElement('retention');
$ret->setAttribute('days', $this->_settings->get('retention'));
$caps->appendChild($ret);
} // if
$reg = $doc->createElement('registration');
$reg->setAttribute('available', 'no');
$reg->setAttribute('open', 'no');
$caps->appendChild($reg);
$searching = $doc->createElement('searching');
$caps->appendChild($searching);
$search = $doc->createElement('search');
$search->setAttribute('available', 'yes');
$searching->appendChild($search);
$tvsearch = $doc->createElement('tv-search');
$tvsearch->setAttribute('available', 'yes');
$tvsearch->setAttribute('supportedParams', 'q,rid,tvmazeid,season,ep');
$searching->appendChild($tvsearch);
$moviesearch = $doc->createElement('movie-search');
$moviesearch->setAttribute('available', 'yes');
$moviesearch->setAttribute('supportedParams', 'q,imdbid');
$searching->appendChild($moviesearch);
$pcsearch = $doc->createElement('pc-search');
$pcsearch->setAttribute('available', 'yes');
$pcsearch->setAttribute('supportedParams', 'q');
$searching->appendChild($pcsearch);
$audiosearch = $doc->createElement('audio-search');
$audiosearch->setAttribute('available', 'yes');
$searching->appendChild($audiosearch);
$categories = $doc->createElement('categories');
$caps->appendChild($categories);
foreach ($this->categories() as $category) {
$cat = $doc->createElement('category');
$cat->setAttribute('id', $category['cat']);
$cat->setAttribute('name', $category['name']);
$categories->appendChild($cat);
foreach ($category['subcata'] as $name => $subcata) {
$subCat = $doc->createElement('subcat');
$subCat->setAttribute('id', $subcata);
$subCat->setAttribute('name', $name);
$cat->appendChild($subCat);
} // foreach
} // foreach
if ($outputtype == 'json') {
$this->sendContentTypeHeader('json');
echo Zend\Xml2Json\Xml2Json::fromXml($doc->saveXML(), false);
} else {
$this->sendContentTypeHeader('xml');
echo $doc->saveXML();
}
}
// caps
public function Cat2NewznabCat($hcat, $cat, $catZ)
{
$catList = explode('|', $cat);
$cat = $catList[0];
$nr = substr($cat, 1);
$zcatList = explode('|', $catZ);
$zcat = $zcatList[0];
$znr = substr($zcat, 1);
//$znr = min(4,$znr);
if ($znr == 2) {
$znr = 0;
}
// Als $nr niet gevonden kan worden is dat niet erg, het mag echter
// geen Notice veroorzaken.
if (!empty($cat[0])) {
switch ($cat[0]) {
case 'a':
if ($hcat == 0 or $hcat == 1) {
$newznabcat = $this->spotAcat2nabcat();
$r = @$newznabcat[$hcat][$znr][$nr];
if ($r == null) {
$r = '';
}
return $r;
} else {
$newznabcat = $this->spotAcat2nabcat();
$r = @$newznabcat[$hcat][$znr][$nr];
if ($r = null) {
$r = '';
}
return $r;
}
case 'b': $newznabcat = $this->spotBcat2nabcat();
return @$newznabcat[$nr];
break;
} // switch
} // if
return '';
}
// Cat2NewznabCat
public function showApiError($errcode = 42)
{
switch ($errcode) {
case 100: $errtext = 'Incorrect user credentials';
break;
case 101: $errtext = 'Account suspended';
break;
case 102: $errtext = 'Insufficient priviledges/not authorized';
break;
case 103: $errtext = 'Registration denied';
break;
case 104: $errtext = 'Registrations are closed';
break;
case 105: $errtext = 'Invalid registration (Email Address Taken)';
break;
case 106: $errtext = 'Invalid registration (Email Address Bad Format)';
break;
case 107: $errtext = 'Registration Failed (Data error)';
break;
case 200: $errtext = 'Missing parameter';
break;
case 201: $errtext = 'Incorrect parameter';
break;
case 202: $errtext = 'No such function';
break;
case 203: $errtext = 'Function not available';
break;
case 300: $errtext = 'On TVSearch no q, tvmaze or rid parameter present';
break;
case 301: $errtext = 'IMDB information returned is invalid';
break;
case 302: $errtext = 'Error in fetching spot information';
break;
case 500: $errtext = 'Request limit reached';
break;
case 501: $errtext = 'Download limit reached';
break;
default: $errtext = 'Unknown error';
break;
} // switch
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$error = $doc->createElement('error');
$error->setAttribute('code', $errcode);
$error->setAttribute('description', $errtext);
$doc->appendChild($error);
$this->sendContentTypeHeader('xml');
echo $doc->saveXML();
}
// showApiError
public function categories()
{
return [
['name' => 'Console',
'cat' => '1000',
'subcata' => ['NDS' => '1010',
'PSP' => '1020',
'Wii' => '1030',
'Xbox' => '1040',
'Xbox 360' => '1050',
'PS3' => '1080', ],
], ['name' => 'Movies',
'cat' => '2000',
'subcata' => ['SD' => '2030',
'HD' => '2040',
'BluRay' => '2050',
'3D' => '2060', ],
], ['name' => 'Audio',
'cat' => '3000',
'subcata' => ['MP3' => '3010',
'Video' => '3020',
'Lossless' => '3040', ],
], ['name' => 'PC',
'cat' => '4000',
'subcata' => ['Windows' => '4020',
'Mac' => '4030',
'Mobile' => '4040',
'Games' => '4050', ],
], ['name' => 'TV',
'cat' => '5000',
'subcata' => ['Foreign' => '5020',
'SD' => '5030',
'HD' => '5040',
'Other' => '5050',
'Sport' => '5060',
'Anime' => '5070', ],
], ['name' => 'XXX',
'cat' => '6000',
'subcata' => ['DVD' => '6010',
'WMV' => '6020',
'XviD' => '6030',
'x264' => '6040', ],
], ['name' => 'Other',
'cat' => '7000',
'subcata' => ['Misc' => '7010',
'Ebook' => '7020', ],
],
];
}
// categories
public function nabcat2spotcat($cat)
{
switch ($cat) {
case 1000: return 'cat2_a3,cat2_a4,cat2_a5,cat2_a6,cat2_a7,cat2_a8,cat2_a9,cat2_a10,cat2_a11,cat2_a12';
case 1010: return 'cat2_a10';
case 1020: return 'cat2_a5';
case 1030: return 'cat2_a11';
case 1040: return 'cat2_a6';
case 1050: return 'cat2_a7';
case 1060: return 'cat2_a7';
case 2000: return 'cat0_z0';
case 2010:
case 2030: return 'cat0_z0_a0,cat0_z0_a1,cat0_z0_a2,cat0_z0_a3,cat0_z0_a10'; // Movies/SD
case 2040: return 'cat0_z0_a4,cat0_z0_a7,cat0_z0_a8,cat0_z0_a9'; // Movies/HD
case 2050: return 'cat0_z0_a6'; // Movies/BluRay
case 2060: return 'cat0_z0_a14'; // Movies/3D
case 3000: return 'cat1_a';
case 3010: return 'cat1_a0';
case 3020: return 'cat0_d13';
case 3030: return 'cat1_z3';
case 3040: return 'cat1_a2,cat1_a4,cat1_a7,cat1_a8';
case 4000: return 'cat3';
case 4020: return 'cat3_a0';
case 4030: return 'cat3_a1';
case 4040: return 'cat3_a4,cat3_a5,cat3_a6,cat3_a7';
case 4050: return 'cat2_a';
case 5000: return 'cat0_z1,cat0_z1_d';
case 5020: return 'cat0_z1,cat0_z1_d';
case 5030: return 'cat0_z1_a0,cat0_z1_a1,cat0_z1_a2,cat0_z1_a3,cat0_z1_a10,cat0_z1_d';