admin.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. },
  331. {
  332. title: lang.action,
  333. data: 'action',
  334. className: 'text-md-end dt-sm-head-hidden dt-body-right',
  335. defaultContent: ''
  336. },
  337. ]
  338. });
  339. }
  340. function draw_relayhosts() {
  341. // just recalc width if instance already exists
  342. if ($.fn.DataTable.isDataTable('#relayhoststable') ) {
  343. $('#relayhoststable').DataTable().columns.adjust().responsive.recalc();
  344. return;
  345. }
  346. $('#relayhoststable').DataTable({
  347. responsive: true,
  348. processing: true,
  349. serverSide: false,
  350. stateSave: true,
  351. dom: "<'row'<'col-sm-12 col-md-6'f><'col-sm-12 col-md-6'l>>" +
  352. "tr" +
  353. "<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
  354. language: lang_datatables,
  355. ajax: {
  356. type: "GET",
  357. url: "/api/v1/get/relayhost/all",
  358. dataSrc: function(data){
  359. return process_table_data(data, 'relayhoststable');
  360. }
  361. },
  362. columns: [
  363. {
  364. // placeholder, so checkbox will not block child row toggle
  365. title: '',
  366. data: null,
  367. searchable: false,
  368. orderable: false,
  369. defaultContent: ''
  370. },
  371. {
  372. title: '',
  373. data: 'chkbox',
  374. searchable: false,
  375. orderable: false,
  376. defaultContent: ''
  377. },
  378. {
  379. title: 'ID',
  380. data: 'id',
  381. defaultContent: ''
  382. },
  383. {
  384. title: lang.host,
  385. data: 'hostname',
  386. defaultContent: ''
  387. },
  388. {
  389. title: lang.username,
  390. data: 'username',
  391. defaultContent: ''
  392. },
  393. {
  394. title: lang.in_use_by,
  395. data: 'in_use_by',
  396. defaultContent: ''
  397. },
  398. {
  399. title: lang.active,
  400. data: 'active',
  401. defaultContent: '',
  402. render: function (data, type) {
  403. if(data == 1) return '<i class="bi bi-check-lg"></i>';
  404. else return '<i class="bi bi-x-lg"></i>'
  405. }
  406. },
  407. {
  408. title: lang.action,
  409. data: 'action',
  410. className: 'text-md-end dt-sm-head-hidden dt-body-right',
  411. defaultContent: ''
  412. },
  413. ]
  414. });
  415. }
  416. function draw_transport_maps() {
  417. // just recalc width if instance already exists
  418. if ($.fn.DataTable.isDataTable('#transportstable') ) {
  419. $('#transportstable').DataTable().columns.adjust().responsive.recalc();
  420. return;
  421. }
  422. $('#transportstable').DataTable({
  423. responsive: true,
  424. processing: true,
  425. serverSide: false,
  426. stateSave: true,
  427. dom: "<'row'<'col-sm-12 col-md-6'f><'col-sm-12 col-md-6'l>>" +
  428. "tr" +
  429. "<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
  430. language: lang_datatables,
  431. ajax: {
  432. type: "GET",
  433. url: "/api/v1/get/transport/all",
  434. dataSrc: function(data){
  435. return process_table_data(data, 'transportstable');
  436. }
  437. },
  438. columns: [
  439. {
  440. // placeholder, so checkbox will not block child row toggle
  441. title: '',
  442. data: null,
  443. searchable: false,
  444. orderable: false,
  445. defaultContent: ''
  446. },
  447. {
  448. title: '',
  449. data: 'chkbox',
  450. searchable: false,
  451. orderable: false,
  452. defaultContent: ''
  453. },
  454. {
  455. title: 'ID',
  456. data: 'id',
  457. defaultContent: ''
  458. },
  459. {
  460. title: lang.destination,
  461. data: 'destination',
  462. defaultContent: ''
  463. },
  464. {
  465. title: lang.nexthop,
  466. data: 'nexthop',
  467. defaultContent: ''
  468. },
  469. {
  470. title: lang.username,
  471. data: 'username',
  472. defaultContent: ''
  473. },
  474. {
  475. title: lang.active,
  476. data: 'active',
  477. defaultContent: '',
  478. render: function (data, type) {
  479. if(data == 1) return '<i class="bi bi-check-lg"></i>';
  480. else return '<i class="bi bi-x-lg"></i>'
  481. }
  482. },
  483. {
  484. title: lang.action,
  485. data: 'action',
  486. className: 'text-md-end dt-sm-head-hidden dt-body-right',
  487. defaultContent: ''
  488. },
  489. ]
  490. });
  491. }
  492. function process_table_data(data, table) {
  493. if (table == 'relayhoststable') {
  494. $.each(data, function (i, item) {
  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="sender-dependent" class="btn btn-xs btn-xs-third btn-secondary"><i class="bi bi-caret-right-fill"></i> Test</a>' +
  497. '<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>' +
  498. '<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>' +
  499. '</div>';
  500. if (item.used_by_mailboxes == '') { item.in_use_by = item.used_by_domains; }
  501. else if (item.used_by_domains == '') { item.in_use_by = item.used_by_mailboxes; }
  502. else { item.in_use_by = item.used_by_mailboxes + '<hr style="margin:5px 0px 5px 0px;">' + item.used_by_domains; }
  503. item.chkbox = '<input type="checkbox" data-id="rlyhosts" name="multi_select" value="' + item.id + '" />';
  504. });
  505. } else if (table == 'transportstable') {
  506. $.each(data, function (i, item) {
  507. if (item.is_mx_based) {
  508. 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>';
  509. }
  510. if (item.username) {
  511. item.username = '<i style="color:#' + intToRGB(hashCode(item.nexthop)) + ';" class="bi bi-square-fill"></i> ' + item.username;
  512. }
  513. item.action = '<div class="btn-group">' +
  514. '<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>' +
  515. '<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>' +
  516. '<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>' +
  517. '</div>';
  518. item.chkbox = '<input type="checkbox" data-id="transports" name="multi_select" value="' + item.id + '" />';
  519. });
  520. } else if (table == 'queuetable') {
  521. $.each(data, function (i, item) {
  522. item.chkbox = '<input type="checkbox" data-id="mailqitems" name="multi_select" value="' + item.queue_id + '" />';
  523. rcpts = $.map(item.recipients, function(i) {
  524. return escapeHtml(i);
  525. });
  526. item.recipients = rcpts.join('<hr style="margin:1px!important">');
  527. item.action = '<div class="btn-group">' +
  528. '<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>' +
  529. '</div>';
  530. });
  531. } else if (table == 'forwardinghoststable') {
  532. $.each(data, function (i, item) {
  533. item.action = '<div class="btn-group">' +
  534. '<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>' +
  535. '</div>';
  536. item.chkbox = '<input type="checkbox" data-id="fwdhosts" name="multi_select" value="' + item.host + '" />';
  537. });
  538. } else if (table == 'oauth2clientstable') {
  539. $.each(data, function (i, item) {
  540. item.action = '<div class="btn-group">' +
  541. '<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>' +
  542. '<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>' +
  543. '</div>';
  544. item.scope = "profile";
  545. item.grant_types = 'refresh_token password authorization_code';
  546. item.chkbox = '<input type="checkbox" data-id="oauth2_clients" name="multi_select" value="' + item.id + '" />';
  547. });
  548. } else if (table == 'domainadminstable') {
  549. $.each(data, function (i, item) {
  550. item.selected_domains = escapeHtml(item.selected_domains);
  551. item.selected_domains = item.selected_domains.toString().replace(/,/g, "<br>");
  552. item.chkbox = '<input type="checkbox" data-id="domain_admins" name="multi_select" value="' + item.username + '" />';
  553. item.action = '<div class="btn-group">' +
  554. '<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>' +
  555. '<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>' +
  556. '<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>' +
  557. '</div>';
  558. });
  559. } else if (table == 'adminstable') {
  560. $.each(data, function (i, item) {
  561. if (admin_username.toLowerCase() == item.username.toLowerCase()) {
  562. item.usr = '<i class="bi bi-person-check"></i> ' + item.username;
  563. } else {
  564. item.usr = item.username;
  565. }
  566. item.chkbox = '<input type="checkbox" data-id="admins" name="multi_select" value="' + item.username + '" />';
  567. item.action = '<div class="btn-group">' +
  568. '<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>' +
  569. '<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>' +
  570. '</div>';
  571. });
  572. }
  573. return data
  574. };
  575. // detect element visibility changes
  576. function onVisible(element, callback) {
  577. $(document).ready(function() {
  578. element_object = document.querySelector(element);
  579. if (element_object === null) return;
  580. new IntersectionObserver((entries, observer) => {
  581. entries.forEach(entry => {
  582. if(entry.intersectionRatio > 0) {
  583. callback(element_object);
  584. }
  585. });
  586. }).observe(element_object);
  587. });
  588. }
  589. // Draw Table if tab is active
  590. onVisible("[id^=adminstable]", () => draw_admins());
  591. onVisible("[id^=domainadminstable]", () => draw_domain_admins());
  592. onVisible("[id^=oauth2clientstable]", () => draw_oauth2_clients());
  593. onVisible("[id^=forwardinghoststable]", () => draw_fwd_hosts());
  594. onVisible("[id^=relayhoststable]", () => draw_relayhosts());
  595. onVisible("[id^=transportstable]", () => draw_transport_maps());
  596. $('body').on('click', 'span.footable-toggle', function () {
  597. event.stopPropagation();
  598. })
  599. // API IP check toggle
  600. $("#skip_ip_check_ro").click(function( event ) {
  601. $("#skip_ip_check_ro").not(this).prop('checked', false);
  602. if ($("#skip_ip_check_ro:checked").length > 0) {
  603. $('#allow_from_ro').prop('disabled', true);
  604. }
  605. else {
  606. $("#allow_from_ro").removeAttr('disabled');
  607. }
  608. });
  609. $("#skip_ip_check_rw").click(function( event ) {
  610. $("#skip_ip_check_rw").not(this).prop('checked', false);
  611. if ($("#skip_ip_check_rw:checked").length > 0) {
  612. $('#allow_from_rw').prop('disabled', true);
  613. }
  614. else {
  615. $("#allow_from_rw").removeAttr('disabled');
  616. }
  617. });
  618. // Relayhost
  619. $('#testRelayhostModal').on('show.bs.modal', function (e) {
  620. $('#test_relayhost_result').text("-");
  621. button = $(e.relatedTarget)
  622. if (button != null) {
  623. $('#relayhost_id').val(button.data('relayhost-id'));
  624. }
  625. })
  626. $('#test_relayhost').on('click', function (e) {
  627. e.preventDefault();
  628. prev = $('#test_relayhost').text();
  629. $(this).prop("disabled",true);
  630. $(this).html('<i class="bi bi-arrow-repeat icon-spin"></i> ');
  631. $.ajax({
  632. type: 'GET',
  633. url: 'inc/ajax/relay_check.php',
  634. dataType: 'text',
  635. data: $('#test_relayhost_form').serialize(),
  636. complete: function (data) {
  637. $('#test_relayhost_result').html(data.responseText);
  638. $('#test_relayhost').prop("disabled",false);
  639. $('#test_relayhost').text(prev);
  640. }
  641. });
  642. })
  643. // Transport
  644. $('#testTransportModal').on('show.bs.modal', function (e) {
  645. $('#test_transport_result').text("-");
  646. button = $(e.relatedTarget)
  647. if (button != null) {
  648. $('#transport_id').val(button.data('transport-id'));
  649. $('#transport_type').val(button.data('transport-type'));
  650. }
  651. })
  652. $('#test_transport').on('click', function (e) {
  653. e.preventDefault();
  654. prev = $('#test_transport').text();
  655. $(this).prop("disabled",true);
  656. $(this).html('<div class="spinner-border" role="status"><span class="visually-hidden">Loading...</span></div> ');
  657. $.ajax({
  658. type: 'GET',
  659. url: 'inc/ajax/transport_check.php',
  660. dataType: 'text',
  661. data: $('#test_transport_form').serialize(),
  662. complete: function (data) {
  663. $('#test_transport_result').html(data.responseText);
  664. $('#test_transport').prop("disabled",false);
  665. $('#test_transport').text(prev);
  666. }
  667. });
  668. })
  669. // DKIM private key modal
  670. $('#showDKIMprivKey').on('show.bs.modal', function (e) {
  671. $('#priv_key_pre').text("-");
  672. p_related = $(e.relatedTarget)
  673. if (p_related != null) {
  674. var decoded_key = Base64.decode((p_related.data('priv-key')));
  675. $('#priv_key_pre').text(decoded_key);
  676. }
  677. })
  678. // FIDO2 friendly name modal
  679. $('#fido2ChangeFn').on('show.bs.modal', function (e) {
  680. rename_link = $(e.relatedTarget)
  681. if (rename_link != null) {
  682. $('#fido2_cid').val(rename_link.data('cid'));
  683. $('#fido2_subject_desc').text(Base64.decode(rename_link.data('subject')));
  684. }
  685. })
  686. // App links
  687. function add_table_row(table_id, type) {
  688. var row = $('<tr />');
  689. if (type == "app_link") {
  690. cols = '<td><input class="input-sm input-xs-lg form-control" data-id="app_links" type="text" name="app" required></td>';
  691. cols += '<td><input class="input-sm input-xs-lg form-control" data-id="app_links" type="text" name="href" required></td>';
  692. 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>';
  693. } else if (type == "f2b_regex") {
  694. cols = '<td><input style="text-align:center" class="input-sm input-xs-lg form-control" data-id="f2b_regex" type="text" value="+" disabled></td>';
  695. cols += '<td><input class="input-sm input-xs-lg form-control regex-input" data-id="f2b_regex" type="text" name="regex" required></td>';
  696. 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>';
  697. }
  698. row.append(cols);
  699. table_id.append(row);
  700. }
  701. $('#app_link_table').on('click', 'tr a', function (e) {
  702. e.preventDefault();
  703. $(this).parents('tr').remove();
  704. });
  705. $('#f2b_regex_table').on('click', 'tr a', function (e) {
  706. e.preventDefault();
  707. $(this).parents('tr').remove();
  708. });
  709. $('#add_app_link_row').click(function() {
  710. add_table_row($('#app_link_table'), "app_link");
  711. });
  712. $('#add_f2b_regex_row').click(function() {
  713. add_table_row($('#f2b_regex_table'), "f2b_regex");
  714. });
  715. });