admin.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. // http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery
  5. var entityMap={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;","/":"&#x2F;","`":"&#x60;","=":"&#x3D;"};
  6. function escapeHtml(n){return String(n).replace(/[&<>"'`=\/]/g,function(n){return entityMap[n]})}
  7. 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]}
  8. $("#rspamd_preset_1").on('click', function(e) {
  9. e.preventDefault();
  10. $("form[data-id=rsetting]").find("#adminRspamdSettingsDesc").val(lang.rsettings_preset_1);
  11. $("form[data-id=rsetting]").find("#adminRspamdSettingsContent").val('priority = 10;\nauthenticated = yes;\napply "default" {\n symbols_enabled = ["DKIM_SIGNED", "RATELIMIT_UPDATE", "RATELIMIT_CHECK", "DYN_RL_CHECK", "HISTORY_SAVE", "MILTER_HEADERS", "ARC_SIGNED"];\n}');
  12. });
  13. $("#rspamd_preset_2").on('click', function(e) {
  14. e.preventDefault();
  15. $("form[data-id=rsetting]").find("#adminRspamdSettingsDesc").val(lang.rsettings_preset_2);
  16. $("form[data-id=rsetting]").find("#adminRspamdSettingsContent").val('priority = 10;\nrcpt = "/postmaster@.*/";\nwant_spam = yes;');
  17. });
  18. $("#dkim_missing_keys").on('click', function(e) {
  19. e.preventDefault();
  20. var domains = [];
  21. $('.dkim_missing').each(function() {
  22. domains.push($(this).val());
  23. });
  24. $('#dkim_add_domains').val(domains);
  25. });
  26. $("#import_dkim_legend").on('click', function(e) { e.preventDefault(); $('#import_dkim_arrow').toggleClass("animation"); });
  27. $("#duplicate_dkim_legend").on('click', function(e) { e.preventDefault(); $('#duplicate_dkim_arrow').toggleClass("animation"); });
  28. $("#api_legend").on('click', function(e) { e.preventDefault(); $('#api_arrow').toggleClass("animation"); });
  29. $("#mass_exclude").change(function(){ $("#mass_include").selectpicker('deselectAll'); });
  30. $("#mass_include").change(function(){ $("#mass_exclude").selectpicker('deselectAll'); });
  31. $("#mass_disarm").click(function() { $("#mass_send").attr("disabled", !this.checked); });
  32. $("#super_delete").click(function() { return confirm(lang.queue_ays); });
  33. $(".refresh_table").on('click', function(e) {
  34. e.preventDefault();
  35. var table_name = $(this).data('table');
  36. $('#' + table_name).find("tr.footable-empty").remove();
  37. draw_table = $(this).data('draw');
  38. eval(draw_table + '()');
  39. });
  40. if (localStorage.getItem("current_page") === null) {
  41. var current_page = {};
  42. } else {
  43. var current_page = JSON.parse(localStorage.getItem('current_page'));
  44. }
  45. function table_admin_ready(ft, name) {
  46. heading = ft.$el.parents('.tab-pane').find('.panel-heading')
  47. var ft_paging = ft.use(FooTable.Paging)
  48. $(heading).children('.table-lines').text(function(){
  49. return ft_paging.totalRows;
  50. })
  51. if (current_page[name]) {
  52. ft_paging.goto(parseInt(current_page[name]))
  53. }
  54. }
  55. function paging_admin_after(ft, name) {
  56. var ft_paging = ft.use(FooTable.Paging)
  57. current_page[name] = ft_paging.current;
  58. localStorage.setItem('current_page', JSON.stringify(current_page));
  59. }
  60. function draw_domain_admins() {
  61. ft_domainadmins = FooTable.init('#domainadminstable', {
  62. "columns": [
  63. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  64. {"sorted": true,"name":"username","title":lang.username,"style":{"width":"250px"}},
  65. {"name":"selected_domains","title":lang.admin_domains,"breakpoints":"xs sm"},
  66. {"name":"tfa_active","title":"TFA", "filterable": false,"style":{"maxWidth":"80px","width":"80px"}},
  67. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  68. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"250px","width":"250px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  69. ],
  70. "rows": $.ajax({
  71. dataType: 'json',
  72. url: '/api/v1/get/domain-admin/all',
  73. jsonp: false,
  74. error: function () {
  75. console.log('Cannot draw domain admin table');
  76. },
  77. success: function (data) {
  78. return process_table_data(data, 'domainadminstable');
  79. }
  80. }),
  81. "empty": lang.empty,
  82. "paging": {"enabled": true,"limit": 5,"size": log_pagination_size},
  83. "filtering": {"enabled": true,"delay": 1,"position": "left","connectors": false,"placeholder": lang.filter_table
  84. },
  85. "sorting": {"enabled": true}
  86. });
  87. }
  88. function draw_admins() {
  89. ft_admins = FooTable.init('#adminstable', {
  90. "columns": [
  91. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  92. {"sorted": true,"name":"usr","title":lang.username,"style":{"width":"250px"}},
  93. {"name":"tfa_active","title":"TFA", "filterable": false,"style":{"maxWidth":"80px","width":"80px"}},
  94. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  95. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"250px","width":"250px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  96. ],
  97. "rows": $.ajax({
  98. dataType: 'json',
  99. url: '/api/v1/get/admin/all',
  100. jsonp: false,
  101. error: function () {
  102. console.log('Cannot draw admin table');
  103. },
  104. success: function (data) {
  105. return process_table_data(data, 'adminstable');
  106. }
  107. }),
  108. "empty": lang.empty,
  109. "paging": {"enabled": true,"limit": 5,"size": log_pagination_size},
  110. "filtering": {"enabled": false},
  111. "sorting": {"enabled": true}
  112. });
  113. }
  114. function draw_fwd_hosts() {
  115. ft_forwardinghoststable = FooTable.init('#forwardinghoststable', {
  116. "columns": [
  117. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  118. {"name":"host","type":"text","title":lang.host,"style":{"width":"250px"}},
  119. {"name":"source","title":lang.source,"breakpoints":"xs sm"},
  120. {"name":"keep_spam","title":lang.spamfilter, "type": "text","style":{"maxWidth":"80px","width":"80px"}},
  121. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  122. ],
  123. "rows": $.ajax({
  124. dataType: 'json',
  125. url: '/api/v1/get/fwdhost/all',
  126. jsonp: false,
  127. error: function () {
  128. console.log('Cannot draw forwarding hosts table');
  129. },
  130. success: function (data) {
  131. return process_table_data(data, 'forwardinghoststable');
  132. }
  133. }),
  134. "empty": lang.empty,
  135. "paging": {"enabled": true,"limit": 5,"size": log_pagination_size},
  136. "sorting": {"enabled": true}
  137. });
  138. }
  139. function draw_relayhosts() {
  140. ft_relayhoststable = FooTable.init('#relayhoststable', {
  141. "columns": [
  142. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  143. {"name":"id","type":"text","title":"ID","style":{"width":"50px"}},
  144. {"name":"hostname","type":"text","title":lang.host,"style":{"width":"250px"}},
  145. {"name":"username","title":lang.username,"breakpoints":"xs sm"},
  146. {"name":"used_by_domains","title":lang.in_use_by,"style":{"width":"110px"}, "type": "text","breakpoints":"xs sm"},
  147. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  148. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"280px","width":"280px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  149. ],
  150. "rows": $.ajax({
  151. dataType: 'json',
  152. url: '/api/v1/get/relayhost/all',
  153. jsonp: false,
  154. error: function () {
  155. console.log('Cannot draw forwarding hosts table');
  156. },
  157. success: function (data) {
  158. return process_table_data(data, 'relayhoststable');
  159. }
  160. }),
  161. "empty": lang.empty,
  162. "paging": {"enabled": true,"limit": 5,"size": log_pagination_size},
  163. "sorting": {"enabled": true}
  164. });
  165. }
  166. function draw_queue() {
  167. ft_queuetable = FooTable.init('#queuetable', {
  168. "columns": [
  169. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  170. {"name":"queue_id","type":"text","title":"QID","style":{"width":"50px"}},
  171. {"name":"queue_name","type":"text","title":"Queue","style":{"width":"120px"}},
  172. {"name":"arrival_time","formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleString();},"title":lang.arrival_time,"style":{"width":"170px"}},
  173. {"name":"message_size","style":{"whiteSpace":"nowrap"},"title":lang.message_size,"formatter": function(value){
  174. return humanFileSize(value);
  175. }},
  176. {"name":"sender","title":lang.sender, "type": "text","breakpoints":"xs sm"},
  177. {"name":"recipients","title":lang.recipients, "type": "text","breakpoints":"xs sm"},
  178. ],
  179. "rows": $.ajax({
  180. dataType: 'json',
  181. url: '/api/v1/get/mailq',
  182. jsonp: false,
  183. error: function () {
  184. console.log('Cannot draw forwarding hosts table');
  185. },
  186. success: function (data) {
  187. return process_table_data(data, 'queuetable');
  188. }
  189. }),
  190. "empty": lang.empty,
  191. "paging": {"enabled": true,"limit": 5,"size": log_pagination_size},
  192. "sorting": {"enabled": true},
  193. "on": {
  194. "ready.ft.table": function(e, ft){
  195. table_admin_ready(ft, 'queuetable');
  196. },
  197. "after.ft.paging": function(e, ft){
  198. paging_admin_after(ft, 'queuetable');
  199. }
  200. }
  201. });
  202. }
  203. function process_table_data(data, table) {
  204. if (table == 'relayhoststable') {
  205. $.each(data, function (i, item) {
  206. item.action = '<div class="btn-group">' +
  207. '<a href="#" data-toggle="modal" id="miau" data-target="#testRelayhostModal" data-relayhost-id="' + encodeURI(item.id) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-stats"></span> Test</a>' +
  208. '<a href="/edit/relayhost/' + encodeURI(item.id) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  209. '<a href="#" data-action="delete_selected" data-id="single-rlshost" data-api-url="delete/relayhost" data-item="' + encodeURI(item.id) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  210. '</div>';
  211. item.chkbox = '<input type="checkbox" data-id="rlyhosts" name="multi_select" value="' + item.id + '" />';
  212. });
  213. } else if (table == 'queuetable') {
  214. $.each(data, function (i, item) {
  215. item.chkbox = '<input type="checkbox" data-id="mailqitems" name="multi_select" value="' + item.queue_id + '" />';
  216. item.recipients = JSON.stringify(item.recipients);
  217. });
  218. } else if (table == 'forwardinghoststable') {
  219. $.each(data, function (i, item) {
  220. item.action = '<div class="btn-group">' +
  221. '<a href="#" data-action="delete_selected" data-id="single-fwdhost" data-api-url="delete/fwdhost" data-item="' + encodeURI(item.host) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  222. '</div>';
  223. if (item.keep_spam == "yes") {
  224. item.keep_spam = lang.no;
  225. }
  226. else {
  227. item.keep_spam = lang.yes;
  228. }
  229. item.chkbox = '<input type="checkbox" data-id="fwdhosts" name="multi_select" value="' + item.host + '" />';
  230. });
  231. } else if (table == 'domainadminstable') {
  232. $.each(data, function (i, item) {
  233. item.selected_domains = escapeHtml(item.selected_domains);
  234. item.selected_domains = item.selected_domains.toString().replace(/,/g, "<br>");
  235. item.chkbox = '<input type="checkbox" data-id="domain_admins" name="multi_select" value="' + item.username + '" />';
  236. item.action = '<div class="btn-group">' +
  237. '<a href="/edit/domainadmin/' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  238. '<a href="#" data-action="delete_selected" data-id="single-domain-admin" data-api-url="delete/domain-admin" data-item="' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  239. '<a href="/index.php?duallogin=' + encodeURIComponent(item.username) + '" class="btn btn-xs btn-success"><span class="glyphicon glyphicon-user"></span> Login</a>' +
  240. '</div>';
  241. });
  242. } else if (table == 'adminstable') {
  243. $.each(data, function (i, item) {
  244. if (admin_username == item.username) {
  245. item.usr = '→ ' + item.username;
  246. } else {
  247. item.usr = item.username;
  248. }
  249. item.chkbox = '<input type="checkbox" data-id="admins" name="multi_select" value="' + item.username + '" />';
  250. item.action = '<div class="btn-group">' +
  251. '<a href="/edit/admin/' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  252. '<a href="#" data-action="delete_selected" data-id="single-admin" data-api-url="delete/admin" data-item="' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  253. '</div>';
  254. });
  255. }
  256. return data
  257. };
  258. // Initial table drawings
  259. draw_domain_admins();
  260. draw_admins();
  261. draw_fwd_hosts();
  262. draw_relayhosts();
  263. draw_queue();
  264. // Relayhost
  265. $('#testRelayhostModal').on('show.bs.modal', function (e) {
  266. $('#test_relayhost_result').text("-");
  267. button = $(e.relatedTarget)
  268. if (button != null) {
  269. $('#relayhost_id').val(button.data('relayhost-id'));
  270. }
  271. })
  272. $('#test_relayhost').on('click', function (e) {
  273. e.preventDefault();
  274. prev = $('#test_relayhost').text();
  275. $(this).prop("disabled",true);
  276. $(this).html('<span class="glyphicon glyphicon-refresh glyphicon-spin"></span> ');
  277. $.ajax({
  278. type: 'GET',
  279. url: 'inc/ajax/relay_check.php',
  280. dataType: 'text',
  281. data: $('#test_relayhost_form').serialize(),
  282. complete: function (data) {
  283. $('#test_relayhost_result').html(data.responseText);
  284. $('#test_relayhost').prop("disabled",false);
  285. $('#test_relayhost').text(prev);
  286. }
  287. });
  288. })
  289. // DKIM private key modal
  290. $('#showDKIMprivKey').on('show.bs.modal', function (e) {
  291. $('#priv_key_pre').text("-");
  292. p_related = $(e.relatedTarget)
  293. if (p_related != null) {
  294. var decoded_key = Base64.decode((p_related.data('priv-key')));
  295. $('#priv_key_pre').text(decoded_key);
  296. }
  297. })
  298. // App links
  299. function add_table_row(table_id) {
  300. var row = $('<tr />');
  301. cols = '<td><input class="input-sm form-control" data-id="app_links" type="text" name="app" required></td>';
  302. cols += '<td><input class="input-sm form-control" data-id="app_links" type="text" name="href" required></td>';
  303. cols += '<td><a href="#" role="button" class="btn btn-xs btn-default" type="button">Remove row</a></td>';
  304. row.append(cols);
  305. table_id.append(row);
  306. }
  307. $('#app_link_table').on('click', 'tr a', function (e) {
  308. e.preventDefault();
  309. $(this).parents('tr').remove();
  310. });
  311. $('#add_app_link_row').click(function() {
  312. add_table_row($('#app_link_table'));
  313. });
  314. });
  315. $(window).load(function(){
  316. initial_width = $("#sidebar-admin").width();
  317. $("#scrollbox").css("width", initial_width);
  318. if (sessionStorage.scrollTop > 70) {
  319. $('#scrollbox').addClass('scrollboxFixed');
  320. }
  321. $(window).bind('scroll', function() {
  322. if ($(window).scrollTop() > 70) {
  323. $('#scrollbox').addClass('scrollboxFixed');
  324. } else {
  325. $('#scrollbox').removeClass('scrollboxFixed');
  326. }
  327. });
  328. });
  329. function resizeScrollbox() {
  330. on_resize_width = $("#sidebar-admin").width();
  331. $("#scrollbox").removeAttr("style");
  332. $("#scrollbox").css("width", on_resize_width);
  333. }
  334. $(window).on('resize', resizeScrollbox);
  335. $('a[data-toggle="tab"]').on('shown.bs.tab', resizeScrollbox);