/
Adapter.php
1594 lines (1479 loc) · 62.6 KB
/
Adapter.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_Imap_Adapter::
*
* @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 2012-2014 Horde LLC (http://www.horde.org)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package ActiveSync
*/
/**
* Horde_ActiveSync_Imap_Adapter:: Contains methods for communicating with
* Horde's Horde_Imap_Client library.
*
* @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 2012-2014 Horde LLC (http://www.horde.org)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package ActiveSync
*/
class Horde_ActiveSync_Imap_Adapter
{
/**
* @var Horde_ActiveSync_Interface_ImapFactory
*/
protected $_imap;
/**
* @var Horde_Log_Logger
*/
protected $_logger;
/**
* @var string
*/
protected $_defaultNamespace;
/**
* Process id used for logging.
*
* @var integer
*/
protected $_procid;
/**
* Cont'r
*
* @param array $params Parameters:
* - factory: (Horde_ActiveSync_Interface_ImapFactory) Factory object
* DEFAULT: none - REQUIRED
*/
public function __construct(array $params = array())
{
$this->_imap = $params['factory'];
Horde_Mime_Part::$defaultCharset = 'UTF-8';
Horde_Mime_Headers::$defaultCharset = 'UTF-8';
$this->_procid = getmypid();
$this->_logger = new Horde_Support_Stub();
}
/**
* Append a message to the specified mailbox. Used for saving sent email.
*
* @param string $folderid The mailbox
* @param string|stream $msg The message
* @param array $flags Any flags to set on the newly appended message.
*
* @throws new Horde_ActiveSync_Exception, Horde_ActiveSync_Exception_FolderGone
*/
public function appendMessage($folderid, $msg, array $flags = array())
{
$imap = $this->_getImapOb();
$message = array(array('data' => $msg, 'flags' => $flags));
$mbox = new Horde_Imap_Client_Mailbox($folderid);
try {
$imap->append($mbox, $message);
} catch (Horde_Imap_Client_Exception $e) {
if (!$this->_mailboxExists($folderid)) {
throw new Horde_ActiveSync_Exception_FolderGone();
}
throw new Horde_ActiveSync_Exception($e);
}
}
/**
* Create a new mailbox on the server, and subscribe to it.
*
* @param string $name The new mailbox name.
* @param string $parent The parent mailbox, if any.
*
* @return string The new serverid for the mailbox. This is the UTF-8 name
* of the mailbox. @since 2.9.0
* @throws Horde_ActiveSync_Exception, Horde_ActiveSync_Exception_FolderExists
*/
public function createMailbox($name, $parent = null)
{
if (!empty($parent)) {
$ns = $this->_defaultNamespace();
$name = $parent . $ns['delimiter'] . $name;
}
$mbox = new Horde_Imap_Client_Mailbox($this->_prependNamespace($name));
$imap = $this->_getImapOb();
try {
$imap->createMailbox($mbox);
$imap->subscribeMailbox($mbox, true);
} catch (Horde_Imap_Client_Exception $e) {
if ($e->getCode() == Horde_Imap_Client_Exception::ALREADYEXISTS) {
throw new Horde_ActiveSync_Exception_FolderExists('Folder Exists!');
}
throw new Horde_ActiveSync_Exception($e);
}
return $mbox->utf8;
}
/**
* Delete a mailbox
*
* @param string $name The mailbox name to delete.
*
* @throws Horde_ActiveSync_Exception, Horde_ActiveSync_Exception_FolderGone
*/
public function deleteMailbox($name)
{
$mbox = new Horde_Imap_Client_Mailbox($name);
try {
$this->_getImapOb()->deleteMailbox($mbox);
} catch (Horde_Imap_Client_Exception $e) {
if ($e->getCode() == Horde_Imap_Client_Exception::NONEXISTENT) {
throw new Horde_ActiveSync_Exception_FolderGone();
}
throw new Horde_ActiveSync_Exception($e);
}
}
/**
* Completely empty specified mailbox.
*
* @param string $mbox The mailbox to empty.
*
* @throws Horde_ActiveSync_Exception
* @since 2.18.0
*/
public function emptyMailbox($mbox)
{
$imap = $this->_getImapOb();
$mbox = new Horde_Imap_Client_Mailbox($mbox);
try {
$imap->expunge($mbox, array('delete' => true));
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_ActiveSync_Exception($e);
}
}
/**
* Permanently delete a mail message.
*
* @param array $uids The message UIDs
* @param string $folderid The folder id.
*
* @return array An array of uids that were successfully deleted.
* @throws Horde_ActiveSync_Exception
*/
public function deleteMessages(array $uids, $folderid)
{
$imap = $this->_getImapOb();
$mbox = new Horde_Imap_Client_Mailbox($folderid);
$ids_obj = new Horde_Imap_Client_Ids($uids);
// Need to ensure the source message exists so we may properly notify
// the client of the error.
$search_q = new Horde_Imap_Client_Search_Query();
$search_q->ids($ids_obj);
$fetch_res = $imap->search($mbox, $search_q);
if ($fetch_res['count'] != count($uids)) {
$ids_obj = $fetch_res['match'];
}
try {
$imap->store($mbox, array(
'ids' => $ids_obj,
'add' => array('\deleted'))
);
$imap->expunge($mbox, array('ids' => $ids_obj));
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_ActiveSync_Exception($e);
}
return $ids_obj->ids;
}
/**
* Return the content of a specific MIME part of the specified message.
*
* @param string $mailbox The mailbox name.
* @param string $uid The message UID.
* @param string $part The MIME part identifier.
*
* @return Horde_Mime_Part The attachment data
*
* @throws Horde_ActiveSync_Exception
*/
public function getAttachment($mailbox, $uid, $part)
{
$imap = $this->_getImapOb();
$mbox = new Horde_Imap_Client_Mailbox($mailbox);
$messages = $this->_getMailMessages($mbox, array($uid));
if (empty($messages[$uid]) || !$messages[$uid]->exists(Horde_Imap_Client::FETCH_STRUCTURE)) {
throw new Horde_ActiveSync_Exception('Message Gone');
}
$msg = new Horde_ActiveSync_Imap_Message($imap, $mbox, $messages[$uid]);
$part = $msg->getMimePart($part);
return $part;
}
/**
* Return an array of available mailboxes. Uses's the mail/mailboxList API
* method for obtaining the list.
*
* @return array
*/
public function getMailboxes()
{
return $this->_imap->getMailboxes();
}
/**
* Return the list of special mailboxes.
*
* @return array
*/
public function getSpecialMailboxes()
{
return $this->_imap->getSpecialMailboxes();
}
/**
* Return a complete Horde_ActiveSync_Imap_Message object for the requested
* uid.
*
* @param string $mailbox The mailbox name.
* @param array|integer $uid The message uid.
* @param array $options Additional options:
* - headers: (boolean) Also fetch the message headers if this is true.
* DEFAULT: false (Do not fetch headers).
*
* @return array An array of Horde_ActiveSync_Imap_Message objects.
*/
public function getImapMessage($mailbox, $uid, array $options = array())
{
if (!is_array($uid)) {
$uid = array($uid);
}
$mbox = new Horde_Imap_Client_Mailbox($mailbox);
// @todo H6 - expand the $options array the same as _getMailMessages()
// for now, always retrieve the envelope data as well.
$options['envelope'] = true;
$messages = $this->_getMailMessages($mbox, $uid, $options);
$res = array();
foreach ($messages as $id => $message) {
if ($message->exists(Horde_Imap_Client::FETCH_STRUCTURE)) {
$res[$id] = new Horde_ActiveSync_Imap_Message($this->_getImapOb(), $mbox, $message);
}
}
return $res;
}
/**
* Return message changes from the specified mailbox.
*
* @param Horde_ActiveSync_Folder_Imap $folder The folder object.
* @param array $options Additional options:
* - sincedate: (integer) Timestamp of earliest message to retrieve.
* DEFAULT: 0 (Don't filter).
* - protocolversion: (float) EAS protocol version to support.
* DEFAULT: none REQUIRED
* - softdelete: (boolean) If true, calculate SOFTDELETE data.
* @since 2.8.0
*
* @return Horde_ActiveSync_Folder_Imap The folder object, containing any
* change instructions for the device.
*
* @throws Horde_ActiveSync_Exception_FolderGone,
* Horde_ActiveSync_Exception, Horde_ActiveSync_Exception_StaleState
*/
public function getMessageChanges(
Horde_ActiveSync_Folder_Imap $folder, array $options = array())
{
$imap = $this->_getImapOb();
$mbox = new Horde_Imap_Client_Mailbox($folder->serverid());
// Note: non-CONDSTORE servers will return a highestmodseq of 0
$status_flags = Horde_Imap_Client::STATUS_HIGHESTMODSEQ |
Horde_Imap_Client::STATUS_UIDVALIDITY |
Horde_Imap_Client::STATUS_UIDNEXT_FORCE |
Horde_Imap_Client::STATUS_MESSAGES |
Horde_Imap_Client::STATUS_FORCE_REFRESH;
try {
$status = $imap->status($mbox, $status_flags);
} catch (Horde_Imap_Client_Exception $e) {
// If we can't status the mailbox, assume it's gone.
throw new Horde_ActiveSync_Exception_FolderGone($e);
}
$this->_logger->info(sprintf(
'[%s] IMAP status: %s',
$this->_procid,
serialize($status))
);
$flags = array();
$categories = array();
$modseq = $status[Horde_ActiveSync_Folder_Imap::HIGHESTMODSEQ];
if ($modseq && $folder->modseq() > 0 && $folder->modseq() < $modseq) {
$this->_logger->info(sprintf(
'[%s] CONDSTORE and CHANGES', $this->_procid));
$folder->checkValidity($status);
$query = new Horde_Imap_Client_Fetch_Query();
$query->modseq();
$query->flags();
$query->headerText(array('peek' => true));
try {
$fetch_ret = $imap->fetch($mbox, $query, array(
'changedsince' => $folder->modseq()
));
} catch (Horde_Imap_Client_Exception $e) {
$this->_logger->err($e->getMessage());
throw new Horde_ActiveSync_Exception($e);
}
// Prepare the changes and flags array, ensuring no changes after
// $modseq sneak in yet (they will be caught on the next PING or
// SYNC).
$changes = array();
// Get custom flags to use as categories.
$msgFlags = $this->_getMsgFlags();
foreach ($fetch_ret as $uid => $data) {
if ($options['sincedate']) {
$since = new Horde_Date($options['sincedate']);
$headers = Horde_Mime_Headers::parseHeaders($data->getHeaderText());
try {
$date = new Horde_Date($headers->getValue('Date'));
if ($date->compareDate($since) <= -1) {
// Ignore, it's out of the FILTERTYPE range.
$this->_logger->info(sprintf(
'[%s] Ignoring UID %s since it is outside of the FILTERTYPE (%s)',
$this->_procid,
$uid,
$headers->getValue('Date')));
continue;
}
} catch (Horde_Date_Exception $e) {}
}
if ($data->getModSeq() <= $modseq) {
$changes[] = $uid;
$flags[$uid] = array(
'read' => (array_search(Horde_Imap_Client::FLAG_SEEN, $data->getFlags()) !== false) ? 1 : 0
);
if (($options['protocolversion']) > Horde_ActiveSync::VERSION_TWOFIVE) {
$flags[$uid]['flagged'] = (array_search(Horde_Imap_Client::FLAG_FLAGGED, $data->getFlags()) !== false) ? 1 : 0;
}
if ($options['protocolversion'] > Horde_ActiveSync::VERSION_TWELVEONE) {
$categories[$uid] = array();
foreach ($data->getFlags() as $flag) {
if (!empty($msgFlags[strtolower($flag)])) {
$categories[$uid][] = $msgFlags[strtolower($flag)];
}
}
}
}
}
$folder->setChanges($changes, $flags, $categories);
try {
$deleted = $imap->vanished(
$mbox,
$folder->modseq(),
array('ids' => new Horde_Imap_Client_Ids($folder->messages())));
} catch (Horde_Imap_Client_Excetion $e) {
$this->_logger->err($e->getMessage());
throw new Horde_ActiveSync_Exception($e);
}
$folder->setRemoved($deleted->ids);
if (!empty($options['softdelete']) && !empty($options['sincedate'])) {
$this->_logger->info(sprintf(
'[%s] Polling for SOFTDELETE in %s before %d',
$this->_procid, $folder->serverid(), $options['sincedate']));
$query = new Horde_Imap_Client_Search_Query();
$query->dateSearch(
new Horde_Date($options['sincedate']),
Horde_Imap_Client_Search_Query::DATE_BEFORE);
$query->ids(new Horde_Imap_Client_Ids($folder->messages()));
try {
$search_ret = $imap->search(
$mbox,
$query,
array('results' => array(Horde_Imap_Client::SEARCH_RESULTS_MATCH)));
} catch (Horde_Imap_Client_Exception $e) {
$this->_logger->err($e->getMessage());
throw new Horde_ActiveSync_Exception($e);
}
if ($search_ret['count']) {
$this->_logger->info(sprintf(
'[%s] Found %d messages to SOFTDELETE.',
$this->_procid, count($search_ret['match']->ids)));
$folder->setSoftDeleted($search_ret['match']->ids);
} else {
$this->_logger->info(sprintf(
'[%s] Found NO messages to SOFTDELETE.',
$this->_procid));
}
$folder->setSoftDeleteTimes($options['sincedate'], time());
}
} elseif ($folder->uidnext() == 0) {
$this->_logger->info(sprintf(
'[%s] INITIAL SYNC', $this->_procid));
$query = new Horde_Imap_Client_Search_Query();
if (!empty($options['sincedate'])) {
$query->dateSearch(
new Horde_Date($options['sincedate']),
Horde_Imap_Client_Search_Query::DATE_SINCE);
}
$search_ret = $imap->search(
$mbox,
$query,
array('results' => array(Horde_Imap_Client::SEARCH_RESULTS_MATCH)));
if ($modseq && $folder->modseq() > 0 && $search_ret['count']) {
$folder->setChanges($search_ret['match']->ids, array());
} elseif (count($search_ret['match']->ids)) {
$query = new Horde_Imap_Client_Fetch_Query();
$query->flags();
$fetch_ret = $imap->fetch($mbox, $query, array('ids' => $search_ret['match']));
foreach ($fetch_ret as $uid => $data) {
$flags[$uid] = array(
'read' => (array_search(Horde_Imap_Client::FLAG_SEEN, $data->getFlags()) !== false) ? 1 : 0
);
if (($options['protocolversion']) > Horde_ActiveSync::VERSION_TWOFIVE) {
$flags[$uid]['flagged'] = (array_search(Horde_Imap_Client::FLAG_FLAGGED, $data->getFlags()) !== false) ? 1 : 0;
}
}
$folder->setChanges($search_ret['match']->ids, $flags);
}
} elseif ($modseq == 0) {
$this->_logger->info(sprintf(
'[%s] NO CONDSTORE or per mailbox MODSEQ. minuid: %s, total_messages: %s',
$this->_procid, $folder->minuid(), $status['messages']));
$folder->checkValidity($status);
$query = new Horde_Imap_Client_Search_Query();
if (!empty($options['sincedate'])) {
$query->dateSearch(
new Horde_Date($options['sincedate']),
Horde_Imap_Client_Search_Query::DATE_SINCE);
}
try {
$search_ret = $imap->search(
$mbox,
$query,
array('results' => array(Horde_Imap_Client::SEARCH_RESULTS_MATCH)));
} catch (Horde_Imap_Client_Exception $e) {
$this->_logger->err($e->getMessage());
throw new Horde_ActiveSync_Exception($e);
}
if (count($search_ret['match']->ids)) {
// Update flags.
$query = new Horde_Imap_Client_Fetch_Query();
$query->flags();
try {
$fetch_ret = $imap->fetch($mbox, $query, array('ids' => $search_ret['match']));
} catch (Horde_Imap_Client_Exception $e) {
$this->_logger->err($e->getMessage());
throw new Horde_ActiveSync_Exception($e);
}
foreach ($fetch_ret as $uid => $data) {
$flags[$uid] = array(
'read' => (array_search(Horde_Imap_Client::FLAG_SEEN, $data->getFlags()) !== false) ? 1 : 0
);
if (($options['protocolversion']) > Horde_ActiveSync::VERSION_TWOFIVE) {
$flags[$uid]['flagged'] = (array_search(Horde_Imap_Client::FLAG_FLAGGED, $data->getFlags()) !== false) ? 1 : 0;
}
}
$folder->setChanges($search_ret['match']->ids, $flags);
}
$folder->setRemoved($imap->vanished($mbox, null, array('ids' => new Horde_Imap_Client_Ids($folder->messages())))->ids);
} elseif ($modseq > 0 && $folder->modseq() == 0) {
throw new Horde_ActiveSync_Exception_StaleState('Transition to MODSEQ enabled server');
}
$folder->setStatus($status);
return $folder;
}
/**
* Return AS mail messages, from the given IMAP UIDs.
*
* @param string $folderid The mailbox folder.
* @param array $messages List of IMAP message UIDs
* @param array $options Additional Options:
* - truncation: (integer) The truncation constant, if sent from device.
* DEFAULT: false (No truncation).
* - bodyprefs: (array) The bodypref settings, if sent from device.
* DEFAULT: none (No body prefs sent, or enforced).
* - bodypartprefs: (array) The bodypartprefs settings, if present.
* - mimesupport: (integer) Indicates if MIME is supported or not.
* Possible values: 0 - Not supported 1 - Only S/MIME or
* 2 - All MIME.
* DEFAULT: 0 (No MIME support)
* - protocolversion: (float) The EAS protocol version to support.
* DEFAULT: 2.5
*
* @return array An array of Horde_ActiveSync_Message_Mail objects.
*/
public function getMessages($folderid, array $messages, array $options = array())
{
$mbox = new Horde_Imap_Client_Mailbox($folderid);
$results = $this->_getMailMessages($mbox, $messages, array('headers' => true, 'envelope' => true));
$ret = array();
foreach ($results as $data) {
if ($data->exists(Horde_Imap_Client::FETCH_STRUCTURE)) {
try {
$ret[] = $this->_buildMailMessage($mbox, $data, $options);
} catch (Horde_Exception_NotFound $e) {
}
}
}
return $ret;
}
/**
* Return an array of message UIDs from a list of Message-IDs.
*
* @param string $mid The Message-ID
* @param Horde_ActiveSync_Folder_Imap $folder The folder object to search.
*
* @return integer The UID
* @throws Horde_Exception_NotFound
*/
public function getUidFromMid($mid, Horde_ActiveSync_Folder_Imap $folder)
{
$iids = new Horde_Imap_Client_Ids(array_diff($folder->messages(), $folder->removed()));
$search_q = new Horde_Imap_Client_Search_Query();
$search_q->ids($iids);
$search_q->headerText('Message-ID', $mid);
$mbox = new Horde_Imap_Client_Mailbox($folder->serverid());
$results = $this->_getImapOb()->search($mbox, $search_q);
$uid = $results['match']->ids;
if (!empty($uid)) {
return current($uid);
}
throw new Horde_Exception_NotFound('Message not found.');
}
/**
* Attempt to find a Message-ID in a list of mail folders.
*
* @return array An array with the 0 element being the mbox
* @throws Horde_Exception_NotFound, Horde_ActiveSync_Exception
*
* @deprecated This is unused and should be removed.
*/
public function getUidFromMidInFolders($id, array $folders)
{
$search_q = new Horde_Imap_Client_Search_Query();
$search_q->headerText('Message-ID', $id);
foreach ($folders as $folder) {
$mbox = new Horde_Imap_Client_Mailbox($folder->_serverid);
try {
$results = $this->_getImapOb()->search($mbox, $search_q);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_ActiveSync_Exception($e->getMessage());
}
$uid = $results['match']->ids;
if (!empty($uid)) {
return array($mbox, current($uid));
}
}
throw new Horde_Exception_NotFound('Message not found.');
}
/**
* Move a mail message
*
* @param string $folderid The existing folderid.
* @param array $ids The message UIDs of the messages to move.
* @param string $newfolderid The folder id to move $id to.
*
* @return array An hash of oldUID => newUID.
*
* @throws Horde_ActiveSync_Exception
*/
public function moveMessage($folderid, array $ids, $newfolderid)
{
$imap = $this->_getImapOb();
$from = new Horde_Imap_Client_Mailbox($folderid);
$to = new Horde_Imap_Client_Mailbox($newfolderid);
$ids_obj = new Horde_Imap_Client_Ids($ids);
// Need to ensure the source message exists so we may properly notify
// the client of the error.
$search_q = new Horde_Imap_Client_Search_Query();
$search_q->ids($ids_obj);
$fetch_res = $imap->search($from, $search_q);
if ($fetch_res['count'] != count($ids)) {
$ids_obj = $fetch_res['match'];
}
try {
return $imap->copy($from, $to, array('ids' => $ids_obj, 'move' => true, 'force_map' => true));
} catch (Horde_Imap_Client_Exception $e) {
// We already got rid of the missing ids, must be something else.
$this->_logger->err($e->getMessage());
throw new Horde_ActiveSync_Exception($e);
}
}
/**
* Ping a mailbox. This detects only if any new messages have arrived in
* the specified mailbox.
*
* @param Horde_ActiveSync_Folder_Imap $folder The folder object.
*
* @return boolean True if changes were detected, otherwise false.
* @throws Horde_ActiveSync_Exception, Horde_ActiveSync_Exception_FolderGone
*/
public function ping(Horde_ActiveSync_Folder_Imap $folder)
{
$imap = $this->_getImapOb();
$mbox = new Horde_Imap_Client_Mailbox($folder->serverid());
// Note: non-CONDSTORE servers will return a highestmodseq of 0
$status_flags = Horde_Imap_Client::STATUS_HIGHESTMODSEQ |
Horde_Imap_Client::STATUS_UIDNEXT_FORCE |
Horde_Imap_Client::STATUS_MESSAGES |
Horde_Imap_Client::STATUS_FORCE_REFRESH;
// Get IMAP status.
try {
$status = $imap->status($mbox, $status_flags);
} catch (Horde_Imap_Client_Exception $e) {
// See if the folder disappeared.
if (!$this->_mailboxExists($mbox->utf8)) {
throw new Horde_ActiveSync_Exception_FolderGone();
}
throw new Horde_ActiveSync_Exception($e);
}
// If we have per mailbox MODSEQ then we can pick up flag changes too.
$modseq = $status[Horde_ActiveSync_Folder_Imap::HIGHESTMODSEQ];
if ($modseq && $folder->modseq() > 0 && $folder->modseq() < $modseq) {
return true;
}
// Increase in UIDNEXT is always a positive PING.
if ($folder->uidnext() < $status['uidnext']) {
return true;
}
// If the message count changes, something certainly changed.
if ($folder->total_messages() != $status['messages']) {
return true;
}
// Otherwise, no PING detectable changes present.
return false;
}
/**
* Perform a search from a search mailbox request.
*
* @param array $query The query array.
*
* @return array An array of 'uniqueid', 'searchfolderid' hashes.
*/
public function queryMailbox($query)
{
return $this->_doQuery($query['query']);
}
/**
* Rename a mailbox
*
* @param string $old The old mailbox name.
* @param string $new The new mailbox name.
* @param string $parent The parent mailbox, if any.
*
* @return string The new serverid for the mailbox.
* @since 2.9.0
* @throws Horde_ActiveSync_Exception
*/
public function renameMailbox($old, $new, $parent = null)
{
if ($old == $new) {
return;
}
$imap = $this->_getImapOb();
if (!empty($parent)) {
$ns = $this->_defaultNamespace();
$new = $parent . $ns['delimiter'] . $new;
}
$new_mbox = new Horde_Imap_Client_Mailbox($new);
try {
$imap->renameMailbox(
new Horde_Imap_Client_Mailbox($old),
$new_mbox
);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_ActiveSync_Exception($e);
}
return $new_mbox->utf8;
}
/**
* Set a IMAP message flag.
*
* @param string $mailbox The mailbox name.
* @param integer $uid The message UID.
* @param string $flag The flag to set. A Horde_ActiveSync:: constant.
*
* @throws Horde_ActiveSync_Exception
*/
public function setImapFlag($mailbox, $uid, $flag)
{
$mbox = new Horde_Imap_Client_Mailbox($mailbox);
$options = array(
'ids' => new Horde_Imap_Client_Ids(array($uid))
);
switch ($flag) {
case Horde_ActiveSync::IMAP_FLAG_REPLY:
$options['add'] = array(Horde_Imap_Client::FLAG_ANSWERED);
break;
case Horde_ActiveSync::IMAP_FLAG_FORWARD:
$options['add'] = array(Horde_Imap_Client::FLAG_FORWARDED);
}
try {
$this->_getImapOb()->store($mbox, $options);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_ActiveSync_Exception($e);
}
}
/**
* Set this instance's logger.
*
* @param Horde_Log_Logger $logger The logger.
*/
public function setLogger(Horde_Log_Logger $logger)
{
$this->_logger = $logger;
}
/**
* Set a POOMMAIL_FLAG on a mail message. This method differs from
* setReadFlag() in that it is passed a Flag object, which contains
* other data beside the seen status. Used for setting flagged for followup
* and potentially creating tasks based on the email.
*
* @param string $mailbox The mailbox name.
* @param integer $uid The message uid.
* @param Horde_ActiveSync_Message_Flag $flag The flag
*
* @throws Horde_ActiveSync_Exception
*/
public function setMessageFlag($mailbox, $uid, $flag)
{
// There is no standard in EAS for the name of flags, so it is impossible
// to map flagtype to an actual message flag. Until a better solution
// is thought of, just always use \flagged. There is also no meaning
// of a "completed" flag/task in IMAP email, so if it's not active,
// clear the flag.
$mbox = new Horde_Imap_Client_Mailbox($mailbox);
$options = array(
'ids' => new Horde_Imap_Client_Ids(array($uid)),
);
switch ($flag->flagstatus) {
case Horde_ActiveSync_Message_Flag::FLAG_STATUS_ACTIVE:
$options['add'] = array(Horde_Imap_Client::FLAG_FLAGGED);
break;
default:
$options['remove'] = array(Horde_Imap_Client::FLAG_FLAGGED);
}
$imap = $this->_getImapOb();
try {
$imap->store($mbox, $options);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_ActiveSync_Exception($e);
}
}
public function categoriesToFlags($mailbox, $categories, $uid)
{
$msgFlags = $this->_getMsgFlags();
$mbox = new Horde_Imap_Client_Mailbox($mailbox);
$options = array(
'ids' => new Horde_Imap_Client_Ids(array($uid)),
'add' => array()
);
foreach ($categories as $category) {
// Do our best to make sure the imap flag is a RFC 3501 compliant.
$atom = new Horde_Imap_Client_Data_Format_Atom(strtr(Horde_String_Transliterate::toAscii($category), ' ', '_'));
$imapflag = strtolower($atom->stripNonAtomCharacters());
if (!empty($msgFlags[$imapflag])) {
$options['add'][] = $imapflag;
unset($msgFlags[$imapflag]);
}
}
$options['remove'] = array_keys($msgFlags);
try {
$this->_getImapOb()->store($mbox, $options);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_ActiveSync_Exception($e);
}
}
/**
* Set the message's read status.
*
* @param string $mailbox The mailbox name.
* @param string $uid The message uid.
* @param integer $flag Horde_ActiveSync_Message_Mail::FLAG_* constant
*
* @throws Horde_ActiveSync_Exception
*/
public function setReadFlag($mailbox, $uid, $flag)
{
$mbox = new Horde_Imap_Client_Mailbox($mailbox);
$options = array(
'ids' => new Horde_Imap_Client_Ids(array($uid)),
);
if ($flag == Horde_ActiveSync_Message_Mail::FLAG_READ_SEEN) {
$options['add'] = array(Horde_Imap_Client::FLAG_SEEN);
} else if ($flag == Horde_ActiveSync_Message_Mail::FLAG_READ_UNSEEN) {
$options['remove'] = array(Horde_Imap_Client::FLAG_SEEN);
}
$imap = $this->_getImapOb();
try {
$imap->store($mbox, $options);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_ActiveSync_Exception($e);
}
}
/**
* Helper to build a subquery
*
* @param array $query A subquery array.
*
* @return Horde_Imap_Client_Search_Query The query object.
*/
protected function _buildSubQuery(array $query)
{
$imap_query = new Horde_Imap_Client_Search_Query();
foreach ($query as $q) {
foreach ($q['value'] as $type => $value) {
switch ($type) {
case Horde_ActiveSync_Message_Mail::POOMMAIL_DATERECEIVED:
if ($q['op'] == Horde_ActiveSync_Request_Search::SEARCH_GREATERTHAN) {
$range = Horde_Imap_Client_Search_Query::DATE_SINCE;
} elseif ($q['op'] == Horde_ActiveSync_Request_Search::SEARCH_LESSTHAN) {
$range = Horde_Imap_Client_Search_Query::DATE_BEFORE;
} else {
$range = Horde_Imap_Client_Search_Query::DATE_ON;
}
$imap_query->dateSearch($value, $range);
break;
case Horde_ActiveSync_Request_Search::SEARCH_FREETEXT:
$imap_query->text($value);
break;
}
}
}
return $imap_query;
}
/**
* Builds a proper AS mail message object.
*
* @param Horde_Imap_Client_Mailbox $mbox The IMAP mailbox.
* @param Horde_Imap_Client_Data_Fetch $data The fetch results.
* @param array $options Additional Options:
* - truncation: (integer) Truncate the message body to this length.
* DEFAULT: No truncation.
* - bodyprefs: (array) Bodyprefs, if sent from device.
* DEFAULT: none (No body prefs sent or enforced).
* - bodypartprefs: (array) Bodypartprefs, if sent from device.
* DEFAULT: none (No body part prefs sent or enforced).
* - mimesupport: (integer) Indicates if MIME is supported or not.
* Possible values: 0 - Not supported 1 - Only S/MIME or
* 2 - All MIME.
* DEFAULT: 0 (No MIME support)
* - protocolversion: (float) The EAS protocol version to support.
* DEFAULT: 2.5
*
* @return Horde_ActiveSync_Message_Mail The message object suitable for
* streaming to the device.
*/
protected function _buildMailMessage(
Horde_Imap_Client_Mailbox $mbox,
Horde_Imap_Client_Data_Fetch $data,
$options = array())
{
$version = empty($options['protocolversion']) ?
Horde_ActiveSync::VERSION_TWOFIVE :
$options['protocolversion'];
$imap_message = new Horde_ActiveSync_Imap_Message($this->_getImapOb(), $mbox, $data);
$eas_message = Horde_ActiveSync::messageFactory('Mail');
// Build To: data (POOMMAIL_TO has a max length of 32768).
$to = $imap_message->getToAddresses();
$eas_message->to = array_pop($to['to']);
foreach ($to['to'] as $to_atom) {
if (strlen($eas_message->to) + strlen($to_atom) > 32768) {
break;
}
$eas_message->to .= ',' . $to_atom;
}
$eas_message->displayto = implode(';', $to['displayto']);
if (empty($eas_message->displayto)) {
$eas_message->displayto = $eas_message->to;
}
// Ensure we don't send broken UTF8 data to the client. It makes clients
// angry. And we don't like angry clients.
$hdr_charset = $imap_message->getStructure()->getHeaderCharset();
// Fill in other header data
$eas_message->from = $imap_message->getFromAddress();
$eas_message->subject = Horde_ActiveSync_Utils::ensureUtf8($imap_message->getSubject(), $hdr_charset);
$eas_message->threadtopic = $eas_message->subject;
$eas_message->datereceived = $imap_message->getDate();
$eas_message->read = $imap_message->getFlag(Horde_Imap_Client::FLAG_SEEN);
$eas_message->cc = $imap_message->getCc();
$eas_message->reply_to = $imap_message->getReplyTo();
// Default to IPM.Note - may change below depending on message content.
$eas_message->messageclass = 'IPM.Note';
// Codepage id. MS recommends to always set to UTF-8 when possible.
// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756%28v=vs.85%29.aspx
$eas_message->cpid = Horde_ActiveSync_Message_Mail::INTERNET_CPID_UTF8;
// Message importance. First try X-Priority, then Importance since
// Outlook sends the later.
if ($priority = $imap_message->getHeaders()->getValue('X-priority')) {
$priority = preg_replace('/\D+/', '', $priority);
} else {
$priority = $imap_message->getHeaders()->getValue('Importance');
}
$eas_message->importance = $this->_getEASImportance($priority);
// Get the body data.
$mbd = $imap_message->getMessageBodyDataObject($options);
if ($version == Horde_ActiveSync::VERSION_TWOFIVE) {
$eas_message->body = $mbd->plain['body']->stream;
$eas_message->bodysize = $mbd->plain['body']->length(true);
$eas_message->bodytruncated = $mbd->plain['truncated'];
$eas_message->attachments = $imap_message->getAttachments($version);
} else {
// Get the message body and determine original type.
if ($mbd->html) {
$eas_message->airsyncbasenativebodytype = Horde_ActiveSync::BODYPREF_TYPE_HTML;
} else {
$eas_message->airsyncbasenativebodytype = Horde_ActiveSync::BODYPREF_TYPE_PLAIN;
}
$airsync_body = Horde_ActiveSync::messageFactory('AirSyncBaseBody');
$body_type_pref = $mbd->getBodyTypePreference();
if ($body_type_pref == Horde_ActiveSync::BODYPREF_TYPE_MIME) {
$this->_logger->info(sprintf(
'[%s] Sending MIME Message.', $this->_procid));
// ActiveSync *REQUIRES* all data sent to be in UTF-8, so we
// must convert the body parts to UTF-8. Unfortunately if the
// email is signed (or encrypted for that matter) we can't
// alter the data in anyway or the signature will not be
// verified, so we fetch the entire message and hope for the best.
if (!$imap_message->isSigned() && !$imap_message->isEncrypted()) {
$mime = new Horde_Mime_Part();
if ($mbd->plain) {
$plain_mime = new Horde_Mime_Part();
$plain_mime->setType('text/plain');