"; endif; if ($debug) : echo "hq_id = " . $hq_id . ""; endif; $usrAccount = ""; $jsAction = ""; if (isset($usr_id) && $usr_id != "" && is_numeric($usr_id) && $usr_id > 0 && isset($hq_id) && $hq_id != "" && is_numeric($hq_id) && $hq_id > 0) : $usrHqId = getFieldValueFromId("user", "usr_id", $usr_id, "hq_id"); $usrAccount = getFieldValueFromId("user", "usr_id", $usr_id, "usr_account"); $usrTotpSecretOld = getFieldValueFromId("user", "usr_id", $usr_id, "usr_totp_secret"); if ($debug) : echo "usrHqId = " . $usrHqId . ""; endif; if ($debug) : echo "usrAccount = " . $usrAccount . ""; endif; if ($debug) : echo "usrTotpSecretOld = " . $usrTotpSecretOld . ""; endif; if (!isset($activated2FA) || $activated2FA == "") : $activated2FA = "0"; endif; if (!isset($f_2faCode)) : $f_2faCode = ""; endif; $ga = new GoogleAuthenticator(); if ($f_act == "verify" && $f_2faCode != "") : // Verify code to check the user scanned code with authentication app $verificationOK = false; $usrTotpSecret = getFieldValueFromId("user", "usr_id", $usr_id, "usr_totp_secret"); if ($usrTotpSecret != "") : $currentCode = $ga->getCode($usrTotpSecret); if ($f_2faCode == $currentCode) : $verificationOK = true; endif; endif; if ($verificationOK) : $res = updateStmt("user","usr_id",$usr_id,array("usr_totp_activated", "1"), "usr_totp_secret != ''"); if ($res > 0) : $tmpNum = rand(0,getrandmax()); $tmpSessionHash = md5($tmpNum); updateStmt("user", "usr_id", $usr_id, array("usr_totp_sessionkey", $tmpSessionHash)); $_SESSION["sessionkey_2fa"] = $tmpSessionHash; $jsAction = "opener.location.reload(); alert('" . getLngt("Das hat geklappt! Eine Deaktivierung können Sie auf der Startseite vornehmen!") . "'); this.close();"; else : $statusMessage = getLngt("Die Aktivierung hat leider nicht geklappt! Beginnen Sie bitte erneut oder wenden Sie sich bitte an Ihren Ansprechpartner!"); $res = updateStmt("user","usr_id",$usr_id,array("usr_totp_secret", "", "usr_totp_activated", "0")); endif; else : $statusMessage = getLngt("Das war leider nicht der korrekte Code. Geben Sie bitte die in der App angezeigte Zahlenfolge erneut ein!"); if ($debug) : echo "VERIFICATION NOT OK:"; endif; if ($debug) : echo "f_2faCode = " . $f_2faCode . ""; endif; if ($debug) : echo "currentCode = " . $currentCode . ""; endif; endif; else : // Generate secret $secret = $ga->createSecret(); if ($debug) : echo "secret = " . $secret . ""; endif; if ($hq_id == $usrHqId) : $res = updateStmt("user","usr_id",$usr_id,array("usr_totp_secret", $secret)); if ($res > 0) : $qrCodeUrl = $ga->getQRCodeGoogleUrl("Stadtbote:votian", $secret); if ($debug) : echo "qrCodeUrl = " . $qrCodeUrl . ""; endif; $oneCode = $ga->getCode($secret); if ($debug) : echo "Checking Code '$oneCode' and Secret '$secret':"; endif; $checkResult = $ga->verifyCode($secret, $oneCode, 2); // 2 = 2*30sec clock tolerance if ($checkResult) { if ($debug) : echo 'OK'; endif; } else { if ($debug) : echo 'FAILED'; endif; } else : $statusMessage = getLngt("Ein Schlüssel konnte nicht erstellt werden!"); endif; else : $statusMessage = getLngt("Ihre aktuelle Niederlassung entspricht nicht der Ihrem Account zugeordneten! Ggfs. vor Aufruf bitte wechseln!"); endif; endif; endif; ?>