functions.php 34 KB

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