admin.js 28 KB

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