forked from spotweb/spotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
527 lines (473 loc) · 19.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
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
<?php
//error_reporting(2147483647);
//ini_set('display_errors', 1);
require_once __DIR__.'/vendor/autoload.php';
//- main() -#
try {
SpotTranslation::initialize('en_US');
SpotTiming::enable();
SpotTiming::start('total');
/*
* Initialize the Spotweb base classes
*/
$bootstrap = new Bootstrap();
list($settings, $daoFactory, $req) = $bootstrap->boot();
/*
* Enable debug logging mechanism if timing is enabled
*/
if ($settings->get('enable_timing')) {
SpotDebug::enable(SpotDebug::TRACE);
} // if
// helper functions for passed variables
$page = $req->getDef('page', 'index');
// Retrieve the users object of the user which is logged on
SpotTiming::start('auth');
$svcUserAuth = new Services_User_Authentication($daoFactory, $settings);
if ($req->doesExist('apikey')) {
$currentSession = $svcUserAuth->verifyApi($req->getDef('apikey', ''));
} else {
$currentSession = $svcUserAuth->useOrStartSession(false);
} // if
/*
* If three is no user object, we don't have a security system
* either. Without a security system we cannot boot, so fatal
*/
if ($currentSession === false) {
if ($req->doesExist('apikey')) {
$currentSession = $svcUserAuth->useOrStartSession(true);
throw new PermissionDeniedException(SpotSecurity::spotsec_consume_api, 'invalid API key');
} else {
throw new SqlErrorException('Unable to create session');
} // else
} // if
SpotTiming::stop('auth');
/*
* And check if the security groups need updating
*/
if (!$currentSession['security']->securityValid()) {
throw new SecurityNotUpgradedException();
} // if
// User session has been loaded, let's translate the categories
if ($currentSession['user']['prefs']['user_language'] != 'en_US') {
SpotTranslation::initialize($currentSession['user']['prefs']['user_language']);
} // if
SpotCategories::startTranslation();
/*
* Let the form handler know what userid we are using so
* we can make the CSRF cookie be user-bounded
*/
$req->setUserId($currentSession['user']['userid']);
/*
* Only now it is safe to check wether the user is actually allowed
* to authenticate with an API key
*/
if ($req->doesExist('apikey')) {
/*
* To use the Spotweb API we need the actual permission
*/
$currentSession['security']->fatalPermCheck(SpotSecurity::spotsec_consume_api, '');
/*
* but we also need a specific permission, because else things could
* be automated which we simply do not want to be automated
*/
$currentSession['security']->fatalPermCheck(SpotSecurity::spotsec_consume_api, $page);
} // if
SpotTiming::start('renderpage');
switch ($page) {
case 'render':
$svcUserFilters = new Services_User_Filters($daoFactory, $settings);
$page = new SpotPage_render(
$daoFactory,
$settings,
$currentSession,
$req->getDef('tplname', ''),
['search' => $req->getDef('search', $svcUserFilters->getIndexFilter($currentSession['user']['userid'])),
'data' => $req->getDef('data', []),
'messageid' => $req->getDef('messageid', ''),
'pagenr' => $req->getDef('pagenr', 0),
'perpage' => $req->getDef('perpage', 10),
'sortby' => $req->getDef('sortby', ''),
'sortdir' => $req->getDef('sortdir', ''), ]
);
$page->render();
break;
// render
case 'getspot':
if (strpos($_SERVER['HTTP_USER_AGENT'], 'SABnzbd+') === 0) {
$page = new SpotPage_getnzb(
$daoFactory,
$settings,
$currentSession,
['messageid' => $req->getDef('messageid', ''),
'action' => $req->getDef('action', 'display'),
'username' => $req->getDef('username', ''),
'apikey' => $req->getDef('apikey', ''), ]
);
} else {
$page = new SpotPage_getspot($daoFactory, $settings, $currentSession, ['messageid' => $req->getDef('messageid', '')]);
} // else
$page->render();
break;
// getspot
case 'getnzb':
$page = new SpotPage_getnzb(
$daoFactory,
$settings,
$currentSession,
['messageid' => $req->getDef('messageid', ''),
'action' => $req->getDef('action', 'display'),
'username' => $req->getDef('username', ''),
'apikey' => $req->getDef('apikey', ''), ]
);
$page->render();
break;
case 'erasedls':
$page = new SpotPage_erasedls($daoFactory, $settings, $currentSession);
$page->render();
break;
// erasedls
case 'catsjson':
$svcUserFilters = new Services_User_Filters($daoFactory, $settings);
$page = new SpotPage_catsjson(
$daoFactory,
$settings,
$currentSession,
['search' => $req->getDef('search', $svcUserFilters->getIndexFilter($currentSession['user']['userid'])),
'subcatz' => $req->getDef('subcatz', '*'),
'category' => $req->getDef('category', '*'),
'rendertype' => $req->getDef('rendertype', 'tree'),
'disallowstrongnot' => $req->getDef('disallowstrongnot', ''), ]
);
$page->render();
break;
// catsjson
case 'markallasread':
$page = new SpotPage_markallasread($daoFactory, $settings, $currentSession);
$page->render();
break;
// markallasread
case 'getimage':
$page = new SpotPage_getimage(
$daoFactory,
$settings,
$currentSession,
['messageid' => $req->getDef('messageid', ''),
'image' => $req->getDef('image', []), ]
);
$page->render();
break;
case 'newznabapi':
$page = new SpotPage_newznabapi(
$daoFactory,
$settings,
$currentSession,
['t' => $req->getDef('t', ''),
'messageid' => $req->getDef('id', ''),
'apikey' => $req->getDef('apikey', ''),
'q' => $req->getDef('q', ''),
'limit' => $req->getDef('limit', ''),
'cat' => $req->getDef('cat', ''),
'imdbid' => $req->getDef('imdbid', ''),
'artist' => $req->getDef('artist', ''),
'rid' => $req->getDef('rid', ''),
'tvmazeid' => $req->getDef('tvmazeid', ''),
'season' => $req->getDef('season', ''),
'ep' => $req->getDef('ep', ''),
'o' => $req->getDef('o', ''),
'extended' => $req->getDef('extended', ''),
'maxage' => $req->getDef('maxage', ''),
'offset' => $req->getDef('offset', ''),
'del' => $req->getDef('del', ''),
'spotcat' => $req->getDef('spotcat', ''),
'noalt' => $req->getDef('noalt', ''),
'poster' => $req->getDef('poster', ''),
]
);
$page->render();
break;
// api
case 'rss':
$svcUserFilters = new Services_User_Filters($daoFactory, $settings);
$page = new SpotPage_rss(
$daoFactory,
$settings,
$currentSession,
['search' => $req->getDef('search', $svcUserFilters->getIndexFilter($currentSession['user']['userid'])),
'page' => $req->getDef('page', 0),
'sortby' => $req->getDef('sortby', ''),
'sortdir' => $req->getDef('sortdir', ''),
'username' => $req->getDef('username', ''),
'apikey' => $req->getDef('apikey', ''), ]
);
$page->render();
break;
// rss
case 'statics':
$page = new SpotPage_statics(
$daoFactory,
$settings,
$currentSession,
['type' => $req->getDef('type', '')]
);
$page->render();
break;
// statics
case 'createuser':
$page = new SpotPage_createuser(
$daoFactory,
$settings,
$currentSession,
['createuserform' => $req->getForm('createuserform')]
);
$page->render();
break;
// createuser
case 'editsettings':
$page = new SpotPage_editsettings(
$daoFactory,
$settings,
$currentSession,
['editsettingsform' => $req->getForm('editsettingsform')]
);
$page->render();
break;
// editsettings
case 'edituserprefs':
$page = new SpotPage_edituserprefs(
$daoFactory,
$settings,
$currentSession,
['edituserprefsform' => $req->getForm('edituserprefsform'),
'userid' => $req->getDef('userid', ''),
'data' => $req->getDef('data', []),
'dialogembedded' => $req->getDef('dialogembedded', 0), ]
);
$page->render();
break;
// edituserprefs
case 'editsecgroup':
$page = new SpotPage_editsecgroup(
$daoFactory,
$settings,
$currentSession,
['editsecgroupform' => $req->getForm('editsecgroupform'),
'groupid' => $req->getDef('groupid', 0), ]
);
$page->render();
break;
// editsecgroup
case 'editfilter':
$page = new SpotPage_editfilter(
$daoFactory,
$settings,
$currentSession,
['editfilterform' => $req->getForm('editfilterform'),
'orderfilterslist' => $req->getDef('orderfilterslist', []),
'search' => $req->getDef('search', []),
'sorton' => $req->getDef('sortby', ''),
'sortorder' => $req->getDef('sortdir', ''),
'filterid' => $req->getDef('filterid', 0),
'data' => $req->getDef('data', []), ]
);
$page->render();
break;
// editfilter
case 'edituser':
$page = new SpotPage_edituser(
$daoFactory,
$settings,
$currentSession,
['edituserform' => $req->getForm('edituserform'),
'userid' => $req->getDef('userid', ''), ]
);
$page->render();
break;
// edituser
case 'editspot':
$page = new SpotPage_editspot(
$daoFactory,
$settings,
$currentSession,
['editspotform' => $req->getForm('editspotform'),
'messageid' => $req->getDef('messageid', ''), ]
);
$page->render();
break;
// editspot
case 'login':
$page = new SpotPage_login(
$daoFactory,
$settings,
$currentSession,
['loginform' => $req->getForm('loginform'),
'data' => $req->getDef('data', []), ]
);
$page->render();
break;
// login
case 'postcomment':
$page = new SpotPage_postcomment(
$daoFactory,
$settings,
$currentSession,
['commentform' => $req->getForm('postcommentform'),
'inreplyto' => $req->getDef('inreplyto', ''), ]
);
$page->render();
break;
// postcomment
case 'postspot':
$page = new SpotPage_postspot(
$daoFactory,
$settings,
$currentSession,
['spotform' => $req->getForm('newspotform')]
);
$page->render();
break;
// postspot
case 'reportpost':
$page = new SpotPage_reportpost(
$daoFactory,
$settings,
$currentSession,
['reportform' => $req->getForm('postreportform'),
'inreplyto' => $req->getDef('inreplyto', ''), ]
);
$page->render();
break;
// reportpost
case 'blacklistspotter':
$page = new SpotPage_blacklistspotter(
$daoFactory,
$settings,
$currentSession,
['blform' => $req->getForm('blacklistspotterform')]
);
$page->render();
break;
// blacklistspotter
case 'logout':
$page = new SpotPage_logout($daoFactory, $settings, $currentSession);
$page->render();
break;
// logout
case 'nzbhandlerapi':
$page = new SpotPage_nzbhandlerapi(
$daoFactory,
$settings,
$currentSession,
['nzbhandlerapikey' => $req->getDef('nzbhandlerapikey', ''),
'action' => $req->getDef('action', ''),
'limit' => $req->getDef('limit', ''),
'id' => $req->getDef('id', ''),
'category' => $req->getDef('category', ''),
'priority' => $req->getDef('priority', ''),
'password' => $req->getDef('password', ''),
'name' => $req->getDef('name', ''), ]
);
$page->render();
break;
// nzbhandlerapi
case 'twitteroauth':
$page = new SpotPage_twitteroauth(
$daoFactory,
$settings,
$currentSession,
['action' => $req->getDef('action', ''),
'pin' => $req->getDef('pin', ''), ]
);
$page->render();
break;
// twitteroauth
case 'statistics':
$page = new SpotPage_statistics(
$daoFactory,
$settings,
$currentSession,
['limit' => $req->getDef('limit', '')]
);
$page->render();
break;
// statistics
default:
SpotTiming::start('renderpage->case-default');
if ((empty($_SERVER['HTTP_X_PURPOSE']) ? '' : $_SERVER['HTTP_X_PURPOSE']) == 'preview') {
$page = new SpotPage_getimage(
$daoFactory,
$settings,
$currentSession,
['messageid' => $req->getDef('messageid', ''),
'image' => ['type' => 'speeddial'], ]
);
} else {
$svcUserFilters = new Services_User_Filters($daoFactory, $settings);
$page = new SpotPage_index(
$daoFactory,
$settings,
$currentSession,
['search' => $req->getDef('search', $svcUserFilters->getIndexFilter($currentSession['user']['userid'])),
'pagenr' => $req->getDef('pagenr', 0),
'sortby' => $req->getDef('sortby', ''),
'sortdir' => $req->getDef('sortdir', ''),
'messageid' => $req->getDef('messageid', ''),
'action' => $req->getDef('action', ''),
'data' => $req->getDef('data', []), ]
);
} // if
SpotTiming::stop('renderpage->case-default');
$page->render();
break;
// default
} // switch
SpotTiming::stop('renderpage');
// timing
SpotTiming::stop('total');
// enable or disable timer
if ($settings->get('enable_timing') && (!in_array($req->getDef('page', ''), ['catsjson', 'statics', 'getnzb', 'getnzbmobile', 'markallasread']))) {
SpotTiming::display();
} // if
} catch (PermissionDeniedException $x) {
/*
* We try to render a permission denied error using the already created
* renderer first. We do this, so pages which are supposed to output
* XML, can also output their errors using XML.
*
* If no page is initiated just yet, we create an basic renderer object
* to render an error page
*/
if (!($page instanceof SpotPage_Abs)) {
$page = new SpotPage_render($daoFactory, $settings, $currentSession, '', []);
} // if
$page->permissionDenied($x, $page, $req->getHttpReferer());
} // PermissionDeniedException
catch (InvalidOwnSettingsSettingException $x) {
echo 'There is an error in your ownsettings.php<br><br>'.PHP_EOL;
echo nl2br($x->getMessage());
} // InvalidOwnSettingsSettingException
catch (OwnsettingsCreatedOutputException $x) {
echo 'ownsettings.php or dbsettings.inc.php created output. Please make sure theese files do not contain a PHP closing tag ( ?> ) and no information before the PHP opening tag ( <?php )<br><br>'.PHP_EOL;
echo nl2br($x->getMessage()).PHP_EOL;
} // OwnsettingsCreatedOutputException
catch (SchemaNotUpgradedException $x) {
echo "Database schema has been changed. Please run 'bin/upgrade-db.php' from an console window".PHP_EOL;
} // SchemaNotUpgradedException
catch (SecurityNotUpgradedException $x) {
echo "Spotweb contains updated security settings. Please run 'bin/upgrade-db.php' from a console window".PHP_EOL;
} // SecurityNotUpgradedException
catch (SettingsNotUpgradedException $x) {
echo "Spotweb contains updated global settings settings. Please run 'bin/upgrade-db.php' from a console window".PHP_EOL;
} // SecurityNotUpgradedException
catch (DatabaseConnectionException $x) {
echo 'Unable to connect to database: <br>'.PHP_EOL;
echo nl2br($x->getMessage()).PHP_EOL.'<br>'.PHP_EOL;
echo '<br><br>Please make sure your database server is up and running and your connection parameters are set<br>'.PHP_EOL;
} // DatabaseConnectionException
catch (Exception $x) {
echo 'SpotWeb v'.SPOTWEB_VERSION.' on PHP v'.PHP_VERSION.' crashed'.PHP_EOL;
if ((isset($settings) && is_object($settings) && $settings->get('enable_stacktrace')) || (!isset($settings))) {
var_dump($x);
} // if
echo $x->getMessage().PHP_EOL;
error_log('SpotWeb Exception occured: '.$x->getMessage());
} // catch