diff --git a/index.php b/index.php index fbe01c0..02020dc 100644 --- a/index.php +++ b/index.php @@ -8,7 +8,7 @@ $notificationMessage = ""; if (isset($_GET['logout']) && $current_user > 0) { session_destroy(); - header('Location: ./index2.php?loggedout'); + header('Location: ./?loggedout'); } elseif (isset($_GET['login'])) { if (isset($_POST['email']) && isset($_POST['password'])) { @@ -16,33 +16,33 @@ elseif (isset($_GET['login'])) { if (EmailExists($_POST['email'])) { if (Validate_Login($_POST['email'], $_POST['password'])) { $_SESSION['user'] = Get_User_Id($_POST['email']); - header('Location: ./index2.php'); + header('Location: ./'); } else { - header('Location: ./index2.php?error=loginfailed'); + header('Location: ./?error=loginfailed'); } } else { - header('Location: ./index2.php?error=emaildoesnotexist'); + header('Location: ./?error=emaildoesnotexist'); } } else { - header('Location: ./index2.php?error=emailinvalid'); + header('Location: ./?error=emailinvalid'); } } else { - header('Location: ./index2.php?error=loginemailorpasswordblank'); + header('Location: ./?error=loginemailorpasswordblank'); } } elseif (isset($_GET['createaccount'])) { if (isset($_POST['email']) && isset($_POST['password'])) { if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) && !EmailExists($_POST['email'])) { if (query("INSERT INTO users (email, password, public_name, allow_email) VALUES ('" . $_POST['email'] . "','" . crypt($_POST['password'], $_POST['email']) . "','" . htmlspecialchars($_POST['publicname'], ENT_QUOTES) . "'," . (($_POST['allowemails'] != "on") ? 0 : 1) . ")")) { - header('Location: ./index2.php?success'); + header('Location: ./?success'); } else { - header('Location: ./index2.php?error=couldnotcreate'); + header('Location: ./?error=couldnotcreate'); } } else { - header('Location: ./index2.php?error=emailcreateinvalid'); + header('Location: ./?error=emailcreateinvalid'); } } else { - header('Location: ./index2.php?error=createemailorpasswordblank'); + header('Location: ./?error=createemailorpasswordblank'); } } elseif (isset($_GET['error'])) { @@ -240,7 +240,6 @@ elseif (isset($_GET['loggedout'])) { @@ -252,7 +251,7 @@ elseif (isset($_GET['loggedout'])) { diff --git a/js/dictionaryBuilder.js b/js/dictionaryBuilder.js index 4987bdf..f1d7529 100644 --- a/js/dictionaryBuilder.js +++ b/js/dictionaryBuilder.js @@ -1,14 +1,13 @@ /* global markdown */ /* global Defiant */ -var currentVersion = 0.3; var currentUser = 0; var publicName = "Someone"; var currentDictionary = { name: "New", description: "A new dictionary.", - creatorName: publicName, + createdBy: publicName, words: [], settings: { allowDuplicates: false, @@ -17,7 +16,7 @@ var currentDictionary = { isComplete: false }, externalID: 0, - dictionaryImportVersion: currentVersion // This needs to always be last. + fileIdentifier: "Lexiconga Dictionary" }; var defaultDictionaryJSON = JSON.stringify(currentDictionary); //Saves a stringifyed default dictionary. @@ -260,7 +259,7 @@ function EditWord(index) { function SaveAndUpdateDictionary(keepFormContents, sendWords) { sendWords = (typeof sendWords !== 'undefined') ? sendWords : false; currentDictionary.words.sort(dynamicSort("name")); - SaveDictionary(sendWords); + SaveDictionary(true, sendWords); ShowDictionary(); if (!keepFormContents) { ClearForm(); @@ -533,11 +532,11 @@ function EmptyWholeDictionary() { } } -function SaveDictionary(sendWords) { +function SaveDictionary(sendToDatabase, sendWords) { localStorage.setItem('dictionary', JSON.stringify(currentDictionary)); //Always save local copy of current dictionary, but if logged in also send to database. - if (currentUser > 0) { + if (currentUser > 0 && sendToDatabase) { sendWords = (typeof sendWords !== 'undefined') ? sendWords : false; SendDictionary(sendWords); } @@ -559,6 +558,7 @@ function SendDictionary(sendWords) { var sendDictionary = new XMLHttpRequest(); sendDictionary.open('POST', "php/ajax_dictionarymanagement.php?action=" + action); + sendDictionary.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); sendDictionary.onreadystatechange = function() { if (sendDictionary.readyState == 4 && sendDictionary.status == 200) { if (sendDictionary.responseText == "updated successfully") { @@ -611,6 +611,7 @@ function DataToSend(doSendWords) { } function LoadDictionary() { + LoadLocalDictionary(); if (currentUser > 0) { //If logged in, load the dictionary from database var loadDictionary = new XMLHttpRequest(); loadDictionary.open('GET', "php/ajax_dictionarymanagement.php?action=load"); @@ -624,7 +625,10 @@ function LoadDictionary() { loadDictionary.responseText == "no info provided") { console.log(loadDictionary.responseText); } else { + console.log(loadDictionary.responseText); currentDictionary = JSON.parse(loadDictionary.responseText); + SaveDictionary(false, false); + ProcessLoad(); } return true; } else { @@ -632,16 +636,22 @@ function LoadDictionary() { } } loadDictionary.send(); - } else { //Otherwise load the local one. - if (localStorage.getItem('dictionary')) { - var tmpDictionary = JSON.parse(localStorage.getItem('dictionary')); - if (tmpDictionary.words.length > 0) { - currentDictionary = JSON.parse(localStorage.getItem('dictionary')); - } - tmpDictionary = null; - } + } else { + ProcessLoad(); } - +} + +function LoadLocalDictionary() { + if (localStorage.getItem('dictionary')) { + var tmpDictionary = JSON.parse(localStorage.getItem('dictionary')); + if (tmpDictionary.words.length > 0) { + currentDictionary = JSON.parse(localStorage.getItem('dictionary')); + } + tmpDictionary = null; + } +} + +function ProcessLoad() { HideSettingsWhenComplete(); ShowDictionary(""); @@ -690,7 +700,7 @@ function ImportDictionary() { // When it's loaded, process it reader.onloadend = function () { if (reader.result && reader.result.length) { - if (reader.result.substr(reader.result.length - 30) == '"dictionaryImportVersion":' + currentVersion + '}') { + if (reader.result.substr(reader.result.length - 40) == '"fileIdentifier":"Lexiconga Dictionary"}') { localStorage.setItem('dictionary', reader.result); document.getElementById("importFile").value = ""; LoadDictionary(); diff --git a/php/ajax_dictionarymanagement.php b/php/ajax_dictionarymanagement.php index 29755ab..34a4062 100644 --- a/php/ajax_dictionarymanagement.php +++ b/php/ajax_dictionarymanagement.php @@ -1,6 +1,8 @@ 0) { + if (num_rows($dictionary) === 1) { + while ($dict = fetch_assoc($dictionary)) { + $_SESSION['dictionary'] = $dict['id']; + $json = '{"name":"' . $dict['name'] . '",'; + $json .= '"description":"' . $dict['description'] . '",'; + $json .= '"createdBy":"' . $dict['public_name'] . '",'; + $json .= '"words":' . $dict['words'] . ','; + $json .= '"settings":{'; + $json .= '"allowDuplicates":' . (($dict['allow_duplicates'] == 1) ? 'true' : 'false') . ','; + $json .= '"caseSensitive":' . (($dict['case_sensitive'] == 1) ? 'true' : 'false') . ','; + $json .= '"partsOfSpeech":"' . $dict['parts_of_speech'] . '",'; + $json .= '"isComplete":' . (($dict['is_complete'] == 1) ? 'true' : 'false') . '},'; + $json .= '"externalID":' . $dict['id'] . ','; + $json .= '"fileIdentifier":"Lexiconga Dictionary"}'; + echo $json; + return true; + } + } else { + echo "more than 1 returned"; } } else { - echo "more than 1 returned"; + echo "no dictionaries"; } } else { echo "could not load"; @@ -80,17 +91,18 @@ function Load_Current_Dictionary() { function Save_Current_DictionaryAsNew() { if (isset($_SESSION['user'])) { + $conn = connection(); $query = "INSERT INTO `dictionaries`(`user`, `is_current`, `name`, `description`, `words`, `allow_duplicates`, `case_sensitive`, `parts_of_speech`, `is_complete`, `is_public`) "; - $query .= "VALUES (" . $_SESSION['user'] . ",1,'" . $_POST['name'] . "','" . $_POST['description'] . "','" . $_POST['words'] . "'," . $_POST['allowduplicates'] . "," . $_POST['casesensitive'] . "," . $_POST['partsofspeech'] . "," . $_POST['iscomplete'] . "," . $_POST['ispublic'] . ")"; - $update = query($query); + $query .= "VALUES (" . $_SESSION['user'] . ",1,'" . $_POST['name'] . "','" . $_POST['description'] . "','" . $_POST['words'] . "'," . $_POST['allowduplicates'] . "," . $_POST['casesensitive'] . ",'" . $_POST['partsofspeech'] . "'," . $_POST['iscomplete'] . "," . $_POST['ispublic'] . ")"; + $update = mysqli_query($conn, $query); if ($update) { - $_SESSION['dictionary'] = mysql_insert_id(connection()); + $_SESSION['dictionary'] = mysqli_insert_id($conn); $_SESSION['dictionaries'][] = $_SESSION['dictionary']; //Add new id to valid dictionaries. echo $_SESSION['dictionary']; return true; } else { - echo "could not update"; + echo "could not update:\n" . mysqli_error($conn) . "\n" . $query; } } else { echo "no info provided";