admin.js 27 KB

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