causefx 8 rokov pred
rodič
commit
8ccaf79ad6

+ 87 - 1
api/functions/auth-functions.php

@@ -103,7 +103,6 @@ if (function_exists('ldap_connect')){
 		return 'LDAP - Disabled (Dependancy: php-ldap missing!)';
 	}
 }
-
 // Pass credentials to FTP backend
 function plugin_auth_ftp($username, $password) {
 	// Calculate parts
@@ -135,3 +134,90 @@ function plugin_auth_ftp($username, $password) {
 	}
 	return false;
 }
+
+// Pass credentials to Emby Backend
+function plugin_auth_emby_local($username, $password) {
+	try{
+		$url = qualifyURL($GLOBALS['embyURL']).'/Users/AuthenticateByName';
+		$headers = array(
+			'Authorization'=> 'MediaBrowser UserId="e8837bc1-ad67-520e-8cd2-f629e3155721", Client="None", Device="Organizr", DeviceId="xxx", Version="1.0.0.0"',
+			'Content-Type' => 'application/json',
+		);
+		$data = array(
+			'Username' => $username,
+			'Password' => sha1($password),
+			'PasswordMd5' => md5($password),
+		);
+		$response = Requests::post($url, $headers, json_encode($data));
+		if($response->success){
+			$json = json_decode($response->body, true);
+			if (is_array($json) && isset($json['SessionInfo']) && isset($json['User']) && $json['User']['HasPassword'] == true) {
+				// Login Success - Now Logout Emby Session As We No Longer Need It
+				$headers = array(
+					'X-Mediabrowser-Token' => $json['AccessToken'],
+				);
+				$response = Requests::post(qualifyURL($GLOBALS['embyURL']).'/Sessions/Logout', $headers, array());
+				return true;
+			}
+		}
+		return false;
+	}catch( Requests_Exception $e ) {
+		writeLog('success', 'Emby Local Auth Function - Error: '.$e->getMessage(), $username);
+	};
+}
+// Authenicate against emby connect
+function plugin_auth_emby_connect($username, $password) {
+	try{
+		// Get A User
+		$connectId = '';
+		$url = qualifyURL($GLOBALS['embyURL']).'/Users?api_key='.$GLOBALS['embyToken'];
+		$response = Requests::get($url);
+		if($response->success){
+			$json = json_decode($response->body, true);
+			if (is_array($json)) {
+				foreach ($json as $key => $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',
+					);
+					$data = array(
+						'nameOrEmail' => $username,
+						'rawpw' => $password,
+					);
+					$response = Requests::post($connectURL, $headers, json_encode($data));
+					if($response->success){
+						$json = json_decode($response->body, true);
+						if (is_array($json) && isset($json['AccessToken']) && isset($json['User']) && $json['User']['Id'] == $connectId) {
+							return array(
+								'email' => $json['User']['Email'],
+								'image' => $json['User']['ImageUrl'],
+							);
+						}
+					}
+				}
+			}
+		}
+		return false;
+	}catch( Requests_Exception $e ) {
+		writeLog('success', 'Emby Connect Auth Function - Error: '.$e->getMessage(), $username);
+		return false;
+	};
+}
+// Authenticate Against Emby Local (first) and Emby Connect
+function plugin_auth_emby_all($username, $password) {
+	$localResult = plugin_auth_emby_local($username, $password);
+	if ($localResult) {
+		return $localResult;
+	} else {
+		return plugin_auth_emby_connect($username, $password);
+	}
+}

+ 71 - 0
api/functions/normal-functions.php

@@ -254,3 +254,74 @@ function array_filter_key(array $array, $callback){
 	$matchedKeys = array_filter(array_keys($array), $callback);
 	return array_intersect_key($array, array_flip($matchedKeys));
 }
+// Qualify URL
+function qualifyURL($url) {
+	//local address?
+	if(substr($url, 0,1) == "/"){
+		if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
+			$protocol = "https://";
+		} else {
+			$protocol = "http://";
+		}
+		$url = $protocol.getServer().$url;
+	}
+	// Get Digest
+	$digest = parse_url($url);
+	// http/https
+	if (!isset($digest['scheme'])) {
+		if (isset($digest['port']) && in_array($digest['port'], array(80,8080,8096,32400,7878,8989,8182,8081,6789))) {
+			$scheme = 'http';
+		} else {
+			$scheme = 'https';
+		}
+	} else {
+		$scheme = $digest['scheme'];
+	}
+	// Host
+	$host = (isset($digest['host'])?$digest['host']:'');
+	// Port
+	$port = (isset($digest['port'])?':'.$digest['port']:'');
+	// Path
+	$path = (isset($digest['path'])?$digest['path']:'');
+	// Output
+	return $scheme.'://'.$host.$port.$path;
+}
+function getServerPath() {
+	if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https"){
+		$protocol = "https://";
+	}elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
+        $protocol = "https://";
+    } else {
+        $protocol = "http://";
+    }
+	$domain = '';
+    if (isset($_SERVER['SERVER_NAME']) && strpos($_SERVER['SERVER_NAME'], '.') !== false){
+        $domain = $_SERVER['SERVER_NAME'];
+	}elseif(isset($_SERVER['HTTP_HOST'])){
+		if (strpos($_SERVER['HTTP_HOST'], ':') !== false) {
+			$domain = explode(':', $_SERVER['HTTP_HOST'])[0];
+			$port = explode(':', $_SERVER['HTTP_HOST'])[1];
+			if ($port == "80" || $port == "443"){
+				$domain = $domain;
+			}else{
+				$domain = $_SERVER['HTTP_HOST'];
+			}
+		}else{
+        	$domain = $_SERVER['HTTP_HOST'];
+		}
+	}
+    return $protocol . $domain . str_replace("\\", "/", dirname($_SERVER['REQUEST_URI']));
+}
+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';
+}
+function getServer(){
+    return isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : $_SERVER["SERVER_NAME"];
+}