admin.js 27 KB

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