Matthias Andreas Benkard | d1f5b68 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 1 | const LOCALE = undefined; |
| 2 | const DATETIME_FORMAT = { |
| 3 | year: "numeric", |
| 4 | month: "2-digit", |
| 5 | day: "2-digit", |
| 6 | hour: "2-digit", |
| 7 | minute: "2-digit", |
| 8 | second: "2-digit" |
| 9 | }; |
| 10 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 11 | $(document).ready(function() { |
| 12 | // mailcow alert box generator |
| 13 | window.mailcow_alert_box = function(message, type) { |
| 14 | msg = $('<span/>').text(message).text(); |
| 15 | if (type == 'danger' || type == 'info') { |
| 16 | auto_hide = 0; |
| 17 | $('#' + localStorage.getItem("add_modal")).modal('show'); |
| 18 | localStorage.removeItem("add_modal"); |
| 19 | } else { |
| 20 | auto_hide = 5000; |
| 21 | } |
| 22 | $.notify({message: msg},{z_index: 20000, delay: auto_hide, type: type,placement: {from: "bottom",align: "right"},animate: {enter: 'animated fadeInUp',exit: 'animated fadeOutDown'}}); |
| 23 | } |
| 24 | |
Matthias Andreas Benkard | d1f5b68 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 25 | $(".generate_password").click(async function( event ) { |
| 26 | try { |
| 27 | var password_policy = await window.fetch("/api/v1/get/passwordpolicy", { method:'GET', cache:'no-cache' }); |
| 28 | var password_policy = await password_policy.json(); |
| 29 | random_passwd_length = password_policy.length; |
| 30 | } catch(err) { |
| 31 | var random_passwd_length = 8; |
| 32 | } |
| 33 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 34 | event.preventDefault(); |
| 35 | $('[data-hibp]').trigger('input'); |
| 36 | if (typeof($(this).closest("form").data('pwgen-length')) == "number") { |
| 37 | var random_passwd = GPW.pronounceable($(this).closest("form").data('pwgen-length')) |
| 38 | } |
| 39 | else { |
Matthias Andreas Benkard | d1f5b68 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 40 | var random_passwd = GPW.pronounceable(random_passwd_length) |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 41 | } |
| 42 | $(this).closest("form").find('[data-pwgen-field]').attr('type', 'text'); |
| 43 | $(this).closest("form").find('[data-pwgen-field]').val(random_passwd); |
| 44 | }); |
| 45 | function str_rot13(str) { |
| 46 | return (str + '').replace(/[a-z]/gi, function(s){ |
| 47 | return String.fromCharCode(s.charCodeAt(0) + (s.toLowerCase() < 'n' ? 13 : -13)) |
| 48 | }) |
| 49 | } |
| 50 | $(".rot-enc").html(function(){ |
| 51 | return str_rot13($(this).html()) |
| 52 | }); |
| 53 | // https://stackoverflow.com/questions/4399005/implementing-jquerys-shake-effect-with-animate |
| 54 | function shake(div,interval,distance,times) { |
| 55 | if(typeof interval === 'undefined') { |
| 56 | interval = 100; |
| 57 | } |
| 58 | if(typeof distance === 'undefined') { |
| 59 | distance = 10; |
| 60 | } |
| 61 | if(typeof times === 'undefined') { |
| 62 | times = 4; |
| 63 | } |
| 64 | $(div).css('position','relative'); |
| 65 | for(var iter=0;iter<(times+1);iter++){ |
| 66 | $(div).animate({ left: ((iter%2==0 ? distance : distance*-1))}, interval); |
| 67 | } |
| 68 | $(div).animate({ left: 0},interval); |
| 69 | } |
| 70 | |
| 71 | // form cache |
| 72 | $('[data-cached-form="true"]').formcache({key: $(this).data('id')}); |
| 73 | |
| 74 | // tooltips |
| 75 | $(function () { |
| 76 | $('[data-bs-toggle="tooltip"]').tooltip() |
| 77 | }); |
| 78 | |
| 79 | // remember last navigation pill |
| 80 | (function () { |
| 81 | 'use strict'; |
| 82 | // remember desktop tabs |
| 83 | $('button[data-bs-toggle="tab"]').on('click', function (e) { |
| 84 | if ($(this).data('dont-remember') == 1) { |
| 85 | return true; |
| 86 | } |
| 87 | var id = $(this).parents('[role="tablist"]').attr('id'); |
| 88 | var key = 'lastTag'; |
| 89 | if (id) { |
| 90 | key += ':' + id; |
| 91 | } |
| 92 | |
| 93 | var tab_id = $(e.target).attr('data-bs-target').substring(1); |
| 94 | localStorage.setItem(key, tab_id); |
| 95 | }); |
| 96 | // remember mobile tabs |
| 97 | $('button[data-bs-target^="#collapse-tab-"]').on('click', function (e) { |
| 98 | // only remember tab if its being opened |
| 99 | if ($(this).hasClass('collapsed')) return false; |
| 100 | var tab_id = $(this).closest('div[role="tabpanel"]').attr('id'); |
| 101 | |
| 102 | if ($(this).data('dont-remember') == 1) { |
| 103 | return true; |
| 104 | } |
| 105 | var id = $(this).parents('[role="tablist"]').attr('id');; |
| 106 | var key = 'lastTag'; |
| 107 | if (id) { |
| 108 | key += ':' + id; |
| 109 | } |
| 110 | |
| 111 | localStorage.setItem(key, tab_id); |
| 112 | }); |
| 113 | // open last tab |
| 114 | $('[role="tablist"]').each(function (idx, elem) { |
| 115 | var id = $(elem).attr('id'); |
| 116 | var key = 'lastTag'; |
| 117 | if (id) { |
| 118 | key += ':' + id; |
| 119 | } |
| 120 | var lastTab = localStorage.getItem(key); |
| 121 | if (lastTab) { |
| 122 | $('[data-bs-target="#' + lastTab + '"]').click(); |
| 123 | var tab = $('[id^="' + lastTab + '"]'); |
Matthias Andreas Benkard | d1f5b68 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 124 | $(tab).find('.card-body.collapse:first').collapse('show'); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 125 | } |
| 126 | }); |
| 127 | })(); |
Matthias Andreas Benkard | d1f5b68 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 128 | |
| 129 | // responsive tabs, scroll to opened tab |
| 130 | $(document).on("shown.bs.collapse shown.bs.tab", function (e) { |
| 131 | var target = $(e.target); |
| 132 | if($(window).width() <= 767) { |
| 133 | var offset = target.offset().top - 60; |
| 134 | $("html, body").stop().animate({ |
| 135 | scrollTop: offset |
| 136 | }, 100); |
| 137 | } |
| 138 | }); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 139 | |
| 140 | // IE fix to hide scrollbars when table body is empty |
| 141 | $('tbody').filter(function (index) { |
| 142 | return $(this).children().length < 1; |
| 143 | }).remove(); |
| 144 | |
| 145 | // selectpicker |
| 146 | $('select').selectpicker({ |
| 147 | 'styleBase': 'btn btn-xs-lg', |
| 148 | 'noneSelectedText': lang_footer.nothing_selected |
| 149 | }); |
| 150 | |
| 151 | // haveibeenpwned and passwd policy |
| 152 | $.ajax({ |
| 153 | url: '/api/v1/get/passwordpolicy/html', |
| 154 | type: 'GET', |
| 155 | success: function(res) { |
| 156 | $(".hibp-out").after(res); |
| 157 | } |
| 158 | }); |
| 159 | $('[data-hibp]').after('<p class="small haveibeenpwned"><i class="bi bi-shield-fill-exclamation"></i> ' + lang_footer.hibp_check + '</p><span class="hibp-out"></span>'); |
| 160 | $('[data-hibp]').on('input', function() { |
| 161 | out_field = $(this).next('.haveibeenpwned').next('.hibp-out').text('').attr('class', 'hibp-out'); |
| 162 | }); |
| 163 | $('.haveibeenpwned:not(.task-running)').on('click', function() { |
| 164 | var hibp_field = $(this) |
| 165 | $(hibp_field).addClass('task-running'); |
| 166 | var hibp_result = $(hibp_field).next('.hibp-out') |
| 167 | var password_field = $(this).prev('[data-hibp]') |
| 168 | if ($(password_field).val() == '') { |
| 169 | shake(password_field); |
| 170 | } |
| 171 | else { |
| 172 | $(hibp_result).attr('class', 'hibp-out badge fs-5 bg-info'); |
| 173 | $(hibp_result).text(lang_footer.loading); |
| 174 | var password_digest = $.sha1($(password_field).val()) |
| 175 | var digest_five = password_digest.substring(0, 5).toUpperCase(); |
| 176 | var queryURL = "https://api.pwnedpasswords.com/range/" + digest_five; |
| 177 | var compl_digest = password_digest.substring(5, 41).toUpperCase(); |
| 178 | $.ajax({ |
| 179 | url: queryURL, |
| 180 | type: 'GET', |
| 181 | success: function(res) { |
| 182 | if (res.search(compl_digest) > -1){ |
| 183 | $(hibp_result).removeClass('badge fs-5 bg-info').addClass('badge fs-5 bg-danger'); |
| 184 | $(hibp_result).text(lang_footer.hibp_nok) |
| 185 | } else { |
| 186 | $(hibp_result).removeClass('badge fs-5 bg-info').addClass('badge fs-5 bg-success'); |
| 187 | $(hibp_result).text(lang_footer.hibp_ok) |
| 188 | } |
| 189 | $(hibp_field).removeClass('task-running'); |
| 190 | }, |
| 191 | error: function(xhr, status, error) { |
| 192 | $(hibp_result).removeClass('badge fs-5 bg-info').addClass('badge fs-5 bg-warning'); |
| 193 | $(hibp_result).text('API error: ' + xhr.responseText) |
| 194 | $(hibp_field).removeClass('task-running'); |
| 195 | } |
| 196 | }); |
| 197 | } |
| 198 | }); |
| 199 | |
| 200 | // Disable disallowed inputs |
| 201 | $('[data-acl="0"]').each(function(event){ |
| 202 | if ($(this).is("a")) { |
| 203 | $(this).removeAttr("data-bs-toggle"); |
| 204 | $(this).removeAttr("data-bs-target"); |
| 205 | $(this).removeAttr("data-action"); |
| 206 | $(this).click(function(event) { |
| 207 | event.preventDefault(); |
| 208 | }); |
| 209 | } |
| 210 | if ($(this).is("select")) { |
| 211 | $(this).selectpicker('destroy'); |
| 212 | $(this).replaceWith(function() { |
| 213 | return '<label class="control-label"><b>' + this.innerText + '</b></label>'; |
| 214 | }); |
| 215 | } |
| 216 | if ($(this).hasClass('btn-group')) { |
| 217 | $(this).find('a').each(function(){ |
| 218 | $(this).removeClass('dropdown-toggle') |
| 219 | .removeAttr('data-bs-toggle') |
| 220 | .removeAttr('data-bs-target') |
| 221 | .removeAttr('data-action') |
| 222 | .removeAttr('id') |
| 223 | .attr("disabled", true); |
| 224 | $(this).click(function(event) { |
| 225 | event.preventDefault(); |
| 226 | return; |
| 227 | }); |
| 228 | }); |
| 229 | $(this).find('button').each(function() { |
| 230 | $(this).attr("disabled", true); |
| 231 | }); |
| 232 | } else if ($(this).hasClass('input-group')) { |
| 233 | $(this).find('input').each(function() { |
| 234 | $(this).removeClass('dropdown-toggle') |
| 235 | .removeAttr('data-bs-toggle') |
| 236 | .attr("disabled", true); |
| 237 | $(this).click(function(event) { |
| 238 | event.preventDefault(); |
| 239 | }); |
| 240 | }); |
| 241 | $(this).find('button').each(function() { |
| 242 | $(this).attr("disabled", true); |
| 243 | }); |
| 244 | } else if ($(this).hasClass('form-group')) { |
| 245 | $(this).find('input').each(function() { |
| 246 | $(this).attr("disabled", true); |
| 247 | }); |
| 248 | } else if ($(this).hasClass('btn')) { |
| 249 | $(this).attr("disabled", true); |
| 250 | } else if ($(this).attr('data-provide') == 'slider') { |
| 251 | $(this).attr('disabled', true); |
| 252 | } else if ($(this).is(':checkbox')) { |
| 253 | $(this).attr("disabled", true); |
| 254 | } |
| 255 | $(this).data("toggle", "tooltip"); |
| 256 | $(this).attr("title", lang_acl.prohibited); |
| 257 | $(this).tooltip(); |
| 258 | }); |
| 259 | |
| 260 | // disable submit after submitting form (not API driven buttons) |
| 261 | $('form').submit(function() { |
| 262 | if ($('form button[type="submit"]').data('submitted') == '1') { |
| 263 | return false; |
| 264 | } else { |
| 265 | $(this).find('button[type="submit"]').first().text(lang_footer.loading); |
| 266 | $('form button[type="submit"]').attr('data-submitted', '1'); |
| 267 | function disableF5(e) { if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82) e.preventDefault(); }; |
| 268 | $(document).on("keydown", disableF5); |
| 269 | } |
| 270 | }); |
| 271 | // Textarea line numbers |
| 272 | $(".textarea-code").numberedtextarea({allowTabChar: true}); |
| 273 | // trigger container restart |
| 274 | $('#RestartContainer').on('show.bs.modal', function(e) { |
| 275 | var container = $(e.relatedTarget).data('container'); |
| 276 | $('#containerName').text(container); |
| 277 | $('#triggerRestartContainer').click(function(){ |
| 278 | $(this).prop("disabled",true); |
| 279 | $(this).html('<div class="spinner-border text-white" role="status"><span class="visually-hidden">Loading...</span></div>'); |
| 280 | $('#statusTriggerRestartContainer').html(lang_footer.restarting_container); |
| 281 | $.ajax({ |
| 282 | method: 'get', |
| 283 | url: '/inc/ajax/container_ctrl.php', |
| 284 | timeout: docker_timeout, |
| 285 | data: { |
| 286 | 'service': container, |
| 287 | 'action': 'restart' |
| 288 | } |
| 289 | }) |
| 290 | .always( function (data, status) { |
| 291 | $('#statusTriggerRestartContainer').append(data); |
| 292 | var htmlResponse = $.parseHTML(data) |
| 293 | if ($(htmlResponse).find('span').hasClass('text-success')) { |
| 294 | $('#triggerRestartContainer').html('<i class="bi bi-check-lg"></i> '); |
| 295 | setTimeout(function(){ |
| 296 | $('#RestartContainer').modal('toggle'); |
| 297 | window.location = window.location.href.split("#")[0]; |
| 298 | }, 1200); |
| 299 | } else { |
| 300 | $('#triggerRestartContainer').html('<i class="bi bi-slash-lg"></i> '); |
| 301 | } |
| 302 | }) |
| 303 | }); |
| 304 | }) |
| 305 | |
| 306 | // Jquery Datatables, enable responsive plugin |
| 307 | $.extend($.fn.dataTable.defaults, { |
| 308 | responsive: true |
| 309 | }); |
Matthias Andreas Benkard | d1f5b68 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 310 | // disable default datatable click listener |
| 311 | $(document).off('click', 'tbody>tr'); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 312 | |
| 313 | // tag boxes |
| 314 | $('.tag-box .tag-add').click(function(){ |
| 315 | addTag(this); |
| 316 | }); |
| 317 | $(".tag-box .tag-input").keydown(function (e) { |
| 318 | if (e.which == 13){ |
| 319 | e.preventDefault(); |
| 320 | addTag(this); |
| 321 | } |
| 322 | }); |
| 323 | |
| 324 | // Dark Mode Loader |
| 325 | $('#dark-mode-toggle').click(toggleDarkMode); |
| 326 | if ($('#dark-mode-theme').length) { |
| 327 | $('#dark-mode-toggle').prop('checked', true); |
Matthias Andreas Benkard | d1f5b68 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 328 | $('.main-logo').addClass('d-none'); |
| 329 | $('.main-logo-dark').removeClass('d-none'); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 330 | if ($('#rspamd_logo').length) $('#rspamd_logo').attr('src', '/img/rspamd_logo_light.png'); |
| 331 | if ($('#rspamd_logo_sm').length) $('#rspamd_logo_sm').attr('src', '/img/rspamd_logo_light.png'); |
Matthias Andreas Benkard | d1f5b68 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 332 | } else { |
| 333 | $('.main-logo').removeClass('d-none'); |
| 334 | $('.main-logo-dark').addClass('d-none'); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 335 | } |
| 336 | function toggleDarkMode(){ |
| 337 | if($('#dark-mode-theme').length){ |
| 338 | $('#dark-mode-theme').remove(); |
| 339 | $('#dark-mode-toggle').prop('checked', false); |
Matthias Andreas Benkard | d1f5b68 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 340 | $('.main-logo').removeClass('d-none'); |
| 341 | $('.main-logo-dark').addClass('d-none'); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 342 | if ($('#rspamd_logo').length) $('#rspamd_logo').attr('src', '/img/rspamd_logo_dark.png'); |
| 343 | if ($('#rspamd_logo_sm').length) $('#rspamd_logo_sm').attr('src', '/img/rspamd_logo_dark.png'); |
| 344 | localStorage.setItem('theme', 'light'); |
| 345 | }else{ |
| 346 | $('head').append('<link id="dark-mode-theme" rel="stylesheet" type="text/css" href="/css/themes/mailcow-darkmode.css">'); |
| 347 | $('#dark-mode-toggle').prop('checked', true); |
Matthias Andreas Benkard | d1f5b68 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 348 | $('.main-logo').addClass('d-none'); |
| 349 | $('.main-logo-dark').removeClass('d-none'); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame] | 350 | if ($('#rspamd_logo').length) $('#rspamd_logo').attr('src', '/img/rspamd_logo_light.png'); |
| 351 | if ($('#rspamd_logo_sm').length) $('#rspamd_logo_sm').attr('src', '/img/rspamd_logo_light.png'); |
| 352 | localStorage.setItem('theme', 'dark'); |
| 353 | } |
| 354 | } |
| 355 | }); |
| 356 | |
| 357 | |
| 358 | // https://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery |
| 359 | function escapeHtml(n){var entityMap={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="}; return String(n).replace(/[&<>"'`=\/]/g,function(n){return entityMap[n]})} |
| 360 | function unescapeHtml(t){var n={"&":"&","<":"<",">":">",""":'"',"'":"'","/":"/","`":"`","=":"="};return String(t).replace(/&|<|>|"|'|/|`|=/g,function(t){return n[t]})} |
| 361 | |
| 362 | function addTag(tagAddElem, tag = null){ |
| 363 | var tagboxElem = $(tagAddElem).parent(); |
| 364 | var tagInputElem = $(tagboxElem).find(".tag-input")[0]; |
| 365 | var tagValuesElem = $(tagboxElem).find(".tag-values")[0]; |
| 366 | |
| 367 | if (!tag) |
| 368 | tag = $(tagInputElem).val(); |
| 369 | if (!tag) return; |
| 370 | var value_tags = []; |
| 371 | try { |
| 372 | value_tags = JSON.parse($(tagValuesElem).val()); |
| 373 | } catch {} |
| 374 | if (!Array.isArray(value_tags)) value_tags = []; |
| 375 | if (value_tags.includes(tag)) return; |
| 376 | |
| 377 | $('<span class="badge bg-primary tag-badge btn-badge"><i class="bi bi-tag-fill"></i> ' + escapeHtml(tag) + '</span>').insertBefore('.tag-input').click(function(){ |
| 378 | var del_tag = unescapeHtml($(this).text()); |
| 379 | var del_tags = []; |
| 380 | try { |
| 381 | del_tags = JSON.parse($(tagValuesElem).val()); |
| 382 | } catch {} |
| 383 | if (Array.isArray(del_tags)){ |
| 384 | del_tags.splice(del_tags.indexOf(del_tag), 1); |
| 385 | $(tagValuesElem).val(JSON.stringify(del_tags)); |
| 386 | } |
| 387 | $(this).remove(); |
| 388 | }); |
| 389 | |
| 390 | value_tags.push(tag); |
| 391 | $(tagValuesElem).val(JSON.stringify(value_tags)); |
| 392 | $(tagInputElem).val(''); |
| 393 | } |