quarantine.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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":"rcpt","title":lang.rcpt, "breakpoints":"xs sm md", "type": "text"},
  55. {"name":"virus","title":lang.danger, "type": "text"},
  56. {"name":"score","title": lang.spam_score, "type": "text"},
  57. {"name":"notified","title":lang.notified, "type": "text"},
  58. {"name":"created","formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleString();},"title":lang.received,"style":{"width":"170px"}},
  59. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right"},"style":{"width":"220px"},"type":"html","title":lang.action,"breakpoints":"xs sm md"}
  60. ],
  61. "rows": $.ajax({
  62. dataType: 'json',
  63. url: '/api/v1/get/quarantine/all',
  64. jsonp: false,
  65. error: function () {
  66. console.log('Cannot draw quarantine table');
  67. },
  68. success: function (data) {
  69. $.each(data, function (i, item) {
  70. if (item.subject === null) {
  71. item.subject = '';
  72. } else {
  73. item.subject = escapeHtml(item.subject);
  74. }
  75. if (item.score === null) {
  76. item.score = '-';
  77. }
  78. if (item.virus_flag > 0) {
  79. item.virus = '<span class="dot-danger"></span>';
  80. } else {
  81. item.virus = '<span class="dot-neutral"></span>';
  82. }
  83. if(item.notified > 0) {
  84. item.notified = '&#10004;';
  85. } else {
  86. item.notified = '&#10006;';
  87. }
  88. if (acl_data.login_as === 1) {
  89. item.action = '<div class="btn-group">' +
  90. '<a href="#" data-item="' + encodeURI(item.id) + '" class="btn btn-xs btn-info show_qid_info"><span class="glyphicon glyphicon-modal-window"></span> ' + lang.show_item + '</a>' +
  91. '<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"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  92. '</div>';
  93. }
  94. else {
  95. item.action = '<div class="btn-group">' +
  96. '<a href="#" data-item="' + encodeURI(item.id) + '" class="btn btn-xs btn-info show_qid_info"><span class="glyphicon glyphicon-modal-window"></span> ' + lang.show_item + '</a>' +
  97. '</div>';
  98. }
  99. item.chkbox = '<input type="checkbox" data-id="qitems" name="multi_select" value="' + item.id + '" />';
  100. });
  101. }
  102. }),
  103. "empty": lang.empty,
  104. "paging": {"enabled": true,"limit": 5,"size": pagination_size},
  105. "sorting": {"enabled": true},
  106. "filtering": {"enabled": true,"position": "left","connectors": false,"placeholder": lang.filter_table},
  107. "toggleSelector": "table tbody span.footable-toggle",
  108. "on": {
  109. "destroy.ft.table": function(e, ft){
  110. $('.refresh_table').attr('disabled', 'true');
  111. },
  112. "ready.ft.table": function(e, ft){
  113. table_quarantine_ready(ft, 'quarantinetable');
  114. },
  115. "after.ft.filtering": function(e, ft){
  116. table_quarantine_ready(ft, 'quarantinetable');
  117. }
  118. },
  119. });
  120. }
  121. $('body').on('click', '.show_qid_info', function (e) {
  122. e.preventDefault();
  123. var qitem = $(this).data('item');
  124. var qError = $("#qid_error");
  125. $('#qidDetailModal').modal('show');
  126. qError.hide();
  127. $.ajax({
  128. url: '/inc/ajax/qitem_details.php',
  129. data: { id: qitem },
  130. dataType: 'json',
  131. success: function(data){
  132. if (typeof data.error !== 'undefined') {
  133. qError.text(data.error);
  134. qError.show();
  135. }
  136. $('[data-id="qitems_single"]').each(function(index) {
  137. $(this).attr("data-item", qitem);
  138. });
  139. $('#qid_detail_subj').text(data.subject);
  140. $('#qid_detail_text').text(data.text_plain);
  141. $('#qid_detail_text_from_html').text(data.text_html);
  142. $('#qid_detail_score').text(data.score);
  143. $('#qid_detail_symbols').html('');
  144. if (typeof data.symbols !== 'undefined') {
  145. data.symbols.sort(function (a, b) {
  146. if (a.score === 0) return 1
  147. if (b.score === 0) return -1
  148. if (b.score < 0 && a.score < 0) {
  149. return a.score - b.score
  150. }
  151. if (b.score > 0 && a.score > 0) {
  152. return b.score - a.score
  153. }
  154. return b.score - a.score
  155. })
  156. $.each(data.symbols, function (index, value) {
  157. var highlightClass = ''
  158. if (value.score > 0) highlightClass = 'negative'
  159. else if (value.score < 0) highlightClass = 'positive'
  160. else highlightClass = 'neutral'
  161. $('#qid_detail_symbols').append('<span class="rspamd-symbol ' + highlightClass + '" title="' + (value.options ? value.options.join(', ') : '') + '">' + value.name + ' (<span class="score">' + value.score + '</span>)</span>');
  162. });
  163. }
  164. $('#qid_detail_recipients').html('');
  165. if (typeof data.recipients !== 'undefined') {
  166. $.each(data.recipients, function(index, value) {
  167. var elem = $('<span class="mail-address-item"></span>');
  168. elem.text(value.address + (value.type != 'to' ? (' (' + value.type.toUpperCase() + ')') : ''));
  169. $('#qid_detail_recipients').append(elem);
  170. });
  171. }
  172. var qAtts = $("#qid_detail_atts");
  173. if (typeof data.attachments !== 'undefined') {
  174. qAtts.text('');
  175. $.each(data.attachments, function(index, value) {
  176. qAtts.append(
  177. '<p><a href="/inc/ajax/qitem_details.php?id=' + qitem + '&att=' + index + '" target="_blank">' + value[0] + '</a> (' + value[1] + ')' +
  178. ' - <small><a href="' + value[3] + '" target="_blank">' + lang.check_hash + '</a></small></p>'
  179. );
  180. });
  181. }
  182. else {
  183. qAtts.text('-');
  184. }
  185. }
  186. });
  187. });
  188. $('body').on('click', 'span.footable-toggle', function () {
  189. event.stopPropagation();
  190. })
  191. // Initial table drawings
  192. draw_quarantine_table();
  193. });