]> git.proxmox.com Git - pmg-gui.git/blob - js/Utils.js
normalize the dashboard graph to per minute
[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 if (editor) {
120 return editor.subject;
121 }
122 return 'unknown';
123 },
124
125 format_ldap_protocol: function(p) {
126 if (p === undefined) { return 'LDAP'; }
127 if (p === 'ldap') { return 'LDAP'; }
128 if (p === 'ldaps') { return 'LDAPS'; }
129 return 'unknown';
130 },
131
132 convert_field_to_per_min: function(value, record) {
133 return (value/(record.data.timespan/60));
134 },
135
136 object_editors: {
137 1000: {
138 xtype: 'proxmoxWindowEdit',
139 subdir: 'regex',
140 subject: gettext("Regular Expression"),
141 width: 400,
142 items: [
143 {
144 xtype: 'textfield',
145 name: 'regex',
146 labelWidth: 150,
147 fieldLabel: gettext("Regular Expression")
148 }
149 ]
150 },
151 1005: {
152 xtype: 'pmgLDAPGroupEditor',
153 subdir: 'ldap',
154 subject: gettext("LDAP Group")
155 },
156 1006: {
157 xtype: 'pmgLDAPUserEditor',
158 subdir: 'ldapuser',
159 subject: gettext("LDAP User")
160 },
161 1009: {
162 xtype: 'proxmoxWindowEdit',
163 subdir: 'receiver_regex',
164 subject: gettext("Regular Expression"),
165 receivertest: true,
166 width: 400,
167 items: [
168 {
169 xtype: 'textfield',
170 name: 'regex',
171 labelWidth: 150,
172 fieldLabel: gettext("Regular Expression")
173 }
174 ]
175 },
176 1001: {
177 xtype: 'proxmoxWindowEdit',
178 subdir: 'email',
179 subject: gettext("Email"),
180 width: 400,
181 items: [
182 {
183 xtype: 'textfield',
184 name: 'email',
185 fieldLabel: gettext("Email")
186 }
187 ]
188 },
189 1007: {
190 xtype: 'proxmoxWindowEdit',
191 subdir: 'receiver',
192 subject: gettext("Email"),
193 receivertest: true,
194 width: 400,
195 items: [
196 {
197 xtype: 'textfield',
198 name: 'email',
199 fieldLabel: gettext("Email")
200 }
201 ]
202 },
203 1002: {
204 xtype: 'proxmoxWindowEdit',
205 subdir: 'domain',
206 subject: gettext("Domain"),
207 width: 400,
208 items: [
209 {
210 xtype: 'textfield',
211 name: 'domain',
212 fieldLabel: gettext("Domain")
213 }
214 ]
215 },
216 1008: {
217 xtype: 'proxmoxWindowEdit',
218 subdir: 'receiver_domain',
219 subject: gettext("Domain"),
220 receivertest: true,
221 width: 400,
222 items: [
223 {
224 xtype: 'textfield',
225 name: 'domain',
226 fieldLabel: gettext("Domain")
227 }
228 ]
229 },
230 1003: {
231 xtype: 'proxmoxWindowEdit',
232 subdir: 'ip',
233 subject: gettext("IP Address"),
234 width: 400,
235 items: [
236 {
237 xtype: 'textfield',
238 name: 'ip',
239 fieldLabel: gettext("IP Address")
240 }
241 ]
242 },
243 1004: {
244 xtype: 'proxmoxWindowEdit',
245 subdir: 'network',
246 subject: gettext("IP Network"),
247 width: 400,
248 items: [
249 {
250 xtype: 'textfield',
251 name: 'cidr',
252 fieldLabel: gettext("IP Network")
253 }
254 ]
255 },
256 2000: {
257 xtype: 'proxmoxWindowEdit',
258 subdir: 'timeframe',
259 subject: gettext("TimeFrame"),
260 items: [
261 {
262 xtype: 'timefield',
263 name: 'start',
264 format: 'H:i',
265 fieldLabel: gettext("Start Time")
266 },
267 {
268 xtype: 'timefield',
269 name: 'end',
270 format: 'H:i',
271 fieldLabel: gettext("End Time")
272 }
273 ]
274 },
275 3000: {
276 xtype: 'proxmoxWindowEdit',
277 subdir: 'spamfilter',
278 subject: gettext('Spam Filter'),
279 items: [
280 {
281 xtype: 'proxmoxintegerfield',
282 name: 'spamlevel',
283 allowBlank: false,
284 minValue: 0,
285 fieldLabel: gettext('Level')
286 }
287 ]
288 },
289 3002: {
290 xtype: 'proxmoxWindowEdit',
291 subdir: 'matchfield',
292 subject: gettext('Match Field'),
293 width: 400,
294 items: [
295 {
296 xtype: 'textfield',
297 name: 'field',
298 labelWidth: 150,
299 allowBlank: false,
300 fieldLabel: gettext('Field')
301 },
302 {
303 xtype: 'textfield',
304 reference: 'value',
305 name: 'value',
306 labelWidth: 150,
307 allowBlank: false,
308 fieldLabel: gettext('Value')
309 },
310 {
311 labelWidth: 150,
312 fieldLabel: gettext('Test String'),
313 xtype: 'pmgRegexTester',
314 regexFieldReference: 'value'
315 }
316 ]
317 },
318 3003: {
319 xtype: 'proxmoxWindowEdit',
320 subdir: 'contenttype',
321 width: 400,
322 subject: gettext('Content Type Filter'),
323 items: [
324 {
325 xtype: 'combobox',
326 displayField: 'text',
327 labelWidth: 150,
328 valueField: 'mimetype',
329 name: 'contenttype',
330 editable: true,
331 queryMode: 'local',
332 store: {
333 autoLoad: true,
334 proxy: {
335 type: 'proxmox',
336 url: '/api2/json/config/mimetypes'
337 }
338 },
339 fieldLabel: gettext('Content Type'),
340 anyMatch: true,
341 matchFieldWidth: false,
342 listeners: {
343 change: function(cb, value) {
344 var me = this;
345 me.up().down('displayfield').setValue(value);
346 }
347 }
348 },
349 {
350 xtype: 'displayfield',
351 fieldLabel: gettext('Value'),
352 labelWidth: 150,
353 allowBlank: false,
354 reset: Ext.emptyFn
355 }
356 ]
357 },
358 3004: {
359 xtype: 'proxmoxWindowEdit',
360 subdir: 'filenamefilter',
361 width: 400,
362 subject: gettext('Match Filename'),
363 items: [
364 {
365 xtype: 'textfield',
366 name: 'filename',
367 reference: 'filename',
368 fieldLabel: gettext('Filename'),
369 labelWidth: 150,
370 allowBlank: false
371 },
372 {
373 labelWidth: 150,
374 fieldLabel: gettext('Test String'),
375 wholeMatch: true,
376 xtype: 'pmgRegexTester',
377 regexFieldReference: 'filename'
378 }
379 ]
380 },
381 3005: {
382 xtype: 'proxmoxWindowEdit',
383 subdir: 'archivefilter',
384 width: 400,
385 subject: gettext('Archive Filter'),
386 items: [
387 {
388 xtype: 'combobox',
389 displayField: 'text',
390 labelWidth: 150,
391 valueField: 'mimetype',
392 name: 'contenttype',
393 editable: true,
394 queryMode: 'local',
395 store: {
396 autoLoad: true,
397 proxy: {
398 type: 'proxmox',
399 url: '/api2/json/config/mimetypes'
400 }
401 },
402 fieldLabel: gettext('Content Type'),
403 anyMatch: true,
404 matchFieldWidth: false,
405 listeners: {
406 change: function(cb, value) {
407 var me = this;
408 me.up().down('displayfield').setValue(value);
409 }
410 }
411 },
412 {
413 xtype: 'displayfield',
414 fieldLabel: gettext('Value'),
415 labelWidth: 150,
416 allowBlank: false,
417 reset: Ext.emptyFn
418 }
419 ]
420 },
421 4005: {
422 xtype: 'proxmoxWindowEdit',
423 subdir: 'bcc',
424 subject: gettext('BCC'),
425 width: 400,
426 items: [
427 {
428 xtype: 'textfield',
429 name: 'name',
430 allowBlank: false,
431 fieldLabel: gettext('Name')
432 },
433 {
434 xtype: 'textareafield',
435 name: 'info',
436 fieldLabel: gettext("Description")
437 },
438 {
439 xtype: 'textfield',
440 name: 'target',
441 allowBlank: false,
442 fieldLabel: gettext("Target")
443 },
444 {
445 xtype: 'proxmoxcheckbox',
446 checked: true,
447 name: 'original',
448 fieldLabel: gettext("send orig. Mail")
449 }
450 ]
451
452 }
453 },
454
455 openVNCViewer: function(consoletype, nodename) {
456 var url = Ext.urlEncode({
457 console: consoletype, // upgrade or shell
458 novnc: 1,
459 node: nodename
460 });
461 var nw = window.open("?" + url, '_blank',
462 "innerWidth=745,innerheight=427");
463 nw.focus();
464 },
465
466 updateLoginData: function(data) {
467 Proxmox.CSRFPreventionToken = data.CSRFPreventionToken;
468 Proxmox.UserName = data.username;
469 Ext.util.Cookies.set('PMGAuthCookie', data.ticket, null, '/', null, true );
470 },
471
472 quarantineActionExtracted: false,
473
474 extractQuarantineAction: function() {
475
476 if (PMG.Utils.quarantineActionExtracted) { return; }
477
478 PMG.Utils.quarantineActionExtracted = true;
479
480 var qs = Ext.Object.fromQueryString(location.search);
481
482 var cselect = qs.cselect;
483 var action = qs.action;
484 var ticket = qs.ticket;
485
486 delete qs.cselect;
487 delete qs.action;
488 delete qs.ticket;
489
490 var newsearch = Ext.Object.toQueryString(qs);
491
492 var newurl = location.protocol + "//" + location.host + location.pathname;
493 if (newsearch) { newurl += '?' + newsearch; }
494 newurl += location.hash;
495
496 if (window.history) {
497 window.history.pushState({ path:newurl }, '', newurl);
498 }
499
500 if (action || cselect) {
501 return { action: action, cselect: cselect };
502 }
503 },
504
505 doQuarantineAction: function(action, id, callback) {
506 Proxmox.Utils.API2Request({
507 url: '/quarantine/content/',
508 params: {
509 action: action,
510 id: id
511 },
512 method: 'POST',
513 failure: function(response, opts) {
514 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
515 },
516 success: function(response, opts) {
517 var win = Ext.create('Ext.window.MessageBox',{
518 closeAction: 'destroy'
519 }).show({
520 title: gettext('Info'),
521 message: "Action '" + action + ' ' +
522 id + "' successful",
523 buttons: Ext.Msg.OK,
524 icon: Ext.MessageBox.INFO
525 });
526
527 if (Ext.isFunction(callback)) {
528 callback();
529 }
530 }
531 });
532 },
533
534 sender_renderer: function(value, metaData, rec) {
535 var subject = Ext.htmlEncode(value);
536 var from = Ext.htmlEncode(rec.data.from);
537 var sender = Ext.htmlEncode(rec.data.sender);
538 if (sender) {
539 from = Ext.String.format(gettext("{0} on behalf of {1}"),
540 sender, from);
541 }
542 return '<small>' + from + '</small><br>' + subject;
543 },
544
545 constructor: function() {
546 var me = this;
547
548 // do whatever you want here
549 }
550 });