015-mailcow.js 13 KB

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