6e4e404f407aff74cf32201a7bda340cc2658587
[Packages/TYPO3.CMS.git] / typo3 / js / loginrefresh.js
1 /***************************************************************
2 * Copyright notice
3 *
4 * (c) 2009 Steffen Kamper <info@sk-typo3.de>
5 * All rights reserved
6 *
7 * This script is part of the TYPO3 project. The TYPO3 project is
8 * free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * The GNU General Public License can be found at
14 * http://www.gnu.org/copyleft/gpl.html.
15 * A copy is found in the textfile GPL.txt and important notices to the license
16 * from the author is found in LICENSE.txt distributed with these scripts.
17 *
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * AJAX login refresh box
29 */
30
31 /**
32 * Functions for session-expiry detection:
33 */
34 function busy() { //
35 this.loginRefreshed = busy_loginRefreshed;
36 this.openRefreshWindow = busy_OpenRefreshWindow;
37 this.openLockedWaitWindow = busy_openLockedWaitWindow;
38 this.busyloadTime=0;
39 this.openRefreshW=0;
40 this.reloginCancelled=0;
41 this.earlyRelogin=0;
42 this.locked=0;
43
44 // starts the timer and resets the earlyRelogin variable so that
45 // the countdown works properly.
46 this.startTimer = function() {
47 this.earlyRelogin = 0;
48 this.timer.start();
49 }
50
51 this.stopTimer = function() {
52 this.timer.stop();
53 }
54
55 // simple timer that polls the server to determine imminent timeout.
56 this.timer = new Ajax.PeriodicalUpdater("","ajax.php", {
57 method: "get",
58 frequency: 60,
59 decay: 1,
60 parameters: "ajaxID=BackendLogin::isTimedOut&skipSessionUpdate=1",
61 onSuccess: function(e) {
62 var login = e.responseJSON.login.evalJSON();
63 if(login.locked) {
64 busy.locked = 1;
65 busy.openLockedWaitWindow();
66 } else if(login.timed_out) {
67 busy.openRefreshWindow();
68 }
69 if (busy.locked && !login.locked && !login.timed_out) {
70 busy.locked = 0;
71 Ext.MessageBox.hide();
72 }
73 }
74 });
75
76 // this function runs the countdown and opens the login window
77 // as soon as the countdown expires.
78 this.countDown = function(progressControl, progressTextFormatPlural, progressTextFormatSingular, secondsRemaining, totalSeconds) {
79
80 if(busy.earlyRelogin == 0) {
81 if(secondsRemaining > 1) {
82 progressControl.updateText(String.format(progressTextFormatPlural, secondsRemaining));
83 progressControl.updateProgress(secondsRemaining/(1.0*totalSeconds));
84 setTimeout(function () {
85 busy.countDown(progressControl, progressTextFormatPlural, progressTextFormatSingular,secondsRemaining - 1, totalSeconds);
86 }, 1000);
87 } else if(secondsRemaining > 0) {
88 progressControl.updateText(String.format(progressTextFormatSingular, secondsRemaining));
89 progressControl.updateProgress(secondsRemaining/(1.0*totalSeconds));
90 setTimeout(function () {
91 busy.countDown(progressControl, progressTextFormatPlural, progressTextFormatSingular,secondsRemaining - 1, totalSeconds);
92 }, 1000);
93 } else {
94 busy.openRefreshW = 1;
95 busy.openLogin();
96 }
97 }
98 };
99
100 // Closes the countdown window and opens a new one with a login form.
101 this.openLogin = function() {
102 var login;
103 doChallengeResponse = function(superchallenged) {
104 password = $$("#loginform form")[0].p_field.value;
105
106 if (password) {
107 if (superchallenged) {
108 password = MD5(password); // this makes it superchallenged!!
109 }
110 str = $("login_username").value+":"+password+":"+$("challenge").value;
111 $("userident").value = MD5(str);
112 $("password").value = "";
113
114 return true;
115 }
116 }
117
118 submitForm = function() {
119 if(TS.securityLevel == "superchallenged") {
120 doChallengeResponse(1);
121 } else if (TS.securityLevel == "challenged") {
122 doChallengeResponse(0);
123 } else {
124 $("userident").value = $$("#loginform form")[0].p_field.value;
125 $("password").value= "";
126 }
127
128 login.getForm().submit({
129 method: "post",
130 waitTitle: TYPO3.LLL.core.waitTitle,
131 waitMsg: " ",
132 params: "ajaxID=BackendLogin::login&login_status=login",
133 success: function() {
134 win.close();
135 setTimeout("busy.startTimer()", 2000);
136
137 },
138
139 failure: function() {
140 // TODO: add failure to notification system instead of alert
141 // Ext.tip.msg("Login failed", "Username or Password incorrect!");
142 Ext.Msg.alert(TYPO3.LLL.core.refresh_login_failed, TYPO3.LLL.core.refresh_login_failed_message);
143 }
144 });
145 }
146
147 logout = new Ajax.Request("ajax.php", {
148 method: "get",
149 parameters: "ajaxID=BackendLogin::logout"
150 });
151
152 Ext.onReady(function(){
153 login = new Ext.FormPanel({
154 url: "ajax.php",
155 id: "loginform",
156 title: TYPO3.LLL.core.refresh_login_title,
157 defaultType: "textfield",
158 width: "100%",
159 bodyStyle: "padding: 5px 5px 3px 5px; border-width: 0; margin-bottom: 7px;",
160
161 items: [{
162 xtype: "panel",
163 bodyStyle: "margin-bottom: 7px; border: none;",
164 html: TYPO3.LLL.core.login_expired
165 },{
166 fieldLabel: TYPO3.LLL.core.refresh_login_username,
167 name: "username",
168 id: "login_username",
169 allowBlank: false,
170 width: 250
171 },{
172 fieldLabel: TYPO3.LLL.core.refresh_login_password,
173 name: "p_field",
174 width: 250,
175 id: "password",
176 inputType: "password"
177 },{
178 xtype: "hidden",
179 name: "userident",
180 id: "userident",
181 value: ""
182 }, {
183 xtype: "hidden",
184 name: "challenge",
185 id: "challenge",
186 value: TYPO3.configuration.challenge
187 }
188 ],
189 keys:({
190 key: Ext.EventObject.ENTER,
191 fn: submitForm,
192 scope: this
193 }),
194 buttons: [{
195 text: TYPO3.LLL.core.refresh_login_button,
196 formBind: true,
197 handler: submitForm
198 }, {
199 text: TYPO3.LLL.core.refresh_logout_button,
200 formBind: true,
201 handler: function() {
202 top.location.href = TYPO3.configuration.siteUrl + TYPO3.configuration.TYPO3_mainDir;
203 }
204 }]
205 });
206 win.close();
207 win = new Ext.Window({
208 width: 450,
209 autoHeight: true,
210 closable: false,
211 resizable: false,
212 plain: true,
213 border: false,
214 modal: true,
215 draggable: false,
216 items: [login]
217 });
218 win.show();
219 });
220 }
221 }
222
223 function busy_loginRefreshed() { //
224 this.openRefreshW=0;
225 this.earlyRelogin=0;
226 }
227
228 function busy_openLockedWaitWindow() {
229 Ext.MessageBox.show({
230 title: TYPO3.LLL.core.please_wait,
231 msg: TYPO3.LLL.core.be_locked,
232 width: 500,
233 icon: Ext.MessageBox.INFO,
234 closable: false
235 });
236 }
237
238 function busy_OpenRefreshWindow() {
239 this.openRefreshW = 1;
240
241 busy.stopTimer();
242
243 var seconds = 30;
244 var progressTextFormatSingular = TYPO3.LLL.core.refresh_login_countdown_singular;
245 var progressTextFormatPlural = TYPO3.LLL.core.refresh_login_countdown;
246 var progressText = String.format(progressTextFormatPlural, seconds);
247 var progressControl = new Ext.ProgressBar({
248 autoWidth: true,
249 autoHeight: true,
250 value: 1,
251 text: progressText
252 });
253
254 win = new Ext.Window({
255 closable: false,
256 resizable: false,
257 draggable: false,
258 modal: true,
259 items: [{
260 xtype: "panel",
261 bodyStyle: "padding: 5px 5px 3px 5px; border-width: 0; margin-bottom: 7px;",
262 bodyBorder: false,
263 autoHeight: true,
264 autoWidth: true,
265 html: TYPO3.LLL.core.login_about_to_expire
266 },
267 progressControl
268 ],
269 title: TYPO3.LLL.core.login_about_to_expire_title,
270 width: 450,
271
272 buttons: [{
273 text: TYPO3.LLL.core.refresh_login_refresh_button,
274 handler: function() {
275 refresh = new Ajax.Request("ajax.php", {
276 method: "get",
277 parameters: "ajaxID=BackendLogin::refreshLogin"
278 });
279 win.close();
280 busy.earlyRelogin = 1;
281 setTimeout("busy.startTimer()", 2000);
282 }
283 }, {
284 text: TYPO3.LLL.core.refresh_direct_logout_button,
285 handler: function() {
286 top.location.href = TYPO3.configuration.siteUrl + TYPO3.configuration.TYPO3_mainDir + "logout.php";
287 }
288 }]
289 });
290 win.show();
291 busy.countDown(progressControl, progressTextFormatPlural, progressTextFormatSingular, seconds, seconds);
292 }
293
294 /**
295 * Initialize login expiration warning object
296 */
297 var busy = new busy();
298 busy.loginRefreshed();