admin.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. jQuery(function($){
  2. // http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery
  3. var entityMap = {
  4. '&': '&',
  5. '<': '&lt;',
  6. '>': '&gt;',
  7. '"': '&quot;',
  8. "'": '&#39;',
  9. '/': '&#x2F;',
  10. '`': '&#x60;',
  11. '=': '&#x3D;'
  12. };
  13. function escapeHtml(string) {
  14. return String(string).replace(/[&<>"'`=\/]/g, function (s) {
  15. return entityMap[s];
  16. });
  17. }
  18. function humanFileSize(bytes) {
  19. if(Math.abs(bytes) < 1024) {
  20. return bytes + ' B';
  21. }
  22. var units = ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
  23. var u = -1;
  24. do {
  25. bytes /= 1024;
  26. ++u;
  27. } while(Math.abs(bytes) >= 1024 && u < units.length - 1);
  28. return bytes.toFixed(1)+' '+units[u];
  29. }
  30. $("#refresh_postfix_log").on('click', function(e) {
  31. e.preventDefault();
  32. draw_postfix_logs();
  33. });
  34. $("#refresh_dovecot_log").on('click', function(e) {
  35. e.preventDefault();
  36. draw_dovecot_logs();
  37. });
  38. $("#refresh_sogo_log").on('click', function(e) {
  39. e.preventDefault();
  40. draw_sogo_logs();
  41. });
  42. $("#refresh_fail2ban_log").on('click', function(e) {
  43. e.preventDefault();
  44. draw_fail2ban_logs();
  45. });
  46. $("#refresh_rspamd_history").on('click', function(e) {
  47. e.preventDefault();
  48. draw_rspamd_history();
  49. });
  50. $("#import_dkim_legend").on('click', function(e) {
  51. e.preventDefault();
  52. $('#import_dkim_arrow').toggleClass("animation");
  53. });
  54. function draw_postfix_logs() {
  55. ft_postfix_logs = FooTable.init('#postfix_log', {
  56. "columns": [
  57. {"name":"time","formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleString();},"title":lang.time,"style":{"width":"170px"}},
  58. {"name":"priority","title":lang.priority,"style":{"width":"80px"}},
  59. {"name":"message","title":lang.message},
  60. ],
  61. "rows": $.ajax({
  62. dataType: 'json',
  63. url: '/api/v1/get/logs/postfix/1000',
  64. jsonp: false,
  65. error: function () {
  66. console.log('Cannot draw postfix log table');
  67. },
  68. success: function (data) {
  69. $.each(data, function (i, item) {
  70. item.message = escapeHtml(item.message);
  71. var danger_class = ["emerg", "alert", "crit", "err"];
  72. var warning_class = ["warning", "warn"];
  73. var info_class = ["notice", "info", "debug"];
  74. if (jQuery.inArray(item.priority, danger_class) !== -1) {
  75. item.priority = '<span class="label label-danger">' + item.priority + '</span>';
  76. }
  77. else if (jQuery.inArray(item.priority, warning_class) !== -1) {
  78. item.priority = '<span class="label label-warning">' + item.priority + '</span>';
  79. }
  80. else if (jQuery.inArray(item.priority, info_class) !== -1) {
  81. item.priority = '<span class="label label-info">' + item.priority + '</span>';
  82. }
  83. });
  84. }
  85. }),
  86. "empty": lang.empty,
  87. "paging": {
  88. "enabled": true,
  89. "limit": 5,
  90. "size": log_pagination_size
  91. },
  92. "filtering": {
  93. "enabled": true,
  94. "position": "left",
  95. "placeholder": lang.filter_table
  96. },
  97. "sorting": {
  98. "enabled": true
  99. }
  100. });
  101. }
  102. function draw_fail2ban_logs() {
  103. ft_fail2ban_logs = FooTable.init('#fail2ban_log', {
  104. "columns": [
  105. {"name":"time","formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleString();},"title":lang.time,"style":{"width":"170px"}},
  106. {"name":"priority","title":lang.priority,"style":{"width":"80px"}},
  107. {"name":"message","title":lang.message},
  108. ],
  109. "rows": $.ajax({
  110. dataType: 'json',
  111. url: '/api/v1/get/logs/fail2ban/1000',
  112. jsonp: false,
  113. error: function () {
  114. console.log('Cannot draw fail2ban log table');
  115. },
  116. success: function (data) {
  117. $.each(data, function (i, item) {
  118. var danger_class = ["emerg", "alert", "crit", "err"];
  119. var warning_class = ["warning", "warn"];
  120. var info_class = ["notice", "info", "debug"];
  121. item.message = escapeHtml(item.message);
  122. if (jQuery.inArray(item.priority, danger_class) !== -1) {
  123. item.priority = '<span class="label label-danger">' + item.priority + '</span>';
  124. }
  125. else if (jQuery.inArray(item.priority, warning_class) !== -1) {
  126. item.priority = '<span class="label label-warning">' + item.priority + '</span>';
  127. }
  128. else if (jQuery.inArray(item.priority, info_class) !== -1) {
  129. item.priority = '<span class="label label-info">' + item.priority + '</span>';
  130. }
  131. });
  132. }
  133. }),
  134. "empty": lang.empty,
  135. "paging": {
  136. "enabled": true,
  137. "limit": 5,
  138. "size": log_pagination_size
  139. },
  140. "filtering": {
  141. "enabled": true,
  142. "position": "left",
  143. "placeholder": lang.filter_table
  144. },
  145. "sorting": {
  146. "enabled": true
  147. }
  148. });
  149. }
  150. function draw_sogo_logs() {
  151. ft_sogo_logs = FooTable.init('#sogo_log', {
  152. "columns": [
  153. {"name":"time","formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleString();},"title":lang.time,"style":{"width":"170px"}},
  154. {"name":"priority","title":lang.priority,"style":{"width":"80px"}},
  155. {"name":"message","title":lang.message},
  156. ],
  157. "rows": $.ajax({
  158. dataType: 'json',
  159. url: '/api/v1/get/logs/sogo/1000',
  160. jsonp: false,
  161. error: function () {
  162. console.log('Cannot draw sogo log table');
  163. },
  164. success: function (data) {
  165. $.each(data, function (i, item) {
  166. var danger_class = ["emerg", "alert", "crit", "err"];
  167. var warning_class = ["warning", "warn"];
  168. var info_class = ["notice", "info", "debug"];
  169. item.message = escapeHtml(item.message);
  170. if (jQuery.inArray(item.priority, danger_class) !== -1) {
  171. item.priority = '<span class="label label-danger">' + item.priority + '</span>';
  172. }
  173. else if (jQuery.inArray(item.priority, warning_class) !== -1) {
  174. item.priority = '<span class="label label-warning">' + item.priority + '</span>';
  175. }
  176. else if (jQuery.inArray(item.priority, info_class) !== -1) {
  177. item.priority = '<span class="label label-info">' + item.priority + '</span>';
  178. }
  179. });
  180. }
  181. }),
  182. "empty": lang.empty,
  183. "paging": {
  184. "enabled": true,
  185. "limit": 5,
  186. "size": log_pagination_size
  187. },
  188. "filtering": {
  189. "enabled": true,
  190. "position": "left",
  191. "placeholder": lang.filter_table
  192. },
  193. "sorting": {
  194. "enabled": true
  195. }
  196. });
  197. }
  198. function draw_dovecot_logs() {
  199. ft_postfix_logs = FooTable.init('#dovecot_log', {
  200. "columns": [
  201. {"name":"time","formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleString();},"title":lang.time,"style":{"width":"170px"}},
  202. {"name":"priority","title":lang.priority,"style":{"width":"80px"}},
  203. {"name":"message","title":lang.message},
  204. ],
  205. "rows": $.ajax({
  206. dataType: 'json',
  207. url: '/api/v1/get/logs/dovecot/1000',
  208. jsonp: false,
  209. error: function () {
  210. console.log('Cannot draw dovecot log table');
  211. },
  212. success: function (data) {
  213. $.each(data, function (i, item) {
  214. var danger_class = ["emerg", "alert", "crit", "err"];
  215. var warning_class = ["warning", "warn"];
  216. var info_class = ["notice", "info", "debug"];
  217. item.message = escapeHtml(item.message);
  218. if (jQuery.inArray(item.priority, danger_class) !== -1) {
  219. item.priority = '<span class="label label-danger">' + item.priority + '</span>';
  220. }
  221. else if (jQuery.inArray(item.priority, warning_class) !== -1) {
  222. item.priority = '<span class="label label-warning">' + item.priority + '</span>';
  223. }
  224. else if (jQuery.inArray(item.priority, info_class) !== -1) {
  225. item.priority = '<span class="label label-info">' + item.priority + '</span>';
  226. }
  227. });
  228. }
  229. }),
  230. "empty": lang.empty,
  231. "paging": {
  232. "enabled": true,
  233. "limit": 5,
  234. "size": log_pagination_size
  235. },
  236. "filtering": {
  237. "enabled": true,
  238. "position": "left",
  239. "placeholder": lang.filter_table
  240. },
  241. "sorting": {
  242. "enabled": true
  243. }
  244. });
  245. }
  246. function draw_domain_admins() {
  247. ft_domainadmins = FooTable.init('#domainadminstable', {
  248. "columns": [
  249. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  250. {"sorted": true,"name":"username","title":lang.username,"style":{"width":"250px"}},
  251. {"name":"selected_domains","title":lang.admin_domains,"breakpoints":"xs sm"},
  252. {"name":"tfa_active","title":"TFA", "filterable": false,"style":{"maxWidth":"80px","width":"80px"}},
  253. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  254. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  255. ],
  256. "rows": $.ajax({
  257. dataType: 'json',
  258. url: '/api/v1/get/domain-admin/all',
  259. jsonp: false,
  260. error: function () {
  261. console.log('Cannot draw domain admin table');
  262. },
  263. success: function (data) {
  264. $.each(data, function (i, item) {
  265. item.chkbox = '<input type="checkbox" data-id="domain_admins" name="multi_select" value="' + item.username + '" />';
  266. item.action = '<div class="btn-group">' +
  267. '<a href="/edit.php?domainadmin=' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  268. '<a href="#" id="delete_selected" data-id="single-domain-admin" data-api-url="delete/domain-admin" data-item="' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  269. '</div>';
  270. });
  271. }
  272. }),
  273. "empty": lang.empty,
  274. "paging": {
  275. "enabled": true,
  276. "limit": 5,
  277. "size": log_pagination_size
  278. },
  279. "filtering": {
  280. "enabled": true,
  281. "position": "left",
  282. "placeholder": lang.filter_table
  283. },
  284. "sorting": {
  285. "enabled": true
  286. }
  287. });
  288. }
  289. function draw_fwd_hosts() {
  290. ft_forwardinghoststable = FooTable.init('#forwardinghoststable', {
  291. "columns": [
  292. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  293. {"name":"host","type":"text","title":lang.host,"style":{"width":"250px"}},
  294. {"name":"source","title":lang.source,"breakpoints":"xs sm"},
  295. {"name":"keep_spam","title":lang.spamfilter, "type": "text","style":{"maxWidth":"80px","width":"80px"}},
  296. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  297. ],
  298. "rows": $.ajax({
  299. dataType: 'json',
  300. url: '/api/v1/get/fwdhost/all',
  301. jsonp: false,
  302. error: function () {
  303. console.log('Cannot draw forwarding hosts table');
  304. },
  305. success: function (data) {
  306. $.each(data, function (i, item) {
  307. item.action = '<div class="btn-group">' +
  308. '<a href="#" id="delete_selected" data-id="single-fwdhost" data-api-url="delete/fwdhost" data-item="' + encodeURI(item.host) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  309. '</div>';
  310. if (item.keep_spam == "yes") {
  311. item.keep_spam = lang.no;
  312. }
  313. else {
  314. item.keep_spam = lang.yes;
  315. }
  316. item.chkbox = '<input type="checkbox" data-id="fwdhosts" name="multi_select" value="' + item.host + '" />';
  317. });
  318. }
  319. }),
  320. "empty": lang.empty,
  321. "paging": {
  322. "enabled": true,
  323. "limit": 5,
  324. "size": log_pagination_size
  325. },
  326. "sorting": {
  327. "enabled": true
  328. }
  329. });
  330. }
  331. function draw_relayhosts() {
  332. ft_forwardinghoststable = FooTable.init('#relayhoststable', {
  333. "columns": [
  334. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  335. {"name":"id","type":"text","title":"ID","style":{"width":"50px"}},
  336. {"name":"hostname","type":"text","title":lang.host,"style":{"width":"250px"}},
  337. {"name":"username","title":lang.username,"breakpoints":"xs sm"},
  338. {"name":"used_by_domains","title":lang.in_use_by, "type": "text","breakpoints":"xs sm"},
  339. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  340. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  341. ],
  342. "rows": $.ajax({
  343. dataType: 'json',
  344. url: '/api/v1/get/relayhost/all',
  345. jsonp: false,
  346. error: function () {
  347. console.log('Cannot draw forwarding hosts table');
  348. },
  349. success: function (data) {
  350. $.each(data, function (i, item) {
  351. item.action = '<div class="btn-group">' +
  352. '<a href="#" id="delete_selected" data-id="single-rlshost" data-api-url="delete/relayhost" data-item="' + encodeURI(item.id) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  353. '</div>';
  354. item.chkbox = '<input type="checkbox" data-id="rlyhosts" name="multi_select" value="' + item.id + '" />';
  355. });
  356. }
  357. }),
  358. "empty": lang.empty,
  359. "paging": {
  360. "enabled": true,
  361. "limit": 5,
  362. "size": log_pagination_size
  363. },
  364. "sorting": {
  365. "enabled": true
  366. }
  367. });
  368. }
  369. function draw_rspamd_history() {
  370. ft_postfix_logs = FooTable.init('#rspamd_history', {
  371. "columns": [{
  372. "name":"unix_time",
  373. "formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleString();},
  374. "title":lang.time,
  375. "style":{
  376. "width":"170px"
  377. }
  378. }, {
  379. "name": "ip",
  380. "title": "IP address",
  381. "breakpoints": "all",
  382. "style": {
  383. "minWidth": 88
  384. }
  385. }, {
  386. "name": "sender_mime",
  387. "title": "From",
  388. "breakpoints": "xs sm md",
  389. "style": {
  390. "minWidth": 100
  391. }
  392. }, {
  393. "name": "rcpt_mime",
  394. "title": "To",
  395. "breakpoints": "xs sm md",
  396. "style": {
  397. "minWidth": 100
  398. }
  399. }, {
  400. "name": "subject",
  401. "title": "Subject",
  402. "breakpoints": "all",
  403. "style": {
  404. "word-break": "break-all",
  405. "minWidth": 150
  406. }
  407. }, {
  408. "name": "action",
  409. "title": "Action",
  410. "style": {
  411. "minwidth": 82
  412. }
  413. }, {
  414. "name": "score",
  415. "title": "Score",
  416. "style": {
  417. "maxWidth": 110
  418. },
  419. }, {
  420. "name": "symbols",
  421. "title": "Symbols",
  422. "breakpoints": "all",
  423. }, {
  424. "name": "size",
  425. "title": "Msg size",
  426. "breakpoints": "all",
  427. "style": {
  428. "minwidth": 50,
  429. },
  430. "formatter": function(value) { return humanFileSize(value); }
  431. }, {
  432. "name": "scan_time",
  433. "title": "Scan time",
  434. "breakpoints": "all",
  435. "style": {
  436. "maxWidth": 72
  437. },
  438. }, {
  439. "name": "message-id",
  440. "title": "ID",
  441. "breakpoints": "all",
  442. "style": {
  443. "minWidth": 130,
  444. "overflow": "hidden",
  445. "textOverflow": "ellipsis",
  446. "wordBreak": "break-all",
  447. "whiteSpace": "normal"
  448. }
  449. }, {
  450. "name": "user",
  451. "title": "Authenticated user",
  452. "breakpoints": "xs sm md",
  453. "style": {
  454. "minWidth": 100
  455. }
  456. }],
  457. "rows": $.ajax({
  458. dataType: 'json',
  459. url: '/api/v1/get/logs/rspamd-history',
  460. jsonp: false,
  461. error: function () {
  462. console.log('Cannot draw rspamd history table');
  463. },
  464. success: function (data) {
  465. $.each(data, function (i, item) {
  466. item.rcpt_mime = item.rcpt_mime.join(",&#8203;");
  467. Object.keys(item.symbols).map(function(key) {
  468. var sym = item.symbols[key];
  469. if (sym.score <= 0) {
  470. sym.score_formatted = '(<span class="text-success"><b>' + sym.score + '</b></span>)'
  471. }
  472. else {
  473. sym.score_formatted = '(<span class="text-danger"><b>' + sym.score + '</b></span>)'
  474. }
  475. var str = '<strong>' + key + '</strong> ' + sym.score_formatted;
  476. if (sym.options) {
  477. str += ' [' + sym.options.join(",") + "]";
  478. }
  479. item.symbols[key].str = str;
  480. });
  481. item.symbols = Object.keys(item.symbols).
  482. map(function(key) {
  483. return item.symbols[key];
  484. }).sort(function(e1, e2) {
  485. return Math.abs(e1.score) < Math.abs(e2.score);
  486. }).map(function(e) {
  487. return e.str;
  488. }).join("<br>\n");
  489. var scan_time = item.time_real.toFixed(3) + ' / ' + item.time_virtual.toFixed(3);
  490. item.scan_time = {
  491. "options": {
  492. "sortValue": item.time_real
  493. },
  494. "value": scan_time
  495. };
  496. if (item.action === 'clean' || item.action === 'no action') {
  497. item.action = "<div class='label label-success'>" + item.action + "</div>";
  498. } else if (item.action === 'rewrite subject' || item.action === 'add header' || item.action === 'probable spam') {
  499. item.action = "<div class='label label-warning'>" + item.action + "</div>";
  500. } else if (item.action === 'spam' || item.action === 'reject') {
  501. item.action = "<div class='label label-danger'>" + item.action + "</div>";
  502. } else {
  503. item.action = "<div class='label label-info'>" + item.action + "</div>";
  504. }
  505. var score_content;
  506. if (item.score < item.required_score) {
  507. score_content = "[ <span class='text-success'>" + item.score.toFixed(2) + " / " + item.required_score + "</span> ]";
  508. } else {
  509. score_content = "[ <span class='text-danger'>" + item.score.toFixed(2) + " / " + item.required_score + "</span> ]";
  510. }
  511. item.score = {
  512. "options": {
  513. "sortValue": item.score
  514. },
  515. "value": score_content
  516. };
  517. if (item.user == null) {
  518. item.user = "none";
  519. }
  520. });
  521. }
  522. }),
  523. "empty": lang.empty,
  524. "paging": {
  525. "enabled": true,
  526. "limit": 5,
  527. "size": log_pagination_size
  528. },
  529. "filtering": {
  530. "enabled": true,
  531. "position": "left",
  532. "placeholder": lang.filter_table
  533. },
  534. "sorting": {
  535. "enabled": true
  536. }
  537. });
  538. }
  539. draw_postfix_logs();
  540. draw_dovecot_logs();
  541. draw_sogo_logs();
  542. draw_fail2ban_logs();
  543. draw_domain_admins();
  544. draw_fwd_hosts();
  545. draw_relayhosts();
  546. draw_rspamd_history();
  547. });
  548. $(window).load(function(){
  549. width = $("#scrollbox").width();
  550. $(window).bind('scroll', function() {
  551. if ($(window).scrollTop() > 70) {
  552. $('#scrollbox').addClass('scrollboxFixed');
  553. $("#scrollbox").css("width", width);
  554. } else {
  555. width = $("#scrollbox").width();
  556. $('#scrollbox').removeClass('scrollboxFixed');
  557. $("#scrollbox").removeAttr("style");
  558. }
  559. });
  560. });