user.js 14 KB

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