footer.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. require_once $_SERVER['DOCUMENT_ROOT'] . '/modals/footer.php';
  3. logger();
  4. $hash = $js_minifier->getDataHash();
  5. $JSPath = '/tmp/' . $hash . '.js';
  6. if(!file_exists($JSPath)) {
  7. $js_minifier->minify($JSPath);
  8. cleanupJS($hash);
  9. }
  10. ?>
  11. <script src="/cache/<?=basename($JSPath)?>"></script>
  12. <script>
  13. <?php
  14. $lang_footer = json_encode($lang['footer']);
  15. $lang_acl = json_encode($lang['acl']);
  16. $lang_tfa = json_encode($lang['tfa']);
  17. $lang_fido2 = json_encode($lang['fido2']);
  18. echo "var lang_footer = ". $lang_footer . ";\n";
  19. echo "var lang_acl = ". $lang_acl . ";\n";
  20. echo "var lang_tfa = ". $lang_tfa . ";\n";
  21. echo "var lang_fido2 = ". $lang_fido2 . ";\n";
  22. echo "var docker_timeout = ". $DOCKER_TIMEOUT * 1000 . ";\n";
  23. ?>
  24. $(window).scroll(function() {
  25. sessionStorage.scrollTop = $(this).scrollTop();
  26. });
  27. // Select language and reopen active URL without POST
  28. function setLang(sel) {
  29. $.post( "<?= $_SERVER['REQUEST_URI']; ?>", {lang: sel} );
  30. window.location.href = window.location.pathname + window.location.search;
  31. }
  32. // FIDO2 functions
  33. function arrayBufferToBase64(buffer) {
  34. let binary = '';
  35. let bytes = new Uint8Array(buffer);
  36. let len = bytes.byteLength;
  37. for (let i = 0; i < len; i++) {
  38. binary += String.fromCharCode( bytes[ i ] );
  39. }
  40. return window.btoa(binary);
  41. }
  42. function recursiveBase64StrToArrayBuffer(obj) {
  43. let prefix = '=?BINARY?B?';
  44. let suffix = '?=';
  45. if (typeof obj === 'object') {
  46. for (let key in obj) {
  47. if (typeof obj[key] === 'string') {
  48. let str = obj[key];
  49. if (str.substring(0, prefix.length) === prefix && str.substring(str.length - suffix.length) === suffix) {
  50. str = str.substring(prefix.length, str.length - suffix.length);
  51. let binary_string = window.atob(str);
  52. let len = binary_string.length;
  53. let bytes = new Uint8Array(len);
  54. for (let i = 0; i < len; i++) {
  55. bytes[i] = binary_string.charCodeAt(i);
  56. }
  57. obj[key] = bytes.buffer;
  58. }
  59. } else {
  60. recursiveBase64StrToArrayBuffer(obj[key]);
  61. }
  62. }
  63. }
  64. }
  65. $(window).load(function() {
  66. $(".overlay").hide();
  67. });
  68. $(document).ready(function() {
  69. $(document).on('shown.bs.modal', function(e) {
  70. modal_id = $(e.relatedTarget).data('target');
  71. $(modal_id).attr("aria-hidden","false");
  72. });
  73. // TFA, CSRF, Alerts in footer.inc.php
  74. // Other general functions in mailcow.js
  75. <?php
  76. $alertbox_log_parser = alertbox_log_parser($_SESSION);
  77. if (is_array($alertbox_log_parser)) {
  78. foreach($alertbox_log_parser as $log) {
  79. $alerts[$log['type']][] = $log['msg'];
  80. }
  81. $alerts = array_filter(array_unique($alerts));
  82. foreach($alerts as $alert_type => $alert_msg) {
  83. ?>
  84. mailcow_alert_box(<?=json_encode(implode('<hr class="alert-hr">', $alert_msg));?>, <?=$alert_type;?>);
  85. <?php
  86. }
  87. unset($_SESSION['return']);
  88. }
  89. ?>
  90. // Confirm TFA modal
  91. <?php if (isset($_SESSION['pending_tfa_method'])):?>
  92. $('#ConfirmTFAModal').modal({
  93. backdrop: 'static',
  94. keyboard: false
  95. });
  96. $('#u2f_status_auth').html('<p><i class="bi bi-arrow-repeat icon-spin"></i> ' + lang_tfa.init_u2f + '</p>');
  97. $('#ConfirmTFAModal').on('shown.bs.modal', function(){
  98. $(this).find('input[name=token]').focus();
  99. // If U2F
  100. if(document.getElementById("u2f_auth_data") !== null) {
  101. $.ajax({
  102. type: "GET",
  103. cache: false,
  104. dataType: 'script',
  105. url: "/api/v1/get/u2f-authentication/<?= (isset($_SESSION['pending_mailcow_cc_username'])) ? rawurlencode($_SESSION['pending_mailcow_cc_username']) : null; ?>",
  106. complete: function(data){
  107. $('#u2f_status_auth').html(lang_tfa.waiting_usb_auth);
  108. data;
  109. setTimeout(function() {
  110. console.log("Ready to authenticate");
  111. u2f.sign(appId, challenge, registeredKeys, function(data) {
  112. var form = document.getElementById('u2f_auth_form');
  113. var auth = document.getElementById('u2f_auth_data');
  114. console.log("Authenticate callback", data);
  115. auth.value = JSON.stringify(data);
  116. form.submit();
  117. });
  118. }, 1000);
  119. }
  120. });
  121. }
  122. });
  123. $('#ConfirmTFAModal').on('hidden.bs.modal', function(){
  124. $.ajax({
  125. type: "GET",
  126. cache: false,
  127. dataType: 'script',
  128. url: '/inc/ajax/destroy_tfa_auth.php',
  129. complete: function(data){
  130. window.location = window.location.href.split("#")[0];
  131. }
  132. });
  133. });
  134. <?php endif; ?>
  135. // Validate FIDO2
  136. $("#fido2-login").click(function(){
  137. $('#fido2-alerts').html();
  138. if (!window.fetch || !navigator.credentials || !navigator.credentials.create) {
  139. window.alert('Browser not supported.');
  140. return;
  141. }
  142. window.fetch("/api/v1/get/fido2-get-args", {method:'GET',cache:'no-cache'}).then(function(response) {
  143. return response.json();
  144. }).then(function(json) {
  145. if (json.success === false) {
  146. throw new Error();
  147. }
  148. recursiveBase64StrToArrayBuffer(json);
  149. return json;
  150. }).then(function(getCredentialArgs) {
  151. return navigator.credentials.get(getCredentialArgs);
  152. }).then(function(cred) {
  153. return {
  154. id: cred.rawId ? arrayBufferToBase64(cred.rawId) : null,
  155. clientDataJSON: cred.response.clientDataJSON ? arrayBufferToBase64(cred.response.clientDataJSON) : null,
  156. authenticatorData: cred.response.authenticatorData ? arrayBufferToBase64(cred.response.authenticatorData) : null,
  157. signature : cred.response.signature ? arrayBufferToBase64(cred.response.signature) : null
  158. };
  159. }).then(JSON.stringify).then(function(AuthenticatorAttestationResponse) {
  160. return window.fetch("/api/v1/process/fido2-args", {method:'POST', body: AuthenticatorAttestationResponse, cache:'no-cache'});
  161. }).then(function(response) {
  162. return response.json();
  163. }).then(function(json) {
  164. if (json.success) {
  165. window.location = window.location.href.split("#")[0];
  166. } else {
  167. throw new Error();
  168. }
  169. }).catch(function(err) {
  170. if (typeof err.message === 'undefined') {
  171. mailcow_alert_box(lang_fido2.fido2_validation_failed, "danger");
  172. } else {
  173. mailcow_alert_box(lang_fido2.fido2_validation_failed + ":<br><i>" + err.message + "</i>", "danger");
  174. }
  175. });
  176. });
  177. // Set TFA/FIDO2
  178. $("#register-fido2").click(function(){
  179. $("option:selected").prop("selected", false);
  180. if (!window.fetch || !navigator.credentials || !navigator.credentials.create) {
  181. window.alert('Browser not supported.');
  182. return;
  183. }
  184. window.fetch("/api/v1/get/fido2-registration/<?= (isset($_SESSION['mailcow_cc_username'])) ? rawurlencode($_SESSION['mailcow_cc_username']) : null; ?>", {method:'GET',cache:'no-cache'}).then(function(response) {
  185. return response.json();
  186. }).then(function(json) {
  187. if (json.success === false) {
  188. throw new Error(json.msg);
  189. }
  190. recursiveBase64StrToArrayBuffer(json);
  191. return json;
  192. }).then(function(createCredentialArgs) {
  193. console.log(createCredentialArgs);
  194. return navigator.credentials.create(createCredentialArgs);
  195. }).then(function(cred) {
  196. return {
  197. clientDataJSON: cred.response.clientDataJSON ? arrayBufferToBase64(cred.response.clientDataJSON) : null,
  198. attestationObject: cred.response.attestationObject ? arrayBufferToBase64(cred.response.attestationObject) : null
  199. };
  200. }).then(JSON.stringify).then(function(AuthenticatorAttestationResponse) {
  201. return window.fetch("/api/v1/add/fido2-registration", {method:'POST', body: AuthenticatorAttestationResponse, cache:'no-cache'});
  202. }).then(function(response) {
  203. return response.json();
  204. }).then(function(json) {
  205. if (json.success) {
  206. window.location = window.location.href.split("#")[0];
  207. } else {
  208. throw new Error(json.msg);
  209. }
  210. }).catch(function(err) {
  211. $('#fido2-alerts').html('<span class="text-danger"><b>' + err.message + '</b></span>');
  212. });
  213. });
  214. $('#selectTFA').change(function () {
  215. if ($(this).val() == "yubi_otp") {
  216. $('#YubiOTPModal').modal('show');
  217. $("option:selected").prop("selected", false);
  218. }
  219. if ($(this).val() == "totp") {
  220. $('#TOTPModal').modal('show');
  221. request_token = $('#tfa-qr-img').data('totp-secret');
  222. $.ajax({
  223. url: '/inc/ajax/qr_gen.php',
  224. data: {
  225. token: request_token,
  226. },
  227. }).done(function (result) {
  228. $("#tfa-qr-img").attr("src", result);
  229. });
  230. $("option:selected").prop("selected", false);
  231. }
  232. if ($(this).val() == "u2f") {
  233. $('#U2FModal').modal('show');
  234. $("option:selected").prop("selected", false);
  235. $("#start_u2f_register").click(function(){
  236. $('#u2f_return_code').html('');
  237. $('#u2f_return_code').hide();
  238. $('#u2f_status_reg').html('<p><i class="bi bi-arrow-repeat icon-spin"></i> ' + lang_tfa.init_u2f + '</p>');
  239. $.ajax({
  240. type: "GET",
  241. cache: false,
  242. dataType: 'script',
  243. url: "/api/v1/get/u2f-registration/<?= (isset($_SESSION['mailcow_cc_username'])) ? rawurlencode($_SESSION['mailcow_cc_username']) : null; ?>",
  244. complete: function(data){
  245. data;
  246. setTimeout(function() {
  247. console.log("Ready to register");
  248. $('#u2f_status_reg').html(lang_tfa.waiting_usb_register);
  249. u2f.register(appId, registerRequests, registeredKeys, function(deviceResponse) {
  250. var form = document.getElementById('u2f_reg_form');
  251. var reg = document.getElementById('u2f_register_data');
  252. console.log("Register callback: ", data);
  253. if (deviceResponse.errorCode && deviceResponse.errorCode != 0) {
  254. var u2f_return_code = document.getElementById('u2f_return_code');
  255. u2f_return_code.style.display = u2f_return_code.style.display === 'none' ? '' : null;
  256. if (deviceResponse.errorCode == "4") {
  257. deviceResponse.errorCode = "4 - The presented device is not eligible for this request. For a registration request this may mean that the token is already registered, and for a sign request it may mean that the token does not know the presented key handle";
  258. }
  259. else if (deviceResponse.errorCode == "5") {
  260. deviceResponse.errorCode = "5 - Timeout reached before request could be satisfied.";
  261. }
  262. u2f_return_code.innerHTML = lang_tfa.error_code + ': ' + deviceResponse.errorCode + ' ' + lang_tfa.reload_retry;
  263. return;
  264. }
  265. reg.value = JSON.stringify(deviceResponse);
  266. form.submit();
  267. });
  268. }, 1000);
  269. }
  270. });
  271. });
  272. }
  273. if ($(this).val() == "none") {
  274. $('#DisableTFAModal').modal('show');
  275. $("option:selected").prop("selected", false);
  276. }
  277. });
  278. // Reload after session timeout
  279. var session_lifetime = <?=((int)$SESSION_LIFETIME * 1000) + 15000;?>;
  280. <?php
  281. if (isset($_SESSION['mailcow_cc_username'])):
  282. ?>
  283. setTimeout(function() {
  284. location.reload();
  285. }, session_lifetime);
  286. <?php
  287. endif;
  288. ?>
  289. // CSRF
  290. $('<input type="hidden" value="<?= $_SESSION['CSRF']['TOKEN']; ?>">').attr('name', 'csrf_token').appendTo('form');
  291. if (sessionStorage.scrollTop != "undefined") {
  292. $(window).scrollTop(sessionStorage.scrollTop);
  293. }
  294. });
  295. </script>
  296. <div class="container footer">
  297. <?php if (!empty($UI_TEXTS['ui_footer'])) { ?>
  298. <hr><span class="rot-enc"><?=str_rot13($UI_TEXTS['ui_footer']);?></span>
  299. <?php } ?>
  300. </div>
  301. </body>
  302. </html>
  303. <?php
  304. if (isset($_SESSION['mailcow_cc_api'])) {
  305. session_regenerate_id(true);
  306. session_unset();
  307. session_destroy();
  308. session_write_close();
  309. header("Location: /");
  310. }
  311. $stmt = null;
  312. $pdo = null;