admin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. $(document).ready(function() {
  2. // Collect values of input fields with name multi_select with same data-id to js array multi_data[data-id]
  3. var multi_data = [];
  4. $(document).on('change', 'input[name=multi_select]:checkbox', function() {
  5. if ($(this).is(':checked') && $(this).data('id')) {
  6. var id = $(this).data('id');
  7. if (typeof multi_data[id] == "undefined") {
  8. multi_data[id] = [];
  9. }
  10. multi_data[id].push($(this).val());
  11. }
  12. else {
  13. var id = $(this).data('id');
  14. multi_data[id].splice($.inArray($(this).val(), multi_data[id]),1);
  15. }
  16. });
  17. // Select checkbox by click on parent tr
  18. $(document).on('click', 'tbody>tr', function(e) {
  19. if (e.target.type == "checkbox") {
  20. e.stopPropagation();
  21. } else {
  22. var checkbox = $(this).find(':checkbox');
  23. checkbox.trigger('click');
  24. }
  25. });
  26. // Select or deselect all checkboxes with same data-id
  27. $(document).on('click', '#toggle_multi_select_all', function(e) {
  28. e.preventDefault();
  29. id = $(this).data("id");
  30. multi_data[id] = [];
  31. var all_checkboxes = $("input[data-id=" + id + "]:enabled");
  32. all_checkboxes.prop("checked", !all_checkboxes.prop("checked")).change();
  33. });
  34. // General API edit function
  35. $(document).on('click', '#delete_selected', function(e) {
  36. e.preventDefault();
  37. var id = $(this).data('id');
  38. if (typeof multi_data[id] == "undefined" || multi_data[id] == "") return;
  39. data_array = multi_data[id];
  40. api_url = $(this).data('api-url');
  41. $(document).on('show.bs.modal','#ConfirmDeleteModal', function () {
  42. $("#ItemsToDelete").empty();
  43. for (var i in data_array) {
  44. $("#ItemsToDelete").append("<li>" + data_array[i] + "</li>");
  45. }
  46. })
  47. $('#ConfirmDeleteModal').modal({
  48. backdrop: 'static',
  49. keyboard: false
  50. })
  51. .one('click', '#IsConfirmed', function(e) {
  52. $.ajax({
  53. type: "POST",
  54. dataType: "json",
  55. data: { "items": JSON.stringify(data_array), "csrf_token": csrf_token },
  56. url: '/api/v1/' + api_url,
  57. jsonp: false,
  58. complete: function (data) {
  59. location.assign(window.location);
  60. }
  61. });
  62. })
  63. .one('click', '#isCanceled', function(e) {
  64. $('#ConfirmDeleteModal').modal('hide');
  65. });;
  66. });
  67. });
  68. jQuery(function($){
  69. function unix_time_format(tm) {
  70. var date = new Date(tm ? tm * 1000 : 0);
  71. return date.toLocaleString();
  72. }
  73. $("#refresh_postfix_log").on('click', function(e) {
  74. e.preventDefault();
  75. draw_postfix_logs();
  76. });
  77. $("#refresh_dovecot_log").on('click', function(e) {
  78. e.preventDefault();
  79. draw_dovecot_logs();
  80. });
  81. $("#refresh_sogo_log").on('click', function(e) {
  82. e.preventDefault();
  83. draw_sogo_logs();
  84. });
  85. function draw_postfix_logs() {
  86. ft_postfix_logs = FooTable.init('#postfix_log', {
  87. "columns": [
  88. {"name":"time","formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleString();},"title":lang.time,"style":{"width":"170px"}},
  89. {"name":"priority","title":lang.priority,"style":{"width":"80px"}},
  90. {"name":"message","title":lang.message},
  91. ],
  92. "rows": $.ajax({
  93. dataType: 'json',
  94. url: '/api/v1/get/logs/postfix/1000',
  95. jsonp: false,
  96. error: function () {
  97. console.log('Cannot draw postfix log table');
  98. },
  99. success: function (data) {
  100. $.each(data, function (i, item) {
  101. var danger_class = ["emerg", "alert", "crit"];
  102. var warning_class = ["warning"];
  103. var info_class = ["notice", "info", "debug"];
  104. if (jQuery.inArray(item.priority, danger_class) !== -1) {
  105. item.priority = '<span class="label label-danger">' + item.priority + '</span>';
  106. }
  107. else if (jQuery.inArray(item.priority, warning_class) !== -1) {
  108. item.priority = '<span class="label label-warning">' + item.priority + '</span>';
  109. }
  110. else if (jQuery.inArray(item.priority, info_class) !== -1) {
  111. item.priority = '<span class="label label-info">' + item.priority + '</span>';
  112. }
  113. });
  114. }
  115. }),
  116. "empty": lang.empty,
  117. "paging": {
  118. "enabled": true,
  119. "limit": 5,
  120. "size": pagination_size
  121. },
  122. "filtering": {
  123. "enabled": true,
  124. "position": "left",
  125. "placeholder": lang.filter_table
  126. },
  127. "sorting": {
  128. "enabled": true
  129. }
  130. });
  131. }
  132. function draw_sogo_logs() {
  133. ft_sogo_logs = FooTable.init('#sogo_log', {
  134. "columns": [
  135. {"name":"time","formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleString();},"title":lang.time,"style":{"width":"170px"}},
  136. {"name":"priority","title":lang.priority,"style":{"width":"80px"}},
  137. {"name":"message","title":lang.message},
  138. ],
  139. "rows": $.ajax({
  140. dataType: 'json',
  141. url: '/api/v1/get/logs/sogo/1000',
  142. jsonp: false,
  143. error: function () {
  144. console.log('Cannot draw sogo log table');
  145. },
  146. success: function (data) {
  147. $.each(data, function (i, item) {
  148. var danger_class = ["emerg", "alert", "crit"];
  149. var warning_class = ["warning"];
  150. var info_class = ["notice", "info", "debug"];
  151. if (jQuery.inArray(item.priority, danger_class) !== -1) {
  152. item.priority = '<span class="label label-danger">' + item.priority + '</span>';
  153. }
  154. else if (jQuery.inArray(item.priority, warning_class) !== -1) {
  155. item.priority = '<span class="label label-warning">' + item.priority + '</span>';
  156. }
  157. else if (jQuery.inArray(item.priority, info_class) !== -1) {
  158. item.priority = '<span class="label label-info">' + item.priority + '</span>';
  159. }
  160. });
  161. }
  162. }),
  163. "empty": lang.empty,
  164. "paging": {
  165. "enabled": true,
  166. "limit": 5,
  167. "size": pagination_size
  168. },
  169. "filtering": {
  170. "enabled": true,
  171. "position": "left",
  172. "placeholder": lang.filter_table
  173. },
  174. "sorting": {
  175. "enabled": true
  176. }
  177. });
  178. }
  179. function draw_dovecot_logs() {
  180. ft_postfix_logs = FooTable.init('#dovecot_log', {
  181. "columns": [
  182. {"name":"time","formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleString();},"title":lang.time,"style":{"width":"170px"}},
  183. {"name":"priority","title":lang.priority,"style":{"width":"80px"}},
  184. {"name":"message","title":lang.message},
  185. ],
  186. "rows": $.ajax({
  187. dataType: 'json',
  188. url: '/api/v1/get/logs/dovecot/1000',
  189. jsonp: false,
  190. error: function () {
  191. console.log('Cannot draw dovecot log table');
  192. },
  193. success: function (data) {
  194. $.each(data, function (i, item) {
  195. var danger_class = ["emerg", "alert", "crit"];
  196. var warning_class = ["warning"];
  197. var info_class = ["notice", "info", "debug"];
  198. if (jQuery.inArray(item.priority, danger_class) !== -1) {
  199. item.priority = '<span class="label label-danger">' + item.priority + '</span>';
  200. }
  201. else if (jQuery.inArray(item.priority, warning_class) !== -1) {
  202. item.priority = '<span class="label label-warning">' + item.priority + '</span>';
  203. }
  204. else if (jQuery.inArray(item.priority, info_class) !== -1) {
  205. item.priority = '<span class="label label-info">' + item.priority + '</span>';
  206. }
  207. });
  208. }
  209. }),
  210. "empty": lang.empty,
  211. "paging": {
  212. "enabled": true,
  213. "limit": 5,
  214. "size": pagination_size
  215. },
  216. "filtering": {
  217. "enabled": true,
  218. "position": "left",
  219. "placeholder": lang.filter_table
  220. },
  221. "sorting": {
  222. "enabled": true
  223. }
  224. });
  225. }
  226. function draw_domain_admins() {
  227. ft_domainadmins = FooTable.init('#domainadminstable', {
  228. "columns": [
  229. {"sorted": true,"name":"username","title":lang.username,"style":{"width":"250px"}},
  230. {"name":"selected_domains","title":lang.admin_domains,"breakpoints":"xs sm"},
  231. {"name":"tfa_active","title":"TFA", "filterable": false,"style":{"maxWidth":"80px","width":"80px"}},
  232. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  233. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  234. ],
  235. "rows": $.ajax({
  236. dataType: 'json',
  237. url: '/api/v1/get/domain-admin/all',
  238. jsonp: false,
  239. error: function () {
  240. console.log('Cannot draw domain admin table');
  241. },
  242. success: function (data) {
  243. $.each(data, function (i, item) {
  244. item.action = '<div class="btn-group">' +
  245. '<a href="/edit.php?domainadmin=' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  246. '<a href="/delete.php?domainadmin=' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  247. '</div>';
  248. });
  249. }
  250. }),
  251. "empty": lang.empty,
  252. "paging": {
  253. "enabled": true,
  254. "limit": 5,
  255. "size": pagination_size
  256. },
  257. "filtering": {
  258. "enabled": true,
  259. "position": "left",
  260. "placeholder": lang.filter_table
  261. },
  262. "sorting": {
  263. "enabled": true
  264. }
  265. });
  266. }
  267. function draw_fwd_hosts() {
  268. ft_domainadmins = FooTable.init('#forwardinghoststable', {
  269. "columns": [
  270. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  271. {"name":"host","type":"text","title":lang.host,"style":{"width":"250px"}},
  272. {"name":"source","title":lang.source,"breakpoints":"xs sm"},
  273. {"name":"keep_spam","title":lang.spamfilter, "type": "text","style":{"maxWidth":"80px","width":"80px"}},
  274. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  275. ],
  276. "rows": $.ajax({
  277. dataType: 'json',
  278. url: '/api/v1/get/fwdhost/all',
  279. jsonp: false,
  280. error: function () {
  281. console.log('Cannot draw forwarding hosts table');
  282. },
  283. success: function (data) {
  284. $.each(data, function (i, item) {
  285. item.action = '<div class="btn-group">' +
  286. '<a href="/delete.php?forwardinghost=' + encodeURI(item.host) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  287. '</div>';
  288. if (item.keep_spam == "yes") {
  289. item.keep_spam = lang.no;
  290. }
  291. else {
  292. item.keep_spam = lang.yes;
  293. }
  294. item.chkbox = '<input type="checkbox" data-id="fwdhosts" name="multi_select" value="' + item.host + '" />';
  295. });
  296. }
  297. }),
  298. "empty": lang.empty,
  299. "paging": {
  300. "enabled": true,
  301. "limit": 5,
  302. "size": pagination_size
  303. },
  304. "sorting": {
  305. "enabled": true
  306. }
  307. });
  308. }
  309. draw_postfix_logs();
  310. draw_dovecot_logs();
  311. draw_sogo_logs();
  312. draw_domain_admins();
  313. draw_fwd_hosts();
  314. });