admin.js 17 KB

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