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) {
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'])) {
</div>
</contents>
<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>
</footer>
@ -252,7 +251,7 @@ elseif (isset($_GET['loggedout'])) {
<script src="js/dictionaryBuilder.js"></script>
<script>
currentUser = <?php echo $current_user; ?>;
publicName = <?php echo Get_Public_Name($current_user); ?>;
publicName = "<?php echo Get_Public_Name($current_user); ?>";
</script>
<?php //include_once("php/google/analytics.php"); ?>
</body>

View File

@ -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,7 +636,12 @@ function LoadDictionary() {
}
}
loadDictionary.send();
} else { //Otherwise load the local one.
} else {
ProcessLoad();
}
}
function LoadLocalDictionary() {
if (localStorage.getItem('dictionary')) {
var tmpDictionary = JSON.parse(localStorage.getItem('dictionary'));
if (tmpDictionary.words.length > 0) {
@ -642,6 +651,7 @@ function LoadDictionary() {
}
}
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();

View File

@ -1,6 +1,8 @@
<?php
require_once("../required.php");
session_start();
if ($_GET['action'] == 'getall') {
Get_Dictionaries();
}
@ -49,26 +51,35 @@ function Get_Dictionaries() {
function Load_Current_Dictionary() {
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);
if ($dictionary) {
if (num_rows($dictionary) > 0) {
if (num_rows($dictionary) === 1) {
while ($dict = fetch_assoc($dictionary)) {
$_SESSION['dictionary'] = $dict['id'];
$json = '{"name":"' . $dict['name'] . '","description":"' . $dict['description'] . '","words":"' . $dict['words'] . '",';
$json .= '"settings":{"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 .= '"dictionaryImportVersion":' . $dict['import_version'] . '}';
$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 "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";