From 10e88ee697a4158bb4a9ca0c9731e5a793725e61 Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 19 Apr 2018 21:18:17 -0600 Subject: [PATCH] Version 8: * Local storage is no longer a strict requirement to run UI3, but cookies are. Login version 17: * Local storage feature detection improved to not throw an exception when the feature is unavailable. --- applet/loginScripts.js | 2 ++ login.htm | 27 ++++++++++++++++++--------- ui3/ui3.js | 42 ++++++++++++++++++++++++++++-------------- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/applet/loginScripts.js b/applet/loginScripts.js index 133bab2..605b7c9 100644 --- a/applet/loginScripts.js +++ b/applet/loginScripts.js @@ -27,6 +27,8 @@ b.length;)c=this._keyStr.indexOf(b.charAt(e++)),a=this._keyStr.indexOf(b.charAt( // ExecJSON, UrlParameters var jsonBaseUrl="";function ExecJSON(b,c,d){$.ajax({type:"POST",url:jsonBaseUrl+"json",contentType:"text/plain",data:JSON.stringify(b),dataType:"json",success:function(a){c&&c(a)},error:function(a,e,f){a&&404==a.status&&""==jsonBaseUrl?(jsonBaseUrl="/",ExecJSON(b,c,d)):d&&d(a,e,f)}})} var UrlParameters={loaded:!1,parsed_url_params:{},Get:function(b){if(!this.loaded){var c=this.parsed_url_params;window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(b,a,e){c[a.toLowerCase()]=decodeURIComponent(e)});this.loaded=!0}return"undefined"!=typeof this.parsed_url_params[b.toLowerCase()]?this.parsed_url_params[b.toLowerCase()]:""}}; +// Cookie / Local Storage feature detection +function areCookiesEnabled(){try{var a=$.cookie("session");if(a)return!0;$.cookie("session","test",{path:"/"});a=$.cookie("session");$.cookie("session","",{path:"/"});return"test"===a}catch(b){}return!1}function isLocalStorageEnabled(){try{return localStorage.setItem("local_storage_test_item","local_storage_test_item"),localStorage.removeItem("local_storage_test_item"),!0}catch(a){return!1}}; // jquery.longpress.js (function(c){c.fn.longpress=function(f,g,d){"undefined"===typeof d&&(d=500);return this.each(function(){function h(a){if(1==a.which){k=(new Date).getTime();var b=c(this);l=a.pageX;m=a.pageY;e=setTimeout(function(){"function"===typeof f?f.call(b,a):c.error("Callback required for long press. You provided: "+typeof f)},d)}}function n(a){1==a.which&&(new Date).getTime()-k Blue Iris Login @@ -104,9 +104,13 @@

%%SYSNAME%%

var existingSession = ""; var isStoredDataLoaded = false; var windowUnloading = false; + var cookiesEnabled; + var localStorageEnabled; $(function () { - if (UrlParameters.Get("autologin") == "0") + cookiesEnabled = areCookiesEnabled(); + localStorageEnabled = isLocalStorageEnabled(); + if (UrlParameters.Get("autologin") === "0") { SetPersistedValue("bi_override_disable_auto_login_once", "1"); location.href = location.href.replace(/autologin=0&?/gi, ''); @@ -115,12 +119,14 @@

%%SYSNAME%%

clearTimeout(showLoadingMessageTimeout); $("#loginLoading").hide(); $("#login").show(); - if (typeof window.JSON == 'undefined') + if (typeof window.JSON === 'undefined') { - $("#login").text("Your web browser is too old to use the Blue Iris web interface properly.\n\nTo proceed with this browser, disable the \"Secure only\" requirement within Blue Iris's web server settings."); + $("#login").html("
Your web browser is too old to use the Blue Iris web interface properly.

To proceed with this browser, disable the \"Secure only\" requirement within Blue Iris's web server settings.
"); $("#login").css("color", "#EEEEEE").css("margin", "8px"); return; } + if (!cookiesEnabled && !localStorageEnabled) + $("#cbLoginAutomatically").parent().text("Note: Cookies and Local Storage are disabled in your browser.").css("color", "#EEEEEE"); SetupLoginContextMenu(); SetStatus(); $(window).resize(resized); @@ -258,10 +264,13 @@

%%SYSNAME%%

} function LeaveLoginPage() { - $.cookie("session", existingSession, { path: "/" }); var page = UrlParameters.Get("page"); if (page == "") page = "/"; + if (cookiesEnabled) + $.cookie("session", existingSession, { path: "/" }); + else + page += (page.indexOf("?") < 0 ? "?" : "&") + "session=" + existingSession; location.href = page + location.hash; } function cancelAutoLogin() @@ -291,9 +300,9 @@

%%SYSNAME%%

function GetPersistedValue(key) { var value; - if (typeof Storage !== "undefined" && localStorage) + if (localStorageEnabled) value = localStorage.getItem(key); - else + else if (cookiesEnabled) value = $.cookie(key); if (!value) value = ""; @@ -301,9 +310,9 @@

%%SYSNAME%%

} function SetPersistedValue(key, value) { - if (typeof Storage !== "undefined" && localStorage) + if (localStorageEnabled) return localStorage.setItem(key, value); - else + else if (cookiesEnabled) return $.cookie(key, value, { expires: 365 }); } function pwKeypress(ele, e) diff --git a/ui3/ui3.js b/ui3/ui3.js index 589cce3..1c63fc2 100644 --- a/ui3/ui3.js +++ b/ui3/ui3.js @@ -26,8 +26,8 @@ function DoUIFeatureDetection() requestAnimationFramePolyFill(); if (!isCanvasSupported()) MissingRequiredFeature("HTML5 Canvas"); // Excludes IE 8 - else if (!isLocalStorageEnabled()) - MissingRequiredFeature("Local Storage"); + else if (!areCookiesEnabled()) + MissingRequiredFeature("Cookies", "Cookies are required for UI3's session management."); else { // All critical tests pass @@ -121,18 +121,32 @@ function DoUIFeatureDetection() } } } -function MissingRequiredFeature(featureName) +function MissingRequiredFeature(featureName, description) { - alert("This web interface requires a feature that is unavailable or disabled in your web browser.\n\nMissing feature: " + featureName + "\n\nYou will be redirected to a simpler web interface."); + alert("This web interface requires a feature that is unavailable or disabled in your web browser.\n\nMissing feature: " + featureName + (description ? ". " + description : "") + "\n\nYou will be redirected to a simpler web interface."); } function isCanvasSupported() { var elem = document.createElement('canvas'); return !!(elem.getContext && elem.getContext('2d')); } -function isLocalStorageEnabled() +function areCookiesEnabled() { try + { + var session = $.cookie("session"); + if (session) + return true; + $.cookie("session", "test", { path: "/" }); + session = $.cookie("session") + $.cookie("session", "", { path: "/" }); + return session === "test"; + } catch (e) { } + return false; +} +function isLocalStorageEnabled() +{ + try // May throw exception if local storage is disabled by browser settings! { var key = "local_storage_test_item"; localStorage.setItem(key, key); @@ -1179,14 +1193,8 @@ function GetLocalStorage() /// Returns the localStorage object, or a dummy localStorage object if the localStorage object is not available. /// This method should be used only when the wrapped localStorage object is not desired (e.g. when using settings that are persisted globally, not specific to a Blue Iris server). /// - try - { - if (typeof (Storage) !== "undefined" && localStorage) - return localStorage; // May throw exception if local storage is disabled by browser settings! - } - catch (ex) - { - } + if (isLocalStorageEnabled()) + return localStorage; return GetDummyLocalStorage(); } function IsNewGeneration(key, gen) @@ -1209,7 +1217,7 @@ function IsNewGeneration(key, gen) function GetLocalStorageWrapper() { /// Returns the local storage object or a wrapper suitable for the current Blue Iris server. The result of this should be stored in the settings variable. - if (typeof (Storage) !== "undefined" && localStorage) + if (isLocalStorageEnabled()) { if (currentServer.isUsingRemoteServer) { @@ -1309,6 +1317,12 @@ $(function () var fileSystemErrorMessage = "This interface must be loaded through the Blue Iris web server, and cannot function when loaded directly from your filesystem."; alert(fileSystemErrorMessage); toaster.Error(fileSystemErrorMessage, 60000); + return; + } + + if (!isLocalStorageEnabled()) + { + toaster.Warning("Local Storage is disabled or unavailable in your browser. Settings will not be saved between sessions.", 10000); } $("#ui_version_label").text(ui_version);