admin.js 29 KB

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