user.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. $(document).ready(function() {
  2. $('#syncjobLogModal').on('show.bs.modal', function(e) {
  3. var syncjob_id = $(e.relatedTarget).data('syncjob-id');
  4. $.ajax({
  5. url: '/inc/ajax/syncjob_logs.php',
  6. data: { id: syncjob_id },
  7. dataType: 'text',
  8. success: function(data){
  9. $(e.currentTarget).find('#logText').text(data);
  10. },
  11. error: function(xhr, status, error) {
  12. $(e.currentTarget).find('#logText').text(xhr.responseText);
  13. }
  14. });
  15. });
  16. $(".arrow-toggle").on('click', function(e) { e.preventDefault(); $(this).find('.arrow').toggleClass("animation"); });
  17. $("#pushover_delete").click(function() { return confirm(lang.delete_ays); });
  18. });
  19. jQuery(function($){
  20. // http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery
  21. var entityMap = {
  22. '&': '&',
  23. '<': '&lt;',
  24. '>': '&gt;',
  25. '"': '&quot;',
  26. "'": '&#39;',
  27. '/': '&#x2F;',
  28. '`': '&#x60;',
  29. '=': '&#x3D;'
  30. };
  31. function escapeHtml(string) {
  32. return String(string).replace(/[&<>"'`=\/]/g, function (s) {
  33. return entityMap[s];
  34. });
  35. }
  36. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  37. function validateEmail(email) {
  38. 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,}))$/;
  39. return re.test(email);
  40. }
  41. function unix_time_format(tm) {
  42. var date = new Date(tm ? tm * 1000 : 0);
  43. return date.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"});
  44. }
  45. acl_data = JSON.parse(acl);
  46. var last_login = $('.last_login_date').data('time');
  47. $('.last_login_date').text(unix_time_format(last_login));
  48. function draw_tla_table() {
  49. ft_tla_table = FooTable.init('#tla_table', {
  50. "columns": [
  51. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px","text-align":"center"},"filterable": false,"sortable": false,"type":"html"},
  52. {"sorted": true,"name":"address","title":lang.alias},
  53. {"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"}},
  54. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  55. ],
  56. "empty": lang.empty,
  57. "rows": $.ajax({
  58. dataType: 'json',
  59. url: '/api/v1/get/time_limited_aliases',
  60. jsonp: false,
  61. error: function () {
  62. console.log('Cannot draw tla table');
  63. },
  64. success: function (data) {
  65. $.each(data, function (i, item) {
  66. if (acl_data.spam_alias === 1) {
  67. item.action = '<div class="btn-group">' +
  68. '<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"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  69. '</div>';
  70. item.chkbox = '<input type="checkbox" data-id="tla" name="multi_select" value="' + encodeURIComponent(item.address) + '" />';
  71. item.address = escapeHtml(item.address);
  72. }
  73. else {
  74. item.chkbox = '<input type="checkbox" disabled />';
  75. item.action = '<span>-</span>';
  76. }
  77. });
  78. }
  79. }),
  80. "paging": {
  81. "enabled": true,
  82. "limit": 5,
  83. "size": pagination_size
  84. },
  85. "state": {"enabled": true},
  86. "sorting": {
  87. "enabled": true
  88. },
  89. "toggleSelector": "table tbody span.footable-toggle"
  90. });
  91. }
  92. function draw_sync_job_table() {
  93. ft_syncjob_table = FooTable.init('#sync_job_table', {
  94. "columns": [
  95. {"name":"chkbox","title":"","style":{"maxWidth":"60px","width":"60px","text-align":"center"},"filterable": false,"sortable": false,"type":"html"},
  96. {"sorted": true,"name":"id","title":"ID","style":{"maxWidth":"60px","width":"60px","text-align":"center"}},
  97. {"name":"server_w_port","title":"Server"},
  98. {"name":"enc1","title":lang.encryption,"breakpoints":"xs sm"},
  99. {"name":"user1","title":lang.username},
  100. {"name":"exclude","title":lang.excludes,"breakpoints":"all"},
  101. {"name":"mins_interval","title":lang.interval + " (min)","breakpoints":"all"},
  102. {"name":"last_run","title":lang.last_run,"breakpoints":"all"},
  103. {"name":"log","title":"Log"},
  104. {"name":"active","filterable": false,"style":{"maxWidth":"70px","width":"70px"},"title":lang.active,"formatter": function(value){return 1==value?'&#10003;':0==value&&'&#10005;';}},
  105. {"name":"is_running","filterable": false,"style":{"maxWidth":"120px","width":"100px"},"title":lang.status},
  106. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  107. ],
  108. "empty": lang.empty,
  109. "rows": $.ajax({
  110. dataType: 'json',
  111. url: '/api/v1/get/syncjobs/' + encodeURIComponent(mailcow_cc_username) + '/no_log',
  112. jsonp: false,
  113. error: function () {
  114. console.log('Cannot draw sync job table');
  115. },
  116. success: function (data) {
  117. $.each(data, function (i, item) {
  118. item.user1 = escapeHtml(item.user1);
  119. item.log = '<a href="#syncjobLogModal" data-toggle="modal" data-syncjob-id="' + item.id + '">Open logs</a>'
  120. if (!item.exclude > 0) {
  121. item.exclude = '-';
  122. } else {
  123. item.exclude = '<code>' + escapeHtml(item.exclude) + '</code>';
  124. }
  125. item.server_w_port = escapeHtml(item.user1 + '@' + item.host1 + ':' + item.port1);
  126. if (acl_data.syncjobs === 1) {
  127. item.action = '<div class="btn-group">' +
  128. '<a href="/edit/syncjob/' + item.id + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  129. '<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"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  130. '</div>';
  131. item.chkbox = '<input type="checkbox" data-id="syncjob" name="multi_select" value="' + item.id + '" />';
  132. }
  133. else {
  134. item.action = '<span>-</span>';
  135. item.chkbox = '<input type="checkbox" disabled />';
  136. }
  137. if (item.is_running == 1) {
  138. item.is_running = '<span id="active-script" class="label label-success">' + lang.running + '</span>';
  139. } else {
  140. item.is_running = '<span id="inactive-script" class="label label-warning">' + lang.waiting + '</span>';
  141. }
  142. if (!item.last_run > 0) {
  143. item.last_run = lang.waiting;
  144. }
  145. });
  146. }
  147. }),
  148. "paging": {
  149. "enabled": true,
  150. "limit": 5,
  151. "size": pagination_size
  152. },
  153. "state": {"enabled": true},
  154. "sorting": {
  155. "enabled": true
  156. },
  157. "toggleSelector": "table tbody span.footable-toggle"
  158. });
  159. }
  160. function draw_app_passwd_table() {
  161. ft_apppasswd_table = FooTable.init('#app_passwd_table', {
  162. "columns": [
  163. {"name":"chkbox","title":"","style":{"maxWidth":"60px","width":"60px","text-align":"center"},"filterable": false,"sortable": false,"type":"html"},
  164. {"sorted": true,"name":"id","title":"ID","style":{"maxWidth":"60px","width":"60px","text-align":"center"}},
  165. {"name":"name","title":lang.app_name},
  166. {"name":"active","filterable": false,"style":{"maxWidth":"70px","width":"70px"},"title":lang.active,"formatter": function(value){return 1==value?'&#10003;':0==value&&'&#10005;';}},
  167. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  168. ],
  169. "empty": lang.empty,
  170. "rows": $.ajax({
  171. dataType: 'json',
  172. url: '/api/v1/get/app-passwd/all',
  173. jsonp: false,
  174. error: function () {
  175. console.log('Cannot draw app passwd table');
  176. },
  177. success: function (data) {
  178. $.each(data, function (i, item) {
  179. if (acl_data.app_passwds === 1) {
  180. item.action = '<div class="btn-group">' +
  181. '<a href="/edit/app-passwd/' + item.id + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  182. '<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"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  183. '</div>';
  184. item.chkbox = '<input type="checkbox" data-id="apppasswd" name="multi_select" value="' + item.id + '" />';
  185. }
  186. else {
  187. item.action = '<span>-</span>';
  188. item.chkbox = '<input type="checkbox" disabled />';
  189. }
  190. });
  191. }
  192. }),
  193. "paging": {
  194. "enabled": true,
  195. "limit": 5,
  196. "size": pagination_size
  197. },
  198. "state": {"enabled": true},
  199. "sorting": {
  200. "enabled": true
  201. },
  202. "toggleSelector": "table tbody span.footable-toggle"
  203. });
  204. }
  205. function draw_wl_policy_mailbox_table() {
  206. ft_wl_policy_mailbox_table = FooTable.init('#wl_policy_mailbox_table', {
  207. "columns": [
  208. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px","text-align":"center"},"filterable": false,"sortable": false,"type":"html"},
  209. {"name":"prefid","style":{"maxWidth":"40px","width":"40px"},"title":"ID","filterable": false,"sortable": false},
  210. {"sorted": true,"name":"value","title":lang.spamfilter_table_rule},
  211. {"name":"object","title":"Scope"}
  212. ],
  213. "empty": lang.empty,
  214. "rows": $.ajax({
  215. dataType: 'json',
  216. url: '/api/v1/get/policy_wl_mailbox',
  217. jsonp: false,
  218. error: function () {
  219. console.log('Cannot draw mailbox policy wl table');
  220. },
  221. success: function (data) {
  222. $.each(data, function (i, item) {
  223. if (validateEmail(item.object)) {
  224. item.chkbox = '<input type="checkbox" data-id="policy_wl_mailbox" name="multi_select" value="' + item.prefid + '" />';
  225. }
  226. else {
  227. item.chkbox = '<input type="checkbox" disabled title="' + lang.spamfilter_table_domain_policy + '" />';
  228. }
  229. if (acl_data.spam_policy === 0) {
  230. item.chkbox = '<input type="checkbox" disabled />';
  231. }
  232. });
  233. }
  234. }),
  235. "state": {"enabled": true},
  236. "paging": {
  237. "enabled": true,
  238. "limit": 5,
  239. "size": pagination_size
  240. },
  241. "sorting": {
  242. "enabled": true
  243. }
  244. });
  245. }
  246. function draw_bl_policy_mailbox_table() {
  247. ft_bl_policy_mailbox_table = FooTable.init('#bl_policy_mailbox_table', {
  248. "columns": [
  249. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px","text-align":"center"},"filterable": false,"sortable": false,"type":"html"},
  250. {"name":"prefid","style":{"maxWidth":"40px","width":"40px"},"title":"ID","filterable": false,"sortable": false},
  251. {"sorted": true,"name":"value","title":lang.spamfilter_table_rule},
  252. {"name":"object","title":"Scope"}
  253. ],
  254. "empty": lang.empty,
  255. "rows": $.ajax({
  256. dataType: 'json',
  257. url: '/api/v1/get/policy_bl_mailbox',
  258. jsonp: false,
  259. error: function () {
  260. console.log('Cannot draw mailbox policy bl table');
  261. },
  262. success: function (data) {
  263. $.each(data, function (i, item) {
  264. if (validateEmail(item.object)) {
  265. item.chkbox = '<input type="checkbox" data-id="policy_bl_mailbox" name="multi_select" value="' + item.prefid + '" />';
  266. }
  267. else {
  268. item.chkbox = '<input type="checkbox" disabled tooltip="' + lang.spamfilter_table_domain_policy + '" />';
  269. }
  270. if (acl_data.spam_policy === 0) {
  271. item.chkbox = '<input type="checkbox" disabled />';
  272. }
  273. });
  274. }
  275. }),
  276. "paging": {
  277. "enabled": true,
  278. "limit": 5,
  279. "size": pagination_size
  280. },
  281. "state": {"enabled": true},
  282. "sorting": {
  283. "enabled": true
  284. }
  285. });
  286. }
  287. $('body').on('click', 'span.footable-toggle', function () {
  288. event.stopPropagation();
  289. })
  290. draw_sync_job_table();
  291. draw_app_passwd_table();
  292. draw_tla_table();
  293. draw_wl_policy_mailbox_table();
  294. draw_bl_policy_mailbox_table();
  295. // Sieve data modal
  296. $('#userFilterModal').on('show.bs.modal', function(e) {
  297. $('#user_sieve_filter').text(lang.loading);
  298. $.ajax({
  299. dataType: 'json',
  300. url: '/api/v1/get/active-user-sieve/' + encodeURIComponent(mailcow_cc_username),
  301. jsonp: false,
  302. error: function () {
  303. console.log('Cannot get active sieve script');
  304. },
  305. complete: function (data) {
  306. if (data.responseText == '{}') {
  307. $('#user_sieve_filter').text(lang.no_active_filter);
  308. } else {
  309. $('#user_sieve_filter').text(JSON.parse(data.responseText));
  310. }
  311. }
  312. })
  313. });
  314. $('#userFilterModal').on('hidden.bs.modal', function () {
  315. $('#user_sieve_filter').text(lang.loading);
  316. });
  317. });