quarantine.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. $(".refresh_table").on('click', function(e) {
  10. e.preventDefault();
  11. var table_name = $(this).data('table');
  12. $('#' + table_name).DataTable().ajax.reload();
  13. });
  14. function draw_quarantine_table() {
  15. $('#quarantinetable').DataTable({
  16. processing: true,
  17. serverSide: false,
  18. language: lang_datatables,
  19. ajax: {
  20. type: "GET",
  21. url: "/api/v1/get/quarantine/all",
  22. dataSrc: function(data){
  23. $.each(data, function (i, item) {
  24. if (item.subject === null) {
  25. item.subject = '';
  26. } else {
  27. item.subject = escapeHtml(item.subject);
  28. }
  29. if (item.score === null) {
  30. item.score = '-';
  31. }
  32. if (item.virus_flag > 0) {
  33. item.virus = '<span class="badge fs-5 bg-danger">' + lang.high_danger + '</span>';
  34. } else {
  35. item.virus = '<span class="badge fs-5 bg-secondary">' + lang.neutral_danger + '</span>';
  36. }
  37. if (item.action === "reject") {
  38. item.rspamdaction = '<span class="badge fs-5 bg-danger">' + lang.rejected + '</span>';
  39. } else if (item.action === "add header") {
  40. item.rspamdaction = '<span class="badge fs-5 bg-warning">' + lang.junk_folder + '</span>';
  41. } else if (item.action === "rewrite subject") {
  42. item.rspamdaction = '<span class="badge fs-5 bg-warning">' + lang.rewrite_subject + '</span>';
  43. }
  44. if(item.notified > 0) {
  45. item.notified = '&#10004;';
  46. } else {
  47. item.notified = '&#10006;';
  48. }
  49. if (acl_data.login_as === 1) {
  50. item.action = '<div class="btn-group">' +
  51. '<a href="#" data-item="' + encodeURI(item.id) + '" class="btn btn-xs btn-xs-half btn-info show_qid_info"><i class="bi bi-box-arrow-up-right"></i> ' + lang.show_item + '</a>' +
  52. '<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-xs-half btn-danger"><i class="bi bi-trash"></i> ' + lang.remove + '</a>' +
  53. '</div>';
  54. }
  55. else {
  56. item.action = '<div class="btn-group">' +
  57. '<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>' +
  58. '</div>';
  59. }
  60. item.chkbox = '<input type="checkbox" data-id="qitems" name="multi_select" value="' + item.id + '" />';
  61. });
  62. return data;
  63. }
  64. },
  65. columns: [
  66. {
  67. // placeholder, so checkbox will not block child row toggle
  68. title: '',
  69. data: null,
  70. searchable: false,
  71. orderable: false,
  72. defaultContent: ''
  73. },
  74. {
  75. title: '',
  76. data: 'chkbox',
  77. searchable: false,
  78. orderable: false,
  79. defaultContent: ''
  80. },
  81. {
  82. title: 'ID',
  83. data: 'id',
  84. },
  85. {
  86. title: lang.qid,
  87. data: 'qid'
  88. },
  89. {
  90. title: lang.sender,
  91. data: 'sender'
  92. },
  93. {
  94. title: lang.subj,
  95. data: 'sender'
  96. },
  97. {
  98. title: lang.rspamd_result,
  99. data: 'rspamdaction'
  100. },
  101. {
  102. title: lang.rcpt,
  103. data: 'rcpt'
  104. },
  105. {
  106. title: lang.danger,
  107. data: 'virus'
  108. },
  109. {
  110. title: lang.spam_score,
  111. data: 'score'
  112. },
  113. {
  114. title: lang.notified,
  115. data: 'notified'
  116. },
  117. {
  118. title: lang.received,
  119. data: 'created',
  120. render: function (data,type) {
  121. var date = new Date(data ? data * 1000 : 0);
  122. return date.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"});
  123. }
  124. },
  125. {
  126. title: lang.action,
  127. data: 'action',
  128. className: 'text-md-end d-md-block dt-sm-head-hidden dt-body-right'
  129. },
  130. ]
  131. });
  132. }
  133. $('body').on('click', '.show_qid_info', function (e) {
  134. e.preventDefault();
  135. var qitem = $(this).attr('data-item');
  136. var qError = $("#qid_error");
  137. $('#qidDetailModal').modal('show');
  138. qError.hide();
  139. $.ajax({
  140. url: '/inc/ajax/qitem_details.php',
  141. data: { id: qitem },
  142. dataType: 'json',
  143. success: function(data){
  144. $('[data-id="qitems_single"]').each(function(index) {
  145. $(this).attr("data-item", qitem);
  146. });
  147. $("#quick_download_link").attr("onclick", "window.open('/inc/ajax/qitem_details.php?id=" + qitem + "&eml', '_blank')");
  148. $("#quick_release_link").attr("onclick", "window.open('/inc/ajax/qitem_details.php?id=" + qitem + "&quick_release', '_blank')");
  149. $("#quick_delete_link").attr("onclick", "window.open('/inc/ajax/qitem_details.php?id=" + qitem + "&quick_delete', '_blank')");
  150. $('#qid_detail_subj').text(data.subject);
  151. $('#qid_detail_hfrom').text(data.header_from);
  152. $('#qid_detail_efrom').text(data.env_from);
  153. $('#qid_detail_score').html('');
  154. $('#qid_detail_recipients').html('');
  155. $('#qid_detail_symbols').html('');
  156. $('#qid_detail_fuzzy').html('');
  157. if (typeof data.symbols !== 'undefined') {
  158. data.symbols.sort(function (a, b) {
  159. if (a.score === 0) return 1
  160. if (b.score === 0) return -1
  161. if (b.score < 0 && a.score < 0) {
  162. return a.score - b.score
  163. }
  164. if (b.score > 0 && a.score > 0) {
  165. return b.score - a.score
  166. }
  167. return b.score - a.score
  168. })
  169. $.each(data.symbols, function (index, value) {
  170. var highlightClass = ''
  171. if (value.score > 0) highlightClass = 'negative'
  172. else if (value.score < 0) highlightClass = 'positive'
  173. else highlightClass = 'neutral'
  174. $('#qid_detail_symbols').append('<span data-bs-toggle="tooltip" class="rspamd-symbol ' + highlightClass + '" title="' + (value.options ? value.options.join(', ') : '') + '">' + value.name + ' (<span class="score">' + value.score + '</span>)</span>');
  175. });
  176. $('[data-bs-toggle="tooltip"]').tooltip()
  177. }
  178. if (typeof data.fuzzy_hashes === 'object' && data.fuzzy_hashes !== null && data.fuzzy_hashes.length !== 0) {
  179. $.each(data.fuzzy_hashes, function (index, value) {
  180. $('#qid_detail_fuzzy').append('<p style="font-family:monospace">' + value + '</p>');
  181. });
  182. } else {
  183. $('#qid_detail_fuzzy').append('-');
  184. }
  185. if (typeof data.score !== 'undefined' && typeof data.action !== 'undefined') {
  186. if (data.action == "add header") {
  187. $('#qid_detail_score').append('<span class="label-rspamd-action badge fs-5 bg-warning"><b>' + data.score + '</b> - ' + lang.junk_folder + '</span>');
  188. } else if (data.action == "reject") {
  189. $('#qid_detail_score').append('<span class="label-rspamd-action badge fs-5 bg-danger"><b>' + data.score + '</b> - ' + lang.rejected + '</span>');
  190. } else if (data.action == "rewrite subject") {
  191. $('#qid_detail_score').append('<span class="label-rspamd-action badge fs-5 bg-warning"><b>' + data.score + '</b> - ' + lang.rewrite_subject + '</span>');
  192. }
  193. }
  194. if (typeof data.recipients !== 'undefined') {
  195. $.each(data.recipients, function(index, value) {
  196. var elem = $('<span class="mail-address-item"></span>');
  197. elem.text(value.address + ' (' + value.type.toUpperCase() + ')');
  198. $('#qid_detail_recipients').append(elem);
  199. });
  200. }
  201. $('#qid_detail_text').text(data.text_plain);
  202. $('#qid_detail_text_from_html').text(data.text_html);
  203. var qAtts = $("#qid_detail_atts");
  204. if (typeof data.attachments !== 'undefined') {
  205. qAtts.text('');
  206. $.each(data.attachments, function(index, value) {
  207. qAtts.append(
  208. '<p><a href="/inc/ajax/qitem_details.php?id=' + qitem + '&att=' + index + '" target="_blank">' + value[0] + '</a> (' + value[1] + ')' +
  209. ' - <small><a href="' + value[3] + '" target="_blank">' + lang.check_hash + '</a></small></p>'
  210. );
  211. });
  212. }
  213. else {
  214. qAtts.text('-');
  215. }
  216. },
  217. error: function(data){
  218. if (typeof data.error !== 'undefined') {
  219. $('#qid_detail_subj').text('-');
  220. $('#qid_detail_hfrom').text('-');
  221. $('#qid_detail_efrom').text('-');
  222. $('#qid_detail_score').html('-');
  223. $('#qid_detail_recipients').html('-');
  224. $('#qid_detail_symbols').html('-');
  225. $('#qid_detail_fuzzy').html('-');
  226. $('#qid_detail_text').text('-');
  227. $('#qid_detail_text_from_html').text('-');
  228. qError.text("Error loading quarantine item");
  229. qError.show();
  230. }
  231. }
  232. });
  233. });
  234. $('body').on('click', 'span.footable-toggle', function () {
  235. event.stopPropagation();
  236. })
  237. // Initial table drawings
  238. draw_quarantine_table();
  239. });