forked from jamesiarmes/php-ews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EWSAutodiscover.php
818 lines (727 loc) · 23.2 KB
/
EWSAutodiscover.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
<?php
/**
* Contains EWSAutodiscover.
*/
/**
* Exchange Web Services Autodiscover implementation
*
* This class supports POX (Plain Old XML), which is deprecated but functional
* in Exchange 2010. It may make sense for you to combine your Autodiscovery
* efforts with a SOAP Autodiscover request as well.
*
* USAGE:
*
* (after any auto-loading class incantation)
*
* $ews = EWSAutodiscover::getEWS($email, $password);
*
* -- OR --
*
* If there are issues with your cURL installation that require you to specify
* a path to a valid Certificate Authority, you can configure that manually.
*
* $auto = new EWSAutodiscover($email, $password);
* $auto->setCAInfo('/path/to/your/cacert.pem');
* $ews = $auto->newEWS();
*
* @link http://technet.microsoft.com/en-us/library/bb332063(EXCHG.80).aspx
* @link https://www.testexchangeconnectivity.com/
*
* @package php-ews\AutoDiscovery
*/
class EWSAutodiscover
{
/**
* The path appended to the various schemes and hostnames used during
* autodiscovery.
*
* @var string
*/
const AUTODISCOVER_PATH = '/autodiscover/autodiscover.xml';
/**
* Server was discovered using the TLD method.
*
* @var integer
*/
const AUTODISCOVERED_VIA_TLD = 10;
/**
* Server was discovered using the subdomain method.
*
* @var integer
*/
const AUTODISCOVERED_VIA_SUBDOMAIN = 11;
/**
* Server was discovered using the unauthenticated GET method.
*
* @var integer
*/
const AUTODISCOVERED_VIA_UNAUTHENTICATED_GET = 12;
/**
* Server was discovered using the DNS SRV redirect method.
*
* @var integer
*/
const AUTODISCOVERED_VIA_SRV_RECORD = 13;
/**
* Server was discovered using the HTTP redirect method.
*
* @var integer
*
* @todo We do not currently support this.
*/
const AUTODISCOVERED_VIA_RESPONSE_REDIRECT = 14;
/**
* The email address to attempt autodiscovery against.
*
* @var string
*/
protected $email;
/**
* The password to present during autodiscovery.
*
* @var string
*/
protected $password;
/**
* The Exchange username to use during authentication. If unspecified,
* the provided email address will be used as the username.
*
* @var string
*/
protected $username;
/**
* The top-level domain name, extracted from the provided email address.
*
* @var string
*/
protected $tld;
/**
* The Autodiscover XML request. Since it's used repeatedly, it's cached
* in this property to avoid redundant re-generation.
*
* @var string
*/
protected $requestxml;
/**
* The Certificate Authority path. Should point to a directory containing
* one or more certificates to use in SSL verification.
*
* @var string
*/
protected $capath;
/**
* The path to a specific Certificate Authority file. Get one and use it
* for full Autodiscovery compliance.
*
* @var string
*
* @link http://curl.haxx.se/ca/cacert.pem
* @link http://curl.haxx.se/ca/
*/
protected $cainfo;
/**
* Skip SSL verification. Bad idea, and violates the strict Autodiscover
* protocol. But, here in case you have no other option.
* Defaults to FALSE.
*
* @var boolean
*/
protected $skip_ssl_verification = false;
/**
* An associative array of response headers that resulted from the
* last request. Keys are lowercased for easy checking.
*
* @var array
*/
public $last_response_headers;
/**
* The output of curl_info() relating to the most recent cURL request.
*
* @var array
*/
public $last_info;
/**
* The cURL error code associated with the most recent cURL request.
*
* @var integer
*/
public $last_curl_errno;
/**
* Human-readable description of the most recent cURL error.
*
* @var string
*/
public $last_curl_error;
/**
* The value in seconds to use for Autodiscover host connection timeouts.
* Default connection timeout is 2 seconds, so that unresponsive methods
* can be bypassed quickly.
*
* @var integer
*/
public $connection_timeout = 2;
/**
* Information about an Autodiscover Response containing an error will
* be stored here.
*
* @var mixed
*/
public $error = false;
/**
* Information about an Autodiscover Response with a redirect will be
* retained here.
*
* @var mixed
*/
public $redirect = false;
/**
* A successful, non-error and non-redirect parsed Autodiscover response
* will be stored here.
*
* @var mixed
*/
public $discovered = null;
/**
* Constructor for the EWSAutodiscover class.
*
* @param string $email
* @param string $password
* @param string $username If left blank, the email provided will be used.
*/
public function __construct($email, $password, $username = null)
{
$this->email = $email;
$this->password = $password;
if ($username === null) {
$this->username = $email;
} else {
$this->username = $username;
}
$this->setTLD();
}
/**
* Execute the full discovery chain of events in the correct sequence
* until a valid response is received, or all methods have failed.
*
* @return An AUTODISCOVERED_VIA_* constant or FALSE on failure.
*/
public function discover()
{
$result = $this->tryTLD();
if ($result === false) {
$result = $this->trySubdomain();
}
if ($result === false) {
$result = $this->trySubdomainUnauthenticatedGet();
}
if ($result === false) {
$result = $this->trySRVRecord();
}
return $result;
}
/**
* Return the settings discovered from the Autodiscover process.
*
* NULL indicates discovery hasn't completed (or been attempted)
* FALSE indicates discovery wasn't successful. Check for errors
* or redirects.
* An array will be returned with discovered settings on success.
*
* @return mixed
*/
public function discoveredSettings()
{
return $this->discovered;
}
/**
* Toggle skipping of SSL verification in cURL requests.
*
* @param boolean $skip To skip, or not.
* @return self
*/
public function skipSSLVerification($skip = true)
{
$this->skip_ssl_verification = (bool) $skip;
return $this;
}
/**
* Parse the hex ServerVersion value and return a valid
* ExchangeWebServices::VERSION_* constant.
*
* @return string|boolean A known version constant, or FALSE if it could not
* be determined.
*
* @link http://msdn.microsoft.com/en-us/library/bb204122(v=exchg.140).aspx
* @link http://blogs.msdn.com/b/pcreehan/archive/2009/09/21/parsing-serverversion-when-an-int-is-really-5-ints.aspx
* @link http://office.microsoft.com/en-us/outlook-help/determine-the-version-of-microsoft-exchange-server-my-account-connects-to-HA001191800.aspx
*/
public function parseServerVersion($version_hex)
{
$svbinary = base_convert($version_hex, 16, 2);
if (strlen($svbinary) == 31) {
$svbinary = '0'.$svbinary;
}
$majorversion = base_convert(substr($svbinary, 4, 6), 2, 10);
$minorversion = base_convert(substr($svbinary, 10, 6), 2, 10);
$buildversion = base_convert(substr($svbinary, 17, 15), 2, 10);
if ($majorversion == 8) {
switch ($minorversion) {
case 0:
return ExchangeWebServices::VERSION_2007;
break;
case 1:
return ExchangeWebServices::VERSION_2007_SP1;
break;
case 2:
return ExchangeWebServices::VERSION_2007_SP2;
break;
case 3:
return ExchangeWebServices::VERSION_2007_SP3;
break;
default:
return ExchangeWebServices::VERSION_2007;
}
} elseif ($majorversion == 14) {
switch ($minorversion) {
case 0:
return ExchangeWebServices::VERSION_2010;
break;
case 1:
return ExchangeWebServices::VERSION_2010_SP1;
break;
case 2:
return ExchangeWebServices::VERSION_2010_SP2;
break;
default:
return ExchangeWebServices::VERSION_2010;
}
}
// Guess we didn't find a known version.
return false;
}
/**
* Method to return a new ExchangeWebServices object, auto-configured
* with the proper hostname.
*
* @return mixed ExchangeWebServices object on success, FALSE on failure.
*/
public function newEWS()
{
// Discovery not yet attempted.
if ($this->discovered === null) {
$this->discover();
}
// Discovery not successful.
if ($this->discovered === false) {
return false;
}
$server = false;
$version = null;
// Pick out the host from the EXPR (Exchange RPC over HTTP).
foreach ($this->discovered['Account']['Protocol'] as $protocol) {
if (
($protocol['Type'] == 'EXCH' || $protocol['Type'] == 'EXPR')
&& isset($protocol['ServerVersion'])
) {
if ($version == null) {
$sv = $this->parseServerVersion($protocol['ServerVersion']);
if ($sv !== false) {
$version = $sv;
}
}
}
if ($protocol['Type'] == 'EXPR' && isset($protocol['Server'])) {
$server = $protocol['Server'];
}
}
if ($server) {
if ($version === null) {
// EWS class default.
$version = ExchangeWebServices::VERSION_2007;
}
return new ExchangeWebServices(
$server,
$this->email,
$this->password,
$version
);
}
return false;
}
/**
* Static method may fail if there are issues surrounding SSL certificates.
* In such cases, set up the object as needed, and then call newEWS().
*
* @param string $email
* @param string $password
* @param string $username If left blank, the email provided will be used.
* @return mixed
*/
public static function getEWS($email, $password, $username = null)
{
$auto = new EWSAutodiscover($email, $password, $username);
return $auto->newEWS();
}
/**
* Perform an NTLM authenticated HTTPS POST to the top-level
* domain of the email address.
*
* @return An AUTODISCOVERED_VIA_* constant or FALSE on failure.
*/
public function tryTLD()
{
$url = 'https://www.'.$this->tld . self::AUTODISCOVER_PATH;
$result = $this->doNTLMPost($url, 5);
if ($result) {
return self::AUTODISCOVERED_VIA_TLD;
}
return false;
}
/**
* Perform an NTLM authenticated HTTPS POST to the 'autodiscover'
* subdomain of the email address' TLD.
*
* @return An AUTODISCOVERED_VIA_* constant or FALSE on failure.
*/
public function trySubdomain()
{
$url = 'https://autodiscover.'.$this->tld . self::AUTODISCOVER_PATH;
$result = $this->doNTLMPost($url, 5);
if ($result) {
return self::AUTODISCOVERED_VIA_SUBDOMAIN;
}
return false;
}
/**
* Perform an unauthenticated HTTP GET in an attempt to get redirected
* via 302 to the correct location to perform the HTTPS POST.
*
* @return An AUTODISCOVERED_VIA_* constant or FALSE on failure.
*/
public function trySubdomainUnauthenticatedGet()
{
$this->reset();
$url = 'http://autodiscover.'.$this->tld . self::AUTODISCOVER_PATH;
$ch = curl_init();
$opts = array(
CURLOPT_URL => $url,
CURLOPT_HTTPGET => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 4,
CURLOPT_CONNECTTIMEOUT => $this->connection_timeout,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HEADER => false,
CURLOPT_HEADERFUNCTION => array($this, 'readHeaders'),
CURLOPT_HTTP200ALIASES => array(301, 302),
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4
);
curl_setopt_array($ch, $opts);
$this->last_response = curl_exec($ch);
$this->last_info = curl_getinfo($ch);
$this->last_curl_errno = curl_errno($ch);
$this->last_curl_error = curl_error($ch);
if (
$this->last_info['http_code'] == 302
|| $this->last_info['http_code'] == 301
) {
// Do the NTLM POST to the redirect.
$result = $this->doNTLMPost(
$this->last_response_headers['location']
);
if ($result) {
return self::AUTODISCOVERED_VIA_UNAUTHENTICATED_GET;
}
}
return false;
}
/**
* Attempt to retrieve the autodiscover host from an SRV DNS record.
*
* @link http://support.microsoft.com/kb/940881
* @return self::AUTODISCOVERED_VIA_SRV_RECORD or false
*/
public function trySRVRecord()
{
$srvhost = '_autodiscover._tcp.' . $this->tld;
$lookup = dns_get_record($srvhost, DNS_SRV);
if (sizeof($lookup) > 0) {
$host = $lookup[0]['target'];
$url = 'https://' . $host . self::AUTODISCOVER_PATH;
$result = $this->doNTLMPost($url);
if ($result) {
return self::AUTODISCOVERED_VIA_SRV_RECORD;
}
}
return false;
}
/**
* Set the path to the file to be used by CURLOPT_CAINFO.
*
* @param string $path Path to a certificate file such as cacert.pem
* @return self
*/
public function setCAInfo($path)
{
if (file_exists($path) && is_file($path)) {
$this->cainfo = $path;
}
return $this;
}
/**
* Set the path to the file to be used by CURLOPT_CAPATH.
*
* @param string $path Path to a directory containing one or more CA
* certificates.
* @return self
*/
public function setCAPath($path)
{
if (is_dir($path)) {
$this->capath = $path;
}
return $this;
}
/**
* Set a connection timeout for the POST methods.
*
* @param integer $seconds Seconds to wait for a connection.
* @return self
*/
public function setConnectionTimeout($seconds)
{
$this->connection_timeout = intval($seconds);
return $this;
}
/**
* Perform the NTLM authenticated post against one of the chosen
* endpoints.
*
* @param string $url URL to try posting to
* @param integer $timeout Overall cURL timeout for this request
* @return boolean
*/
public function doNTLMPost($url, $timeout = 6)
{
$this->reset();
$ch = curl_init();
$opts = array(
CURLOPT_URL => $url,
CURLOPT_HTTPAUTH => CURLAUTH_NTLM,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $this->getAutoDiscoverRequest(),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => $this->username.':'.$this->password,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_CONNECTTIMEOUT => $this->connection_timeout,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => false,
CURLOPT_HEADERFUNCTION => array($this, 'readHeaders'),
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => true,
);
// Set the appropriate content-type.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8'));
if (! empty($this->cainfo)) {
$opts[CURLOPT_CAINFO] = $this->cainfo;
}
if (! empty($this->capath)) {
$opts[CURLOPT_CAPATH] = $this->capath;
}
if ($this->skip_ssl_verification) {
$opts[CURLOPT_SSL_VERIFYPEER] = false;
$opts[CURLOPT_SSL_VERIFYHOST] = false;
}
curl_setopt_array($ch, $opts);
$this->last_response = curl_exec($ch);
$this->last_info = curl_getinfo($ch);
$this->last_curl_errno = curl_errno($ch);
$this->last_curl_error = curl_error($ch);
if ($this->last_curl_errno != CURLE_OK) {
return false;
}
$discovered = $this->parseAutodiscoverResponse();
return $discovered;
}
/**
* Parse the Autoresponse Payload, particularly to determine if an
* additional request is necessary.
*
* @return mixed FALSE if response isn't XML or parsed response array
*/
protected function parseAutodiscoverResponse()
{
// Content-type isn't trustworthy, unfortunately. Shame on Microsoft.
if (substr($this->last_response, 0, 5) !== '<?xml') {
return false;
}
$response = $this->responseToArray($this->last_response);
if (isset($response['Error'])) {
$this->error = $response['Error'];
return false;
}
// Check the account action for redirect.
switch ($response['Account']['Action']) {
case 'redirectUrl':
$this->redirect = array(
'redirectUrl' => $response['Account']['redirectUrl']
);
return false;
break;
case 'redirectAddr':
$this->redirect = array(
'redirectAddr' => $response['Account']['redirectAddr']
);
return false;
break;
case 'settings':
default:
$this->discovered = $response;
return true;
}
}
/**
* Set the top-level domain to be used with autodiscover attempts based
* on the provided email address.
*
* @return boolean
*/
protected function setTLD()
{
$pos = strpos($this->email, '@');
if ($pos !== false) {
$this->tld = trim(substr($this->email, $pos+1));
return true;
}
return false;
}
/**
* Reset the response-related structures. Called before making a new
* request.
*
* @return self
*/
public function reset()
{
$this->last_response_headers = array();
$this->last_info = array();
$this->last_curl_errno = 0;
$this->last_curl_error = '';
return $this;
}
/**
* Return the generated Autodiscover XML request body.
*
* @return string
*/
public function getAutodiscoverRequest()
{
if (! empty($this->requestxml)) {
return $this->requestxml;
}
$xml = new XMLWriter;
$xml->openMemory();
$xml->setIndent(true);
$xml->startDocument('1.0', 'UTF-8');
$xml->startElementNS(
null,
'Autodiscover',
'http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006'
);
$xml->startElement('Request');
$xml->writeElement('EMailAddress', $this->email);
$xml->writeElement(
'AcceptableResponseSchema',
'http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a'
);
$xml->endElement();
$xml->endElement();
$this->requestxml = $xml->outputMemory();
return $this->requestxml;
}
/**
* Utility function to pick headers off of the incoming cURL response.
* Used with CURLOPT_HEADERFUNCTION.
*
* @param resource $_ch cURL handle
* @param string $str Header string to read
* @return integer Bytes read
*/
public function readHeaders($_ch, $str)
{
$pos = strpos($str, ':');
if ($pos !== false) {
$key = strtolower(substr($str, 0, $pos));
$val = trim(substr($str, $pos+1));
$this->last_response_headers[$key] = $val;
}
return strlen($str);
}
/**
* Utility function to parse XML payloads from the response into easier
* to manage associative arrays.
*
* @param string $xml XML to parse
* @return array
*/
public function responseToArray($xml)
{
$doc = new \DOMDocument();
$doc->loadXML($xml);
$out = $this->nodeToArray($doc->documentElement);
return $out['Response'];
}
/**
* Recursive method for parsing DOM nodes.
*
* @link https://github.com/gaarf/XML-string-to-PHP-array
* @param object $node DOMNode object
* @return mixed
*/
protected function nodeToArray($node)
{
$output = array();
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i=0, $m = $node->childNodes->length; $i < $m; $i++) {
$child = $node->childNodes->item($i);
$v = $this->nodeToArray($child);
if (isset($child->tagName)) {
$t = $child->tagName;
if (!isset($output[$t])) {
$output[$t] = array();
}
$output[$t][] = $v;
} elseif ($v || $v === '0') {
$output = (string) $v;
}
}
// Edge case of a node containing a text node, which also has
// attributes. this way we'll retain text and attributes for
// this node.
if (is_string($output) && $node->attributes->length) {
$output = array('@text' => $output);
}
if (is_array($output)) {
if ($node->attributes->length) {
$a = array();
foreach ($node->attributes as $attrName => $attrNode) {
$a[$attrName] = (string) $attrNode->value;
}
$output['@attributes'] = $a;
}
foreach ($output as $t => $v) {
if (is_array($v) && count($v)==1 && $t!='@attributes') {
$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
}