]> git.proxmox.com Git - pmg-gui.git/blob - js/Utils.js
add 'Match Archive Filename' to list of what objects
[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 3006: {
468 onlineHelp: 'pmg_mailfilter_regex',
469 iconCls: 'fa fa-file-archive-o',
470 xtype: 'proxmoxWindowEdit',
471 subdir: 'archivefilenamefilter',
472 width: 400,
473 subject: gettext('Match Archive Filename'),
474 items: [
475 {
476 xtype: 'textfield',
477 name: 'filename',
478 reference: 'filename',
479 fieldLabel: gettext('Filename'),
480 labelWidth: 150,
481 allowBlank: false
482 },
483 {
484 labelWidth: 150,
485 fieldLabel: gettext('Test String'),
486 wholeMatch: true,
487 xtype: 'pmgRegexTester',
488 regexFieldReference: 'filename'
489 }
490 ]
491 },
492 4002: {
493 onlineHelp: 'pmg_mailfilter_action',
494 xtype: 'proxmoxWindowEdit',
495 subdir: 'notification',
496 subject: gettext('Notification'),
497 width: 400,
498 items: [
499 {
500 xtype: 'textfield',
501 name: 'name',
502 allowBlank: false,
503 fieldLabel: gettext('Name')
504 },
505 {
506 xtype: 'textareafield',
507 name: 'info',
508 fieldLabel: gettext("Comment")
509 },
510 {
511 xtype: 'textfield',
512 name: 'to',
513 allowBlank: false,
514 value: '__ADMIN__',
515 fieldLabel: gettext('Receiver')
516 },
517 {
518 xtype: 'textfield',
519 name: 'subject',
520 allowBlank: false,
521 value: 'Notification: __SUBJECT__',
522 fieldLabel: gettext('Subject')
523 },
524 {
525 xtype: 'textarea',
526 name: 'body',
527 allowBlank: false,
528 grow: true,
529 growMax: 250,
530 value:
531 "Proxmox Notifcation:\n\n" +
532 "Sender: __SENDER__\n" +
533 "Receiver: __RECEIVERS__\n" +
534 "Targets: __TARGETS__\n\n" +
535 "Subject: __SUBJECT__\n\n" +
536 "Matching Rule: __RULE__\n\n" +
537 "__RULE_INFO__\n\n" +
538 "__VIRUS_INFO__\n" +
539 "__SPAM_INFO__\n",
540 fieldLabel: gettext('Body')
541 },
542 {
543 xtype: 'proxmoxcheckbox',
544 name: 'attach',
545 fieldLabel: gettext("Attach orig. Mail")
546 }
547 ]
548 },
549 4003: {
550 onlineHelp: 'pmg_mailfilter_action',
551 xtype: 'proxmoxWindowEdit',
552 subdir: 'field',
553 subject: gettext('Header Attribute'),
554 width: 400,
555 items: [
556 {
557 xtype: 'textfield',
558 name: 'name',
559 allowBlank: false,
560 fieldLabel: gettext('Name')
561 },
562 {
563 xtype: 'textareafield',
564 name: 'info',
565 fieldLabel: gettext("Comment")
566 },
567 {
568 xtype: 'textfield',
569 name: 'field',
570 allowBlank: false,
571 fieldLabel: gettext('Field')
572 },
573 {
574 xtype: 'textfield',
575 reference: 'value',
576 name: 'value',
577 allowBlank: false,
578 fieldLabel: gettext('Value')
579 }
580 ]
581 },
582 4005: {
583 onlineHelp: 'pmg_mailfilter_action',
584 xtype: 'proxmoxWindowEdit',
585 subdir: 'bcc',
586 subject: gettext('BCC'),
587 width: 400,
588 items: [
589 {
590 xtype: 'textfield',
591 name: 'name',
592 allowBlank: false,
593 fieldLabel: gettext('Name')
594 },
595 {
596 xtype: 'textareafield',
597 name: 'info',
598 fieldLabel: gettext("Comment")
599 },
600 {
601 xtype: 'textfield',
602 name: 'target',
603 allowBlank: false,
604 fieldLabel: gettext("Target")
605 },
606 {
607 xtype: 'proxmoxcheckbox',
608 checked: true,
609 name: 'original',
610 fieldLabel: gettext("send orig. Mail")
611 }
612 ]
613
614 },
615 4007: {
616 onlineHelp: 'pmg_mailfilter_action',
617 xtype: 'proxmoxWindowEdit',
618 subdir: 'removeattachments',
619 subject: gettext('Remove Attachments'),
620 width: 500,
621 fieldDefaults: {
622 labelWidth: 150
623 },
624 items: [
625 {
626 xtype: 'textfield',
627 name: 'name',
628 allowBlank: false,
629 fieldLabel: gettext('Name')
630 },
631 {
632 xtype: 'textareafield',
633 name: 'info',
634 fieldLabel: gettext("Comment")
635 },
636 {
637 xtype: 'textareafield',
638 name: 'text',
639 grow: true,
640 growMax: 250,
641 fieldLabel: gettext("Text Replacement")
642 },
643 {
644 xtype: 'proxmoxcheckbox',
645 checked: true,
646 name: 'all',
647 fieldLabel: gettext("Remove all attachments")
648 },
649 {
650 xtype: 'proxmoxcheckbox',
651 checked: false,
652 name: 'quarantine',
653 fieldLabel: gettext("Copy orignal mail to Attachment Quarantine")
654 }
655 ]
656 },
657 4009: {
658 onlineHelp: 'pmg_mailfilter_action',
659 xtype: 'proxmoxWindowEdit',
660 subdir: 'disclaimer',
661 subject: gettext('Disclaimer'),
662 width: 400,
663 items: [
664 {
665 xtype: 'textfield',
666 name: 'name',
667 allowBlank: false,
668 fieldLabel: gettext('Name')
669 },
670 {
671 xtype: 'textareafield',
672 name: 'info',
673 fieldLabel: gettext("Comment")
674 },
675 {
676 xtype: 'textareafield',
677 name: 'disclaimer',
678 grow: true,
679 growMax: 250,
680 fieldLabel: gettext("Disclaimer")
681 }
682 ]
683 }
684 },
685
686 updateLoginData: function(data) {
687 Proxmox.CSRFPreventionToken = data.CSRFPreventionToken;
688 Proxmox.UserName = data.username;
689 Ext.util.Cookies.set('PMGAuthCookie', data.ticket, null, '/', null, true );
690 },
691
692 quarantineActionExtracted: false,
693
694 extractQuarantineAction: function() {
695
696 if (PMG.Utils.quarantineActionExtracted) { return; }
697
698 PMG.Utils.quarantineActionExtracted = true;
699
700 var qs = Ext.Object.fromQueryString(location.search);
701
702 var cselect = qs.cselect;
703 var action = qs.action;
704 var ticket = qs.ticket;
705 var dateString = qs.date;
706
707 if (dateString) {
708 var date = new Date(dateString).getTime()/1000;
709
710 // set from date for QuarantineList
711 /*jslint confusion: true*/
712 /*from is a string above and number here */
713 PMG.QuarantineList.from = date;
714 /*jslint confusion: false*/
715 }
716
717 delete qs.cselect;
718 delete qs.action;
719 delete qs.ticket;
720 delete qs.date;
721
722 var newsearch = Ext.Object.toQueryString(qs);
723
724 var newurl = location.protocol + "//" + location.host + location.pathname;
725 if (newsearch) { newurl += '?' + newsearch; }
726 newurl += location.hash;
727
728 if (window.history) {
729 window.history.pushState({ path:newurl }, '', newurl);
730 }
731
732 if (action || cselect) {
733 return { action: action, cselect: cselect };
734 }
735 },
736
737 doQuarantineAction: function(action, id, callback) {
738 var count = id.split(';').length;
739 var successMessage = "Action '{0}'";
740 if (count > 1) {
741 successMessage += " for '{1}' items";
742 }
743 successMessage += " successful";
744
745 /*jslint confusion: true*/
746 /*format is string and function*/
747 Proxmox.Utils.API2Request({
748 url: '/quarantine/content/',
749 params: {
750 action: action,
751 id: id
752 },
753 method: 'POST',
754 failure: function(response, opts) {
755 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
756 },
757 success: function(response, opts) {
758 var win = Ext.create('Ext.window.MessageBox',{
759 closeAction: 'destroy'
760 }).show({
761 title: gettext('Info'),
762 message: Ext.String.format(successMessage, action, count),
763 buttons: Ext.Msg.OK,
764 icon: Ext.MessageBox.INFO
765 });
766
767 if (Ext.isFunction(callback)) {
768 callback();
769 }
770 }
771 });
772 /*jslint confusion: false*/
773 },
774
775 render_filetype: function(value) {
776 let iconCls = 'fa-file-o';
777 let text = Proxmox.Utils.unknownText;
778
779 if (!value) {
780 return `<i class='fa ${iconCls}'></i> ${text}`;
781 }
782
783 text = value.toString().toLowerCase();
784 let type = text.split('/')[0];
785
786 switch (type) {
787 case 'audio':
788 case 'image':
789 case 'video':
790 case 'text':
791 iconCls = `fa-file-${type}-o`;
792 break;
793 case 'application':
794 let subtypes = ['excel', 'pdf', 'word', 'powerpoint'];
795 let found = subtypes.find(st => text.includes(st));
796 if (found !== undefined) {
797 iconCls = `fa-file-${found}-o`;
798 }
799 break;
800 default:
801 break;
802 }
803
804 return `<i class='fa ${iconCls}'></i> ${text}`;
805 },
806
807 sender_renderer: function(value, metaData, rec) {
808 var subject = Ext.htmlEncode(value);
809 var from = Ext.htmlEncode(rec.data.from);
810 var sender = Ext.htmlEncode(rec.data.sender);
811 if (sender) {
812 /*jslint confusion: true*/
813 /*format is a string above*/
814 from = Ext.String.format(gettext("{0} on behalf of {1}"),
815 sender, from);
816 /*jslint confusion: false*/
817 }
818 return '<small>' + from + '</small><br>' + subject;
819 },
820
821 constructor: function() {
822 var me = this;
823
824 // do whatever you want here
825 }
826 });