]> git.proxmox.com Git - pmg-gui.git/blob - js/Utils.js
ActionList: make labels in editwindow consistent
[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 if (p === 'ldap+starttls') { return 'LDAP+STARTTLS'; }
121 return 'unknown';
122 },
123
124 convert_field_to_per_min: function(value, record) {
125 return (value/(record.data.timespan/60));
126 },
127
128 object_editors: {
129 1000: {
130 onlineHelp: 'pmg_mailfilter_regex',
131 iconCls: 'fa fa-filter',
132 xtype: 'proxmoxWindowEdit',
133 subdir: 'regex',
134 subject: gettext("Regular Expression"),
135 width: 400,
136 items: [
137 {
138 xtype: 'textfield',
139 name: 'regex',
140 labelWidth: 150,
141 reference: 'regex',
142 fieldLabel: gettext("Regular Expression")
143 },
144 {
145 labelWidth: 150,
146 fieldLabel: gettext('Test String'),
147 xtype: 'pmgRegexTester',
148 wholeMatch: true,
149 regexFieldReference: 'regex'
150 }
151 ]
152 },
153 1005: {
154 onlineHelp: 'pmgconfig_ldap',
155 iconCls: 'fa fa-users',
156 xtype: 'pmgLDAPGroupEditor',
157 subdir: 'ldap',
158 subject: gettext("LDAP Group")
159 },
160 1006: {
161 onlineHelp: 'pmgconfig_ldap',
162 iconCls: 'fa fa-user',
163 xtype: 'pmgLDAPUserEditor',
164 subdir: 'ldapuser',
165 subject: gettext("LDAP User")
166 },
167 1009: {
168 onlineHelp: 'pmg_mailfilter_regex',
169 iconCls: 'fa fa-filter',
170 xtype: 'proxmoxWindowEdit',
171 subdir: 'receiver_regex',
172 subject: gettext("Regular Expression"),
173 receivertest: true,
174 width: 400,
175 items: [
176 {
177 xtype: 'textfield',
178 name: 'regex',
179 labelWidth: 150,
180 fieldLabel: gettext("Regular Expression")
181 }
182 ]
183 },
184 1001: {
185 onlineHelp: 'pmg_mailfilter_who',
186 iconCls: 'fa fa-envelope-o',
187 xtype: 'proxmoxWindowEdit',
188 subdir: 'email',
189 subject: gettext("E-Mail"),
190 width: 400,
191 items: [
192 {
193 xtype: 'textfield',
194 name: 'email',
195 fieldLabel: gettext("E-Mail")
196 }
197 ]
198 },
199 1007: {
200 onlineHelp: 'pmg_mailfilter_who',
201 iconCls: 'fa fa-envelope-o',
202 xtype: 'proxmoxWindowEdit',
203 subdir: 'receiver',
204 subject: gettext("E-Mail"),
205 receivertest: true,
206 width: 400,
207 items: [
208 {
209 xtype: 'textfield',
210 name: 'email',
211 fieldLabel: gettext("E-Mail")
212 }
213 ]
214 },
215 1002: {
216 onlineHelp: 'pmg_mailfilter_who',
217 iconCls: 'fa fa-globe',
218 xtype: 'proxmoxWindowEdit',
219 subdir: 'domain',
220 subject: gettext("Domain"),
221 width: 400,
222 items: [
223 {
224 xtype: 'textfield',
225 name: 'domain',
226 fieldLabel: gettext("Domain")
227 }
228 ]
229 },
230 1008: {
231 onlineHelp: 'pmg_mailfilter_who',
232 iconCls: 'fa fa-globe',
233 xtype: 'proxmoxWindowEdit',
234 subdir: 'receiver_domain',
235 subject: gettext("Domain"),
236 receivertest: true,
237 width: 400,
238 items: [
239 {
240 xtype: 'textfield',
241 name: 'domain',
242 fieldLabel: gettext("Domain")
243 }
244 ]
245 },
246 1003: {
247 onlineHelp: 'pmg_mailfilter_who',
248 iconCls: 'fa fa-globe',
249 xtype: 'proxmoxWindowEdit',
250 subdir: 'ip',
251 subject: gettext("IP Address"),
252 width: 400,
253 items: [
254 {
255 xtype: 'textfield',
256 name: 'ip',
257 fieldLabel: gettext("IP Address")
258 }
259 ]
260 },
261 1004: {
262 onlineHelp: 'pmg_mailfilter_who',
263 iconCls: 'fa fa-globe',
264 xtype: 'proxmoxWindowEdit',
265 subdir: 'network',
266 subject: gettext("IP Network"),
267 width: 400,
268 items: [
269 {
270 xtype: 'textfield',
271 name: 'cidr',
272 fieldLabel: gettext("IP Network")
273 }
274 ]
275 },
276 2000: {
277 onlineHelp: 'pmg_mailfilter_when',
278 iconCls: 'fa fa-clock-o',
279 xtype: 'proxmoxWindowEdit',
280 subdir: 'timeframe',
281 subject: gettext("TimeFrame"),
282 items: [
283 {
284 xtype: 'timefield',
285 name: 'start',
286 format: 'H:i',
287 fieldLabel: gettext("Start Time")
288 },
289 {
290 xtype: 'timefield',
291 name: 'end',
292 format: 'H:i',
293 fieldLabel: gettext("End Time")
294 }
295 ]
296 },
297 3000: {
298 onlineHelp: 'pmg_mailfilter_what',
299 iconCls: 'fa fa-bullhorn',
300 xtype: 'proxmoxWindowEdit',
301 subdir: 'spamfilter',
302 subject: gettext('Spam Filter'),
303 items: [
304 {
305 xtype: 'proxmoxintegerfield',
306 name: 'spamlevel',
307 allowBlank: false,
308 minValue: 0,
309 fieldLabel: gettext('Level')
310 }
311 ]
312 },
313 3001: {
314 onlineHelp: 'pmg_mailfilter_what',
315 iconCls: 'fa fa-bug',
316 xtype: 'proxmoxWindowEdit',
317 subdir: 'virusfilter',
318 subject: gettext('Virus Filter'),
319 uneditable: true,
320 // there are no parameters to give, so we simply submit it
321 listeners: {
322 show: function(win) {
323 win.submit();
324 }
325 }
326 },
327 3002: {
328 onlineHelp: 'pmg_mailfilter_regex',
329 iconCls: 'fa fa-code',
330 xtype: 'proxmoxWindowEdit',
331 subdir: 'matchfield',
332 subject: gettext('Match Field'),
333 width: 400,
334 items: [
335 {
336 xtype: 'textfield',
337 name: 'field',
338 labelWidth: 150,
339 allowBlank: false,
340 fieldLabel: gettext('Field')
341 },
342 {
343 xtype: 'textfield',
344 reference: 'value',
345 name: 'value',
346 labelWidth: 150,
347 allowBlank: false,
348 fieldLabel: gettext('Value')
349 },
350 {
351 labelWidth: 150,
352 fieldLabel: gettext('Test String'),
353 xtype: 'pmgRegexTester',
354 regexFieldReference: 'value'
355 }
356 ]
357 },
358 3003: {
359 onlineHelp: 'pmg_mailfilter_what',
360 iconCls: 'fa fa-file-image-o',
361 xtype: 'proxmoxWindowEdit',
362 subdir: 'contenttype',
363 width: 400,
364 subject: gettext('Content Type Filter'),
365 items: [
366 {
367 xtype: 'combobox',
368 displayField: 'text',
369 labelWidth: 150,
370 valueField: 'mimetype',
371 name: 'contenttype',
372 editable: true,
373 queryMode: 'local',
374 store: {
375 autoLoad: true,
376 proxy: {
377 type: 'proxmox',
378 url: '/api2/json/config/mimetypes'
379 }
380 },
381 fieldLabel: gettext('Content Type'),
382 anyMatch: true,
383 matchFieldWidth: false,
384 listeners: {
385 change: function(cb, value) {
386 var me = this;
387 me.up().down('displayfield').setValue(value);
388 }
389 }
390 },
391 {
392 xtype: 'displayfield',
393 fieldLabel: gettext('Value'),
394 labelWidth: 150,
395 allowBlank: false,
396 reset: Ext.emptyFn
397 }
398 ]
399 },
400 3004: {
401 onlineHelp: 'pmg_mailfilter_regex',
402 iconCls: 'fa fa-file-o',
403 xtype: 'proxmoxWindowEdit',
404 subdir: 'filenamefilter',
405 width: 400,
406 subject: gettext('Match Filename'),
407 items: [
408 {
409 xtype: 'textfield',
410 name: 'filename',
411 reference: 'filename',
412 fieldLabel: gettext('Filename'),
413 labelWidth: 150,
414 allowBlank: false
415 },
416 {
417 labelWidth: 150,
418 fieldLabel: gettext('Test String'),
419 wholeMatch: true,
420 xtype: 'pmgRegexTester',
421 regexFieldReference: 'filename'
422 }
423 ]
424 },
425 3005: {
426 onlineHelp: 'pmg_mailfilter_what',
427 iconCls: 'fa fa-file-archive-o',
428 xtype: 'proxmoxWindowEdit',
429 subdir: 'archivefilter',
430 width: 400,
431 subject: gettext('Archive Filter'),
432 items: [
433 {
434 xtype: 'combobox',
435 displayField: 'text',
436 labelWidth: 150,
437 valueField: 'mimetype',
438 name: 'contenttype',
439 editable: true,
440 queryMode: 'local',
441 store: {
442 autoLoad: true,
443 proxy: {
444 type: 'proxmox',
445 url: '/api2/json/config/mimetypes'
446 }
447 },
448 fieldLabel: gettext('Content Type'),
449 anyMatch: true,
450 matchFieldWidth: false,
451 listeners: {
452 change: function(cb, value) {
453 var me = this;
454 me.up().down('displayfield').setValue(value);
455 }
456 }
457 },
458 {
459 xtype: 'displayfield',
460 fieldLabel: gettext('Value'),
461 labelWidth: 150,
462 allowBlank: false,
463 reset: Ext.emptyFn
464 }
465 ]
466 },
467 4002: {
468 onlineHelp: 'pmg_mailfilter_action',
469 xtype: 'proxmoxWindowEdit',
470 subdir: 'notification',
471 subject: gettext('Notification'),
472 width: 400,
473 items: [
474 {
475 xtype: 'textfield',
476 name: 'name',
477 allowBlank: false,
478 fieldLabel: gettext('Name')
479 },
480 {
481 xtype: 'textareafield',
482 name: 'info',
483 fieldLabel: gettext("Comment")
484 },
485 {
486 xtype: 'textfield',
487 name: 'to',
488 allowBlank: false,
489 value: '__ADMIN__',
490 fieldLabel: gettext('Receiver')
491 },
492 {
493 xtype: 'textfield',
494 name: 'subject',
495 allowBlank: false,
496 value: 'Notification: __SUBJECT__',
497 fieldLabel: gettext('Subject')
498 },
499 {
500 xtype: 'textarea',
501 name: 'body',
502 allowBlank: false,
503 grow: true,
504 growMax: 250,
505 value:
506 "Proxmox Notifcation:\n\n" +
507 "Sender: __SENDER__\n" +
508 "Receiver: __RECEIVERS__\n" +
509 "Targets: __TARGETS__\n\n" +
510 "Subject: __SUBJECT__\n\n" +
511 "Matching Rule: __RULE__\n\n" +
512 "__RULE_INFO__\n\n" +
513 "__VIRUS_INFO__\n" +
514 "__SPAM_INFO__\n",
515 fieldLabel: gettext('Body')
516 },
517 {
518 xtype: 'proxmoxcheckbox',
519 name: 'attach',
520 fieldLabel: gettext("Attach orig. Mail")
521 }
522 ]
523 },
524 4003: {
525 onlineHelp: 'pmg_mailfilter_action',
526 xtype: 'proxmoxWindowEdit',
527 subdir: 'field',
528 subject: gettext('Header Attribute'),
529 width: 400,
530 items: [
531 {
532 xtype: 'textfield',
533 name: 'name',
534 allowBlank: false,
535 fieldLabel: gettext('Name')
536 },
537 {
538 xtype: 'textareafield',
539 name: 'info',
540 fieldLabel: gettext("Comment")
541 },
542 {
543 xtype: 'textfield',
544 name: 'field',
545 allowBlank: false,
546 fieldLabel: gettext('Field')
547 },
548 {
549 xtype: 'textfield',
550 reference: 'value',
551 name: 'value',
552 allowBlank: false,
553 fieldLabel: gettext('Value')
554 }
555 ]
556 },
557 4005: {
558 onlineHelp: 'pmg_mailfilter_action',
559 xtype: 'proxmoxWindowEdit',
560 subdir: 'bcc',
561 subject: gettext('BCC'),
562 width: 400,
563 items: [
564 {
565 xtype: 'textfield',
566 name: 'name',
567 allowBlank: false,
568 fieldLabel: gettext('Name')
569 },
570 {
571 xtype: 'textareafield',
572 name: 'info',
573 fieldLabel: gettext("Comment")
574 },
575 {
576 xtype: 'textfield',
577 name: 'target',
578 allowBlank: false,
579 fieldLabel: gettext("Target")
580 },
581 {
582 xtype: 'proxmoxcheckbox',
583 checked: true,
584 name: 'original',
585 fieldLabel: gettext("send orig. Mail")
586 }
587 ]
588
589 },
590 4007: {
591 onlineHelp: 'pmg_mailfilter_action',
592 xtype: 'proxmoxWindowEdit',
593 subdir: 'removeattachments',
594 subject: gettext('Remove Attachments'),
595 width: 500,
596 fieldDefaults: {
597 labelWidth: 150
598 },
599 items: [
600 {
601 xtype: 'textfield',
602 name: 'name',
603 allowBlank: false,
604 fieldLabel: gettext('Name')
605 },
606 {
607 xtype: 'textareafield',
608 name: 'info',
609 fieldLabel: gettext("Comment")
610 },
611 {
612 xtype: 'textareafield',
613 name: 'text',
614 grow: true,
615 growMax: 250,
616 fieldLabel: gettext("Text Replacement")
617 },
618 {
619 xtype: 'proxmoxcheckbox',
620 checked: true,
621 name: 'all',
622 fieldLabel: gettext("Remove all attachments")
623 },
624 {
625 xtype: 'proxmoxcheckbox',
626 checked: false,
627 name: 'quarantine',
628 fieldLabel: gettext("Copy orignal mail to Attachment Quarantine")
629 }
630 ]
631 },
632 4009: {
633 onlineHelp: 'pmg_mailfilter_action',
634 xtype: 'proxmoxWindowEdit',
635 subdir: 'disclaimer',
636 subject: gettext('Disclaimer'),
637 width: 400,
638 items: [
639 {
640 xtype: 'textfield',
641 name: 'name',
642 allowBlank: false,
643 fieldLabel: gettext('Name')
644 },
645 {
646 xtype: 'textareafield',
647 name: 'info',
648 fieldLabel: gettext("Comment")
649 },
650 {
651 xtype: 'textareafield',
652 name: 'disclaimer',
653 grow: true,
654 growMax: 250,
655 fieldLabel: gettext("Disclaimer")
656 }
657 ]
658 }
659 },
660
661 updateLoginData: function(data) {
662 Proxmox.CSRFPreventionToken = data.CSRFPreventionToken;
663 Proxmox.UserName = data.username;
664 Ext.util.Cookies.set('PMGAuthCookie', data.ticket, null, '/', null, true );
665 },
666
667 quarantineActionExtracted: false,
668
669 extractQuarantineAction: function() {
670
671 if (PMG.Utils.quarantineActionExtracted) { return; }
672
673 PMG.Utils.quarantineActionExtracted = true;
674
675 var qs = Ext.Object.fromQueryString(location.search);
676
677 var cselect = qs.cselect;
678 var action = qs.action;
679 var ticket = qs.ticket;
680 var dateString = qs.date;
681
682 if (dateString) {
683 var date = new Date(dateString).getTime()/1000;
684
685 // set from date for QuarantineList
686 /*jslint confusion: true*/
687 /*from is a string above and number here */
688 PMG.QuarantineList.from = date;
689 /*jslint confusion: false*/
690 }
691
692 delete qs.cselect;
693 delete qs.action;
694 delete qs.ticket;
695 delete qs.date;
696
697 var newsearch = Ext.Object.toQueryString(qs);
698
699 var newurl = location.protocol + "//" + location.host + location.pathname;
700 if (newsearch) { newurl += '?' + newsearch; }
701 newurl += location.hash;
702
703 if (window.history) {
704 window.history.pushState({ path:newurl }, '', newurl);
705 }
706
707 if (action || cselect) {
708 return { action: action, cselect: cselect };
709 }
710 },
711
712 doQuarantineAction: function(action, id, callback) {
713 var count = id.split(';').length;
714 var successMessage = "Action '{0}'";
715 if (count > 1) {
716 successMessage += " for '{1}' items";
717 }
718 successMessage += " successful";
719
720 /*jslint confusion: true*/
721 /*format is string and function*/
722 Proxmox.Utils.API2Request({
723 url: '/quarantine/content/',
724 params: {
725 action: action,
726 id: id
727 },
728 method: 'POST',
729 failure: function(response, opts) {
730 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
731 },
732 success: function(response, opts) {
733 var win = Ext.create('Ext.window.MessageBox',{
734 closeAction: 'destroy'
735 }).show({
736 title: gettext('Info'),
737 message: Ext.String.format(successMessage, action, count),
738 buttons: Ext.Msg.OK,
739 icon: Ext.MessageBox.INFO
740 });
741
742 if (Ext.isFunction(callback)) {
743 callback();
744 }
745 }
746 });
747 /*jslint confusion: false*/
748 },
749
750 render_filetype: function(value) {
751 let iconCls = 'fa-file-o';
752 let text = Proxmox.Utils.unknownText;
753
754 if (!value) {
755 return `<i class='fa ${iconCls}'></i> ${text}`;
756 }
757
758 text = value.toString().toLowerCase();
759 let type = text.split('/')[0];
760
761 switch (type) {
762 case 'audio':
763 case 'image':
764 case 'video':
765 case 'text':
766 iconCls = `fa-file-${type}-o`;
767 break;
768 case 'application':
769 let subtypes = ['excel', 'pdf', 'word', 'powerpoint'];
770 let found = subtypes.find(st => text.includes(st));
771 if (found !== undefined) {
772 iconCls = `fa-file-${found}-o`;
773 }
774 break;
775 default:
776 break;
777 }
778
779 return `<i class='fa ${iconCls}'></i> ${text}`;
780 },
781
782 sender_renderer: function(value, metaData, rec) {
783 var subject = Ext.htmlEncode(value);
784 var from = Ext.htmlEncode(rec.data.from);
785 var sender = Ext.htmlEncode(rec.data.sender);
786 if (sender) {
787 /*jslint confusion: true*/
788 /*format is a string above*/
789 from = Ext.String.format(gettext("{0} on behalf of {1}"),
790 sender, from);
791 /*jslint confusion: false*/
792 }
793 return '<small>' + from + '</small><br>' + subject;
794 },
795
796 constructor: function() {
797 var me = this;
798
799 // do whatever you want here
800 }
801 });