From e309ce1fb46a89a6e43c4bb379797158d4e13e74 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Sun, 9 Jun 2019 12:57:28 -0600 Subject: [PATCH] Migrate stored dictionary to https if loading http --- src/index.js | 3 ++ src/js/migration.js | 74 +++++++++++++++++++++++++++++++++++++++++++++ src/php/router.php | 6 ++++ template-index.html | 1 + 4 files changed, 84 insertions(+) create mode 100644 src/js/migration.js diff --git a/src/index.js b/src/index.js index aa62245..ccc92e8 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +import migrate from './js/migration'; import setupListeners from './js/setupListeners'; import { renderAll } from './js/render'; import { hasToken, addMessage } from './js/utilities'; @@ -10,6 +11,8 @@ function initialize() { addMessage('You are using the Offline version of Lexiconga.
Refresh the page while connected to the internet to enable using accounts.', 0); } + migrate(); + loadSettings(); loadDictionary(); setupListeners(); diff --git a/src/js/migration.js b/src/js/migration.js new file mode 100644 index 0000000..8e53423 --- /dev/null +++ b/src/js/migration.js @@ -0,0 +1,74 @@ +import { LOCAL_STORAGE_KEY } from "../constants"; + +export default function migrate() { + if (window.location.pathname === '/') { + if (isNotSecure()) { + sendDictionaryToHTTPS(); + } else { + checkForReceived(); + } + } +} + +function isNotSecure() { + return window.location.host !== 'localhost' && window.location.protocol !== 'https'; +} + +function sendDictionaryToHTTPS() { + const storedDictionary = window.localStorage.getItem(LOCAL_STORAGE_KEY); + const httpsURL = 'https://' + window.location.host; + if (storedDictionary) { + if (!dictionaryIsOldDefault(storedDictionary)) { + const form = document.createElement('form'); + form.action = httpsURL; + form.method = 'POST'; + form.hidden = true; + const field = document.createElement('input'); + field.name = 'oldDictionaryFromHTTP'; + field.value = storedDictionary; + form.appendChild(field); + const blackoutShield = document.createElement('div'); + blackoutShield.classList.add('modal-background'); + document.body.appendChild(form); + document.body.appendChild(blackoutShield); + alert('You are about to be redirected to the secure https version of Lexiconga. Please update your bookmarks.') + form.submit(); + return; + } + } + window.location = httpsURL; +} + +function dictionaryIsOldDefault(dictionaryJSON) { + const defaultDictionary = { + name: "New", + description: "A new dictionary.", + // createdBy: publicName, + words: [], + nextWordId: 1, + settings: { + allowDuplicates: false, + caseSensitive: false, + partsOfSpeech: "Noun,Adjective,Verb,Adverb,Preposition,Pronoun,Conjunction", + sortByEquivalent: false, + isComplete: false, + isPublic: false + }, + externalID: 0 + } + const dictionary = JSON.parse(dictionaryJSON); + delete dictionary.createdBy; + + return JSON.stringify(defaultDictionary) === JSON.stringify(dictionary); +} + +function checkForReceived() { + if (window.hasOwnProperty('dictionaryImportedFromHTTP')) { + const storedDictionary = window.localStorage.getItem(LOCAL_STORAGE_KEY); + if (!storedDictionary) { + window.localStorage.setItem(LOCAL_STORAGE_KEY, window.dictionaryImportedFromHTTP); + delete window.dictionaryImportedFromHTTP; + return; + } + } +} \ No newline at end of file diff --git a/src/php/router.php b/src/php/router.php index cd191e6..f18213d 100644 --- a/src/php/router.php +++ b/src/php/router.php @@ -123,6 +123,12 @@ switch ($view) { "; $html = str_replace('{{upup_insert}}', $upup_insert, $html); + + $imported_from_http = ''; + if (isset($_POST['oldDictionaryFromHTTP'])) { + $imported_from_http = ''; + } + $html = str_replace('{{imported_from_http}}', $imported_from_http, $html); return Response::html($html); break; diff --git a/template-index.html b/template-index.html index d485bfe..c33beb0 100644 --- a/template-index.html +++ b/template-index.html @@ -7,6 +7,7 @@ Lexiconga + {{imported_from_http}}