admin.js 11 KB

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