footer.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. foreach($alerts as $alert_type => $alert_msg) {
  82. ?>
  83. mailcow_alert_box(<?=json_encode(implode('<hr class="alert-hr">', $alert_msg));?>, <?=$alert_type;?>);
  84. <?php
  85. }
  86. unset($_SESSION['return']);
  87. }
  88. ?>
  89. // Confirm TFA modal
  90. <?php if (isset($_SESSION['pending_tfa_method'])):?>
  91. $('#ConfirmTFAModal').modal({
  92. backdrop: 'static',
  93. keyboard: false
  94. });
  95. $('#u2f_status_auth').html('<p><span class="glyphicon glyphicon-refresh glyphicon-spin"></span> ' + lang_tfa.init_u2f + '</p>');
  96. $('#ConfirmTFAModal').on('shown.bs.modal', function(){
  97. $(this).find('input[name=token]').focus();
  98. // If U2F
  99. if(document.getElementById("u2f_auth_data") !== null) {
  100. $.ajax({
  101. type: "GET",
  102. cache: false,
  103. dataType: 'script',
  104. url: "/api/v1/get/u2f-authentication/<?= (isset($_SESSION['pending_mailcow_cc_username'])) ? rawurlencode($_SESSION['pending_mailcow_cc_username']) : null; ?>",
  105. complete: function(data){
  106. $('#u2f_status_auth').html(lang_tfa.waiting_usb_auth);
  107. data;
  108. setTimeout(function() {
  109. console.log("Ready to authenticate");
  110. u2f.sign(appId, challenge, registeredKeys, function(data) {
  111. var form = document.getElementById('u2f_auth_form');
  112. var auth = document.getElementById('u2f_auth_data');
  113. console.log("Authenticate callback", data);
  114. auth.value = JSON.stringify(data);
  115. form.submit();
  116. });
  117. }, 1000);
  118. }
  119. });
  120. }
  121. });
  122. $('#ConfirmTFAModal').on('hidden.bs.modal', function(){
  123. $.ajax({
  124. type: "GET",
  125. cache: false,
  126. dataType: 'script',
  127. url: '/inc/ajax/destroy_tfa_auth.php',
  128. complete: function(data){
  129. window.location = window.location.href.split("#")[0];
  130. }
  131. });
  132. });
  133. <?php endif; ?>
  134. // Validate FIDO2
  135. $("#fido2-login").click(function(){
  136. $('#fido2-alerts').html();
  137. if (!window.fetch || !navigator.credentials || !navigator.credentials.create) {
  138. window.alert('Browser not supported.');
  139. return;
  140. }
  141. window.fetch("/api/v1/get/fido2-get-args", {method:'GET',cache:'no-cache'}).then(function(response) {
  142. return response.json();
  143. }).then(function(json) {
  144. if (json.success === false) {
  145. throw new Error();
  146. }
  147. recursiveBase64StrToArrayBuffer(json);
  148. return json;
  149. }).then(function(getCredentialArgs) {
  150. return navigator.credentials.get(getCredentialArgs);
  151. }).then(function(cred) {
  152. return {
  153. id: cred.rawId ? arrayBufferToBase64(cred.rawId) : null,
  154. clientDataJSON: cred.response.clientDataJSON ? arrayBufferToBase64(cred.response.clientDataJSON) : null,
  155. authenticatorData: cred.response.authenticatorData ? arrayBufferToBase64(cred.response.authenticatorData) : null,
  156. signature : cred.response.signature ? arrayBufferToBase64(cred.response.signature) : null
  157. };
  158. }).then(JSON.stringify).then(function(AuthenticatorAttestationResponse) {
  159. return window.fetch("/api/v1/process/fido2-args", {method:'POST', body: AuthenticatorAttestationResponse, cache:'no-cache'});
  160. }).then(function(response) {
  161. return response.json();
  162. }).then(function(json) {
  163. if (json.success) {
  164. window.location = window.location.href.split("#")[0];
  165. } else {
  166. throw new Error();
  167. }
  168. }).catch(function(err) {
  169. mailcow_alert_box(lang_fido2.fido2_validation_failed, "danger");
  170. });
  171. });
  172. // Set TFA/FIDO2
  173. $("#register-fido2").click(function(){
  174. $("option:selected").prop("selected", false);
  175. if (!window.fetch || !navigator.credentials || !navigator.credentials.create) {
  176. window.alert('Browser not supported.');
  177. return;
  178. }
  179. 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) {
  180. return response.json();
  181. }).then(function(json) {
  182. if (json.success === false) {
  183. throw new Error(json.msg);
  184. }
  185. recursiveBase64StrToArrayBuffer(json);
  186. return json;
  187. }).then(function(createCredentialArgs) {
  188. console.log(createCredentialArgs);
  189. return navigator.credentials.create(createCredentialArgs);
  190. }).then(function(cred) {
  191. return {
  192. clientDataJSON: cred.response.clientDataJSON ? arrayBufferToBase64(cred.response.clientDataJSON) : null,
  193. attestationObject: cred.response.attestationObject ? arrayBufferToBase64(cred.response.attestationObject) : null
  194. };
  195. }).then(JSON.stringify).then(function(AuthenticatorAttestationResponse) {
  196. return window.fetch("/api/v1/add/fido2-registration", {method:'POST', body: AuthenticatorAttestationResponse, cache:'no-cache'});
  197. }).then(function(response) {
  198. return response.json();
  199. }).then(function(json) {
  200. if (json.success) {
  201. window.location = window.location.href.split("#")[0];
  202. } else {
  203. throw new Error(json.msg);
  204. }
  205. }).catch(function(err) {
  206. $('#fido2-alerts').html('<span class="text-danger"><b>' + err.message + '</b></span>');
  207. });
  208. });
  209. $('#selectTFA').change(function () {
  210. if ($(this).val() == "yubi_otp") {
  211. $('#YubiOTPModal').modal('show');
  212. $("option:selected").prop("selected", false);
  213. }
  214. if ($(this).val() == "totp") {
  215. $('#TOTPModal').modal('show');
  216. request_token = $('#tfa-qr-img').data('totp-secret');
  217. $.ajax({
  218. url: '/inc/ajax/qr_gen.php',
  219. data: {
  220. token: request_token,
  221. },
  222. }).done(function (result) {
  223. $("#tfa-qr-img").attr("src", result);
  224. });
  225. $("option:selected").prop("selected", false);
  226. }
  227. if ($(this).val() == "u2f") {
  228. $('#U2FModal').modal('show');
  229. $("option:selected").prop("selected", false);
  230. $("#start_u2f_register").click(function(){
  231. $('#u2f_return_code').html('');
  232. $('#u2f_return_code').hide();
  233. $('#u2f_status_reg').html('<p><span class="glyphicon glyphicon-refresh glyphicon-spin"></span> ' + lang_tfa.init_u2f + '</p>');
  234. $.ajax({
  235. type: "GET",
  236. cache: false,
  237. dataType: 'script',
  238. url: "/api/v1/get/u2f-registration/<?= (isset($_SESSION['mailcow_cc_username'])) ? rawurlencode($_SESSION['mailcow_cc_username']) : null; ?>",
  239. complete: function(data){
  240. data;
  241. setTimeout(function() {
  242. console.log("Ready to register");
  243. $('#u2f_status_reg').html(lang_tfa.waiting_usb_register);
  244. u2f.register(appId, registerRequests, registeredKeys, function(deviceResponse) {
  245. var form = document.getElementById('u2f_reg_form');
  246. var reg = document.getElementById('u2f_register_data');
  247. console.log("Register callback: ", data);
  248. if (deviceResponse.errorCode && deviceResponse.errorCode != 0) {
  249. var u2f_return_code = document.getElementById('u2f_return_code');
  250. u2f_return_code.style.display = u2f_return_code.style.display === 'none' ? '' : null;
  251. if (deviceResponse.errorCode == "4") {
  252. 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";
  253. }
  254. else if (deviceResponse.errorCode == "5") {
  255. deviceResponse.errorCode = "5 - Timeout reached before request could be satisfied.";
  256. }
  257. u2f_return_code.innerHTML = lang_tfa.error_code + ': ' + deviceResponse.errorCode + ' ' + lang_tfa.reload_retry;
  258. return;
  259. }
  260. reg.value = JSON.stringify(deviceResponse);
  261. form.submit();
  262. });
  263. }, 1000);
  264. }
  265. });
  266. });
  267. }
  268. if ($(this).val() == "none") {
  269. $('#DisableTFAModal').modal('show');
  270. $("option:selected").prop("selected", false);
  271. }
  272. });
  273. // Reload after session timeout
  274. var session_lifetime = <?=((int)$SESSION_LIFETIME * 1000) + 15000;?>;
  275. <?php
  276. if (isset($_SESSION['mailcow_cc_username'])):
  277. ?>
  278. setTimeout(function() {
  279. location.reload();
  280. }, session_lifetime);
  281. <?php
  282. endif;
  283. ?>
  284. // CSRF
  285. $('<input type="hidden" value="<?= $_SESSION['CSRF']['TOKEN']; ?>">').attr('name', 'csrf_token').appendTo('form');
  286. if (sessionStorage.scrollTop != "undefined") {
  287. $(window).scrollTop(sessionStorage.scrollTop);
  288. }
  289. });
  290. </script>
  291. <div class="container footer">
  292. <?php if (!empty($UI_TEXTS['ui_footer'])) { ?>
  293. <hr><span class="rot-enc"><?=str_rot13($UI_TEXTS['ui_footer']);?></span>
  294. <?php } ?>
  295. </div>
  296. </body>
  297. </html>
  298. <?php
  299. $stmt = null;
  300. $pdo = null;