013-mailcow.js 15 KB

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