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