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