1 /* ***** BEGIN LICENSE BLOCK *****
  2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3  *
  4  * The contents of this file are subject to the Mozilla Public License Version
  5  * 1.1 (the "License"); you may not use this file except in compliance with
  6  * the License. You may obtain a copy of the License at
  7  * http://www.mozilla.org/MPL/
  8  *
  9  * Software distributed under the License is distributed on an "AS IS" basis,
 10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11  * for the specific language governing rights and limitations under the
 12  * License.
 13  *
 14  * The Initial Developer of the Original Code is Fireinput Inc.
 15  *
 16  * Portions created by the Initial Developer are Copyright (C) 2007
 17  * the Initial Developer. All Rights Reserved.
 18  *
 19  * Contributor(s):
 20  *     Olly Ja <ollyja@gmail.com>
 21  *
 22  * Alternatively, the contents of this file may be used under the terms of
 23  * either the GNU General Public License Version 2 or later (the "GPL"), or
 24  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 25  * in which case the provisions of the GPL or the LGPL are applicable instead
 26  * of those above. If you wish to allow use of your version of this file only
 27  * under the terms of either the GPL or the LGPL, and not to allow others to
 28  * use your version of this file under the terms of the MPL, indicate your
 29  * decision by deleting the provisions above and replace them with the notice
 30  * and other provisions required by the GPL or the LGPL. If you do not delete
 31  * the provisions above, a recipient may use your version of this file under
 32  * the terms of any one of the MPL, the GPL or the LGPL.
 33  *
 34  * ***** END LICENSE BLOCK ***** 
 35  */
 36 
 37 var FireinputServerLogin = 
 38 {
 39     checkUserLogon: function(loginCallback)
 40     {
 41        var ajax = new Ajax();
 42        if(!ajax)
 43           return;
 44        var self = this;
 45          
 46        ajax.setOptions(
 47           {
 48              method: 'get',
 49              onSuccess: function(p) { self.checkUserLogonSuccess(p, loginCallback); },
 50              onFailure: function(p) { self.checkUserLogonFailure(p); }
 51           });
 52        ajax.request(SERVER_URL + "/account/logon_info.php"); 
 53     },
 54       
 55     checkUserLogonSuccess: function(p, loginCallback)
 56     {
 57        if(!p || p.responseText.length <= 0)
 58        {
 59           this.checkUserLogonFailure(p); 
 60           return;
 61        }
 62 
 63        var jsonResp;
 64        try {
 65           jsonResp = eval('(' + p.responseText + ')');
 66        }
 67        catch(e) 
 68        {
 69           this.checkUserLogonFailure(p); 
 70           return;
 71        };
 72 
 73        if(typeof(jsonResp) == 'undefined')
 74        {
 75           this.checkUserLogonFailure(p); 
 76           return;
 77        } 
 78        
 79        var handle = document.getElementById("logonUser");
 80        handle.innerHTML = "您好! " + jsonResp.name; 
 81        handle = document.getElementById("logonUserBox");
 82        handle.style.display = ""; 
 83 
 84        handle = document.getElementById("logonForm"); 
 85        handle.style.display = "none";
 86 
 87        handle = document.getElementById("logonButton");
 88        handle.setAttribute("image", "");
 89 
 90        handle = document.getElementById("logonLink"); 
 91        handle.style.display = "none";
 92 
 93        // we are ready to issue loginCallback now 
 94        if(loginCallback)
 95          setTimeout(function() { loginCallback();}, 200);  
 96     },
 97 
 98     checkUserLogonFailure: function(p)
 99     {
100 
101        var handle = document.getElementById("logonUser");
102        handle.innerHTML = "";
103 
104        handle = document.getElementById("logonUserBox");
105        handle.style.display = "none";
106 
107     }, 
108 
109     toggleLogonForm: function(event)
110     {
111        var handle = document.getElementById("logonForm"); 
112        if(handle.style.display != "none")
113        {
114          handle.style.display = "none";
115          return; 
116        }
117        var ajax = new Ajax();
118        if(!ajax)
119           return;
120 
121        var self = this;
122        var px = event.pageX - 400; 
123        var py = event.pageY+1; 
124        var target = event.target; 
125        target.oldInnerHTML = target.innerHTML; 
126        target.innerHTML = '<img src="chrome://fireinput/skin/loading.gif"/>'; 
127        ajax.setOptions(
128           {
129              method: 'get',
130              onSuccess: function(p) { self.showLogonFormSuccess(p, px, py,target); },
131              onFailure: function(p) { self.showLogonFormFailure(p, px, py,target); }
132           });
133        ajax.request(SERVER_URL + "/account/logon_form_simple.php");
134     },
135  
136     showLogonFormSuccess: function(p, px, py,target)
137     {
138        target.innerHTML = target.oldInnerHTML; 
139        var handle = document.getElementById("logonSeed");
140        handle.value =  p.responseText; 
141        $("#logonForm").css("left", px);
142        $("#logonForm").css("top", py);
143        $("#logonForm").css("height", '8em');
144        $("#logonForm").show(); 
145     }, 
146 
147     showLogonFormFailure: function(p, px, py,target)
148     {
149        target.innerHTML = target.oldInnerHTML; 
150        $("#logonForm").css("left", px);
151        $("#logonForm").css("top", py);
152        $("#logonForm").css("height", 50);
153        $("#logonForm").html("<div style='margin: 10px; color:red'>无法显示登录网页,连接火输网站失败</div>"); 
154        $("#logonForm").show(); 
155     },
156 
157     logon: function(event, loginCallback)
158     {
159        // check enter key 
160        if(event && event.keyCode!=13)
161              return true; 
162        var handle = document.getElementById("logonMessage"); 
163        handle.value=""; 
164 
165        handle = document.getElementById("logonButton");
166        handle.type = "image";
167        handle.src = "chrome://fireinput/skin/loading.gif"; 
168        var self = this; 
169        setTimeout(function(){self.loginServer(loginCallback); }, 1000); 
170        return false; 
171     },
172 
173     loginServer: function(loginCallback)
174     {
175 
176        var email = document.getElementById("logonEmail").value; 
177        var password = document.getElementById("logonPasswd").value; 
178        var seed = document.getElementById("logonSeed").value; 
179        var salt = password.substr(Math.floor(password.length/2),password.length);
180        var md5hex1 = hex_hmac_md5(password, salt);
181        var md5hex2 = hex_hmac_md5(md5hex1, seed);
182        var url = "/account/logon_user.php";
183        var params = "email="+email + "&password="+md5hex2;
184 
185        var ajax = new Ajax();
186        var self = this; 
187        ajax.setOptions(
188         {
189           method: 'post',
190           postBody: params,
191           contentType: 'application/x-www-form-urlencoded',
192           onSuccess: function(p) { self.loginServerSuccess(p, loginCallback); },
193           onFailure: function(p) { self.loginServerFailure(p); }
194         });
195        ajax.request(SERVER_URL + url);
196     },
197 
198     loginServerSuccess: function(p, loginCallback)
199     {
200        var handle = document.getElementById("logonButton");
201        handle.type = "button";
202        handle.src = ""; 
203        this.checkUserLogon(loginCallback); 
204     }, 
205 
206     loginServerFailure: function(p)
207     {
208        var handle = document.getElementById("logonMessage"); 
209        handle.innerHTML="登录失败,您输入的邮箱或密码不正确"; 
210 
211        handle = document.getElementById("logonButton");
212        handle.type = "button";
213        handle.src = ""; 
214     }, 
215 
216 }; 


syntax highlighted by Code2HTML, v. 0.9.1