admin.js 31 KB

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