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