OK that title is not very smooth or clever, but a project I worked on several years ago was syncing Microsoft Exchange with PHP. There's a lot out there not documented, primarily because there are several problems with PHP's implementation of XML and Microsoft's over-use of namespaces and so forth. However, with some serious hacking, I've come up with some solutions.
This was initially based on some code I had found online, however it was so long ago I have not been able to figure it out so I cannot provide proper attribution. Additionally, impersonation does work, and I've seen a lot of people have a problem with this as well.
The biggest thing is it requires a lot of manual XML to actually work, you can't build the objects and expect it to work correctly, it literally will not, primarily due to PHP's SOAP class.
The primary benefit of this code is to see how I hacked around dealing with PHP's issues when it came to trying to deal with the Exchange calendar and task system.
Feel free to reply with any fixes, updates, etc.
A few things to note:
- When I created this, it was a hackfest with very little time, so the code is not my best work at all; I mean it uses globals for god sake, and improper OOP, improper usage of PSR, classes doing far too much, etc.
- This code was built for PHP 5.2 – 5.4, and takes no advantage of PHP 7 or anything.
- Yes, those two above mean I am embarrassed by the quality of this code, but hiding it isn't really useful to anyone, especially because I am not using it in my project any more.
- This will not work out of box, because it was built specifically for my project, however because it was such a nightmare and other people are struggling, I'm sharing it. It should give you a good idea of where to start and how to deal with some hiccoughs.
- I assume you've already got the basis down, and you've got messages.xsd, Services.wsdl, and types.xsd, and all the other stuff. Basically I am just assuming you've got it working, you're just running into problems actually doing anything useful.
Usage from my job which pull down basically everything first. This is because repeating calendar entries are only listed once, so you have to pre-grab and store everything in order to deal with it later. The most important aspects are storing the ID and change key so that you can properly manage it later. If someone edits a calendar entry, the change key will be "changed," so every once in a while, depending on your desire of accuracy, you may need to resync everything, unless you find a better way to do it (if so please share).
1 2 3 |
$Exchange = new Exchange('phpuser@mydomain.com', 'mypassword'); $entries = $Exchange->calendarList(600000, 0, True, 'Beginning'); |
Here's the classes:
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 |
<?php /**************************************** * @author @tonyshowoff * @version 1 * @license MIT/X11 ****************************************/ $GLOBALS['_EXCHANGE'] = array( 'impersonate' => Null, /* Email address of account to impersonate */ 'curl' => Null, 'full_debug' => False, /* This will display all traffic as well */ 'curl_debug' => False /* Set cURL as verbose response */ ); $GLOBALS['_CONFIG'] = array( 'debug_mode' => True, 'exchange_cache' => '', /* Pathh which contains your Services.wsdl, etc */ ); class Exchange { public $lastError; private $client; private $impersonate; private $pass; private $user; private $wsdl; function __construct($user, $pass, $impersonate = Null) { $this->wsdl = $GLOBALS['_CONFIG']['exchange_cache'] . '/Services.wsdl'; $this->user = $user; $this->pass = $pass; $this->impersonate = $impersonate; $GLOBALS['_EXCHANGE']['impersonate'] = $impersonate; $GLOBALS['_EXCHANGE']['curl'] = Null; $this->client = new ExchangeNTLMSoapClient($this->wsdl); $this->client->user = $this->user; $this->client->password = $this->pass; } function calendarEntry($id) { $this->setup(); $req_xml = new stdClass(); $req_xml->ItemShape = new stdClass(); $req_xml->ItemShape->BaseShape = 'AllProperties'; $req_xml->ItemIds = new stdClass(); $req_xml->ItemIds->ItemId = new stdClass(); $req_xml->ItemIds->ItemId->Id = $id; try { $response = $this->client->GetItem($req_xml); } catch (Exception $e) { echo 'calendarEntry() exception on ID "' . $id . '": ', $e->getMessage(), "\n"; $this->teardown(); return False; } $this->teardown(); if ($response->ResponseMessages->GetItemResponseMessage->ResponseCode == 'NoError') { return $response->ResponseMessages->GetItemResponseMessage->Items->CalendarItem; } else { return False; } } function calendarEventAdd($subject, $start, $end, $location, $body = '', $type = 'HTML', $isallday = False) { /* $start and $end must be in ISO date format or rather 'c' in PHP date format This function returns an array of item details upon success and False upon failure. */ $this->setup(); $event_xml = new stdClass(); $event_xml->SendMeetingInvitations = 'SendToNone'; $event_xml->SavedItemFolderId = new stdClass(); $event_xml->SavedItemFolderId->DistinguishedFolderId = new stdClass(); $event_xml->SavedItemFolderId->DistinguishedFolderId->Id = 'calendar'; $event_xml->Items = new stdClass(); $event_xml->Items->CalendarItem = new stdClass(); $event_xml->Items->CalendarItem->Subject = $subject; $event_xml->Items->CalendarItem->Start = $start; $event_xml->Items->CalendarItem->End = $end; $event_xml->Items->CalendarItem->Body = new stdClass(); $event_xml->Items->CalendarItem->Body->BodyType = $type; $event_xml->Items->CalendarItem->Body->_ = $body; $event_xml->Items->CalendarItem->IsAllDayEvent = $isallday; $event_xml->Items->CalendarItem->LegacyFreeBusyStatus = 'Busy'; // Legacy only (never use) $event_xml->Items->CalendarItem->Location = $location; try { $response = $this->client->CreateItem($event_xml); } catch (Exception $e) { echo 'calendarEventAdd() exception: ', $e->getMessage(), "\n"; $this->teardown(); return False; } $this->teardown(); if ($response->ResponseMessages->CreateItemResponseMessage->ResponseCode == 'NoError') { return ['ItemId' => $response->ResponseMessages->CreateItemResponseMessage->Items->CalendarItem->ItemId->Id, 'ChangeKey' => $response->ResponseMessages->CreateItemResponseMessage->Items->CalendarItem->ItemId->ChangeKey]; } else { $this->lastError = $response->ResponseMessages->CreateItemResponseMessage->ResponseCode; log_out_error('exchange', "Calendar:\r\nError: " . print_r($this->lastError, 1) . "\r\n\r\nInitial data:\r\n" . print_r($event_xml, 1)); return False; } } /** * Sets up strream handling. Internally used. * * @access private * @return void */ private function setup() { stream_wrapper_unregister('http'); stream_wrapper_register('http', 'ExchangeNTLMStream') or die('Failed to register protocol: 8c251e34-77e6-41ce-88da-4dbc90bc8a60'); } /** * Tears down stream handling. Internally used. * * @access private * @return void */ function teardown() { stream_wrapper_restore('http'); } function calendarEventDelete($item_id, $type = 'HardDelete') { $this->setup(); $DeleteItem = new stdClass(); $DeleteItem->DeleteType = $type; $DeleteItem->SendMeetingCancellations = 'SendToNone'; $DeleteItem->ItemIds = new stdClass(); $DeleteItem->ItemIds->ItemId = new stdClass(); $DeleteItem->ItemIds->ItemId->Id = $item_id; try { $response = $this->client->DeleteItem($DeleteItem); } catch (Exception $e) { echo 'calendarEventDelete() exception on ID "' . $item_id . '": ', $e->getMessage(), "\n"; $this->teardown(); return False; } $this->teardown(); if ($response->ResponseMessages->DeleteItemResponseMessage->ResponseCode == 'NoError') { return True; } else { $this->lastError = $response->ResponseMessages->DeleteItemResponseMessage->ResponseCode; log_out_error('exchange', "Calendar:\r\nError: " . print_r($this->lastError, 1) . "\r\n\r\nInitial data:\r\n" . print_r($DeleteItem, 1)); return False; } } function calendarList($limit = 10, $offset = 0, $full = False, $point = 'Beginning') { $this->setup(); $FindItem = new stdClass(); $FindItem->Traversal = 'Shallow'; $FindItem->ItemShape = new stdClass(); if (True == $full) { $FindItem->ItemShape->BaseShape = 'AllProperties'; } else { $FindItem->ItemShape->BaseShape = 'IdOnly'; $FindItem->ItemShape->AdditionalProperties = new SoapVar( '<ns1:AdditionalProperties> <ns1:FieldURI FieldURI="calendar:CalendarItemType"/> <ns1:FieldURI FieldURI="calendar:IsMeeting"/> <ns1:FieldURI FieldURI="calendar:IsRecurring"/> <ns1:FieldURI FieldURI="calendar:Organizer"/> <ns1:FieldURI FieldURI="calendar:Start"/> <ns1:FieldURI FieldURI="calendar:End"/> <ns1:FieldURI FieldURI="calendar:Duration"/> <ns1:FieldURI FieldURI="calendar:IsAllDayEvent"/> <ns1:FieldURI FieldURI="calendar:Location"/> <ns1:FieldURI FieldURI="calendar:MeetingRequestWasSent"/> <ns1:FieldURI FieldURI="calendar:MyResponseType"/> <ns1:FieldURI FieldURI="calendar:AppointmentSequenceNumber"/> <ns1:FieldURI FieldURI="calendar:AppointmentState"/> <ns1:FieldURI FieldURI="item:Categories"/> <ns1:FieldURI FieldURI="item:HasAttachments"/> <ns1:FieldURI FieldURI="item:ReminderDueBy"/> <ns1:FieldURI FieldURI="item:ReminderIsSet"/> <ns1:FieldURI FieldURI="item:ReminderMinutesBeforeStart"/> <ns1:FieldURI FieldURI="item:Subject"/> <ns1:FieldURI FieldURI="item:ItemClass"/> <ns1:FieldURI FieldURI="item:Sensitivity"/> <ns1:FieldURI FieldURI="item:DateTimeReceived"/> <ns1:FieldURI FieldURI="item:Importance"/> <ns1:FieldURI FieldURI="item:IsDraft"/> <ns1:FieldURI FieldURI="item:DateTimeCreated"/> </ns1:AdditionalProperties>', XSD_ANYXML); } $FindItem->IndexedPageItemView = new stdClass(); $FindItem->IndexedPageItemView->MaxEntriesReturned = $limit; $FindItem->IndexedPageItemView->Offset = $offset; $FindItem->IndexedPageItemView->BasePoint = $point; $FindItem->ParentFolderIds = new stdClass(); $FindItem->ParentFolderIds->DistinguishedFolderId = new stdClass(); $FindItem->ParentFolderIds->DistinguishedFolderId->Id = 'calendar'; try { $response = $this->client->FindItem($FindItem); } catch (Exception $e) { return False; } if ($response->ResponseMessages->FindItemResponseMessage->ResponseCode != 'NoError') { $this->lastError = $response->ResponseMessages->FindItemResponseMessage->ResponseCode; return false; } if (empty($response->ResponseMessages->FindItemResponseMessage->RootFolder->Items->CalendarItem)) { $items = False; } else { $items = $response->ResponseMessages->FindItemResponseMessage->RootFolder->Items->CalendarItem; if (!is_array($items)) { $items = $response->ResponseMessages->FindItemResponseMessage->RootFolder->Items; } } $this->teardown(); return $items; } function calendar_event_update($item_id, $changekey, $subject, $start, $end, $location, $body = '', $type = 'HTML', $isallday = False) { /* $start and $end must be in ISO date format or rather 'c' in PHP date format This function returns an array of item details upon success and False upon failure. */ $this->setup(); $update_xml = new stdClass(); $update_xml->SendMeetingInvitationsOrCancellations = 'SendToNone'; $update_xml->MessageDisposition = 'SaveOnly'; $update_xml->ConflictResolution = 'AlwaysOverwrite'; $update_xml->ItemChanges = new stdClass(); $update_xml->ItemChanges->ItemChange = new stdClass(); $update_xml->ItemChanges->ItemChange->ItemId = new stdClass(); $update_xml->ItemChanges->ItemChange->ItemId->Id = $item_id; $update_xml->ItemChanges->ItemChange->ItemId->ChangeKey = $changekey; $update_xml->ItemChanges->ItemChange->Updates = new stdClass(); $update_xml->ItemChanges->ItemChange->Updates = new SoapVar( '<ns1:Updates> <ns1:SetItemField> <ns1:FieldURI FieldURI="item:Subject" /> <ns1:CalendarItem> <ns1:Subject>' . htmlspecialchars($subject) . '</ns1:Subject> </ns1:CalendarItem> </ns1:SetItemField> <ns1:SetItemField> <ns1:FieldURI FieldURI="calendar:Location" /> <ns1:CalendarItem> <ns1:Location>' . htmlspecialchars($location) . '</ns1:Location> </ns1:CalendarItem> </ns1:SetItemField> <ns1:SetItemField> <ns1:FieldURI FieldURI="calendar:Start" /> <ns1:CalendarItem> <ns1:Start>' . $start . '</ns1:Start> </ns1:CalendarItem> </ns1:SetItemField> <ns1:SetItemField> <ns1:FieldURI FieldURI="calendar:End" /> <ns1:CalendarItem> <ns1:End>' . $end . '</ns1:End> </ns1:CalendarItem> </ns1:SetItemField> <ns1:SetItemField> <ns1:FieldURI FieldURI="item:Body" /> <ns1:CalendarItem> <ns1:Body BodyType="HTML">' . htmlspecialchars($body) . '</ns1:Body> </ns1:CalendarItem> </ns1:SetItemField> </ns1:Updates>', XSD_ANYXML); try { $response = $this->client->UpdateItem($update_xml); } catch (Exception $e) { echo 'calendar_event_update() exception on ID "' . $item_id . '": ', $e->getMessage(), "\n"; $this->teardown(); return False; } $this->teardown(); if ($response->ResponseMessages->UpdateItemResponseMessage->ResponseCode == 'NoError') { return ['changekey' => $response->ResponseMessages->UpdateItemResponseMessage->Items->CalendarItem->ItemId->ChangeKey]; } else { $this->lastError = $response->ResponseMessages->UpdateItemResponseMessage->ResponseCode; log_out_error('exchange', "Calendar:\r\nError: " . print_r($this->lastError, 1) . "\r\n\r\nInitial data:\r\n" . print_r($update_xml, 1)); return False; } } function close() { curl_close($GLOBALS['_EXCHANGE']['curl']); $GLOBALS['_EXCHANGE']['impersonate'] = Null; $GLOBALS['_EXCHANGE']['curl'] = Null; if ($GLOBALS['_CONFIG']['debug_mode'] == True) { echo " [D] Close Exchange connection.\n"; } } /** * Deletes a message in the mailbox of the current user. * * @access public * * @param ItemId $ItemId (such as one returned by get_messages) * @param string $deletetype . (default: "HardDelete") * * @return bool $success (true: message was deleted, false: message failed to delete) */ function delete_message($ItemId, $deletetype = "HardDelete") { $this->setup(); $DeleteItem->DeleteType = $deletetype; $DeleteItem->ItemIds->ItemId = $ItemId; $response = $this->client->DeleteItem($DeleteItem); $this->teardown(); if ($response->ResponseMessages->DeleteItemResponseMessage->ResponseCode == "NoError") { return true; } else { $this->lastError = $response->ResponseMessages->DeleteItemResponseMessage->ResponseCode; return false; } } function getAttachment($item_id, $attachment_id) { // Doesn't seem to work. $this->setup(); $req_xml = new stdClass(); $req_xml->ItemShape = new stdClass(); $req_xml->ItemShape->BaseShape = 'AllProperties'; $req_xml->ItemIds = new stdClass(); $req_xml->ItemIds->ItemId = new stdClass(); $req_xml->ItemIds->ItemId->Id = $item_id; $req_xml->ItemIds->ItemId->AttachmentId = $attachment_id; try { $response = $this->client->GetAttachment($req_xml); } catch (Exception $e) { echo 'getAttachment() exception on ID "' . $item_id . '": ', $e->getMessage(), "\n"; $this->teardown(); return False; } $this->teardown(); if ($response->ResponseMessages->GetItemResponseMessage->ResponseCode == 'NoError') { return $response->ResponseMessages->GetItemResponseMessage->Items->Attachment; } else { return False; } } function getInbox($limit = 10, $offset = 0) { $this->setup(); $FindItem = new stdClass(); $FindItem->Traversal = 'Shallow'; $FindItem->ItemShape = new stdClass(); $FindItem->ItemShape->BaseShape = 'IdOnly'; $FindItem->ItemShape->AdditionalProperties = new SoapVar( '<ns1:AdditionalProperties> <ns1:FieldURI FieldURI="item:Subject"/> </ns1:AdditionalProperties>', XSD_ANYXML); $FindItem->IndexedPageItemView = new stdClass(); $FindItem->IndexedPageItemView->MaxEntriesReturned = $limit; $FindItem->IndexedPageItemView->Offset = $offset; $FindItem->IndexedPageItemView->BasePoint = 'Beginning'; $FindItem->ParentFolderIds = new stdClass(); $FindItem->ParentFolderIds->DistinguishedFolderId = new stdClass(); $FindItem->ParentFolderIds->DistinguishedFolderId->Id = 'inbox'; try { $response = $this->client->FindItem($FindItem); } catch (Exception $e) { return False; } if ($response->ResponseMessages->FindItemResponseMessage->ResponseCode != 'NoError') { $this->lastError = $response->ResponseMessages->FindItemResponseMessage->ResponseCode; return false; } $items = $response->ResponseMessages->FindItemResponseMessage->RootFolder->Items->Message; $this->teardown(); return $items; } function itemExists($id) { $this->setup(); $req_xml = new stdClass(); $req_xml->ItemShape = new stdClass(); $req_xml->ItemShape->BaseShape = 'IdOnly'; $req_xml->ItemIds = new stdClass(); $req_xml->ItemIds->ItemId = new stdClass(); $req_xml->ItemIds->ItemId->Id = $id; try { $response = $this->client->GetItem($req_xml); } catch (Exception $e) { echo 'itemExists() exception on ID "' . $id . '": ', $e->getMessage(), "\n"; return False; } $this->teardown(); if ($response->ResponseMessages->GetItemResponseMessage->ResponseCode == 'NoError') { return True; } else { return False; } } function sendMessage($to, $subject, $content, $type = 'HTML', $save = False, $read = True) { /* This sends a message through Exchange as the user that's logged in via init. $to - The email address we're sending to, can be internal or external $subject - The subject $content - The content, style dependent on $type $type - Default is 'Text', but 'HTML' should be used for HTML emails $save - Default F, T/F save to user's sent folder? $read - Default F, T/F mark as read. @BUG: Doesn't seem to work, possibly missing another field? */ $this->setup(); $msg_xml = new stdClass(); if ($save == True) { $msg_xml->MessageDisposition = 'SendOnly'; $msg_xml->SavedItemFolderId = new stdClass(); $msg_xml->SavedItemFolderId->DistinguishedFolderId = new stdClass(); $msg_xml->SavedItemFolderId->DistinguishedFolderId->Id = 'sentitems'; } else { $msg_xml->MessageDisposition = 'SendOnly'; } $msg_xml->Items = new stdClass(); $msg_xml->Items->Message = new stdClass(); $msg_xml->Items->Message->ItemClass = 'IPM.Note'; $msg_xml->Items->Message->Subject = $subject; $msg_xml->Items->Message->Body = new stdClass(); $msg_xml->Items->Message->Body->BodyType = $type; $msg_xml->Items->Message->Body->_ = $content; $msg_xml->Items->Message->ToRecipients = new stdClass(); $msg_xml->Items->Message->ToRecipients->Mailbox = new stdClass(); $msg_xml->Items->Message->ToRecipients->Mailbox->EmailAddress = $to; if ($read == True) { $msg_xml->Items->Message->IsRead = 'true'; } try { $response = $this->client->CreateItem($msg_xml); } catch (Exception $e) { return False; } $this->teardown(); if ($response->ResponseMessages->CreateItemResponseMessage->ResponseCode == 'NoError') { return True; } else { $this->lastError = $response->ResponseMessages->CreateItemResponseMessage->ResponseCode; log_out_error('exchange', "sendMessage:\r\nError: " . print_r($this->lastError, 1) . "\r\n\r\nInitial data:\r\n" . print_r($msg_xml, 1)); return False; } } } class ExchangeNTLMSoapClient extends NTLMSoapClient { public $password = ''; public $user = ''; } class ExchangeNTLMStream extends NTLMStream { //protected $user = ''; //protected $password = ''; } class ImpersonationHeader { var $ConnectingSID; /*ExchangeImpersonationType and the ConnectingSIDType*/ function __construct($email) { $this->ConnectingSID = new stdClass(); $this->ConnectingSID->PrincipalName = $email; /* $this->ConnectingSID = new stdClass(); $this->ConnectingSID->PrimarySmtpAddress = $email; */ } } class NTLMSoapClient extends SoapClient { function __doRequest($request, $location, $action, $version, $one_way = 0) { if ($GLOBALS['_EXCHANGE']['full_debug'] == True) { echo 'Request: ' . $request . "\n<br />"; echo 'Location: ' . $location . "\n<br />"; echo 'Action: ' . $action . "\n<br />"; echo 'Version: ' . $version . "\n<br />"; echo 'One-way: ' . $one_way . "\n<br />"; echo 'Request: ' . print_r($request, 1); } $headers = [ 'Method: POST', 'Connection: Keep-Alive', 'User-Agent: PHP-SOAP-CURL', 'Content-Type: text/xml; charset=utf-8', 'SOAPAction: "' . $action . '"', ]; // @TODO: Somehow we need to pass the $impersonate var // from the exchange class to here, probably just in init // or something, but we'll do this hack for now. if ($GLOBALS['_EXCHANGE']['impersonate'] != Null) { // @TODO / @HACK: Ideally we'd want to just use the SOAP system // built into PHP to do this, but it has a flaw which // will not let you properly impersonate someone. // Until it's fixed (been 2 years as of now) we do this. $xml = '<SOAP-ENV:Header>'; $xml .= '<ns1:ExchangeImpersonation>'; $xml .= '<ns1:ConnectingSID>'; $xml .= '<ns1:PrimarySmtpAddress>' . $GLOBALS['_EXCHANGE']['impersonate'] . '</ns1:PrimarySmtpAddress>'; $xml .= '</ns1:ConnectingSID>'; $xml .= '</ns1:ExchangeImpersonation>'; $xml .= '</SOAP-ENV:Header>'; $request = str_replace('><SOAP-ENV:Body', '>' . $xml . '<SOAP-ENV:Body', $request); } $this->__last_request_headers = $headers; $GLOBALS['_EXCHANGE']['curl'] = curl_init($location); curl_setopt($GLOBALS['_EXCHANGE']['curl'], CURLOPT_RETURNTRANSFER, true); curl_setopt($GLOBALS['_EXCHANGE']['curl'], CURLOPT_HTTPHEADER, $headers); curl_setopt($GLOBALS['_EXCHANGE']['curl'], CURLOPT_POST, true); curl_setopt($GLOBALS['_EXCHANGE']['curl'], CURLOPT_POSTFIELDS, $request); curl_setopt($GLOBALS['_EXCHANGE']['curl'], CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($GLOBALS['_EXCHANGE']['curl'], CURLOPT_HTTPAUTH, CURLAUTH_NTLM); curl_setopt($GLOBALS['_EXCHANGE']['curl'], CURLOPT_USERPWD, $this->user . ':' . $this->password); curl_setopt($GLOBALS['_EXCHANGE']['curl'], CURLOPT_SSL_VERIFYPEER, false); curl_setopt($GLOBALS['_EXCHANGE']['curl'], CURLOPT_SSL_VERIFYHOST, false); curl_setopt($GLOBALS['_EXCHANGE']['curl'], CURLOPT_VERBOSE, $GLOBALS['_EXCHANGE']['curl_debug']); $response = curl_exec($GLOBALS['_EXCHANGE']['curl']); if ($GLOBALS['_EXCHANGE']['full_debug'] == True) { echo 'Response: ' . print_r($response, 1); } // @TODO: If there's an error we need to log it, but // this may need to be done at all the functions // which parse this, rather than here. Though we // should still log all CURL errors. return $response; } function __getLastRequestHeaders() { return implode("n", $this->__last_request_headers) . "n"; } } class NTLMStream { private $buffer; private $mode; private $opened_path; private $options; private $path; private $pos; public function stream_close() { echo "[NTLMStream::stream_close] n"; curl_close($this->ch); } public function stream_eof() { echo "[NTLMStream::stream_eof] "; if ($this->pos > strlen($this->buffer)) { echo "true n"; return true; } echo "false n"; return false; } public function stream_flush() { echo "[NTLMStream::stream_flush] n"; $this->buffer = null; $this->pos = null; } public function stream_open($path, $mode, $options, $opened_path) { echo "[NTLMStream::stream_open] $path , mode=$mode n"; $this->path = $path; $this->mode = $mode; $this->options = $options; $this->opened_path = $opened_path; $this->createBuffer($path); return true; } private function createBuffer($path) { if ($this->buffer) { return; } echo "[NTLMStream::createBuffer] create buffer from : $pathn"; $this->ch = curl_init($path); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); curl_setopt($this->ch, CURLOPT_USERPWD, $this->user . ':' . $this->password); echo $this->buffer = curl_exec($this->ch); echo "[NTLMStream::createBuffer] buffer size : " . strlen($this->buffer) . "bytesn"; $this->pos = 0; } /* return the position of the current read pointer */ public function stream_read($count) { echo "[NTLMStream::stream_read] $count n"; if (strlen($this->buffer) == 0) { return false; } $read = substr($this->buffer, $this->pos, $count); $this->pos += $count; return $read; } public function stream_stat() { echo "[NTLMStream::stream_stat] n"; $this->createBuffer($this->path); $stat = [ 'size' => strlen($this->buffer), ]; return $stat; } public function stream_tell() { echo "[NTLMStream::stream_tell] n"; return $this->pos; } public function stream_write($data) { echo "[NTLMStream::stream_write] n"; if (strlen($this->buffer) == 0) { return false; } return true; } /* Create the buffer by requesting the url through cURL */ public function url_stat($path, $flags) { echo "[NTLMStream::url_stat] n"; $this->createBuffer($path); $stat = [ 'size' => strlen($this->buffer), ]; return $stat; } } class UpdateResponse { } ?> |