rlm@0: // js handling the login procedures rlm@0: rlm@0: // constants rlm@0: var NORMAL_STATE = 4; rlm@0: var LOGIN_PREFIX = 'login.php?'; rlm@0: rlm@0: // variables rlm@0: var http = getHTTPObject(); // We create the HTTP Object rlm@0: var hasSeed = false; rlm@0: var loggedIn = false; rlm@0: var seed_id = 0; rlm@0: var seed = 0; rlm@0: var fullname = ''; rlm@0: var messages = ''; rlm@0: rlm@0: // getSeed method: gets a seed from the server for this transaction rlm@0: function getSeed() rlm@0: { // only get a seed if we're not logged in and we don't already have one rlm@0: if (!loggedIn && !hasSeed) { rlm@0: // open up the path rlm@0: http.open('GET', LOGIN_PREFIX + 'task=getseed', true); rlm@0: http.onreadystatechange = handleHttpGetSeed; rlm@0: http.send(null); rlm@0: } rlm@0: } rlm@0: rlm@0: // handleHttpGetSeed method: called when the seed is returned from the server rlm@0: function handleHttpGetSeed() rlm@0: { rlm@0: // if there hasn't been any errors rlm@0: if (http.readyState == NORMAL_STATE) { rlm@0: // split by the divider | rlm@0: results = http.responseText.split('|'); rlm@0: rlm@0: // id is the first element rlm@0: seed_id = results[0]; rlm@0: rlm@0: // seed is the second element rlm@0: seed = results[1]; rlm@0: rlm@0: // now we have the seed rlm@0: hasSeed = true; rlm@0: } rlm@0: } rlm@0: rlm@0: // validateLogin method: validates a login request rlm@0: function validateLogin() rlm@0: { rlm@0: // ignore request if we are already logged in rlm@0: if (loggedIn) rlm@0: return; rlm@0: rlm@0: // get form form elements 'username' and 'password' rlm@0: username = document.getElementById('username').value; rlm@0: password = document.getElementById('password').value; rlm@0: rlm@0: // ignore if either is empty rlm@0: if (username != '' && password != '') { rlm@0: // compute the hash of the hash of the password and the seed rlm@0: hash = hex_md5(hex_md5(password) + seed); rlm@0: rlm@0: // open the http connection rlm@0: http.open('GET', LOGIN_PREFIX + 'task=checklogin&username='+username+'&id='+seed_id+'&hash='+hash, true); rlm@0: rlm@0: // where to go rlm@0: http.onreadystatechange = handleHttpValidateLogin; rlm@0: http.send(null); rlm@0: } rlm@0: } rlm@0: rlm@0: // handleHttpValidateLogin method: called when the validation results are returned from the server rlm@0: function handleHttpValidateLogin() rlm@0: { rlm@0: // did the connection work? rlm@0: if (http.readyState == NORMAL_STATE) { rlm@0: // split by the pipe rlm@0: results = http.responseText.split('|'); rlm@0: if (results[0] == 'true') rlm@0: { rlm@0: hasSeed = false; rlm@0: loggedIn = true; rlm@0: fullname = results[1]; rlm@0: messages = ''; rlm@0: } rlm@0: else rlm@0: { rlm@0: messages = results[1]; rlm@0: } rlm@0: showLogin(); rlm@0: } rlm@0: } rlm@0: rlm@0: // resetLogin method: if logged in, 'logs out' and allows a different user/pass to be entered rlm@0: function resetLogin() rlm@0: { rlm@0: loggedIn = false; rlm@0: hasSeed = false; rlm@0: }