]> git.proxmox.com Git - pmg-gui.git/blame - js/Utils.js
LoginView.js - remove realm selector
[pmg-gui.git] / js / Utils.js
CommitLineData
2c1d504e 1/*global Proxmox */
fe81f069
DM
2Ext.ns('PMG');
3
fe81f069
DM
4console.log("Starting PMG Manager");
5
fe81f069 6
2c7b542d
DM
7Ext.define('PMG.Utils', {
8 singleton: true,
ad834b6f 9
fe81f069
DM
10 // this singleton contains miscellaneous utilities
11
f9b851e8
DM
12 senderText: gettext('Sender'),
13 receiverText: gettext('Receiver'),
ad88af09 14 scoreText: gettext('Score'),
012f0f49 15
7818f0a3
DM
16 user_role_text: {
17 root: gettext('Superuser'),
18 admin: gettext('Administrator'),
19 qmanager: gettext('Quarantine Manager'),
20 audit: gettext('Auditor'),
21 },
22
23 format_user_role: function(role) {
24 return PMG.Utils.user_role_text[role] || role;
25 },
26
c790d2ab
DM
27 oclass_text: {
28 who: gettext('Who Objects'),
29 what: gettext('What Objects'),
30 when: gettext('When Objects'),
076d6a72
DC
31 action: gettext('Action Objects'),
32 from: gettext('From'),
33 to: gettext('To')
34 },
35
36 oclass_icon: {
37 who: '<span class="fa fa-fw fa-user-circle"></span> ',
38 what: '<span class="fa fa-fw fa-cube"></span> ',
39 when: '<span class="fa fa-fw fa-clock-o"></span> ',
40 action: '<span class="fa fa-fw fa-flag"></span> ',
41 from: '<span class="fa fa-fw fa-user-circle"></span> ',
cc6b80b6 42 to: '<span class="fa fa-fw fa-user-circle"></span> '
076d6a72
DC
43 },
44
cf248533
DM
45 mail_status_map: {
46 2: 'delivered',
47 4: 'deferred',
48 5: 'bounced',
49 N: 'rejected',
50 G: 'greylisted',
51 A: 'accepted',
52 B: 'blocked',
53 Q: 'quarantine'
54 },
55
c4ff49f7
DC
56 icon_status_map: {
57 2: {
58 fa: 'check-circle',
59 color: 'green'
60 },
61 4: {
62 fa: 'clock-o',
63 },
64 5: {
65 fa: 'mail-reply',
66 color: 'gray'
67 },
68 N: {
69 fa: 'times-circle'
70 },
71 G: {
72 fa: 'list'
73 },
74 A: {
75 fa: 'check',
76 color: 'green'
77 },
78 B: {
79 fa: 'ban',
80 color: 'red'
81 },
82 Q: {
83 fa: 'cube'
84 }
85 },
86
87 format_status_icon: function(status) {
88 var icon = PMG.Utils.icon_status_map[status] || {};
89 return '<i class="fa fa-' + (icon.fa || 'question-circle') + ' ' +
90 (icon.color || '') + '"></i> ';
91 },
92
076d6a72
DC
93 format_oclass: function(oclass) {
94 var icon = PMG.Utils.oclass_icon[oclass] || '';
95 var text = PMG.Utils.oclass_text[oclass] || oclass;
96 return icon + text;
c790d2ab 97 },
ad834b6f
DM
98
99 rule_direction_text: {
100 0: gettext('In'),
101 1: gettext('Out'),
102 2: gettext('In & Out')
103 },
104
4b7e5c48 105 rule_direction_icon: {
2b97521b
DC
106 0: '<span class="fa fa-fw fa-long-arrow-left"></span> ',
107 1: '<span class="fa fa-fw fa-long-arrow-right"></span> ',
108 2: '<span class="fa fa-fw fa-exchange"></span> '
4b7e5c48
DC
109 },
110
ad834b6f 111 format_rule_direction: function(dir) {
4b7e5c48
DC
112 var icon = PMG.Utils.rule_direction_icon[dir] || '';
113 var text = PMG.Utils.rule_direction_text[dir] || dir;
114 return icon + text;
ad834b6f
DM
115 },
116
012f0f49
DM
117 format_otype: function(otype) {
118 var editor = PMG.Utils.object_editors[otype];
ea4f2a79 119 var iconCls = 'fa fa-question-circle';
012f0f49 120 if (editor) {
ea4f2a79
DC
121 var icon = '<span class="fa-fw ' + (editor.iconCls || iconCls) + '"></span> ';
122 return icon + editor.subject;
012f0f49 123 }
ea4f2a79
DC
124
125 return '<span class="fa-fw ' + iconCls + '"></span> unknown';
f6b1b3bf
DM
126 },
127
128 format_ldap_protocol: function(p) {
cc6b80b6
DM
129 if (p === undefined) { return 'LDAP'; }
130 if (p === 'ldap') { return 'LDAP'; }
131 if (p === 'ldaps') { return 'LDAPS'; }
f6b1b3bf 132 return 'unknown';
012f0f49
DM
133 },
134
b79fbba8
DC
135 convert_field_to_per_min: function(value, record) {
136 return (value/(record.data.timespan/60));
137 },
138
012f0f49
DM
139 object_editors: {
140 1000: {
ea4f2a79 141 iconCls: 'fa fa-filter',
5f3ec152 142 xtype: 'proxmoxWindowEdit',
012f0f49
DM
143 subdir: 'regex',
144 subject: gettext("Regular Expression"),
f7be608f 145 width: 400,
012f0f49
DM
146 items: [
147 {
148 xtype: 'textfield',
149 name: 'regex',
f7be608f 150 labelWidth: 150,
012f0f49
DM
151 fieldLabel: gettext("Regular Expression")
152 }
153 ]
154 },
5f3ec152 155 1005: {
ea4f2a79 156 iconCls: 'fa fa-users',
5f3ec152
DM
157 xtype: 'pmgLDAPGroupEditor',
158 subdir: 'ldap',
159 subject: gettext("LDAP Group")
160 },
6c137003 161 1006: {
ea4f2a79 162 iconCls: 'fa fa-user',
6c137003
DM
163 xtype: 'pmgLDAPUserEditor',
164 subdir: 'ldapuser',
165 subject: gettext("LDAP User")
166 },
012f0f49 167 1009: {
ea4f2a79 168 iconCls: 'fa fa-filter',
5f3ec152 169 xtype: 'proxmoxWindowEdit',
012f0f49
DM
170 subdir: 'receiver_regex',
171 subject: gettext("Regular Expression"),
f7be608f
DM
172 receivertest: true,
173 width: 400,
012f0f49
DM
174 items: [
175 {
176 xtype: 'textfield',
177 name: 'regex',
f7be608f 178 labelWidth: 150,
012f0f49
DM
179 fieldLabel: gettext("Regular Expression")
180 }
181 ]
182 },
183 1001: {
ea4f2a79 184 iconCls: 'fa fa-envelope-o',
5f3ec152 185 xtype: 'proxmoxWindowEdit',
012f0f49
DM
186 subdir: 'email',
187 subject: gettext("Email"),
f7be608f 188 width: 400,
012f0f49
DM
189 items: [
190 {
191 xtype: 'textfield',
192 name: 'email',
193 fieldLabel: gettext("Email")
194 }
195 ]
196 },
197 1007: {
ea4f2a79 198 iconCls: 'fa fa-envelope-o',
5f3ec152 199 xtype: 'proxmoxWindowEdit',
012f0f49
DM
200 subdir: 'receiver',
201 subject: gettext("Email"),
f7be608f
DM
202 receivertest: true,
203 width: 400,
012f0f49
DM
204 items: [
205 {
206 xtype: 'textfield',
207 name: 'email',
208 fieldLabel: gettext("Email")
209 }
210 ]
211 },
212 1002: {
ea4f2a79 213 iconCls: 'fa fa-globe',
5f3ec152 214 xtype: 'proxmoxWindowEdit',
012f0f49
DM
215 subdir: 'domain',
216 subject: gettext("Domain"),
f7be608f 217 width: 400,
012f0f49
DM
218 items: [
219 {
220 xtype: 'textfield',
221 name: 'domain',
222 fieldLabel: gettext("Domain")
223 }
224 ]
225 },
226 1008: {
ea4f2a79 227 iconCls: 'fa fa-globe',
5f3ec152 228 xtype: 'proxmoxWindowEdit',
012f0f49
DM
229 subdir: 'receiver_domain',
230 subject: gettext("Domain"),
f7be608f
DM
231 receivertest: true,
232 width: 400,
012f0f49
DM
233 items: [
234 {
235 xtype: 'textfield',
236 name: 'domain',
237 fieldLabel: gettext("Domain")
238 }
239 ]
240 },
241 1003: {
ea4f2a79 242 iconCls: 'fa fa-globe',
5f3ec152 243 xtype: 'proxmoxWindowEdit',
012f0f49
DM
244 subdir: 'ip',
245 subject: gettext("IP Address"),
f7be608f 246 width: 400,
012f0f49
DM
247 items: [
248 {
249 xtype: 'textfield',
250 name: 'ip',
251 fieldLabel: gettext("IP Address")
252 }
253 ]
254 },
255 1004: {
ea4f2a79 256 iconCls: 'fa fa-globe',
5f3ec152 257 xtype: 'proxmoxWindowEdit',
012f0f49
DM
258 subdir: 'network',
259 subject: gettext("IP Network"),
f7be608f 260 width: 400,
012f0f49
DM
261 items: [
262 {
263 xtype: 'textfield',
264 name: 'cidr',
265 fieldLabel: gettext("IP Network")
266 }
267 ]
b4eee4f7
DM
268 },
269 2000: {
ea4f2a79 270 iconCls: 'fa fa-clock-o',
5f3ec152 271 xtype: 'proxmoxWindowEdit',
b4eee4f7
DM
272 subdir: 'timeframe',
273 subject: gettext("TimeFrame"),
274 items: [
275 {
276 xtype: 'timefield',
277 name: 'start',
278 format: 'H:i',
279 fieldLabel: gettext("Start Time")
280 },
281 {
282 xtype: 'timefield',
283 name: 'end',
284 format: 'H:i',
285 fieldLabel: gettext("End Time")
286 }
287 ]
f5de8682 288 },
bed64e74 289 3000: {
ea4f2a79 290 iconCls: 'fa fa-bullhorn',
bed64e74
DC
291 xtype: 'proxmoxWindowEdit',
292 subdir: 'spamfilter',
293 subject: gettext('Spam Filter'),
294 items: [
295 {
296 xtype: 'proxmoxintegerfield',
297 name: 'spamlevel',
298 allowBlank: false,
299 minValue: 0,
300 fieldLabel: gettext('Level')
cc6b80b6 301 }
bed64e74
DC
302 ]
303 },
8ddd9c44 304 3001: {
ea4f2a79 305 iconCls: 'fa fa-bug',
8ddd9c44
DC
306 xtype: 'proxmoxWindowEdit',
307 subdir: 'virusfilter',
308 subject: gettext('Virus Filter'),
309 uneditable: true,
310 // there are no parameters to give, so we simply submit it
311 listeners: {
312 show: function(win) {
313 win.submit();
314 }
315 }
316 },
3a9b95a7 317 3002: {
ea4f2a79 318 iconCls: 'fa fa-code',
3a9b95a7
DC
319 xtype: 'proxmoxWindowEdit',
320 subdir: 'matchfield',
321 subject: gettext('Match Field'),
50fa6520 322 width: 400,
3a9b95a7
DC
323 items: [
324 {
325 xtype: 'textfield',
326 name: 'field',
50fa6520 327 labelWidth: 150,
3a9b95a7
DC
328 allowBlank: false,
329 fieldLabel: gettext('Field')
330 },
331 {
332 xtype: 'textfield',
50fa6520 333 reference: 'value',
3a9b95a7 334 name: 'value',
50fa6520 335 labelWidth: 150,
3a9b95a7
DC
336 allowBlank: false,
337 fieldLabel: gettext('Value')
338 },
50fa6520
DC
339 {
340 labelWidth: 150,
341 fieldLabel: gettext('Test String'),
342 xtype: 'pmgRegexTester',
cc6b80b6 343 regexFieldReference: 'value'
50fa6520 344 }
3a9b95a7
DC
345 ]
346 },
347 3003: {
ea4f2a79 348 iconCls: 'fa fa-file-image-o',
3a9b95a7
DC
349 xtype: 'proxmoxWindowEdit',
350 subdir: 'contenttype',
351 width: 400,
3fb5067f 352 subject: gettext('Content Type Filter'),
3a9b95a7
DC
353 items: [
354 {
355 xtype: 'combobox',
356 displayField: 'text',
357 labelWidth: 150,
358 valueField: 'mimetype',
359 name: 'contenttype',
360 editable: true,
361 queryMode: 'local',
362 store: {
363 autoLoad: true,
364 proxy: {
365 type: 'proxmox',
366 url: '/api2/json/config/mimetypes'
cc6b80b6 367 }
3a9b95a7
DC
368 },
369 fieldLabel: gettext('Content Type'),
370 anyMatch: true,
371 matchFieldWidth: false,
372 listeners: {
373 change: function(cb, value) {
374 var me = this;
375 me.up().down('displayfield').setValue(value);
376 }
377 }
378 },
379 {
380 xtype: 'displayfield',
381 fieldLabel: gettext('Value'),
382 labelWidth: 150,
383 allowBlank: false,
3fb5067f 384 reset: Ext.emptyFn
cc6b80b6 385 }
3a9b95a7
DC
386 ]
387 },
6af39772 388 3004: {
ea4f2a79 389 iconCls: 'fa fa-file-o',
6af39772
DC
390 xtype: 'proxmoxWindowEdit',
391 subdir: 'filenamefilter',
392 width: 400,
393 subject: gettext('Match Filename'),
394 items: [
395 {
396 xtype: 'textfield',
397 name: 'filename',
398 reference: 'filename',
399 fieldLabel: gettext('Filename'),
400 labelWidth: 150,
401 allowBlank: false
402 },
403 {
404 labelWidth: 150,
405 fieldLabel: gettext('Test String'),
406 wholeMatch: true,
407 xtype: 'pmgRegexTester',
408 regexFieldReference: 'filename'
409 }
410 ]
411 },
e234e99d 412 3005: {
ea4f2a79 413 iconCls: 'fa fa-file-archive-o',
e234e99d
DC
414 xtype: 'proxmoxWindowEdit',
415 subdir: 'archivefilter',
416 width: 400,
417 subject: gettext('Archive Filter'),
418 items: [
419 {
420 xtype: 'combobox',
421 displayField: 'text',
422 labelWidth: 150,
423 valueField: 'mimetype',
424 name: 'contenttype',
425 editable: true,
426 queryMode: 'local',
427 store: {
428 autoLoad: true,
429 proxy: {
430 type: 'proxmox',
431 url: '/api2/json/config/mimetypes'
cc6b80b6 432 }
e234e99d
DC
433 },
434 fieldLabel: gettext('Content Type'),
435 anyMatch: true,
436 matchFieldWidth: false,
437 listeners: {
438 change: function(cb, value) {
439 var me = this;
440 me.up().down('displayfield').setValue(value);
441 }
442 }
443 },
444 {
445 xtype: 'displayfield',
446 fieldLabel: gettext('Value'),
447 labelWidth: 150,
448 allowBlank: false,
449 reset: Ext.emptyFn
450 }
451 ]
452 },
f5de8682 453 4005: {
5f3ec152 454 xtype: 'proxmoxWindowEdit',
f5de8682
DM
455 subdir: 'bcc',
456 subject: gettext('BCC'),
457 width: 400,
458 items: [
459 {
460 xtype: 'textfield',
461 name: 'name',
462 allowBlank: false,
463 fieldLabel: gettext('Name')
464 },
465 {
466 xtype: 'textareafield',
467 name: 'info',
468 fieldLabel: gettext("Description")
469 },
470 {
471 xtype: 'textfield',
472 name: 'target',
473 allowBlank: false,
474 fieldLabel: gettext("Target")
475 },
476 {
477 xtype: 'proxmoxcheckbox',
478 checked: true,
479 name: 'original',
480 fieldLabel: gettext("send orig. Mail")
481 }
482 ]
483
012f0f49
DM
484 }
485 },
ad834b6f 486
e653eb82
DM
487 openVNCViewer: function(consoletype, nodename) {
488 var url = Ext.urlEncode({
489 console: consoletype, // upgrade or shell
490 novnc: 1,
491 node: nodename
492 });
493 var nw = window.open("?" + url, '_blank',
494 "innerWidth=745,innerheight=427");
495 nw.focus();
496 },
497
f1ab2a14
DC
498 updateLoginData: function(data) {
499 Proxmox.CSRFPreventionToken = data.CSRFPreventionToken;
500 Proxmox.UserName = data.username;
501 Ext.util.Cookies.set('PMGAuthCookie', data.ticket, null, '/', null, true );
502 },
503
258d48b5
DM
504 quarantineActionExtracted: false,
505
506 extractQuarantineAction: function() {
507
cc6b80b6 508 if (PMG.Utils.quarantineActionExtracted) { return; }
258d48b5
DM
509
510 PMG.Utils.quarantineActionExtracted = true;
511
512 var qs = Ext.Object.fromQueryString(location.search);
513
514 var cselect = qs.cselect;
515 var action = qs.action;
516 var ticket = qs.ticket;
517
518 delete qs.cselect;
519 delete qs.action;
520 delete qs.ticket;
521
522 var newsearch = Ext.Object.toQueryString(qs);
523
524 var newurl = location.protocol + "//" + location.host + location.pathname;
cc6b80b6 525 if (newsearch) { newurl += '?' + newsearch; }
258d48b5
DM
526 newurl += location.hash;
527
528 if (window.history) {
529 window.history.pushState({ path:newurl }, '', newurl);
530 }
531
207471c0 532 if (action || cselect) {
258d48b5
DM
533 return { action: action, cselect: cselect };
534 }
535 },
536
cf5268ca
DC
537 doQuarantineAction: function(action, id, callback) {
538 Proxmox.Utils.API2Request({
539 url: '/quarantine/content/',
540 params: {
541 action: action,
542 id: id
543 },
544 method: 'POST',
545 failure: function(response, opts) {
546 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
547 },
548 success: function(response, opts) {
2fa0b505
DC
549 var win = Ext.create('Ext.window.MessageBox',{
550 closeAction: 'destroy'
551 }).show({
cf5268ca
DC
552 title: gettext('Info'),
553 message: "Action '" + action + ' ' +
554 id + "' successful",
555 buttons: Ext.Msg.OK,
556 icon: Ext.MessageBox.INFO
557 });
558
559 if (Ext.isFunction(callback)) {
560 callback();
561 }
562 }
563 });
564 },
565
566 sender_renderer: function(value, metaData, rec) {
567 var subject = Ext.htmlEncode(value);
568 var from = Ext.htmlEncode(rec.data.from);
569 var sender = Ext.htmlEncode(rec.data.sender);
570 if (sender) {
571 from = Ext.String.format(gettext("{0} on behalf of {1}"),
572 sender, from);
573 }
574 return '<small>' + from + '</small><br>' + subject;
575 },
576
fe81f069
DM
577 constructor: function() {
578 var me = this;
fe81f069 579
2c7b542d 580 // do whatever you want here
fe81f069
DM
581 }
582});