footer.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. if (typeof err.message === 'undefined') {
  170. mailcow_alert_box(lang_fido2.fido2_validation_failed, "danger");
  171. } else {
  172. mailcow_alert_box(lang_fido2.fido2_validation_failed + ":<br><i>" + err.message + "</i>", "danger");
  173. }
  174. });
  175. });
  176. // Set TFA/FIDO2
  177. $("#register-fido2").click(function(){
  178. $("option:selected").prop("selected", false);
  179. if (!window.fetch || !navigator.credentials || !navigator.credentials.create) {
  180. window.alert('Browser not supported.');
  181. return;
  182. }
  183. 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) {
  184. return response.json();
  185. }).then(function(json) {
  186. if (json.success === false) {
  187. throw new Error(json.msg);
  188. }
  189. recursiveBase64StrToArrayBuffer(json);
  190. return json;
  191. }).then(function(createCredentialArgs) {
  192. console.log(createCredentialArgs);
  193. return navigator.credentials.create(createCredentialArgs);
  194. }).then(function(cred) {
  195. return {
  196. clientDataJSON: cred.response.clientDataJSON ? arrayBufferToBase64(cred.response.clientDataJSON) : null,
  197. attestationObject: cred.response.attestationObject ? arrayBufferToBase64(cred.response.attestationObject) : null
  198. };
  199. }).then(JSON.stringify).then(function(AuthenticatorAttestationResponse) {
  200. return window.fetch("/api/v1/add/fido2-registration", {method:'POST', body: AuthenticatorAttestationResponse, cache:'no-cache'});
  201. }).then(function(response) {
  202. return response.json();
  203. }).then(function(json) {
  204. if (json.success) {
  205. window.location = window.location.href.split("#")[0];
  206. } else {
  207. throw new Error(json.msg);
  208. }
  209. }).catch(function(err) {
  210. $('#fido2-alerts').html('<span class="text-danger"><b>' + err.message + '</b></span>');
  211. });
  212. });
  213. $('#selectTFA').change(function () {
  214. if ($(this).val() == "yubi_otp") {
  215. $('#YubiOTPModal').modal('show');
  216. $("option:selected").prop("selected", false);
  217. }
  218. if ($(this).val() == "totp") {
  219. $('#TOTPModal').modal('show');
  220. request_token = $('#tfa-qr-img').data('totp-secret');
  221. $.ajax({
  222. url: '/inc/ajax/qr_gen.php',
  223. data: {
  224. token: request_token,
  225. },
  226. }).done(function (result) {
  227. $("#tfa-qr-img").attr("src", result);
  228. });
  229. $("option:selected").prop("selected", false);
  230. }
  231. if ($(this).val() == "u2f") {
  232. $('#U2FModal').modal('show');
  233. $("option:selected").prop("selected", false);
  234. $("#start_u2f_register").click(function(){
  235. $('#u2f_return_code').html('');
  236. $('#u2f_return_code').hide();
  237. $('#u2f_status_reg').html('<p><span class="glyphicon glyphicon-refresh glyphicon-spin"></span> ' + lang_tfa.init_u2f + '</p>');
  238. $.ajax({
  239. type: "GET",
  240. cache: false,
  241. dataType: 'script',
  242. url: "/api/v1/get/u2f-registration/<?= (isset($_SESSION['mailcow_cc_username'])) ? rawurlencode($_SESSION['mailcow_cc_username']) : null; ?>",
  243. complete: function(data){
  244. data;
  245. setTimeout(function() {
  246. console.log("Ready to register");
  247. $('#u2f_status_reg').html(lang_tfa.waiting_usb_register);
  248. u2f.register(appId, registerRequests, registeredKeys, function(deviceResponse) {
  249. var form = document.getElementById('u2f_reg_form');
  250. var reg = document.getElementById('u2f_register_data');
  251. console.log("Register callback: ", data);
  252. if (deviceResponse.errorCode && deviceResponse.errorCode != 0) {
  253. var u2f_return_code = document.getElementById('u2f_return_code');
  254. u2f_return_code.style.display = u2f_return_code.style.display === 'none' ? '' : null;
  255. if (deviceResponse.errorCode == "4") {
  256. 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";
  257. }
  258. else if (deviceResponse.errorCode == "5") {
  259. deviceResponse.errorCode = "5 - Timeout reached before request could be satisfied.";
  260. }
  261. u2f_return_code.innerHTML = lang_tfa.error_code + ': ' + deviceResponse.errorCode + ' ' + lang_tfa.reload_retry;
  262. return;
  263. }
  264. reg.value = JSON.stringify(deviceResponse);
  265. form.submit();
  266. });
  267. }, 1000);
  268. }
  269. });
  270. });
  271. }
  272. if ($(this).val() == "none") {
  273. $('#DisableTFAModal').modal('show');
  274. $("option:selected").prop("selected", false);
  275. }
  276. });
  277. // Reload after session timeout
  278. var session_lifetime = <?=((int)$SESSION_LIFETIME * 1000) + 15000;?>;
  279. <?php
  280. if (isset($_SESSION['mailcow_cc_username'])):
  281. ?>
  282. setTimeout(function() {
  283. location.reload();
  284. }, session_lifetime);
  285. <?php
  286. endif;
  287. ?>
  288. // CSRF
  289. $('<input type="hidden" value="<?= $_SESSION['CSRF']['TOKEN']; ?>">').attr('name', 'csrf_token').appendTo('form');
  290. if (sessionStorage.scrollTop != "undefined") {
  291. $(window).scrollTop(sessionStorage.scrollTop);
  292. }
  293. });
  294. </script>
  295. <div class="container footer">
  296. <?php if (!empty($UI_TEXTS['ui_footer'])) { ?>
  297. <hr><span class="rot-enc"><?=str_rot13($UI_TEXTS['ui_footer']);?></span>
  298. <?php } ?>
  299. </div>
  300. </body>
  301. </html>
  302. <?php
  303. $stmt = null;
  304. $pdo = null;