Browse Source

Emby Connect, Comments, Cleanup

Added Support for Emby Connect
Cleaned up Plex recent and streams (hopefully it works)
Cerothen 9 years ago
parent
commit
4f2e2ba19b
1 changed files with 442 additions and 472 deletions
  1. 442 472
      functions.php

+ 442 - 472
functions.php

@@ -44,8 +44,58 @@ function plugin_auth_ftp($username, $password) {
 	return false;
 	return false;
 }
 }
 
 
+// Authenticate Against Emby Local (first) and Emby Connect
+function plugin_auth_emby_all($username, $password) {
+	return plugin_auth_emby($username, $password) || plugin_auth_emby_connect($username, $password);
+}
+
+// Authenicate against emby connect
+function plugin_auth_emby_connect($username, $password) {
+	$urlCheck = stripos(AUTHBACKENDHOST, "http");
+	if ($urlCheck === false) {
+		$embyAddress = "http://" . AUTHBACKENDHOST;
+	} else {
+		$embyAddress = AUTHBACKENDHOST;	
+	}
+	if(AUTHBACKENDPORT !== "") { $embyAddress .= ":" . AUTHBACKENDPORT; }
+	
+	// Get A User
+	$connectId = '';
+	$userIds = json_decode(file_get_contents($embyAddress.'/Users?api_key='.EMBYTOKEN),true);
+	foreach ($userIds as $value) { // Scan for this user
+		if (isset($value['ConnectUserName']) && isset($value['ConnectUserId'])) { // Qualifty as connect account
+			if ($value['ConnectUserName'] == $username || $value['Name'] == $username) {
+				$connectId = $value['ConnectUserId'];
+			}
+			break;
+		}
+	}
+	
+	if ($connectId) {
+		$connectURL = 'https://connect.emby.media/service/user/authenticate';
+		$headers = array(
+			'Accept'=> 'application/json',
+			'Content-Type' => 'application/x-www-form-urlencoded',
+		);
+		$body = array(
+			'nameOrEmail' => $username,
+			'rawpw' => $password,
+		);
+		
+		$result = json_decode(curl_post($connectURL, $body, $headers),true);
+		
+		if (isset($response['content'])) {
+			$json = json_decode($response['content'], true);
+			if (is_array($json) && isset($json['SessionInfo']) && isset($json['User']) && $json['User']['Id'] == $connectId) {
+				return true;
+			}
+		}
+	}
+	return false;
+}
+
 // Pass credentials to Emby Backend
 // Pass credentials to Emby Backend
-function plugin_auth_emby($username, $password) {
+function plugin_auth_emby_local($username, $password) {
 	$urlCheck = stripos(AUTHBACKENDHOST, "http");
 	$urlCheck = stripos(AUTHBACKENDHOST, "http");
 	if ($urlCheck === false) {
 	if ($urlCheck === false) {
 		$embyAddress = "http://" . AUTHBACKENDHOST;
 		$embyAddress = "http://" . AUTHBACKENDHOST;
@@ -56,7 +106,7 @@ function plugin_auth_emby($username, $password) {
 	
 	
 	$headers = array(
 	$headers = array(
 		'Authorization'=> 'MediaBrowser UserId="e8837bc1-ad67-520e-8cd2-f629e3155721", Client="None", Device="Organizr", DeviceId="xxx", Version="1.0.0.0"',
 		'Authorization'=> 'MediaBrowser UserId="e8837bc1-ad67-520e-8cd2-f629e3155721", Client="None", Device="Organizr", DeviceId="xxx", Version="1.0.0.0"',
-		'Content-type' => 'application/json',
+		'Content-Type' => 'application/json',
 	);
 	);
 	$body = array(
 	$body = array(
 		'Username' => $username,
 		'Username' => $username,
@@ -80,233 +130,130 @@ function plugin_auth_emby($username, $password) {
 	return false;
 	return false;
 }
 }
 
 
-function clean($strin) {
-    $strout = null;
-
-    for ($i = 0; $i < strlen($strin); $i++) {
-            $ord = ord($strin[$i]);
-
-            if (($ord > 0 && $ord < 32) || ($ord >= 127)) {
-                    $strout .= "&amp;#{$ord};";
-            }
-            else {
-                    switch ($strin[$i]) {
-                            case '<':
-                                    $strout .= '&lt;';
-                                    break;
-                            case '>':
-                                    $strout .= '&gt;';
-                                    break;
-                            case '&':
-                                    $strout .= '&amp;';
-                                    break;
-                            case '"':
-                                    $strout .= '&quot;';
-                                    break;
-                            default:
-                                    $strout .= $strin[$i];
-                    }
-            }
-    }
-
-    return $strout;
-    
+// Direct request to curl if it exists, otherwise handle if not HTTPS
+function post_router($url, $data, $headers = array(), $referer='') {
+	if (function_exists('curl_version')) {
+		return curl_post($url, $data, $headers, $referer);
+	} else {
+		return post_request($url, $data, $headers, $referer);
+	}
 }
 }
 
 
-function registration_callback($username, $email, $userdir){
-    
-    global $data;
-    
-    $data = array($username, $email, $userdir);
-
+// Curl Post
+function curl_post($url, $data, $headers = array(), $referer='') {
+	// Initiate cURL
+	$curlReq = curl_init($url);
+	// As post request
+	curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, "POST"); 
+	curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true);
+	// Format Data
+	switch ($headers['Content-Type']) {
+		case 'application/json': 
+			curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data));
+			break;
+		case 'application/x-www-form-urlencoded';
+			curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
+			break;
+		default:
+			$headers['Content-Type'] = 'application/x-www-form-urlencoded';
+			curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
+	}
+	// Format Headers
+	$cHeaders = array();
+	foreach ($headers as $k => $v) {
+		$cHeaders[] = $k.': '.$v;
+	}
+	if (count($cHeaders)) {
+		curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders);
+	}
+	// Execute
+	$result = curl_exec($curlReq);
+	// Close
+	curl_close($curlReq);
+	// Return
+	return array('content'=>$result);
 }
 }
 
 
-function printArray($arrayName){
-    
-    $messageCount = count($arrayName);
-    
-    $i = 0;
-    
-    foreach ( $arrayName as $item ) :
-    
-        $i++; 
-    
-        if($i < $messageCount) :
-    
-            echo "<small class='text-uppercase'>" . $item . "</small> & ";
-    
-        elseif($i = $messageCount) :
-    
-            echo "<small class='text-uppercase'>" . $item . "</small>";
-    
-        endif;
-        
-    endforeach;
+// HTTP post request (Removes need for curl, probably useless)
+function post_request($url, $data, $headers = array(), $referer='') {
+	// Adapted from http://stackoverflow.com/a/28387011/6810513
+	
+    // Convert the data array into URL Parameters like a=b&foo=bar etc.
+	if (isset($headers['Content-Type'])) {
+		switch ($headers['Content-Type']) {
+			case 'application/json':
+				$data = json_encode($data);
+				break;
+			case 'application/x-www-form-urlencoded':
+				$data = http_build_query($data);
+				break;
+		}
+	} else {
+		$headers['Content-Type'] = 'application/x-www-form-urlencoded';
+		$data = http_build_query($data);
+	}
     
     
-}
+    // parse the given URL
+    $urlDigest = parse_url($url);
 
 
-function write_ini_file($content, $path) { 
-    
-    if (!$handle = fopen($path, 'w')) {
-        
-        return false; 
-    
+    // extract host and path:
+    $host = $urlDigest['host'].(isset($urlDigest['port'])?':'.$urlDigest['port']:'');
+    $path = $urlDigest['path'];
+	
+    if ($urlDigest['scheme'] != 'http') {
+        die('Error: Only HTTP request are supported, please use cURL to add HTTPS support! ('.$urlDigest['scheme'].'://'.$host.')');
     }
     }
-    
-    $success = fwrite($handle, trim($content));
-    
-    fclose($handle); 
-    
-    return $success; 
-
-}
-
-function gotTimezone(){
 
 
-    $regions = array(
-        'Africa' => DateTimeZone::AFRICA,
-        'America' => DateTimeZone::AMERICA,
-        'Antarctica' => DateTimeZone::ANTARCTICA,
-        'Arctic' => DateTimeZone::ARCTIC,
-        'Asia' => DateTimeZone::ASIA,
-        'Atlantic' => DateTimeZone::ATLANTIC,
-        'Australia' => DateTimeZone::AUSTRALIA,
-        'Europe' => DateTimeZone::EUROPE,
-        'Indian' => DateTimeZone::INDIAN,
-        'Pacific' => DateTimeZone::PACIFIC
-    );
-    
-    $timezones = array();
-
-    foreach ($regions as $name => $mask) {
-        
-        $zones = DateTimeZone::listIdentifiers($mask);
-
-        foreach($zones as $timezone) {
+    // open a socket connection on port 80 - timeout: 30 sec
+    $fp = fsockopen($host, 80, $errno, $errstr, 30);
 
 
-            $time = new DateTime(NULL, new DateTimeZone($timezone));
+    if ($fp){
 
 
-            $ampm = $time->format('H') > 12 ? ' ('. $time->format('g:i a'). ')' : '';
+        // send the request headers:
+        fputs($fp, "POST $path HTTP/1.1\r\n");
+        fputs($fp, "Host: $host\r\n");
 
 
-            $timezones[$name][$timezone] = substr($timezone, strlen($name) + 1) . ' - ' . $time->format('H:i') . $ampm;
+        if ($referer != '')
+            fputs($fp, "Referer: $referer\r\n");
+		
+        fputs($fp, "Content-length: ". strlen($data) ."\r\n");
+		foreach($headers as $k => $v) {
+			fputs($fp, $k.": ".$v."\r\n");
+		}
+        fputs($fp, "Connection: close\r\n\r\n");
+        fputs($fp, $data);
 
 
+        $result = '';
+        while(!feof($fp)) {
+            // receive the results of the request
+            $result .= fgets($fp, 128);
         }
         }
-        
-    }   
-    
-    print '<select name="timezone" id="timezone" class="form-control material input-sm" required>';
-    
-    foreach($timezones as $region => $list) {
-    
-        print '<optgroup label="' . $region . '">' . "\n";
-    
-        foreach($list as $timezone => $name) {
-            
-            if($timezone == TIMEZONE) : $selected = " selected"; else : $selected = ""; endif;
-            
-            print '<option value="' . $timezone . '"' . $selected . '>' . $name . '</option>' . "\n";
-    
-        }
-    
-        print '</optgroup>' . "\n";
-    
     }
     }
-    
-    print '</select>';
-    
-}
-
-function getTimezone(){
-
-    $regions = array(
-        'Africa' => DateTimeZone::AFRICA,
-        'America' => DateTimeZone::AMERICA,
-        'Antarctica' => DateTimeZone::ANTARCTICA,
-        'Arctic' => DateTimeZone::ARCTIC,
-        'Asia' => DateTimeZone::ASIA,
-        'Atlantic' => DateTimeZone::ATLANTIC,
-        'Australia' => DateTimeZone::AUSTRALIA,
-        'Europe' => DateTimeZone::EUROPE,
-        'Indian' => DateTimeZone::INDIAN,
-        'Pacific' => DateTimeZone::PACIFIC
-    );
-    
-    $timezones = array();
-
-    foreach ($regions as $name => $mask) {
-        
-        $zones = DateTimeZone::listIdentifiers($mask);
-
-        foreach($zones as $timezone) {
-
-            $time = new DateTime(NULL, new DateTimeZone($timezone));
-
-            $ampm = $time->format('H') > 12 ? ' ('. $time->format('g:i a'). ')' : '';
-
-            $timezones[$name][$timezone] = substr($timezone, strlen($name) + 1) . ' - ' . $time->format('H:i') . $ampm;
-
-        }
-        
-    }   
-    
-    print '<select name="timezone" id="timezone" class="form-control material" required>';
-    
-    foreach($timezones as $region => $list) {
-    
-        print '<optgroup label="' . $region . '">' . "\n";
-    
-        foreach($list as $timezone => $name) {
-            
-            print '<option value="' . $timezone . '">' . $name . '</option>' . "\n";
-    
-        }
-    
-        print '</optgroup>' . "\n";
-    
+    else {
+        return array(
+            'status' => 'err',
+            'error' => "$errstr ($errno)"
+        );
     }
     }
-    
-    print '</select>';
-    
-}
 
 
-function explosion($string, $position){
-    
-    $getWord = explode("|", $string);
-    return $getWord[$position];
-    
-}
+    // close the socket connection:
+    fclose($fp);
 
 
-function getServerPath() {
-    
-    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { 
-        
-        $protocol = "https://"; 
-    
-    } else {  
-        
-        $protocol = "http://"; 
-    
-    }
-    
-    return $protocol . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']);
-      
-}
+    // split the result header from the content
+    $result = explode("\r\n\r\n", $result, 2);
 
 
-function get_browser_name() {
-    
-    $user_agent = $_SERVER['HTTP_USER_AGENT'];
-    
-    if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) return 'Opera';
-    elseif (strpos($user_agent, 'Edge')) return 'Edge';
-    elseif (strpos($user_agent, 'Chrome')) return 'Chrome';
-    elseif (strpos($user_agent, 'Safari')) return 'Safari';
-    elseif (strpos($user_agent, 'Firefox')) return 'Firefox';
-    elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) return 'Internet Explorer';
-    
-    return 'Other';
-    
+    $header = isset($result[0]) ? $result[0] : '';
+    $content = isset($result[1]) ? $result[1] : '';
+
+    // return as structured array:
+    return array(
+        'status' => 'ok',
+        'header' => $header,
+        'content' => $content,
+	);
 }
 }
 
 
+// Format item from Emby for Carousel
 function resolveEmbyItem($address, $token, $item) {
 function resolveEmbyItem($address, $token, $item) {
 	// Static Height
 	// Static Height
 	$height = 150;
 	$height = 150;
@@ -346,6 +293,46 @@ function resolveEmbyItem($address, $token, $item) {
 	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>';
 	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>';
 }
 }
 
 
+// Format item from Plex for Carousel
+function resolvePlexItem($server, $token, $item) {
+	// Static Height
+	$height = 150;
+	
+	$address = "https://app.plex.tv/web/app#!/server/$server/details?key=/library/metadata/".$item['ratingKey'];
+	
+	switch ($item['Type']) {
+		case 'season':
+			$title = $item['parentTitle'];
+			$summary = $item['parentSummary'];
+			$width = 100;
+			$image = 'carousel-image season';
+			$style = '';
+			break;
+		case 'album':
+			$title = $item['parentTitle'];
+			$summary = $item['title'];
+			$width = 150;
+			$image = 'album';
+			$style = 'left: 160px !important;';
+			break;
+		default:
+			$title = $item['title'];
+			$summary = $item['summary'];
+			$width = 100;
+			$image = 'carousel-image movie';
+			$style = '';
+	}
+	
+	// If No Overview
+	if (!isset($itemDetails['Overview'])) {
+		$itemDetails['Overview'] = '';
+	}
+	
+	// Assemble Item And Cache Into Array 
+	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>';
+}
+
+// Create Carousel
 function outputCarousel($header, $size, $type, $items) {
 function outputCarousel($header, $size, $type, $items) {
 	// If None Populate Empty Item
 	// If None Populate Empty Item
 	if (!count($items)) {
 	if (!count($items)) {
@@ -359,19 +346,20 @@ function outputCarousel($header, $size, $type, $items) {
 	$buttons = '';
 	$buttons = '';
 	if (count($items) > 1) {
 	if (count($items) > 1) {
 		$buttons = '
 		$buttons = '
-			<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>
-			<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>';
+			<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>';
 	}
 	}
 	
 	
 	return '
 	return '
 	<div class="col-lg-'.$size.'">
 	<div class="col-lg-'.$size.'">
 		<h5 class="text-center">'.$header.'</h5>
 		<h5 class="text-center">'.$header.'</h5>
-		<div id="carousel-'.$type.'-emby" class="carousel slide box-shadow white-bg" data-ride="carousel"><div class="carousel-inner" role="listbox">
+		<div id="carousel-'.$type.'" class="carousel slide box-shadow white-bg" data-ride="carousel"><div class="carousel-inner" role="listbox">
 			'.implode('',$items).'
 			'.implode('',$items).'
 		</div>'.$buttons.'
 		</div>'.$buttons.'
 	</div></div>'; 
 	</div></div>'; 
 }
 }
 
 
+// Get Now Playing Streams From Emby
 function getEmbyStreams($url, $port, $token, $size, $header) {
 function getEmbyStreams($url, $port, $token, $size, $header) {
     if (stripos($url, "http") === false) {
     if (stripos($url, "http") === false) {
         $url = "http://" . $url;
         $url = "http://" . $url;
@@ -392,9 +380,41 @@ function getEmbyStreams($url, $port, $token, $size, $header) {
 		}
 		}
 	}
 	}
 	
 	
-	return outputCarousel($header, $size, 'streams', $playingItems);
+	return outputCarousel($header, $size, 'streams-emby', $playingItems);
+}
+
+// Get Now Playing Streams From Plex
+function getPlexStreams($url, $port, $token, $size, $header){
+    if (stripos($url, "http") === false) {
+        $url = "http://" . $url;
+    }
+    
+    if ($port !== "") { 
+		$url = $url . ":" . $port;
+	}
+    
+    $address = $url;
+    
+	// Perform API requests
+    $api = file_get_contents($address."/status/sessions?X-Plex-Token=".$token);
+    $api = simplexml_load_string($api);
+    $getServer = file_get_contents($address."/servers?X-Plex-Token=".$token);
+    $getServer = simplexml_load_string($getServer);
+    
+	// Identify the local machine
+    foreach($getServer AS $child) {
+       $gotServer = $child['machineIdentifier'];
+    }
+	
+	$items = array();
+	foreach($api AS $child) {
+		$items[] = resolvePlexItem($gotServer, $token, $child);
+	}
+	
+	return outputCarousel($header, $size, 'streams-plex', $items);
 }
 }
 
 
+// Get Recent Content From Emby
 function getEmbyRecent($url, $port, $type, $token, $size, $header) {
 function getEmbyRecent($url, $port, $type, $token, $size, $header) {
     if (stripos($url, "http") === false) {
     if (stripos($url, "http") === false) {
         $url = "http://" . $url;
         $url = "http://" . $url;
@@ -439,209 +459,269 @@ function getEmbyRecent($url, $port, $type, $token, $size, $header) {
 		$items[] = resolveEmbyItem($address, $token, $v);
 		$items[] = resolveEmbyItem($address, $token, $v);
 	}
 	}
 	
 	
-	return outputCarousel($header, $size, $type, $items);
+	return outputCarousel($header, $size, $type.'-emby', $items);
 }
 }
 
 
+// Get Recent Content From Plex
 function getPlexRecent($url, $port, $type, $token, $size, $header){
 function getPlexRecent($url, $port, $type, $token, $size, $header){
-    
-    $urlCheck = stripos($url, "http");
-
-    if ($urlCheck === false) {
-        
+    if (stripos($url, "http") === false) {
         $url = "http://" . $url;
         $url = "http://" . $url;
-    
     }
     }
     
     
-    if($port !== ""){ $url = $url . ":" . $port; }
+    if ($port !== "") { 
+		$url = $url . ":" . $port;
+	}
     
     
     $address = $url;
     $address = $url;
     
     
+	// Perform Requests
     $api = file_get_contents($address."/library/recentlyAdded?X-Plex-Token=".$token);
     $api = file_get_contents($address."/library/recentlyAdded?X-Plex-Token=".$token);
     $api = simplexml_load_string($api);
     $api = simplexml_load_string($api);
-    $getServer = file_get_contents($address."/?X-Plex-Token=".$token);
+    $getServer = file_get_contents($address."/servers?X-Plex-Token=".$token);
     $getServer = simplexml_load_string($getServer);
     $getServer = simplexml_load_string($getServer);
-    $gotServer = $getServer['machineIdentifier'];
+	
+	// Identify the local machine
+    foreach($getServer AS $child) {
+       $gotServer = $child['machineIdentifier'];
+    }
+	
+	$items = array();
+	foreach($api AS $child) {
+		if($child['type'] == $type){
+			$items[] = resolvePlexItem($gotServer, $token, $child);
+		}
+	}
+	
+	return outputCarousel($header, $size, $type.'-plex', $items);
+}
 
 
+// ==============
+
+function clean($strin) {
+    $strout = null;
+
+    for ($i = 0; $i < strlen($strin); $i++) {
+            $ord = ord($strin[$i]);
+
+            if (($ord > 0 && $ord < 32) || ($ord >= 127)) {
+                    $strout .= "&amp;#{$ord};";
+            }
+            else {
+                    switch ($strin[$i]) {
+                            case '<':
+                                    $strout .= '&lt;';
+                                    break;
+                            case '>':
+                                    $strout .= '&gt;';
+                                    break;
+                            case '&':
+                                    $strout .= '&amp;';
+                                    break;
+                            case '"':
+                                    $strout .= '&quot;';
+                                    break;
+                            default:
+                                    $strout .= $strin[$i];
+                    }
+            }
+    }
+
+    return $strout;
+    
+}
+
+function registration_callback($username, $email, $userdir){
+    
+    global $data;
+    
+    $data = array($username, $email, $userdir);
+
+}
+
+function printArray($arrayName){
+    
+    $messageCount = count($arrayName);
+    
     $i = 0;
     $i = 0;
     
     
-    $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">';
+    foreach ( $arrayName as $item ) :
+    
+        $i++; 
+    
+        if($i < $messageCount) :
+    
+            echo "<small class='text-uppercase'>" . $item . "</small> & ";
+    
+        elseif($i = $messageCount) :
+    
+            echo "<small class='text-uppercase'>" . $item . "</small>";
+    
+        endif;
         
         
-    foreach($api AS $child) {
-     
-        if($child['type'] == $type){
-            
-            $i++;
-            
-            if($i == 1){ $active = "active"; }else{ $active = "";}
-            
-            $thumb = $child['thumb'];
-
-            $plexLink = "https://app.plex.tv/web/app#!/server/$gotServer/details?key=/library/metadata/".$child['ratingKey'];
-            
-            if($type == "movie"){ 
-                
-                $title = $child['title']; 
-                $summary = $child['summary'];
-                $height = "150";
-                $width = "100";
-            
-            }elseif($type == "season"){ 
-                
-                $title = $child['parentTitle'];
-                $summary = $child['parentSummary'];
-                $height = "150";
-                $width = "100";
-            
-            }elseif($type == "album"){
-                
-                $title = $child['parentTitle']; 
-                $summary = $child['title'];
-                $height = "150";
-                $width = "150";
-            
-            }
-            
-            
-            $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>';
-            
-            $plexLink = "";
+    endforeach;
+    
+}
 
 
-        }
+function write_ini_file($content, $path) { 
+    
+    if (!$handle = fopen($path, 'w')) {
         
         
+        return false; 
+    
     }
     }
     
     
-    $gotPlex .= '</div>';
+    $success = fwrite($handle, trim($content));
+    
+    fclose($handle); 
     
     
-    if ($i > 1){ 
+    return $success; 
 
 
-        $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>';
-        
-    }
+}
 
 
-    $gotPlex .= '</div></div>';
+function gotTimezone(){
 
 
-    $noPlex = '<div class="col-lg-'.$size.'"><h5 class="text-center">'.$header.'</h5>';
-    $noPlex .= '<div id="carousel-'.$type.'" class="carousel slide box-shadow white-bg" data-ride="carousel">';
-    $noPlex .= '<div class="carousel-inner" role="listbox">';
-    $noPlex .= '<div class="item active">';
-    $noPlex .= "<img alt='nada' class='carousel-image movie' src='images/nadaplaying.jpg'>";
-    $noPlex .= '<div class="carousel-caption"> <h4>Nothing New</h4> <small> <em>Get to Adding!</em> </small></div></div></div></div></div>';
+    $regions = array(
+        'Africa' => DateTimeZone::AFRICA,
+        'America' => DateTimeZone::AMERICA,
+        'Antarctica' => DateTimeZone::ANTARCTICA,
+        'Arctic' => DateTimeZone::ARCTIC,
+        'Asia' => DateTimeZone::ASIA,
+        'Atlantic' => DateTimeZone::ATLANTIC,
+        'Australia' => DateTimeZone::AUSTRALIA,
+        'Europe' => DateTimeZone::EUROPE,
+        'Indian' => DateTimeZone::INDIAN,
+        'Pacific' => DateTimeZone::PACIFIC
+    );
     
     
-    if ($i != 0){ return $gotPlex; }
-    if ($i == 0){ return $noPlex; }
+    $timezones = array();
 
 
-}
+    foreach ($regions as $name => $mask) {
+        
+        $zones = DateTimeZone::listIdentifiers($mask);
 
 
-function getPlexStreams($url, $port, $token, $size, $header){
-    
-    $urlCheck = stripos($url, "http");
+        foreach($zones as $timezone) {
 
 
-    if ($urlCheck === false) {
+            $time = new DateTime(NULL, new DateTimeZone($timezone));
+
+            $ampm = $time->format('H') > 12 ? ' ('. $time->format('g:i a'). ')' : '';
+
+            $timezones[$name][$timezone] = substr($timezone, strlen($name) + 1) . ' - ' . $time->format('H:i') . $ampm;
+
+        }
         
         
-        $url = "http://" . $url;
+    }   
     
     
-    }
+    print '<select name="timezone" id="timezone" class="form-control material input-sm" required>';
     
     
-    if($port !== ""){ $url = $url . ":" . $port; }
+    foreach($timezones as $region => $list) {
     
     
-    $address = $url;
+        print '<optgroup label="' . $region . '">' . "\n";
     
     
-    $api = file_get_contents($address."/status/sessions?X-Plex-Token=".$token);
-    $api = simplexml_load_string($api);
-    $getServer = file_get_contents($address."/servers?X-Plex-Token=".$token);
-    $getServer = simplexml_load_string($getServer);
+        foreach($list as $timezone => $name) {
+            
+            if($timezone == TIMEZONE) : $selected = " selected"; else : $selected = ""; endif;
+            
+            print '<option value="' . $timezone . '"' . $selected . '>' . $name . '</option>' . "\n";
+    
+        }
+    
+        print '</optgroup>' . "\n";
     
     
-    foreach($getServer AS $child) {
-
-       $gotServer = $child['machineIdentifier'];
     }
     }
     
     
-    $i = 0;
+    print '</select>';
     
     
-    $gotPlex = '<div class="col-lg-'.$size.'"><h5 class="text-center">'.$header.'</h5>';
-    $gotPlex .= '<div id="carousel-streams" class="carousel slide box-shadow white-bg" data-ride="carousel">';
-    $gotPlex .= '<div class="carousel-inner" role="listbox">';
-        
-    foreach($api AS $child) {
-     
-        $type = $child['type'];
+}
 
 
-        $plexLink = "https://app.plex.tv/web/app#!/server/$gotServer/details?key=/library/metadata/".$child['ratingKey'];
-            
-        $i++;
+function getTimezone(){
 
 
-        if($i == 1){ $active = "active"; }else{ $active = "";}
+    $regions = array(
+        'Africa' => DateTimeZone::AFRICA,
+        'America' => DateTimeZone::AMERICA,
+        'Antarctica' => DateTimeZone::ANTARCTICA,
+        'Arctic' => DateTimeZone::ARCTIC,
+        'Asia' => DateTimeZone::ASIA,
+        'Atlantic' => DateTimeZone::ATLANTIC,
+        'Australia' => DateTimeZone::AUSTRALIA,
+        'Europe' => DateTimeZone::EUROPE,
+        'Indian' => DateTimeZone::INDIAN,
+        'Pacific' => DateTimeZone::PACIFIC
+    );
+    
+    $timezones = array();
 
 
+    foreach ($regions as $name => $mask) {
         
         
-        if($type == "movie"){ 
-
-            $title = $child['title']; 
-            $summary = htmlentities($child['summary'], ENT_QUOTES);
-            $thumb = $child['thumb'];
-            $image = "movie";
-            $height = "150";
-            $width = "100";
-
-        }elseif($type == "episode"){ 
-
-            $title = $child['grandparentTitle'];
-            $summary = htmlentities($child['summary'], ENT_QUOTES);
-            $thumb = $child['grandparentThumb'];
-            $image = "season";
-            $height = "150";
-            $width = "100";
-
+        $zones = DateTimeZone::listIdentifiers($mask);
 
 
-        }elseif($type == "track"){
+        foreach($zones as $timezone) {
 
 
-            $title = $child['grandparentTitle'] . " - " . $child['parentTitle']; 
-            $summary = htmlentities($child['title'], ENT_QUOTES);
-            $thumb = $child['thumb'];
-            $image = "album";
-            $height = "150";
-            $width = "150";
+            $time = new DateTime(NULL, new DateTimeZone($timezone));
 
 
-        }elseif($type == "clip"){
+            $ampm = $time->format('H') > 12 ? ' ('. $time->format('g:i a'). ')' : '';
 
 
-            $title = $child['title'].' - Trailer';
-            $summary = ($child['summary'] != "" ? $child['summary'] : "<i>No summary loaded.</i>");
-            $thumb = ($child['thumb'] != "" ? $child['thumb'] : 'images/nadaplaying.jpg');
-            $image = "movie";
-            $height = "150";
-            $width = "100";
+            $timezones[$name][$timezone] = substr($timezone, strlen($name) + 1) . ' - ' . $time->format('H:i') . $ampm;
 
 
         }
         }
-
-        $gotPlex .= '<div class="item '.$active.'">';
-
-        $gotPlex .= "<a href='$plexLink' target='_blank'><img alt='$title' class='carousel-image $image' src='image.php?img=$thumb&height=$height&width=$width'></a>";
-
-        $gotPlex .= '<div class="carousel-caption '. $image . '""><h4>'.$title.'</h4><small><em>'.$summary.'</em></small></div></div>';
         
         
-        $plexLink = "";
-
+    }   
+    
+    print '<select name="timezone" id="timezone" class="form-control material" required>';
+    
+    foreach($timezones as $region => $list) {
+    
+        print '<optgroup label="' . $region . '">' . "\n";
+    
+        foreach($list as $timezone => $name) {
+            
+            print '<option value="' . $timezone . '">' . $name . '</option>' . "\n";
+    
+        }
+    
+        print '</optgroup>' . "\n";
+    
     }
     }
     
     
-    $gotPlex .= '</div>';
+    print '</select>';
+    
+}
+
+function explosion($string, $position){
+    
+    $getWord = explode("|", $string);
+    return $getWord[$position];
     
     
-    if ($i > 1){ 
+}
 
 
-        $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>';
+function getServerPath() {
+    
+    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { 
+        
+        $protocol = "https://"; 
+    
+    } else {  
         
         
+        $protocol = "http://"; 
+    
     }
     }
+    
+    return $protocol . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']);
+      
+}
 
 
-    $gotPlex .= '</div></div>';
+function get_browser_name() {
     
     
-    $noPlex = '<div class="col-lg-'.$size.'"><h5 class="text-center">'.$header.'</h5>';
-    $noPlex .= '<div id="carousel-streams" class="carousel slide box-shadow white-bg" data-ride="carousel">';
-    $noPlex .= '<div class="carousel-inner" role="listbox">';
-    $noPlex .= '<div class="item active">';
-    $noPlex .= "<img alt='nada' class='carousel-image movie' src='images/nadaplaying.jpg'>";
-    $noPlex .= '<div class="carousel-caption"><h4>Nothing Playing</h4><small><em>Get to Streaming!</em></small></div></div></div></div></div>';
+    $user_agent = $_SERVER['HTTP_USER_AGENT'];
+    
+    if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) return 'Opera';
+    elseif (strpos($user_agent, 'Edge')) return 'Edge';
+    elseif (strpos($user_agent, 'Chrome')) return 'Chrome';
+    elseif (strpos($user_agent, 'Safari')) return 'Safari';
+    elseif (strpos($user_agent, 'Firefox')) return 'Firefox';
+    elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) return 'Internet Explorer';
+    
+    return 'Other';
     
     
-    if ($i != 0){ return $gotPlex; }
-    if ($i == 0){ return $noPlex; }
-
 }
 }
 
 
 function getSickrageCalendarWanted($array){
 function getSickrageCalendarWanted($array){
@@ -752,7 +832,8 @@ function getSonarrCalendar($array){
 
 
         $i++;
         $i++;
         $seriesName = $child['series']['title'];
         $seriesName = $child['series']['title'];
-        $episodeID = $child['series']['tvdbId'];
+        $episodeID = $child['series']['imdbId'];
+        if(!isset($episodeID)){ $episodeID = ""; }
         $episodeName = htmlentities($child['title'], ENT_QUOTES);
         $episodeName = htmlentities($child['title'], ENT_QUOTES);
         if($child['episodeNumber'] == "1"){ $episodePremier = "true"; }else{ $episodePremier = "false"; }
         if($child['episodeNumber'] == "1"){ $episodePremier = "true"; }else{ $episodePremier = "false"; }
         $episodeAirDate = $child['airDateUtc'];
         $episodeAirDate = $child['airDateUtc'];
@@ -764,7 +845,7 @@ function getSonarrCalendar($array){
         $downloaded = $child['hasFile'];
         $downloaded = $child['hasFile'];
         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"; }
         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"; }
         
         
-        $gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded\", imagetype: \"tv\", url: \"https://thetvdb.com/?tab=series&id=$episodeID\" }, \n";
+        $gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded\", imagetype: \"tv\", url: \"http://www.imdb.com/title/$episodeID\" }, \n";
         
         
     }
     }
 
 
@@ -992,116 +1073,5 @@ function getHeadphonesCalendar($url, $port, $key, $list){
 
 
 }
 }
 
 
-function post_router($url, $data, $headers = array(), $referer='') {
-	if (function_exists('curl_version')) {
-		return curl_post($url, $data, $headers, $referer);
-	} else {
-		return post_request($url, $data, $headers, $referer);
-	}
-}
-
-function curl_post($url, $data, $headers = array(), $referer='') {
-	// Initiate cURL
-	$curlReq = curl_init($url);
-	// As post request
-	curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, "POST"); 
-	curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true);
-	// Format Headers
-	$cHeaders = array();
-	foreach ($headers as $k => $v) {
-		$cHeaders[] = $k.': '.$v;
-	}
-	if (count($cHeaders)) {
-		curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders);
-	}
-	// Format Data
-	curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data));
-	// Execute
-	$result = curl_exec($curlReq);
-	// Close
-	curl_close($curlReq);
-	// Return
-	return array('content'=>$result);
-}
-
-function post_request($url, $data, $headers = array(), $referer='') {
-	// Adapted from http://stackoverflow.com/a/28387011/6810513
-	
-    // Convert the data array into URL Parameters like a=b&foo=bar etc.
-	if (isset($headers['Content-type'])) {
-		switch ($headers['Content-type']) {
-			case 'application/json':
-				$data = json_encode($data);
-				break;
-			case 'application/x-www-form-urlencoded':
-				$data = http_build_query($data);
-				break;
-		}
-	} else {
-		$headers['Content-type'] = 'application/x-www-form-urlencoded';
-		$data = http_build_query($data);
-	}
-    
-    // parse the given URL
-    $urlDigest = parse_url($url);
-
-    // extract host and path:
-    $host = $urlDigest['host'].(isset($urlDigest['port'])?':'.$urlDigest['port']:'');
-    $path = $urlDigest['path'];
-	
-    if ($urlDigest['scheme'] != 'http') {
-        die('Error: Only HTTP request are supported, please use cURL to add HTTPS support! ('.$urlDigest['scheme'].'://'.$host.')');
-    }
-
-    // open a socket connection on port 80 - timeout: 30 sec
-    $fp = fsockopen($host, 80, $errno, $errstr, 30);
-
-    if ($fp){
-
-        // send the request headers:
-        fputs($fp, "POST $path HTTP/1.1\r\n");
-        fputs($fp, "Host: $host\r\n");
-
-        if ($referer != '')
-            fputs($fp, "Referer: $referer\r\n");
-		
-        fputs($fp, "Content-length: ". strlen($data) ."\r\n");
-		foreach($headers as $k => $v) {
-			fputs($fp, $k.": ".$v."\r\n");
-		}
-        fputs($fp, "Connection: close\r\n\r\n");
-        fputs($fp, $data);
-
-        $result = '';
-        while(!feof($fp)) {
-            // receive the results of the request
-            $result .= fgets($fp, 128);
-        }
-    }
-    else {
-        return array(
-            'status' => 'err',
-            'error' => "$errstr ($errno)"
-        );
-    }
-
-    // close the socket connection:
-    fclose($fp);
-
-    // split the result header from the content
-    $result = explode("\r\n\r\n", $result, 2);
-
-    $header = isset($result[0]) ? $result[0] : '';
-    $content = isset($result[1]) ? $result[1] : '';
-
-    // return as structured array:
-    return array(
-        'status' => 'ok',
-        'header' => $header,
-        'content' => $content,
-	);
-}
 
 
-	
-	
 ?>
 ?>