Browse Source

Org now uses UUID properly
Redid Plex oAuth - Thanks Swifty
Fix #950 (Guest group rename bug)

causefx 7 years ago
parent
commit
ff5442fca8

+ 7 - 1
api/functions.php

@@ -9,17 +9,23 @@ foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'functions' . DIRECTORY_SEPARATOR
 }
 // Set Root Directory
 $GLOBALS['root'] = dirname(__DIR__, 1);
+$GLOBALS['uuid'] = '';
 // Add in default and custom settings
 configLazy();
 // Define Logs and files after db location is set
 if (isset($GLOBALS['dbLocation'])) {
 	$GLOBALS['organizrLog'] = $GLOBALS['dbLocation'] . 'organizrLog.json';
 	$GLOBALS['organizrLoginLog'] = $GLOBALS['dbLocation'] . 'organizrLoginLog.json';
+	if (($GLOBALS['uuid'] == '')) {
+		$uuid = gen_uuid();
+		$GLOBALS['uuid'] = $uuid;
+		updateConfig(array('uuid' => $uuid));
+	}
 	//Upgrade Check
 	upgradeCheck();
 }
 // Cookie name
-$GLOBALS['cookieName'] = isset($GLOBALS['organizrAPI']) ? 'organizr_token_' . hash('sha256', $GLOBALS['organizrAPI']) : 'organizr_token_temp';
+$GLOBALS['cookieName'] = $GLOBALS['uuid'] !== '' ? 'organizr_token_' . $GLOBALS['uuid'] : 'organizr_token_temp';
 // Validate Token if set and set guest if not - sets GLOBALS
 getOrganizrUserToken();
 // Include all pages files

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

@@ -436,7 +436,7 @@ function getGuest()
 				'driver' => 'sqlite3',
 				'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
 			]);
-			$all = $connect->fetch('SELECT * FROM groups WHERE `group` = "Guest"');
+			$all = $connect->fetch('SELECT * FROM groups WHERE `group_id` = 999');
 			return $all;
 		} catch (Dibi\Exception $e) {
 			return false;

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

@@ -145,7 +145,7 @@ function plugin_auth_plex($username, $password)
 				'Content-Type' => 'application/x-www-form-urlencoded',
 				'X-Plex-Product' => 'Organizr',
 				'X-Plex-Version' => '2.0',
-				'X-Plex-Client-Identifier' => '01010101-10101010',
+				'X-Plex-Client-Identifier' => $GLOBALS['uuid'],
 			);
 			$data = array(
 				'user[login]' => $username,

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

@@ -559,4 +559,23 @@ function utf8ize($mixed)
 		return mb_convert_encoding($mixed, "UTF-8", "UTF-8");
 	}
 	return $mixed;
+}
+
+function gen_uuid()
+{
+	return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
+		// 32 bits for "time_low"
+		mt_rand(0, 0xffff), mt_rand(0, 0xffff),
+		// 16 bits for "time_mid"
+		mt_rand(0, 0xffff),
+		// 16 bits for "time_hi_and_version",
+		// four most significant bits holds version number 4
+		mt_rand(0, 0x0fff) | 0x4000,
+		// 16 bits, 8 bits for "clk_seq_hi_res",
+		// 8 bits for "clk_seq_low",
+		// two most significant bits holds zero and one for variant DCE1.1
+		mt_rand(0, 0x3fff) | 0x8000,
+		// 48 bits for "node"
+		mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
+	);
 }

+ 2 - 1
api/functions/organizr-functions.php

@@ -73,6 +73,7 @@ function organizrSpecialSettings()
 			'unsortedTabs' => $GLOBALS['unsortedTabs'],
 			'authBackend' => $GLOBALS['authBackend'],
 			'newMessageSound' => $GLOBALS['CHAT-newMessageSound-include'],
+			'uuid' => $GLOBALS['uuid'],
 		)
 	);
 }
@@ -1760,7 +1761,7 @@ function plexJoin($username, $email, $password)
 			'Content-Type' => 'application/x-www-form-urlencoded',
 			'X-Plex-Product' => 'Organizr',
 			'X-Plex-Version' => '2.0',
-			'X-Plex-Client-Identifier' => '01010101-10101010',
+			'X-Plex-Client-Identifier' => $GLOBALS['uuid'],
 		);
 		$data = array(
 			'user[email]' => $email,

+ 149 - 49
js/functions.js

@@ -4678,65 +4678,164 @@ function homepageCalendar(timeout){
 	if(typeof timeouts['calendar'] !== 'undefined'){ clearTimeout(timeouts['calendar']); }
 	timeouts['calendar'] = setTimeout(function(){ homepageCalendar(timeout); }, timeout);
 }
-function oAuthLoop(type,code) {
-    switch(type) {
-        case 'plex':
-            return $.ajax({
+// Thanks Swifty!
+function PopupCenter(url, title, w, h) {
+    // Fixes dual-screen position                         Most browsers      Firefox
+    var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : window.screenX;
+    var dualScreenTop = window.screenTop != undefined ? window.screenTop : window.screenY;
+    var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
+    var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
+    var left = ((width / 2) - (w / 2)) + dualScreenLeft;
+    var top = ((height / 2) - (h / 2)) + dualScreenTop;
+    var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
+    // Puts focus on the newWindow
+    if (window.focus) {
+        newWindow.focus();
+    }
+    return newWindow;
+}
+function getPlexHeaders(){
+    return {
+        'Accept': 'application/json',
+        'X-Plex-Product': activeInfo.appearance.title,
+        'X-Plex-Version': '2.0',
+        'X-Plex-Client-Identifier': activeInfo.settings.misc.uuid,
+    };
+}
+var plex_oauth_window = null;
+const plex_oauth_loader = '<style>' +
+    '.login-loader-container {' +
+    'font-family: "Open Sans", Arial, sans-serif;' +
+    'position: absolute;' +
+    'top: 0;' +
+    'right: 0;' +
+    'bottom: 0;' +
+    'left: 0;' +
+    '}' +
+    '.login-loader-message {' +
+    'color: #282A2D;' +
+    'text-align: center;' +
+    'position: absolute;' +
+    'left: 50%;' +
+    'top: 25%;' +
+    'transform: translate(-50%, -50%);' +
+    '}' +
+    '.login-loader {' +
+    'border: 5px solid #ccc;' +
+    '-webkit-animation: spin 1s linear infinite;' +
+    'animation: spin 1s linear infinite;' +
+    'border-top: 5px solid #282A2D;' +
+    'border-radius: 50%;' +
+    'width: 50px;' +
+    'height: 50px;' +
+    'position: relative;' +
+    'left: calc(50% - 25px);' +
+    '}' +
+    '@keyframes spin {' +
+    '0% { transform: rotate(0deg); }' +
+    '100% { transform: rotate(360deg); }' +
+    '}' +
+    '</style>' +
+    '<div class="login-loader-container">' +
+    '<div class="login-loader-message">' +
+    '<div class="login-loader"></div>' +
+    '<br>' +
+    'Redirecting to the Plex login page...' +
+    '</div>' +
+    '</div>';
+function closePlexOAuthWindow() {
+    if (plex_oauth_window) {
+        plex_oauth_window.close();
+    }
+}
+getPlexOAuthPin = function () {
+    var x_plex_headers = getPlexHeaders();
+    var deferred = $.Deferred();
+    $.ajax({
+        url: 'https://plex.tv/api/v2/pins?strong=true',
+        type: 'POST',
+        headers: x_plex_headers,
+        success: function(data) {
+            deferred.resolve({pin: data.id, code: data.code});
+        },
+        error: function() {
+            closePlexOAuthWindow();
+            deferred.reject();
+        }
+    });
+    return deferred;
+};
+var polling = null;
+function PlexOAuth(success, error, pre) {
+    if (typeof pre === "function") {
+        pre()
+    }
+    closePlexOAuthWindow();
+    plex_oauth_window = PopupCenter('', 'Plex-OAuth', 600, 700);
+    $(plex_oauth_window.document.body).html(plex_oauth_loader);
+    getPlexOAuthPin().then(function (data) {
+        var x_plex_headers = getPlexHeaders();
+        const pin = data.pin;
+        const code = data.code;
+        plex_oauth_window.location = 'https://app.plex.tv/auth/#!?clientID=' + x_plex_headers['X-Plex-Client-Identifier'] + '&code=' + code;
+        polling = pin;
+        (function poll() {
+            $.ajax({
+                url: 'https://plex.tv/api/v2/pins/' + pin,
                 type: 'GET',
-                headers: {
-                    'X-Plex-Product':'Organizr',
-                    'X-Plex-Version':'2.0',
-                    'X-Plex-Client-Identifier':'01010101-10101010'
+                headers: x_plex_headers,
+                success: function (data) {
+                    if (data.authToken){
+                        closePlexOAuthWindow();
+                        if (typeof success === "function") {
+                            success('plex',data.authToken)
+                        }
+                    }
+                },
+                error: function (jqXHR, textStatus, errorThrown) {
+                    if (textStatus !== "timeout") {
+                        closePlexOAuthWindow();
+                        if (typeof error === "function") {
+                            error()
+                        }
+                    }
                 },
-                url: 'https://plex.tv/api/v2/pins/'+code,
+                complete: function () {
+                    if (!plex_oauth_window.closed && polling === pin){
+                        setTimeout(function() {poll()}, 1000);
+                    }
+                },
+                timeout: 10000
             });
+        })();
+    }, function () {
+        closePlexOAuthWindow();
+        if (typeof error === "function") {
+            error()
+        }
+    });
+}
+function oAuthSuccess(type,token){
+    switch(type) {
+        case 'plex':
+            $('#oAuth-Input').val(token);
+            $('#oAuthType-Input').val(type);
+            $('#login-username-Input').addClass('hidden');
+            $('#login-password-Input').addClass('hidden');
+            $('#oAuth-div').removeClass('hidden');
+            $('.login-button').trigger('click');
+            break;
         default:
             break;
     }
 }
+function oAuthError(){
+    messageSingle('',window.lang.translate('Error Connecting to oAuth Provider'),activeInfo.settings.notifications.position,'#FFF','error','5000');
+}
 function oAuthStart(type){
     switch(type){
         case 'plex':
-            $.ajax({
-                type: 'POST',
-                headers: {
-                    'X-Plex-Product':'Organizr',
-                    'X-Plex-Version':'2.0',
-                    'X-Plex-Client-Identifier':'01010101-10101010'
-                },
-                url: 'https://plex.tv/api/v2/pins?strong=true',
-                cache: false,
-                async: false,
-                complete: function(xhr, status) {
-                    if (xhr.status === 201) {
-                        var result = $.parseXML(xhr.responseText),
-                        $xml = $( result ), $title = $xml.find( "pin" ), $id = $title.find("id");
-                        var id = $title['0'].attributes.id.value;
-                        var code = $title['0'].attributes.code.value;
-                        window.open(`https://app.plex.tv/auth/#!?clientID=01010101-10101010&code=`+code, "Organizr!!", `menubar=no,location=no,resizable=no,scrollbars=no,status=no,width=1050,height=1050,left=0`);
-                        var loopAuth = setInterval(function(){
-                            var success =  oAuthLoop('plex',id).success(function(data) {
-                                $xml = $( data ), $title = $xml.find( "pin" ), $id = $title.find("id");
-                                var id = $title['0'].attributes.id.value;
-                                var code = $title['0'].attributes.code.value;
-                                var authToken = $title['0'].attributes.authToken.value;
-                                success = (authToken !== '') ? authToken : false;
-                                if(success !== false){
-                                    clearInterval(loopAuth);
-                                    $('#oAuth-Input').val(success);
-                                    $('#oAuthType-Input').val('plex');
-                                    $('#login-username-Input').addClass('hidden');
-                                    $('#login-password-Input').addClass('hidden');
-                                    $('#oAuth-div').removeClass('hidden');
-                                    $('.login-button').trigger('click');
-                                }
-                            });
-                        }, 1000);
-                    } else {
-                        console.log('An error - will add message later');
-                    }
-                }
-            });
+            PlexOAuth(oAuthSuccess,oAuthError);
             break;
         default:
             break;
@@ -5548,6 +5647,7 @@ function launch(){
 			branch:json.branch,
 			sso:json.sso,
 			settings:json.settings,
+            appearance:json.appearance,
 			theme:json.theme,
 			style:json.style,
 			version:json.version