| 1 |
User = { |
| 2 |
randomString: function(len) |
| 3 |
{ |
| 4 |
var hex = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']; |
| 5 |
var string = ""; |
| 6 |
while(len-->0) { string += hex[parseInt(16*Math.random())]; } |
| 7 |
return string; }, |
| 8 |
|
| 9 |
markInvalid: function(input, reason) { |
| 10 |
var classes = ""; |
| 11 |
if(input["class"]) { classes = input.getAttribute("class"); } |
| 12 |
input.setAttribute("class", classes + " error"); |
| 13 |
input.title = reason; |
| 14 |
return false; }, |
| 15 |
|
| 16 |
markValid: function(input) { |
| 17 |
if(input.getAttribute("class")) { |
| 18 |
var stripped = input.getAttribute("class").replace("error", ""); |
| 19 |
input.setAttribute("class", stripped); } |
| 20 |
input.title = ""; |
| 21 |
return true; }, |
| 22 |
|
| 23 |
validName: function(input) |
| 24 |
{ |
| 25 |
var username = input.value; |
| 26 |
if(username.trim()=="") { return this.markInvalid(input, "You forgot your user name."); } |
| 27 |
if(username.indexOf("'")>-1) { return this.markInvalid(input, "Apostrophes are not allowed in user names."); } |
| 28 |
if(username.length<4) { return this.markInvalid(input, "Sorry, user names must be more than 3 letters."); } |
| 29 |
return this.markValid(input); |
| 30 |
}, |
| 31 |
|
| 32 |
validEmail: function(input) |
| 33 |
{ |
| 34 |
var valid = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/.test(input.value); |
| 35 |
if(!valid) { return this.markInvalid(input,"This is not a real email address..."); } |
| 36 |
return this.markValid(input); |
| 37 |
}, |
| 38 |
|
| 39 |
passwordMatch: function(input1, input2) |
| 40 |
{ |
| 41 |
var matched = (input1.value==input2.value); |
| 42 |
if(!matched) { return this.markInvalid(input2, "The two passwords don't match"); } |
| 43 |
return this.markValid(input2); |
| 44 |
}, |
| 45 |
|
| 46 |
validPassword: function(input) |
| 47 |
{ |
| 48 |
var password = input.value; |
| 49 |
if(password.trim()=="") { return this.markInvalid(input, "You need to fill in a password"); } |
| 50 |
return this.markValid(input); |
| 51 |
}, |
| 52 |
|
| 53 |
strongPassword: function(input) |
| 54 |
{ |
| 55 |
var password = input.value; |
| 56 |
if(!this.validPassword(input)) { return false; } |
| 57 |
if(password.length<8) { return this.markInvalid(input, "Your password is too easy to guess, please pick something longer. Use an entire sentence. if you like."); } |
| 58 |
return this.markValid(input); |
| 59 |
}, |
| 60 |
|
| 61 |
processRegistration: function() |
| 62 |
{ |
| 63 |
var valid = true; |
| 64 |
var form = document.getElementById('registration'); |
| 65 |
|
| 66 |
valid &= this.validName(form["username"]); |
| 67 |
valid &= this.validEmail(form["email"]); |
| 68 |
valid &= this.passwordMatch(form["password1"], form["password2"]); |
| 69 |
valid &= this.strongPassword(form["password1"]); |
| 70 |
|
| 71 |
if(valid) { |
| 72 |
form["sha1"].value = Sha1.hash(form["password1"].value); |
| 73 |
form["password1"].value = this.randomString(16); |
| 74 |
form["password2"].value = form["password1"].value; |
| 75 |
form.submit(); } |
| 76 |
}, |
| 77 |
|
| 78 |
processLogin: function() |
| 79 |
{ |
| 80 |
var valid = true; |
| 81 |
var form = document.getElementById('login'); |
| 82 |
|
| 83 |
valid &= this.validName(form["username"]); |
| 84 |
valid &= this.validPassword(form["password1"]); |
| 85 |
|
| 86 |
if(valid) { |
| 87 |
form["sha1"].value = Sha1.hash(form["password1"].value); |
| 88 |
form["password1"].value = this.randomString(16); |
| 89 |
form.submit(); } |
| 90 |
}, |
| 91 |
|
| 92 |
processUpdate: function() |
| 93 |
{ |
| 94 |
var valid = true; |
| 95 |
var update = false; |
| 96 |
var form = document.getElementById('update'); |
| 97 |
|
| 98 |
if(form["email"].value.trim()!="") { |
| 99 |
valid &= this.validEmail(form["email"]); |
| 100 |
if(valid) update = true; } |
| 101 |
|
| 102 |
if(form["password1"].value.trim()!="") { |
| 103 |
valid &= this.passwordMatch(form["password1"], form["password2"]); |
| 104 |
valid &= this.strongPassword(form["password1"]); |
| 105 |
if(valid) { |
| 106 |
form["sha1"].value = Sha1.hash(form["password1"].value); |
| 107 |
form["password1"].value = this.randomString(16); |
| 108 |
form["password2"].value = form["password1"].value; |
| 109 |
update = true; }} |
| 110 |
|
| 111 |
if(valid && update) { form.submit(); } |
| 112 |
}, |
| 113 |
|
| 114 |
|
| 115 |
add: function(p, c) { p.appendChild(c); }, |
| 116 |
|
| 117 |
make: function(tag, properties) { |
| 118 |
var tag = document.createElement(tag); |
| 119 |
if(properties !== null) { |
| 120 |
for(property in properties) { |
| 121 |
tag[property] = properties[property]; }} |
| 122 |
return tag; }, |
| 123 |
|
| 124 |
injectLogin: function(parent) { |
| 125 |
var add = this.add; |
| 126 |
var make = this.make; |
| 127 |
|
| 128 |
var form = this.make("form", {id: "usered_login_form", action: ".", method: "POST"}); |
| 129 |
add(form, make("label", {"for": "usered_username", innerHTML: "user name"})); |
| 130 |
add(form, make("input", {id: "usered_username", type: "text"})); |
| 131 |
add(form, make("label", {"for": "usered_password", innerHTML: "password"})); |
| 132 |
add(form, make("input", {id: "usered_password", type: "password"})); |
| 133 |
add(form, make("input", {id: "usered_login_button", type: "submit", value: "log in"})); |
| 134 |
add(parent, form); |
| 135 |
}, |
| 136 |
|
| 137 |
|
| 138 |
injectLogout: function(parent) { |
| 139 |
var add = this.add; |
| 140 |
var make = this.make; |
| 141 |
|
| 142 |
var form = make("form", {id: "usered_logout_form", action: ".", method: "POST"}); |
| 143 |
add(form, make("input", {type: "hidden", name: "op", value: "logout"})); |
| 144 |
add(form, make("input", {id: "usered_logout_button", type: "submit", value: "log out"})); |
| 145 |
add(parent, form) |
| 146 |
} |
| 147 |
}; |
| 148 |
|