// Base64 functions 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>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>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;e127&&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;e191&&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}}; jQuery(function($){ // http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery var entityMap={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="}; function jq(myid) {return "#" + myid.replace( /(:|\.|\[|\]|,|=|@)/g, "\\$1" );} function escapeHtml(n){return String(n).replace(/[&<>"'`=\/]/g,function(n){return entityMap[n]})} 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}} 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'; else return '' } }, { title: lang.active, data: 'active', defaultContent: '', render: function (data, type) { if(data == 1) return ''; else return '' } }, { title: lang.action, data: 'action', className: 'text-md-end dt-sm-head-hidden dt-body-right', defaultContent: '' }, ], initComplete: function(settings, json){ console.log(settings); } }); } function draw_oauth2_clients() { // just recalc width if instance already exists if ($.fn.DataTable.isDataTable('#oauth2clientstable') ) { $('#oauth2clientstable').DataTable().columns.adjust().responsive.recalc(); return; } $('#oauth2clientstable').DataTable({ processing: true, serverSide: false, language: lang_datatables, ajax: { type: "GET", url: "/api/v1/get/oauth2-client/all", dataSrc: function(data){ return process_table_data(data, 'oauth2clientstable'); } }, columns: [ { // placeholder, so checkbox will not block child row toggle title: '', data: null, searchable: false, orderable: false, defaultContent: '' }, { title: '', data: 'chkbox', searchable: false, orderable: false, defaultContent: '' }, { title: 'ID', data: 'id', defaultContent: '' }, { title: lang.oauth2_client_id, data: 'client_id', defaultContent: '' }, { title: lang.oauth2_client_secret, data: 'client_secret', defaultContent: '' }, { title: lang.oauth2_redirect_uri, data: 'redirect_uri', defaultContent: '' }, { title: lang.action, data: 'action', className: 'text-md-end dt-sm-head-hidden dt-body-right', defaultContent: '' }, ] }); } function draw_admins() { // just recalc width if instance already exists if ($.fn.DataTable.isDataTable('#adminstable') ) { $('#adminstable').DataTable().columns.adjust().responsive.recalc(); return; } $('#adminstable').DataTable({ processing: true, serverSide: false, language: lang_datatables, ajax: { type: "GET", url: "/api/v1/get/admin/all", dataSrc: function(data){ return process_table_data(data, 'adminstable'); } }, columns: [ { // placeholder, so checkbox will not block child row toggle title: '', data: null, searchable: false, orderable: false, defaultContent: '' }, { title: '', data: 'chkbox', searchable: false, orderable: false, defaultContent: '' }, { title: lang.username, data: 'username', defaultContent: '' }, { title: "TFA", data: 'tfa_active', defaultContent: '', render: function (data, type) { if(data == 1) return ''; else return '' } }, { title: lang.active, data: 'active', defaultContent: '', render: function (data, type) { if(data == 1) return ''; else return '' } }, { title: lang.action, data: 'action', defaultContent: '', className: 'text-md-end dt-sm-head-hidden dt-body-right' }, ] }); } function draw_fwd_hosts() { // just recalc width if instance already exists if ($.fn.DataTable.isDataTable('#forwardinghoststable') ) { $('#forwardinghoststable').DataTable().columns.adjust().responsive.recalc(); return; } $('#forwardinghoststable').DataTable({ processing: true, serverSide: false, language: lang_datatables, ajax: { type: "GET", url: "/api/v1/get/fwdhost/all", dataSrc: function(data){ return process_table_data(data, 'forwardinghoststable'); } }, columns: [ { // placeholder, so checkbox will not block child row toggle title: '', data: null, searchable: false, orderable: false, defaultContent: '' }, { title: '', data: 'chkbox', searchable: false, orderable: false, defaultContent: '' }, { title: lang.host, data: 'host', defaultContent: '' }, { title: lang.source, data: 'source', defaultContent: '' }, { title: lang.spamfilter, data: 'keep_spam', defaultContent: '' }, { title: lang.action, data: 'action', className: 'text-md-end dt-sm-head-hidden dt-body-right', defaultContent: '' }, ] }); } function draw_relayhosts() { // just recalc width if instance already exists if ($.fn.DataTable.isDataTable('#relayhoststable') ) { $('#relayhoststable').DataTable().columns.adjust().responsive.recalc(); return; } $('#relayhoststable').DataTable({ processing: true, serverSide: false, language: lang_datatables, ajax: { type: "GET", url: "/api/v1/get/relayhost/all", dataSrc: function(data){ return process_table_data(data, 'relayhoststable'); } }, columns: [ { // placeholder, so checkbox will not block child row toggle title: '', data: null, searchable: false, orderable: false, defaultContent: '' }, { title: '', data: 'chkbox', searchable: false, orderable: false, defaultContent: '' }, { title: 'ID', data: 'id', defaultContent: '' }, { title: lang.host, data: 'hostname', defaultContent: '' }, { title: lang.username, data: 'username', defaultContent: '' }, { title: lang.in_use_by, data: 'in_use_by', defaultContent: '' }, { title: lang.active, data: 'active', defaultContent: '', render: function (data, type) { if(data == 1) return ''; else return '' } }, { title: lang.action, data: 'action', className: 'text-md-end dt-sm-head-hidden dt-body-right', defaultContent: '' }, ] }); } function draw_transport_maps() { // just recalc width if instance already exists if ($.fn.DataTable.isDataTable('#transportstable') ) { $('#transportstable').DataTable().columns.adjust().responsive.recalc(); return; } $('#transportstable').DataTable({ processing: true, serverSide: false, language: lang_datatables, ajax: { type: "GET", url: "/api/v1/get/transport/all", dataSrc: function(data){ return process_table_data(data, 'transportstable'); } }, columns: [ { // placeholder, so checkbox will not block child row toggle title: '', data: null, searchable: false, orderable: false, defaultContent: '' }, { title: '', data: 'chkbox', searchable: false, orderable: false, defaultContent: '' }, { title: 'ID', data: 'id', defaultContent: '' }, { title: lang.destination, data: 'destination', defaultContent: '' }, { title: lang.nexthop, data: 'nexthop', defaultContent: '' }, { title: lang.username, data: 'username', defaultContent: '' }, { title: lang.active, data: 'active', defaultContent: '', render: function (data, type) { if(data == 1) return ''; else return '' } }, { title: lang.action, data: 'action', className: 'text-md-end dt-sm-head-hidden dt-body-right', defaultContent: '' }, ] }); } function draw_queue() { // just recalc width if instance already exists if ($.fn.DataTable.isDataTable('#queuetable') ) { $('#queuetable').DataTable().columns.adjust().responsive.recalc(); return; } $('#queuetable').DataTable({ processing: true, serverSide: false, language: lang_datatables, ajax: { type: "GET", url: "/api/v1/get/mailq/all", dataSrc: function(data){ return process_table_data(data, 'queuetable'); } }, columns: [ { // placeholder, so checkbox will not block child row toggle title: '', data: null, searchable: false, orderable: false, defaultContent: '' }, { title: '', data: 'chkbox', searchable: false, orderable: false, defaultContent: '' }, { title: 'QID', data: 'queue_id', defaultContent: '' }, { title: 'Queue', data: 'queue_name', defaultContent: '' }, { title: lang.arrival_time, data: 'arrival_time', defaultContent: '', render: function (data, type){ var date = new Date(data ? data * 1000 : 0); return date.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"}); } }, { title: lang.message_size, data: 'message_size', defaultContent: '', render: function (data, type){ return humanFileSize(data); } }, { title: lang.sender, data: 'sender', defaultContent: '' }, { title: lang.recipients, data: 'recipients', defaultContent: '' }, { title: lang.action, data: 'action', className: 'text-md-end dt-sm-head-hidden dt-body-right', defaultContent: '' }, ] }); } function process_table_data(data, table) { if (table == 'relayhoststable') { $.each(data, function (i, item) { item.action = ''; if (item.used_by_mailboxes == '') { item.in_use_by = item.used_by_domains; } else if (item.used_by_domains == '') { item.in_use_by = item.used_by_mailboxes; } else { item.in_use_by = item.used_by_mailboxes + '
' + item.used_by_domains; } item.chkbox = ''; }); } else if (table == 'transportstable') { $.each(data, function (i, item) { if (item.is_mx_based) { item.destination = ' ' + item.destination + ''; } if (item.username) { item.username = ' ' + item.username; } item.action = ''; item.chkbox = ''; }); } else if (table == 'queuetable') { $.each(data, function (i, item) { item.chkbox = ''; rcpts = $.map(item.recipients, function(i) { return escapeHtml(i); }); item.recipients = rcpts.join('
'); item.action = ''; }); } else if (table == 'forwardinghoststable') { $.each(data, function (i, item) { item.action = ''; item.chkbox = ''; }); } else if (table == 'oauth2clientstable') { $.each(data, function (i, item) { item.action = ''; item.scope = "profile"; item.grant_types = 'refresh_token password authorization_code'; item.chkbox = ''; }); } else if (table == 'domainadminstable') { $.each(data, function (i, item) { item.selected_domains = escapeHtml(item.selected_domains); item.selected_domains = item.selected_domains.toString().replace(/,/g, "
"); item.chkbox = ''; item.action = ''; }); } else if (table == 'adminstable') { $.each(data, function (i, item) { if (admin_username.toLowerCase() == item.username.toLowerCase()) { item.usr = ' ' + item.username; } else { item.usr = item.username; } item.chkbox = ''; item.action = ''; }); } return data }; // detect element visibility changes function onVisible(element, callback) { $(document).ready(function() { element_object = document.querySelector(element); if (element_object === null) return; new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if(entry.intersectionRatio > 0) { callback(element_object); } }); }).observe(element_object); }); } // Draw Table if tab is active onVisible("[id^=adminstable]", () => draw_admins()); onVisible("[id^=domainadminstable]", () => draw_domain_admins()); onVisible("[id^=oauth2clientstable]", () => draw_oauth2_clients()); onVisible("[id^=forwardinghoststable]", () => draw_fwd_hosts()); onVisible("[id^=relayhoststable]", () => draw_relayhosts()); onVisible("[id^=transportstable]", () => draw_transport_maps()); onVisible("[id^=queuetable]", () => draw_queue()); $('body').on('click', 'span.footable-toggle', function () { event.stopPropagation(); }) // API IP check toggle $("#skip_ip_check_ro").click(function( event ) { $("#skip_ip_check_ro").not(this).prop('checked', false); if ($("#skip_ip_check_ro:checked").length > 0) { $('#allow_from_ro').prop('disabled', true); } else { $("#allow_from_ro").removeAttr('disabled'); } }); $("#skip_ip_check_rw").click(function( event ) { $("#skip_ip_check_rw").not(this).prop('checked', false); if ($("#skip_ip_check_rw:checked").length > 0) { $('#allow_from_rw').prop('disabled', true); } else { $("#allow_from_rw").removeAttr('disabled'); } }); // Relayhost $('#testRelayhostModal').on('show.bs.modal', function (e) { $('#test_relayhost_result').text("-"); button = $(e.relatedTarget) if (button != null) { $('#relayhost_id').val(button.data('relayhost-id')); } }) $('#test_relayhost').on('click', function (e) { e.preventDefault(); prev = $('#test_relayhost').text(); $(this).prop("disabled",true); $(this).html(' '); $.ajax({ type: 'GET', url: 'inc/ajax/relay_check.php', dataType: 'text', data: $('#test_relayhost_form').serialize(), complete: function (data) { $('#test_relayhost_result').html(data.responseText); $('#test_relayhost').prop("disabled",false); $('#test_relayhost').text(prev); } }); }) // Transport $('#testTransportModal').on('show.bs.modal', function (e) { $('#test_transport_result').text("-"); button = $(e.relatedTarget) if (button != null) { $('#transport_id').val(button.data('transport-id')); $('#transport_type').val(button.data('transport-type')); } }) // Queue item $('#showQueuedMsg').on('show.bs.modal', function (e) { $('#queue_msg_content').text(lang.loading); button = $(e.relatedTarget) if (button != null) { $('#queue_id').text(button.data('queue-id')); } $.ajax({ type: 'GET', url: '/api/v1/get/postcat/' + button.data('queue-id'), dataType: 'text', complete: function (data) { $('#queue_msg_content').text(data.responseText); } }); }) $('#test_transport').on('click', function (e) { e.preventDefault(); prev = $('#test_transport').text(); $(this).prop("disabled",true); $(this).html('
Loading...
'); $.ajax({ type: 'GET', url: 'inc/ajax/transport_check.php', dataType: 'text', data: $('#test_transport_form').serialize(), complete: function (data) { $('#test_transport_result').html(data.responseText); $('#test_transport').prop("disabled",false); $('#test_transport').text(prev); } }); }) // DKIM private key modal $('#showDKIMprivKey').on('show.bs.modal', function (e) { $('#priv_key_pre').text("-"); p_related = $(e.relatedTarget) if (p_related != null) { var decoded_key = Base64.decode((p_related.data('priv-key'))); $('#priv_key_pre').text(decoded_key); } }) // FIDO2 friendly name modal $('#fido2ChangeFn').on('show.bs.modal', function (e) { rename_link = $(e.relatedTarget) if (rename_link != null) { $('#fido2_cid').val(rename_link.data('cid')); $('#fido2_subject_desc').text(Base64.decode(rename_link.data('subject'))); } }) // App links function add_table_row(table_id, type) { var row = $(''); if (type == "app_link") { cols = ''; cols += ''; cols += '' + lang.remove_row + ''; } else if (type == "f2b_regex") { cols = ''; cols += ''; cols += '' + lang.remove_row + ''; } row.append(cols); table_id.append(row); } $('#app_link_table').on('click', 'tr a', function (e) { e.preventDefault(); $(this).parents('tr').remove(); }); $('#f2b_regex_table').on('click', 'tr a', function (e) { e.preventDefault(); $(this).parents('tr').remove(); }); $('#add_app_link_row').click(function() { add_table_row($('#app_link_table'), "app_link"); }); $('#add_f2b_regex_row').click(function() { add_table_row($('#f2b_regex_table'), "f2b_regex"); }); // show whats new modal if (mailcow_info.updatedAt > last_login){ var parsedSeenTimestamp = parseInt(localStorage.getItem("seenChangelog")); if (!isNaN(parsedSeenTimestamp) && mailcow_info.updatedAt < parsedSeenTimestamp) { console.log("changelog seen"); return; } $.ajax({ type: 'GET', url: 'https://api.github.com/repos/' + mailcow_info.project_owner + '/' + mailcow_info.project_repo + '/releases/tags/' + mailcow_info.version_tag, dataType: 'json', success: function (data) { var md = window.markdownit(); var result = md.render(data.body); result = parseLinks(result); $('#showWhatsNew').find(".modal-body").html(`

` + data.name + `

` + result + ` `); localStorage.setItem("seenChangelog", Math.floor(Date.now() / 1000).toString()); } }); new bootstrap.Modal(document.getElementById("showWhatsNew"), { backdrop: 'static', keyboard: false }).show(); } function parseLinks(inputText) { var replacedText, replacePattern1; replacePattern1 = /(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim; replacedText = inputText.replace(replacePattern1, (matched, index, original, input_string) => { if (matched.includes('github.com')){ // return short link if it's github link last_uri_path = matched.split('/'); last_uri_path = last_uri_path[last_uri_path.length - 1]; // adjust Full Changelog link to match last git version and new git version, if link is a compare link if (matched.includes('/compare/') && mailcow_info.last_version_tag !== ''){ matched = matched.replace(last_uri_path, mailcow_info.last_version_tag + '...' + mailcow_info.version_tag); last_uri_path = mailcow_info.last_version_tag + '...' + mailcow_info.version_tag; } return '' + last_uri_path + '
'; }; // if it's not a github link, return complete link return '' + matched + ''; }); return replacedText; } });