admin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. $("#import_dkim_legend").on('click', function(e) {
  9. e.preventDefault();
  10. $('#import_dkim_arrow').toggleClass("animation");
  11. });
  12. $("#duplicate_dkim_legend").on('click', function(e) {
  13. e.preventDefault();
  14. $('#duplicate_dkim_arrow').toggleClass("animation");
  15. });
  16. $("#api_legend").on('click', function(e) {
  17. e.preventDefault();
  18. $('#api_arrow').toggleClass("animation");
  19. });
  20. $("#rspamd_preset_1").on('click', function(e) {
  21. e.preventDefault();
  22. $("form[data-id=rsetting]").find("#adminRspamdSettingsDesc").val(lang.rsettings_preset_1);
  23. $("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}');
  24. });
  25. $("#rspamd_preset_2").on('click', function(e) {
  26. e.preventDefault();
  27. $("form[data-id=rsetting]").find("#adminRspamdSettingsDesc").val(lang.rsettings_preset_2);
  28. $("form[data-id=rsetting]").find("#adminRspamdSettingsContent").val('priority = 10;\nrcpt = "/postmaster@.*/";\nwant_spam = yes;');
  29. });
  30. $("#dkim_missing_keys").on('click', function(e) {
  31. e.preventDefault();
  32. var domains = [];
  33. $('.dkim_missing').each(function() {
  34. domains.push($(this).val());
  35. });
  36. $('#dkim_add_domains').val(domains);
  37. });
  38. function draw_domain_admins() {
  39. ft_domainadmins = FooTable.init('#domainadminstable', {
  40. "columns": [
  41. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  42. {"sorted": true,"name":"username","title":lang.username,"style":{"width":"250px"}},
  43. {"name":"selected_domains","title":lang.admin_domains,"breakpoints":"xs sm"},
  44. {"name":"tfa_active","title":"TFA", "filterable": false,"style":{"maxWidth":"80px","width":"80px"}},
  45. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  46. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"250px","width":"250px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  47. ],
  48. "rows": $.ajax({
  49. dataType: 'json',
  50. url: '/api/v1/get/domain-admin/all',
  51. jsonp: false,
  52. error: function () {
  53. console.log('Cannot draw domain admin table');
  54. },
  55. success: function (data) {
  56. return process_table_data(data, 'domainadminstable');
  57. }
  58. }),
  59. "empty": lang.empty,
  60. "paging": {"enabled": true,"limit": 5,"size": log_pagination_size},
  61. "filtering": {"enabled": true,"delay": 1,"position": "left","connectors": false,"placeholder": lang.filter_table
  62. },
  63. "sorting": {"enabled": true}
  64. });
  65. }
  66. function draw_fwd_hosts() {
  67. ft_forwardinghoststable = FooTable.init('#forwardinghoststable', {
  68. "columns": [
  69. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  70. {"name":"host","type":"text","title":lang.host,"style":{"width":"250px"}},
  71. {"name":"source","title":lang.source,"breakpoints":"xs sm"},
  72. {"name":"keep_spam","title":lang.spamfilter, "type": "text","style":{"maxWidth":"80px","width":"80px"}},
  73. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  74. ],
  75. "rows": $.ajax({
  76. dataType: 'json',
  77. url: '/api/v1/get/fwdhost/all',
  78. jsonp: false,
  79. error: function () {
  80. console.log('Cannot draw forwarding hosts table');
  81. },
  82. success: function (data) {
  83. return process_table_data(data, 'forwardinghoststable');
  84. }
  85. }),
  86. "empty": lang.empty,
  87. "paging": {"enabled": true,"limit": 5,"size": log_pagination_size},
  88. "sorting": {"enabled": true}
  89. });
  90. }
  91. function draw_relayhosts() {
  92. ft_relayhoststable = FooTable.init('#relayhoststable', {
  93. "columns": [
  94. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  95. {"name":"id","type":"text","title":"ID","style":{"width":"50px"}},
  96. {"name":"hostname","type":"text","title":lang.host,"style":{"width":"250px"}},
  97. {"name":"username","title":lang.username,"breakpoints":"xs sm"},
  98. {"name":"used_by_domains","title":lang.in_use_by,"style":{"width":"110px"}, "type": "text","breakpoints":"xs sm"},
  99. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  100. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"280px","width":"280px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  101. ],
  102. "rows": $.ajax({
  103. dataType: 'json',
  104. url: '/api/v1/get/relayhost/all',
  105. jsonp: false,
  106. error: function () {
  107. console.log('Cannot draw forwarding hosts table');
  108. },
  109. success: function (data) {
  110. return process_table_data(data, 'relayhoststable');
  111. }
  112. }),
  113. "empty": lang.empty,
  114. "paging": {"enabled": true,"limit": 5,"size": log_pagination_size},
  115. "sorting": {"enabled": true}
  116. });
  117. }
  118. function process_table_data(data, table) {
  119. if (table == 'relayhoststable') {
  120. $.each(data, function (i, item) {
  121. item.action = '<div class="btn-group">' +
  122. '<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>' +
  123. '<a href="/edit.php?relayhost=' + encodeURI(item.id) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  124. '<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>' +
  125. '</div>';
  126. item.chkbox = '<input type="checkbox" data-id="rlyhosts" name="multi_select" value="' + item.id + '" />';
  127. });
  128. } else if (table == 'forwardinghoststable') {
  129. $.each(data, function (i, item) {
  130. item.action = '<div class="btn-group">' +
  131. '<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>' +
  132. '</div>';
  133. if (item.keep_spam == "yes") {
  134. item.keep_spam = lang.no;
  135. }
  136. else {
  137. item.keep_spam = lang.yes;
  138. }
  139. item.chkbox = '<input type="checkbox" data-id="fwdhosts" name="multi_select" value="' + item.host + '" />';
  140. });
  141. } else if (table == 'domainadminstable') {
  142. $.each(data, function (i, item) {
  143. item.selected_domains = escapeHtml(item.selected_domains.toString().replace(/,/g, " "));
  144. item.chkbox = '<input type="checkbox" data-id="domain_admins" name="multi_select" value="' + item.username + '" />';
  145. item.action = '<div class="btn-group">' +
  146. '<a href="/edit.php?domainadmin=' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  147. '<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>' +
  148. '<a href="/index.php?duallogin=' + encodeURIComponent(item.username) + '" class="btn btn-xs btn-success"><span class="glyphicon glyphicon-user"></span> Login</a>' +
  149. '</div>';
  150. });
  151. }
  152. return data
  153. };
  154. // Initial table drawings
  155. draw_domain_admins();
  156. draw_fwd_hosts();
  157. draw_relayhosts();
  158. // Relayhost
  159. $('#testRelayhostModal').on('show.bs.modal', function (e) {
  160. $('#test_relayhost_result').text("-");
  161. button = $(e.relatedTarget)
  162. if (button != null) {
  163. $('#relayhost_id').val(button.data('relayhost-id'));
  164. }
  165. })
  166. $('#test_relayhost').on('click', function (e) {
  167. e.preventDefault();
  168. prev = $('#test_relayhost').text();
  169. $(this).prop("disabled",true);
  170. $(this).html('<span class="glyphicon glyphicon-refresh glyphicon-spin"></span> ');
  171. $.ajax({
  172. type: 'GET',
  173. url: 'inc/ajax/relay_check.php',
  174. dataType: 'text',
  175. data: $('#test_relayhost_form').serialize(),
  176. complete: function (data) {
  177. $('#test_relayhost_result').html(data.responseText);
  178. $('#test_relayhost').prop("disabled",false);
  179. $('#test_relayhost').text(prev);
  180. }
  181. });
  182. })
  183. // DKIM private key modal
  184. $('#showDKIMprivKey').on('show.bs.modal', function (e) {
  185. $('#priv_key_pre').text("-");
  186. p_related = $(e.relatedTarget)
  187. if (p_related != null) {
  188. var decoded_key = Base64.decode((p_related.data('priv-key')));
  189. $('#priv_key_pre').text(decoded_key);
  190. }
  191. })
  192. // App links
  193. function add_table_row(table_id) {
  194. var row = $('<tr />');
  195. cols = '<td><input class="input-sm form-control" data-id="app_links" type="text" name="app" required></td>';
  196. cols += '<td><input class="input-sm form-control" data-id="app_links" type="text" name="href" required></td>';
  197. cols += '<td><a href="#" role="button" class="btn btn-xs btn-default" type="button">Remove row</a></td>';
  198. row.append(cols);
  199. table_id.append(row);
  200. }
  201. $('#app_link_table').on('click', 'tr a', function (e) {
  202. e.preventDefault();
  203. $(this).parents('tr').remove();
  204. });
  205. $('#add_app_link_row').click(function() {
  206. add_table_row($('#app_link_table'));
  207. });
  208. });
  209. $(window).load(function(){
  210. initial_width = $("#sidebar-admin").width();
  211. $("#scrollbox").css("width", initial_width);
  212. if (sessionStorage.scrollTop > 70) {
  213. $('#scrollbox').addClass('scrollboxFixed');
  214. }
  215. $(window).bind('scroll', function() {
  216. if ($(window).scrollTop() > 70) {
  217. $('#scrollbox').addClass('scrollboxFixed');
  218. } else {
  219. $('#scrollbox').removeClass('scrollboxFixed');
  220. }
  221. });
  222. });
  223. function resizeScrollbox() {
  224. on_resize_width = $("#sidebar-admin").width();
  225. $("#scrollbox").removeAttr("style");
  226. $("#scrollbox").css("width", on_resize_width);
  227. }
  228. $(window).on('resize', resizeScrollbox);
  229. $('a[data-toggle="tab"]').on('shown.bs.tab', resizeScrollbox);