Fixed database load/save. Changed import acceptance policy/method.

This commit is contained in:
Robbie Antenesse 2015-11-04 16:43:52 -07:00
parent 10cf5a34c5
commit dd3783be5c
3 changed files with 67 additions and 46 deletions

View File

@ -8,7 +8,7 @@ $notificationMessage = "";
if (isset($_GET['logout']) && $current_user > 0) { if (isset($_GET['logout']) && $current_user > 0) {
session_destroy(); session_destroy();
header('Location: ./index2.php?loggedout'); header('Location: ./?loggedout');
} }
elseif (isset($_GET['login'])) { elseif (isset($_GET['login'])) {
if (isset($_POST['email']) && isset($_POST['password'])) { if (isset($_POST['email']) && isset($_POST['password'])) {
@ -16,33 +16,33 @@ elseif (isset($_GET['login'])) {
if (EmailExists($_POST['email'])) { if (EmailExists($_POST['email'])) {
if (Validate_Login($_POST['email'], $_POST['password'])) { if (Validate_Login($_POST['email'], $_POST['password'])) {
$_SESSION['user'] = Get_User_Id($_POST['email']); $_SESSION['user'] = Get_User_Id($_POST['email']);
header('Location: ./index2.php'); header('Location: ./');
} else { } else {
header('Location: ./index2.php?error=loginfailed'); header('Location: ./?error=loginfailed');
} }
} else { } else {
header('Location: ./index2.php?error=emaildoesnotexist'); header('Location: ./?error=emaildoesnotexist');
} }
} else { } else {
header('Location: ./index2.php?error=emailinvalid'); header('Location: ./?error=emailinvalid');
} }
} else { } else {
header('Location: ./index2.php?error=loginemailorpasswordblank'); header('Location: ./?error=loginemailorpasswordblank');
} }
} }
elseif (isset($_GET['createaccount'])) { elseif (isset($_GET['createaccount'])) {
if (isset($_POST['email']) && isset($_POST['password'])) { if (isset($_POST['email']) && isset($_POST['password'])) {
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) && !EmailExists($_POST['email'])) { 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) . ")")) { 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 { } else {
header('Location: ./index2.php?error=couldnotcreate'); header('Location: ./?error=couldnotcreate');
} }
} else { } else {
header('Location: ./index2.php?error=emailcreateinvalid'); header('Location: ./?error=emailcreateinvalid');
} }
} else { } else {
header('Location: ./index2.php?error=createemailorpasswordblank'); header('Location: ./?error=createemailorpasswordblank');
} }
} }
elseif (isset($_GET['error'])) { elseif (isset($_GET['error'])) {
@ -240,7 +240,6 @@ elseif (isset($_GET['loggedout'])) {
</div> </div>
</contents> </contents>
<footer> <footer>
<?php if (isset($_GET['login'])) echo 'cool '; ?>
Dictionary Builder only guaranteed to work with most up-to-date HTML5 browsers. <span class="clickable" onclick="ShowInfo('terms')" style="font-size:12px;">Terms</span> <span class="clickable" onclick="ShowInfo('privacy')" style="font-size:12px;">Privacy</span> Dictionary Builder only guaranteed to work with most up-to-date HTML5 browsers. <span class="clickable" onclick="ShowInfo('terms')" style="font-size:12px;">Terms</span> <span class="clickable" onclick="ShowInfo('privacy')" style="font-size:12px;">Privacy</span>
</footer> </footer>
@ -252,7 +251,7 @@ elseif (isset($_GET['loggedout'])) {
<script src="js/dictionaryBuilder.js"></script> <script src="js/dictionaryBuilder.js"></script>
<script> <script>
currentUser = <?php echo $current_user; ?>; currentUser = <?php echo $current_user; ?>;
publicName = <?php echo Get_Public_Name($current_user); ?>; publicName = "<?php echo Get_Public_Name($current_user); ?>";
</script> </script>
<?php //include_once("php/google/analytics.php"); ?> <?php //include_once("php/google/analytics.php"); ?>
</body> </body>

View File

@ -1,14 +1,13 @@
/* global markdown */ /* global markdown */
/* global Defiant */ /* global Defiant */
var currentVersion = 0.3;
var currentUser = 0; var currentUser = 0;
var publicName = "Someone"; var publicName = "Someone";
var currentDictionary = { var currentDictionary = {
name: "New", name: "New",
description: "A new dictionary.", description: "A new dictionary.",
creatorName: publicName, createdBy: publicName,
words: [], words: [],
settings: { settings: {
allowDuplicates: false, allowDuplicates: false,
@ -17,7 +16,7 @@ var currentDictionary = {
isComplete: false isComplete: false
}, },
externalID: 0, externalID: 0,
dictionaryImportVersion: currentVersion // This needs to always be last. fileIdentifier: "Lexiconga Dictionary"
}; };
var defaultDictionaryJSON = JSON.stringify(currentDictionary); //Saves a stringifyed default dictionary. var defaultDictionaryJSON = JSON.stringify(currentDictionary); //Saves a stringifyed default dictionary.
@ -260,7 +259,7 @@ function EditWord(index) {
function SaveAndUpdateDictionary(keepFormContents, sendWords) { function SaveAndUpdateDictionary(keepFormContents, sendWords) {
sendWords = (typeof sendWords !== 'undefined') ? sendWords : false; sendWords = (typeof sendWords !== 'undefined') ? sendWords : false;
currentDictionary.words.sort(dynamicSort("name")); currentDictionary.words.sort(dynamicSort("name"));
SaveDictionary(sendWords); SaveDictionary(true, sendWords);
ShowDictionary(); ShowDictionary();
if (!keepFormContents) { if (!keepFormContents) {
ClearForm(); ClearForm();
@ -533,11 +532,11 @@ function EmptyWholeDictionary() {
} }
} }
function SaveDictionary(sendWords) { function SaveDictionary(sendToDatabase, sendWords) {
localStorage.setItem('dictionary', JSON.stringify(currentDictionary)); localStorage.setItem('dictionary', JSON.stringify(currentDictionary));
//Always save local copy of current dictionary, but if logged in also send to database. //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; sendWords = (typeof sendWords !== 'undefined') ? sendWords : false;
SendDictionary(sendWords); SendDictionary(sendWords);
} }
@ -559,6 +558,7 @@ function SendDictionary(sendWords) {
var sendDictionary = new XMLHttpRequest(); var sendDictionary = new XMLHttpRequest();
sendDictionary.open('POST', "php/ajax_dictionarymanagement.php?action=" + action); sendDictionary.open('POST', "php/ajax_dictionarymanagement.php?action=" + action);
sendDictionary.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
sendDictionary.onreadystatechange = function() { sendDictionary.onreadystatechange = function() {
if (sendDictionary.readyState == 4 && sendDictionary.status == 200) { if (sendDictionary.readyState == 4 && sendDictionary.status == 200) {
if (sendDictionary.responseText == "updated successfully") { if (sendDictionary.responseText == "updated successfully") {
@ -611,6 +611,7 @@ function DataToSend(doSendWords) {
} }
function LoadDictionary() { function LoadDictionary() {
LoadLocalDictionary();
if (currentUser > 0) { //If logged in, load the dictionary from database if (currentUser > 0) { //If logged in, load the dictionary from database
var loadDictionary = new XMLHttpRequest(); var loadDictionary = new XMLHttpRequest();
loadDictionary.open('GET', "php/ajax_dictionarymanagement.php?action=load"); loadDictionary.open('GET', "php/ajax_dictionarymanagement.php?action=load");
@ -624,7 +625,10 @@ function LoadDictionary() {
loadDictionary.responseText == "no info provided") { loadDictionary.responseText == "no info provided") {
console.log(loadDictionary.responseText); console.log(loadDictionary.responseText);
} else { } else {
console.log(loadDictionary.responseText);
currentDictionary = JSON.parse(loadDictionary.responseText); currentDictionary = JSON.parse(loadDictionary.responseText);
SaveDictionary(false, false);
ProcessLoad();
} }
return true; return true;
} else { } else {
@ -632,7 +636,12 @@ function LoadDictionary() {
} }
} }
loadDictionary.send(); loadDictionary.send();
} else { //Otherwise load the local one. } else {
ProcessLoad();
}
}
function LoadLocalDictionary() {
if (localStorage.getItem('dictionary')) { if (localStorage.getItem('dictionary')) {
var tmpDictionary = JSON.parse(localStorage.getItem('dictionary')); var tmpDictionary = JSON.parse(localStorage.getItem('dictionary'));
if (tmpDictionary.words.length > 0) { if (tmpDictionary.words.length > 0) {
@ -640,8 +649,9 @@ function LoadDictionary() {
} }
tmpDictionary = null; tmpDictionary = null;
} }
} }
function ProcessLoad() {
HideSettingsWhenComplete(); HideSettingsWhenComplete();
ShowDictionary(""); ShowDictionary("");
@ -690,7 +700,7 @@ function ImportDictionary() {
// When it's loaded, process it // When it's loaded, process it
reader.onloadend = function () { reader.onloadend = function () {
if (reader.result && reader.result.length) { 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); localStorage.setItem('dictionary', reader.result);
document.getElementById("importFile").value = ""; document.getElementById("importFile").value = "";
LoadDictionary(); LoadDictionary();

View File

@ -1,6 +1,8 @@
<?php <?php
require_once("../required.php"); require_once("../required.php");
session_start();
if ($_GET['action'] == 'getall') { if ($_GET['action'] == 'getall') {
Get_Dictionaries(); Get_Dictionaries();
} }
@ -49,26 +51,35 @@ function Get_Dictionaries() {
function Load_Current_Dictionary() { function Load_Current_Dictionary() {
if (isset($_SESSION['user'])) { if (isset($_SESSION['user'])) {
$query = "SELECT * FROM `dictionaries` WHERE `is_current`=1 AND `user`=" . $_SESSION['user'] . ";"; $query = "SELECT `d`.`id`, `d`.`name`, `d`.`description`, `u`.`public_name`, `d`.`words`, `d`.`allow_duplicates`, `d`.`case_sensitive`, `d`.`parts_of_speech`, `d`.`is_complete` ";
$query .= "FROM `dictionaries` AS `d` LEFT JOIN `users` AS `u` ON `user`=`u`.`id` WHERE `is_current`=1 AND `user`=" . $_SESSION['user'] . ";";
$dictionary = query($query); $dictionary = query($query);
if ($dictionary) { if ($dictionary) {
if (num_rows($dictionary) > 0) {
if (num_rows($dictionary) === 1) { if (num_rows($dictionary) === 1) {
while ($dict = fetch_assoc($dictionary)) { while ($dict = fetch_assoc($dictionary)) {
$_SESSION['dictionary'] = $dict['id']; $_SESSION['dictionary'] = $dict['id'];
$json = '{"name":"' . $dict['name'] . '","description":"' . $dict['description'] . '","words":"' . $dict['words'] . '",'; $json = '{"name":"' . $dict['name'] . '",';
$json .= '"settings":{"allowDuplicates":' . ($dict['allow_duplicates'] == 1) ? "true" : "false" . ','; $json .= '"description":"' . $dict['description'] . '",';
$json .= '"caseSensitive":' . ($dict['case_sensitive'] == 1) ? "true" : "false" . ','; $json .= '"createdBy":"' . $dict['public_name'] . '",';
$json .= '"partsOfSpeech":"' . $dict['parts_of_speech'] . ','; $json .= '"words":' . $dict['words'] . ',';
$json .= '"isComplete":' . ($dict['is_complete'] == 1) ? "true" : "false" . '},'; $json .= '"settings":{';
$json .= '"externalID":"' . $dict['id'] . ','; $json .= '"allowDuplicates":' . (($dict['allow_duplicates'] == 1) ? 'true' : 'false') . ',';
$json .= '"dictionaryImportVersion":' . $dict['import_version'] . '}'; $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; echo $json;
return true; return true;
} }
} else { } else {
echo "more than 1 returned"; echo "more than 1 returned";
} }
} else {
echo "no dictionaries";
}
} else { } else {
echo "could not load"; echo "could not load";
} }
@ -80,17 +91,18 @@ function Load_Current_Dictionary() {
function Save_Current_DictionaryAsNew() { function Save_Current_DictionaryAsNew() {
if (isset($_SESSION['user'])) { 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 = "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'] . ")"; $query .= "VALUES (" . $_SESSION['user'] . ",1,'" . $_POST['name'] . "','" . $_POST['description'] . "','" . $_POST['words'] . "'," . $_POST['allowduplicates'] . "," . $_POST['casesensitive'] . ",'" . $_POST['partsofspeech'] . "'," . $_POST['iscomplete'] . "," . $_POST['ispublic'] . ")";
$update = query($query); $update = mysqli_query($conn, $query);
if ($update) { if ($update) {
$_SESSION['dictionary'] = mysql_insert_id(connection()); $_SESSION['dictionary'] = mysqli_insert_id($conn);
$_SESSION['dictionaries'][] = $_SESSION['dictionary']; //Add new id to valid dictionaries. $_SESSION['dictionaries'][] = $_SESSION['dictionary']; //Add new id to valid dictionaries.
echo $_SESSION['dictionary']; echo $_SESSION['dictionary'];
return true; return true;
} else { } else {
echo "could not update"; echo "could not update:\n" . mysqli_error($conn) . "\n" . $query;
} }
} else { } else {
echo "no info provided"; echo "no info provided";