-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.php
552 lines (447 loc) · 15.1 KB
/
data.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
<?php
include_once 'enums.php';
include_once 'secrets.php';
const ITEMSPATH = '/Items/';
const USERSPATH = '/Users/';
const VIDEOSPATH = '/Videos/';
const PREFERENCESPATH = '/DisplayPreferences/usersettings/';
$apiCallCount = 0;
const CLIENTNAME = 'Jellyfin-NMT';
const CLIENTVERSION = '0.6.0';
const POSTCONTENTTYPE = 'application/json';
class Device
{
private const HTTP_USER_AGENT = 'HTTP_USER_AGENT';
public $name = 'My testbed class';
public $id = 1;
public $isNMT = false;
public function __construct()
{
$this->id = $_SERVER['REMOTE_ADDR'];
$this->isNMT = (stripos($_SERVER[self::HTTP_USER_AGENT], "Syabas") !== false);
if (stripos($_SERVER[self::HTTP_USER_AGENT], "Chrome") !== false) {
$this->name = 'Chrome';
} elseif ($this->isNMT) {
$this->name = 'Popcorn Hour';
} else {
$this->name = $_SERVER[self::HTTP_USER_AGENT];
}
}
}
$filterCategories = [
'Filters', 'Features', 'SeriesStatus', 'Genres', 'NameStartsWith', 'OfficialRatings', 'Years', 'Tags',
'hasSpecialFeature', 'hasSubtitles', 'hasTrailer', 'hasThemeSong', 'hasThemeVideo'
];
class UserItemsParams
{
public $Features = null;
public $Fields = null;
public $Filters = null;
public $Genres = null;
public $GroupItems = null;
public $ExcludeItemTypes = null;
public $IncludeItemTypes = null;
public $IsFavorite = null;
public $IsPlayed = null;
public $Limit = null;
public $NameStartsWith = null;
public $OfficialRatings = null;
public $ParentID = null;
public $ParentIndexNumber = null;
public $PersonIDs = null;
public $Recursive = null;
public $SeriesStatus = null;
public $SortBy = null;
public $SortOrder = null;
public $collapseBoxSetItems = null;
public $StartIndex = null;
public $StudioIDs = null;
public $Tags = null;
public $Years = null;
public const SORTNAME = 'SortName';
public const ASC = 'Ascending';
public const DESC = 'Descending';
private $defaultSortBy = UserItemsParams::SORTNAME;
public function setSortByDefault($value)
{
$this->defaultSortBy = $value;
}
public function setFromQueryString()
{
global $filterCategories;
$params = $_GET['params'];
if (is_array($params)) {
$params = array_map("htmlspecialchars", $_GET['params']);
} else {
$params = null;
}
$this->ParentID = $params['ParentID'];
if (empty($this->ParentID)) {
$this->ParentID = $_GET['topParentId'];
}
if (isset($_GET['clearfilter'])) {
$this->SortBy = $this->defaultSortBy;
} else {
$this->SortBy = empty($params['SortBy']) ? $this->defaultSortBy : $params['SortBy'];
$this->SortOrder = empty($params['SortOrder']) ? null : $params['SortOrder'];
$this->collapseBoxSetItems = empty($params['collapseBoxSetItems']) ? null : $params['collapseBoxSetItems'];
foreach ($filterCategories as $cat) {
$this->$cat = $params[$cat];
}
}
}
public function addParam($categoryName, $searchTerm)
{
$this->$categoryName = $searchTerm;
}
}
class ImageParams
{
const SMALLINDICATORS = 0;
const MEDIUMINDICATORS = 1;
const LARGEINDICATORS = 2;
public $height = null;
public $width = null;
public $maxHeight = null;
public $maxWidth = null;
public $quality = null;
public $tag = null;
public $unplayedCount = null;
public $AddPlayedIndicator = null;
public $percentPlayed = null;
public $mediaSourceCount = null;
public $specialFeatureCount = null;
public $indicatorSize = null;
public function __construct($height = null, $width = null, $tag = null)
{
$this->height = $height;
$this->width = $width;
$this->tag = $tag;
}
public function setIndicators($item, $size = ImageParams::LARGEINDICATORS)
{
$played = ($item->Type == ItemType::SERIES || $item->Type == ItemType::SEASON ? null : $item->UserData->Played);
$this->indicatorSize = $size;
$this->unplayedCount = getUnplayedCount($item);
$this->AddPlayedIndicator = $played;
$this->percentPlayed = $item->UserData->PlayedPercentage > 0 ? $item->UserData->PlayedPercentage : null;
$this->mediaSourceCount = $item->MediaSourceCount && $item->MediaSourceCount > 1 ? $item->MediaSourceCount : null;
$this->specialFeatureCount = $item->SpecialFeatureCount && $item->SpecialFeatureCount > 0 ? $item->SpecialFeatureCount : null;
}
}
function mapItemTypeToCollectionType($itemType)
{
$itemTypeToCollectionType = array(
ItemType::SERIES => CollectionType::TVSHOWS,
ItemType::SEASON => CollectionType::TVSHOWS, ItemType::EPISODE => CollectionType::TVSHOWS,
ItemType::MOVIE => CollectionType::MOVIES, ItemType::BOXSET => CollectionType::BOXSETS,
ItemType::PLAYLIST => CollectionType::PLAYLISTS, ItemType::MUSICVIDEO => CollectionType::MUSICVIDEOS
);
return $itemTypeToCollectionType[$itemType];
}
function mapFolderTypeToSingleItemType($folderType, $collectionType)
{
$collectionTypeToItemType = array(
CollectionType::TVSHOWS => ItemType::SERIES,
CollectionType::MOVIES => ItemType::MOVIE, CollectionType::BOXSETS => ItemType::BOXSET,
CollectionType::PLAYLISTS => ItemType::PLAYLIST, CollectionType::MUSICVIDEOS => ItemType::MUSICVIDEO
);
//folders are itemtypes
if ($folderType == ItemType::COLLECTIONFOLDER || $folderType == ItemType::USERVIEW) {
return $collectionTypeToItemType[$collectionType];
} else {
return $folderType;
}
}
function strboolNull($value)
{
if (is_null($value)) {
return null;
} else {
return strbool($value);
}
}
function strbool($value)
{
return $value ? 'true' : 'false';
}
function apiCall($path, $debug = false, $includeAPIKey = true)
{
global $api_url, $api_key, $apiCallCount;
$apiCallCount++;
$url = $api_url . $path;
if ($includeAPIKey) {
$url .= "&api_key=" . $api_key;
}
if ($debug || isset($_GET['debug'])) {
echo "<a href=\"" . $url . "\">url</a><br/>";
}
$json = @file_get_contents($url);
if (strpos($http_response_header[0], "200")) {
return json_decode($json);
} else {
return "";
}
}
//JF 10.7.0+ only supports JSON post calls
function apiCallPost($path, $post = null)
{
global $api_url;
$authHeaderFormat = 'X-Emby-Authorization: MediaBrowser Client="%s", Version="%s", Device="%s", DeviceId="%s"';
$tokenFormat = ', Token="%s"';
$dev = new Device;
$authHeader = sprintf($authHeaderFormat, CLIENTNAME, CLIENTVERSION, $dev->name, $dev->id);
if ($_SESSION["accessToken"]) {
//add token if exists to header
$authHeader .= sprintf($tokenFormat, $_SESSION["accessToken"]);
}
$opts = array(
'http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: ' . POSTCONTENTTYPE . "\r\n" . $authHeader
)
);
if ($post) {
$opts['http']['content'] = json_encode($post);
}
$context = stream_context_create($opts);
$url = $api_url . $path;
return json_decode(file_get_contents($url, false, $context));
}
function itemImageExists($itemId, $ImageType = ImageType::PRIMARY)
{
if ($itemId != '') {
$path = ITEMSPATH . $itemId . "/Images/?";
$images = apiCall($path);
$foundImages = array_filter($images, function ($image) use ($ImageType) {
return $image->ImageType == $ImageType;
});
return (count($foundImages) > 0);
} else {
return false;
}
}
function firstEpisodeFromSeason($seasonId, $seasonNumber)
{
//seasonNumber - don't include specials in regular seasons
$params = new UserItemsParams();
$params->Fields = 'Path';
$params->Limit = 1;
$params->ParentID = $seasonId;
$params->ParentIndexNumber = $seasonNumber;
$all_episodes = getUsersItems($params);
//return first
return $all_episodes->Items[0];
}
function firstEpisodeFromSeries($seriesId)
{
$params = new UserItemsParams();
$params->Fields = 'Path';
$params->Limit = 1;
$params->ParentID = $seriesId;
$params->IncludeItemTypes = ItemType::EPISODE;
$params->Recursive = true;
$all_episodes = getUsersItems($params);
return $all_episodes->Items[0];
}
function firstSeasonFromSeries($seriesId)
{
$params = new UserItemsParams();
$params->Limit = 1;
$params->ParentID = $seriesId;
$params->IncludeItemTypes = ItemType::SEASON;
$seasons = getUsersItems($params);
return $seasons->Items[0];
}
function latestSeasonFromSeries($seriesId)
{
$params = new UserItemsParams();
$params->ParentID = $seriesId;
$params->IncludeItemTypes = ItemType::SEASON;
$seasons = getUsersItems($params);
return $seasons->Items[$seasons->TotalRecordCount - 1];
}
function getSeasonFromSeriesBySeasonNumber($seriesId, $seasonNumber)
{
$params = new UserItemsParams();
$params->ParentID = $seriesId;
$params->IncludeItemTypes = ItemType::SEASON;
$seasons = getUsersItems($params);
$season = array_filter($seasons->Items, function ($s) use ($seasonNumber) { return $s->IndexNumber == $seasonNumber; });
return array_values($season)[0];
}
function YAMJpath($item)
{
global $jukebox_url;
return $jukebox_url . pathinfo($item->Path)['filename'] . ".html";
}
function getSeasonBySeriesIdURL($seriesId)
{
//find first episode in season, this will be YAMJ filename
return YAMJpath(firstEpisodeFromSeries($seriesId));
}
function getSeasonURL($SeasonId, $ParentIndexNumber)
{
//find first episode in season, this will be YAMJ filename
return YAMJpath(firstEpisodeFromSeason($SeasonId, $ParentIndexNumber));
}
function getUsersItems(UserItemsParams $params, $suffix = null)
{
global $user_id;
$path = USERSPATH . $user_id . ITEMSPATH . $suffix . '?';
$params->GroupItems = strboolNull($params->GroupItems);
$params->IsFavorite = strboolNull($params->IsFavorite);
$params->IsPlayed = strboolNull($params->IsPlayed);
$params->Recursive = strboolNull($params->Recursive);
$querystring = http_build_query($params);
return apiCall($path . $querystring);
}
function getUser()
{
global $user_id;
$path = USERSPATH . $user_id . '?';
return apiCall($path);
}
function getUserPreferences()
{
global $user_id;
$params = array(
'UserID' => $user_id,
'Client' => 'emby' //use settings from web client
);
$path = PREFERENCESPATH . '?' . http_build_query($params);
return apiCall($path);
}
function getUsersViews()
{
global $user_id;
$path = USERSPATH . $user_id . "/Views/?IncludeExternalContent=false";
return apiCall($path);
}
function getUsersPublic()
{
$path = USERSPATH . 'Public';
return apiCall($path, false, false);
}
function getLatest($itemType, $Limit, $ParentID)
{
global $GroupItems;
$params = new UserItemsParams();
$params->Fields = 'Path,MediaSourceCount,SpecialFeatureCount,ExtraIds';
$params->Limit = $Limit;
$params->IncludeItemTypes = $itemType;
$params->GroupItems = $GroupItems;
$params->ParentID = $ParentID;
return getUsersItems($params, 'Latest');
}
function getResume($Limit, $StartIndex = 0)
{
$params = new UserItemsParams();
$params->Fields = 'Path,MediaSourceCount,SpecialFeatureCount,ExtraIds';
$params->Limit = $Limit;
$params->StartIndex = $StartIndex;
return getUsersItems($params, 'Resume');
}
function getNextUp($Limit, $startIndex = 0, $enableRewatching = null)
{
global $user_id;
$params = array(
'UserID' => $user_id,
'Fields' => 'Path,SpecialEpisodeNumbers,MediaSourceCount,SpecialFeatureCount,ExtraIds',
'Limit' => $Limit,
'StartIndex' => $startIndex,
'enableRewatching' => strboolNull($enableRewatching)
);
$path = "/Shows/NextUp?" . http_build_query($params);
return apiCall($path);
}
function getItems(UserItemsParams $params)
{
//set defaults
$params->Fields = $params->Fields ?? 'Path,ChildCount,MediaSourceCount,SpecialFeatureCount,ExtraIds';
return getUsersItems($params);
}
function getItem($Id)
{
$params = new UserItemsParams();
return getUsersItems($params, $Id);
}
function getItemExtras($Id, $ExtrasType)
{
global $user_id;
$params = new UserItemsParams();
if ($ExtrasType == ExtrasType::ADDITIONALPARTS) {
$path = VIDEOSPATH . $Id . '/' . ExtrasType::ADDITIONALPARTS . '?UserID=' . $user_id;
//returns an Items block with counts and index
//just return Items so it's the same as other calls
return apiCall($path)->Items;
} else {
return getUsersItems($params, $Id . '/' . $ExtrasType);
}
}
function getSimilarItems($Id, $limit = null)
{
global $user_id;
$path = ITEMSPATH . $Id . "/Similar?UserID=" . $user_id;
$path .= $limit ? "&Limit=" . $limit : "";
return apiCall($path);
}
function getUnplayedCount($item)
{
global $libraryBrowse;
global $displayepisode;
if ($item->Type == ItemType::EPISODE) {
if (!$displayepisode) {
//API
$series = getItem($item->SeriesId);
$unplayedCount = $series->UserData->UnplayedItemCount;
}
} else {
$unplayedCount = $item->UserData->UnplayedItemCount;
}
//libraryBrowse, but should be based on if watched are hidden, like always in next up, or sometimes in latest
$minUnplayedCount = $libraryBrowse ? 0 : 1;
return $unplayedCount > $minUnplayedCount ? $unplayedCount : null;
}
function getFilters($parentID = null, $itemTypes = null, $Recursive = null)
{
global $user_id;
$path = ITEMSPATH . "Filters?UserID=" . $user_id;
$path .= $parentID ? "&ParentID=" . $parentID : "";
$path .= $itemTypes ? "&IncludeItemTypes=" . implode(",", $itemTypes) : "";
$path .= !is_null($Recursive) ? "&Recursive=" . strbool($Recursive) : "";
return apiCall($path);
}
function getImageURL($id, ImageParams $imageProperties, $imageType = null, $itemsOrUsers = null)
{
global $api_url;
$itemsOrUsers = $itemsOrUsers ?? 'Items';
$imageType = $imageType ?? ImageType::PRIMARY;
$imageProperties->AddPlayedIndicator = ($imageProperties->AddPlayedIndicator ? 'true' : null);
if ($imageProperties->unplayedCount
|| $imageProperties->AddPlayedIndicator
|| $imageProperties->percentPlayed
|| $imageProperties->mediaSourceCount
|| $imageProperties->specialFeatureCount
) {
// process image for indicators
return "imageWithIndicators.php?id=" . $id . "&imageType=" . $imageType . "&"
. http_build_query($imageProperties);
} else {
// clear imagesize
$imageProperties->indicatorSize = null;
}
return $api_url . "/" . $itemsOrUsers . "/" . $id . "/Images/" . $imageType . "?"
. http_build_query($imageProperties);
}
function getFavIconURL()
{
global $api_url;
return $api_url . "/web/favicon.ico";
}
function getLogoURL()
{
global $api_url;
return $api_url . "/web/assets/img/banner-light.png";
}