013-mailcow.js 13 KB

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