013-mailcow.js 15 KB

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