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