admin.js 29 KB

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