mailbox.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. $(document).ready(function() {
  2. // Auto-fill domain quota when adding new domain
  3. auto_fill_quota = function(domain) {
  4. $.get("/api/v1/get/domain/" + domain, function(data){
  5. var result = $.parseJSON(JSON.stringify(data));
  6. max_new_mailbox_quota = ( result.max_new_mailbox_quota / 1048576);
  7. if (max_new_mailbox_quota != '0') {
  8. $("#quotaBadge").html('max. ' + max_new_mailbox_quota + ' MiB');
  9. $('#addInputQuota').attr({"disabled": false, "value": "", "type": "number", "max": max_new_mailbox_quota});
  10. $('#addInputQuota').val(max_new_mailbox_quota);
  11. }
  12. else {
  13. $("#quotaBadge").html('max. ' + max_new_mailbox_quota + ' MiB');
  14. $('#addInputQuota').attr({"disabled": true, "value": "", "type": "text", "value": "n/a"});
  15. $('#addInputQuota').val(max_new_mailbox_quota);
  16. }
  17. });
  18. }
  19. $('#addSelectDomain').on('change', function() {
  20. auto_fill_quota($('#addSelectDomain').val());
  21. });
  22. auto_fill_quota($('#addSelectDomain').val());
  23. $(".generate_password").click(function( event ) {
  24. event.preventDefault();
  25. var random_passwd = Math.random().toString(36).slice(-8)
  26. $('#password').prop('type', 'text');
  27. $('#password').val(random_passwd);
  28. $('#password2').prop('type', 'text');
  29. $('#password2').val(random_passwd);
  30. });
  31. $("#goto_null").click(function( event ) {
  32. if ($("#goto_null").is(":checked")) {
  33. $('#textarea_alias_goto').prop('disabled', true);
  34. }
  35. else {
  36. $("#textarea_alias_goto").removeAttr('disabled');
  37. }
  38. });
  39. // Log modal
  40. $('#logModal').on('show.bs.modal', function(e) {
  41. var logText = $(e.relatedTarget).data('log-text');
  42. $(e.currentTarget).find('#logText').html('<pre style="background:none;font-size:11px;line-height:1.1;border:0px">' + logText + '</pre>');
  43. });
  44. });
  45. jQuery(function($){
  46. // http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery
  47. var entityMap = {
  48. '&': '&amp;',
  49. '<': '&lt;',
  50. '>': '&gt;',
  51. '"': '&quot;',
  52. "'": '&#39;',
  53. '/': '&#x2F;',
  54. '`': '&#x60;',
  55. '=': '&#x3D;'
  56. };
  57. function escapeHtml(string) {
  58. return String(string).replace(/[&<>"'`=\/]/g, function (s) {
  59. return entityMap[s];
  60. });
  61. }
  62. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  63. function validateEmail(email) {
  64. 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,}))$/;
  65. return re.test(email);
  66. }
  67. // Calculation human readable file sizes
  68. function humanFileSize(bytes) {
  69. if(Math.abs(bytes) < 1024) {
  70. return bytes + ' B';
  71. }
  72. var units = ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
  73. var u = -1;
  74. do {
  75. bytes /= 1024;
  76. ++u;
  77. } while(Math.abs(bytes) >= 1024 && u < units.length - 1);
  78. return bytes.toFixed(1)+' '+units[u];
  79. }
  80. function unix_time_format(tm) {
  81. var date = new Date(tm ? tm * 1000 : 0);
  82. return date.toLocaleString();
  83. }
  84. function draw_domain_table() {
  85. ft_domain_table = FooTable.init('#domain_table', {
  86. "columns": [
  87. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  88. {"sorted": true,"name":"domain_name","title":lang.domain,"style":{"width":"250px"}},
  89. {"name":"aliases","title":lang.aliases,"breakpoints":"xs sm"},
  90. {"name":"mailboxes","title":lang.mailboxes},
  91. {"name":"quota","style":{"whiteSpace":"nowrap"},"title":lang.domain_quota,"formatter": function(value){
  92. res = value.split("/");
  93. return humanFileSize(res[0]) + " / " + humanFileSize(res[1]);
  94. },
  95. "sortValue": function(value){
  96. res = value.split("/");
  97. return res[0];
  98. },
  99. },
  100. {"name":"max_quota_for_mbox","title":lang.mailbox_quota,"breakpoints":"xs sm"},
  101. {"name":"backupmx","filterable": false,"style":{"maxWidth":"120px","width":"120px"},"title":lang.backup_mx,"breakpoints":"xs sm"},
  102. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  103. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  104. ],
  105. "rows": $.ajax({
  106. dataType: 'json',
  107. url: '/api/v1/get/domain/all',
  108. jsonp: false,
  109. error: function (data) {
  110. console.log('Cannot draw domain table');
  111. },
  112. success: function (data) {
  113. $.each(data, function (i, item) {
  114. item.aliases = item.aliases_in_domain + " / " + item.max_num_aliases_for_domain;
  115. item.mailboxes = item.mboxes_in_domain + " / " + item.max_num_mboxes_for_domain;
  116. item.quota = item.quota_used_in_domain + "/" + item.max_quota_for_domain;
  117. item.max_quota_for_mbox = humanFileSize(item.max_quota_for_mbox);
  118. item.chkbox = '<input type="checkbox" data-id="domain" name="multi_select" value="' + item.domain_name + '" />';
  119. if (role == "admin") {
  120. item.action = '<div class="btn-group">' +
  121. '<a href="/edit.php?domain=' + encodeURI(item.domain_name) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  122. '<a href="#" id="delete_selected" data-id="single-domain" data-api-url="delete/domain" data-item="' + encodeURI(item.domain_name) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  123. '</div>';
  124. }
  125. else {
  126. item.action = '<div class="btn-group">' +
  127. '<a href="/edit.php?domain=' + encodeURI(item.domain_name) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  128. '</div>';
  129. }
  130. });
  131. }
  132. }),
  133. "empty": lang.empty,
  134. "paging": {
  135. "enabled": true,
  136. "limit": 5,
  137. "size": pagination_size
  138. },
  139. "filtering": {
  140. "enabled": true,
  141. "position": "left",
  142. "placeholder": lang.filter_table
  143. },
  144. "sorting": {
  145. "enabled": true
  146. }
  147. });
  148. }
  149. function draw_mailbox_table() {
  150. ft_mailbox_table = FooTable.init('#mailbox_table', {
  151. "columns": [
  152. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  153. {"sorted": true,"name":"username","style":{"word-break":"break-all","min-width":"120px"},"title":lang.username},
  154. {"name":"name","title":lang.fname,"style":{"word-break":"break-all","min-width":"120px"},"breakpoints":"xs sm"},
  155. {"name":"domain","title":lang.domain,"breakpoints":"xs sm"},
  156. {"name":"quota","style":{"whiteSpace":"nowrap"},"title":lang.domain_quota,"formatter": function(value){
  157. res = value.split("/");
  158. return humanFileSize(res[0]) + " / " + humanFileSize(res[1]);
  159. },
  160. "sortValue": function(value){
  161. res = value.split("/");
  162. return res[0];
  163. },
  164. },
  165. {"name":"spam_aliases","filterable": false,"title":lang.spam_aliases,"breakpoints":"xs sm md"},
  166. {"name":"in_use","filterable": false,"type":"html","title":lang.in_use},
  167. {"name":"messages","filterable": false,"title":lang.msg_num,"breakpoints":"xs sm md"},
  168. {"name":"active","filterable": false,"title":lang.active},
  169. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","min-width":"250px"},"type":"html","title":lang.action,"breakpoints":"xs sm md"}
  170. ],
  171. "empty": lang.empty,
  172. "rows": $.ajax({
  173. dataType: 'json',
  174. url: '/api/v1/get/mailbox/all',
  175. jsonp: false,
  176. error: function () {
  177. console.log('Cannot draw mailbox table');
  178. },
  179. success: function (data) {
  180. $.each(data, function (i, item) {
  181. item.quota = item.quota_used + "/" + item.quota;
  182. item.max_quota_for_mbox = humanFileSize(item.max_quota_for_mbox);
  183. item.chkbox = '<input type="checkbox" data-id="mailbox" name="multi_select" value="' + item.username + '" />';
  184. if (role == "admin") {
  185. item.action = '<div class="btn-group">' +
  186. '<a href="/edit.php?mailbox=' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  187. '<a href="#" id="delete_selected" data-id="single-mailbox" data-api-url="delete/mailbox" data-item="' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  188. '<a href="/index.php?duallogin=' + encodeURI(item.username) + '" class="btn btn-xs btn-success"><span class="glyphicon glyphicon-user"></span> Login</a>' +
  189. '</div>';
  190. }
  191. else {
  192. item.action = '<div class="btn-group">' +
  193. '<a href="/edit.php?mailbox=' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  194. '<a href="#" id="delete_selected" data-id="single-mailbox" data-api-url="delete/mailbox" data-item="' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  195. '</div>';
  196. }
  197. item.in_use = '<div class="progress">' +
  198. '<div class="progress-bar progress-bar-' + item.percent_class + ' role="progressbar" aria-valuenow="' + item.percent_in_use + '" aria-valuemin="0" aria-valuemax="100" ' +
  199. 'style="min-width:2em;width:' + item.percent_in_use + '%">' + item.percent_in_use + '%' + '</div></div>';
  200. });
  201. }
  202. }),
  203. "paging": {
  204. "enabled": true,
  205. "limit": 5,
  206. "size": pagination_size
  207. },
  208. "filtering": {
  209. "enabled": true,
  210. "position": "left",
  211. "placeholder": lang.filter_table
  212. },
  213. "sorting": {
  214. "enabled": true
  215. }
  216. });
  217. }
  218. function draw_resource_table() {
  219. ft_resource_table = FooTable.init('#resource_table', {
  220. "columns": [
  221. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  222. {"sorted": true,"name":"description","title":lang.description,"style":{"width":"250px"}},
  223. {"name":"kind","title":lang.kind},
  224. {"name":"domain","title":lang.domain,"breakpoints":"xs sm"},
  225. {"name":"multiple_bookings","filterable": false,"style":{"maxWidth":"120px","width":"120px"},"title":lang.multiple_bookings,"breakpoints":"xs sm"},
  226. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  227. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  228. ],
  229. "empty": lang.empty,
  230. "rows": $.ajax({
  231. dataType: 'json',
  232. url: '/api/v1/get/resource/all',
  233. jsonp: false,
  234. error: function () {
  235. console.log('Cannot draw resource table');
  236. },
  237. success: function (data) {
  238. $.each(data, function (i, item) {
  239. item.action = '<div class="btn-group">' +
  240. '<a href="/edit.php?resource=' + encodeURI(item.name) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  241. '<a href="#" id="delete_selected" data-id="single-resource" data-api-url="delete/resource" data-item="' + encodeURI(item.name) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  242. '</div>';
  243. item.chkbox = '<input type="checkbox" data-id="resource" name="multi_select" value="' + item.name + '" />';
  244. });
  245. }
  246. }),
  247. "paging": {
  248. "enabled": true,
  249. "limit": 5,
  250. "size": pagination_size
  251. },
  252. "filtering": {
  253. "enabled": true,
  254. "position": "left",
  255. "placeholder": lang.filter_table
  256. },
  257. "sorting": {
  258. "enabled": true
  259. }
  260. });
  261. }
  262. function draw_alias_table() {
  263. ft_alias_table = FooTable.init('#alias_table', {
  264. "columns": [
  265. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  266. {"sorted": true,"name":"address","title":lang.alias,"style":{"width":"250px"}},
  267. {"name":"goto","title":lang.target_address},
  268. {"name":"domain","title":lang.domain,"breakpoints":"xs sm"},
  269. {"name":"active","filterable": false,"style":{"maxWidth":"50px","width":"70px"},"title":lang.active},
  270. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  271. ],
  272. "empty": lang.empty,
  273. "rows": $.ajax({
  274. dataType: 'json',
  275. url: '/api/v1/get/alias/all',
  276. jsonp: false,
  277. error: function () {
  278. console.log('Cannot draw alias table');
  279. },
  280. success: function (data) {
  281. $.each(data, function (i, item) {
  282. item.action = '<div class="btn-group">' +
  283. '<a href="/edit.php?alias=' + encodeURI(item.address) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  284. '<a href="#" id="delete_selected" data-id="single-alias" data-api-url="delete/alias" data-item="' + encodeURI(item.address) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  285. '</div>';
  286. item.chkbox = '<input type="checkbox" data-id="alias" name="multi_select" value="' + item.address + '" />';
  287. if (item.is_catch_all == 1) {
  288. item.address = '<div class="label label-default">Catch-All</div> ' + item.address;
  289. }
  290. if (item.goto == "null@localhost") {
  291. item.goto = '⤷ <span style="font-size:12px" class="glyphicon glyphicon-trash" aria-hidden="true"></span>';
  292. }
  293. if (item.in_primary_domain !== "") {
  294. item.domain = "↳ " + item.domain + " (" + item.in_primary_domain + ")";
  295. }
  296. });
  297. }
  298. }),
  299. "paging": {
  300. "enabled": true,
  301. "limit": 5,
  302. "size": pagination_size
  303. },
  304. "filtering": {
  305. "enabled": true,
  306. "position": "left",
  307. "placeholder": lang.filter_table
  308. },
  309. "sorting": {
  310. "enabled": true
  311. }
  312. });
  313. }
  314. function draw_aliasdomain_table() {
  315. ft_aliasdomain_table = FooTable.init('#aliasdomain_table', {
  316. "columns": [
  317. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  318. {"sorted": true,"name":"alias_domain","title":lang.alias,"style":{"width":"250px"}},
  319. {"name":"target_domain","title":lang.target_domain},
  320. {"name":"active","filterable": false,"style":{"maxWidth":"50px","width":"70px"},"title":lang.active},
  321. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  322. ],
  323. "empty": lang.empty,
  324. "rows": $.ajax({
  325. dataType: 'json',
  326. url: '/api/v1/get/alias-domain/all',
  327. jsonp: false,
  328. error: function () {
  329. console.log('Cannot draw alias domain table');
  330. },
  331. success: function (data) {
  332. $.each(data, function (i, item) {
  333. item.action = '<div class="btn-group">' +
  334. '<a href="/edit.php?aliasdomain=' + encodeURI(item.alias_domain) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  335. '<a href="#" id="delete_selected" data-id="single-alias-domain" data-api-url="delete/alias-domain" data-item="' + encodeURI(item.alias_domain) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  336. '</div>';
  337. item.chkbox = '<input type="checkbox" data-id="alias-domain" name="multi_select" value="' + item.alias_domain + '" />';
  338. });
  339. }
  340. }),
  341. "paging": {
  342. "enabled": true,
  343. "limit": 5,
  344. "size": pagination_size
  345. },
  346. "filtering": {
  347. "enabled": true,
  348. "position": "left",
  349. "placeholder": lang.filter_table
  350. },
  351. "sorting": {
  352. "enabled": true
  353. }
  354. });
  355. }
  356. function draw_sync_job_table() {
  357. ft_syncjob_table = FooTable.init('#sync_job_table', {
  358. "columns": [
  359. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px","text-align":"center"},"filterable": false,"sortable": false,"type":"html"},
  360. {"sorted": true,"name":"id","title":"ID","style":{"maxWidth":"60px","width":"60px","text-align":"center"}},
  361. {"name":"user2","title":lang.owner},
  362. {"name":"server_w_port","title":"Server","breakpoints":"xs"},
  363. {"name":"mins_interval","title":lang.mins_interval,"breakpoints":"all"},
  364. {"name":"last_run","title":lang.last_run,"breakpoints":"all"},
  365. {"name":"log","title":"Log"},
  366. {"name":"active","filterable": false,"style":{"maxWidth":"50px","width":"70px"},"title":lang.active},
  367. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  368. ],
  369. "empty": lang.empty,
  370. "rows": $.ajax({
  371. dataType: 'json',
  372. url: '/api/v1/get/syncjobs/all',
  373. jsonp: false,
  374. error: function () {
  375. console.log('Cannot draw sync job table');
  376. },
  377. success: function (data) {
  378. $.each(data, function (i, item) {
  379. item.log = '<a href="#logModal" data-toggle="modal" data-log-text="' + escapeHtml(item.returned_text) + '">Open logs</a>'
  380. item.exclude = '<code>' + item.exclude + '</code>'
  381. item.server_w_port = item.user1 + '@' + item.host1 + ':' + item.port1;
  382. item.action = '<div class="btn-group">' +
  383. '<a href="/edit.php?syncjob=' + item.id + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  384. '<a href="#" id="delete_selected" data-id="single-syncjob" data-api-url="delete/syncjob" data-item="' + encodeURI(item.id) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  385. '</div>';
  386. item.chkbox = '<input type="checkbox" data-id="syncjob" name="multi_select" value="' + item.id + '" />';
  387. });
  388. }
  389. }),
  390. "paging": {
  391. "enabled": true,
  392. "limit": 5,
  393. "size": pagination_size
  394. },
  395. "sorting": {
  396. "enabled": true
  397. }
  398. });
  399. }
  400. draw_domain_table();
  401. draw_mailbox_table();
  402. draw_resource_table();
  403. draw_alias_table();
  404. draw_aliasdomain_table();
  405. draw_sync_job_table();
  406. });