33ca93ba7f00cac5ba0d2c627e7822d8eab267a2
[Packages/TYPO3.CMS.git] / typo3 / js / loginrefresh.js
1 /***************************************************************
2 * Copyright notice
3 *
4 * (c) 2009-2011 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 Ext.namespace('Ext.ux.TYPO3');
31
32 Ext.ux.TYPO3.loginRefresh = Ext.extend(Ext.util.Observable, {
33 locked: 0,
34 interval: 60,
35
36 constructor: function(config) {
37 config = config || {};
38 Ext.apply(this, config);
39 this.initComponents();
40 this.loadingTask = {
41 run: function(){
42 // interval run
43 Ext.Ajax.request({
44 url: "ajax.php",
45 params: {
46 "ajaxID": "BackendLogin::isTimedOut",
47 "skipSessionUpdate": 1
48 },
49 method: "GET",
50 success: function(response, options) {
51 var result = Ext.util.JSON.decode(response.responseText);
52 if (result.login.locked) {
53 this.locked = 1;
54 Ext.MessageBox.show({
55 title: TYPO3.LLL.core.please_wait,
56 msg: TYPO3.LLL.core.be_locked,
57 width: 500,
58 icon: Ext.MessageBox.INFO,
59 closable: false
60 });
61 } else {
62 if (this.locked === 1) {
63 this.locked = 0;
64 Ext.MessageBox.hide();
65 }
66 }
67 if ((result.login.timed_out || result.login.will_time_out) && Ext.getCmp("loginformWindow")) {
68 Ext.getCmp("login_username").value = TYPO3.configuration.username;
69 this.stopTimer();
70 if (result.login.timed_out) {
71 this.showLoginForm();
72 } else {
73 this.progressWindow.show();
74 }
75 }
76 },
77 failure: function() {
78
79 },
80 scope: this
81 });
82 },
83 interval: this.interval * 1000,
84 scope: this
85 };
86 this.startTimer();
87 Ext.ux.TYPO3.loginRefresh.superclass.constructor.call(this, config);
88 },
89
90 initComponents: function() {
91 var loginPanel = new Ext.FormPanel({
92 url: "ajax.php",
93 id: "loginform",
94 title: TYPO3.LLL.core.refresh_login_title,
95 defaultType: 'textfield',
96 scope: this,
97 width: "100%",
98 bodyStyle: "padding: 5px 5px 3px 5px; border-width: 0; margin-bottom: 7px;",
99
100 items: [{
101 xtype: "panel",
102 bodyStyle: "margin-bottom: 7px; border: none;",
103 html: TYPO3.LLL.core.login_expired
104 },{
105 fieldLabel: TYPO3.LLL.core.refresh_login_password,
106 name: "p_field",
107 width: 250,
108 id: "password",
109 inputType: "password"
110 },{
111 inputType: "hidden",
112 name: "username",
113 id: "login_username",
114 value: ""
115 },{
116 inputType: "hidden",
117 name: "userident",
118 id: "userident",
119 value: ""
120 }, {
121 inputType: "hidden",
122 name: "challenge",
123 id: "challenge",
124 value: ''
125 }
126 ],
127 keys:({
128 key: Ext.EventObject.ENTER,
129 fn: this.triggerSubmitForm,
130 scope: this
131 }),
132 buttons: [{
133 text: TYPO3.LLL.core.refresh_login_button,
134 formBind: true,
135 handler: this.triggerSubmitForm
136 }, {
137 text: TYPO3.LLL.core.refresh_logout_button,
138 formBind: true,
139 handler: function() {
140 top.location.href = TYPO3.configuration.siteUrl + TYPO3.configuration.TYPO3_mainDir + "logout.php";
141 }
142 }]
143 });
144 this.loginRefreshWindow = new Ext.Window({
145 id: "loginformWindow",
146 width: 450,
147 autoHeight: true,
148 closable: true,
149 resizable: false,
150 plain: true,
151 border: false,
152 modal: true,
153 draggable: false,
154 items: [loginPanel],
155 listeners: {
156 activate: function() {
157 Ext.getCmp('password').focus(false, 800);
158 }
159 }
160 });
161
162 var progressControl = new Ext.ProgressBar({
163 autoWidth: true,
164 autoHeight: true,
165 value: 30
166 });
167
168 this.progressWindow = new Ext.Window({
169 closable: false,
170 resizable: false,
171 draggable: false,
172 modal: true,
173 id: "loginRefreshWindow",
174 items: [{
175 xtype: "panel",
176 bodyStyle: "padding: 5px 5px 3px 5px; border-width: 0; margin-bottom: 7px;",
177 bodyBorder: false,
178 autoHeight: true,
179 autoWidth: true,
180 html: TYPO3.LLL.core.login_about_to_expire
181 },
182 progressControl
183 ],
184 title: TYPO3.LLL.core.login_about_to_expire_title,
185 width: 450,
186
187 buttons: [{
188 text: TYPO3.LLL.core.refresh_login_refresh_button,
189 handler: function() {
190 var refresh = Ext.Ajax.request({
191 url: "ajax.php",
192 params: {
193 "ajaxID": "BackendLogin::isTimedOut"
194 },
195 method: "GET",
196 scope: this
197 });
198 TYPO3.loginRefresh.progressWindow.hide();
199 progressControl.reset();
200 TYPO3.loginRefresh.startTimer();
201 }
202 }, {
203 text: TYPO3.LLL.core.refresh_direct_logout_button,
204 handler: function() {
205 top.location.href = TYPO3.configuration.siteUrl + TYPO3.configuration.TYPO3_mainDir + "logout.php";
206 }
207 }]
208 });
209 this.progressWindow.on('show', function(){
210 progressControl.wait({
211 interval: 1000,
212 duration: 30000,
213 increment: 32,
214 text: String.format(TYPO3.LLL.core.refresh_login_countdown, '30'),
215 fn: function() {
216 TYPO3.loginRefresh.showLoginForm();
217 }
218 });
219
220 });
221 progressControl.on('update', function(control, value, text) {
222 var rest = parseInt(30 - (value * 30), 10);
223 if (rest === 1) {
224 control.updateText(String.format(TYPO3.LLL.core.refresh_login_countdown_singular, rest));
225 } else {
226 control.updateText(String.format(TYPO3.LLL.core.refresh_login_countdown, rest));
227 }
228 });
229
230 this.loginRefreshWindow.on('close', function(){
231 TYPO3.loginRefresh.startTimer();
232 });
233 },
234
235 showLoginForm: function() {
236 if (TYPO3.configuration.showRefreshLoginPopup) {
237 //log off for sure
238 Ext.Ajax.request({
239 url: "ajax.php",
240 params: {
241 "ajaxID": "BackendLogin::logout"
242 },
243 method: "GET",
244 scope: this,
245 success: function(response, opts) {
246 TYPO3.loginRefresh.showLoginPopup();
247 },
248 failure: function(response, opts) {
249 alert("something went wrong");
250 }
251 });
252 } else {
253 Ext.getCmp("loginRefreshWindow").hide();
254 Ext.getCmp("loginformWindow").show();
255 }
256 },
257
258 showLoginPopup: function() {
259 Ext.getCmp("loginRefreshWindow").hide();
260 var vHWin = window.open("login_frameset.php","relogin_" + TS.uniqueID,"height=450,width=700,status=0,menubar=0,location=1");
261 vHWin.focus();
262 },
263
264 startTimer: function() {
265 Ext.TaskMgr.start(this.loadingTask);
266 },
267
268 stopTimer: function() {
269 Ext.TaskMgr.stop(this.loadingTask);
270 },
271
272 submitForm: function(challenge) {
273 var form = Ext.getCmp("loginform").getForm();
274 var fields = form.getValues();
275 if (fields.p_field === "") {
276 Ext.Msg.alert(TYPO3.LLL.core.refresh_login_failed, TYPO3.LLL.core.refresh_login_emptyPassword);
277 } else {
278 if (TS.securityLevel === "superchallenged") {
279 fields.p_field = MD5(fields.p_field);
280 }
281 if (TS.securityLevel === "superchallenged" || TS.securityLevel === "challenged") {
282 fields.challenge = challenge;
283 fields.userident = MD5(fields.username + ":" + fields.p_field + ":" + challenge);
284 } else {
285 fields.userident = fields.p_field;
286 }
287 fields.p_field = "";
288 form.setValues(fields);
289
290 form.submit({
291 method: "POST",
292 waitTitle: TYPO3.LLL.core.waitTitle,
293 waitMsg: " ",
294 params: {
295 "ajaxID": "BackendLogin::login",
296 "login_status": "login"
297 },
298 success: function(form, action) {
299 // response object is "login" so real result will be available in failure handler
300 Ext.getCmp("loginformWindow").hide();
301 TYPO3.loginRefresh.startTimer();
302 },
303 failure: function(form, action) {
304 var result = Ext.util.JSON.decode(action.response.responseText).login;
305 if (result.success) {
306 // User is logged in
307 Ext.getCmp("loginformWindow").hide();
308 TYPO3.loginRefresh.startTimer();
309 } else {
310 // TODO: add failure to notification system instead of alert
311 Ext.Msg.alert(TYPO3.LLL.core.refresh_login_failed, TYPO3.LLL.core.refresh_login_failed_message);
312 }
313 }
314 });
315 }
316 },
317
318 triggerSubmitForm: function() {
319 if (TS.securityLevel === 'superchallenged' || TS.securityLevel === 'challenged') {
320 Ext.Ajax.request({
321 url: 'ajax.php',
322 params: {
323 'ajaxID': 'BackendLogin::getChallenge',
324 'skipSessionUpdate': 1
325 },
326 method: 'GET',
327 success: function(response) {
328 var result = Ext.util.JSON.decode(response.responseText);
329 if (result.challenge) {
330 Ext.getCmp('challenge').value = result.challenge;
331 TYPO3.loginRefresh.submitForm(result.challenge);
332 }
333 },
334 scope: this
335 });
336 } else {
337 this.submitForm();
338 }
339 }
340 });
341
342
343
344 /**
345 * Initialize login expiration warning object
346 */
347 Ext.onReady(function() {
348 TYPO3.loginRefresh = new Ext.ux.TYPO3.loginRefresh();
349 });