admin.js 28 KB

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