Changeset View
Changeset View
Standalone View
Standalone View
lib/AutodiscoverJson.php
Show All 23 Lines | |||||
*/ | */ | ||||
/** | /** | ||||
* Autodiscover Service class for Microsoft Autodiscover V2 | * Autodiscover Service class for Microsoft Autodiscover V2 | ||||
*/ | */ | ||||
class AutodiscoverJson extends Autodiscover | class AutodiscoverJson extends Autodiscover | ||||
{ | { | ||||
/** | |||||
* process incoming request | |||||
*/ | |||||
public function handle_request() | public function handle_request() | ||||
{ | { | ||||
Log::debug('Request [json]: ' . $_SERVER['REQUEST_URI']); | |||||
// 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' | |||||
); | |||||
} | |||||
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' | |||||
); | |||||
} | |||||
// check email | |||||
if (preg_match('|autodiscover.json/v1.0/([^\?]+)|', $_SERVER['REQUEST_URI'], $regs)) { | if (preg_match('|autodiscover.json/v1.0/([^\?]+)|', $_SERVER['REQUEST_URI'], $regs)) { | ||||
$this->email = $regs[1]; | $this->email = $regs[1]; | ||||
} | } | ||||
elseif (!empty($_GET['Email'])) { | |||||
$this->email = $_GET['Email']; | |||||
} | |||||
elseif (!empty($_GET['email'])) { | |||||
$this->email = $_GET['email']; | |||||
} | |||||
Log::debug('Request [json]: ' . $_SERVER['REQUEST_URI']); | if (empty($this->email) || !strpos($this->email, '@')) { | ||||
$this->error( | |||||
'A valid smtp address must be provided', | |||||
'MandatoryParameterMissing' | |||||
); | |||||
} | |||||
} | } | ||||
/** | /** | ||||
* Generates JSON response | * Generates JSON response | ||||
*/ | */ | ||||
protected function handle_response() | protected function handle_response() | ||||
{ | { | ||||
if (strtolower($_GET['Protocol']) == 'activesync' | if (strtolower($_GET['Protocol']) == 'activesync') { | ||||
&& !empty($this->config['activesync']) | // throw error if activesync is disable | ||||
) { | 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'])) { | if (!preg_match('/^https?:/i', $this->config['activesync'])) { | ||||
$this->config['activesync'] = 'https://' . $this->config['activesync'] . '/Microsoft-Server-ActiveSync'; | $this->config['activesync'] = 'https://' . $this->config['activesync'] . '/Microsoft-Server-ActiveSync'; | ||||
} | } | ||||
$json = array( | $json = array( | ||||
'Protocol' => 'ActiveSync', | 'Protocol' => 'ActiveSync', | ||||
'Url' => $this->config['activesync'] | 'Url' => $this->config['activesync'] | ||||
); | ); | ||||
} | } | ||||
elseif (strtolower($_GET['Protocol']) == 'autodiscoverv1') { | elseif (strtolower($_GET['Protocol']) == 'autodiscoverv1') { | ||||
$json = array( | $json = array( | ||||
'Protocol' => 'ActiveSync', | 'Protocol' => 'AutodiscoverV1', | ||||
'Url' => 'https://' . $_SERVER['HTTP_HOST'] . '/Autodiscover/Autodiscover.xml' | 'Url' => 'https://' . $_SERVER['HTTP_HOST'] . '/Autodiscover/Autodiscover.xml' | ||||
); | ); | ||||
} | } | ||||
else { | |||||
http_response_code(400); | |||||
$json = array( | |||||
'ErrorCore' => 'InvalidProtocol', | |||||
'ErrorMessage' => 'The given protocol value \u0027' | |||||
. $_GET['Protocol'] | |||||
. '\u0027 is invalid. Supported values are \u0027' | |||||
. (!empty($this->config['activesync']) ? 'ActiveSync,' : '') | |||||
. 'AutodiscoverV1\u0027' | |||||
); | |||||
} | |||||
$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); | Log::debug('Response [json]: ' . $response); | ||||
header('Content-Type: application/json; charset=' . Autodiscover::CHARSET); | header('Content-Type: application/json; charset=' . Autodiscover::CHARSET); | ||||
echo $response; | echo $response; | ||||
exit; | exit; | ||||
} | } | ||||
/** | |||||
* Send error to the client and exit | |||||
*/ | |||||
protected function error($msg, $code="InternalServerError") | |||||
{ | |||||
http_response_code(400); | |||||
$json = array( | |||||
'ErrorCode' => $code, | |||||
machniak: I guess it should be `ErrorCode`. | |||||
'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; | |||||
} | |||||
} | } |
I guess it should be ErrorCode.