2013-06-04 22:26:17 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
Copyright (c) 2013 Calvin Montgomery
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-06-19 21:54:27 +00:00
|
|
|
var uname = readCookie("cytube_uname") || "";
|
|
|
|
var session = readCookie("cytube_session") || "";
|
2013-05-29 15:49:09 +00:00
|
|
|
var api = WEB_URL + "/api/json/";
|
|
|
|
var loggedin = false;
|
|
|
|
|
|
|
|
if(uname && session) {
|
2013-07-27 14:11:31 +00:00
|
|
|
var loginstr = "name=" + encodeURIComponent(uname)
|
|
|
|
+ "&session=" + session;
|
2013-05-29 15:49:09 +00:00
|
|
|
var url = api + "login?" + loginstr + "&callback=?";
|
|
|
|
$.getJSON(url, function(data) {
|
|
|
|
if(data.success) {
|
|
|
|
onLogin();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onLogin() {
|
|
|
|
$("#cpwusername").val(uname);
|
|
|
|
$("#ceusername").val(uname);
|
|
|
|
$("#accountnav li")[0].innerHTML = "Logged in as " + uname;
|
|
|
|
$("#register").hide();
|
|
|
|
loggedin = true;
|
|
|
|
$("#login").text("Logout");
|
2013-06-19 22:11:11 +00:00
|
|
|
createCookie("cytube_uname", uname, 7);
|
|
|
|
createCookie("cytube_session", session, 7);
|
2013-05-29 15:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeTabCallback(tabid, paneid) {
|
|
|
|
return function() {
|
|
|
|
$("#accountnav li").each(function() {
|
|
|
|
$(this).removeClass("active");
|
|
|
|
});
|
|
|
|
|
|
|
|
$(tabid).parent().addClass("active");
|
|
|
|
$(".span7").each(function() {
|
|
|
|
$(this).css("display", "none");
|
|
|
|
});
|
|
|
|
$(paneid).css("display", "");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
$("#register").click(makeTabCallback("#register", "#registerpane"));
|
|
|
|
$("#pwchange").click(makeTabCallback("#pwchange", "#changepwpane"));
|
2013-05-29 18:19:43 +00:00
|
|
|
$("#pwreset").click(makeTabCallback("#pwreset", "#pwresetpane"));
|
2013-05-29 15:49:09 +00:00
|
|
|
$("#email").click(makeTabCallback("#email", "#changeemailpane"));
|
2013-05-30 01:04:22 +00:00
|
|
|
$("#profile").click(makeTabCallback("#profile", "#profilepane"));
|
|
|
|
$("#profile").click(function() {
|
|
|
|
if(uname != "") {
|
2013-07-27 14:11:31 +00:00
|
|
|
$.getJSON(api + "getprofile?name=" + encodeURIComponent(uname) + "&callback=?", function(data) {
|
2013-05-30 01:04:22 +00:00
|
|
|
if(data.success) {
|
|
|
|
$("#profiletext").val(data.profile_text);
|
|
|
|
$("#profileimg").val(data.profile_image);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text("Failed to retrieve profile: " + data.error)
|
|
|
|
.insertBefore($("#profilepane form"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-08-06 15:20:15 +00:00
|
|
|
$("#channels").click(makeTabCallback("#channels", "#channelspane"));
|
|
|
|
$("#channels").click(function () {
|
|
|
|
if(!loggedin) {
|
|
|
|
var error = $("<div/>").addClass("alert alert-error")
|
|
|
|
.text("You must be logged in to view this page")
|
|
|
|
.insertBefore($("#channellist"));
|
|
|
|
$("<button/>").addClass("close pull-right").click(function () {
|
|
|
|
error.remove();
|
|
|
|
}).html("×").prependTo(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.getJSON(api + "listuserchannels?name=" + encodeURIComponent(uname) +
|
|
|
|
"&session=" + session + "&callback=?", function(data) {
|
|
|
|
$("#channellist tbody").remove();
|
|
|
|
data.channels.forEach(function (chan) {
|
|
|
|
var tr = $("<tr/>").appendTo($("#channellist"));
|
|
|
|
var td = $("<td/>").appendTo(tr);
|
|
|
|
$("<a/>").attr("href", "./r/" + chan.name)
|
|
|
|
.attr("target", "_blank")
|
|
|
|
.text(chan.name)
|
|
|
|
.appendTo(td);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-05-29 15:49:09 +00:00
|
|
|
|
|
|
|
$("#registerbtn").click(function() {
|
|
|
|
$("#registerpane").find(".alert-error").remove();
|
|
|
|
$("#registerpane").find(".error").removeClass("error");
|
2013-05-29 18:19:43 +00:00
|
|
|
var name = $("#regusername").val();
|
2013-05-29 15:49:09 +00:00
|
|
|
var pw = $("#regpw").val();
|
|
|
|
var pwc = $("#regpwconfirm").val();
|
|
|
|
|
|
|
|
var err = false;
|
2013-05-29 18:19:43 +00:00
|
|
|
if(!name.match(/^[a-z0-9_]{1,20}$/i)) {
|
2013-05-29 15:49:09 +00:00
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text("Usernames must be 1-20 characters long and contain only a-z, 0-9, and underscores")
|
|
|
|
.insertAfter($("#regusername").parent().parent());
|
|
|
|
err = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pw == "") {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text("Password must not be blank")
|
|
|
|
.insertAfter($("#regpw").parent().parent());
|
|
|
|
$("#regpw").parent().parent().addClass("error");
|
|
|
|
err = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pw != pwc) {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text("Passwords do not match")
|
|
|
|
.insertAfter($("#regpwconfirm").parent().parent());
|
|
|
|
$("#regpwconfirm").parent().parent().addClass("error");
|
|
|
|
err = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Input valid, try registering
|
2013-05-29 18:19:43 +00:00
|
|
|
var url = api + "register?" + [
|
2013-07-27 14:11:31 +00:00
|
|
|
"name=" + encodeURIComponent(name),
|
|
|
|
"pw=" + encodeURIComponent(pw)
|
2013-05-29 18:19:43 +00:00
|
|
|
].join("&") + "&callback=?";
|
|
|
|
|
|
|
|
$.getJSON(url, function(data) {
|
|
|
|
if(data.success) {
|
|
|
|
uname = name;
|
|
|
|
session = data.session;
|
|
|
|
onLogin();
|
|
|
|
$("<div/>").addClass("alert alert-success")
|
|
|
|
.text("Registration successful")
|
|
|
|
.insertBefore($("#registerpane form"));
|
|
|
|
$("#regpw").val("");
|
|
|
|
$("#regusername").val("");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text(data.error)
|
|
|
|
.insertBefore($("#registerpane form"));
|
|
|
|
}
|
|
|
|
});
|
2013-05-29 15:49:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#loginbtn").click(function() {
|
|
|
|
$("#loginpane").find(".alert-error").remove();
|
|
|
|
$("#loginpane").find(".alert-success").remove();
|
|
|
|
|
|
|
|
if($("#loginpw").val() == "") {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text("Please provide a password")
|
|
|
|
.insertAfter($("#loginpw").parent().parent());
|
|
|
|
$("#loginpw").parent().parent().addClass("error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uname = $("#loginusername").val();
|
2013-07-27 14:11:31 +00:00
|
|
|
var loginstr = "name=" + encodeURIComponent(uname)
|
|
|
|
+ "&pw=" + encodeURIComponent($("#loginpw").val());
|
2013-05-29 15:49:09 +00:00
|
|
|
var url = api + "login?" + loginstr + "&callback=?";
|
|
|
|
$.getJSON(url, function(data) {
|
|
|
|
if(data.success) {
|
|
|
|
session = data.session;
|
|
|
|
onLogin();
|
|
|
|
$("<div/>").addClass("alert alert-success")
|
|
|
|
.text("Login successful")
|
|
|
|
.insertBefore($("#loginpane form"));
|
|
|
|
$("#loginpw").val("");
|
|
|
|
$("#loginusername").val("");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text(data.error)
|
|
|
|
.insertBefore($("#loginpane form"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#cpwbtn").click(function() {
|
|
|
|
$("#changepwpane").find(".alert-error").remove();
|
|
|
|
$("#changepwpane").find(".alert-success").remove();
|
|
|
|
$("#changepwpane").find(".error").removeClass("error");
|
|
|
|
var name = $("#cpwusername").val();
|
|
|
|
var oldpw = $("#cpwoldpw").val();
|
|
|
|
var newpw = $("#cpwnewpw").val();
|
|
|
|
var newpwc = $("#cpwconfirm").val();
|
|
|
|
|
|
|
|
var err = false;
|
|
|
|
if(oldpw == "") {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text("Password must not be empty")
|
|
|
|
.insertAfter($("#cpwoldpw").parent().parent());
|
|
|
|
$("#cpwoldpw").parent().parent().addClass("error");
|
|
|
|
err = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(newpw == "") {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text("Password must not be empty")
|
|
|
|
.insertAfter($("#cpwnewpw").parent().parent());
|
|
|
|
$("#cpwnewpw").parent().parent().addClass("error");
|
|
|
|
err = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(newpw != newpwc) {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text("Passwords do not match")
|
|
|
|
.insertAfter($("#cpwconfirm").parent().parent());
|
|
|
|
$("#cpwconfirm").parent().parent().addClass("error");
|
|
|
|
err = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Input valid, try changing password
|
|
|
|
var url = api + "changepass?" + [
|
2013-07-27 14:11:31 +00:00
|
|
|
"name=" + encodeURIComponent(name),
|
|
|
|
"oldpw=" + encodeURIComponent(oldpw),
|
|
|
|
"newpw=" + encodeURIComponent(newpw)
|
2013-05-29 15:49:09 +00:00
|
|
|
].join("&") + "&callback=?";
|
|
|
|
$.getJSON(url, function(data) {
|
|
|
|
if(data.success) {
|
|
|
|
$("<div/>").addClass("alert alert-success")
|
|
|
|
.text("Password changed.")
|
|
|
|
.insertBefore($("#changepwpane form"));
|
|
|
|
uname = name;
|
|
|
|
session = data.session;
|
|
|
|
onLogin();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text(data.error)
|
|
|
|
.insertBefore($("#changepwpane form"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#cebtn").click(function() {
|
|
|
|
$("#changeemailpane").find(".alert-error").remove();
|
|
|
|
$("#changeemailpane").find(".alert-success").remove();
|
|
|
|
var name = $("#ceusername").val();
|
|
|
|
var pw = $("#cepw").val();
|
|
|
|
var email = $("#ceemail").val();
|
|
|
|
if(pw == "") {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text("Please provide a password")
|
|
|
|
.insertAfter($("#cepw").parent().parent());
|
|
|
|
$("#cepw").parent().parent().addClass("error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!email.match(/^[a-z0-9_\.]+@[a-z0-9_\.]+[a-z]+$/)) {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text("Invalid email")
|
|
|
|
.insertAfter($("#ceemail").parent().parent());
|
|
|
|
$("#ceemail").parent().parent().addClass("error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(email.match(/.*@(localhost|127\.0\.0\.1)/i)) {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text("Nice try, but no.")
|
|
|
|
.insertAfter($("#ceemail").parent().parent());
|
|
|
|
$("#ceemail").parent().parent().addClass("error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var url = api + "setemail?" + [
|
2013-07-27 14:11:31 +00:00
|
|
|
"name=" + encodeURIComponent(name),
|
|
|
|
"pw=" + encodeURIComponent(pw),
|
|
|
|
"email=" + encodeURIComponent(email)
|
2013-05-29 15:49:09 +00:00
|
|
|
].join("&") + "&callback=?";
|
|
|
|
$.getJSON(url, function(data) {
|
|
|
|
if(data.success) {
|
|
|
|
$("<div/>").addClass("alert alert-success")
|
|
|
|
.text("Email updated")
|
|
|
|
.insertBefore($("#changeemailpane form"));
|
|
|
|
uname = name;
|
|
|
|
session = data.session;
|
|
|
|
onLogin();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text(data.error)
|
|
|
|
.insertBefore($("#changeemailpane form"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-05-29 18:19:43 +00:00
|
|
|
$("#rpbtn").click(function() {
|
2013-05-30 18:07:29 +00:00
|
|
|
$("#rpbtn").text("Sending...");
|
2013-05-29 18:19:43 +00:00
|
|
|
$("#pwresetpane").find(".alert-error").remove();
|
|
|
|
$("#pwresetpane").find(".alert-success").remove();
|
|
|
|
var name = $("#rpusername").val();
|
|
|
|
var email = $("#rpemail").val();
|
|
|
|
|
|
|
|
var url = api + "resetpass?" + [
|
2013-07-27 14:11:31 +00:00
|
|
|
"name=" + encodeURIComponent(name),
|
|
|
|
"email=" + encodeURIComponent(email)
|
2013-05-29 18:19:43 +00:00
|
|
|
].join("&") + "&callback=?";
|
|
|
|
$.getJSON(url, function(data) {
|
2013-05-30 18:07:29 +00:00
|
|
|
$("#rpbtn").text("Send Reset");
|
2013-05-29 18:19:43 +00:00
|
|
|
if(data.success) {
|
|
|
|
$("<div/>").addClass("alert alert-success")
|
|
|
|
.text("Password reset link issued. Check your email.")
|
|
|
|
.insertBefore($("#pwresetpane form"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text(data.error)
|
|
|
|
.insertBefore($("#pwresetpane form"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-05-30 01:04:22 +00:00
|
|
|
$("#profilesave").click(function() {
|
|
|
|
$("#profilepane").find(".alert-error").remove();
|
|
|
|
$("#profilepane").find(".alert-success").remove();
|
|
|
|
var img = $("#profileimg").val();
|
2013-07-27 14:11:31 +00:00
|
|
|
/*
|
2013-05-30 01:04:22 +00:00
|
|
|
img = escape(img).replace(/\//g, "%2F")
|
|
|
|
.replace(/&/g, "%26")
|
|
|
|
.replace(/=/g, "%3D")
|
|
|
|
.replace(/\?/g, "%3F");
|
2013-07-27 14:11:31 +00:00
|
|
|
*/
|
2013-05-30 01:04:22 +00:00
|
|
|
var url = api + "setprofile?" + [
|
2013-07-27 14:11:31 +00:00
|
|
|
"name=" + encodeURIComponent(uname),
|
2013-05-30 01:04:22 +00:00
|
|
|
"session=" + session,
|
2013-07-27 14:11:31 +00:00
|
|
|
"profile_image=" + encodeURIComponent(img),
|
|
|
|
"profile_text=" + encodeURIComponent($("#profiletext").val())
|
2013-05-30 01:04:22 +00:00
|
|
|
].join("&") + "&callback=?";
|
|
|
|
|
|
|
|
$.getJSON(url, function(data) {
|
|
|
|
if(data.success) {
|
|
|
|
$("<div/>").addClass("alert alert-success")
|
|
|
|
.text("Profile updated.")
|
|
|
|
.insertBefore($("#profilepane form"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$("<div/>").addClass("alert alert-error")
|
|
|
|
.text(data.error)
|
|
|
|
.insertBefore($("#profilepane form"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-05-29 15:49:09 +00:00
|
|
|
$("#login").click(function() {
|
|
|
|
if(!loggedin) {
|
|
|
|
makeTabCallback("#login", "#loginpane")();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uname = "";
|
|
|
|
session = "";
|
2013-06-19 22:11:11 +00:00
|
|
|
eraseCookie("cytube_uname");
|
|
|
|
eraseCookie("cytube_session");
|
2013-05-29 15:49:09 +00:00
|
|
|
$("#accountnav li")[0].innerHTML = "Not Logged In";
|
|
|
|
$("#register").show();
|
|
|
|
$("#login").text("Login");
|
|
|
|
loggedin = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function createCookie(name,value,days) {
|
|
|
|
if (days) {
|
|
|
|
var date = new Date();
|
|
|
|
date.setTime(date.getTime()+(days*24*60*60*1000));
|
|
|
|
var expires = "; expires="+date.toGMTString();
|
|
|
|
}
|
|
|
|
else var expires = "";
|
|
|
|
document.cookie = name+"="+value+expires+"; path=/";
|
|
|
|
}
|
|
|
|
|
|
|
|
function readCookie(name) {
|
|
|
|
var nameEQ = name + "=";
|
|
|
|
var ca = document.cookie.split(";");
|
|
|
|
for(var i=0;i < ca.length;i++) {
|
|
|
|
var c = ca[i];
|
|
|
|
while (c.charAt(0)==" ") c = c.substring(1,c.length);
|
|
|
|
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function eraseCookie(name) {
|
|
|
|
createCookie(name,"",-1);
|
|
|
|
}
|