user.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. $(document).ready(function() {
  2. // Spam score slider
  3. var spam_slider = $('#spam_score')[0];
  4. noUiSlider.create(spam_slider, {
  5. start: user_spam_score,
  6. connect: [true, true, true],
  7. range: {
  8. 'min': [0], //stepsize is 50.000
  9. '50%': [10],
  10. '70%': [20, 5],
  11. '80%': [50, 10],
  12. '90%': [100, 100],
  13. '95%': [1000, 1000],
  14. 'max': [5000]
  15. },
  16. });
  17. var connect = spam_slider.querySelectorAll('.noUi-connect');
  18. var classes = ['c-1-color', 'c-2-color', 'c-3-color'];
  19. for (var i = 0; i < connect.length; i++) {
  20. connect[i].classList.add(classes[i]);
  21. }
  22. spam_slider.noUiSlider.on('update', function (values, handle) {
  23. $('.spam-ham-score').text('< ' + Math.round(values[0] * 10) / 10);
  24. $('.spam-spam-score').text(Math.round(values[0] * 10) / 10 + ' - ' + Math.round(values[1] * 10) / 10);
  25. $('.spam-reject-score').text('> ' + Math.round(values[1] * 10) / 10);
  26. $('#spam_score_value').val((Math.round(values[0] * 10) / 10) + ',' + (Math.round(values[1] * 10) / 10));
  27. });
  28. // syncjobLogModal
  29. $('#syncjobLogModal').on('show.bs.modal', function(e) {
  30. var syncjob_id = $(e.relatedTarget).data('syncjob-id');
  31. $.ajax({
  32. url: '/inc/ajax/syncjob_logs.php',
  33. data: { id: syncjob_id },
  34. dataType: 'text',
  35. success: function(data){
  36. $(e.currentTarget).find('#logText').text(data);
  37. },
  38. error: function(xhr, status, error) {
  39. $(e.currentTarget).find('#logText').text(xhr.responseText);
  40. }
  41. });
  42. });
  43. $(".arrow-toggle").on('click', function(e) { e.preventDefault(); $(this).find('.arrow').toggleClass("animation"); });
  44. $("#pushover_delete").click(function() { return confirm(lang.delete_ays); });
  45. });
  46. jQuery(function($){
  47. // http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery
  48. var entityMap = {
  49. '&': '&amp;',
  50. '<': '&lt;',
  51. '>': '&gt;',
  52. '"': '&quot;',
  53. "'": '&#39;',
  54. '/': '&#x2F;',
  55. '`': '&#x60;',
  56. '=': '&#x3D;'
  57. };
  58. function escapeHtml(string) {
  59. return String(string).replace(/[&<>"'`=\/]/g, function (s) {
  60. return entityMap[s];
  61. });
  62. }
  63. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  64. function validateEmail(email) {
  65. var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  66. return re.test(email);
  67. }
  68. function unix_time_format(tm) {
  69. var date = new Date(tm ? tm * 1000 : 0);
  70. return date.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"});
  71. }
  72. acl_data = JSON.parse(acl);
  73. $('.clear-last-logins').on('click', function () {
  74. if (confirm(lang.delete_ays)) {
  75. last_logins('reset');
  76. }
  77. })
  78. function last_logins(action, lines = 5) {
  79. if (action == 'get') {
  80. $.ajax({
  81. dataType: 'json',
  82. url: '/api/v1/get/last-login/' + encodeURIComponent(mailcow_cc_username) + '/' + lines,
  83. jsonp: false,
  84. error: function () {
  85. console.log('error reading last logins');
  86. },
  87. success: function (data) {
  88. $('.last-login').html();
  89. if (data.ui.time) {
  90. $('.last-login').html('<i class="bi bi-person-fill"></i> ' + lang.last_ui_login + ': ' + unix_time_format(data.ui.time));
  91. } else {
  92. $('.last-login').text(lang.no_last_login);
  93. }
  94. if (data.sasl) {
  95. $('.last-login').append('<ul class="list-group">');
  96. $.each(data.sasl, function (i, item) {
  97. var datetime = new Date(item.datetime.replace(/-/g, "/"));
  98. var local_datetime = datetime.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"});
  99. if (item.location) {
  100. ip_location = '<span class="lang-sm ip-location-flag" lang="' + item.location + '"></span>';
  101. } else {
  102. ip_location = '';
  103. }
  104. if (item.service == "smtp") {
  105. service = '<div class="label label-default">' + item.service.toUpperCase() + '<i class="bi bi-chevron-compact-right"></i></div>';
  106. } else if (item.service == "imap") {
  107. service = '<div class="label label-default"><i class="bi bi-chevron-compact-left"></i> ' + item.service.toUpperCase() + '</div>';
  108. } else {
  109. service = '<div class="label label-default">' + item.service.toUpperCase() + '</div>';
  110. }
  111. if (item.real_rip.startsWith("Web")) {
  112. real_rip = item.real_rip;
  113. } else {
  114. real_rip = '<a href="https://bgp.he.net/ip/' + item.real_rip + '" target="_blank">' + item.real_rip + '</a> ';
  115. }
  116. real_rip = real_rip + ' ' + ip_location;
  117. $('.last-login').append('<li class="list-group-item">' +
  118. local_datetime + ' ' + service + ' ' + lang.from + ' ' +
  119. real_rip +
  120. '</li>');
  121. })
  122. $('.last-login').append('</ul>');
  123. }
  124. }
  125. })
  126. } else if (action == 'reset') {
  127. $.ajax({
  128. dataType: 'json',
  129. url: '/api/v1/get/reset-last-login/' + encodeURIComponent(mailcow_cc_username),
  130. jsonp: false,
  131. error: function () {
  132. console.log('cannot reset last logins');
  133. },
  134. success: function (data) {
  135. last_logins('get');
  136. }
  137. })
  138. }
  139. }
  140. function draw_tla_table() {
  141. ft_tla_table = FooTable.init('#tla_table', {
  142. "columns": [
  143. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px","text-align":"center"},"filterable": false,"sortable": false,"type":"html"},
  144. {"name":"address","title":lang.alias},
  145. {"name":"validity","formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"});},"title":lang.alias_valid_until,"style":{"width":"170px"}},
  146. {"sorted": true,"sortValue": function(value){res = new Date(value);return res.getTime();},"direction":"DESC","name":"created","formatter":function date_format(datetime) { var date = new Date(datetime.replace(/-/g, "/")); return date.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"});},"title":lang.created_on,"style":{"width":"170px"}},
  147. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  148. ],
  149. "empty": lang.empty,
  150. "rows": $.ajax({
  151. dataType: 'json',
  152. url: '/api/v1/get/time_limited_aliases',
  153. jsonp: false,
  154. error: function () {
  155. console.log('Cannot draw tla table');
  156. },
  157. success: function (data) {
  158. $.each(data, function (i, item) {
  159. if (acl_data.spam_alias === 1) {
  160. item.action = '<div class="btn-group">' +
  161. '<a href="#" data-action="delete_selected" data-id="single-tla" data-api-url="delete/time_limited_alias" data-item="' + encodeURIComponent(item.address) + '" class="btn btn-xs btn-danger"><i class="bi bi-trash"></i> ' + lang.remove + '</a>' +
  162. '</div>';
  163. item.chkbox = '<input type="checkbox" data-id="tla" name="multi_select" value="' + encodeURIComponent(item.address) + '" />';
  164. item.address = escapeHtml(item.address);
  165. }
  166. else {
  167. item.chkbox = '<input type="checkbox" disabled />';
  168. item.action = '<span>-</span>';
  169. }
  170. });
  171. }
  172. }),
  173. "paging": {
  174. "enabled": true,
  175. "limit": 5,
  176. "size": pagination_size
  177. },
  178. "state": {"enabled": true},
  179. "sorting": {
  180. "enabled": true
  181. },
  182. "toggleSelector": "table tbody span.footable-toggle"
  183. });
  184. }
  185. function draw_sync_job_table() {
  186. ft_syncjob_table = FooTable.init('#sync_job_table', {
  187. "columns": [
  188. {"name":"chkbox","title":"","style":{"maxWidth":"60px","width":"60px","text-align":"center"},"filterable": false,"sortable": false,"type":"html"},
  189. {"sorted": true,"name":"id","title":"ID","style":{"maxWidth":"60px","width":"60px","text-align":"center"}},
  190. {"name":"server_w_port","title":"Server"},
  191. {"name":"enc1","title":lang.encryption,"breakpoints":"all"},
  192. {"name":"user1","title":lang.username},
  193. {"name":"exclude","title":lang.excludes,"breakpoints":"all"},
  194. {"name":"mins_interval","title":lang.interval + " (min)","breakpoints":"all"},
  195. {"name":"last_run","title":lang.last_run,"breakpoints":"all"},
  196. {"name":"log","title":"Log"},
  197. {"name":"active","filterable": false,"style":{"maxWidth":"70px","width":"70px"},"title":lang.active,"formatter": function(value){return 1==value?'<i class="bi bi-check-lg"></i>':0==value&&'<i class="bi bi-x-lg"></i>';}},
  198. {"name":"is_running","filterable": false,"style":{"maxWidth":"120px","width":"100px"},"title":lang.status},
  199. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","min-width":"260px","width":"260px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  200. ],
  201. "empty": lang.empty,
  202. "rows": $.ajax({
  203. dataType: 'json',
  204. url: '/api/v1/get/syncjobs/' + encodeURIComponent(mailcow_cc_username) + '/no_log',
  205. jsonp: false,
  206. error: function () {
  207. console.log('Cannot draw sync job table');
  208. },
  209. success: function (data) {
  210. $.each(data, function (i, item) {
  211. item.user1 = escapeHtml(item.user1);
  212. item.log = '<a href="#syncjobLogModal" data-toggle="modal" data-syncjob-id="' + item.id + '">Open logs</a>'
  213. if (!item.exclude > 0) {
  214. item.exclude = '-';
  215. } else {
  216. item.exclude = '<code>' + escapeHtml(item.exclude) + '</code>';
  217. }
  218. item.server_w_port = escapeHtml(item.user1 + '@' + item.host1 + ':' + item.port1);
  219. if (acl_data.syncjobs === 1) {
  220. item.action = '<div class="btn-group">' +
  221. '<a href="/edit/syncjob/' + item.id + '" class="btn btn-xs btn-default"><i class="bi bi-pencil-fill"></i> ' + lang.edit + '</a>' +
  222. '<a href="#" data-action="delete_selected" data-id="single-syncjob" data-api-url="delete/syncjob" data-item="' + item.id + '" class="btn btn-xs btn-danger"><i class="bi bi-trash"></i> ' + lang.remove + '</a>' +
  223. '</div>';
  224. item.chkbox = '<input type="checkbox" data-id="syncjob" name="multi_select" value="' + item.id + '" />';
  225. }
  226. else {
  227. item.action = '<span>-</span>';
  228. item.chkbox = '<input type="checkbox" disabled />';
  229. }
  230. if (item.is_running == 1) {
  231. item.is_running = '<span id="active-script" class="label label-success">' + lang.running + '</span>';
  232. } else {
  233. item.is_running = '<span id="inactive-script" class="label label-warning">' + lang.waiting + '</span>';
  234. }
  235. if (!item.last_run > 0) {
  236. item.last_run = lang.waiting;
  237. }
  238. });
  239. }
  240. }),
  241. "paging": {
  242. "enabled": true,
  243. "limit": 5,
  244. "size": pagination_size
  245. },
  246. "state": {"enabled": true},
  247. "sorting": {
  248. "enabled": true
  249. },
  250. "toggleSelector": "table tbody span.footable-toggle"
  251. });
  252. }
  253. function draw_app_passwd_table() {
  254. ft_apppasswd_table = FooTable.init('#app_passwd_table', {
  255. "columns": [
  256. {"name":"chkbox","title":"","style":{"maxWidth":"60px","width":"60px","text-align":"center"},"filterable": false,"sortable": false,"type":"html"},
  257. {"sorted": true,"name":"id","title":"ID","style":{"maxWidth":"60px","width":"60px","text-align":"center"}},
  258. {"name":"name","title":lang.app_name},
  259. {"name":"active","filterable": false,"style":{"maxWidth":"70px","width":"70px"},"title":lang.active,"formatter": function(value){return 1==value?'<i class="bi bi-check-lg"></i>':0==value&&'<i class="bi bi-x-lg"></i>';}},
  260. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  261. ],
  262. "empty": lang.empty,
  263. "rows": $.ajax({
  264. dataType: 'json',
  265. url: '/api/v1/get/app-passwd/all',
  266. jsonp: false,
  267. error: function () {
  268. console.log('Cannot draw app passwd table');
  269. },
  270. success: function (data) {
  271. $.each(data, function (i, item) {
  272. item.name = escapeHtml(item.name);
  273. if (acl_data.app_passwds === 1) {
  274. item.action = '<div class="btn-group">' +
  275. '<a href="/edit/app-passwd/' + item.id + '" class="btn btn-xs btn-default"><i class="bi bi-pencil-fill"></i> ' + lang.edit + '</a>' +
  276. '<a href="#" data-action="delete_selected" data-id="single-apppasswd" data-api-url="delete/app-passwd" data-item="' + item.id + '" class="btn btn-xs btn-danger"><i class="bi bi-trash"></i> ' + lang.remove + '</a>' +
  277. '</div>';
  278. item.chkbox = '<input type="checkbox" data-id="apppasswd" name="multi_select" value="' + item.id + '" />';
  279. }
  280. else {
  281. item.action = '<span>-</span>';
  282. item.chkbox = '<input type="checkbox" disabled />';
  283. }
  284. });
  285. }
  286. }),
  287. "paging": {
  288. "enabled": true,
  289. "limit": 5,
  290. "size": pagination_size
  291. },
  292. "state": {"enabled": true},
  293. "sorting": {
  294. "enabled": true
  295. },
  296. "toggleSelector": "table tbody span.footable-toggle"
  297. });
  298. }
  299. function draw_wl_policy_mailbox_table() {
  300. ft_wl_policy_mailbox_table = FooTable.init('#wl_policy_mailbox_table', {
  301. "columns": [
  302. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px","text-align":"center"},"filterable": false,"sortable": false,"type":"html"},
  303. {"name":"prefid","style":{"maxWidth":"40px","width":"40px"},"title":"ID","filterable": false,"sortable": false},
  304. {"sorted": true,"name":"value","title":lang.spamfilter_table_rule},
  305. {"name":"object","title":"Scope"}
  306. ],
  307. "empty": lang.empty,
  308. "rows": $.ajax({
  309. dataType: 'json',
  310. url: '/api/v1/get/policy_wl_mailbox',
  311. jsonp: false,
  312. error: function () {
  313. console.log('Cannot draw mailbox policy wl table');
  314. },
  315. success: function (data) {
  316. $.each(data, function (i, item) {
  317. if (validateEmail(item.object)) {
  318. item.chkbox = '<input type="checkbox" data-id="policy_wl_mailbox" name="multi_select" value="' + item.prefid + '" />';
  319. }
  320. else {
  321. item.chkbox = '<input type="checkbox" disabled title="' + lang.spamfilter_table_domain_policy + '" />';
  322. }
  323. if (acl_data.spam_policy === 0) {
  324. item.chkbox = '<input type="checkbox" disabled />';
  325. }
  326. });
  327. }
  328. }),
  329. "state": {"enabled": true},
  330. "paging": {
  331. "enabled": true,
  332. "limit": 5,
  333. "size": pagination_size
  334. },
  335. "sorting": {
  336. "enabled": true
  337. }
  338. });
  339. }
  340. function draw_bl_policy_mailbox_table() {
  341. ft_bl_policy_mailbox_table = FooTable.init('#bl_policy_mailbox_table', {
  342. "columns": [
  343. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px","text-align":"center"},"filterable": false,"sortable": false,"type":"html"},
  344. {"name":"prefid","style":{"maxWidth":"40px","width":"40px"},"title":"ID","filterable": false,"sortable": false},
  345. {"sorted": true,"name":"value","title":lang.spamfilter_table_rule},
  346. {"name":"object","title":"Scope"}
  347. ],
  348. "empty": lang.empty,
  349. "rows": $.ajax({
  350. dataType: 'json',
  351. url: '/api/v1/get/policy_bl_mailbox',
  352. jsonp: false,
  353. error: function () {
  354. console.log('Cannot draw mailbox policy bl table');
  355. },
  356. success: function (data) {
  357. $.each(data, function (i, item) {
  358. if (validateEmail(item.object)) {
  359. item.chkbox = '<input type="checkbox" data-id="policy_bl_mailbox" name="multi_select" value="' + item.prefid + '" />';
  360. }
  361. else {
  362. item.chkbox = '<input type="checkbox" disabled tooltip="' + lang.spamfilter_table_domain_policy + '" />';
  363. }
  364. if (acl_data.spam_policy === 0) {
  365. item.chkbox = '<input type="checkbox" disabled />';
  366. }
  367. });
  368. }
  369. }),
  370. "paging": {
  371. "enabled": true,
  372. "limit": 5,
  373. "size": pagination_size
  374. },
  375. "state": {"enabled": true},
  376. "sorting": {
  377. "enabled": true
  378. }
  379. });
  380. }
  381. $('body').on('click', 'span.footable-toggle', function () {
  382. event.stopPropagation();
  383. })
  384. draw_sync_job_table();
  385. draw_app_passwd_table();
  386. draw_tla_table();
  387. draw_wl_policy_mailbox_table();
  388. draw_bl_policy_mailbox_table();
  389. last_logins('get');
  390. // FIDO2 friendly name modal
  391. $('#fido2ChangeFn').on('show.bs.modal', function (e) {
  392. rename_link = $(e.relatedTarget)
  393. if (rename_link != null) {
  394. $('#fido2_cid').val(rename_link.data('cid'));
  395. $('#fido2_subject_desc').text(Base64.decode(rename_link.data('subject')));
  396. }
  397. })
  398. // Sieve data modal
  399. $('#userFilterModal').on('show.bs.modal', function(e) {
  400. $('#user_sieve_filter').text(lang.loading);
  401. $.ajax({
  402. dataType: 'json',
  403. url: '/api/v1/get/active-user-sieve/' + encodeURIComponent(mailcow_cc_username),
  404. jsonp: false,
  405. error: function () {
  406. console.log('Cannot get active sieve script');
  407. },
  408. complete: function (data) {
  409. if (data.responseText == '{}') {
  410. $('#user_sieve_filter').text(lang.no_active_filter);
  411. } else {
  412. $('#user_sieve_filter').text(JSON.parse(data.responseText));
  413. }
  414. }
  415. })
  416. });
  417. $('#userFilterModal').on('hidden.bs.modal', function () {
  418. $('#user_sieve_filter').text(lang.loading);
  419. });
  420. });