admin.js 31 KB

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