admin.js 12 KB

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