diff --git a/api.js b/api.js index 9d3f9a3b..f1c5437c 100644 --- a/api.js +++ b/api.js @@ -28,6 +28,8 @@ var jsonHandlers = { "changepass" : handlePasswordChange, "resetpass" : handlePasswordReset, "recoverpw" : handlePasswordRecover, + "setprofile" : handleProfileChange, + "getprofile" : handleProfileGet, "setemail" : handleEmailChange, "globalbans" : handleGlobalBans, "admreports" : handleAdmReports, @@ -236,8 +238,9 @@ function handlePasswordReset(params, req, res) { var email = unescape(params.email || ""); var ip = req.socket.address().address; + var hash = false; try { - Database.generatePasswordReset(ip, name, email); + hash = Database.generatePasswordReset(ip, name, email); } catch(e) { sendJSON(res, { @@ -247,6 +250,7 @@ function handlePasswordReset(params, req, res) { return; } + sendJSON(res, { success: true }); @@ -275,6 +279,52 @@ function handlePasswordRecover(params, req, res) { } +function handleProfileGet(params, req, res) { + var name = params.name || ""; + + try { + var prof = Database.getProfile(name); + sendJSON(res, { + success: true, + profile_image: prof.profile_image, + profile_text: prof.profile_text + }); + } + catch(e) { + sendJSON(res, { + success: false, + error: e + }); + } +} + +function handleProfileChange(params, req, res) { + var name = params.name || ""; + var pw = params.pw || ""; + var session = params.session || ""; + var img = unescape(params.profile_image || ""); + var text = unescape(params.profile_text || ""); + + var row = Auth.login(name, pw, session); + if(!row) { + sendJSON(res, { + success: false, + error: "Invalid login" + }); + return; + } + + var result = Database.setProfile(name, { + image: img, + text: text + }); + + sendJSON(res, { + success: result, + error: result ? "" : "Internal error. Contact an administrator" + }); +} + function handleEmailChange(params, req, res) { var name = params.name || ""; var pw = params.pw || ""; diff --git a/database.js b/database.js index fc4454d7..0929ae16 100644 --- a/database.js +++ b/database.js @@ -558,6 +558,34 @@ function channelUnbanName(chan, name) { /* REGION Users */ +function getProfile(name) { + var db = getConnection(); + if(!db) { + return false; + } + + var query = createQuery( + "SELECT profile_image,profile_text FROM registrations WHERE uname=?", + [name] + ); + + var results = db.querySync(query); + if(!results) { + Logger.errlog.log("! Failed to retrieve user profile"); + throw "Database failure. Contact an administrator."; + } + + var rows = results.fetchAllSync(); + if(rows.length == 0) { + throw "User not found"; + } + + return { + profile_image: rows[0].profile_image, + profile_text: rows[0].profile_text + }; +} + function setProfile(name, data) { var db = getConnection(); if(!db) { @@ -637,7 +665,7 @@ function generatePasswordReset(ip, name, email) { return false; } - return true; + return hash; } function recoverPassword(hash) { @@ -723,6 +751,7 @@ exports.channelBan = channelBan; exports.channelUnbanIP = channelUnbanIP; exports.channelUnbanName = channelUnbanName; exports.setProfile = setProfile; +exports.getProfile = getProfile; exports.setUserEmail = setUserEmail; exports.generatePasswordReset = generatePasswordReset; exports.recoverPassword = recoverPassword; diff --git a/www/account.html b/www/account.html index fcee1ffa..05fc10e3 100644 --- a/www/account.html +++ b/www/account.html @@ -182,6 +182,28 @@ +
diff --git a/www/assets/js/account.js b/www/assets/js/account.js index 1148b6d1..74f55385 100644 --- a/www/assets/js/account.js +++ b/www/assets/js/account.js @@ -42,6 +42,22 @@ $("#register").click(makeTabCallback("#register", "#registerpane")); $("#pwchange").click(makeTabCallback("#pwchange", "#changepwpane")); $("#pwreset").click(makeTabCallback("#pwreset", "#pwresetpane")); $("#email").click(makeTabCallback("#email", "#changeemailpane")); +$("#profile").click(makeTabCallback("#profile", "#profilepane")); +$("#profile").click(function() { + if(uname != "") { + $.getJSON(api + "getprofile?name=" + uname + "&callback=?", function(data) { + if(data.success) { + $("#profiletext").val(data.profile_text); + $("#profileimg").val(data.profile_image); + } + else { + $("
").addClass("alert alert-error") + .text("Failed to retrieve profile: " + data.error) + .insertBefore($("#profilepane form")); + } + }); + } +}); $("#registerbtn").click(function() { $("#registerpane").find(".alert-error").remove(); @@ -276,6 +292,35 @@ $("#rpbtn").click(function() { }); +$("#profilesave").click(function() { + $("#profilepane").find(".alert-error").remove(); + $("#profilepane").find(".alert-success").remove(); + var img = $("#profileimg").val(); + img = escape(img).replace(/\//g, "%2F") + .replace(/&/g, "%26") + .replace(/=/g, "%3D") + .replace(/\?/g, "%3F"); + var url = api + "setprofile?" + [ + "name=" + uname, + "session=" + session, + "profile_image=" + img, + "profile_text=" + escape($("#profiletext").val()) + ].join("&") + "&callback=?"; + + $.getJSON(url, function(data) { + if(data.success) { + $("
").addClass("alert alert-success") + .text("Profile updated.") + .insertBefore($("#profilepane form")); + } + else { + $("
").addClass("alert alert-error") + .text(data.error) + .insertBefore($("#profilepane form")); + } + }); +}); + $("#login").click(function() { if(!loggedin) { makeTabCallback("#login", "#loginpane")();