-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathautodiscover.php
188 lines (152 loc) · 5.2 KB
/
autodiscover.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* Syncroton
*
* @package Syncroton
* @license http://www.tine20.org/licenses/lgpl.html LGPL Version 3
* @copyright Copyright (c) 2013-2013 Metaways Infosystems GmbH (http://www.metaways.de)
* @author Lars Kneschke <[email protected]>
*/
define('SYNCURL', 'https://sync.example.com/Microsoft-Server-ActiveSync');
/**
* exception for data not found
*
* @package Syncroton
* @subpackage Exception
*/
class Syncroton_Exception extends Exception
{
}
/**
* interface for all Syncroton command classes
*
* @package Syncroton
* @subpackage Command
*/
interface Syncroton_Command_ICommand
{
/**
* constructor of this class
*
* @param resource $_requestBody
* @param Syncroton_Model_IDevice $_device
* @param string $_policyKey
*/
public function __construct($_requestBody, Syncroton_Model_IDevice $_device, $_policyKey);
/**
* process the incoming data
*/
public function handle();
/**
* create the response
*/
public function getResponse();
/**
* return headers of command
*
* @return array list of headers
*/
public function getHeaders();
}
/**
* class to handle AutoDiscover command
*
* @package Syncroton
* @subpackage Command
*/
class Syncroton_Command_AutoDiscover implements Syncroton_Command_ICommand
{
/**
* the domDocucment containing the xml request from the client
*
* @var DOMDocument
*/
protected $requestBody;
protected $emailAddress;
public $mobileSyncUrl;
public $certEnrollUrl;
/**
* constructor of this class
*
* @param DOMDocument $_requestBody
* @param Syncroton_Model_IDevice $_device
* @param string $_policyKey
*/
public function __construct($requestBody, Syncroton_Model_IDevice $device = null, $policyKey = null)
{
$this->requestBody = $requestBody;
}
/**
* process the incoming data
*/
public function handle()
{
$xpath = new DomXPath($this->requestBody);
$xpath->registerNamespace('2006', 'http://schemas.microsoft.com/exchange/autodiscover/mobilesync/requestschema/2006');
$nodes = $xpath->query('//2006:Autodiscover/2006:Request/2006:EMailAddress');
if ($nodes->length === 0) {
throw new Syncroton_Exception();
}
$this->emailAddress = $nodes->item(0)->nodeValue;
}
/**
* create the response
*
* @return DOMDocument
*/
public function getResponse()
{
// Creates an instance of the DOMImplementation class
$imp = new DOMImplementation();
// Creates a DOMDocument instance
$document = $imp->createDocument("http://schemas.microsoft.com/exchange/autodiscover/mobilesync/requestschema/2006", 'Autodiscover');
$document->xmlVersion = '1.0';
$document->encoding = 'UTF-8';
$document->formatOutput = false;
$response = $document->documentElement->appendChild($document->createElement('Response'));
$user = $response->appendChild($document->createElement('User'));
$user->appendChild($document->createElement('EMailAddress', $this->emailAddress));
$settings = $document->createElement('Settings');
if (!empty($this->mobileSyncUrl)) {
$server = $document->createElement('Server');
$server->appendChild($document->createElement('Type', 'MobileSync'));
$server->appendChild($document->createElement('Url', $this->mobileSyncUrl));
$server->appendChild($document->createElement('Name', $this->mobileSyncUrl));
$settings->appendChild($server);
}
if (!empty($this->certEnrollUrl)) {
$server = $document->createElement('Server');
$server->appendChild($document->createElement('Type', 'CertEnroll'));
$server->appendChild($document->createElement('Url', $this->certEnrollUrl));
$server->appendChild($document->createElement('Name'));
$server->appendChild($document->createElement('ServerData', 'CertEnrollTemplate'));
$settings->appendChild($server);
}
if ($settings->hasChildNodes()) {
$action = $response->appendChild($document->createElement('Action'));
$action->appendChild($settings);
}
return $document;
}
/**
* return headers of command
*
* @return array list of headers
*/
public function getHeaders()
{
return array(
'Content-Type' => 'text/xml;charset=utf-8'
);
}
}
$document = new DOMDocument();
$document->loadXML(file_get_contents('php://input'));
$autoDiscover = new Syncroton_Command_AutoDiscover($document);
$autoDiscover->mobileSyncUrl = SYNCURL;
$autoDiscover->handle();
$response = $autoDiscover->getResponse();
foreach ($autoDiscover->getHeaders() as $name => $value) {
header($name . ': ' . $value);
}
echo $response->saveXML();