// 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}}; $(document).ready(function() { // Spam score slider var spam_slider = $('#spam_score')[0]; if (typeof spam_slider !== 'undefined') { noUiSlider.create(spam_slider, { start: user_spam_score, connect: [true, true, true], range: { 'min': [0], //stepsize is 50.000 '50%': [10], '70%': [20, 5], '80%': [50, 10], '90%': [100, 100], '95%': [1000, 1000], 'max': [5000] }, }); var connect = spam_slider.querySelectorAll('.noUi-connect'); var classes = ['c-1-color', 'c-2-color', 'c-3-color']; for (var i = 0; i < connect.length; i++) { connect[i].classList.add(classes[i]); } spam_slider.noUiSlider.on('update', function (values, handle) { $('.spam-ham-score').text('< ' + Math.round(values[0] * 10) / 10); $('.spam-spam-score').text(Math.round(values[0] * 10) / 10 + ' - ' + Math.round(values[1] * 10) / 10); $('.spam-reject-score').text('> ' + Math.round(values[1] * 10) / 10); $('#spam_score_value').val((Math.round(values[0] * 10) / 10) + ',' + (Math.round(values[1] * 10) / 10)); }); } // syncjobLogModal $('#syncjobLogModal').on('show.bs.modal', function(e) { var syncjob_id = $(e.relatedTarget).data('syncjob-id'); $.ajax({ url: '/inc/ajax/syncjob_logs.php', data: { id: syncjob_id }, dataType: 'text', success: function(data){ $(e.currentTarget).find('#logText').text(data); }, error: function(xhr, status, error) { $(e.currentTarget).find('#logText').text(xhr.responseText); } }); }); $(".arrow-toggle").on('click', function(e) { e.preventDefault(); $(this).find('.arrow').toggleClass("animation"); }); $("#pushover_delete").click(function() { return confirm(lang.delete_ays); }); }); jQuery(function($){ // http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery var entityMap = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '/': '/', '`': '`', '=': '=' }; function escapeHtml(string) { return String(string).replace(/[&<>"'`=\/]/g, function (s) { return entityMap[s]; }); } // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript function validateEmail(email) { var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(email); } function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"}); } acl_data = JSON.parse(acl); $('.clear-last-logins').on('click', function () {if (confirm(lang.delete_ays)) {last_logins('reset');}}) $(".login-history").on('click', function(e) {e.preventDefault(); last_logins('get', $(this).data('days'));$(this).addClass('active').siblings().removeClass('active');}); function last_logins(action, days = 7) { if (action == 'get') { $('.last-login').html('' + lang.waiting); $.ajax({ dataType: 'json', url: '/api/v1/get/last-login/' + encodeURIComponent(mailcow_cc_username) + '/' + days, jsonp: false, error: function () { console.log('error reading last logins'); }, success: function (data) { $('.last-login').html(); if (data.ui.time) { $('.last-login').html(' ' + lang.last_ui_login + ': ' + unix_time_format(data.ui.time)); } else { $('.last-login').text(lang.no_last_login); } if (data.sasl) { $('.last-login').append('
    '); $.each(data.sasl, function (i, item) { var datetime = new Date(item.datetime.replace(/-/g, "/")); var local_datetime = datetime.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"}); var service = '
    ' + item.service.toUpperCase() + '
    '; var app_password = item.app_password ? ' ' + escapeHtml(item.app_password_name || "App") + '' : ''; var real_rip = item.real_rip.startsWith("Web") ? item.real_rip : '' + item.real_rip + ""; var ip_location = item.location ? ' ' : ''; var ip_data = real_rip + ip_location + app_password; $(".last-login").append('
  • ' + local_datetime + " " + service + " " + lang.from + " " + ip_data + "
  • "); }) $('.last-login').append('
'); } } }) } else if (action == 'reset') { $.ajax({ dataType: 'json', url: '/api/v1/get/reset-last-login/' + encodeURIComponent(mailcow_cc_username), jsonp: false, error: function () { console.log('cannot reset last logins'); }, success: function (data) { last_logins('get'); } }) } } function draw_tla_table() { $('#tla_table').DataTable({ processing: true, serverSide: false, language: lang_datatables, ajax: { type: "GET", url: "/api/v1/get/time_limited_aliases", dataSrc: function(data){ console.log(data); $.each(data, function (i, item) { if (acl_data.spam_alias === 1) { item.action = ''; item.chkbox = ''; item.address = escapeHtml(item.address); } else { item.chkbox = ''; item.action = '-'; } }); return data; } }, 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.alias, data: 'address' }, { title: lang.alias_valid_until, data: 'validity', 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.action, data: 'action', className: 'text-md-end d-md-block dt-sm-head-hidden dt-body-right' } ] }); } function draw_sync_job_table() { $('#sync_job_table').DataTable({ processing: true, serverSide: false, language: lang_datatables, ajax: { type: "GET", url: '/api/v1/get/syncjobs/' + encodeURIComponent(mailcow_cc_username) + '/no_log', dataSrc: function(data){ console.log(data); $.each(data, function (i, item) { item.user1 = escapeHtml(item.user1); item.log = '' + lang.open_logs + '' if (!item.exclude > 0) { item.exclude = '-'; } else { item.exclude = '' + escapeHtml(item.exclude) + ''; } item.server_w_port = escapeHtml(item.user1 + '@' + item.host1 + ':' + item.port1); if (acl_data.syncjobs === 1) { item.action = ''; item.chkbox = ''; } else { item.action = '-'; item.chkbox = ''; } if (item.is_running == 1) { item.is_running = '' + lang.running + ''; } else { item.is_running = '' + lang.waiting + ''; } if (!item.last_run > 0) { item.last_run = lang.waiting; } if (item.success == null) { item.success = '-'; item.exit_status = ''; } else { item.success = ''; } if (lang['syncjob_'+item.exit_status]) { item.exit_status = lang['syncjob_'+item.exit_status]; } else if (item.success != '-') { item.exit_status = lang.syncjob_check_log; } item.exit_status = item.success + ' ' + item.exit_status; }); return data; } }, 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' }, { title: 'Server', data: 'server_w_port' }, { title: lang.encryption, data: 'enc1' }, { title: lang.username, data: 'user1' }, { title: lang.excludes, data: 'exclude' }, { title: lang.interval + " (min)", data: 'mins_interval' }, { title: lang.last_run, data: 'last_run' }, { title: lang.syncjob_last_run_result, data: 'exit_status' }, { title: 'Log', data: 'log' }, { title: lang.active, data: 'active', render: function (data, type) { return 1==data?'':0==data&&'' } }, { title: lang.status, data: 'is_running' }, { title: lang.action, data: 'action', className: 'text-md-end d-md-block dt-sm-head-hidden dt-body-right' } ] }); } function draw_app_passwd_table() { $('#app_passwd_table').DataTable({ processing: true, serverSide: false, language: lang_datatables, ajax: { type: "GET", url: '/api/v1/get/app-passwd/all', dataSrc: function(data){ console.log(data); $.each(data, function (i, item) { item.name = escapeHtml(item.name) item.protocols = [] if (item.imap_access == 1) { item.protocols.push("IMAP"); } if (item.smtp_access == 1) { item.protocols.push("SMTP"); } if (item.eas_access == 1) { item.protocols.push("EAS/ActiveSync"); } if (item.dav_access == 1) { item.protocols.push("DAV"); } if (item.pop3_access == 1) { item.protocols.push("POP3"); } if (item.sieve_access == 1) { item.protocols.push("Sieve"); } item.protocols = item.protocols.join(" ") if (acl_data.app_passwds === 1) { item.action = ''; item.chkbox = ''; } else { item.action = '-'; item.chkbox = ''; } }); return data; } }, 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' }, { title: lang.app_name, data: 'name' }, { title: lang.allowed_protocols, data: 'protocols' }, { title: lang.active, data: 'active', render: function (data, type) { return 1==data?'':0==data&&'' } }, { title: lang.action, data: 'action', className: 'text-md-end d-md-block dt-sm-head-hidden dt-body-right' } ] }); } function draw_wl_policy_mailbox_table() { $('#wl_policy_mailbox_table').DataTable({ processing: true, serverSide: false, language: lang_datatables, ajax: { type: "GET", url: '/api/v1/get/policy_wl_mailbox', dataSrc: function(data){ console.log(data); $.each(data, function (i, item) { if (validateEmail(item.object)) { item.chkbox = ''; } else { item.chkbox = ''; } if (acl_data.spam_policy === 0) { item.chkbox = ''; } }); return data; } }, 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: 'prefid' }, { title: lang.spamfilter_table_rule, data: 'name' }, { title:'Scope', data: 'object' } ] }); } function draw_bl_policy_mailbox_table() { $('#bl_policy_mailbox_table').DataTable({ processing: true, serverSide: false, language: lang_datatables, ajax: { type: "GET", url: '/api/v1/get/policy_bl_mailbox', dataSrc: function(data){ console.log(data); $.each(data, function (i, item) { if (validateEmail(item.object)) { item.chkbox = ''; } else { item.chkbox = ''; } if (acl_data.spam_policy === 0) { item.chkbox = ''; } }); return data; } }, 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: 'prefid' }, { title: lang.spamfilter_table_rule, data: 'name' }, { title:'Scope', data: 'object' } ] }); } // 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'))); } }) // Sieve data modal $('#userFilterModal').on('show.bs.modal', function(e) { $('#user_sieve_filter').text(lang.loading); $.ajax({ dataType: 'json', url: '/api/v1/get/active-user-sieve/' + encodeURIComponent(mailcow_cc_username), jsonp: false, error: function () { console.log('Cannot get active sieve script'); }, complete: function (data) { if (data.responseText == '{}') { $('#user_sieve_filter').text(lang.no_active_filter); } else { $('#user_sieve_filter').text(JSON.parse(data.responseText)); } } }) }); $('#userFilterModal').on('hidden.bs.modal', function () { $('#user_sieve_filter').text(lang.loading); }); // detect element visibility changes function onVisible(element, callback) { $(element).ready(function() { element_object = document.querySelector(element) new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if(entry.intersectionRatio > 0) { callback(element_object); observer.disconnect(); } }); }).observe(element_object); }); } // Load only if the tab is visible onVisible("[id^=tla_table]", () => draw_tla_table()); onVisible("[id^=bl_policy_mailbox_table]", () => draw_bl_policy_mailbox_table()); onVisible("[id^=wl_policy_mailbox_table]", () => draw_wl_policy_mailbox_table()); onVisible("[id^=sync_job_table]", () => draw_sync_job_table()); onVisible("[id^=app_passwd_table]", () => draw_app_passwd_table()); last_logins('get'); });