diff --git a/lib/AutodiscoverJson.php b/lib/AutodiscoverJson.php --- a/lib/AutodiscoverJson.php +++ b/lib/AutodiscoverJson.php @@ -29,40 +29,49 @@ class AutodiscoverJson extends Autodiscover { + /** + * process incoming request + */ public function handle_request() { Log::debug('Request [json]: ' . $_SERVER['REQUEST_URI']); - $supportedProtocols = array('autodiscoverv1' => 'AutodiscoverV1'); - if ($this->conf->get('autodiscover', 'activesync')) { - $supportedProtocols['activesync'] = 'ActiveSync'; + // check protocol (at this state we don't know if autodiscover is configured) + $allowedProtocols = ['activesync', 'autodiscoverv1']; + if (empty($_GET['Protocol'])) { + $this->error( + "A valid value must be provided for the query parameter 'Protocol'", + 'MandatoryParameterMissing' + ); } - - $protocol = isset($_GET['Protocol']) ? $_GET['Protocol'] : ''; - - // Exit early on unsupported protocol - if (empty($protocol) || !isset($supportedProtocols[strtolower($protocol)])) { - $json = array( - 'ErrorCode' => 'ProtocolNotSupported', - 'ErrorMessage' => 'The given protocol value \u0027' . $protocol . '\u0027 is invalid.' - . ' Supported values are \u0027' . implode(',', $supportedProtocols) . '\u0027' + elseif (!in_array(strtolower($_GET['Protocol']), $allowedProtocols)) { + $this->error( + sprintf( + "The given protocol value '%s' is invalid. Supported values are '%s'", + $_GET['Protocol'], + implode(",", $allowedProtocols) + ), + 'InvalidProtocol' ); - - $response = json_encode($json, JSON_PRETTY_PRINT); - Log::debug('Response [json]: ' . $response); - - http_response_code(400); - header('Content-Type: application/json; charset=' . Autodiscover::CHARSET); - echo $response; - exit; } + // check email if (preg_match('|autodiscover.json/v1.0/([^\?]+)|', $_SERVER['REQUEST_URI'], $regs)) { $this->email = $regs[1]; } - else if (!empty($_GET['Email'])) { + elseif (!empty($_GET['Email'])) { $this->email = $_GET['Email']; } + elseif (!empty($_GET['email'])) { + $this->email = $_GET['email']; + } + + if (empty($this->email) || !strpos($this->email, '@')) { + $this->error( + 'A valid smtp address must be provided', + 'MandatoryParameterMissing' + ); + } } /** @@ -70,9 +79,18 @@ */ protected function handle_response() { - if (strtolower($_GET['Protocol']) == 'activesync' - && !empty($this->config['activesync']) - ) { + if (strtolower($_GET['Protocol']) == 'activesync') { + // throw error if activesync is disabled + if (empty($this->config['activesync'])) { + $this->error( + sprintf( + "The given protocol value '%s' is invalid. Supported values are '%s'", + $_GET['Protocol'], 'autodiscoverv1' + ), + 'InvalidProtocol' + ); + } + if (!preg_match('/^https?:/i', $this->config['activesync'])) { $this->config['activesync'] = 'https://' . $this->config['activesync'] . '/Microsoft-Server-ActiveSync'; } @@ -88,11 +106,29 @@ ); } - $response = json_encode($json, JSON_PRETTY_PRINT); + $response = json_encode($json, JSON_PRETTY_PRINT | JSON_HEX_APOS | JSON_HEX_QUOT); Log::debug('Response [json]: ' . $response); header('Content-Type: application/json; charset=' . Autodiscover::CHARSET); echo $response; exit; } + + /** + * Send error to the client and exit + */ + protected function error($msg, $code="InternalServerError") + { + http_response_code(400); + $json = array( + 'ErrorCode' => $code, + 'ErrorMessage' => $msg + ); + $response = json_encode($json, JSON_PRETTY_PRINT | JSON_HEX_APOS | JSON_HEX_QUOT); + Log::debug('Error [json]: ' . $response); + header('Content-Type: application/json; charset=' . Autodiscover::CHARSET); + echo $response; + exit; + } + } diff --git a/lib/AutodiscoverMicrosoft.php b/lib/AutodiscoverMicrosoft.php --- a/lib/AutodiscoverMicrosoft.php +++ b/lib/AutodiscoverMicrosoft.php @@ -121,6 +121,42 @@ exit; } + /** + * Send error to the client and exit + */ + protected function error($msg) + { + $xml = new DOMDocument('1.0', Autodiscover::CHARSET); + $doc = $xml->createElementNS(self::NS, 'Autodiscover'); + $doc = $xml->appendChild($doc); + + $response = $xml->createElement('Response'); + $response = $doc->appendChild($response); + + $error = $xml->createElement('Error'); + list($usec, $sec) = explode(' ', microtime()); + $error->setAttribute('Time',date('H:i:s',$sec).".".substr($usec, 2, 6)); + $error->setAttribute('Id',sprintf("%u",crc32($_SERVER['HTTP_HOST']))); + $response->appendChild($error); + + $code = $xml->createElement('ErrorCode'); + $code->appendChild($xml->createTextNode(600)); + $error->appendChild($code); + + $message = $xml->createElement('Message'); + $message->appendChild($xml->createTextNode($msg)); + $error->appendChild($message); + + $response->appendChild($xml->createElement('DebugData')); + + $xml->formatOutput = true; + Log::debug('Error [microsoft]: ' . $msg); + + header('Content-type: text/xml; charset=' . Autodiscover::CHARSET); + echo $xml->saveXML(); + exit; + } + /** * Generates XML response for Activesync */