Skip to content

Commit

Permalink
Modernize/simplify settings.js request sending
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisrosset authored and reedy committed Aug 17, 2023
1 parent 46587d2 commit a9fec89
Showing 1 changed file with 74 additions and 104 deletions.
178 changes: 74 additions & 104 deletions js/settings.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*
* Create new user accounts and modify existing ones
*/

const URL_SETTINGS = "/php/settings.php";

const PRIVACY_LIST = ["N", "Y", "O"];
Expand Down Expand Up @@ -34,115 +33,86 @@ window.onload = function init() {
}
};

function xmlhttpPost(strURL, type) {
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
function sendSettingsRequest(type) {
const params = new URLSearchParams();
let r = 0;
var form = document.forms["signupform"],
privacy,
editor,
units;

for (r = 0; r < signupform.privacy.length; r++) {
if (signupform.privacy[r].checked) {
privacy = signupform.privacy[r].value;
}
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
for (r = 0; r < signupform.editor.length; r++) {
if (signupform.editor[r].checked) {
editor = signupform.editor[r].value;
}
}
self.xmlHttpReq.open("POST", strURL, true);
self.xmlHttpReq.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
self.xmlHttpReq.onreadystatechange = function () {
if (self.xmlHttpReq.readyState == 4 && strURL == URL_SETTINGS) {
signup(self.xmlHttpReq.responseText);
for (r = 0; r < signupform.units.length; r++) {
if (signupform.units[r].checked) {
units = signupform.units[r].value;
}
};
var query = "";
if (strURL == URL_SETTINGS) {
var form = document.forms["signupform"],
privacy,
editor,
units;
}

params.set("type", type);
params.set("pw", MD5(form.pw1.value + form.username.value.toLowerCase()));
params.set("email", form.email.value);
params.set("privacy", privacy);
params.set("editor", editor);
params.set("units", units);
params.set("locale", form.locale.value);

for (var r = 0; r < signupform.privacy.length; r++) {
if (signupform.privacy[r].checked) {
privacy = signupform.privacy[r].value;
switch (type) {
case "NEW":
params.set("name", form.username.value);
document.getElementById("miniresultbox").innerHTML =
"<I>" + gt.gettext("Creating account...") + "</I>";
break;

case "EDIT":
var startpane;
for (r = 0; r < signupform.startpane.length; r++) {
if (signupform.startpane[r].checked) {
startpane = signupform.startpane[r].value;
}
}
}
for (r = 0; r < signupform.editor.length; r++) {
if (signupform.editor[r].checked) {
editor = signupform.editor[r].value;
if (form.oldpw.value != "") {
params.set(
"oldpw",
MD5(form.oldpw.value + form.username.value.toLowerCase())
);
// Legacy password for case-sensitive days of yore
params.set("oldlpw", MD5(form.oldpw.value + form.username.value));
}
}
for (r = 0; r < signupform.units.length; r++) {
if (signupform.units[r].checked) {
units = signupform.units[r].value;
if (form.guestpw.value != "") {
params.set(
"guestpw",
MD5(form.guestpw.value + form.username.value.toLowerCase())
);
}
}
query =
"type=" +
type +
"&" +
"pw=" +
encodeURIComponent(
MD5(form.pw1.value + form.username.value.toLowerCase())
) +
"&" +
"email=" +
encodeURIComponent(form.email.value) +
"&" +
"privacy=" +
encodeURIComponent(privacy) +
"&" +
"editor=" +
encodeURIComponent(editor) +
"&" +
"units=" +
encodeURIComponent(units) +
"&" +
"locale=" +
encodeURIComponent(form.locale.value);
switch (type) {
case "NEW":
query += "&name=" + encodeURIComponent(form.username.value);
document.getElementById("miniresultbox").innerHTML =
"<I>" + gt.gettext("Creating account...") + "</I>";
break;
params.set("startpane", startpane);
document.getElementById("miniresultbox").innerHTML =
"<I>" + gt.gettext("Saving changes...") + "</I>";
break;

case "EDIT":
var startpane;
for (r = 0; r < signupform.startpane.length; r++) {
if (signupform.startpane[r].checked) {
startpane = signupform.startpane[r].value;
}
}
if (form.oldpw.value != "") {
query +=
"&oldpw=" +
encodeURIComponent(
MD5(form.oldpw.value + form.username.value.toLowerCase())
);
// Legacy password for case-sensitive days of yore
query +=
"&oldlpw=" +
encodeURIComponent(MD5(form.oldpw.value + form.username.value));
}
if (form.guestpw.value != "") {
query +=
"&guestpw=" +
encodeURIComponent(
MD5(form.guestpw.value + form.username.value.toLowerCase())
);
}
query += "&startpane=" + encodeURIComponent(startpane);
document.getElementById("miniresultbox").innerHTML =
"<I>" + gt.gettext("Saving changes...") + "</I>";
break;

case "RESET":
case "LOAD":
// do nothing
break;
}
case "RESET":
case "LOAD":
// do nothing
break;
}
self.xmlHttpReq.send(query);

fetch(URL_SETTINGS, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: params,
})
.then((response) => response.text())
.then(handleResponse);
}

/**
Expand Down Expand Up @@ -221,14 +191,14 @@ function validate(type) {

document.getElementById("miniresultbox").innerHTML =
"<i>" + gt.gettext("Processing...") + "</i>";
xmlhttpPost(URL_SETTINGS, type);
sendSettingsRequest(type);
}

/**
* Check if user creation succeeded
* Check if server processing (user creation, edit, etc.) succeeded
* @param str {string}
*/
function signup(str) {
function handleResponse(str) {
var code = str.split(";")[0],
message = str.split(";")[1];

Expand Down

0 comments on commit a9fec89

Please sign in to comment.