]> git.proxmox.com Git - pmg-gui.git/blob - js/Utils.js
add missing action list editors
[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 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
27 oclass_text: {
28 who: gettext('Who Objects'),
29 what: gettext('What Objects'),
30 when: gettext('When Objects'),
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> ',
42 to: '<span class="fa fa-fw fa-user-circle"></span> '
43 },
44
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
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
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;
97 },
98
99 rule_direction_text: {
100 0: gettext('In'),
101 1: gettext('Out'),
102 2: gettext('In & Out')
103 },
104
105 rule_direction_icon: {
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> '
109 },
110
111 format_rule_direction: function(dir) {
112 var icon = PMG.Utils.rule_direction_icon[dir] || '';
113 var text = PMG.Utils.rule_direction_text[dir] || dir;
114 return icon + text;
115 },
116
117 format_otype: function(otype) {
118 var editor = PMG.Utils.object_editors[otype];
119 var iconCls = 'fa fa-question-circle';
120 if (editor) {
121 var icon = '<span class="fa-fw ' + (editor.iconCls || iconCls) + '"></span> ';
122 return icon + editor.subject;
123 }
124
125 return '<span class="fa-fw ' + iconCls + '"></span> unknown';
126 },
127
128 format_ldap_protocol: function(p) {
129 if (p === undefined) { return 'LDAP'; }
130 if (p === 'ldap') { return 'LDAP'; }
131 if (p === 'ldaps') { return 'LDAPS'; }
132 return 'unknown';
133 },
134
135 convert_field_to_per_min: function(value, record) {
136 return (value/(record.data.timespan/60));
137 },
138
139 object_editors: {
140 1000: {
141 iconCls: 'fa fa-filter',
142 xtype: 'proxmoxWindowEdit',
143 subdir: 'regex',
144 subject: gettext("Regular Expression"),
145 width: 400,
146 items: [
147 {
148 xtype: 'textfield',
149 name: 'regex',
150 labelWidth: 150,
151 fieldLabel: gettext("Regular Expression")
152 }
153 ]
154 },
155 1005: {
156 iconCls: 'fa fa-users',
157 xtype: 'pmgLDAPGroupEditor',
158 subdir: 'ldap',
159 subject: gettext("LDAP Group")
160 },
161 1006: {
162 iconCls: 'fa fa-user',
163 xtype: 'pmgLDAPUserEditor',
164 subdir: 'ldapuser',
165 subject: gettext("LDAP User")
166 },
167 1009: {
168 iconCls: 'fa fa-filter',
169 xtype: 'proxmoxWindowEdit',
170 subdir: 'receiver_regex',
171 subject: gettext("Regular Expression"),
172 receivertest: true,
173 width: 400,
174 items: [
175 {
176 xtype: 'textfield',
177 name: 'regex',
178 labelWidth: 150,
179 fieldLabel: gettext("Regular Expression")
180 }
181 ]
182 },
183 1001: {
184 iconCls: 'fa fa-envelope-o',
185 xtype: 'proxmoxWindowEdit',
186 subdir: 'email',
187 subject: gettext("Email"),
188 width: 400,
189 items: [
190 {
191 xtype: 'textfield',
192 name: 'email',
193 fieldLabel: gettext("Email")
194 }
195 ]
196 },
197 1007: {
198 iconCls: 'fa fa-envelope-o',
199 xtype: 'proxmoxWindowEdit',
200 subdir: 'receiver',
201 subject: gettext("Email"),
202 receivertest: true,
203 width: 400,
204 items: [
205 {
206 xtype: 'textfield',
207 name: 'email',
208 fieldLabel: gettext("Email")
209 }
210 ]
211 },
212 1002: {
213 iconCls: 'fa fa-globe',
214 xtype: 'proxmoxWindowEdit',
215 subdir: 'domain',
216 subject: gettext("Domain"),
217 width: 400,
218 items: [
219 {
220 xtype: 'textfield',
221 name: 'domain',
222 fieldLabel: gettext("Domain")
223 }
224 ]
225 },
226 1008: {
227 iconCls: 'fa fa-globe',
228 xtype: 'proxmoxWindowEdit',
229 subdir: 'receiver_domain',
230 subject: gettext("Domain"),
231 receivertest: true,
232 width: 400,
233 items: [
234 {
235 xtype: 'textfield',
236 name: 'domain',
237 fieldLabel: gettext("Domain")
238 }
239 ]
240 },
241 1003: {
242 iconCls: 'fa fa-globe',
243 xtype: 'proxmoxWindowEdit',
244 subdir: 'ip',
245 subject: gettext("IP Address"),
246 width: 400,
247 items: [
248 {
249 xtype: 'textfield',
250 name: 'ip',
251 fieldLabel: gettext("IP Address")
252 }
253 ]
254 },
255 1004: {
256 iconCls: 'fa fa-globe',
257 xtype: 'proxmoxWindowEdit',
258 subdir: 'network',
259 subject: gettext("IP Network"),
260 width: 400,
261 items: [
262 {
263 xtype: 'textfield',
264 name: 'cidr',
265 fieldLabel: gettext("IP Network")
266 }
267 ]
268 },
269 2000: {
270 iconCls: 'fa fa-clock-o',
271 xtype: 'proxmoxWindowEdit',
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 ]
288 },
289 3000: {
290 iconCls: 'fa fa-bullhorn',
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')
301 }
302 ]
303 },
304 3001: {
305 iconCls: 'fa fa-bug',
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 },
317 3002: {
318 iconCls: 'fa fa-code',
319 xtype: 'proxmoxWindowEdit',
320 subdir: 'matchfield',
321 subject: gettext('Match Field'),
322 width: 400,
323 items: [
324 {
325 xtype: 'textfield',
326 name: 'field',
327 labelWidth: 150,
328 allowBlank: false,
329 fieldLabel: gettext('Field')
330 },
331 {
332 xtype: 'textfield',
333 reference: 'value',
334 name: 'value',
335 labelWidth: 150,
336 allowBlank: false,
337 fieldLabel: gettext('Value')
338 },
339 {
340 labelWidth: 150,
341 fieldLabel: gettext('Test String'),
342 xtype: 'pmgRegexTester',
343 regexFieldReference: 'value'
344 }
345 ]
346 },
347 3003: {
348 iconCls: 'fa fa-file-image-o',
349 xtype: 'proxmoxWindowEdit',
350 subdir: 'contenttype',
351 width: 400,
352 subject: gettext('Content Type Filter'),
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'
367 }
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,
384 reset: Ext.emptyFn
385 }
386 ]
387 },
388 3004: {
389 iconCls: 'fa fa-file-o',
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 },
412 3005: {
413 iconCls: 'fa fa-file-archive-o',
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'
432 }
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 },
453 4002: {
454 xtype: 'proxmoxWindowEdit',
455 subdir: 'notification',
456 subject: gettext('Notification'),
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: 'to',
473 allowBlank: false,
474 value: '__ADMIN__',
475 fieldLabel: gettext('Receiver')
476 },
477 {
478 xtype: 'textfield',
479 name: 'subject',
480 allowBlank: false,
481 value: 'Notification: __SUBJECT__',
482 fieldLabel: gettext('Subject')
483 },
484 {
485 xtype: 'textarea',
486 name: 'body',
487 allowBlank: false,
488 grow: true,
489 growMax: 250,
490 value:
491 "Proxmox Notifcation:\n\n" +
492 "Sender: __SENDER__\n" +
493 "Receiver: __RECEIVERS__\n" +
494 "Targets: __TARGETS__\n\n" +
495 "Subject: __SUBJECT__\n\n" +
496 "Matching Rule: __RULE__\n\n" +
497 "__RULE_INFO__\n\n" +
498 "__VIRUS_INFO__\n" +
499 "__SPAM_INFO__\n",
500 fieldLabel: gettext('Body')
501 },
502 {
503 xtype: 'proxmoxcheckbox',
504 name: 'attach',
505 fieldLabel: gettext("Attach orig. Mail")
506 }
507 ]
508 },
509 4003: {
510 xtype: 'proxmoxWindowEdit',
511 subdir: 'field',
512 subject: gettext('Header Attribute'),
513 width: 400,
514 items: [
515 {
516 xtype: 'textfield',
517 name: 'name',
518 allowBlank: false,
519 fieldLabel: gettext('Name')
520 },
521 {
522 xtype: 'textareafield',
523 name: 'info',
524 fieldLabel: gettext("Description")
525 },
526 {
527 xtype: 'textfield',
528 name: 'field',
529 allowBlank: false,
530 fieldLabel: gettext('Field')
531 },
532 {
533 xtype: 'textfield',
534 reference: 'value',
535 name: 'value',
536 allowBlank: false,
537 fieldLabel: gettext('Value')
538 }
539 ]
540 },
541 4005: {
542 xtype: 'proxmoxWindowEdit',
543 subdir: 'bcc',
544 subject: gettext('BCC'),
545 width: 400,
546 items: [
547 {
548 xtype: 'textfield',
549 name: 'name',
550 allowBlank: false,
551 fieldLabel: gettext('Name')
552 },
553 {
554 xtype: 'textareafield',
555 name: 'info',
556 fieldLabel: gettext("Description")
557 },
558 {
559 xtype: 'textfield',
560 name: 'target',
561 allowBlank: false,
562 fieldLabel: gettext("Target")
563 },
564 {
565 xtype: 'proxmoxcheckbox',
566 checked: true,
567 name: 'original',
568 fieldLabel: gettext("send orig. Mail")
569 }
570 ]
571
572 },
573 4007: {
574 xtype: 'proxmoxWindowEdit',
575 subdir: 'removeattachments',
576 subject: gettext('Remove Attachments'),
577 width: 500,
578 fieldDefaults: {
579 labelWidth: 150
580 },
581 items: [
582 {
583 xtype: 'textfield',
584 name: 'name',
585 allowBlank: false,
586 fieldLabel: gettext('Name')
587 },
588 {
589 xtype: 'textareafield',
590 name: 'info',
591 fieldLabel: gettext("Description")
592 },
593 {
594 xtype: 'textareafield',
595 name: 'text',
596 grow: true,
597 growMax: 250,
598 fieldLabel: gettext("Text Replacement")
599 },
600 {
601 xtype: 'proxmoxcheckbox',
602 checked: true,
603 name: 'all',
604 fieldLabel: gettext("Remove all attachments")
605 }
606 ]
607 },
608 4009: {
609 xtype: 'proxmoxWindowEdit',
610 subdir: 'disclaimer',
611 subject: gettext('Disclaimer'),
612 width: 400,
613 items: [
614 {
615 xtype: 'textfield',
616 name: 'name',
617 allowBlank: false,
618 fieldLabel: gettext('Name')
619 },
620 {
621 xtype: 'textareafield',
622 name: 'info',
623 fieldLabel: gettext("Description")
624 },
625 {
626 xtype: 'textareafield',
627 name: 'disclaimer',
628 grow: true,
629 growMax: 250,
630 fieldLabel: gettext("Disclaimer")
631 }
632 ]
633 }
634 },
635
636 openVNCViewer: function(consoletype, nodename) {
637 var url = Ext.urlEncode({
638 console: consoletype, // upgrade or shell
639 novnc: 1,
640 node: nodename
641 });
642 var nw = window.open("?" + url, '_blank',
643 "innerWidth=745,innerheight=427");
644 nw.focus();
645 },
646
647 updateLoginData: function(data) {
648 Proxmox.CSRFPreventionToken = data.CSRFPreventionToken;
649 Proxmox.UserName = data.username;
650 Ext.util.Cookies.set('PMGAuthCookie', data.ticket, null, '/', null, true );
651 },
652
653 quarantineActionExtracted: false,
654
655 extractQuarantineAction: function() {
656
657 if (PMG.Utils.quarantineActionExtracted) { return; }
658
659 PMG.Utils.quarantineActionExtracted = true;
660
661 var qs = Ext.Object.fromQueryString(location.search);
662
663 var cselect = qs.cselect;
664 var action = qs.action;
665 var ticket = qs.ticket;
666
667 delete qs.cselect;
668 delete qs.action;
669 delete qs.ticket;
670
671 var newsearch = Ext.Object.toQueryString(qs);
672
673 var newurl = location.protocol + "//" + location.host + location.pathname;
674 if (newsearch) { newurl += '?' + newsearch; }
675 newurl += location.hash;
676
677 if (window.history) {
678 window.history.pushState({ path:newurl }, '', newurl);
679 }
680
681 if (action || cselect) {
682 return { action: action, cselect: cselect };
683 }
684 },
685
686 doQuarantineAction: function(action, id, callback) {
687 Proxmox.Utils.API2Request({
688 url: '/quarantine/content/',
689 params: {
690 action: action,
691 id: id
692 },
693 method: 'POST',
694 failure: function(response, opts) {
695 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
696 },
697 success: function(response, opts) {
698 var win = Ext.create('Ext.window.MessageBox',{
699 closeAction: 'destroy'
700 }).show({
701 title: gettext('Info'),
702 message: "Action '" + action + ' ' +
703 id + "' successful",
704 buttons: Ext.Msg.OK,
705 icon: Ext.MessageBox.INFO
706 });
707
708 if (Ext.isFunction(callback)) {
709 callback();
710 }
711 }
712 });
713 },
714
715 sender_renderer: function(value, metaData, rec) {
716 var subject = Ext.htmlEncode(value);
717 var from = Ext.htmlEncode(rec.data.from);
718 var sender = Ext.htmlEncode(rec.data.sender);
719 if (sender) {
720 from = Ext.String.format(gettext("{0} on behalf of {1}"),
721 sender, from);
722 }
723 return '<small>' + from + '</small><br>' + subject;
724 },
725
726 constructor: function() {
727 var me = this;
728
729 // do whatever you want here
730 }
731 });