functions.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. <?php
  2. // Debugging output functions
  3. function debug_out($variable, $die = false) {
  4. $trace = debug_backtrace()[0];
  5. echo '<pre style="background-color: #f2f2f2; border: 2px solid black; border-radius: 5px; padding: 5px; margin: 5px;">'.$trace['file'].':'.$trace['line']."\n\n".print_r($variable, true).'</pre>';
  6. if ($die) { http_response_code(503); die(); }
  7. }
  8. // Auth Plugins
  9. // Pass credentials to LDAP backend
  10. function plugin_auth_ldap($username, $password) {
  11. // returns true or false
  12. $ldap = ldap_connect(AUTHBACKENDHOST.(AUTHBACKENDPORT?':'.AUTHBACKENDPORT:'389'));
  13. if ($bind = ldap_bind($ldap, AUTHBACKENDDOMAIN.'\\'.$username, $password)) {
  14. return true;
  15. } else {
  16. return false;
  17. }
  18. return false;
  19. }
  20. // Pass credentials to FTP backend
  21. function plugin_auth_ftp($username, $password) {
  22. // returns true or false
  23. // Connect to FTP
  24. $conn_id = ftp_ssl_connect(AUTHBACKENDHOST, (AUTHBACKENDPORT?AUTHBACKENDPORT:21), 20); // 20 Second Timeout
  25. // Check if valid FTP connection
  26. if ($conn_id) {
  27. // Attempt login
  28. @$login_result = ftp_login($conn_id, $username, $password);
  29. // Return Result
  30. if ($login_result) {
  31. return true;
  32. } else {
  33. return false;
  34. }
  35. } else {
  36. return false;
  37. }
  38. return false;
  39. }
  40. // Pass credentials to Emby Backend
  41. function plugin_auth_emby($username, $password) {
  42. $urlCheck = stripos(AUTHBACKENDHOST, "http");
  43. if ($urlCheck === false) {
  44. $embyAddress = "http://" . AUTHBACKENDHOST;
  45. } else {
  46. $embyAddress = AUTHBACKENDHOST;
  47. }
  48. if(AUTHBACKENDPORT !== ""){ $embyAddress .= ":" . AUTHBACKENDPORT; }
  49. $headers = array(
  50. 'Authorization'=> 'MediaBrowser UserId="e8837bc1-ad67-520e-8cd2-f629e3155721", Client="None", Device="Organizr", DeviceId="xxx", Version="1.0.0.0"',
  51. 'Content-type' => 'application/json',
  52. );
  53. $body = array(
  54. 'Username' => $username,
  55. 'Password' => sha1($password),
  56. 'PasswordMd5' => md5($password),
  57. );
  58. $response = post_router($embyAddress.'/Users/AuthenticateByName', $body, $headers);
  59. if (isset($response['content'])) {
  60. $json = json_decode($response['content'], true);
  61. if (is_array($json) && isset($json['SessionInfo']) && isset($json['User']) && $json['User']['HasPassword'] == true) {
  62. // Login Success - Now Logout Emby Session As We No Longer Need It
  63. $headers = array(
  64. 'X-Mediabrowser-Token' => $json['AccessToken'],
  65. );
  66. $response = post_router($embyAddress.'/Sessions/Logout', array(), $headers);
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. function clean($strin) {
  73. $strout = null;
  74. for ($i = 0; $i < strlen($strin); $i++) {
  75. $ord = ord($strin[$i]);
  76. if (($ord > 0 && $ord < 32) || ($ord >= 127)) {
  77. $strout .= "&amp;#{$ord};";
  78. }
  79. else {
  80. switch ($strin[$i]) {
  81. case '<':
  82. $strout .= '&lt;';
  83. break;
  84. case '>':
  85. $strout .= '&gt;';
  86. break;
  87. case '&':
  88. $strout .= '&amp;';
  89. break;
  90. case '"':
  91. $strout .= '&quot;';
  92. break;
  93. default:
  94. $strout .= $strin[$i];
  95. }
  96. }
  97. }
  98. return $strout;
  99. }
  100. function registration_callback($username, $email, $userdir){
  101. global $data;
  102. $data = array($username, $email, $userdir);
  103. }
  104. function printArray($arrayName){
  105. $messageCount = count($arrayName);
  106. $i = 0;
  107. foreach ( $arrayName as $item ) :
  108. $i++;
  109. if($i < $messageCount) :
  110. echo "<small class='text-uppercase'>" . $item . "</small> & ";
  111. elseif($i = $messageCount) :
  112. echo "<small class='text-uppercase'>" . $item . "</small>";
  113. endif;
  114. endforeach;
  115. }
  116. function write_ini_file($content, $path) {
  117. if (!$handle = fopen($path, 'w')) {
  118. return false;
  119. }
  120. $success = fwrite($handle, trim($content));
  121. fclose($handle);
  122. return $success;
  123. }
  124. function gotTimezone(){
  125. $regions = array(
  126. 'Africa' => DateTimeZone::AFRICA,
  127. 'America' => DateTimeZone::AMERICA,
  128. 'Antarctica' => DateTimeZone::ANTARCTICA,
  129. 'Arctic' => DateTimeZone::ARCTIC,
  130. 'Asia' => DateTimeZone::ASIA,
  131. 'Atlantic' => DateTimeZone::ATLANTIC,
  132. 'Australia' => DateTimeZone::AUSTRALIA,
  133. 'Europe' => DateTimeZone::EUROPE,
  134. 'Indian' => DateTimeZone::INDIAN,
  135. 'Pacific' => DateTimeZone::PACIFIC
  136. );
  137. $timezones = array();
  138. foreach ($regions as $name => $mask) {
  139. $zones = DateTimeZone::listIdentifiers($mask);
  140. foreach($zones as $timezone) {
  141. $time = new DateTime(NULL, new DateTimeZone($timezone));
  142. $ampm = $time->format('H') > 12 ? ' ('. $time->format('g:i a'). ')' : '';
  143. $timezones[$name][$timezone] = substr($timezone, strlen($name) + 1) . ' - ' . $time->format('H:i') . $ampm;
  144. }
  145. }
  146. print '<select name="timezone" id="timezone" class="form-control material input-sm" required>';
  147. foreach($timezones as $region => $list) {
  148. print '<optgroup label="' . $region . '">' . "\n";
  149. foreach($list as $timezone => $name) {
  150. if($timezone == TIMEZONE) : $selected = " selected"; else : $selected = ""; endif;
  151. print '<option value="' . $timezone . '"' . $selected . '>' . $name . '</option>' . "\n";
  152. }
  153. print '</optgroup>' . "\n";
  154. }
  155. print '</select>';
  156. }
  157. function getTimezone(){
  158. $regions = array(
  159. 'Africa' => DateTimeZone::AFRICA,
  160. 'America' => DateTimeZone::AMERICA,
  161. 'Antarctica' => DateTimeZone::ANTARCTICA,
  162. 'Arctic' => DateTimeZone::ARCTIC,
  163. 'Asia' => DateTimeZone::ASIA,
  164. 'Atlantic' => DateTimeZone::ATLANTIC,
  165. 'Australia' => DateTimeZone::AUSTRALIA,
  166. 'Europe' => DateTimeZone::EUROPE,
  167. 'Indian' => DateTimeZone::INDIAN,
  168. 'Pacific' => DateTimeZone::PACIFIC
  169. );
  170. $timezones = array();
  171. foreach ($regions as $name => $mask) {
  172. $zones = DateTimeZone::listIdentifiers($mask);
  173. foreach($zones as $timezone) {
  174. $time = new DateTime(NULL, new DateTimeZone($timezone));
  175. $ampm = $time->format('H') > 12 ? ' ('. $time->format('g:i a'). ')' : '';
  176. $timezones[$name][$timezone] = substr($timezone, strlen($name) + 1) . ' - ' . $time->format('H:i') . $ampm;
  177. }
  178. }
  179. print '<select name="timezone" id="timezone" class="form-control material" required>';
  180. foreach($timezones as $region => $list) {
  181. print '<optgroup label="' . $region . '">' . "\n";
  182. foreach($list as $timezone => $name) {
  183. print '<option value="' . $timezone . '">' . $name . '</option>' . "\n";
  184. }
  185. print '</optgroup>' . "\n";
  186. }
  187. print '</select>';
  188. }
  189. function explosion($string, $position){
  190. $getWord = explode("|", $string);
  191. return $getWord[$position];
  192. }
  193. function getServerPath() {
  194. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  195. $protocol = "https://";
  196. } else {
  197. $protocol = "http://";
  198. }
  199. return $protocol . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']);
  200. }
  201. function get_browser_name() {
  202. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  203. if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) return 'Opera';
  204. elseif (strpos($user_agent, 'Edge')) return 'Edge';
  205. elseif (strpos($user_agent, 'Chrome')) return 'Chrome';
  206. elseif (strpos($user_agent, 'Safari')) return 'Safari';
  207. elseif (strpos($user_agent, 'Firefox')) return 'Firefox';
  208. elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) return 'Internet Explorer';
  209. return 'Other';
  210. }
  211. function resolveEmbyItem($address, $token, $item) {
  212. // Static Height
  213. $height = 150;
  214. // Get Item Details
  215. $itemDetails = json_decode(file_get_contents($address.'/Items?Ids='.$item['Id'].'&Fields=Overview&api_key='.$token),true)['Items'][0];
  216. switch ($item['Type']) {
  217. case 'Episode':
  218. $title = $item['SeriesName'].': '.$item['Name'].' (Season '.$item['ParentIndexNumber'].': Episode '.$item['IndexNumber'].')';
  219. $imageId = $itemDetails['SeriesId'];
  220. $width = 100;
  221. $image = 'season';
  222. break;
  223. case 'Music':
  224. $title = $item['Name'];
  225. $imageId = $itemDetails['AlbumId'];
  226. $width = 150;
  227. $image = 'music';
  228. break;
  229. default:
  230. $title = $item['Name'];
  231. $imageId = $item['Id'];
  232. $width = 100;
  233. $image = 'movie';
  234. }
  235. // If No Overview
  236. if (!isset($itemDetails['Overview'])) {
  237. $itemDetails['Overview'] = '';
  238. }
  239. // Assemble Item And Cache Into Array
  240. return '<div class="item"><a href="'.$address.'/web/itemdetails.html?id='.$item['Id'].'" target="_blank"><img alt="'.$item['Name'].'" class="carousel-image '.$image.'" src="image.php?source=emby&img='.$imageId.'&height='.$height.'&width='.$width.'"></a><div class="carousel-caption '.$image.'""><h4>'.$title.'</h4><small><em>'.$itemDetails['Overview'].'</em></small></div></div>';
  241. }
  242. function outputCarousel($header, $size, $type, $items) {
  243. // If None Populate Empty Item
  244. if (!count($items)) {
  245. $items = array('<div class="item"><img alt="nada" class="carousel-image movie" src="images/nadaplaying.jpg"><div class="carousel-caption"><h4>Nothing To Show</h4><small><em>Get Some Stuff Going!</em></small></div></div>');
  246. }
  247. // Set First As Active
  248. $items[0] = preg_replace('/^<div class="item ?">/','<div class="item active">', $items[0]);
  249. // Add Buttons
  250. $buttons = '';
  251. if (count($items) > 1) {
  252. $buttons = '
  253. <a class="left carousel-control '.$type.'" href="#carousel-'.$type.'-emby" role="button" data-slide="prev"><span class="fa fa-chevron-left" aria-hidden="true"></span><span class="sr-only">Previous</span></a>
  254. <a class="right carousel-control '.$type.'" href="#carousel-'.$type.'-emby" role="button" data-slide="next"><span class="fa fa-chevron-right" aria-hidden="true"></span><span class="sr-only">Next</span></a>';
  255. }
  256. return '
  257. <div class="col-lg-'.$size.'">
  258. <h5 class="text-center">'.$header.'</h5>
  259. <div id="carousel-'.$type.'-emby" class="carousel slide box-shadow white-bg" data-ride="carousel"><div class="carousel-inner" role="listbox">
  260. '.implode('',$items).'
  261. </div>'.$buttons.'
  262. </div></div>';
  263. }
  264. function getEmbyStreams($url, $port, $token, $size, $header) {
  265. if (stripos($url, "http") === false) {
  266. $url = "http://" . $url;
  267. }
  268. if ($port !== "") {
  269. $url = $url . ":" . $port;
  270. }
  271. $address = $url;
  272. $api = json_decode(file_get_contents($address.'/Sessions?api_key='.$token),true);
  273. $playingItems = array();
  274. foreach($api as $key => $value) {
  275. if (isset($value['NowPlayingItem'])) {
  276. $playingItems[] = resolveEmbyItem($address, $token, $value['NowPlayingItem']);
  277. }
  278. }
  279. return outputCarousel($header, $size, 'streams', $playingItems);
  280. }
  281. function getEmbyRecent($url, $port, $type, $token, $size, $header) {
  282. if (stripos($url, "http") === false) {
  283. $url = "http://" . $url;
  284. }
  285. if ($port !== "") {
  286. $url = $url . ":" . $port;
  287. }
  288. $address = $url;
  289. // Resolve Types
  290. switch ($type) {
  291. case 'movie':
  292. $embyTypeQuery = 'IncludeItemTypes=Movie&';
  293. break;
  294. case 'season':
  295. $embyTypeQuery = 'IncludeItemTypes=Episode&';
  296. break;
  297. case 'album':
  298. $embyTypeQuery = 'IncludeItemTypes=Music&';
  299. break;
  300. default:
  301. $embyTypeQuery = '';
  302. }
  303. // Get A User
  304. $userId = json_decode(file_get_contents($address.'/Users?api_key='.$token),true)[0]['Id'];
  305. // Get the latest Items
  306. $latest = json_decode(file_get_contents($address.'/Users/'.$userId.'/Items/Latest?'.$embyTypeQuery.'EnableImages=false&api_key='.$token),true);
  307. // For Each Item In Category
  308. $items = array();
  309. foreach ($latest as $k => $v) {
  310. $items[] = resolveEmbyItem($address, $token, $v);
  311. }
  312. return outputCarousel($header, $size, $type, $items);
  313. }
  314. function getPlexRecent($url, $port, $type, $token, $size, $header){
  315. $urlCheck = stripos($url, "http");
  316. if ($urlCheck === false) {
  317. $url = "http://" . $url;
  318. }
  319. if($port !== ""){ $url = $url . ":" . $port; }
  320. $address = $url;
  321. $api = file_get_contents($address."/library/recentlyAdded?X-Plex-Token=".$token);
  322. $api = simplexml_load_string($api);
  323. $getServer = file_get_contents($address."/?X-Plex-Token=".$token);
  324. $getServer = simplexml_load_string($getServer);
  325. $gotServer = $getServer['machineIdentifier'];
  326. $i = 0;
  327. $gotPlex = '<div class="col-lg-'.$size.'"><h5 class="text-center">'.$header.'</h5><div id="carousel-'.$type.'" class="carousel slide box-shadow white-bg" data-ride="carousel"><div class="carousel-inner" role="listbox">';
  328. foreach($api AS $child) {
  329. if($child['type'] == $type){
  330. $i++;
  331. if($i == 1){ $active = "active"; }else{ $active = "";}
  332. $thumb = $child['thumb'];
  333. $plexLink = "https://app.plex.tv/web/app#!/server/$gotServer/details?key=/library/metadata/".$child['ratingKey'];
  334. if($type == "movie"){
  335. $title = $child['title'];
  336. $summary = $child['summary'];
  337. $height = "150";
  338. $width = "100";
  339. }elseif($type == "season"){
  340. $title = $child['parentTitle'];
  341. $summary = $child['parentSummary'];
  342. $height = "150";
  343. $width = "100";
  344. }elseif($type == "album"){
  345. $title = $child['parentTitle'];
  346. $summary = $child['title'];
  347. $height = "150";
  348. $width = "150";
  349. }
  350. $gotPlex .= '<div class="item '.$active.'"> <a href="'.$plexLink.'" target="_blank"> <img alt="'.$title.'" class="carousel-image '.$type.'" src="image.php?img='.$thumb.'&height='.$height.'&width='.$width.'"> </a> <div class="carousel-caption '.$type.'"> <h4>'.$title.'</h4> <small> <em>'.$summary.'</em> </small> </div> </div>';
  351. $plexLink = "";
  352. }
  353. }
  354. $gotPlex .= '</div>';
  355. if ($i > 1){
  356. $gotPlex .= '<a class="left carousel-control '.$type.'" href="#carousel-'.$type.'" role="button" data-slide="prev"><span class="fa fa-chevron-left" aria-hidden="true"></span><span class="sr-only">Previous</span></a><a class="right carousel-control '.$type.'" href="#carousel-'.$type.'" role="button" data-slide="next"><span class="fa fa-chevron-right" aria-hidden="true"></span><span class="sr-only">Next</span></a>';
  357. }
  358. $gotPlex .= '</div></div>';
  359. $noPlex = '<div class="col-lg-'.$size.'"><h5 class="text-center">'.$header.'</h5>';
  360. $noPlex .= '<div id="carousel-'.$type.'" class="carousel slide box-shadow white-bg" data-ride="carousel">';
  361. $noPlex .= '<div class="carousel-inner" role="listbox">';
  362. $noPlex .= '<div class="item active">';
  363. $noPlex .= "<img alt='nada' class='carousel-image movie' src='images/nadaplaying.jpg'>";
  364. $noPlex .= '<div class="carousel-caption"> <h4>Nothing New</h4> <small> <em>Get to Adding!</em> </small></div></div></div></div></div>';
  365. if ($i != 0){ return $gotPlex; }
  366. if ($i == 0){ return $noPlex; }
  367. }
  368. function getPlexStreams($url, $port, $token, $size, $header){
  369. $urlCheck = stripos($url, "http");
  370. if ($urlCheck === false) {
  371. $url = "http://" . $url;
  372. }
  373. if($port !== ""){ $url = $url . ":" . $port; }
  374. $address = $url;
  375. $api = file_get_contents($address."/status/sessions?X-Plex-Token=".$token);
  376. $api = simplexml_load_string($api);
  377. $getServer = file_get_contents($address."/servers?X-Plex-Token=".$token);
  378. $getServer = simplexml_load_string($getServer);
  379. foreach($getServer AS $child) {
  380. $gotServer = $child['machineIdentifier'];
  381. }
  382. $i = 0;
  383. $gotPlex = '<div class="col-lg-'.$size.'"><h5 class="text-center">'.$header.'</h5>';
  384. $gotPlex .= '<div id="carousel-streams" class="carousel slide box-shadow white-bg" data-ride="carousel">';
  385. $gotPlex .= '<div class="carousel-inner" role="listbox">';
  386. foreach($api AS $child) {
  387. $type = $child['type'];
  388. $plexLink = "https://app.plex.tv/web/app#!/server/$gotServer/details?key=/library/metadata/".$child['ratingKey'];
  389. $i++;
  390. if($i == 1){ $active = "active"; }else{ $active = "";}
  391. if($type == "movie"){
  392. $title = $child['title'];
  393. $summary = htmlentities($child['summary'], ENT_QUOTES);
  394. $thumb = $child['thumb'];
  395. $image = "movie";
  396. $height = "150";
  397. $width = "100";
  398. }elseif($type == "episode"){
  399. $title = $child['grandparentTitle'];
  400. $summary = htmlentities($child['summary'], ENT_QUOTES);
  401. $thumb = $child['grandparentThumb'];
  402. $image = "season";
  403. $height = "150";
  404. $width = "100";
  405. }elseif($type == "track"){
  406. $title = $child['grandparentTitle'] . " - " . $child['parentTitle'];
  407. $summary = htmlentities($child['title'], ENT_QUOTES);
  408. $thumb = $child['thumb'];
  409. $image = "album";
  410. $height = "150";
  411. $width = "150";
  412. }elseif($type == "clip"){
  413. $title = $child['title'].' - Trailer';
  414. $summary = ($child['summary'] != "" ? $child['summary'] : "<i>No summary loaded.</i>");
  415. $thumb = ($child['thumb'] != "" ? $child['thumb'] : 'images/nadaplaying.jpg');
  416. $image = "movie";
  417. $height = "150";
  418. $width = "100";
  419. }
  420. $gotPlex .= '<div class="item '.$active.'">';
  421. $gotPlex .= "<a href='$plexLink' target='_blank'><img alt='$title' class='carousel-image $image' src='image.php?img=$thumb&height=$height&width=$width'></a>";
  422. $gotPlex .= '<div class="carousel-caption '. $image . '""><h4>'.$title.'</h4><small><em>'.$summary.'</em></small></div></div>';
  423. $plexLink = "";
  424. }
  425. $gotPlex .= '</div>';
  426. if ($i > 1){
  427. $gotPlex .= '<a class="left carousel-control streams" href="#carousel-streams" role="button" data-slide="prev"><span class="fa fa-chevron-left" aria-hidden="true"></span><span class="sr-only">Previous</span></a><a class="right carousel-control streams" href="#carousel-streams" role="button" data-slide="next"><span class="fa fa-chevron-right" aria-hidden="true"></span><span class="sr-only">Next</span></a>';
  428. }
  429. $gotPlex .= '</div></div>';
  430. $noPlex = '<div class="col-lg-'.$size.'"><h5 class="text-center">'.$header.'</h5>';
  431. $noPlex .= '<div id="carousel-streams" class="carousel slide box-shadow white-bg" data-ride="carousel">';
  432. $noPlex .= '<div class="carousel-inner" role="listbox">';
  433. $noPlex .= '<div class="item active">';
  434. $noPlex .= "<img alt='nada' class='carousel-image movie' src='images/nadaplaying.jpg'>";
  435. $noPlex .= '<div class="carousel-caption"><h4>Nothing Playing</h4><small><em>Get to Streaming!</em></small></div></div></div></div></div>';
  436. if ($i != 0){ return $gotPlex; }
  437. if ($i == 0){ return $noPlex; }
  438. }
  439. function getSickrageCalendarWanted($array){
  440. $array = json_decode($array, true);
  441. $gotCalendar = "";
  442. $i = 0;
  443. foreach($array['data']['missed'] AS $child) {
  444. $i++;
  445. $seriesName = $child['show_name'];
  446. $episodeID = $child['tvdbid'];
  447. $episodeAirDate = $child['airdate'];
  448. $episodeAirDateTime = explode(" ",$child['airs']);
  449. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1].$episodeAirDateTime[2]));
  450. $episodeAirDate = strtotime($episodeAirDate.$episodeAirDateTime);
  451. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  452. if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; }
  453. $downloaded = "0";
  454. if($downloaded == "0" && isset($unaired)){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg";}else{ $downloaded = "red-bg"; }
  455. $gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded\", imagetype: \"tv\", url: \"https://thetvdb.com/?tab=series&id=$episodeID\" }, \n";
  456. }
  457. foreach($array['data']['today'] AS $child) {
  458. $i++;
  459. $seriesName = $child['show_name'];
  460. $episodeID = $child['tvdbid'];
  461. $episodeAirDate = $child['airdate'];
  462. $episodeAirDateTime = explode(" ",$child['airs']);
  463. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1].$episodeAirDateTime[2]));
  464. $episodeAirDate = strtotime($episodeAirDate.$episodeAirDateTime);
  465. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  466. if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; }
  467. $downloaded = "0";
  468. if($downloaded == "0" && isset($unaired)){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg";}else{ $downloaded = "red-bg"; }
  469. $gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded\", imagetype: \"tv\", url: \"https://thetvdb.com/?tab=series&id=$episodeID\" }, \n";
  470. }
  471. foreach($array['data']['soon'] AS $child) {
  472. $i++;
  473. $seriesName = $child['show_name'];
  474. $episodeID = $child['tvdbid'];
  475. $episodeAirDate = $child['airdate'];
  476. $episodeAirDateTime = explode(" ",$child['airs']);
  477. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1].$episodeAirDateTime[2]));
  478. $episodeAirDate = strtotime($episodeAirDate.$episodeAirDateTime);
  479. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  480. if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; }
  481. $downloaded = "0";
  482. if($downloaded == "0" && isset($unaired)){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg";}else{ $downloaded = "red-bg"; }
  483. $gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded\", imagetype: \"tv\", url: \"https://thetvdb.com/?tab=series&id=$episodeID\" }, \n";
  484. }
  485. foreach($array['data']['later'] AS $child) {
  486. $i++;
  487. $seriesName = $child['show_name'];
  488. $episodeID = $child['tvdbid'];
  489. $episodeAirDate = $child['airdate'];
  490. $episodeAirDateTime = explode(" ",$child['airs']);
  491. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1].$episodeAirDateTime[2]));
  492. $episodeAirDate = strtotime($episodeAirDate.$episodeAirDateTime);
  493. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  494. if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; }
  495. $downloaded = "0";
  496. if($downloaded == "0" && isset($unaired)){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg";}else{ $downloaded = "red-bg"; }
  497. $gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded\", imagetype: \"tv\", url: \"https://thetvdb.com/?tab=series&id=$episodeID\" }, \n";
  498. }
  499. if ($i != 0){ return $gotCalendar; }
  500. }
  501. function getSickrageCalendarHistory($array){
  502. $array = json_decode($array, true);
  503. $gotCalendar = "";
  504. $i = 0;
  505. foreach($array['data'] AS $child) {
  506. $i++;
  507. $seriesName = $child['show_name'];
  508. $episodeID = $child['tvdbid'];
  509. $episodeAirDate = $child['date'];
  510. $downloaded = "green-bg";
  511. $gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded\", imagetype: \"tv\", url: \"https://thetvdb.com/?tab=series&id=$episodeID\" }, \n";
  512. }
  513. if ($i != 0){ return $gotCalendar; }
  514. }
  515. function getSonarrCalendar($array){
  516. $array = json_decode($array, true);
  517. $gotCalendar = "";
  518. $i = 0;
  519. foreach($array AS $child) {
  520. $i++;
  521. $seriesName = $child['series']['title'];
  522. $episodeID = $child['series']['tvdbId'];
  523. $episodeName = htmlentities($child['title'], ENT_QUOTES);
  524. if($child['episodeNumber'] == "1"){ $episodePremier = "true"; }else{ $episodePremier = "false"; }
  525. $episodeAirDate = $child['airDateUtc'];
  526. $episodeAirDate = strtotime($episodeAirDate);
  527. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  528. if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; }
  529. $downloaded = $child['hasFile'];
  530. if($downloaded == "0" && isset($unaired) && $episodePremier == "true"){ $downloaded = "light-blue-bg"; }elseif($downloaded == "0" && isset($unaired)){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg";}else{ $downloaded = "red-bg"; }
  531. $gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded\", imagetype: \"tv\", url: \"https://thetvdb.com/?tab=series&id=$episodeID\" }, \n";
  532. }
  533. if ($i != 0){ return $gotCalendar; }
  534. }
  535. function getRadarrCalendar($array){
  536. $array = json_decode($array, true);
  537. $gotCalendar = "";
  538. $i = 0;
  539. foreach($array AS $child) {
  540. if(isset($child['inCinemas'])){
  541. $i++;
  542. $movieName = $child['title'];
  543. $movieID = $child['imdbId'];
  544. if(!isset($movieID)){ $movieID = ""; }
  545. if(isset($child['inCinemas']) && isset($child['physicalRelease'])){
  546. $physicalRelease = $child['physicalRelease'];
  547. $physicalRelease = strtotime($physicalRelease);
  548. $physicalRelease = date("Y-m-d", $physicalRelease);
  549. if (new DateTime() < new DateTime($physicalRelease)) { $notReleased = "true"; }else{ $notReleased = "false"; }
  550. $downloaded = $child['hasFile'];
  551. if($downloaded == "0" && $notReleased == "true"){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg"; }else{ $downloaded = "red-bg"; }
  552. }else{
  553. $physicalRelease = $child['inCinemas'];
  554. $downloaded = "light-blue-bg";
  555. }
  556. $gotCalendar .= "{ title: \"$movieName\", start: \"$physicalRelease\", className: \"$downloaded\", imagetype: \"film\", url: \"http://www.imdb.com/title/$movieID\" }, \n";
  557. }
  558. }
  559. if ($i != 0){ return $gotCalendar; }
  560. }
  561. function nzbgetConnect($url, $port, $username, $password, $list){
  562. $urlCheck = stripos($url, "http");
  563. if ($urlCheck === false) {
  564. $url = "http://" . $url;
  565. }
  566. if($port !== ""){ $url = $url . ":" . $port; }
  567. $address = $url;
  568. $api = file_get_contents("$url/$username:$password/jsonrpc/$list");
  569. $api = json_decode($api, true);
  570. $i = 0;
  571. $gotNZB = "";
  572. foreach ($api['result'] AS $child) {
  573. $i++;
  574. //echo '<pre>' . var_export($child, true) . '</pre>';
  575. $downloadName = htmlentities($child['NZBName'], ENT_QUOTES);
  576. $downloadStatus = $child['Status'];
  577. $downloadCategory = $child['Category'];
  578. if($list == "history"){ $downloadPercent = "100"; $progressBar = ""; }
  579. if($list == "listgroups"){ $downloadPercent = (($child['FileSizeMB'] - $child['RemainingSizeMB']) / $child['FileSizeMB']) * 100; $progressBar = "progress-bar-striped active"; }
  580. if($child['Health'] <= "750"){
  581. $downloadHealth = "danger";
  582. }elseif($child['Health'] <= "900"){
  583. $downloadHealth = "warning";
  584. }elseif($child['Health'] <= "1000"){
  585. $downloadHealth = "success";
  586. }
  587. $gotNZB .= '<tr>
  588. <td>'.$downloadName.'</td>
  589. <td>'.$downloadStatus.'</td>
  590. <td>'.$downloadCategory.'</td>
  591. <td>
  592. <div class="progress">
  593. <div class="progress-bar progress-bar-'.$downloadHealth.' '.$progressBar.'" role="progressbar" aria-valuenow="'.$downloadPercent.'" aria-valuemin="0" aria-valuemax="100" style="width: '.$downloadPercent.'%">
  594. <p class="text-center">'.round($downloadPercent).'%</p>
  595. <span class="sr-only">'.$downloadPercent.'% Complete</span>
  596. </div>
  597. </div>
  598. </td>
  599. </tr>';
  600. }
  601. if($i > 0){ return $gotNZB; }
  602. if($i == 0){ echo '<tr><td colspan="4"><p class="text-center">No Results</p></td></tr>'; }
  603. }
  604. function sabnzbdConnect($url, $port, $key, $list){
  605. $urlCheck = stripos($url, "http");
  606. if ($urlCheck === false) {
  607. $url = "http://" . $url;
  608. }
  609. if($port !== ""){ $url = $url . ":" . $port; }
  610. $address = $url;
  611. $api = file_get_contents("$url/api?mode=$list&output=json&apikey=$key");
  612. $api = json_decode($api, true);
  613. $i = 0;
  614. $gotNZB = "";
  615. foreach ($api[$list]['slots'] AS $child) {
  616. $i++;
  617. if($list == "queue"){ $downloadName = $child['filename']; $downloadCategory = $child['cat']; $downloadPercent = (($child['mb'] - $child['mbleft']) / $child['mb']) * 100; $progressBar = "progress-bar-striped active"; }
  618. if($list == "history"){ $downloadName = $child['name']; $downloadCategory = $child['category']; $downloadPercent = "100"; $progressBar = ""; }
  619. $downloadStatus = $child['status'];
  620. $gotNZB .= '<tr>
  621. <td>'.$downloadName.'</td>
  622. <td>'.$downloadStatus.'</td>
  623. <td>'.$downloadCategory.'</td>
  624. <td>
  625. <div class="progress">
  626. <div class="progress-bar progress-bar-success '.$progressBar.'" role="progressbar" aria-valuenow="'.$downloadPercent.'" aria-valuemin="0" aria-valuemax="100" style="width: '.$downloadPercent.'%">
  627. <p class="text-center">'.round($downloadPercent).'%</p>
  628. <span class="sr-only">'.$downloadPercent.'% Complete</span>
  629. </div>
  630. </div>
  631. </td>
  632. </tr>';
  633. }
  634. if($i > 0){ return $gotNZB; }
  635. if($i == 0){ echo '<tr><td colspan="4"><p class="text-center">No Results</p></td></tr>'; }
  636. }
  637. function getHeadphonesCalendar($url, $port, $key, $list){
  638. $urlCheck = stripos($url, "http");
  639. if ($urlCheck === false) {
  640. $url = "http://" . $url;
  641. }
  642. if($port !== ""){ $url = $url . ":" . $port; }
  643. $address = $url;
  644. $api = file_get_contents($address."/api?apikey=".$key."&cmd=$list");
  645. $api = json_decode($api, true);
  646. $i = 0;
  647. $gotCalendar = "";
  648. foreach($api AS $child) {
  649. if($child['Status'] == "Wanted"){
  650. $i++;
  651. $albumName = addslashes($child['AlbumTitle']);
  652. $albumArtist = htmlentities($child['ArtistName'], ENT_QUOTES);
  653. $albumDate = $child['ReleaseDate'];
  654. $albumID = $child['AlbumID'];
  655. $albumDate = strtotime($albumDate);
  656. $albumDate = date("Y-m-d", $albumDate);
  657. $albumStatus = $child['Status'];
  658. if (new DateTime() < new DateTime($albumDate)) { $notReleased = "true"; }else{ $notReleased = "false"; }
  659. if($albumStatus == "Wanted" && $notReleased == "true"){ $albumStatusColor = "indigo-bg"; }elseif($albumStatus == "Downloaded"){ $albumStatusColor = "green-bg"; }else{ $albumStatusColor = "red-bg"; }
  660. $gotCalendar .= "{ title: \"$albumArtist - $albumName\", start: \"$albumDate\", className: \"$albumStatusColor\", imagetype: \"music\", url: \"https://musicbrainz.org/release-group/$albumID\" }, \n";
  661. }
  662. }
  663. if ($i != 0){ return $gotCalendar; }
  664. }
  665. function post_router($url, $data, $headers = array(), $referer='') {
  666. if (function_exists('curl_version')) {
  667. return curl_post($url, $data, $headers, $referer);
  668. } else {
  669. return post_request($url, $data, $headers, $referer);
  670. }
  671. }
  672. function curl_post($url, $data, $headers = array(), $referer='') {
  673. // Initiate cURL
  674. $curlReq = curl_init($url);
  675. // As post request
  676. curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, "POST");
  677. curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true);
  678. // Format Headers
  679. $cHeaders = array();
  680. foreach ($headers as $k => $v) {
  681. $cHeaders[] = $k.': '.$v;
  682. }
  683. if (count($cHeaders)) {
  684. curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders);
  685. }
  686. // Format Data
  687. curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data));
  688. // Execute
  689. $result = curl_exec($curlReq);
  690. // Close
  691. curl_close($curlReq);
  692. // Return
  693. return array('content'=>$result);
  694. }
  695. function post_request($url, $data, $headers = array(), $referer='') {
  696. // Adapted from http://stackoverflow.com/a/28387011/6810513
  697. // Convert the data array into URL Parameters like a=b&foo=bar etc.
  698. if (isset($headers['Content-type'])) {
  699. switch ($headers['Content-type']) {
  700. case 'application/json':
  701. $data = json_encode($data);
  702. break;
  703. case 'application/x-www-form-urlencoded':
  704. $data = http_build_query($data);
  705. break;
  706. }
  707. } else {
  708. $headers['Content-type'] = 'application/x-www-form-urlencoded';
  709. $data = http_build_query($data);
  710. }
  711. // parse the given URL
  712. $urlDigest = parse_url($url);
  713. // extract host and path:
  714. $host = $urlDigest['host'].(isset($urlDigest['port'])?':'.$urlDigest['port']:'');
  715. $path = $urlDigest['path'];
  716. if ($urlDigest['scheme'] != 'http') {
  717. die('Error: Only HTTP request are supported, please use cURL to add HTTPS support! ('.$urlDigest['scheme'].'://'.$host.')');
  718. }
  719. // open a socket connection on port 80 - timeout: 30 sec
  720. $fp = fsockopen($host, 80, $errno, $errstr, 30);
  721. if ($fp){
  722. // send the request headers:
  723. fputs($fp, "POST $path HTTP/1.1\r\n");
  724. fputs($fp, "Host: $host\r\n");
  725. if ($referer != '')
  726. fputs($fp, "Referer: $referer\r\n");
  727. fputs($fp, "Content-length: ". strlen($data) ."\r\n");
  728. foreach($headers as $k => $v) {
  729. fputs($fp, $k.": ".$v."\r\n");
  730. }
  731. fputs($fp, "Connection: close\r\n\r\n");
  732. fputs($fp, $data);
  733. $result = '';
  734. while(!feof($fp)) {
  735. // receive the results of the request
  736. $result .= fgets($fp, 128);
  737. }
  738. }
  739. else {
  740. return array(
  741. 'status' => 'err',
  742. 'error' => "$errstr ($errno)"
  743. );
  744. }
  745. // close the socket connection:
  746. fclose($fp);
  747. // split the result header from the content
  748. $result = explode("\r\n\r\n", $result, 2);
  749. $header = isset($result[0]) ? $result[0] : '';
  750. $content = isset($result[1]) ? $result[1] : '';
  751. // return as structured array:
  752. return array(
  753. 'status' => 'ok',
  754. 'header' => $header,
  755. 'content' => $content,
  756. );
  757. }
  758. ?>