Commit 9ae03bfb authored by Michael Niewoehner's avatar Michael Niewoehner

ZP-1058 autodiscover: Rework exception handling to match z-push core.

Released under the Affero GNU General Public License (AGPL) version
3.
parent 1c560eca
...@@ -104,28 +104,42 @@ class ZPushAutodiscover { ...@@ -104,28 +104,42 @@ class ZPushAutodiscover {
} }
} }
catch (AuthenticationRequiredException $ex) { catch (Exception $ex) {
if (isset($incomingXml)) { // Extract any previous exception message for logging purpose.
// log the failed login attemt e.g. for fail2ban $exclass = get_class($ex);
if (defined('LOGAUTHFAIL') && LOGAUTHFAIL != false) $exception_message = $ex->getMessage();
ZLog::Write(LOGLEVEL_WARN, sprintf("Unable to complete autodiscover because login failed for user with email '%s' from IP %s.", $incomingXml->Request->EMailAddress, $_SERVER["REMOTE_ADDR"])); if($ex->getPrevious()){
do {
$current_exception = $ex->getPrevious();
$exception_message .= ' -> ' . $current_exception->getMessage();
} while($current_exception->getPrevious());
} }
else {
ZLog::Write(LOGLEVEL_ERROR, sprintf("Unable to complete autodiscover incorrect request: '%s'", $ex->getMessage())); ZLog::Write(LOGLEVEL_FATAL, sprintf('Exception: (%s) - %s', $exclass, $exception_message));
if ($ex instanceof AuthenticationRequiredException) {
if (isset($incomingXml)) {
// log the failed login attemt e.g. for fail2ban
if (defined('LOGAUTHFAIL') && LOGAUTHFAIL != false)
ZLog::Write(LOGLEVEL_WARN, sprintf("Unable to complete autodiscover because login failed for user with email '%s' from IP %s.", $incomingXml->Request->EMailAddress, $_SERVER["REMOTE_ADDR"]));
}
else {
ZLog::Write(LOGLEVEL_ERROR, sprintf("Unable to complete autodiscover incorrect request: '%s'", $ex->getMessage()));
}
http_response_code(401);
header('WWW-Authenticate: Basic realm="ZPush"');
} }
http_response_code(401); else if ($ex instanceof ZPushException) {
header('WWW-Authenticate: Basic realm="ZPush"'); ZLog::Write(LOGLEVEL_ERROR, sprintf("Unable to complete autodiscover because of ZPushException. Error: %s", $ex->getMessage()));
} if(!headers_sent()) {
catch (ZPushException $ex) { header('HTTP/1.1 '. $ex->getHTTPCodeString());
ZLog::Write(LOGLEVEL_ERROR, sprintf("Unable to complete autodiscover because of ZPushException. Error: %s", $ex->getMessage())); foreach ($ex->getHTTPHeaders() as $h) {
if(!headers_sent()) { header($h);
header('HTTP/1.1 '. $ex->getHTTPCodeString()); }
foreach ($ex->getHTTPHeaders() as $h) {
header($h);
} }
} }
$this->sendResponse($response);
} }
$this->sendResponse($response);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment