functions.php 34 KB

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