sogo-auth.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * currently disabled: we could add auth_request to ningx sogo_eas.template
  4. * but this seems to be not required with the postfix allow_real_nets option
  5. */
  6. /*
  7. if (substr($_SERVER['HTTP_X_ORIGINAL_URI'], 0, 28) === "/Microsoft-Server-ActiveSync") {
  8. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  9. $server=print_r($_SERVER, true);
  10. $username = $_SERVER['PHP_AUTH_USER'];
  11. $password = $_SERVER['PHP_AUTH_PW'];
  12. $login_check = check_login($username, $password);
  13. if ($login_check !== 'user') {
  14. header('HTTP/1.0 401 Unauthorized');
  15. echo 'Invalid login';
  16. exit;
  17. } else {
  18. echo 'Login OK';
  19. exit;
  20. }
  21. } else {
  22. // other code
  23. }
  24. */
  25. $ALLOW_ADMIN_EMAIL_LOGIN = (preg_match(
  26. "/^([yY][eE][sS]|[yY])+$/",
  27. $_ENV["ALLOW_ADMIN_EMAIL_LOGIN"]
  28. ));
  29. $session_variable = 'sogo-sso-user';
  30. if (!$ALLOW_ADMIN_EMAIL_LOGIN) {
  31. header("Location: /");
  32. exit;
  33. } else if (isset($_GET['login'])) {
  34. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  35. if (isset($_SESSION['mailcow_cc_role']) && $_SESSION['acl']['login_as'] == "1") {
  36. $login = html_entity_decode(rawurldecode($_GET["login"]));
  37. if (filter_var($login, FILTER_VALIDATE_EMAIL)) {
  38. if (!empty(mailbox('get', 'mailbox_details', $login))) {
  39. $_SESSION[$session_variable] = $login;
  40. header("Location: /SOGo/");
  41. exit;
  42. }
  43. }
  44. }
  45. header("Location: /");
  46. exit;
  47. } else {
  48. // this is an nginx auth_request call, we check for an existing sogo-sso-user session variable
  49. session_start();
  50. $username = "";
  51. if (isset($_SESSION[$session_variable]) && filter_var($_SESSION[$session_variable], FILTER_VALIDATE_EMAIL)) {
  52. $username = $_SESSION[$session_variable];
  53. }
  54. // if username is empty, SOGo will display the normal login form
  55. header("X-Username: $username");
  56. exit;
  57. }