forked from spotweb/spotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·454 lines (399 loc) · 16.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
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
<?php
error_reporting(2147483647);
ini_set('display_errors', 1);
require_once "lib/SpotClassAutoload.php";
SpotClassAutoload::register();
#- 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();
// /* ------------------------------------ */
// $tmdb = new Services_MediaInformation_TheMovieDb($daoFactory, $settings);
// $tmdb->setSearchid(57158);
// var_dump($tmdb->retrieveInfo());
//
// die();
// /* ------------------------------------ */
/*
* Enable debug logging mechanism if timing is enabled
*/
if ($settings->get('enable_timing')) {
SpotDebug::enable(SpotDebug::TRACE, $daoFactory->getDebugLogDao());
} # 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', ''),
Array('search' => $req->getDef('search', $svcUserFilters->getIndexFilter($currentSession['user']['userid'])),
'data' => $req->getDef('data', array()),
'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,
Array('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, array('messageid' => $req->getDef('messageid', '')));
} # else
$page->render();
break;
} # getspot
case 'getnzb' : {
$page = new SpotPage_getnzb($daoFactory, $settings, $currentSession,
Array('messageid' => $req->getDef('messageid', ''),
'action' => $req->getDef('action', 'display'),
'username' => $req->getDef('username', ''),
'apikey' => $req->getDef('apikey', '')));
$page->render();
break;
}
case 'getnzbmobile' : {
$page = new SpotPage_getnzbmobile($daoFactory, $settings, $currentSession,
Array('messageid' => $req->getDef('messageid', ''),
'action' => $req->getDef('action', 'display')));
$page->render();
break;
} # getnzbmobile
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,
Array('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,
Array('messageid' => $req->getDef('messageid', ''),
'image' => $req->getDef('image', Array())));
$page->render();
break;
}
case 'newznabapi' : {
$page = new SpotPage_newznabapi($daoFactory, $settings, $currentSession,
Array('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', ''),
'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', '')
)
);
$page->render();
break;
} # api
case 'rss' : {
$svcUserFilters = new Services_User_Filters($daoFactory, $settings);
$page = new SpotPage_rss($daoFactory, $settings, $currentSession,
Array('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,
Array('type' => $req->getDef('type', '')));
$page->render();
break;
} # statics
case 'createuser' : {
$page = new SpotPage_createuser($daoFactory, $settings, $currentSession,
Array('createuserform' => $req->getForm('createuserform')));
$page->render();
break;
} # createuser
case 'editsettings' : {
$page = new SpotPage_editsettings($daoFactory, $settings, $currentSession,
Array('editsettingsform' => $req->getForm('editsettingsform')));
$page->render();
break;
} # editsettings
case 'edituserprefs' : {
$page = new SpotPage_edituserprefs($daoFactory, $settings, $currentSession,
Array('edituserprefsform' => $req->getForm('edituserprefsform'),
'userid' => $req->getDef('userid', ''),
'data' => $req->getDef('data', array()),
'dialogembedded' => $req->getDef('dialogembedded', 0)));
$page->render();
break;
} # edituserprefs
case 'editsecgroup' : {
$page = new SpotPage_editsecgroup($daoFactory, $settings, $currentSession,
Array('editsecgroupform' => $req->getForm('editsecgroupform'),
'groupid' => $req->getDef('groupid', 0)));
$page->render();
break;
} # editsecgroup
case 'editfilter' : {
$page = new SpotPage_editfilter($daoFactory, $settings, $currentSession,
Array('editfilterform' => $req->getForm('editfilterform'),
'orderfilterslist' => $req->getDef('orderfilterslist', array()),
'search' => $req->getDef('search', array()),
'sorton' => $req->getDef('sortby', ''),
'sortorder' => $req->getDef('sortdir', ''),
'filterid' => $req->getDef('filterid', 0),
'data' => $req->getDef('data', array())));
$page->render();
break;
} # editfilter
case 'edituser' : {
$page = new SpotPage_edituser($daoFactory, $settings, $currentSession,
Array('edituserform' => $req->getForm('edituserform'),
'userid' => $req->getDef('userid', '')));
$page->render();
break;
} # edituser
case 'editspot' : {
$page = new SpotPage_editspot($daoFactory, $settings, $currentSession,
Array('editspotform' => $req->getForm('editspotform'),
'messageid' => $req->getDef('messageid', '')));
$page->render();
break;
} # editspot
case 'login' : {
$page = new SpotPage_login($daoFactory, $settings, $currentSession,
Array('loginform' => $req->getForm('loginform'),
'data' => $req->getDef('data', array())));
$page->render();
break;
} # login
case 'postcomment' : {
$page = new SpotPage_postcomment($daoFactory, $settings, $currentSession,
Array('commentform' => $req->getForm('postcommentform'),
'inreplyto' => $req->getDef('inreplyto', '')));
$page->render();
break;
} # postcomment
case 'postspot' : {
$page = new SpotPage_postspot($daoFactory, $settings, $currentSession,
Array('spotform' => $req->getForm('newspotform')));
$page->render();
break;
} # postspot
case 'reportpost' : {
$page = new SpotPage_reportpost($daoFactory, $settings, $currentSession,
Array ('reportform' => $req->getForm('postreportform'),
'inreplyto' => $req->getDef('inreplyto', '')));
$page->render();
break;
} # reportpost
case 'versioncheck' : {
$page = new SpotPage_versioncheck($daoFactory, $settings, $currentSession, array());
$page->render();
break;
} # versioncheck
case 'blacklistspotter' : {
$page = new SpotPage_blacklistspotter($daoFactory, $settings, $currentSession,
Array ('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,
Array('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,
Array('action' => $req->getDef('action', ''),
'pin' => $req->getDef('pin', '')));
$page->render();
break;
} # twitteroauth
case 'statistics' : {
$page = new SpotPage_statistics($daoFactory, $settings, $currentSession,
Array('limit' => $req->getDef('limit', '')));
$page->render();
break;
} # statistics
default : {
SpotTiming::start('renderpage->case-default');
if (@$_SERVER['HTTP_X_PURPOSE'] == 'preview') {
$page = new SpotPage_getimage($daoFactory, $settings, $currentSession,
Array('messageid' => $req->getDef('messageid', ''),
'image' => array('type' => 'speeddial')));
} else {
$svcUserFilters = new Services_User_Filters($daoFactory, $settings);
$page = new SpotPage_index($daoFactory, $settings, $currentSession,
Array('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', array()))
);
} # 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', ''), array('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, '', array());
} # 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 'upgrade-db.php' from an console window". PHP_EOL;
} # SchemaNotUpgradedException
catch(SecurityNotUpgradedException $x) {
echo "Spotweb contains updated security settings. Please run 'upgrade-db.php' from a console window". PHP_EOL;
} # SecurityNotUpgradedException
catch(SettingsNotUpgradedException $x) {
echo "Spotweb contains updated global settings settings. Please run '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