quarantine.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Base64 functions
  2. var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(r){var t,e,o,a,h,n,c,d="",C=0;for(r=Base64._utf8_encode(r);C<r.length;)a=(t=r.charCodeAt(C++))>>2,h=(3&t)<<4|(e=r.charCodeAt(C++))>>4,n=(15&e)<<2|(o=r.charCodeAt(C++))>>6,c=63&o,isNaN(e)?n=c=64:isNaN(o)&&(c=64),d=d+this._keyStr.charAt(a)+this._keyStr.charAt(h)+this._keyStr.charAt(n)+this._keyStr.charAt(c);return d},decode:function(r){var t,e,o,a,h,n,c="",d=0;for(r=r.replace(/[^A-Za-z0-9\+\/\=]/g,"");d<r.length;)t=this._keyStr.indexOf(r.charAt(d++))<<2|(a=this._keyStr.indexOf(r.charAt(d++)))>>4,e=(15&a)<<4|(h=this._keyStr.indexOf(r.charAt(d++)))>>2,o=(3&h)<<6|(n=this._keyStr.indexOf(r.charAt(d++))),c+=String.fromCharCode(t),64!=h&&(c+=String.fromCharCode(e)),64!=n&&(c+=String.fromCharCode(o));return c=Base64._utf8_decode(c)},_utf8_encode:function(r){r=r.replace(/\r\n/g,"\n");for(var t="",e=0;e<r.length;e++){var o=r.charCodeAt(e);o<128?t+=String.fromCharCode(o):o>127&&o<2048?(t+=String.fromCharCode(o>>6|192),t+=String.fromCharCode(63&o|128)):(t+=String.fromCharCode(o>>12|224),t+=String.fromCharCode(o>>6&63|128),t+=String.fromCharCode(63&o|128))}return t},_utf8_decode:function(r){for(var t="",e=0,o=c1=c2=0;e<r.length;)(o=r.charCodeAt(e))<128?(t+=String.fromCharCode(o),e++):o>191&&o<224?(c2=r.charCodeAt(e+1),t+=String.fromCharCode((31&o)<<6|63&c2),e+=2):(c2=r.charCodeAt(e+1),c3=r.charCodeAt(e+2),t+=String.fromCharCode((15&o)<<12|(63&c2)<<6|63&c3),e+=3);return t}};
  3. jQuery(function($){
  4. acl_data = JSON.parse(acl);
  5. // http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery
  6. var entityMap={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;","/":"&#x2F;","`":"&#x60;","=":"&#x3D;"};
  7. function escapeHtml(n){return String(n).replace(/[&<>"'`=\/]/g,function(n){return entityMap[n]})}
  8. function humanFileSize(i){if(Math.abs(i)<1024)return i+" B";var B=["KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"],e=-1;do{i/=1024,++e}while(Math.abs(i)>=1024&&e<B.length-1);return i.toFixed(1)+" "+B[e]}
  9. // Set paging
  10. $('[data-page-size]').on('click', function(e){
  11. e.preventDefault();
  12. var new_size = $(this).data('page-size');
  13. var parent_ul = $(this).closest('ul');
  14. var table_id = $(parent_ul).data('table-id');
  15. FooTable.get('#' + table_id).pageSize(new_size);
  16. //$(this).parent().addClass('active').siblings().removeClass('active')
  17. heading = $(this).parents('.panel').find('.panel-heading')
  18. var n_results = $(heading).children('.table-lines').text().split(' / ')[1];
  19. $(heading).children('.table-lines').text(function(){
  20. if (new_size > n_results) {
  21. new_size = n_results;
  22. }
  23. return new_size + ' / ' + n_results;
  24. })
  25. });
  26. $(".refresh_table").on('click', function(e) {
  27. e.preventDefault();
  28. var table_name = $(this).data('table');
  29. $('#' + table_name).find("tr.footable-empty").remove();
  30. draw_table = $(this).data('draw');
  31. eval(draw_table + '()');
  32. });
  33. function table_quarantine_ready(ft, name) {
  34. $('.refresh_table').prop("disabled", false);
  35. heading = ft.$el.parents('.panel').find('.panel-heading')
  36. var ft_paging = ft.use(FooTable.Paging)
  37. $(heading).children('.table-lines').text(function(){
  38. var total_rows = ft_paging.totalRows;
  39. var size = ft_paging.size;
  40. if (size > total_rows) {
  41. size = total_rows;
  42. }
  43. return size + ' / ' + total_rows;
  44. })
  45. }
  46. function draw_quarantine_table() {
  47. ft_quarantinetable = FooTable.init('#quarantinetable', {
  48. "columns": [
  49. {"name":"chkbox","title":"","style":{"maxWidth":"60px","width":"60px"},"filterable": false,"sortable": false,"type":"html"},
  50. {"name":"id","type":"ID","filterable": false,"sorted": true,"direction":"DESC","title":"ID","style":{"width":"50px"}},
  51. {"name":"qid","breakpoints":"all","type":"text","title":lang.qid,"style":{"width":"125px"}},
  52. {"name":"sender","title":lang.sender},
  53. {"name":"subject","title":lang.subj, "type": "text"},
  54. {"name":"rspamdaction","title":lang.rspamd_result, "type": "html"},
  55. {"name":"rcpt","title":lang.rcpt, "type": "text"},
  56. {"name":"virus","title":lang.danger, "type": "text"},
  57. {"name":"score","title": lang.spam_score, "type": "text"},
  58. {"name":"notified","title":lang.notified, "type": "text"},
  59. {"name":"created","formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"});},"title":lang.received,"style":{"width":"170px"}},
  60. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right"},"style":{"min-width":"200px"},"type":"html","title":lang.action}
  61. ],
  62. "rows": $.ajax({
  63. dataType: 'json',
  64. url: '/api/v1/get/quarantine/all',
  65. jsonp: false,
  66. error: function () {
  67. console.log('Cannot draw quarantine table');
  68. },
  69. success: function (data) {
  70. $.each(data, function (i, item) {
  71. if (item.subject === null) {
  72. item.subject = '';
  73. } else {
  74. item.subject = escapeHtml(item.subject);
  75. }
  76. if (item.score === null) {
  77. item.score = '-';
  78. }
  79. if (item.virus_flag > 0) {
  80. item.virus = '<span class="label label-danger">' + lang.high_danger + '</span>';
  81. } else {
  82. item.virus = '<span class="label label-default">' + lang.neutral_danger + '</span>';
  83. }
  84. if (item.action === "reject") {
  85. item.rspamdaction = '<span class="label label-danger">' + lang.rejected + '</span>';
  86. } else if (item.action === "add header") {
  87. item.rspamdaction = '<span class="label label-warning">' + lang.junk_folder + '</span>';
  88. } else if (item.action === "rewrite subject") {
  89. item.rspamdaction = '<span class="label label-warning">' + lang.rewrite_subject + '</span>';
  90. }
  91. if(item.notified > 0) {
  92. item.notified = '&#10004;';
  93. } else {
  94. item.notified = '&#10006;';
  95. }
  96. if (acl_data.login_as === 1) {
  97. item.action = '<div class="btn-group">' +
  98. '<a href="#" data-item="' + encodeURI(item.id) + '" class="btn btn-xs btn-info show_qid_info"><i class="bi bi-box-arrow-up-right"></i> ' + lang.show_item + '</a>' +
  99. '<a href="#" data-action="delete_selected" data-id="del-single-qitem" data-api-url="delete/qitem" data-item="' + encodeURI(item.id) + '" class="btn btn-xs btn-danger"><i class="bi bi-trash"></i> ' + lang.remove + '</a>' +
  100. '</div>';
  101. }
  102. else {
  103. item.action = '<div class="btn-group">' +
  104. '<a href="#" data-item="' + encodeURI(item.id) + '" class="btn btn-xs btn-info show_qid_info"><i class="bi bi-file-earmark-text"></i> ' + lang.show_item + '</a>' +
  105. '</div>';
  106. }
  107. item.chkbox = '<input type="checkbox" data-id="qitems" name="multi_select" value="' + item.id + '" />';
  108. });
  109. }
  110. }),
  111. "empty": lang.empty,
  112. "paging": {"enabled": true,"limit": 5,"size": pagination_size},
  113. "state": {"enabled": true},
  114. "sorting": {"enabled": true},
  115. "filtering": {"enabled": true,"position": "left","connectors": false,"placeholder": lang.filter_table},
  116. "toggleSelector": "table tbody span.footable-toggle",
  117. "on": {
  118. "destroy.ft.table": function(e, ft){
  119. $('.refresh_table').attr('disabled', 'true');
  120. },
  121. "ready.ft.table": function(e, ft){
  122. table_quarantine_ready(ft, 'quarantinetable');
  123. },
  124. "after.ft.filtering": function(e, ft){
  125. table_quarantine_ready(ft, 'quarantinetable');
  126. }
  127. },
  128. });
  129. }
  130. $('body').on('click', '.show_qid_info', function (e) {
  131. e.preventDefault();
  132. var qitem = $(this).attr('data-item');
  133. var qError = $("#qid_error");
  134. $('#qidDetailModal').modal('show');
  135. qError.hide();
  136. $.ajax({
  137. url: '/inc/ajax/qitem_details.php',
  138. data: { id: qitem },
  139. dataType: 'json',
  140. success: function(data){
  141. $('[data-id="qitems_single"]').each(function(index) {
  142. $(this).attr("data-item", qitem);
  143. });
  144. $("#quick_download_link").attr("onclick", "window.open('/inc/ajax/qitem_details.php?id=" + qitem + "&eml', '_blank')");
  145. $("#quick_release_link").attr("onclick", "window.open('/inc/ajax/qitem_details.php?id=" + qitem + "&quick_release', '_blank')");
  146. $("#quick_delete_link").attr("onclick", "window.open('/inc/ajax/qitem_details.php?id=" + qitem + "&quick_delete', '_blank')");
  147. $('#qid_detail_subj').text(data.subject);
  148. $('#qid_detail_hfrom').text(data.header_from);
  149. $('#qid_detail_efrom').text(data.env_from);
  150. $('#qid_detail_score').html('');
  151. $('#qid_detail_recipients').html('');
  152. $('#qid_detail_symbols').html('');
  153. $('#qid_detail_fuzzy').html('');
  154. if (typeof data.symbols !== 'undefined') {
  155. data.symbols.sort(function (a, b) {
  156. if (a.score === 0) return 1
  157. if (b.score === 0) return -1
  158. if (b.score < 0 && a.score < 0) {
  159. return a.score - b.score
  160. }
  161. if (b.score > 0 && a.score > 0) {
  162. return b.score - a.score
  163. }
  164. return b.score - a.score
  165. })
  166. $.each(data.symbols, function (index, value) {
  167. var highlightClass = ''
  168. if (value.score > 0) highlightClass = 'negative'
  169. else if (value.score < 0) highlightClass = 'positive'
  170. else highlightClass = 'neutral'
  171. $('#qid_detail_symbols').append('<span data-toggle="tooltip" class="rspamd-symbol ' + highlightClass + '" title="' + (value.options ? value.options.join(', ') : '') + '">' + value.name + ' (<span class="score">' + value.score + '</span>)</span>');
  172. });
  173. $('[data-toggle="tooltip"]').tooltip()
  174. }
  175. if (typeof data.fuzzy_hashes === 'object' && data.fuzzy_hashes !== null && data.fuzzy_hashes.length !== 0) {
  176. $.each(data.fuzzy_hashes, function (index, value) {
  177. $('#qid_detail_fuzzy').append('<p style="font-family:monospace">' + value + '</p>');
  178. });
  179. } else {
  180. $('#qid_detail_fuzzy').append('-');
  181. }
  182. if (typeof data.score !== 'undefined' && typeof data.action !== 'undefined') {
  183. if (data.action == "add header") {
  184. $('#qid_detail_score').append('<span class="label-rspamd-action label label-warning"><b>' + data.score + '</b> - ' + lang.junk_folder + '</span>');
  185. } else if (data.action == "reject") {
  186. $('#qid_detail_score').append('<span class="label-rspamd-action label label-danger"><b>' + data.score + '</b> - ' + lang.rejected + '</span>');
  187. } else if (data.action == "rewrite subject") {
  188. $('#qid_detail_score').append('<span class="label-rspamd-action label label-warning"><b>' + data.score + '</b> - ' + lang.rewrite_subject + '</span>');
  189. }
  190. }
  191. if (typeof data.recipients !== 'undefined') {
  192. $.each(data.recipients, function(index, value) {
  193. var elem = $('<span class="mail-address-item"></span>');
  194. elem.text(value.address + ' (' + value.type.toUpperCase() + ')');
  195. $('#qid_detail_recipients').append(elem);
  196. });
  197. }
  198. $('#qid_detail_text').text(data.text_plain);
  199. $('#qid_detail_text_from_html').text(data.text_html);
  200. var qAtts = $("#qid_detail_atts");
  201. if (typeof data.attachments !== 'undefined') {
  202. qAtts.text('');
  203. $.each(data.attachments, function(index, value) {
  204. qAtts.append(
  205. '<p><a href="/inc/ajax/qitem_details.php?id=' + qitem + '&att=' + index + '" target="_blank">' + value[0] + '</a> (' + value[1] + ')' +
  206. ' - <small><a href="' + value[3] + '" target="_blank">' + lang.check_hash + '</a></small></p>'
  207. );
  208. });
  209. }
  210. else {
  211. qAtts.text('-');
  212. }
  213. },
  214. error: function(data){
  215. if (typeof data.error !== 'undefined') {
  216. $('#qid_detail_subj').text('-');
  217. $('#qid_detail_hfrom').text('-');
  218. $('#qid_detail_efrom').text('-');
  219. $('#qid_detail_score').html('-');
  220. $('#qid_detail_recipients').html('-');
  221. $('#qid_detail_symbols').html('-');
  222. $('#qid_detail_fuzzy').html('-');
  223. $('#qid_detail_text').text('-');
  224. $('#qid_detail_text_from_html').text('-');
  225. qError.text("Error loading quarantine item");
  226. qError.show();
  227. }
  228. }
  229. });
  230. });
  231. $('body').on('click', 'span.footable-toggle', function () {
  232. event.stopPropagation();
  233. })
  234. // Initial table drawings
  235. draw_quarantine_table();
  236. });