]> git.proxmox.com Git - pmg-gui.git/blob - js/Utils.js
reword the action success message to not show the id
[pmg-gui.git] / js / Utils.js
1 /*global Proxmox */
2 Ext.ns('PMG');
3
4 console.log("Starting PMG Manager");
5
6
7 Ext.define('PMG.Utils', {
8 singleton: true,
9
10 // this singleton contains miscellaneous utilities
11
12 senderText: gettext('Sender'),
13 receiverText: gettext('Receiver'),
14 scoreText: gettext('Score'),
15
16 user_role_text: {
17 root: gettext('Superuser'),
18 admin: gettext('Administrator'),
19 helpdesk: gettext('Help Desk'),
20 qmanager: gettext('Quarantine Manager'),
21 audit: gettext('Auditor')
22 },
23
24 format_user_role: function(role) {
25 return PMG.Utils.user_role_text[role] || role;
26 },
27
28 oclass_text: {
29 who: gettext('Who Objects'),
30 what: gettext('What Objects'),
31 when: gettext('When Objects'),
32 action: gettext('Action Objects'),
33 from: gettext('From'),
34 to: gettext('To')
35 },
36
37 oclass_icon: {
38 who: '<span class="fa fa-fw fa-user-circle"></span> ',
39 what: '<span class="fa fa-fw fa-cube"></span> ',
40 when: '<span class="fa fa-fw fa-clock-o"></span> ',
41 action: '<span class="fa fa-fw fa-flag"></span> ',
42 from: '<span class="fa fa-fw fa-user-circle"></span> ',
43 to: '<span class="fa fa-fw fa-user-circle"></span> '
44 },
45
46 mail_status_map: {
47 2: 'delivered',
48 4: 'deferred',
49 5: 'bounced',
50 N: 'rejected',
51 G: 'greylisted',
52 A: 'accepted',
53 B: 'blocked',
54 Q: 'quarantine'
55 },
56
57 icon_status_map_class: {
58 2: 'check-circle',
59 4: 'clock-o',
60 5: 'mail-reply',
61 N: 'times-circle',
62 G: 'list',
63 A: 'check',
64 B: 'ban',
65 Q: 'cube'
66 },
67
68 icon_status_map_color: {
69 2: 'green',
70 5: 'gray',
71 A: 'green',
72 B: 'red'
73 },
74
75 format_status_icon: function(status) {
76 var icon = PMG.Utils.icon_status_map_class[status] || 'question-circle';
77 var color = PMG.Utils.icon_status_map_color[status] || '';
78 return '<i class="fa fa-' + icon + ' ' + color + '"></i> ';
79 },
80
81 format_oclass: function(oclass) {
82 var icon = PMG.Utils.oclass_icon[oclass] || '';
83 var text = PMG.Utils.oclass_text[oclass] || oclass;
84 return icon + text;
85 },
86
87 rule_direction_text: {
88 0: gettext('In'),
89 1: gettext('Out'),
90 2: gettext('In & Out')
91 },
92
93 rule_direction_icon: {
94 0: '<span class="fa fa-fw fa-long-arrow-left"></span> ',
95 1: '<span class="fa fa-fw fa-long-arrow-right"></span> ',
96 2: '<span class="fa fa-fw fa-exchange"></span> '
97 },
98
99 format_rule_direction: function(dir) {
100 var icon = PMG.Utils.rule_direction_icon[dir] || '';
101 var text = PMG.Utils.rule_direction_text[dir] || dir;
102 return icon + text;
103 },
104
105 format_otype: function(otype) {
106 var editor = PMG.Utils.object_editors[otype];
107 var iconCls = 'fa fa-question-circle';
108 if (editor) {
109 var icon = '<span class="fa-fw ' + (editor.iconCls || iconCls) + '"></span> ';
110 return icon + editor.subject;
111 }
112
113 return '<span class="fa-fw ' + iconCls + '"></span> unknown';
114 },
115
116 format_ldap_protocol: function(p) {
117 if (p === undefined) { return 'LDAP'; }
118 if (p === 'ldap') { return 'LDAP'; }
119 if (p === 'ldaps') { return 'LDAPS'; }
120 return 'unknown';
121 },
122
123 convert_field_to_per_min: function(value, record) {
124 return (value/(record.data.timespan/60));
125 },
126
127 object_editors: {
128 1000: {
129 iconCls: 'fa fa-filter',
130 xtype: 'proxmoxWindowEdit',
131 subdir: 'regex',
132 subject: gettext("Regular Expression"),
133 width: 400,
134 items: [
135 {
136 xtype: 'textfield',
137 name: 'regex',
138 labelWidth: 150,
139 reference: 'regex',
140 fieldLabel: gettext("Regular Expression")
141 },
142 {
143 labelWidth: 150,
144 fieldLabel: gettext('Test String'),
145 xtype: 'pmgRegexTester',
146 regexFieldReference: 'regex'
147 }
148 ]
149 },
150 1005: {
151 iconCls: 'fa fa-users',
152 xtype: 'pmgLDAPGroupEditor',
153 subdir: 'ldap',
154 subject: gettext("LDAP Group")
155 },
156 1006: {
157 iconCls: 'fa fa-user',
158 xtype: 'pmgLDAPUserEditor',
159 subdir: 'ldapuser',
160 subject: gettext("LDAP User")
161 },
162 1009: {
163 iconCls: 'fa fa-filter',
164 xtype: 'proxmoxWindowEdit',
165 subdir: 'receiver_regex',
166 subject: gettext("Regular Expression"),
167 receivertest: true,
168 width: 400,
169 items: [
170 {
171 xtype: 'textfield',
172 name: 'regex',
173 labelWidth: 150,
174 fieldLabel: gettext("Regular Expression")
175 }
176 ]
177 },
178 1001: {
179 iconCls: 'fa fa-envelope-o',
180 xtype: 'proxmoxWindowEdit',
181 subdir: 'email',
182 subject: gettext("E-Mail"),
183 width: 400,
184 items: [
185 {
186 xtype: 'textfield',
187 name: 'email',
188 fieldLabel: gettext("E-Mail")
189 }
190 ]
191 },
192 1007: {
193 iconCls: 'fa fa-envelope-o',
194 xtype: 'proxmoxWindowEdit',
195 subdir: 'receiver',
196 subject: gettext("E-Mail"),
197 receivertest: true,
198 width: 400,
199 items: [
200 {
201 xtype: 'textfield',
202 name: 'email',
203 fieldLabel: gettext("E-Mail")
204 }
205 ]
206 },
207 1002: {
208 iconCls: 'fa fa-globe',
209 xtype: 'proxmoxWindowEdit',
210 subdir: 'domain',
211 subject: gettext("Domain"),
212 width: 400,
213 items: [
214 {
215 xtype: 'textfield',
216 name: 'domain',
217 fieldLabel: gettext("Domain")
218 }
219 ]
220 },
221 1008: {
222 iconCls: 'fa fa-globe',
223 xtype: 'proxmoxWindowEdit',
224 subdir: 'receiver_domain',
225 subject: gettext("Domain"),
226 receivertest: true,
227 width: 400,
228 items: [
229 {
230 xtype: 'textfield',
231 name: 'domain',
232 fieldLabel: gettext("Domain")
233 }
234 ]
235 },
236 1003: {
237 iconCls: 'fa fa-globe',
238 xtype: 'proxmoxWindowEdit',
239 subdir: 'ip',
240 subject: gettext("IP Address"),
241 width: 400,
242 items: [
243 {
244 xtype: 'textfield',
245 name: 'ip',
246 fieldLabel: gettext("IP Address")
247 }
248 ]
249 },
250 1004: {
251 iconCls: 'fa fa-globe',
252 xtype: 'proxmoxWindowEdit',
253 subdir: 'network',
254 subject: gettext("IP Network"),
255 width: 400,
256 items: [
257 {
258 xtype: 'textfield',
259 name: 'cidr',
260 fieldLabel: gettext("IP Network")
261 }
262 ]
263 },
264 2000: {
265 iconCls: 'fa fa-clock-o',
266 xtype: 'proxmoxWindowEdit',
267 subdir: 'timeframe',
268 subject: gettext("TimeFrame"),
269 items: [
270 {
271 xtype: 'timefield',
272 name: 'start',
273 format: 'H:i',
274 fieldLabel: gettext("Start Time")
275 },
276 {
277 xtype: 'timefield',
278 name: 'end',
279 format: 'H:i',
280 fieldLabel: gettext("End Time")
281 }
282 ]
283 },
284 3000: {
285 iconCls: 'fa fa-bullhorn',
286 xtype: 'proxmoxWindowEdit',
287 subdir: 'spamfilter',
288 subject: gettext('Spam Filter'),
289 items: [
290 {
291 xtype: 'proxmoxintegerfield',
292 name: 'spamlevel',
293 allowBlank: false,
294 minValue: 0,
295 fieldLabel: gettext('Level')
296 }
297 ]
298 },
299 3001: {
300 iconCls: 'fa fa-bug',
301 xtype: 'proxmoxWindowEdit',
302 subdir: 'virusfilter',
303 subject: gettext('Virus Filter'),
304 uneditable: true,
305 // there are no parameters to give, so we simply submit it
306 listeners: {
307 show: function(win) {
308 win.submit();
309 }
310 }
311 },
312 3002: {
313 iconCls: 'fa fa-code',
314 xtype: 'proxmoxWindowEdit',
315 subdir: 'matchfield',
316 subject: gettext('Match Field'),
317 width: 400,
318 items: [
319 {
320 xtype: 'textfield',
321 name: 'field',
322 labelWidth: 150,
323 allowBlank: false,
324 fieldLabel: gettext('Field')
325 },
326 {
327 xtype: 'textfield',
328 reference: 'value',
329 name: 'value',
330 labelWidth: 150,
331 allowBlank: false,
332 fieldLabel: gettext('Value')
333 },
334 {
335 labelWidth: 150,
336 fieldLabel: gettext('Test String'),
337 xtype: 'pmgRegexTester',
338 regexFieldReference: 'value'
339 }
340 ]
341 },
342 3003: {
343 iconCls: 'fa fa-file-image-o',
344 xtype: 'proxmoxWindowEdit',
345 subdir: 'contenttype',
346 width: 400,
347 subject: gettext('Content Type Filter'),
348 items: [
349 {
350 xtype: 'combobox',
351 displayField: 'text',
352 labelWidth: 150,
353 valueField: 'mimetype',
354 name: 'contenttype',
355 editable: true,
356 queryMode: 'local',
357 store: {
358 autoLoad: true,
359 proxy: {
360 type: 'proxmox',
361 url: '/api2/json/config/mimetypes'
362 }
363 },
364 fieldLabel: gettext('Content Type'),
365 anyMatch: true,
366 matchFieldWidth: false,
367 listeners: {
368 change: function(cb, value) {
369 var me = this;
370 me.up().down('displayfield').setValue(value);
371 }
372 }
373 },
374 {
375 xtype: 'displayfield',
376 fieldLabel: gettext('Value'),
377 labelWidth: 150,
378 allowBlank: false,
379 reset: Ext.emptyFn
380 }
381 ]
382 },
383 3004: {
384 iconCls: 'fa fa-file-o',
385 xtype: 'proxmoxWindowEdit',
386 subdir: 'filenamefilter',
387 width: 400,
388 subject: gettext('Match Filename'),
389 items: [
390 {
391 xtype: 'textfield',
392 name: 'filename',
393 reference: 'filename',
394 fieldLabel: gettext('Filename'),
395 labelWidth: 150,
396 allowBlank: false
397 },
398 {
399 labelWidth: 150,
400 fieldLabel: gettext('Test String'),
401 wholeMatch: true,
402 xtype: 'pmgRegexTester',
403 regexFieldReference: 'filename'
404 }
405 ]
406 },
407 3005: {
408 iconCls: 'fa fa-file-archive-o',
409 xtype: 'proxmoxWindowEdit',
410 subdir: 'archivefilter',
411 width: 400,
412 subject: gettext('Archive Filter'),
413 items: [
414 {
415 xtype: 'combobox',
416 displayField: 'text',
417 labelWidth: 150,
418 valueField: 'mimetype',
419 name: 'contenttype',
420 editable: true,
421 queryMode: 'local',
422 store: {
423 autoLoad: true,
424 proxy: {
425 type: 'proxmox',
426 url: '/api2/json/config/mimetypes'
427 }
428 },
429 fieldLabel: gettext('Content Type'),
430 anyMatch: true,
431 matchFieldWidth: false,
432 listeners: {
433 change: function(cb, value) {
434 var me = this;
435 me.up().down('displayfield').setValue(value);
436 }
437 }
438 },
439 {
440 xtype: 'displayfield',
441 fieldLabel: gettext('Value'),
442 labelWidth: 150,
443 allowBlank: false,
444 reset: Ext.emptyFn
445 }
446 ]
447 },
448 4002: {
449 xtype: 'proxmoxWindowEdit',
450 subdir: 'notification',
451 subject: gettext('Notification'),
452 width: 400,
453 items: [
454 {
455 xtype: 'textfield',
456 name: 'name',
457 allowBlank: false,
458 fieldLabel: gettext('Name')
459 },
460 {
461 xtype: 'textareafield',
462 name: 'info',
463 fieldLabel: gettext("Description")
464 },
465 {
466 xtype: 'textfield',
467 name: 'to',
468 allowBlank: false,
469 value: '__ADMIN__',
470 fieldLabel: gettext('Receiver')
471 },
472 {
473 xtype: 'textfield',
474 name: 'subject',
475 allowBlank: false,
476 value: 'Notification: __SUBJECT__',
477 fieldLabel: gettext('Subject')
478 },
479 {
480 xtype: 'textarea',
481 name: 'body',
482 allowBlank: false,
483 grow: true,
484 growMax: 250,
485 value:
486 "Proxmox Notifcation:\n\n" +
487 "Sender: __SENDER__\n" +
488 "Receiver: __RECEIVERS__\n" +
489 "Targets: __TARGETS__\n\n" +
490 "Subject: __SUBJECT__\n\n" +
491 "Matching Rule: __RULE__\n\n" +
492 "__RULE_INFO__\n\n" +
493 "__VIRUS_INFO__\n" +
494 "__SPAM_INFO__\n",
495 fieldLabel: gettext('Body')
496 },
497 {
498 xtype: 'proxmoxcheckbox',
499 name: 'attach',
500 fieldLabel: gettext("Attach orig. Mail")
501 }
502 ]
503 },
504 4003: {
505 xtype: 'proxmoxWindowEdit',
506 subdir: 'field',
507 subject: gettext('Header Attribute'),
508 width: 400,
509 items: [
510 {
511 xtype: 'textfield',
512 name: 'name',
513 allowBlank: false,
514 fieldLabel: gettext('Name')
515 },
516 {
517 xtype: 'textareafield',
518 name: 'info',
519 fieldLabel: gettext("Description")
520 },
521 {
522 xtype: 'textfield',
523 name: 'field',
524 allowBlank: false,
525 fieldLabel: gettext('Field')
526 },
527 {
528 xtype: 'textfield',
529 reference: 'value',
530 name: 'value',
531 allowBlank: false,
532 fieldLabel: gettext('Value')
533 }
534 ]
535 },
536 4005: {
537 xtype: 'proxmoxWindowEdit',
538 subdir: 'bcc',
539 subject: gettext('BCC'),
540 width: 400,
541 items: [
542 {
543 xtype: 'textfield',
544 name: 'name',
545 allowBlank: false,
546 fieldLabel: gettext('Name')
547 },
548 {
549 xtype: 'textareafield',
550 name: 'info',
551 fieldLabel: gettext("Description")
552 },
553 {
554 xtype: 'textfield',
555 name: 'target',
556 allowBlank: false,
557 fieldLabel: gettext("Target")
558 },
559 {
560 xtype: 'proxmoxcheckbox',
561 checked: true,
562 name: 'original',
563 fieldLabel: gettext("send orig. Mail")
564 }
565 ]
566
567 },
568 4007: {
569 xtype: 'proxmoxWindowEdit',
570 subdir: 'removeattachments',
571 subject: gettext('Remove Attachments'),
572 width: 500,
573 fieldDefaults: {
574 labelWidth: 150
575 },
576 items: [
577 {
578 xtype: 'textfield',
579 name: 'name',
580 allowBlank: false,
581 fieldLabel: gettext('Name')
582 },
583 {
584 xtype: 'textareafield',
585 name: 'info',
586 fieldLabel: gettext("Description")
587 },
588 {
589 xtype: 'textareafield',
590 name: 'text',
591 grow: true,
592 growMax: 250,
593 fieldLabel: gettext("Text Replacement")
594 },
595 {
596 xtype: 'proxmoxcheckbox',
597 checked: true,
598 name: 'all',
599 fieldLabel: gettext("Remove all attachments")
600 }
601 ]
602 },
603 4009: {
604 xtype: 'proxmoxWindowEdit',
605 subdir: 'disclaimer',
606 subject: gettext('Disclaimer'),
607 width: 400,
608 items: [
609 {
610 xtype: 'textfield',
611 name: 'name',
612 allowBlank: false,
613 fieldLabel: gettext('Name')
614 },
615 {
616 xtype: 'textareafield',
617 name: 'info',
618 fieldLabel: gettext("Description")
619 },
620 {
621 xtype: 'textareafield',
622 name: 'disclaimer',
623 grow: true,
624 growMax: 250,
625 fieldLabel: gettext("Disclaimer")
626 }
627 ]
628 }
629 },
630
631 updateLoginData: function(data) {
632 Proxmox.CSRFPreventionToken = data.CSRFPreventionToken;
633 Proxmox.UserName = data.username;
634 Ext.util.Cookies.set('PMGAuthCookie', data.ticket, null, '/', null, true );
635 },
636
637 quarantineActionExtracted: false,
638
639 extractQuarantineAction: function() {
640
641 if (PMG.Utils.quarantineActionExtracted) { return; }
642
643 PMG.Utils.quarantineActionExtracted = true;
644
645 var qs = Ext.Object.fromQueryString(location.search);
646
647 var cselect = qs.cselect;
648 var action = qs.action;
649 var ticket = qs.ticket;
650 var dateString = qs.date;
651
652 if (dateString) {
653 var date = new Date(dateString).getTime()/1000;
654
655 // set from date for QuarantineList
656 /*jslint confusion: true*/
657 /*from is a string above and number here */
658 PMG.QuarantineList.from = date;
659 /*jslint confusion: false*/
660 }
661
662 delete qs.cselect;
663 delete qs.action;
664 delete qs.ticket;
665 delete qs.date;
666
667 var newsearch = Ext.Object.toQueryString(qs);
668
669 var newurl = location.protocol + "//" + location.host + location.pathname;
670 if (newsearch) { newurl += '?' + newsearch; }
671 newurl += location.hash;
672
673 if (window.history) {
674 window.history.pushState({ path:newurl }, '', newurl);
675 }
676
677 if (action || cselect) {
678 return { action: action, cselect: cselect };
679 }
680 },
681
682 doQuarantineAction: function(action, id, callback) {
683 var count = id.split(';').length;
684 var successMessage = "Action '{0}'";
685 if (count > 1) {
686 successMessage += " for '{1}' items";
687 }
688 successMessage += " successful";
689
690 /*jslint confusion: true*/
691 /*format is string and function*/
692 Proxmox.Utils.API2Request({
693 url: '/quarantine/content/',
694 params: {
695 action: action,
696 id: id
697 },
698 method: 'POST',
699 failure: function(response, opts) {
700 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
701 },
702 success: function(response, opts) {
703 var win = Ext.create('Ext.window.MessageBox',{
704 closeAction: 'destroy'
705 }).show({
706 title: gettext('Info'),
707 message: Ext.String.format(successMessage, action, count),
708 buttons: Ext.Msg.OK,
709 icon: Ext.MessageBox.INFO
710 });
711
712 if (Ext.isFunction(callback)) {
713 callback();
714 }
715 }
716 });
717 /*jslint confusion: false*/
718 },
719
720 sender_renderer: function(value, metaData, rec) {
721 var subject = Ext.htmlEncode(value);
722 var from = Ext.htmlEncode(rec.data.from);
723 var sender = Ext.htmlEncode(rec.data.sender);
724 if (sender) {
725 /*jslint confusion: true*/
726 /*format is a string above*/
727 from = Ext.String.format(gettext("{0} on behalf of {1}"),
728 sender, from);
729 /*jslint confusion: false*/
730 }
731 return '<small>' + from + '</small><br>' + subject;
732 },
733
734 constructor: function() {
735 var me = this;
736
737 // do whatever you want here
738 }
739 });