quarantine.js 11 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. $(".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 footable-actions">' +
  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. },
  78. {
  79. title: 'ID',
  80. data: 'id',
  81. },
  82. {
  83. title: lang.qid,
  84. data: 'qid'
  85. },
  86. {
  87. title: lang.sender,
  88. data: 'sender'
  89. },
  90. {
  91. title: lang.subj,
  92. data: 'sender'
  93. },
  94. {
  95. title: lang.rspamd_result,
  96. data: 'rspamdaction'
  97. },
  98. {
  99. title: lang.rcpt,
  100. data: 'rcpt'
  101. },
  102. {
  103. title: lang.danger,
  104. data: 'virus'
  105. },
  106. {
  107. title: lang.spam_score,
  108. data: 'score'
  109. },
  110. {
  111. title: lang.notified,
  112. data: 'notified'
  113. },
  114. {
  115. title: lang.received,
  116. data: 'created',
  117. render: function (data,type) {
  118. var date = new Date(data ? data * 1000 : 0);
  119. return date.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"});
  120. }
  121. },
  122. {
  123. title: lang.action,
  124. data: 'action'
  125. },
  126. ]
  127. });
  128. }
  129. $('body').on('click', '.show_qid_info', function (e) {
  130. e.preventDefault();
  131. var qitem = $(this).attr('data-item');
  132. var qError = $("#qid_error");
  133. $('#qidDetailModal').modal('show');
  134. qError.hide();
  135. $.ajax({
  136. url: '/inc/ajax/qitem_details.php',
  137. data: { id: qitem },
  138. dataType: 'json',
  139. success: function(data){
  140. $('[data-id="qitems_single"]').each(function(index) {
  141. $(this).attr("data-item", qitem);
  142. });
  143. $("#quick_download_link").attr("onclick", "window.open('/inc/ajax/qitem_details.php?id=" + qitem + "&eml', '_blank')");
  144. $("#quick_release_link").attr("onclick", "window.open('/inc/ajax/qitem_details.php?id=" + qitem + "&quick_release', '_blank')");
  145. $("#quick_delete_link").attr("onclick", "window.open('/inc/ajax/qitem_details.php?id=" + qitem + "&quick_delete', '_blank')");
  146. $('#qid_detail_subj').text(data.subject);
  147. $('#qid_detail_hfrom').text(data.header_from);
  148. $('#qid_detail_efrom').text(data.env_from);
  149. $('#qid_detail_score').html('');
  150. $('#qid_detail_recipients').html('');
  151. $('#qid_detail_symbols').html('');
  152. $('#qid_detail_fuzzy').html('');
  153. if (typeof data.symbols !== 'undefined') {
  154. data.symbols.sort(function (a, b) {
  155. if (a.score === 0) return 1
  156. if (b.score === 0) return -1
  157. if (b.score < 0 && a.score < 0) {
  158. return a.score - b.score
  159. }
  160. if (b.score > 0 && a.score > 0) {
  161. return b.score - a.score
  162. }
  163. return b.score - a.score
  164. })
  165. $.each(data.symbols, function (index, value) {
  166. var highlightClass = ''
  167. if (value.score > 0) highlightClass = 'negative'
  168. else if (value.score < 0) highlightClass = 'positive'
  169. else highlightClass = 'neutral'
  170. $('#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>');
  171. });
  172. $('[data-bs-toggle="tooltip"]').tooltip()
  173. }
  174. if (typeof data.fuzzy_hashes === 'object' && data.fuzzy_hashes !== null && data.fuzzy_hashes.length !== 0) {
  175. $.each(data.fuzzy_hashes, function (index, value) {
  176. $('#qid_detail_fuzzy').append('<p style="font-family:monospace">' + value + '</p>');
  177. });
  178. } else {
  179. $('#qid_detail_fuzzy').append('-');
  180. }
  181. if (typeof data.score !== 'undefined' && typeof data.action !== 'undefined') {
  182. if (data.action == "add header") {
  183. $('#qid_detail_score').append('<span class="label-rspamd-action badge fs-5 bg-warning"><b>' + data.score + '</b> - ' + lang.junk_folder + '</span>');
  184. } else if (data.action == "reject") {
  185. $('#qid_detail_score').append('<span class="label-rspamd-action badge fs-5 bg-danger"><b>' + data.score + '</b> - ' + lang.rejected + '</span>');
  186. } else if (data.action == "rewrite subject") {
  187. $('#qid_detail_score').append('<span class="label-rspamd-action badge fs-5 bg-warning"><b>' + data.score + '</b> - ' + lang.rewrite_subject + '</span>');
  188. }
  189. }
  190. if (typeof data.recipients !== 'undefined') {
  191. $.each(data.recipients, function(index, value) {
  192. var elem = $('<span class="mail-address-item"></span>');
  193. elem.text(value.address + ' (' + value.type.toUpperCase() + ')');
  194. $('#qid_detail_recipients').append(elem);
  195. });
  196. }
  197. $('#qid_detail_text').text(data.text_plain);
  198. $('#qid_detail_text_from_html').text(data.text_html);
  199. var qAtts = $("#qid_detail_atts");
  200. if (typeof data.attachments !== 'undefined') {
  201. qAtts.text('');
  202. $.each(data.attachments, function(index, value) {
  203. qAtts.append(
  204. '<p><a href="/inc/ajax/qitem_details.php?id=' + qitem + '&att=' + index + '" target="_blank">' + value[0] + '</a> (' + value[1] + ')' +
  205. ' - <small><a href="' + value[3] + '" target="_blank">' + lang.check_hash + '</a></small></p>'
  206. );
  207. });
  208. }
  209. else {
  210. qAtts.text('-');
  211. }
  212. },
  213. error: function(data){
  214. if (typeof data.error !== 'undefined') {
  215. $('#qid_detail_subj').text('-');
  216. $('#qid_detail_hfrom').text('-');
  217. $('#qid_detail_efrom').text('-');
  218. $('#qid_detail_score').html('-');
  219. $('#qid_detail_recipients').html('-');
  220. $('#qid_detail_symbols').html('-');
  221. $('#qid_detail_fuzzy').html('-');
  222. $('#qid_detail_text').text('-');
  223. $('#qid_detail_text_from_html').text('-');
  224. qError.text("Error loading quarantine item");
  225. qError.show();
  226. }
  227. }
  228. });
  229. });
  230. $('body').on('click', 'span.footable-toggle', function () {
  231. event.stopPropagation();
  232. })
  233. // Initial table drawings
  234. draw_quarantine_table();
  235. });