homepage-connect-functions.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. <?php
  2. function homepageConnect($array){
  3. switch ($array['data']['action']) {
  4. case 'getPlexStreams':
  5. return plexConnect('streams');
  6. break;
  7. case 'getPlexRecent':
  8. return plexConnect('recent');
  9. break;
  10. case 'getPlexMetadata':
  11. return plexConnect('metadata',$array['data']['key']);
  12. break;
  13. case 'getEmbyStreams':
  14. return embyConnect('streams');
  15. break;
  16. case 'getEmbyRecent':
  17. return embyConnect('recent');
  18. break;
  19. case 'getEmbyMetadata':
  20. return embyConnect('metadata',$array['data']['key'],true);
  21. break;
  22. case 'getSabnzbd':
  23. return sabnzbdConnect();
  24. break;
  25. case 'getNzbget':
  26. return nzbgetConnect();
  27. break;
  28. case 'getTransmission':
  29. return transmissionConnect();
  30. break;
  31. case 'getqBittorrent':
  32. return qBittorrentConnect();
  33. break;
  34. default:
  35. # code...
  36. break;
  37. }
  38. }
  39. function streamType($value){
  40. if($value == "transcode" || $value == "Transcode"){
  41. return "Transcode";
  42. }elseif($value == "copy" || $value == "DirectStream"){
  43. return "Direct Stream";
  44. }elseif($value == "directplay" || $value == "DirectPlay"){
  45. return "Direct Play";
  46. }else{
  47. return "Direct Play";
  48. }
  49. }
  50. function resolveEmbyItem($itemDetails) {
  51. // Grab Each item info from Emby (extra call)
  52. $id = isset($itemDetails['NowPlayingItem']['Id']) ? $itemDetails['NowPlayingItem']['Id'] : $itemDetails['Id'];
  53. $url = qualifyURL($GLOBALS['embyURL']);
  54. $url = $url.'/Items?Ids='.$id.'&api_key='.$GLOBALS['embyToken'].'&Fields=Overview,People,Genres,CriticRating,Studios,Taglines';
  55. try{
  56. $options = (localURL($url)) ? array('verify' => false ) : array();
  57. $response = Requests::get($url, array(), $options);
  58. if($response->success){
  59. $item = json_decode($response->body,true)['Items'][0];
  60. }
  61. }catch( Requests_Exception $e ) {
  62. return false;
  63. };
  64. // Static Height & Width
  65. $height = 300;
  66. $width = 200;
  67. $nowPlayingHeight = 675;
  68. $nowPlayingWidth = 1200;
  69. $actorHeight = 450;
  70. $actorWidth = 300;
  71. $widthOverride = 100;
  72. // Cache Directories
  73. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  74. $cacheDirectoryWeb = 'plugins/images/cache/';
  75. // Types
  76. $embyItem['array-item'] = $item;
  77. $embyItem['array-itemdetails'] = $itemDetails;
  78. switch (@$item['Type']) {
  79. case 'Series':
  80. $embyItem['type'] = 'tv';
  81. $embyItem['title'] = $item['Name'];
  82. $embyItem['summary'] = '';
  83. $embyItem['ratingKey'] = $item['Id'];
  84. $embyItem['thumb'] = $item['Id'];
  85. $embyItem['key'] = $item['Id'] . "-list";
  86. $embyItem['nowPlayingThumb'] = $item['Id'];
  87. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  88. $embyItem['metadataKey'] = $item['Id'];
  89. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? 'Thumb' : (isset($item['BackdropImageTags'][0]) ? 'Backdrop' : '');
  90. break;
  91. case 'Episode':
  92. $embyItem['type'] = 'tv';
  93. $embyItem['title'] = $item['SeriesName'];
  94. $embyItem['summary'] = '';
  95. $embyItem['ratingKey'] = $item['Id'];
  96. $embyItem['thumb'] = (isset($item['SeriesId'])?$item['SeriesId']:$item['Id']);
  97. $embyItem['key'] = (isset($item['SeriesId'])?$item['SeriesId']:$item['Id']) . "-list";
  98. $embyItem['nowPlayingThumb'] = isset($item['ParentThumbItemId']) ? $item['ParentThumbItemId'] : (isset($item['ParentBackdropItemId']) ? $item['ParentBackdropItemId'] : false);
  99. $embyItem['nowPlayingKey'] = isset($item['ParentThumbItemId']) ? $item['ParentThumbItemId'].'-np' : (isset($item['ParentBackdropItemId']) ? $item['ParentBackdropItemId'].'-np' : false);
  100. $embyItem['metadataKey'] = $item['Id'];
  101. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? 'Thumb' : (isset($item['ParentBackdropImageTags'][0]) ? 'Backdrop' : '');
  102. $embyItem['nowPlayingTitle'] = @$item['SeriesName'].' - '.@$item['Name'];
  103. $embyItem['nowPlayingBottom'] = 'S'.@$item['ParentIndexNumber'].' · E'.@$item['IndexNumber'];
  104. break;
  105. case 'MusicAlbum':
  106. case 'Audio':
  107. $embyItem['type'] = 'music';
  108. $embyItem['title'] = $item['Name'];
  109. $embyItem['summary'] = '';
  110. $embyItem['ratingKey'] = $item['Id'];
  111. $embyItem['thumb'] = $item['Id'];
  112. $embyItem['key'] = $item['Id'] . "-list";
  113. $embyItem['nowPlayingThumb'] = (isset($item['AlbumId']) ? $item['AlbumId'] : @$item['ParentBackdropItemId']);
  114. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  115. $embyItem['metadataKey'] = isset($item['AlbumId']) ? $item['AlbumId'] : $item['Id'];
  116. $embyItem['nowPlayingImageType'] = (isset($item['ParentBackdropItemId']) ? "Primary" : "Backdrop");
  117. $embyItem['nowPlayingTitle'] = @$item['AlbumArtist'].' - '.@$item['Name'];
  118. $embyItem['nowPlayingBottom'] = @$item['Album'];
  119. break;
  120. case 'Movie':
  121. $embyItem['type'] = 'movie';
  122. $embyItem['title'] = $item['Name'];
  123. $embyItem['summary'] = '';
  124. $embyItem['ratingKey'] = $item['Id'];
  125. $embyItem['thumb'] = $item['Id'];
  126. $embyItem['key'] = $item['Id'] . "-list";
  127. $embyItem['nowPlayingThumb'] = $item['Id'];
  128. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  129. $embyItem['metadataKey'] = $item['Id'];
  130. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? "Thumb" : (isset($item['BackdropImageTags']) ? "Backdrop" : false);
  131. $embyItem['nowPlayingTitle'] = @$item['Name'];
  132. $embyItem['nowPlayingBottom'] = @$item['ProductionYear'];
  133. break;
  134. case 'Video':
  135. $embyItem['type'] = 'video';
  136. $embyItem['title'] = $item['Name'];
  137. $embyItem['summary'] = '';
  138. $embyItem['ratingKey'] = $item['Id'];
  139. $embyItem['thumb'] = $item['Id'];
  140. $embyItem['key'] = $item['Id'] . "-list";
  141. $embyItem['nowPlayingThumb'] = $item['Id'];
  142. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  143. $embyItem['metadataKey'] = $item['Id'];
  144. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? "Thumb" : (isset($item['BackdropImageTags']) ? "Backdrop" : false);
  145. $embyItem['nowPlayingTitle'] = @$item['Name'];
  146. $embyItem['nowPlayingBottom'] = @$item['ProductionYear'];
  147. break;
  148. default:
  149. return false;
  150. }
  151. $embyItem['uid'] = $item['Id'];
  152. $embyItem['imageType'] = (isset($item['ImageTags']['Primary']) ? "Primary" : false);
  153. $embyItem['elapsed'] = isset($itemDetails['PlayState']['PositionTicks']) && $itemDetails['PlayState']['PositionTicks'] !== '0' ? (int)$itemDetails['PlayState']['PositionTicks'] : null;
  154. $embyItem['duration'] = isset($itemDetails['NowPlayingItem']['RunTimeTicks']) ? (int)$itemDetails['NowPlayingItem']['RunTimeTicks'] : (int)$item['RunTimeTicks'];
  155. $embyItem['watched'] = ($embyItem['elapsed'] && $embyItem['duration'] ? floor(($embyItem['elapsed'] / $embyItem['duration']) * 100) : 0);
  156. $embyItem['transcoded'] = isset($itemDetails['TranscodingInfo']['CompletionPercentage']) ? floor((int)$itemDetails['TranscodingInfo']['CompletionPercentage']) : 100;
  157. $embyItem['stream'] = @$itemDetails['PlayState']['PlayMethod'];
  158. $embyItem['id'] = $item['ServerId'];
  159. $embyItem['session'] = @$itemDetails['DeviceId'];
  160. $embyItem['bandwidth'] = isset($itemDetails['TranscodingInfo']['Bitrate']) ? $itemDetails['TranscodingInfo']['Bitrate'] / 1000 : '';
  161. $embyItem['bandwidthType'] = 'wan';
  162. $embyItem['sessionType'] = (@$itemDetails['PlayState']['PlayMethod'] == 'Transcode') ? 'Transcoding' : 'Direct Playing';
  163. $embyItem['state'] = ((@(string)$itemDetails['PlayState']['IsPaused'] == '1') ? "pause" : "play");
  164. $embyItem['user'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? @(string)$itemDetails['UserName'] : "";
  165. $embyItem['userThumb'] = '';
  166. $embyItem['userAddress'] = "x.x.x.x";
  167. $embyItem['address'] = $GLOBALS['embyTabURL'] ? '' : '';
  168. $embyItem['nowPlayingOriginalImage'] = 'api/?v1/image&source=emby&type='.$embyItem['nowPlayingImageType'].'&img='.$embyItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$embyItem['nowPlayingKey'].'$'.randString();
  169. $embyItem['originalImage'] = 'api/?v1/image&source=emby&type='.$embyItem['imageType'].'&img='.$embyItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$embyItem['key'].'$'.randString();
  170. $embyItem['openTab'] = $GLOBALS['embyTabURL'] && $GLOBALS['embyTabName'] ? true : false;
  171. $embyItem['tabName'] = $GLOBALS['embyTabName'] ? $GLOBALS['embyTabName'] : '';
  172. // Stream info
  173. $embyItem['userStream'] = array(
  174. 'platform' => @(string)$itemDetails['Client'],
  175. 'product' => @(string)$itemDetails['Client'],
  176. 'device' => @(string)$itemDetails['DeviceName'],
  177. 'stream' => @$itemDetails['PlayState']['PlayMethod'],
  178. 'videoResolution' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]['Width']) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Width'] : '',
  179. 'throttled' => false,
  180. 'sourceVideoCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : '',
  181. 'videoCodec' => @$itemDetails['TranscodingInfo']['VideoCodec'],
  182. 'audioCodec' => @$itemDetails['TranscodingInfo']['AudioCodec'],
  183. 'sourceAudioCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][1]) ? $itemDetails['NowPlayingItem']['MediaStreams'][1]['Codec'] : (isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : ''),
  184. 'videoDecision' => streamType(@$itemDetails['PlayState']['PlayMethod']),
  185. 'audioDecision' => streamType(@$itemDetails['PlayState']['PlayMethod']),
  186. 'container' => isset($itemDetails['NowPlayingItem']['Container']) ? $itemDetails['NowPlayingItem']['Container'] : '',
  187. 'audioChannels' => @$itemDetails['TranscodingInfo']['AudioChannels']
  188. );
  189. // Genre catch all
  190. if($item['Genres']){
  191. $genres = array();
  192. foreach ($item['Genres'] as $genre) {
  193. $genres[] = $genre;
  194. }
  195. }
  196. // Actor catch all
  197. if($item['People'] ){
  198. $actors = array();
  199. foreach ($item['People'] as $key => $value) {
  200. if(@$value['PrimaryImageTag'] && @$value['Role']){
  201. if (file_exists($cacheDirectory.(string)$value['Id'].'-cast.jpg')){ $actorImage = $cacheDirectoryWeb.(string)$value['Id'].'-cast.jpg'; }
  202. if (file_exists($cacheDirectory.(string)$value['Id'].'-cast.jpg') && (time() - 604800) > filemtime($cacheDirectory.(string)$value['Id'].'-cast.jpg') || !file_exists($cacheDirectory.(string)$value['Id'].'-cast.jpg')) {
  203. $actorImage = 'api/?v1/image&source=emby&type=Primary&img='.(string)$value['Id'].'&height='.$actorHeight.'&width='.$actorWidth.'&key='.(string)$value['Id'].'-cast';
  204. }
  205. $actors[] = array(
  206. 'name' => (string)$value['Name'],
  207. 'role' => (string)$value['Role'],
  208. 'thumb' => $actorImage
  209. );
  210. }
  211. }
  212. }
  213. // Metadata information
  214. $embyItem['metadata'] = array(
  215. 'guid' => $item['Id'],
  216. 'summary' => @(string)$item['Overview'],
  217. 'rating' => @(string)$item['CommunityRating'],
  218. 'duration' => @(string)$item['RunTimeTicks'],
  219. 'originallyAvailableAt' => @(string)$item['PremiereDate'],
  220. 'year' => (string)$item['ProductionYear'],
  221. //'studio' => (string)$item['studio'],
  222. 'tagline' => @(string)$item['Taglines'][0],
  223. 'genres' => ($item['Genres']) ? $genres : '',
  224. 'actors' => ($item['People']) ? $actors : ''
  225. );
  226. if (file_exists($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg')){ $embyItem['nowPlayingImageURL'] = $cacheDirectoryWeb.$embyItem['nowPlayingKey'].'.jpg'; }
  227. if (file_exists($cacheDirectory.$embyItem['key'].'.jpg')){ $embyItem['imageURL'] = $cacheDirectoryWeb.$embyItem['key'].'.jpg'; }
  228. if (file_exists($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg') || !file_exists($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg')) {
  229. $embyItem['nowPlayingImageURL'] = 'api/?v1/image&source=emby&type='.$embyItem['nowPlayingImageType'].'&img='.$embyItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$embyItem['nowPlayingKey'].'';
  230. }
  231. if (file_exists($cacheDirectory.$embyItem['key'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$embyItem['key'].'.jpg') || !file_exists($cacheDirectory.$embyItem['key'].'.jpg')) {
  232. $embyItem['imageURL'] = 'api/?v1/image&source=emby&type='.$embyItem['imageType'].'&img='.$embyItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$embyItem['key'].'';
  233. }
  234. if(!$embyItem['nowPlayingThumb'] ){ $embyItem['nowPlayingOriginalImage'] = $embyItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png"; $embyItem['nowPlayingKey'] = "no-np"; }
  235. if(!$embyItem['thumb'] ){ $embyItem['originalImage'] = $embyItem['imageURL'] = "plugins/images/cache/no-list.png"; $embyItem['key'] = "no-list"; }
  236. if(isset($useImage)){ $embyItem['useImage'] = $useImage; }
  237. return $embyItem;
  238. }
  239. function resolvePlexItem($item) {
  240. // Static Height & Width
  241. $height = 300;
  242. $width = 200;
  243. $nowPlayingHeight = 675;
  244. $nowPlayingWidth = 1200;
  245. $widthOverride = 100;
  246. // Cache Directories
  247. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  248. $cacheDirectoryWeb = 'plugins/images/cache/';
  249. // Types
  250. switch ($item['type']) {
  251. case 'season':
  252. $plexItem['type'] = 'tv';
  253. $plexItem['title'] = (string)$item['parentTitle'];
  254. $plexItem['summary'] = (string)$item['parentSummary'];
  255. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  256. $plexItem['thumb'] = (string)$item['thumb'];
  257. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  258. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  259. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  260. $plexItem['metadataKey'] = (string)$item['parentRatingKey'];
  261. break;
  262. case 'episode':
  263. $plexItem['type'] = 'tv';
  264. $plexItem['title'] = (string)$item['grandparentTitle'];
  265. $plexItem['summary'] = (string)$item['title'];
  266. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  267. $plexItem['thumb'] = ($item['parentThumb'] ? (string)$item['parentThumb'] : (string)$item['grandparentThumb']);
  268. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  269. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  270. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  271. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'].' - '.(string)$item['title'];
  272. $plexItem['nowPlayingBottom'] = 'S'.(string)$item['parentIndex'].' · E'.(string)$item['index'];
  273. $plexItem['metadataKey'] = (string)$item['grandparentRatingKey'];
  274. break;
  275. case 'clip':
  276. $useImage = (isset($item['live']) ? "plugins/images/cache/livetv.png" : null);
  277. $plexItem['type'] = 'clip';
  278. $plexItem['title'] = (string)$item['title'];
  279. $plexItem['summary'] = (string)$item['summary'];
  280. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  281. $plexItem['thumb'] = (string)$item['thumb'];
  282. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  283. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  284. $plexItem['nowPlayingKey'] = isset($item['ratingKey']) ? (string)$item['ratingKey'] . "-np" : (isset($item['live']) ? "livetv.png" : ":)");
  285. $plexItem['nowPlayingTitle'] = $plexItem['title'];
  286. $plexItem['nowPlayingBottom'] = isset($item['extraType']) ? "Trailer" : (isset($item['live']) ? "Live TV" : ":)");
  287. break;
  288. case 'album':
  289. case 'track':
  290. $plexItem['type'] = 'music';
  291. $plexItem['title'] = (string)$item['parentTitle'];
  292. $plexItem['summary'] = (string)$item['title'];
  293. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  294. $plexItem['thumb'] = (string)$item['thumb'];
  295. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  296. $plexItem['nowPlayingThumb'] = ($item['parentThumb']) ? (string)$item['parentThumb'] : (string)$item['art'];
  297. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  298. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'].' - '.(string)$item['title'];
  299. $plexItem['nowPlayingBottom'] = (string)$item['parentTitle'];
  300. $plexItem['metadataKey'] = isset($item['grandparentRatingKey']) ? (string)$item['grandparentRatingKey'] : (string)$item['parentRatingKey'];
  301. break;
  302. default:
  303. $plexItem['type'] = 'movie';
  304. $plexItem['title'] = (string)$item['title'];
  305. $plexItem['summary'] = (string)$item['summary'];
  306. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  307. $plexItem['thumb'] = (string)$item['thumb'];
  308. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  309. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  310. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  311. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  312. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  313. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  314. }
  315. $plexItem['uid'] = (string)$item['ratingKey'];
  316. $plexItem['elapsed'] = isset($item['viewOffset']) && $item['viewOffset'] !== '0' ? (int)$item['viewOffset'] : null;
  317. $plexItem['duration'] = isset($item['duration']) ? (int)$item['duration'] : (int)$item->Media['duration'];
  318. $plexItem['watched'] = ($plexItem['elapsed'] && $plexItem['duration'] ? floor(($plexItem['elapsed'] / $plexItem['duration']) * 100) : 0);
  319. $plexItem['transcoded'] = isset($item->TranscodeSession['progress']) ? floor((int)$item->TranscodeSession['progress']- $plexItem['watched']) : '';
  320. $plexItem['stream'] = isset($item->Media->Part->Stream['decision']) ? (string)$item->Media->Part->Stream['decision']: '';
  321. $plexItem['id'] = str_replace('"', '', (string)$item->Player['machineIdentifier']);
  322. $plexItem['session'] = (string)$item->Session['id'];
  323. $plexItem['bandwidth'] = (string)$item->Session['bandwidth'];
  324. $plexItem['bandwidthType'] = (string)$item->Session['location'];
  325. $plexItem['sessionType'] = isset($item->TranscodeSession['progress']) ? 'Transcoding' : 'Direct Playing';
  326. $plexItem['state'] = (((string)$item->Player['state'] == "paused") ? "pause" : "play");
  327. $plexItem['user'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->User['title'] : "";
  328. $plexItem['userThumb'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->User['thumb'] : "";
  329. $plexItem['userAddress'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->Player['address'] : "x.x.x.x";
  330. $plexItem['address'] = $GLOBALS['plexTabURL'] ? $GLOBALS['plexTabURL']."/web/index.html#!/server/".$GLOBALS['plexID']."/details?key=/library/metadata/".$item['ratingKey'] : "https://app.plex.tv/web/app#!/server/".$GLOBALS['plexID']."/details?key=/library/metadata/".$item['ratingKey'];
  331. $plexItem['nowPlayingOriginalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'$'.randString();
  332. $plexItem['originalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'$'.randString();
  333. $plexItem['openTab'] = $GLOBALS['plexTabURL'] && $GLOBALS['plexTabName'] ? true : false;
  334. $plexItem['tabName'] = $GLOBALS['plexTabName'] ? $GLOBALS['plexTabName'] : '';
  335. // Stream info
  336. $plexItem['userStream'] = array(
  337. 'platform' => (string)$item->Player['platform'],
  338. 'product' => (string)$item->Player['product'],
  339. 'device' => (string)$item->Player['device'],
  340. 'stream' => (string)$item->Media->Part['decision'].($item->TranscodeSession['throttled'] == '1' ? ' (Throttled)': ''),
  341. 'videoResolution' => (string)$item->Media['videoResolution'],
  342. 'throttled' => ($item->TranscodeSession['throttled'] == 1) ? true : false,
  343. 'sourceVideoCodec' => (string)$item->TranscodeSession['sourceVideoCodec'],
  344. 'videoCodec' => (string)$item->TranscodeSession['videoCodec'],
  345. 'audioCodec' => (string)$item->TranscodeSession['audioCodec'],
  346. 'sourceAudioCodec' => (string)$item->TranscodeSession['sourceAudioCodec'],
  347. 'videoDecision' => streamType((string)$item->TranscodeSession['videoDecision']),
  348. 'audioDecision' => streamType((string)$item->TranscodeSession['audioDecision']),
  349. 'container' => (string)$item->TranscodeSession['container'],
  350. 'audioChannels' => (string)$item->TranscodeSession['audioChannels']
  351. );
  352. // Genre catch all
  353. if($item->Genre){
  354. $genres = array();
  355. foreach ($item->Genre as $key => $value) {
  356. $genres[] = (string)$value['tag'];
  357. }
  358. }
  359. // Actor catch all
  360. if($item->Role ){
  361. $actors = array();
  362. foreach ($item->Role as $key => $value) {
  363. if($value['thumb']){
  364. $actors[] = array(
  365. 'name' => (string)$value['tag'],
  366. 'role' => (string)$value['role'],
  367. 'thumb' => (string)$value['thumb']
  368. );
  369. }
  370. }
  371. }
  372. // Metadata information
  373. $plexItem['metadata'] = array(
  374. 'guid' => (string)$item['guid'],
  375. 'summary' => (string)$item['summary'],
  376. 'rating' => (string)$item['rating'],
  377. 'duration' => (string)$item['duration'],
  378. 'originallyAvailableAt' => (string)$item['originallyAvailableAt'],
  379. 'year' => (string)$item['year'],
  380. 'studio' => (string)$item['studio'],
  381. 'tagline' => (string)$item['tagline'],
  382. 'genres' => ($item->Genre) ? $genres : '',
  383. 'actors' => ($item->Role) ? $actors : ''
  384. );
  385. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')){ $plexItem['nowPlayingImageURL'] = $cacheDirectoryWeb.$plexItem['nowPlayingKey'].'.jpg'; }
  386. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg')){ $plexItem['imageURL'] = $cacheDirectoryWeb.$plexItem['key'].'.jpg'; }
  387. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') || !file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')) {
  388. $plexItem['nowPlayingImageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'';
  389. }
  390. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['key'].'.jpg') || !file_exists($cacheDirectory.$plexItem['key'].'.jpg')) {
  391. $plexItem['imageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'';
  392. }
  393. if(!$plexItem['nowPlayingThumb'] ){ $plexItem['nowPlayingOriginalImage'] = $plexItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png"; $plexItem['nowPlayingKey'] = "no-np"; }
  394. if(!$plexItem['thumb'] ){ $plexItem['originalImage'] = $plexItem['imageURL'] = "plugins/images/cache/no-list.png"; $plexItem['key'] = "no-list"; }
  395. if(isset($useImage)){ $plexItem['useImage'] = $useImage; }
  396. return $plexItem;
  397. }
  398. function plexConnect($action,$key=null){
  399. if($GLOBALS['homepagePlexEnabled'] && !empty($GLOBALS['plexURL']) && !empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'] && qualifyRequest($GLOBALS['homepagePlexAuth']))){
  400. $url = qualifyURL($GLOBALS['plexURL']);
  401. switch ($action) {
  402. case 'streams':
  403. $url = $url."/status/sessions?X-Plex-Token=".$GLOBALS['plexToken'];
  404. break;
  405. case 'recent':
  406. $url = $url."/library/recentlyAdded?X-Plex-Token=".$GLOBALS['plexToken'];
  407. break;
  408. case 'metadata':
  409. $url = $url."/library/metadata/".$key."?X-Plex-Token=".$GLOBALS['plexToken'];
  410. break;
  411. default:
  412. # code...
  413. break;
  414. }
  415. try{
  416. $options = (localURL($url)) ? array('verify' => false ) : array();
  417. $response = Requests::get($url, array(), $options);
  418. libxml_use_internal_errors(true);
  419. if($response->success){
  420. $items = array();
  421. $plex = simplexml_load_string($response->body);
  422. foreach($plex AS $child) {
  423. $items[] = resolvePlexItem($child);
  424. }
  425. $api['content'] = $items;
  426. $api['plexID'] = $GLOBALS['plexID'];
  427. $api['showNames'] = true;
  428. $api['group'] = '1';
  429. return $api;
  430. }
  431. }catch( Requests_Exception $e ) {
  432. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  433. };
  434. }
  435. return false;
  436. }
  437. function embyConnect($action,$key=null,$skip=false){
  438. if($GLOBALS['homepageEmbyEnabled'] && !empty($GLOBALS['embyURL']) && !empty($GLOBALS['embyToken']) && qualifyRequest($GLOBALS['homepageEmbyAuth'])){
  439. $url = qualifyURL($GLOBALS['embyURL']);
  440. switch ($action) {
  441. case 'streams':
  442. $url = $url.'/Sessions?api_key='.$GLOBALS['embyToken'];
  443. break;
  444. case 'recent':
  445. $username = false;
  446. if (isset($GLOBALS['organizrUser']['username'])) {
  447. $username = strtolower($GLOBALS['organizrUser']['username']);
  448. }
  449. // Get A User
  450. $userIds = $url."/Users?api_key=".$GLOBALS['embyToken'];
  451. $showPlayed = true;
  452. try{
  453. $options = (localURL($userIds)) ? array('verify' => false ) : array();
  454. $response = Requests::get($userIds, array(), $options);
  455. if($response->success){
  456. $emby = json_decode($response->body, true);
  457. foreach ($emby as $value) { // Scan for admin user
  458. if (isset($value['Policy']) && isset($value['Policy']['IsAdministrator']) && $value['Policy']['IsAdministrator']) {
  459. $userId = $value['Id'];
  460. }
  461. if ($username && strtolower($value['Name']) == $username) {
  462. $userId = $value['Id'];
  463. $showPlayed = false;
  464. break;
  465. }
  466. }
  467. }
  468. }catch( Requests_Exception $e ) {
  469. writeLog('error', 'Emby Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  470. };
  471. $url = $url.'/Users/'.$userId.'/Items/Latest?EnableImages=false&Limit=100&api_key='.$GLOBALS['embyToken'].($showPlayed?'':'&IsPlayed=false');
  472. break;
  473. case 'metadata':
  474. $skip = true;
  475. break;
  476. default:
  477. # code...
  478. break;
  479. }
  480. if($skip && $key){
  481. $items[] = resolveEmbyItem(array('Id'=>$key));
  482. $api['content'] = $items;
  483. return $api;
  484. }
  485. try{
  486. $options = (localURL($url)) ? array('verify' => false ) : array();
  487. $response = Requests::get($url, array(), $options);
  488. if($response->success){
  489. $items = array();
  490. $emby = json_decode($response->body, true);
  491. foreach($emby AS $child) {
  492. if (isset($child['NowPlayingItem']) || isset($child['Name'])) {
  493. $items[] = resolveEmbyItem($child);
  494. }
  495. }
  496. $api['content'] = array_filter($items);
  497. return $api;
  498. }
  499. }catch( Requests_Exception $e ) {
  500. writeLog('error', 'Emby Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  501. };
  502. }
  503. return false;
  504. }
  505. function sabnzbdConnect() {
  506. if($GLOBALS['homepageSabnzbdEnabled'] && !empty($GLOBALS['sabnzbdURL']) && !empty($GLOBALS['sabnzbdToken']) && qualifyRequest($GLOBALS['homepageSabnzbdAuth'])){
  507. $url = qualifyURL($GLOBALS['sabnzbdURL']);
  508. $url = $url.'/api?mode=queue&output=json&apikey='.$GLOBALS['sabnzbdToken'];
  509. try{
  510. $options = (localURL($url)) ? array('verify' => false ) : array();
  511. $response = Requests::get($url, array(), $options);
  512. if($response->success){
  513. $api['content']['queueItems'] = json_decode($response->body, true);
  514. }
  515. }catch( Requests_Exception $e ) {
  516. writeLog('error', 'SabNZBd Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  517. };
  518. $url = qualifyURL($GLOBALS['sabnzbdURL']);
  519. $url = $url.'/api?mode=history&output=json&apikey='.$GLOBALS['sabnzbdToken'];
  520. try{
  521. $options = (localURL($url)) ? array('verify' => false ) : array();
  522. $response = Requests::get($url, array(), $options);
  523. if($response->success){
  524. $api['content']['historyItems']= json_decode($response->body, true);
  525. }
  526. }catch( Requests_Exception $e ) {
  527. writeLog('error', 'SabNZBd Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  528. };
  529. $api['content'] = isset($api['content']) ? $api['content'] : false;
  530. return $api;
  531. }
  532. }
  533. function nzbgetConnect() {
  534. if($GLOBALS['homepageNzbgetEnabled'] && !empty($GLOBALS['nzbgetURL']) && qualifyRequest($GLOBALS['homepageNzbgetAuth'])){
  535. $url = qualifyURL($GLOBALS['nzbgetURL']);
  536. $url = $url.'/'.$GLOBALS['nzbgetUsername'].':'.decrypt($GLOBALS['nzbgetPassword']).'/jsonrpc/listgroups';
  537. try{
  538. $options = (localURL($url)) ? array('verify' => false ) : array();
  539. $response = Requests::get($url, array(), $options);
  540. if($response->success){
  541. $api['content']['queueItems'] = json_decode($response->body, true);
  542. }
  543. }catch( Requests_Exception $e ) {
  544. writeLog('error', 'NZBGet Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  545. };
  546. $url = qualifyURL($GLOBALS['nzbgetURL']);
  547. $url = $url.'/'.$GLOBALS['nzbgetUsername'].':'.decrypt($GLOBALS['nzbgetPassword']).'/jsonrpc/history';
  548. try{
  549. $options = (localURL($url)) ? array('verify' => false ) : array();
  550. $response = Requests::get($url, array(), $options);
  551. if($response->success){
  552. $api['content']['historyItems']= json_decode($response->body, true);
  553. }
  554. }catch( Requests_Exception $e ) {
  555. writeLog('error', 'NZBGet Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  556. };
  557. $api['content'] = isset($api['content']) ? $api['content'] : false;
  558. return $api;
  559. }
  560. }
  561. function transmissionConnect() {
  562. if($GLOBALS['homepageTransmissionEnabled'] && !empty($GLOBALS['transmissionURL']) && qualifyRequest($GLOBALS['homepageTransmissionAuth'])){
  563. $digest = qualifyURL($GLOBALS['transmissionURL'], true);
  564. $passwordInclude = ($GLOBALS['transmissionUsername'] != '' && $GLOBALS['transmissionPassword'] != '') ? $GLOBALS['transmissionUsername'].':'.decrypt($GLOBALS['transmissionPassword'])."@" : '';
  565. $url = $digest['scheme'].'://'.$passwordInclude.$digest['host'].$digest['port'].$digest['path'].'/rpc';
  566. try{
  567. $options = (localURL($GLOBALS['transmissionURL'])) ? array('verify' => false ) : array();
  568. $response = Requests::get($url, array(), $options);
  569. if($response->headers['x-transmission-session-id']){
  570. $headers = array(
  571. 'X-Transmission-Session-Id' => $response->headers['x-transmission-session-id'],
  572. 'Content-Type' => 'application/json'
  573. );
  574. $data = array(
  575. 'method' => 'torrent-get',
  576. 'arguments' => array(
  577. 'fields' => array(
  578. "id", "name", "totalSize", "eta", "isFinished", "isStalled", "percentDone", "rateDownload", "status", "downloadDir","errorString"
  579. ),
  580. ),
  581. 'tags' => ''
  582. );
  583. $response = Requests::post($url, $headers, json_encode($data), $options);
  584. if($response->success){
  585. $torrentList = json_decode($response->body, true)['arguments']['torrents'];
  586. if($GLOBALS['transmissionHideSeeding'] || $GLOBALS['transmissionHideCompleted']){
  587. $filter = array();
  588. $torrents['arguments']['torrents'] = array();
  589. if($GLOBALS['transmissionHideSeeding']){ array_push($filter, 6, 5); }
  590. if($GLOBALS['transmissionHideCompleted']){ array_push($filter, 0); }
  591. foreach ($torrentList as $key => $value) {
  592. if(!in_array($value['status'], $filter)){
  593. $torrents['arguments']['torrents'][] = $value;
  594. }
  595. }
  596. }else{
  597. $torrents = json_decode($response->body, true);
  598. }
  599. $api['content']['queueItems'] = $torrents;
  600. $api['content']['historyItems'] = false;
  601. }
  602. }else{
  603. writeLog('error', 'Transmission Connect Function - Error: Could not get session ID', 'SYSTEM');
  604. }
  605. }catch( Requests_Exception $e ) {
  606. writeLog('error', 'Transmission Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  607. };
  608. $api['content'] = isset($api['content']) ? $api['content'] : false;
  609. return $api;
  610. }
  611. }
  612. function qBittorrentConnect() {
  613. if($GLOBALS['homepageqBittorrentEnabled'] && !empty($GLOBALS['qBittorrentURL']) && qualifyRequest($GLOBALS['homepageqBittorrentAuth'])){
  614. $digest = qualifyURL($GLOBALS['qBittorrentURL'], true);
  615. $passwordInclude = ($GLOBALS['qBittorrentUsername'] != '' && $GLOBALS['qBittorrentPassword'] != '') ? 'username='.$GLOBALS['qBittorrentUsername'].'&password='.decrypt($GLOBALS['qBittorrentPassword'])."@" : '';
  616. $data = array('username'=>$GLOBALS['qBittorrentUsername'], 'password'=> decrypt($GLOBALS['qBittorrentPassword']));
  617. $url = $digest['scheme'].'://'.$digest['host'].$digest['port'].$digest['path'].'/login';
  618. try{
  619. $options = (localURL($GLOBALS['qBittorrentURL'])) ? array('verify' => false ) : array();
  620. $response = Requests::post($url, array(), $data, $options);
  621. $reflection = new ReflectionClass($response->cookies);
  622. $cookie = $reflection->getProperty("cookies");
  623. $cookie->setAccessible(true);
  624. $cookie = $cookie->getValue($response->cookies);
  625. if($cookie){
  626. $headers = array(
  627. 'Cookie' => 'SID=' . $cookie['SID']->value
  628. );
  629. $url = $digest['scheme'].'://'.$digest['host'].$digest['port'].$digest['path'].'/query/torrents?sort=eta';
  630. $response = Requests::get($url, $headers, $options);
  631. if($response){
  632. $torrentList = json_decode($response->body, true);
  633. if($GLOBALS['qBittorrentHideSeeding'] || $GLOBALS['qBittorrentHideCompleted']){
  634. $filter = array();
  635. $torrents['arguments']['torrents'] = array();
  636. if($GLOBALS['qBittorrentHideSeeding']){ array_push($filter, 'uploading', 'stalledUP', 'queuedUP'); }
  637. if($GLOBALS['qBittorrentHideCompleted']){ array_push($filter, 'pausedUP'); }
  638. foreach ($torrentList as $key => $value) {
  639. if(!in_array($value['state'], $filter)){
  640. $torrents['arguments']['torrents'][] = $value;
  641. }
  642. }
  643. }else{
  644. $torrents['arguments']['torrents'] = json_decode($response->body, true);
  645. }
  646. $api['content']['queueItems'] = $torrents;
  647. $api['content']['historyItems'] = false;
  648. }
  649. }else{
  650. writeLog('error', 'qBittorrent Connect Function - Error: Could not get session ID', 'SYSTEM');
  651. }
  652. }catch( Requests_Exception $e ) {
  653. writeLog('error', 'qBittorrent Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  654. };
  655. $api['content'] = isset($api['content']) ? $api['content'] : false;
  656. return $api;
  657. }
  658. }
  659. function accessProtected($obj, $prop) {
  660. $reflection = new ReflectionClass($obj);
  661. $property = $reflection->getProperty($prop);
  662. $property->setAccessible(true);
  663. return $property->getValue($obj);
  664. }