admin.js 13 KB

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