/
Driver.php
3539 lines (3284 loc) · 140 KB
/
Driver.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
/**
* Copyright 2010-2016 Horde LLC (http://www.horde.org/)
*
* @author Michael J. Rubinsky <mrubinsk@horde.org>
* @package Core
*/
/**
* Horde backend.
*
* Provides the communication between horde data and ActiveSync server.
*
* @author Michael J. Rubinsky <mrubinsk@horde.org>
* @package Core
*/
class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base
{
/**
* Server folder ids for non-email folders.
* We use the @ modifiers to avoid issues in the (fringe) case of
* having email folders named like contacts etc...
*/
const APPOINTMENTS_FOLDER_UID = '@Calendar@';
const CONTACTS_FOLDER_UID = '@Contacts@';
const TASKS_FOLDER_UID = '@Tasks@';
const NOTES_FOLDER_UID = '@Notes@';
const SPECIAL_SENT = 'sent';
const SPECIAL_SPAM = 'spam';
const SPECIAL_TRASH = 'trash';
const SPECIAL_DRAFTS = 'drafts';
const SPECIAL_INBOX = 'inbox';
const SPECIAL_OUTBOX = 'outbox';
const FOLDER_PART_CLASS = 0;
const FOLDER_PART_ID = 1;
/**
* Mappings for server uids -> display names. Populated in the const'r
* so we can use localized text.
*
* @var array
*/
protected $_displayMap = array();
/**
* Cache message stats
*
* @var array An array of stat hashes
*/
protected $_modCache;
/**
* Horde connector instance
*
* @var Horde_Core_ActiveSync_Connector
*/
protected $_connector;
/**
* Local cache of folders polled from the various backends.
*
* @var array
*/
protected $_folders = array();
/**
* Imap client adapter
*
* @var Horde_ActiveSync_Imap_Adapter
*/
protected $_imap;
/**
* Authentication object
*
* @var Horde_Auth_Base
*/
protected $_auth;
/**
* Current process id
*
* @var integer
*/
protected $_pid;
/**
* Local cache of last verb searches.
*
* @var array
*/
protected $_verbs = array();
/**
* Class => Id map
*
* @var array
*/
protected $_classMap = array(
Horde_ActiveSync::CLASS_TASKS => self::TASKS_FOLDER_UID,
Horde_ActiveSync::CLASS_CALENDAR => self::APPOINTMENTS_FOLDER_UID,
Horde_ActiveSync::CLASS_CONTACTS => self::CONTACTS_FOLDER_UID,
Horde_ActiveSync::CLASS_NOTES => self::NOTES_FOLDER_UID
);
/**
* Const'r
*
* @param array $params Configuration parameters:
* - logger: (Horde_Log_Logger) The logger.
* DEFAULT: none (No logging).
*
* - state: (Horde_ActiveSync_State_Base) The state driver.
* DEFAULT: none (REQUIRED).
* - connector: (Horde_Core_ActiveSync_Connector) The connector object
* for communicating with the registry.
* DEFAULT: none (REQUIRED)
* - auth: (Horde_Auth) The auth object.
* DEFAULT: none (REQUIRED).
* - imap: (Horde_ActiveSync_Imap_Adapter) The IMAP adapter if email
* support is desired.
* DEFAULT: none (No email support will be provided).
* - cache: (Horde_Cache) A cache object to store certain types of
* data, such as mailbox search results.
* @since 2.12.0
*/
public function __construct(array $params = array())
{
parent::__construct($params);
$this->_pid = getmypid();
if (empty($this->_params['connector']) ||
!($this->_params['connector'] instanceof Horde_Core_ActiveSync_Connector)) {
throw new InvalidArgumentException('Missing required connector object.');
}
if (empty($this->_params['auth']) ||
!($this->_params['auth'] instanceof Horde_Auth_Base)) {
throw new InvalidArgumentException('Missing required Auth object');
}
$this->_connector = $params['connector'];
$this->_auth = $params['auth'];
unset($this->_params['connector']);
unset($this->_params['auth']);
if (!empty($this->_params['imap'])) {
$this->_imap = $this->_params['imap'];
unset($this->_params['imap']);
}
if (!empty($this->_params['cache'])) {
$this->_cache = $this->_params['cache'];
}
// Build the displaymap
$this->_displayMap = array(
self::APPOINTMENTS_FOLDER_UID => Horde_ActiveSync_Translation::t('Calendar'),
self::CONTACTS_FOLDER_UID => Horde_ActiveSync_Translation::t('Contacts'),
self::TASKS_FOLDER_UID => Horde_ActiveSync_Translation::t('Tasks'),
self::NOTES_FOLDER_UID => Horde_ActiveSync_Translation::t('Notes'),
);
}
/**
* Set the logger.
*
* @param Horde_Log_Logger $logger The logger.
*/
public function setLogger($logger)
{
parent::setLogger($logger);
$this->_connector->setLogger($logger);
if (!empty($this->_imap)) {
$this->_imap->setLogger($logger);
}
}
/**
* Authenticate to Horde
*
* @param string $username The username to authenticate as (as passed by
* the device).
* @param string $password The password
* @param string $domain The user domain (unused in this driver).
*
* @return mixed Boolean true on success, boolean false on credential
* failure or Horde_ActiveSync::AUTH_REASON_*
* constant on policy failure.
*/
public function authenticate($username, $password, $domain = null)
{
global $injector, $conf;
$this->_logger->info(sprintf(
'[%s] Horde_Core_ActiveSync_Driver::authenticate() attempt for %s',
$this->_pid,
$username));
// First try transparent/X509. Happens for authtype == 'cert' || 'basic_cert'
if ($conf['activesync']['auth']['type'] != 'basic') {
if (!$this->_auth->transparent()) {
$injector->getInstance('Horde_Log_Logger')->notice(sprintf('Login failed ActiveSync client certificate for user %s.', $username));
return false;
}
if ($username != $GLOBALS['registry']->getAuth()) {
$injector->getInstance('Horde_Log_Logger')->notice(sprintf(
'Access granted based on transparent authentication of user %s, but ActiveSync client is requesting access for %s.',
$GLOBALS['registry']->getAuth(), $username));
$GLOBALS['registry']->clearAuth();
return false;
}
$this->_logger->info(sprintf(
'Access granted based on transparent authentication for %s. Client certificate name: %s',
$GLOBALS['registry']->getAuth(), $username));
}
// Now check Basic. Happens for authtype == 'basic' || 'basic_cert'
if ($conf['activesync']['auth']['type'] != 'cert' &&
!$this->_auth->authenticate($username, array('password' => $password))) {
$injector->getInstance('Horde_Log_Logger')->notice(sprintf('Login failed from ActiveSync client for user %s.', $username));
return false;
}
// Get the username from the registry so we capture it after any
// hooks were run on it.
$username = $GLOBALS['registry']->getAuth();
$perms = $injector->getInstance('Horde_Perms');
if ($perms->exists('horde:activesync')) {
// Check permissions to ActiveSync
if (!$this->_getPolicyValue('activesync', $perms->getPermissions('horde:activesync', $username))) {
$this->_logger->info(sprintf(
"Access denied for user %s per policy settings.",
$username)
);
return Horde_ActiveSync::AUTH_REASON_USER_DENIED;
}
}
return parent::authenticate($username, $password, $domain);
}
/**
* Clean up
*
* @see framework/ActiveSync/lib/Horde/ActiveSync/Driver/Horde_ActiveSync_Driver_Base#clearAuthentication()
*/
public function clearAuthentication()
{
$this->_connector->clearAuth();
$this->_logger->info(sprintf(
"[%s] User %s logged off",
$this->_pid,
$this->_user));
return true;
}
/**
* Setup sync parameters. The user provided here is the user the backend
* will sync with. This allows you to authenticate as one user, and sync as
* another, if the backend supports this (Horde does not).
*
* @param string $user The username to sync as on the backend.
*
* @return boolean
*/
public function setup($user)
{
parent::setup($user);
$this->_modCache = array();
return true;
}
/**
* Get the wastebasket folder. If this returns false, imap deletions are
* permanent. If it returns a valid mailbox, deletions are treated as moves
* to this mailbox. Note that any collection class other than
* Horde_ActiveSync::CLASS_EMAIL will return false.
*
* @return string|boolean Returns name of the trash folder, or false
* if not using a trash folder.
*/
public function getWasteBasket($class)
{
if ($class != Horde_ActiveSync::CLASS_EMAIL) {
return false;
}
$folders = $this->_getMailFolders();
foreach ($folders as $folder) {
if ($folder->type == Horde_ActiveSync::FOLDER_TYPE_WASTEBASKET) {
return $folder->serverid;
}
}
return false;
}
/**
* Return an array of stats for the server's folder list.
*
* @return array An array of folder stats @see self::statFolder()
* @todo Horde 6 move to base class
*/
public function getFolderList()
{
$this->_logger->info(sprintf(
"[%s] Horde_Core_ActiveSync_Driver::getFolderList()",
$this->_pid));
$folderlist = $this->getFolders();
$folders = array();
foreach ($folderlist as $f) {
$folders[] = $this->statFolder(
$f->serverid, $f->parentid, $f->displayname, $f->_serverid, $f->type);
}
return $folders;
}
/**
* Return an array of the server's folder objects.
*
* @return array An array of Horde_ActiveSync_Message_Folder objects.
*/
public function getFolders()
{
if (empty($this->_folders)) {
ob_start();
$multiplex = $this->_device->multiplex;
try {
$supported = $this->_connector->horde_listApis();
} catch (Exception $e) {
$this->_logger->err($e->getMessage());
$this->_endBuffer();
return array();
}
$folders = array();
if (array_search('calendar', $supported) !== false) {
$temp = $this->_connector->getFolders(Horde_ActiveSync::CLASS_CALENDAR, $multiplex);
if (is_array($temp)) {
foreach ($temp as $id => $folder) {
try {
$temp_folder = $this->_getFolder(
Horde_ActiveSync::CLASS_CALENDAR . ':' . $id,
array(
'class' => Horde_ActiveSync::CLASS_CALENDAR,
'primary' => $folder['primary'],
'display' => $folder['display']
)
);
if (!empty($temp_folder)) {
$folders[] = $temp_folder;
} else {
$this->_logger->info('Unable to find a useable calendar folder.');
}
} catch (Horde_ActiveSync_Exception $e) {
}
}
} else {
try {
$temp_folder = $this->_getFolder($temp);
if (!empty($temp_folder)) {
$folders[] = $temp_folder;
} else {
$this->_logger->info('Unable to find a useable calendar folder.');
}
} catch (Horde_ActiveSync_Exception $e) {
$this->_logger->info('Unable to find a useable calendar folder.');
}
}
}
if (array_search('contacts', $supported) !== false) {
$temp = $this->_connector->getFolders(Horde_ActiveSync::CLASS_CONTACTS, $multiplex);
if (is_array($temp)) {
foreach ($temp as $id => $folder) {
try {
$temp_folder = $this->_getFolder(
Horde_ActiveSync::CLASS_CONTACTS . ':' . $id,
array(
'class' => Horde_ActiveSync::CLASS_CONTACTS,
'primary' => $folder['primary'],
'display' => $folder['display']
)
);
if (!empty($temp_folder)) {
$folders[] = $temp_folder;
} else {
$this->_logger->info('Unable to find a useable contacts folder.');
}
} catch (Horde_ActiveSync_Exception $e) {
$this->_logger->info('Unable to find a useable contacts folder.');
}
}
} else {
try {
$temp_folder = $this->_getFolder($temp);
if (!empty($temp_folder)) {
$folders[] = $temp_folder;
} else {
$this->_logger->info('Unable to find a useable contacts folder.');
}
} catch (Horde_ActiveSync_Exception $e) {
$this->_logger->info('Unable to find a useable contacts folder.');
}
}
}
if (array_search('tasks', $supported) !== false) {
$temp = $this->_connector->getFolders(Horde_ActiveSync::CLASS_TASKS, $multiplex);
if (is_array($temp)) {
foreach ($temp as $id => $folder) {
try {
$temp_folder = $this->_getFolder(
Horde_ActiveSync::CLASS_TASKS . ':' . $id,
array(
'class' => Horde_ActiveSync::CLASS_TASKS,
'primary' => $folder['primary'],
'display' => $folder['display']
)
);
if (!empty($temp_folder)) {
$folders[] = $temp_folder;
} else {
$this->_logger->info('Unable to find a useable tasks folder.');
}
} catch (Horde_ActiveSync_Exception $e) {
$this->_logger->info('Unable to find a useable tasks folder.');
}
}
} else {
try {
$temp_folder = $this->_getFolder($temp);
if (!empty($temp_folder)) {
$folders[] = $temp_folder;
} else {
$this->_logger->info('Unable to find a useable tasks folder.');
}
} catch (Horde_ActiveSync_Exception $e) {
$this->_logger->info('Unable to find a useable tasks folder.');
}
}
}
if (array_search('notes', $supported) !== false) {
$temp = $this->_connector->getFolders(Horde_ActiveSync::CLASS_NOTES, $multiplex);
if (is_array($temp)) {
foreach ($temp as $id => $folder) {
try {
$temp_folder = $this->_getFolder(
Horde_ActiveSync::CLASS_NOTES . ':' . $id,
array(
'class' => Horde_ActiveSync::CLASS_NOTES,
'primary' => $folder['primary'],
'display' => $folder['display']
)
);
if (!empty($temp_folder)) {
$folders[] = $temp_folder;
} else {
$this->_logger->info('Unable to find a useable notes folder.');
}
} catch (Horde_ActiveSync_Exception $e) {
$this->_logger->info('Unable to find a useable notes folder.');
}
}
} else {
try {
$temp_folder = $this->_getFolder($temp);
} catch (Horde_ActiveSync_Exception $e) {
}
if (!empty($temp_folder)) {
$folders[] = $temp_folder;
} else {
$this->_logger->info('Unable to find a useable notes folder.');
}
}
}
if ($this->_version > Horde_ActiveSync::VERSION_TWELVEONE &&
$GLOBALS['registry']->hasInterface('contacts')) {
$folders[] = $this->_getFolder('RI', array('class' => 'RI'));
}
// Always return at least the "dummy" IMAP folders since some
// clients refuse to consider a FOLDERSYNC successful without
// at least an INBOX, even with email sync turned off.
try {
$folders = array_merge($folders, $this->_getMailFolders());
} catch (Horde_ActiveSync_Exception $e) {
$this->_logger->err($e->getMessage());
$this->_endBuffer();
return array();
}
$this->_endBuffer();
$this->_folders = $folders;
}
return $this->_folders;
}
/**
* Factory for creating new Horde_ActiveSync_Message_Folder objects from
* within this class.
*
* For BC reasons, we need to still accept the *_FOLDER_UID constants
* and they should represent folders that are multiplexed.
*
* @param string $id The folder's server id.
* @param array $params Additional folder parameters:
* - class: The collection class, a Horde_ActiveSync::CLASS_* constant
* - primary: This folder is the 'default' collection for this class.
* - display: The display name for FOLDER_TYPE_USER folders.
*
* @return Horde_ActiveSync_Message_Folder The folder object.
* @throws Horde_ActiveSync_Exception
*/
protected function _getFolder($id, array $params = array())
{
if (empty($id)) {
throw new Horde_ActiveSync_Exception();
}
// First check for legacy/multiplexed IDs.
switch ($id) {
case self::APPOINTMENTS_FOLDER_UID:
return $this->_buildNonMailFolder(
$id,
0,
Horde_ActiveSync::FOLDER_TYPE_APPOINTMENT,
$this->_displayMap[self::APPOINTMENTS_FOLDER_UID]);
case self::CONTACTS_FOLDER_UID:
return $this->_buildNonMailFolder(
$id,
0,
Horde_ActiveSync::FOLDER_TYPE_CONTACT,
$this->_displayMap[self::CONTACTS_FOLDER_UID]);
case self::TASKS_FOLDER_UID:
return $this->_buildNonMailFolder(
$id,
0,
Horde_ActiveSync::FOLDER_TYPE_TASK,
$this->_displayMap[self::TASKS_FOLDER_UID]);
case self::NOTES_FOLDER_UID:
return $this->_buildNonMailFolder(
$id,
0,
Horde_ActiveSync::FOLDER_TYPE_NOTE,
$this->_displayMap[self::NOTES_FOLDER_UID]);
case 'RI':
return $this->_buildNonMailFolder(
'RI',
0,
Horde_ActiveSync::FOLDER_TYPE_RECIPIENT_CACHE,
'RI'
);
}
// Either an email folder or a non-multiplexed non-email folder.
// Check for a valid class.
if (empty($params['class'])) {
// Must be a mail folder
$folders = $this->_getMailFolders();
foreach ($folders as $folder) {
if ($folder->_serverid == $id) {
return $folder;
}
}
$this->_logger->err(sprintf('Folder %s unknown', $id));
throw new Horde_ActiveSync_Exception(sprintf('Folder %s unknown', $id));
}
// Non-Multiplexed non-email collection?
$primary = !empty($params['primary']);
switch ($params['class']) {
case Horde_ActiveSync::CLASS_CALENDAR:
return $this->_buildNonMailFolder(
$id,
0,
$primary ? Horde_ActiveSync::FOLDER_TYPE_APPOINTMENT : Horde_ActiveSync::FOLDER_TYPE_USER_APPOINTMENT,
$params['display']
);
case Horde_ActiveSync::CLASS_CONTACTS:
return $this->_buildNonMailFolder(
$id,
0,
$primary ? Horde_ActiveSync::FOLDER_TYPE_CONTACT : Horde_ActiveSync::FOLDER_TYPE_USER_CONTACT,
$params['display']
);
case Horde_ActiveSync::CLASS_TASKS:
return $this->_buildNonMailFolder(
$id,
0,
$primary ? Horde_ActiveSync::FOLDER_TYPE_TASK : Horde_ActiveSync::FOLDER_TYPE_USER_TASK,
$params['display']
);
case Horde_ActiveSync::CLASS_NOTES:
return $this->_buildNonMailFolder(
$id,
0,
$primary ? Horde_ActiveSync::FOLDER_TYPE_NOTE : Horde_ActiveSync::FOLDER_TYPE_USER_NOTE,
$params['display']
);
}
}
/**
* Return a Horde_ActiveSync_Message_Folder object.
*
* @param string $id The folder's server id.
*
* @return Horde_ActiveSync_Message_Folder
* @throws Horde_ActiveSync_Exception
*/
public function getFolder($id)
{
$folders = $this->getFolders();
foreach ($folders as $folder) {
if ($folder->_serverid == $id) {
return $folder;
}
}
}
/**
* Parse a folderid.
*
* @param string $id The folder id.
* @param boolean $checkEmail Verify $id is not an Email collection.
*
* @return string|array A parsed folder array or
* Horde_ActiveSync::CLASS_* constant.
*/
protected function _parseFolderId($id, $checkEmail = false)
{
if ($id == 'RI') {
return $id;
}
if (strpos($id, ':') === false) {
switch ($id) {
case self::APPOINTMENTS_FOLDER_UID:
return Horde_ActiveSync::CLASS_CALENDAR;
case self::CONTACTS_FOLDER_UID:
return Horde_ActiveSync::CLASS_CONTACTS;
case self::TASKS_FOLDER_UID:
return Horde_ActiveSync::CLASS_TASKS;
case self::NOTES_FOLDER_UID:
return Horde_ActiveSync::CLASS_NOTES;
default:
return Horde_ActiveSync::CLASS_EMAIL;
}
} else {
$parts = explode(':', $id, 2);
if (count($parts) == 2) {
switch ($parts[self::FOLDER_PART_CLASS]) {
case Horde_ActiveSync::CLASS_CALENDAR:
case Horde_ActiveSync::CLASS_TASKS:
case Horde_ActiveSync::CLASS_CONTACTS:
case Horde_ActiveSync::CLASS_NOTES:
if ($checkEmail) {
$folders = $this->_getMailFolders();
foreach ($folders as $folder) {
if ($folder->_serverid == $id) {
return Horde_ActiveSync::CLASS_EMAIL;
}
}
}
return $parts;
}
}
return Horde_ActiveSync::CLASS_EMAIL;
}
}
/**
* Change a folder on the server.
*
* @param string $old_name The server's existing folder id.
* @param string $new_name The new display name.
* @param string $parent The folder's parent server id, if needed.
* @param string $uid The existing folder uid, if this is an edit.
* @since 2.5.0 (@todo Look at this for H6. It's
* here now to save an extra DB lookup for data
* we already have.)
* @param integer $type The EAS Folder type. @since 2.12.0
*
* @return Horde_ActiveSync_Message_Folder
* @throws Horde_ActiveSync_Exception
*/
public function changeFolder($old_name, $new_name, $parent, $uid = null, $type = null)
{
$this->_logger->info(sprintf(
'[%s] Horde_Core_ActiveSync_Driver::changeFolder(%s, %s, %s, %s, %s)',
getmypid(), $old_name, $new_name, $parent, $uid, $type));
// For FOLDERUPDATE requests, the EAS Folder type is not passed by
// the client so we need to figure it out.
if (empty($type) && !empty($old_name)) {
$parts = $this->_parseFolderId($old_name, true);
if (is_array($parts)) {
$type = $parts[self::FOLDER_PART_CLASS];
} else {
$type = $parts;
}
}
switch ($type) {
case Horde_ActiveSync::CLASS_EMAIL:
case Horde_ActiveSync::FOLDER_TYPE_USER_MAIL:
if (!$old_name) {
try {
$serverid = $this->_imap->createMailbox($new_name, $parent);
} catch (Horde_ActiveSync_Exception $e) {
$this->_logger->err($e->getMessage());
throw $e;
}
} else {
try {
$serverid = $this->_imap->renameMailbox($old_name, $new_name, $parent);
$uid = $this->_getFolderUidForBackendId($serverid, Horde_ActiveSync::FOLDER_TYPE_USER_MAIL, $old_name);
} catch (Horde_ActiveSync_Exception $e) {
$this->_logger->err($e->getMessage());
throw $e;
}
}
return $this->getFolder($serverid);
case Horde_ActiveSync::CLASS_TASKS:
case Horde_ActiveSync::FOLDER_TYPE_USER_TASK:
$class = Horde_ActiveSync::CLASS_TASKS;
$type = Horde_ActiveSync::FOLDER_TYPE_USER_TASK;
break;
case Horde_ActiveSync::CLASS_CALENDAR:
case Horde_ActiveSync::FOLDER_TYPE_USER_APPOINTMENT:
$class = Horde_ActiveSync::CLASS_CALENDAR;
$type = Horde_ActiveSync::FOLDER_TYPE_USER_APPOINTMENT;
break;
case Horde_ActiveSync::CLASS_CONTACTS:
case Horde_ActiveSync::FOLDER_TYPE_USER_CONTACT:
$class = Horde_ActiveSync::CLASS_CONTACTS;
$type = Horde_ActiveSync::FOLDER_TYPE_USER_CONTACT;
break;
case Horde_ActiveSync::CLASS_NOTES:
case Horde_ActiveSync::FOLDER_TYPE_USER_NOTE:
$class = Horde_ActiveSync::CLASS_NOTES;
$type = Horde_ActiveSync::FOLDER_TYPE_USER_NOTE;
break;
default:
throw new Horde_ActiveSync_Exception('Unsupported EAS Collection Class.');
}
if (!$old_name) {
try {
$id = $this->_connector->createFolder($class, $new_name);
} catch (Horde_Exception $e) {
$this->_logger->err($e->getMessage());
throw new Horde_ActiveSync_Exception($e);
}
} else {
if (empty($parts)) {
$parts = $this->_parseFolderId($old_name);
}
if (is_array($parts)) {
$id = $parts[self::FOLDER_PART_ID];
} else {
$id = $parts;
}
try {
$this->_connector->changeFolder($class, $id, $new_name);
} catch (Horde_Exception $e) {
$this->_logger->err($e->getMessage());
throw new Horde_ActiveSync_Exception($e);
}
}
return $this->_buildNonMailFolder(
$class . ':' . $id, Horde_ActiveSync::FOLDER_ROOT, $type, $new_name
);
}
/**
* Delete a folder on the server.
*
* @param string $id The server's folder id.
* @param string $parent The folder's parent, if needed. @deprecated
*/
public function deleteFolder($id, $parent = Horde_ActiveSync::FOLDER_ROOT)
{
$this->_logger->info(sprintf(
'[%s] Horde_Core_ActiveSync_Driver::deleteFolder(%s)',
getmypid(), $id));
$parts = $this->_parseFolderId($id);
if (is_array($parts)) {
$folder_class = $parts[self::FOLDER_PART_CLASS];
$folder_id = $parts[self::FOLDER_PART_ID];
} else {
$folder_class = $parts;
$folder_id = $id;
}
switch ($folder_class) {
case Horde_ActiveSync::CLASS_EMAIL:
case Horde_ActiveSync::FOLDER_TYPE_USER_MAIL:
try {
$this->_logger->info($folder_id);
$this->_imap->deleteMailbox($folder_id);
} catch (Horde_ActiveSync_Exception $e) {
$this->_logger->err($e->getMessage());
throw $e;
}
break;
case Horde_ActiveSync::CLASS_TASKS:
case Horde_ActiveSync::CLASS_CALENDAR:
case Horde_ActiveSync::CLASS_CONTACTS:
case Horde_ActiveSync::CLASS_NOTES:
$this->_connector->deleteFolder($folder_class, $folder_id);
}
}
/**
* Stat folder. Note that since the only thing that can ever change for a
* folder is the name, we use that as the 'mod' value.
*
* @param string $id The folder's EAS uid
* @param mixed $parent The parent folder (or 0 if none).
* @param mixed $mod Modification indicator. For folders, this is the
* display name of the folder, since that's the
* only thing that can change.
* @param string $serverid The backend serverid for this folder.
* @param integer $type The EAS folder type, a
* Horde_ActiveSync::FOLDER_TYPE_* contant.
* @since 2.12.0
*
* @return a stat hash:
* - id: The activesync folder identifier.
* - mod: The modification value.
* - parent: The folder's parent id.
* - serverid: The backend server's folder name for this folder.
* - type: The EAS folder type. @since 2.12.0
*
* @todo Horde 6, move to the base class.
*/
public function statFolder($id, $parent = '0', $mod = null, $serverid = null, $type = null)
{
$folder = array();
$folder['id'] = $id;
$folder['mod'] = empty($mod) ? $id : $mod;
$folder['parent'] = $parent;
$folder['serverid'] = !empty($serverid) ? $serverid : $id;
$folder['type'] = !empty($type) ? $type : null;
return $folder;
}
/**
* Get a list of server changes that occured during the specified time
* period.
*
* @param Horde_ActiveSync_Folder_Base $folder
* The ActiveSync folder object to request changes for.
* @param integer $from_ts The starting timestamp
* @param integer $to_ts The ending timestamp
* @param integer $cutoffdate The earliest date to retrieve back to.
*
* @param boolean $ping If true, returned changeset may
* not contain the full changeset, but rather
* only a single change, designed only to
* indicate *some* change has taken place. The
* value should not be used to determine *what*
* change has taken place.
* @param boolean $ignoreFirstSync If true, will not trigger an initial sync
* if $from_ts is 0. Needed to avoid race
* conditions when we don't have any history
* data. @since 2.6.0
* @todo If we can pass the synckey (
* perhaps as part of $folder), we can
* just look for synckey 0 to know when
* we CAN trigger an initial sync without
* this flag.
* @param integer $maxitems Maximum number of recipients for a RI
* collection. @since 2.12.0
* @param boolean $refreshFilter Force a SOFTDELETE operation and check
* for new items within the (newly changed)
* current FilterType interval.
* @since 2.19.0
*
* @return array An array of hashes that contain the ids of items that have
* changed in the specified collection along with a 'type'
* flag that indicates the type of change.
* @todo H6 - Clean up method parameters, update parent class etc...
* - Return a new ids object.
* - Refactor to use a Repository pattern for each supported
* collection and move the bulk of the logic in the switch
* structure below to the various classes - and refactor out most
* of the stuff in the registry connector since the Repositories
* will handle the basic CRUD operations and change detection on
* each collection.
*/
public function getServerChanges(
$folder, $from_ts, $to_ts, $cutoffdate, $ping, $ignoreFirstSync = false, $maxitems = 100, $refreshFilter = false)
{
$this->_logger->info(sprintf(
"[%s] Horde_Core_ActiveSync_Driver::getServerChanges(%s, %u, %u, %u, %d, %s, %u, %s)",
$this->_pid,
$folder->serverid(),
$from_ts,
$to_ts,
$cutoffdate,
$ping,
$ignoreFirstSync,
$maxitems,
$refreshFilter)
);
$changes = array(
'add' => array(),
'delete' => array(),
'modify' => array(),
'soft' => array()
);
ob_start();
switch ($folder->collectionClass()) {
case Horde_ActiveSync::CLASS_CALENDAR:
if ($folder->serverid() == self::APPOINTMENTS_FOLDER_UID) {
$server_id = null;
} else {
$parts = $this->_parseFolderId($folder->serverid());
$server_id = $parts[self::FOLDER_PART_ID];
}
if ($from_ts == 0 && !$ignoreFirstSync) {
// Can't use History if it's a first sync
$startstamp = (int)$cutoffdate;
$endstamp = time() + 32140800; //60 * 60 * 24 * 31 * 12 == one year
try {
$changes['add'] = $this->_connector->calendar_listUids($startstamp, $endstamp, $server_id);
} catch (Horde_Exception_AuthenticationFailure $e) {
$this->_endBuffer();
throw $e;
} catch (Horde_Exception $e) {
$this->_logger->err($e->getMessage());
$this->_endBuffer();
return array();
}
$folder->setSoftDeleteTimes($cutoffdate, time());
} else {
try {
$changes = $this->_connector->getChanges('calendar', $from_ts, $to_ts, $server_id);
} catch (Horde_Exception_AuthenticationFailure $e) {
$this->_endBuffer();
throw $e;
} catch (Horde_Exception $e) {
$this->_logger->err($e->getMessage());
$this->_endBuffer();
return array();
}
// Softdelete. We check this once per close-to 24 hour period,or
// if the FILTERTYPE range changes. We introduce an element of
// randomness in the time to help avoid a large number of
// clients performing a softdelete at once. It's 23 hours + some
// random number of minutes < 60.
//
// @todo We need to populate additional events if the FILTERTYPE
// is expanded, but we need to persist the previous FILTERTYPE
// so we know exactly which events we already have and which
// we don't (since we don't track the UIDs themselves).
if (!$ping) {
if (!$refreshFilter) {
$sd = $folder->getSoftDeleteTimes();
if ($sd[1] + 82800 + mt_rand(0, 3600) < time()) {
$from = $sd[2];
}
} else {
// Force refresh the FILTERTYPE.
$from = 0;
}
if (isset($from)) {
$this->_logger->info(sprintf(
'[%s] Polling for SOFTDELETE items in calendar collection',
getmypid()));
// Gets the list of ids contained between the last softdelete
// run and the current cutoffdate.
$changes['soft'] = $this->_connector->softDelete('calendar', $from, $cutoffdate, $server_id);
$folder->setSoftDeleteTimes($cutoffdate, time());
}
}
}
break;