functions.php 35 KB

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