/
ActiveSync.php
1156 lines (1045 loc) · 41.9 KB
/
ActiveSync.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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Horde_ActiveSync::
*
* @license http://www.horde.org/licenses/gpl GPLv2
* NOTE: According to sec. 8 of the GENERAL PUBLIC LICENSE (GPL),
* Version 2, the distribution of the Horde_ActiveSync module in or
* to the United States of America is excluded from the scope of this
* license.
* @copyright 2009-2014 Horde LLC (http://www.horde.org)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package ActiveSync
*/
/**
* Horde_ActiveSync:: The Horde ActiveSync server. Entry point for performing
* all ActiveSync operations.
*
* @license http://www.horde.org/licenses/gpl GPLv2
* NOTE: According to sec. 8 of the GENERAL PUBLIC LICENSE (GPL),
* Version 2, the distribution of the Horde_ActiveSync module in or
* to the United States of America is excluded from the scope of this
* license.
* @copyright 2009-2014 Horde LLC (http://www.horde.org)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package ActiveSync
*
* @property-read Horde_ActiveSync_Wbxml_Encoder $encoder The Wbxml encoder.
* @property-read Horde_ActiveSync_Wbxml_Decoder $decoder The Wbxml decoder.
* @property-read Horde_ActiveSync_State_Base $state The state object.
* @property-read Horde_Controller_Reqeust_Http $request The HTTP request object.
* @property-read Horde_ActiveSync_Driver_Base $driver The backend driver object.
* @property-read boolean|string $provisioning Provisioning support: True, False, or 'loose'
* @property-read boolean $multipart Indicate this is a multipart request.
* @property-read string $certPath Local path to the certificate bundle.
* @property-read Horde_ActiveSync_Device $device The current device object.
* @property-read Horde_Log_Logger $logger The logger object.
*/
class Horde_ActiveSync
{
/* Conflict resolution */
const CONFLICT_OVERWRITE_SERVER = 0;
const CONFLICT_OVERWRITE_PIM = 1;
/* TRUNCATION Constants */
const TRUNCATION_ALL = 0;
const TRUNCATION_1 = 1;
const TRUNCATION_2 = 2;
const TRUNCATION_3 = 3;
const TRUNCATION_4 = 4;
const TRUNCATION_5 = 5;
const TRUNCATION_6 = 6;
const TRUNCATION_7 = 7;
const TRUNCATION_8 = 8;
const TRUNCATION_NONE = 9;
/* FOLDERHIERARCHY */
const FOLDERHIERARCHY_FOLDERS = 'FolderHierarchy:Folders';
const FOLDERHIERARCHY_FOLDER = 'FolderHierarchy:Folder';
const FOLDERHIERARCHY_DISPLAYNAME = 'FolderHierarchy:DisplayName';
const FOLDERHIERARCHY_SERVERENTRYID = 'FolderHierarchy:ServerEntryId';
const FOLDERHIERARCHY_PARENTID = 'FolderHierarchy:ParentId';
const FOLDERHIERARCHY_TYPE = 'FolderHierarchy:Type';
const FOLDERHIERARCHY_RESPONSE = 'FolderHierarchy:Response';
const FOLDERHIERARCHY_STATUS = 'FolderHierarchy:Status';
const FOLDERHIERARCHY_CONTENTCLASS = 'FolderHierarchy:ContentClass';
const FOLDERHIERARCHY_CHANGES = 'FolderHierarchy:Changes';
const FOLDERHIERARCHY_SYNCKEY = 'FolderHierarchy:SyncKey';
const FOLDERHIERARCHY_FOLDERSYNC = 'FolderHierarchy:FolderSync';
const FOLDERHIERARCHY_COUNT = 'FolderHierarchy:Count';
const FOLDERHIERARCHY_VERSION = 'FolderHierarchy:Version';
/* SYNC */
const SYNC_SYNCHRONIZE = 'Synchronize';
const SYNC_REPLIES = 'Replies';
const SYNC_ADD = 'Add';
const SYNC_MODIFY = 'Modify';
const SYNC_REMOVE = 'Remove';
const SYNC_FETCH = 'Fetch';
const SYNC_SYNCKEY = 'SyncKey';
const SYNC_CLIENTENTRYID = 'ClientEntryId';
const SYNC_SERVERENTRYID = 'ServerEntryId';
const SYNC_STATUS = 'Status';
const SYNC_FOLDER = 'Folder';
const SYNC_FOLDERTYPE = 'FolderType';
const SYNC_VERSION = 'Version';
const SYNC_FOLDERID = 'FolderId';
const SYNC_GETCHANGES = 'GetChanges';
const SYNC_MOREAVAILABLE = 'MoreAvailable';
const SYNC_WINDOWSIZE = 'WindowSize';
const SYNC_COMMANDS = 'Commands';
const SYNC_OPTIONS = 'Options';
const SYNC_FILTERTYPE = 'FilterType';
const SYNC_TRUNCATION = 'Truncation';
const SYNC_RTFTRUNCATION = 'RtfTruncation';
const SYNC_CONFLICT = 'Conflict';
const SYNC_FOLDERS = 'Folders';
const SYNC_DATA = 'Data';
const SYNC_DELETESASMOVES = 'DeletesAsMoves';
const SYNC_NOTIFYGUID = 'NotifyGUID';
const SYNC_SUPPORTED = 'Supported';
const SYNC_SOFTDELETE = 'SoftDelete';
const SYNC_MIMESUPPORT = 'MIMESupport';
const SYNC_MIMETRUNCATION = 'MIMETruncation';
const SYNC_NEWMESSAGE = 'NewMessage';
const SYNC_PARTIAL = 'Partial';
const SYNC_WAIT = 'Wait';
const SYNC_LIMIT = 'Limit';
// 14
const SYNC_HEARTBEATINTERVAL = 'HeartbeatInterval';
const SYNC_CONVERSATIONMODE = 'ConversationMode';
const SYNC_MAXITEMS = 'MaxItems';
/* Document library */
const SYNC_DOCUMENTLIBRARY_LINKID = 'DocumentLibrary:LinkId';
const SYNC_DOCUMENTLIBRARY_DISPLAYNAME = 'DocumentLibrary:DisplayName';
const SYNC_DOCUMENTLIBRARY_ISFOLDER = 'DocumentLibrary:IsFolder';
const SYNC_DOCUMENTLIBRARY_CREATIONDATE = 'DocumentLibrary:CreationDate';
const SYNC_DOCUMENTLIBRARY_LASTMODIFIEDDATE = 'DocumentLibrary:LastModifiedDate';
const SYNC_DOCUMENTLIBRARY_ISHIDDEN = 'DocumentLibrary:IsHidden';
const SYNC_DOCUMENTLIBRARY_CONTENTLENGTH = 'DocumentLibrary:ContentLength';
const SYNC_DOCUMENTLIBRARY_CONTENTTYPE = 'DocumentLibrary:ContentType';
/* AIRSYNCBASE */
const AIRSYNCBASE_BODYPREFERENCE = 'AirSyncBase:BodyPreference';
const AIRSYNCBASE_TYPE = 'AirSyncBase:Type';
const AIRSYNCBASE_TRUNCATIONSIZE = 'AirSyncBase:TruncationSize';
const AIRSYNCBASE_ALLORNONE = 'AirSyncBase:AllOrNone';
const AIRSYNCBASE_BODY = 'AirSyncBase:Body';
const AIRSYNCBASE_DATA = 'AirSyncBase:Data';
const AIRSYNCBASE_ESTIMATEDDATASIZE = 'AirSyncBase:EstimatedDataSize';
const AIRSYNCBASE_TRUNCATED = 'AirSyncBase:Truncated';
const AIRSYNCBASE_ATTACHMENTS = 'AirSyncBase:Attachments';
const AIRSYNCBASE_ATTACHMENT = 'AirSyncBase:Attachment';
const AIRSYNCBASE_DISPLAYNAME = 'AirSyncBase:DisplayName';
const AIRSYNCBASE_FILEREFERENCE = 'AirSyncBase:FileReference';
const AIRSYNCBASE_METHOD = 'AirSyncBase:Method';
const AIRSYNCBASE_CONTENTID = 'AirSyncBase:ContentId';
const AIRSYNCBASE_CONTENTLOCATION = 'AirSyncBase:ContentLocation';
const AIRSYNCBASE_ISINLINE = 'AirSyncBase:IsInline';
const AIRSYNCBASE_NATIVEBODYTYPE = 'AirSyncBase:NativeBodyType';
const AIRSYNCBASE_CONTENTTYPE = 'AirSyncBase:ContentType';
// 14.0
const AIRSYNCBASE_PREVIEW = 'AirSyncBase:Preview';
// 14.1
const AIRSYNCBASE_BODYPARTPREFERENCE = 'AirSyncBase:BodyPartPreference';
const AIRSYNCBASE_BODYPART = 'AirSyncBase:BodyPart';
const AIRSYNCBASE_STATUS = 'AirSyncBase:Status';
/* Body type prefs */
const BODYPREF_TYPE_PLAIN = 1;
const BODYPREF_TYPE_HTML = 2;
const BODYPREF_TYPE_RTF = 3;
const BODYPREF_TYPE_MIME = 4;
/* PROVISION */
const PROVISION_PROVISION = 'Provision:Provision';
const PROVISION_POLICIES = 'Provision:Policies';
const PROVISION_POLICY = 'Provision:Policy';
const PROVISION_POLICYTYPE = 'Provision:PolicyType';
const PROVISION_POLICYKEY = 'Provision:PolicyKey';
const PROVISION_DATA = 'Provision:Data';
const PROVISION_STATUS = 'Provision:Status';
const PROVISION_REMOTEWIPE = 'Provision:RemoteWipe';
const PROVISION_EASPROVISIONDOC = 'Provision:EASProvisionDoc';
/* Policy types */
const POLICYTYPE_XML = 'MS-WAP-Provisioning-XML';
const POLICYTYPE_WBXML = 'MS-EAS-Provisioning-WBXML';
/* Flags */
// @TODO: H6 Change this to CHANGE_TYPE_NEW
const FLAG_NEWMESSAGE = 'NewMessage';
/* Folder types */
const FOLDER_TYPE_OTHER = 1;
const FOLDER_TYPE_INBOX = 2;
const FOLDER_TYPE_DRAFTS = 3;
const FOLDER_TYPE_WASTEBASKET = 4;
const FOLDER_TYPE_SENTMAIL = 5;
const FOLDER_TYPE_OUTBOX = 6;
const FOLDER_TYPE_TASK = 7;
const FOLDER_TYPE_APPOINTMENT = 8;
const FOLDER_TYPE_CONTACT = 9;
const FOLDER_TYPE_NOTE = 10;
const FOLDER_TYPE_JOURNAL = 11;
const FOLDER_TYPE_USER_MAIL = 12;
const FOLDER_TYPE_USER_APPOINTMENT = 13;
const FOLDER_TYPE_USER_CONTACT = 14;
const FOLDER_TYPE_USER_TASK = 15;
const FOLDER_TYPE_USER_JOURNAL = 16;
const FOLDER_TYPE_USER_NOTE = 17;
const FOLDER_TYPE_UNKNOWN = 18;
const FOLDER_TYPE_RECIPIENT_CACHE = 19;
// @TODO, remove const definition in H6, not used anymore.
const FOLDER_TYPE_DUMMY = 999999;
/* Origin of changes **/
const CHANGE_ORIGIN_PIM = 0;
const CHANGE_ORIGIN_SERVER = 1;
const CHANGE_ORIGIN_NA = 3;
/* Remote wipe **/
const RWSTATUS_NA = 0;
const RWSTATUS_OK = 1;
const RWSTATUS_PENDING = 2;
const RWSTATUS_WIPED = 3;
/* GAL **/
const GAL_DISPLAYNAME = 'GAL:DisplayName';
const GAL_PHONE = 'GAL:Phone';
const GAL_OFFICE = 'GAL:Office';
const GAL_TITLE = 'GAL:Title';
const GAL_COMPANY = 'GAL:Company';
const GAL_ALIAS = 'GAL:Alias';
const GAL_FIRSTNAME = 'GAL:FirstName';
const GAL_LASTNAME = 'GAL:LastName';
const GAL_HOMEPHONE = 'GAL:HomePhone';
const GAL_MOBILEPHONE = 'GAL:MobilePhone';
const GAL_EMAILADDRESS = 'GAL:EmailAddress';
// 14.1
const GAL_PICTURE = 'GAL:Picture';
const GAL_STATUS = 'GAL:Status';
const GAL_DATA = 'GAL:Data';
/* Request Type */
const REQUEST_TYPE_SYNC = 'sync';
const REQUEST_TYPE_FOLDERSYNC = 'foldersync';
/* Change Type */
const CHANGE_TYPE_CHANGE = 'change';
const CHANGE_TYPE_DELETE = 'delete';
const CHANGE_TYPE_FLAGS = 'flags';
const CHANGE_TYPE_MOVE = 'move';
const CHANGE_TYPE_FOLDERSYNC = 'foldersync';
const CHANGE_TYPE_SOFTDELETE = 'softdelete';
/* Internal flags to indicate change is a change in reply/forward state */
const CHANGE_REPLY_STATE = '@--reply--@';
const CHANGE_REPLYALL_STATE = '@--replyall--@';
const CHANGE_FORWARD_STATE = '@--forward--@';
/* RM */
const RM_SUPPORT = 'RightsManagement:RightsManagementSupport';
const RM_TEMPLATEID = 'RightsManagement:TemplateId';
/* Collection Classes */
const CLASS_EMAIL = 'Email';
const CLASS_CONTACTS = 'Contacts';
const CLASS_CALENDAR = 'Calendar';
const CLASS_TASKS = 'Tasks';
const CLASS_NOTES = 'Notes';
const CLASS_SMS = 'SMS';
/* Filtertype constants */
const FILTERTYPE_ALL = 0;
const FILTERTYPE_1DAY = 1;
const FILTERTYPE_3DAYS = 2;
const FILTERTYPE_1WEEK = 3;
const FILTERTYPE_2WEEKS = 4;
const FILTERTYPE_1MONTH = 5;
const FILTERTYPE_3MONTHS = 6;
const FILTERTYPE_6MONTHS = 7;
const FILTERTYPE_INCOMPLETETASKS = 8;
const PROVISIONING_FORCE = true;
const PROVISIONING_LOOSE = 'loose';
const PROVISIONING_NONE = false;
const FOLDER_ROOT = 0;
const VERSION_TWOFIVE = '2.5';
const VERSION_TWELVE = '12.0';
const VERSION_TWELVEONE = '12.1';
const VERSION_FOURTEEN = '14.0';
const VERSION_FOURTEENONE = '14.1';
const MIME_SUPPORT_NONE = 0;
const MIME_SUPPORT_SMIME = 1;
const MIME_SUPPORT_ALL = 2;
const IMAP_FLAG_REPLY = 'reply';
const IMAP_FLAG_FORWARD = 'forward';
/* Result Type */
const RESOLVE_RESULT_GAL = 1;
const RESOLVE_RESULT_ADDRESSBOOK = 2;
/* Auth failure reasons */
const AUTH_REASON_USER_DENIED = 'user';
const AUTH_REASON_DEVICE_DENIED = 'device';
const LIBRARY_VERSION = '2.x.y-git';
/**
* Logger
*
* @var Horde_ActiveSync_Interface_LoggerFactory
*/
protected $_loggerFactory;
/**
* The logger for this class.
*
* @var Horde_Log_Logger
*/
static protected $_logger;
/**
* Provisioning support
*
* @var string
*/
protected $_provisioning;
/**
* Highest version to support.
*
* @var float
*/
protected $_maxVersion = self::VERSION_FOURTEENONE;
/**
* The actual version we are supporting.
*
* @var float
*/
static protected $_version;
/**
* Multipart support?
*
* @var boolean
*/
protected $_multipart = false;
/**
* Support gzip compression of certain data parts?
*
* @var boolean
*/
protected $_compression = false;
/**
* Local cache of Get variables/decoded base64 uri
*
* @var array
*/
protected $_get = array();
/**
* Path to root certificate bundle
*
* @var string
*/
protected $_certPath;
/**
*
* @var Horde_ActiveSync_Device
*/
static protected $_device;
/**
* Wbxml encoder
*
* @var Horde_ActiveSync_Wbxml_Encoder
*/
protected $_encoder;
/**
* Wbxml decoder
*
* @var Horde_ActiveSync_Wbxml_Decoder
*/
protected $_decoder;
/**
* The singleton collections handler.
*
* @var Horde_ActiveSync_Collections
*/
protected $_collectionsObj;
/**
* Global error flag.
*
* @var boolean
*/
protected $_globalError = false;
/**
* Process id (used in logging).
*
* @var integer
*/
protected $_procid;
/**
* Supported EAS versions.
*
* @var array
*/
static protected $_supportedVersions = array(
self::VERSION_TWOFIVE,
self::VERSION_TWELVE,
self::VERSION_TWELVEONE,
self::VERSION_FOURTEEN,
self::VERSION_FOURTEENONE
);
/**
* Factory method for creating Horde_ActiveSync_Message objects.
*
* @param string $message The message type.
* @since 2.4.0
*
* @return Horde_ActiveSync_Message_Base The concrete message object.
* @todo For H6, move to Horde_ActiveSync_Message_Base::factory()
*/
static public function messageFactory($message)
{
$class = 'Horde_ActiveSync_Message_' . $message;
if (!class_exists($class)) {
throw new InvalidArgumentException(sprintf('Class %s does not exist.', $class));
}
return new $class(array(
'logger' => self::$_logger,
'protocolversion' => self::$_version,
'device' => self::$_device));
}
/**
* Const'r
*
* @param Horde_ActiveSync_Driver_Base $driver The backend driver.
* @param Horde_ActiveSync_Wbxml_Decoder $decoder The Wbxml decoder.
* @param Horde_ActiveSync_Wbxml_Endcoder $encoder The Wbxml encoder.
* @param Horde_ActiveSync_State_Base $state The state driver.
* @param Horde_Controller_Request_Http $request The HTTP request object.
*
* @return Horde_ActiveSync The ActiveSync server object.
*/
public function __construct(
Horde_ActiveSync_Driver_Base $driver,
Horde_ActiveSync_Wbxml_Decoder $decoder,
Horde_ActiveSync_Wbxml_Encoder $encoder,
Horde_ActiveSync_State_Base $state,
Horde_Controller_Request_Http $request)
{
// The http request
$this->_request = $request;
// Backend driver
$this->_driver = $driver;
$this->_driver->setProtocolVersion($this->getProtocolVersion());
// Device state manager
$this->_state = $state;
// Wbxml handlers
$this->_encoder = $encoder;
$this->_decoder = $decoder;
$this->_procid = getmypid();
}
/**
* Return a collections singleton.
*
* @return Horde_ActiveSync_Collections
* @since 2.4.0
*/
public function getCollectionsObject()
{
if (empty($this->_collectionsObj)) {
$this->_collectionsObj = new Horde_ActiveSync_Collections($this->getSyncCache(), $this);
}
return $this->_collectionsObj;
}
/**
* Return a new, fully configured SyncCache.
*
* @return Horde_ActiveSync_SyncCache
* @since 2.4.0
*/
public function getSyncCache()
{
return new Horde_ActiveSync_SyncCache(
$this->_state,
self::$_device->id,
self::$_device->user,
self::$_logger
);
}
/**
* Return an Importer object.
*
* @return Horde_ActiveSync_Connector_Importer
* @since 2.4.0
*/
public function getImporter()
{
$importer = new Horde_ActiveSync_Connector_Importer($this);
$importer->setLogger(self::$_logger);
return $importer;
}
/**
* Authenticate to the backend.
*
* @return boolean True on successful authentication to the backend.
* @throws Horde_ActiveSync_Exception
*/
public function authenticate($username = '')
{
// Get credentials
$serverVars = $this->_request->getServerVars();
$user = $pass = '';
if (!empty($serverVars['PHP_AUTH_PW'])) {
$user = empty($username) ? $serverVars['PHP_AUTH_USER'] : $username;
$pass = $serverVars['PHP_AUTH_PW'];
} elseif (!empty($serverVars['HTTP_AUTHORIZATION']) || !empty($serverVars['Authorization'])) {
// Some clients use the non-standard 'Authorization' header.
$authorization = !empty($serverVars['HTTP_AUTHORIZATION'])
? $serverVars['HTTP_AUTHORIZATION']
: $serverVars['Authorization'];
$hash = base64_decode(str_replace('Basic ', '', $authorization));
if (strpos($hash, ':') !== false) {
list($user, $pass) = explode(':', $hash, 2);
}
$user = !empty($username) ? $username : $user;
} elseif (!empty($username)) {
// Might not need a password, could be using X509 cert.
$user = $username;
} else {
// No provided username or Authorization header.
self::$_logger->notice(sprintf(
'[%s] Client did not provide authentication data.',
$this->_procid)
);
return false;
}
$user = $this->_driver->getUsernameFromEmail($user);
$pos = strrpos($user, '\\');
if ($pos !== false) {
$domain = substr($user, 0, $pos);
$user = substr($user, $pos + 1);
} else {
$domain = null;
}
// Authenticate
if ($result = $this->_driver->authenticate($user, $pass, $domain)) {
if ($result === self::AUTH_REASON_USER_DENIED) {
$this->_globalError = Horde_ActiveSync_Status::SYNC_NOT_ALLOWED;
} elseif ($result === self::AUTH_REASON_DEVICE_DENIED) {
$this->_globalError = Horde_ActiveSync_Status::DEVICE_BLOCKED_FOR_USER;
} elseif ($result !== true) {
$this->_globalError = Horde_ActiveSync_Status::DENIED;
}
} else {
return false;
}
if (!$this->_driver->setup($user)) {
return false;
}
return true;
}
/**
* Allow to force the highest version to support.
*
* @param float $version The highest version
*/
public function setSupportedVersion($version)
{
$this->_maxVersion = $version;
}
/**
* Set the local path to the root certificate bundle.
*
* @param string $path The local path to the bundle.
*/
public function setRootCertificatePath($path)
{
$this->_certPath = $path;
}
/**
* Getter
*
* @param string $property The property to return.
*
* @return mixed The value of the requested property.
*/
public function __get($property)
{
switch ($property) {
case 'encoder':
case 'decoder':
case 'state':
case 'request':
case 'driver':
case 'provisioning':
case 'multipart':
case 'certPath':
$property = '_' . $property;
return $this->$property;
case 'logger':
return self::$_logger;
case 'device':
return self::$_device;
default:
throw new InvalidArgumentException(sprintf(
'The property %s does not exist',
$property)
);
}
}
/**
* Setter for the logger
*
* @param Horde_Log_Logger $logger The logger object.
*
* @return void
*/
public function setLogger(Horde_ActiveSync_Interface_LoggerFactory $logger)
{
$this->_loggerFactory = $logger;
}
protected function _setLogger(array $options)
{
if (!empty($this->_loggerFactory)) {
self::$_logger = $this->_loggerFactory->create($options);
$this->_encoder->setLogger(self::$_logger);
$this->_decoder->setLogger(self::$_logger);
$this->_driver->setLogger(self::$_logger);
$this->_state->setLogger(self::$_logger);
}
}
/**
* Setter for provisioning support
*
*/
public function setProvisioning($provision)
{
$this->_provisioning = $provision;
}
/**
* Send the headers indicating that provisioning is required.
*/
public function provisioningRequired()
{
$this->provisionHeader();
$this->activeSyncHeader();
$this->versionHeader();
$this->commandsHeader();
header('Cache-Control: private');
}
/**
* The heart of the server. Dispatch a request to the appropriate request
* handler.
*
* @param string $cmd The command we are requesting.
* @param string $devId The device id making the request. @deprecated
*
* @return string|boolean false if failed, true if succeeded and response
* content is wbxml, otherwise the
* content-type string to send in the response.
* @throws Horde_ActiveSync_Exception
* @throws Horde_ActiveSync_Exception_InvalidRequest
* @throws Horde_ActiveSync_PermissionDenied
*/
public function handleRequest($cmd, $devId)
{
$get = $this->getGetVars();
if (empty($cmd)) {
$cmd = $get['Cmd'];
}
if (empty($devId)) {
$devId = !empty($get['DeviceId']) ? strtoupper($get['DeviceId']) : null;
} else {
$devId = strtoupper($devId);
}
$this->_setLogger($get);
// @TODO: Remove is_callable check for H6.
// Callback to give the backend the option to limit EAS version based
// on user/device/etc...
if (is_callable(array($this->_driver, 'versionCallback'))) {
$this->_driver->versionCallback($this);
}
// Autodiscovery handles authentication on it's own.
if ($cmd == 'Autodiscover') {
$request = new Horde_ActiveSync_Request_Autodiscover($this, new Horde_ActiveSync_Device($this->_state));
if (!empty(self::$_logger)) {
$request->setLogger(self::$_logger);
}
$result = $request->handle($this->_request);
$this->_driver->clearAuthentication();
return $result;
}
if (!$this->authenticate(!empty($get['User']) ? $get['User'] : '')) {
$this->activeSyncHeader();
$this->versionHeader();
$this->commandsHeader();
throw new Horde_Exception_AuthenticationFailure();
}
// Set provisioning support now that we are authenticated.
$this->setProvisioning($this->_driver->getProvisioning());
self::$_logger->info(sprintf(
'[%s] %s request received for user %s',
$this->_procid,
strtoupper($cmd),
$this->_driver->getUser())
);
// These are all handled in the same class.
if ($cmd == 'FolderDelete' || $cmd == 'FolderUpdate') {
$cmd = 'FolderCreate';
}
// Device id is REQUIRED
if (empty($devId)) {
if ($cmd == 'Options') {
$this->_doOptionsRequest();
$this->_driver->clearAuthentication();
return true;
}
$this->_driver->clearAuthentication();
throw new Horde_ActiveSync_Exception_InvalidRequest('Device failed to send device id.');
}
// EAS Version
$version = $this->getProtocolVersion();
// Does device exist AND does the user have an account on the device?
if (!$this->_state->deviceExists($devId, $this->_driver->getUser())) {
// Device might exist, but with a new (additional) user account
if ($this->_state->deviceExists($devId)) {
self::$_device = $this->_state->loadDeviceInfo($devId);
} else {
self::$_device = new Horde_ActiveSync_Device($this->_state);
}
self::$_device->policykey = 0;
self::$_device->userAgent = $this->_request->getHeader('User-Agent');
self::$_device->deviceType = !empty($get['DeviceType']) ? $get['DeviceType'] : '';
self::$_device->rwstatus = self::RWSTATUS_NA;
self::$_device->user = $this->_driver->getUser();
self::$_device->id = $devId;
self::$_device->needsVersionUpdate($this->getSupportedVersions());
self::$_device->version = $version;
// @TODO: Remove is_callable check for H6.
// Combine this with the modifyDevice callback? Allow $device
// to be modified here?
if (is_callable(array($this->_driver, 'createDeviceCallback'))) {
$callback_ret = $this->_driver->createDeviceCallback(self::$_device);
if ($callback_ret !== true) {
$msg = sprintf(
'The device %s was disallowed for user %s per policy settings.',
self::$_device->id,
self::$_device->user);
self::$_logger->err($msg);
if ($version > self::VERSION_TWELVEONE) {
$this->_globalError = $callback_ret;
} else {
throw new Horde_ActiveSync_Exception($msg);
}
} else {
// Give the driver a chance to modify device properties.
if (is_callable(array($this->_driver, 'modifyDeviceCallback'))) {
self::$_device = $this->_driver->modifyDeviceCallback(self::$_device);
}
}
}
} else {
self::$_device = $this->_state->loadDeviceInfo($devId, $this->_driver->getUser());
// If the device state was removed from storage, we may lose the
// device properties, so try to repopulate what we can. userAgent
// is ALWAYS available, so if it's missing, the state is gone.
if (empty(self::$_device->userAgent)) {
self::$_device->userAgent = $this->_request->getHeader('User-Agent');
self::$_device->deviceType = !empty($get['DeviceType']) ? $get['DeviceType'] : '';
self::$_device->user = $this->_driver->getUser();
}
if (empty(self::$_device->version)) {
self::$_device->version = $version;
}
if (self::$_device->version < $this->_maxVersion &&
self::$_device->needsVersionUpdate($this->getSupportedVersions())) {
$needMsRp = true;
}
// Give the driver a chance to modify device properties.
if (is_callable(array($this->_driver, 'modifyDeviceCallback'))) {
self::$_device = $this->_driver->modifyDeviceCallback(self::$_device);
}
}
self::$_device->save();
if (is_callable(array($this->_driver, 'deviceCallback'))) {
$callback_ret = $this->_driver->deviceCallback(self::$_device);
if ($callback_ret !== true) {
$msg = sprintf(
'The device %s was disallowed for user %s per policy settings.',
self::$_device->id,
self::$_device->user);
self::$_logger->err($msg);
if ($version > self::VERSION_TWELVEONE) {
$this->_globalError = $callback_ret;
} else {
throw new Horde_ActiveSync_Exception($msg);
}
}
}
// Lastly, check if the device has been set to blocked.
if (self::$_device->blocked) {
$msg = sprintf(
'The device %s was blocked.',
self::$_device->id);
self::$_logger->err($msg);
if ($version > self::VERSION_TWELVEONE) {
$this->_globalError = Horde_ActiveSync_Status::DEVICE_BLOCKED_FOR_USER;
} else {
throw new Horde_ActiveSync_Exception($msg);
}
}
// Don't bother with everything else if all we want are Options
if ($cmd == 'Options') {
$this->_doOptionsRequest();
$this->_driver->clearAuthentication();
return true;
}
// Read the initial Wbxml header
$this->_decoder->readWbxmlHeader();
// Support Multipart response for ITEMOPERATIONS requests?
$headers = $this->_request->getHeaders();
if ((!empty($headers['ms-asacceptmultipart']) && $headers['ms-asacceptmultipart'] == 'T') ||
!empty($get['AcceptMultiPart'])) {
$this->_multipart = true;
self::$_logger->info(sprintf(
'[%s] Requesting multipart data.',
$this->_procid)
);
}
// Load the request handler to handle the request
// We must send the eas header here, since some requests may start
// output and be large enough to flush the buffer (e.g., GetAttachment)
// See Bug: 12486
$this->activeSyncHeader();
if ($cmd != 'GetAttachment') {
$this->contentTypeHeader();
}
// Should we announce a new version is available to the client?
if (!empty($needMsRp)) {
self::$_logger->info(sprintf(
'[%s] Announcing X-MS-RP to client.',
$this->_procid)
);
header("X-MS-RP: ". $this->getSupportedVersions());
}
// @TODO: Look at getting rid of having to set the version in the driver
// and get it from the device object for H6.
$this->_driver->setDevice(self::$_device);
$class = 'Horde_ActiveSync_Request_' . basename($cmd);
if (class_exists($class)) {
$request = new $class($this);
$request->setLogger(self::$_logger);
$result = $request->handle();
self::$_logger->info(sprintf(
'[%s] Maximum memory usage for ActiveSync request: %d bytes.',
$this->_procid,
memory_get_peak_usage())
);
return $result;
}
$this->_driver->clearAuthentication();
throw new Horde_ActiveSync_Exception_InvalidRequest(basename($cmd) . ' not supported.');
}
/**
* Send the MS_Server-ActiveSync header.
*
*/
public function activeSyncHeader()
{
header('Allow: OPTIONS,POST');
header('Server: Horde_ActiveSync Library v' . self::LIBRARY_VERSION);
header('Public: OPTIONS,POST');
switch ($this->_maxVersion) {
case self::VERSION_TWOFIVE:
header('MS-Server-ActiveSync: 6.5.7638.1');
break;
case self::VERSION_TWELVE:
header('MS-Server-ActiveSync: 12.0');
break;
case self::VERSION_TWELVEONE:
header('MS-Server-ActiveSync: 12.1');
break;
case self::VERSION_FOURTEEN:
header('MS-Server-ActiveSync: 14.0');
break;
case self::VERSION_FOURTEENONE:
header('MS-Server-ActiveSync: 14.2');
}
}
/**
* Send the protocol versions header.
*
*/
public function versionHeader()
{
header('MS-ASProtocolVersions: ' . $this->getSupportedVersions());
}
/**
* Return supported versions in a comma delimited string suitable for
* sending as the MS-ASProtocolVersions header.
*
* @return string
*/
public function getSupportedVersions()
{
return implode(',', array_slice(self::$_supportedVersions, 0, (array_search($this->_maxVersion, self::$_supportedVersions) + 1)));
}
/**
* Send protocol commands header.
*
*/
public function commandsHeader()
{
header('MS-ASProtocolCommands: ' . $this->getSupportedCommands());
}
/**
* Return the supported commands in a comma delimited string suitable for
* sending as the MS-ASProtocolCommands header.
*
* @return string
*/
public function getSupportedCommands()
{
switch ($this->_maxVersion) {
case self::VERSION_TWOFIVE:
return 'Sync,SendMail,SmartForward,SmartReply,GetAttachment,GetHierarchy,CreateCollection,DeleteCollection,MoveCollection,FolderSync,FolderCreate,FolderDelete,FolderUpdate,MoveItems,GetItemEstimate,MeetingResponse,ResolveRecipients,ValidateCert,Provision,Search,Ping';
case self::VERSION_TWELVE:
case self::VERSION_TWELVEONE:
case self::VERSION_FOURTEEN:
case self::VERSION_FOURTEENONE:
return 'Sync,SendMail,SmartForward,SmartReply,GetAttachment,GetHierarchy,CreateCollection,DeleteCollection,MoveCollection,FolderSync,FolderCreate,FolderDelete,FolderUpdate,MoveItems,GetItemEstimate,MeetingResponse,Search,Settings,Ping,ItemOperations,Provision,ResolveRecipients,ValidateCert';
}
}
/**
* Send provision header
*
*/
public function provisionHeader()
{
header('HTTP/1.1 449 Retry after sending a PROVISION command');
}
/**
* Obtain the policy key header from the request.
*
* @return integer The policy key or '0' if not set.