admin.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 jq(myid) {return "#" + myid.replace( /(:|\.|\[|\]|,|=|@)/g, "\\$1" );}
  7. function escapeHtml(n){return String(n).replace(/[&<>"'`=\/]/g,function(n){return entityMap[n]})}
  8. function validateRegex(e){var t=e.split("/"),n=e,r="";t.length>1&&(n=t[1],r=t[2]);try{return new RegExp(n,r),!0}catch(e){return!1}}
  9. 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]}
  10. function hashCode(t){for(var n=0,r=0;r<t.length;r++)n=t.charCodeAt(r)+((n<<5)-n);return n}
  11. function intToRGB(t){var n=(16777215&t).toString(16).toUpperCase();return"00000".substring(0,6-n.length)+n}
  12. $("#dkim_missing_keys").on('click', function(e) {
  13. e.preventDefault();
  14. var domains = [];
  15. $('.dkim_missing').each(function() {
  16. domains.push($(this).val());
  17. });
  18. $('#dkim_add_domains').val(domains);
  19. });
  20. $(".arrow-toggle").on('click', function(e) { e.preventDefault(); $(this).find('.arrow').toggleClass("animation"); });
  21. $("#mass_exclude").change(function(){ $("#mass_include").selectpicker('deselectAll'); });
  22. $("#mass_include").change(function(){ $("#mass_exclude").selectpicker('deselectAll'); });
  23. $("#mass_disarm").click(function() { $("#mass_send").attr("disabled", !this.checked); });
  24. $(".admin-ays-dialog").click(function() { return confirm(lang.ays); });
  25. $(".validate_rspamd_regex").click(function( event ) {
  26. event.preventDefault();
  27. var regex_map_id = $(this).data('regex-map');
  28. var regex_data = $(jq(regex_map_id)).val().split(/\r?\n/);
  29. var regex_valid = true;
  30. for(var i = 0;i < regex_data.length;i++){
  31. if(regex_data[i].startsWith('#') || !regex_data[i]){
  32. continue;
  33. }
  34. if(!validateRegex(regex_data[i])) {
  35. mailcow_alert_box('Cannot build regex from line ' + (i+1), 'danger');
  36. var regex_valid = false;
  37. break;
  38. }
  39. if(!regex_data[i].startsWith('/') || !/\/[ims]?$/.test(regex_data[i])){
  40. mailcow_alert_box('Line ' + (i+1) + ' is invalid', 'danger');
  41. var regex_valid = false;
  42. break;
  43. }
  44. }
  45. if (regex_valid) {
  46. mailcow_alert_box('Regex OK', 'success');
  47. $('button[data-id="' + regex_map_id + '"]').attr({"disabled": false});
  48. }
  49. });
  50. $('.textarea-code').on('keyup', function() {
  51. $('.submit_rspamd_regex').attr({"disabled": true});
  52. });
  53. $("#show_rspamd_global_filters").click(function() {
  54. $.get("inc/ajax/show_rspamd_global_filters.php");
  55. $("#confirm_show_rspamd_global_filters").hide();
  56. $("#rspamd_global_filters").removeClass("d-none");
  57. });
  58. $("#super_delete").click(function() { return confirm(lang.queue_ays); });
  59. $(".refresh_table").on('click', function(e) {
  60. e.preventDefault();
  61. var table_name = $(this).data('table');
  62. $('#' + table_name).find("tr.footable-empty").remove();
  63. draw_table = $(this).data('draw');
  64. eval(draw_table + '()');
  65. });
  66. function draw_domain_admins() {
  67. $('#domainadminstable').DataTable({
  68. processing: true,
  69. serverSide: false,
  70. language: lang_datatables,
  71. ajax: {
  72. type: "GET",
  73. url: "/api/v1/get/domain-admin/all",
  74. dataSrc: function(data){
  75. return process_table_data(data, 'domainadminstable');
  76. }
  77. },
  78. columns: [
  79. {
  80. // placeholder, so checkbox will not block child row toggle
  81. title: '',
  82. data: null,
  83. searchable: false,
  84. orderable: false,
  85. defaultContent: ''
  86. },
  87. {
  88. title: '',
  89. data: 'chkbox'
  90. },
  91. {
  92. title: lang.username,
  93. data: 'username',
  94. },
  95. {
  96. title: "TFA",
  97. data: 'tfa_active',
  98. render: function (data, type) {
  99. if(data == 1) return '<i class="bi bi-check-lg"></i>';
  100. else return '<i class="bi bi-x-lg"></i>'
  101. }
  102. },
  103. {
  104. title: lang.admin_domains,
  105. data: 'tfa_active',
  106. render: function (data, type) {
  107. return data;
  108. }
  109. },
  110. {
  111. title: lang.active,
  112. data: 'active',
  113. render: function (data, type) {
  114. if(data == 1) return '<i class="bi bi-check-lg"></i>';
  115. else return '<i class="bi bi-x-lg"></i>'
  116. }
  117. },
  118. {
  119. title: lang.action,
  120. data: 'action'
  121. },
  122. ]
  123. });
  124. }
  125. function draw_oauth2_clients() {
  126. $('#oauth2clientstable').DataTable({
  127. processing: true,
  128. serverSide: false,
  129. language: lang_datatables,
  130. ajax: {
  131. type: "GET",
  132. url: "/api/v1/get/oauth2-client/all",
  133. dataSrc: function(data){
  134. return process_table_data(data, 'oauth2clientstable');
  135. }
  136. },
  137. columns: [
  138. {
  139. // placeholder, so checkbox will not block child row toggle
  140. title: '',
  141. data: null,
  142. searchable: false,
  143. orderable: false,
  144. defaultContent: ''
  145. },
  146. {
  147. title: '',
  148. data: 'chkbox'
  149. },
  150. {
  151. title: 'ID',
  152. data: 'id',
  153. },
  154. {
  155. title: lang.oauth2_client_id,
  156. data: 'client_id'
  157. },
  158. {
  159. title: lang.oauth2_client_secret,
  160. data: 'client_secret'
  161. },
  162. {
  163. title: lang.oauth2_redirect_uri,
  164. data: 'redirect_uri'
  165. },
  166. {
  167. title: lang.action,
  168. data: 'action'
  169. },
  170. ]
  171. });
  172. }
  173. function draw_admins() {
  174. $('#adminstable').DataTable({
  175. processing: true,
  176. serverSide: false,
  177. language: lang_datatables,
  178. ajax: {
  179. type: "GET",
  180. url: "/api/v1/get/admin/all",
  181. dataSrc: function(data){
  182. return process_table_data(data, 'adminstable');
  183. }
  184. },
  185. columns: [
  186. {
  187. // placeholder, so checkbox will not block child row toggle
  188. title: '',
  189. data: null,
  190. searchable: false,
  191. orderable: false,
  192. defaultContent: ''
  193. },
  194. {
  195. title: '',
  196. data: 'chkbox'
  197. },
  198. {
  199. title: lang.username,
  200. data: 'username',
  201. },
  202. {
  203. title: "TFA",
  204. data: 'tfa_active',
  205. render: function (data, type) {
  206. if(data == 1) return '<i class="bi bi-check-lg"></i>';
  207. else return '<i class="bi bi-x-lg"></i>'
  208. }
  209. },
  210. {
  211. title: lang.active,
  212. data: 'active',
  213. render: function (data, type) {
  214. if(data == 1) return '<i class="bi bi-check-lg"></i>';
  215. else return '<i class="bi bi-x-lg"></i>'
  216. }
  217. },
  218. {
  219. title: lang.action,
  220. data: 'action'
  221. },
  222. ]
  223. });
  224. }
  225. function draw_fwd_hosts() {
  226. $('#forwardinghoststable').DataTable({
  227. processing: true,
  228. serverSide: false,
  229. language: lang_datatables,
  230. ajax: {
  231. type: "GET",
  232. url: "/api/v1/get/fwdhost/all",
  233. dataSrc: function(data){
  234. return process_table_data(data, 'forwardinghoststable');
  235. }
  236. },
  237. columns: [
  238. {
  239. // placeholder, so checkbox will not block child row toggle
  240. title: '',
  241. data: null,
  242. searchable: false,
  243. orderable: false,
  244. defaultContent: ''
  245. },
  246. {
  247. title: '',
  248. data: 'chkbox'
  249. },
  250. {
  251. title: lang.host,
  252. data: 'host',
  253. },
  254. {
  255. title: lang.source,
  256. data: 'source'
  257. },
  258. {
  259. title: lang.spamfilter,
  260. data: 'keep_spam'
  261. },
  262. {
  263. title: lang.action,
  264. data: 'action'
  265. },
  266. ]
  267. });
  268. }
  269. function draw_relayhosts() {
  270. $('#relayhoststable').DataTable({
  271. processing: true,
  272. serverSide: false,
  273. language: lang_datatables,
  274. ajax: {
  275. type: "GET",
  276. url: "/api/v1/get/relayhost/all",
  277. dataSrc: function(data){
  278. return process_table_data(data, 'relayhoststable');
  279. }
  280. },
  281. columns: [
  282. {
  283. // placeholder, so checkbox will not block child row toggle
  284. title: '',
  285. data: null,
  286. searchable: false,
  287. orderable: false,
  288. defaultContent: ''
  289. },
  290. {
  291. title: '',
  292. data: 'chkbox'
  293. },
  294. {
  295. title: 'ID',
  296. data: 'id',
  297. },
  298. {
  299. title: lang.host,
  300. data: 'hostname'
  301. },
  302. {
  303. title: lang.username,
  304. data: 'username'
  305. },
  306. {
  307. title: lang.in_use_by,
  308. data: 'in_use_by'
  309. },
  310. {
  311. title: lang.active,
  312. data: 'active'
  313. },
  314. {
  315. title: lang.action,
  316. data: 'action'
  317. },
  318. ]
  319. });
  320. }
  321. function draw_transport_maps() {
  322. $('#transportstable').DataTable({
  323. processing: true,
  324. serverSide: false,
  325. language: lang_datatables,
  326. ajax: {
  327. type: "GET",
  328. url: "/api/v1/get/transport/all",
  329. dataSrc: function(data){
  330. return process_table_data(data, 'transportstable');
  331. }
  332. },
  333. columns: [
  334. {
  335. // placeholder, so checkbox will not block child row toggle
  336. title: '',
  337. data: null,
  338. searchable: false,
  339. orderable: false,
  340. defaultContent: ''
  341. },
  342. {
  343. title: '',
  344. data: 'chkbox'
  345. },
  346. {
  347. title: 'ID',
  348. data: 'id',
  349. },
  350. {
  351. title: lang.destination,
  352. data: 'destination'
  353. },
  354. {
  355. title: lang.nexthop,
  356. data: 'nexthop'
  357. },
  358. {
  359. title: lang.username,
  360. data: 'username'
  361. },
  362. {
  363. title: lang.active,
  364. data: 'active'
  365. },
  366. {
  367. title: lang.action,
  368. data: 'action'
  369. },
  370. ]
  371. });
  372. }
  373. function draw_queue() {
  374. $('#queuetable').DataTable({
  375. processing: true,
  376. serverSide: false,
  377. language: lang_datatables,
  378. ajax: {
  379. type: "GET",
  380. url: "/api/v1/get/mailq/all",
  381. dataSrc: function(data){
  382. return process_table_data(data, 'queuetable');
  383. }
  384. },
  385. columns: [
  386. {
  387. // placeholder, so checkbox will not block child row toggle
  388. title: '',
  389. data: null,
  390. searchable: false,
  391. orderable: false,
  392. defaultContent: ''
  393. },
  394. {
  395. title: '',
  396. data: 'chkbox'
  397. },
  398. {
  399. title: 'QID',
  400. data: 'queue_id',
  401. },
  402. {
  403. title: 'Queue',
  404. data: 'queue_name'
  405. },
  406. {
  407. title: lang.arrival_time,
  408. data: 'arrival_time',
  409. render: function (data, type){
  410. var date = new Date(data ? data * 1000 : 0);
  411. return date.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"});
  412. }
  413. },
  414. {
  415. title: lang.message_size,
  416. data: 'message_size',
  417. render: function (data, type){
  418. return humanFileSize(data);
  419. }
  420. },
  421. {
  422. title: lang.sender,
  423. data: 'sender'
  424. },
  425. {
  426. title: lang.recipients,
  427. data: 'recipients'
  428. },
  429. {
  430. title: lang.action,
  431. data: 'action'
  432. },
  433. ]
  434. });
  435. }
  436. function process_table_data(data, table) {
  437. if (table == 'relayhoststable') {
  438. $.each(data, function (i, item) {
  439. item.action = '<div class="btn-group footable-actions">' +
  440. '<a href="#" data-bs-toggle="modal" data-bs-target="#testTransportModal" data-transport-id="' + encodeURI(item.id) + '" data-transport-type="sender-dependent" class="btn btn-xs btn-xs-third btn-secondary"><i class="bi bi-caret-right-fill"></i> Test</a>' +
  441. '<a href="/edit/relayhost/' + encodeURI(item.id) + '" class="btn btn-xs btn-xs-third btn-secondary"><i class="bi bi-pencil-fill"></i> ' + lang.edit + '</a>' +
  442. '<a href="#" data-action="delete_selected" data-id="single-rlyhost" data-api-url="delete/relayhost" data-item="' + encodeURI(item.id) + '" class="btn btn-xs btn-xs-third btn-danger"><i class="bi bi-trash"></i> ' + lang.remove + '</a>' +
  443. '</div>';
  444. if (item.used_by_mailboxes == '') { item.in_use_by = item.used_by_domains; }
  445. else if (item.used_by_domains == '') { item.in_use_by = item.used_by_mailboxes; }
  446. else { item.in_use_by = item.used_by_mailboxes + '<hr style="margin:5px 0px 5px 0px;">' + item.used_by_domains; }
  447. item.chkbox = '<input type="checkbox" data-id="rlyhosts" name="multi_select" value="' + item.id + '" />';
  448. });
  449. } else if (table == 'transportstable') {
  450. $.each(data, function (i, item) {
  451. if (item.is_mx_based) {
  452. item.destination = '<i class="bi bi-info-circle-fill text-info mx-info" data-bs-toggle="tooltip" title="' + lang.is_mx_based + '"></i> <code>' + item.destination + '</code>';
  453. }
  454. if (item.username) {
  455. item.username = '<i style="color:#' + intToRGB(hashCode(item.nexthop)) + ';" class="bi bi-square-fill"></i> ' + item.username;
  456. }
  457. item.action = '<div class="btn-group footable-actions">' +
  458. '<a href="#" data-bs-toggle="modal" data-bs-target="#testTransportModal" data-transport-id="' + encodeURI(item.id) + '" data-transport-type="transport-map" class="btn btn-xs btn-xs-third btn-secondary"><i class="bi bi-caret-right-fill"></i> Test</a>' +
  459. '<a href="/edit/transport/' + encodeURI(item.id) + '" class="btn btn-xs btn-xs-third btn-secondary"><i class="bi bi-pencil-fill"></i> ' + lang.edit + '</a>' +
  460. '<a href="#" data-action="delete_selected" data-id="single-transport" data-api-url="delete/transport" data-item="' + encodeURI(item.id) + '" class="btn btn-xs btn-xs-third btn-danger"><i class="bi bi-trash"></i> ' + lang.remove + '</a>' +
  461. '</div>';
  462. item.chkbox = '<input type="checkbox" data-id="transports" name="multi_select" value="' + item.id + '" />';
  463. });
  464. } else if (table == 'queuetable') {
  465. $.each(data, function (i, item) {
  466. item.chkbox = '<input type="checkbox" data-id="mailqitems" name="multi_select" value="' + item.queue_id + '" />';
  467. rcpts = $.map(item.recipients, function(i) {
  468. return escapeHtml(i);
  469. });
  470. item.recipients = rcpts.join('<hr style="margin:1px!important">');
  471. item.action = '<div class="btn-group footable-actions">' +
  472. '<a href="#" data-bs-toggle="modal" data-bs-target="#showQueuedMsg" data-queue-id="' + encodeURI(item.queue_id) + '" class="btn btn-xs btn-secondary">' + lang.queue_show_message + '</a>' +
  473. '</div>';
  474. });
  475. } else if (table == 'forwardinghoststable') {
  476. $.each(data, function (i, item) {
  477. item.action = '<div class="btn-group footable-actions">' +
  478. '<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"><i class="bi bi-trash"></i> ' + lang.remove + '</a>' +
  479. '</div>';
  480. item.chkbox = '<input type="checkbox" data-id="fwdhosts" name="multi_select" value="' + item.host + '" />';
  481. });
  482. } else if (table == 'oauth2clientstable') {
  483. $.each(data, function (i, item) {
  484. item.action = '<div class="btn-group footable-actions">' +
  485. '<a href="/edit.php?oauth2client=' + encodeURI(item.id) + '" class="btn btn-xs btn-xs-half btn-secondary"><i class="bi bi-pencil-fill"></i> ' + lang.edit + '</a>' +
  486. '<a href="#" data-action="delete_selected" data-id="single-oauth2-client" data-api-url="delete/oauth2-client" data-item="' + encodeURI(item.id) + '" class="btn btn-xs btn-xs-half btn-danger"><i class="bi bi-trash"></i> ' + lang.remove + '</a>' +
  487. '</div>';
  488. item.scope = "profile";
  489. item.grant_types = 'refresh_token password authorization_code';
  490. item.chkbox = '<input type="checkbox" data-id="oauth2_clients" name="multi_select" value="' + item.id + '" />';
  491. });
  492. } else if (table == 'domainadminstable') {
  493. $.each(data, function (i, item) {
  494. item.selected_domains = escapeHtml(item.selected_domains);
  495. item.selected_domains = item.selected_domains.toString().replace(/,/g, "<br>");
  496. item.chkbox = '<input type="checkbox" data-id="domain_admins" name="multi_select" value="' + item.username + '" />';
  497. item.action = '<div class="btn-group footable-actions">' +
  498. '<a href="/edit/domainadmin/' + encodeURI(item.username) + '" class="btn btn-xs btn-xs-third btn-secondary"><i class="bi bi-pencil-fill"></i> ' + lang.edit + '</a>' +
  499. '<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-xs-third btn-danger"><i class="bi bi-trash"></i> ' + lang.remove + '</a>' +
  500. '<a href="/index.php?duallogin=' + encodeURIComponent(item.username) + '" class="btn btn-xs btn-xs-third btn-success"><i class="bi bi-person-fill"></i> Login</a>' +
  501. '</div>';
  502. });
  503. } else if (table == 'adminstable') {
  504. $.each(data, function (i, item) {
  505. if (admin_username.toLowerCase() == item.username.toLowerCase()) {
  506. item.usr = '<i class="bi bi-person-check"></i> ' + item.username;
  507. } else {
  508. item.usr = item.username;
  509. }
  510. item.chkbox = '<input type="checkbox" data-id="admins" name="multi_select" value="' + item.username + '" />';
  511. item.action = '<div class="btn-group footable-actions">' +
  512. '<a href="/edit/admin/' + encodeURI(item.username) + '" class="btn btn-xs btn-xs-half btn-secondary"><i class="bi bi-pencil-fill"></i> ' + lang.edit + '</a>' +
  513. '<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-xs-half btn-danger"><i class="bi bi-trash"></i> ' + lang.remove + '</a>' +
  514. '</div>';
  515. });
  516. }
  517. return data
  518. };
  519. // detect element visibility changes
  520. function onVisible(element, callback) {
  521. $(element).ready(function() {
  522. element_object = document.querySelector(element)
  523. new IntersectionObserver((entries, observer) => {
  524. entries.forEach(entry => {
  525. if(entry.intersectionRatio > 0) {
  526. callback(element_object);
  527. observer.disconnect();
  528. }
  529. });
  530. }).observe(element_object);
  531. });
  532. }
  533. // Draw Table if tab is active
  534. onVisible("[id^=adminstable]", () => draw_admins());
  535. onVisible("[id^=domainadminstable]", () => draw_domain_admins());
  536. onVisible("[id^=oauth2clientstable]", () => draw_oauth2_clients());
  537. onVisible("[id^=forwardinghoststable]", () => draw_fwd_hosts());
  538. onVisible("[id^=relayhoststable]", () => draw_relayhosts());
  539. onVisible("[id^=transportstable]", () => draw_transport_maps());
  540. onVisible("[id^=queuetable]", () => draw_queue());
  541. $('body').on('click', 'span.footable-toggle', function () {
  542. event.stopPropagation();
  543. })
  544. // API IP check toggle
  545. $("#skip_ip_check_ro").click(function( event ) {
  546. $("#skip_ip_check_ro").not(this).prop('checked', false);
  547. if ($("#skip_ip_check_ro:checked").length > 0) {
  548. $('#allow_from_ro').prop('disabled', true);
  549. }
  550. else {
  551. $("#allow_from_ro").removeAttr('disabled');
  552. }
  553. });
  554. $("#skip_ip_check_rw").click(function( event ) {
  555. $("#skip_ip_check_rw").not(this).prop('checked', false);
  556. if ($("#skip_ip_check_rw:checked").length > 0) {
  557. $('#allow_from_rw').prop('disabled', true);
  558. }
  559. else {
  560. $("#allow_from_rw").removeAttr('disabled');
  561. }
  562. });
  563. // Relayhost
  564. $('#testRelayhostModal').on('show.bs.modal', function (e) {
  565. $('#test_relayhost_result').text("-");
  566. button = $(e.relatedTarget)
  567. if (button != null) {
  568. $('#relayhost_id').val(button.data('relayhost-id'));
  569. }
  570. })
  571. $('#test_relayhost').on('click', function (e) {
  572. e.preventDefault();
  573. prev = $('#test_relayhost').text();
  574. $(this).prop("disabled",true);
  575. $(this).html('<i class="bi bi-arrow-repeat icon-spin"></i> ');
  576. $.ajax({
  577. type: 'GET',
  578. url: 'inc/ajax/relay_check.php',
  579. dataType: 'text',
  580. data: $('#test_relayhost_form').serialize(),
  581. complete: function (data) {
  582. $('#test_relayhost_result').html(data.responseText);
  583. $('#test_relayhost').prop("disabled",false);
  584. $('#test_relayhost').text(prev);
  585. }
  586. });
  587. })
  588. // Transport
  589. $('#testTransportModal').on('show.bs.modal', function (e) {
  590. $('#test_transport_result').text("-");
  591. button = $(e.relatedTarget)
  592. if (button != null) {
  593. $('#transport_id').val(button.data('transport-id'));
  594. $('#transport_type').val(button.data('transport-type'));
  595. }
  596. })
  597. // Queue item
  598. $('#showQueuedMsg').on('show.bs.modal', function (e) {
  599. $('#queue_msg_content').text(lang.loading);
  600. button = $(e.relatedTarget)
  601. if (button != null) {
  602. $('#queue_id').text(button.data('queue-id'));
  603. }
  604. $.ajax({
  605. type: 'GET',
  606. url: '/api/v1/get/postcat/' + button.data('queue-id'),
  607. dataType: 'text',
  608. complete: function (data) {
  609. $('#queue_msg_content').text(data.responseText);
  610. }
  611. });
  612. })
  613. $('#test_transport').on('click', function (e) {
  614. e.preventDefault();
  615. prev = $('#test_transport').text();
  616. $(this).prop("disabled",true);
  617. $(this).html('<i class="bi bi-arrow-repeat icon-spin"></i> ');
  618. $.ajax({
  619. type: 'GET',
  620. url: 'inc/ajax/transport_check.php',
  621. dataType: 'text',
  622. data: $('#test_transport_form').serialize(),
  623. complete: function (data) {
  624. $('#test_transport_result').html(data.responseText);
  625. $('#test_transport').prop("disabled",false);
  626. $('#test_transport').text(prev);
  627. }
  628. });
  629. })
  630. // DKIM private key modal
  631. $('#showDKIMprivKey').on('show.bs.modal', function (e) {
  632. $('#priv_key_pre').text("-");
  633. p_related = $(e.relatedTarget)
  634. if (p_related != null) {
  635. var decoded_key = Base64.decode((p_related.data('priv-key')));
  636. $('#priv_key_pre').text(decoded_key);
  637. }
  638. })
  639. // FIDO2 friendly name modal
  640. $('#fido2ChangeFn').on('show.bs.modal', function (e) {
  641. rename_link = $(e.relatedTarget)
  642. if (rename_link != null) {
  643. $('#fido2_cid').val(rename_link.data('cid'));
  644. $('#fido2_subject_desc').text(Base64.decode(rename_link.data('subject')));
  645. }
  646. })
  647. // App links
  648. function add_table_row(table_id, type) {
  649. var row = $('<tr />');
  650. if (type == "app_link") {
  651. cols = '<td><input class="input-sm input-xs-lg form-control" data-id="app_links" type="text" name="app" required></td>';
  652. cols += '<td><input class="input-sm input-xs-lg form-control" data-id="app_links" type="text" name="href" required></td>';
  653. cols += '<td><a href="#" role="button" class="btn btn-sm btn-xs-lg btn-secondary" type="button">' + lang.remove_row + '</a></td>';
  654. } else if (type == "f2b_regex") {
  655. cols = '<td><input style="text-align:center" class="input-sm input-xs-lg form-control" data-id="f2b_regex" type="text" value="+" disabled></td>';
  656. cols += '<td><input class="input-sm input-xs-lg form-control regex-input" data-id="f2b_regex" type="text" name="regex" required></td>';
  657. cols += '<td><a href="#" role="button" class="btn btn-sm btn-xs-lg btn-secondary" type="button">' + lang.remove_row + '</a></td>';
  658. }
  659. row.append(cols);
  660. table_id.append(row);
  661. }
  662. $('#app_link_table').on('click', 'tr a', function (e) {
  663. e.preventDefault();
  664. $(this).parents('tr').remove();
  665. });
  666. $('#f2b_regex_table').on('click', 'tr a', function (e) {
  667. e.preventDefault();
  668. $(this).parents('tr').remove();
  669. });
  670. $('#add_app_link_row').click(function() {
  671. add_table_row($('#app_link_table'), "app_link");
  672. });
  673. $('#add_f2b_regex_row').click(function() {
  674. add_table_row($('#f2b_regex_table'), "f2b_regex");
  675. });
  676. });