Migrate stored dictionary to https if loading http

This commit is contained in:
Robbie Antenesse 2019-06-09 12:57:28 -06:00
parent 54aa0e2667
commit e309ce1fb4
4 changed files with 84 additions and 0 deletions

View File

@ -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('<strong>You are using the Offline version of Lexiconga.</strong><br>Refresh the page while connected to the internet to enable using accounts.', 0);
}
migrate();
loadSettings();
loadDictionary();
setupListeners();

74
src/js/migration.js Normal file
View File

@ -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;
}
}
}

View File

@ -124,6 +124,12 @@ switch ($view) {
$html = str_replace('{{upup_insert}}', $upup_insert, $html);
$imported_from_http = '';
if (isset($_POST['oldDictionaryFromHTTP'])) {
$imported_from_http = '<script>window.dictionaryImportedFromHTTP = "' . addslashes($_POST['oldDictionaryFromHTTP']) . '";</script>';
}
$html = str_replace('{{imported_from_http}}', $imported_from_http, $html);
return Response::html($html);
break;
}

View File

@ -7,6 +7,7 @@
<title>Lexiconga</title>
<link rel="icon" href="src/images/favicon.png" type="image/x-icon" />
<link rel="stylesheet" href="src/main.scss" />
{{imported_from_http}}
<script src="src/index.js"></script>
</head>
<body id="defaultTheme">