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