summaryrefslogtreecommitdiff
path: root/lib/kolab_sync_backend.php
blob: b88c98413ab33085a9bdd436aa3551ad6cdb9ab5 (plain)
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
<?php

/**
 +--------------------------------------------------------------------------+
 | Kolab Sync (ActiveSync for Kolab)                                        |
 |                                                                          |
 | Copyright (C) 2011-2012, Kolab Systems AG <contact@kolabsys.com>         |
 |                                                                          |
 | This program is free software: you can redistribute it and/or modify     |
 | it under the terms of the GNU Affero General Public License as published |
 | by the Free Software Foundation, either version 3 of the License, or     |
 | (at your option) any later version.                                      |
 |                                                                          |
 | This program is distributed in the hope that it will be useful,          |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             |
 | GNU Affero General Public License for more details.                      |
 |                                                                          |
 | You should have received a copy of the GNU Affero General Public License |
 | along with this program. If not, see <http://www.gnu.org/licenses/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 +--------------------------------------------------------------------------+
*/

class kolab_sync_backend
{
    /**
     * Singleton instace of kolab_sync_backend
     *
     * @var kolab_sync_backend
     */
    static protected $instance;

    protected $storage;
    protected $folder_meta;
    protected $folder_uids;
    protected $root_meta;

    static protected $types = array(
        1  => '',
        2  => 'mail.inbox',
        3  => 'mail.drafts',
        4  => 'mail.wastebasket',
        5  => 'mail.sentitems',
        6  => 'mail.outbox',
        7  => 'task.default',
        8  => 'event.default',
        9  => 'contact.default',
        10 => 'note.default',
        11 => 'journal.default',
        12 => 'mail',
        13 => 'event',
        14 => 'contact',
        15 => 'task',
        16 => 'journal',
        17 => 'note',
    );

    static protected $classes = array(
        Syncroton_Data_Factory::CLASS_CALENDAR => 'event',
        Syncroton_Data_Factory::CLASS_CONTACTS => 'contact',
        Syncroton_Data_Factory::CLASS_EMAIL    => 'mail',
        Syncroton_Data_Factory::CLASS_NOTES    => 'note',
        Syncroton_Data_Factory::CLASS_TASKS    => 'task',
    );

    const ROOT_MAILBOX  = 'INBOX';
//    const ROOT_MAILBOX  = '';
    const ASYNC_KEY     = '/private/vendor/kolab/activesync';
    const UID_KEY       = '/shared/vendor/cmu/cyrus-imapd/uniqueid';


    /**
     * This implements the 'singleton' design pattern
     *
     * @return kolab_sync_backend The one and only instance
     */
    static function get_instance()
    {
        if (!self::$instance) {
            self::$instance = new kolab_sync_backend;
            self::$instance->startup();  // init AFTER object was linked with self::$instance
        }

        return self::$instance;
    }


    /**
     * Class initialization
     */
    public function startup()
    {
        $this->storage = rcube::get_instance()->get_storage();

        // @TODO: reset cache? if we do this for every request the cache would be useless
        // There's no session here
        //$this->storage->clear_cache('mailboxes.', true);

        // set additional header used by libkolab
        $this->storage->set_options(array(
            // @TODO: there can be Roundcube plugins defining additional headers,
            // we maybe would need to add them here
            'fetch_headers' => 'X-KOLAB-TYPE X-KOLAB-MIME-VERSION',
            'skip_deleted'  => true,
            'threading'     => false,
        ));

        // Disable paging
        $this->storage->set_pagesize(999999);
    }


    /**
     * List known devices
     *
     * @return array Device list as hash array
     */
    public function devices_list()
    {
        if ($this->root_meta === null) {
            // @TODO: consider server annotation instead of INBOX
            if ($meta = $this->storage->get_metadata(self::ROOT_MAILBOX, self::ASYNC_KEY)) {
                $this->root_meta = $this->unserialize_metadata($meta[self::ROOT_MAILBOX][self::ASYNC_KEY]);
            }
            else {
                $this->root_meta = array();
            }
        }

        if (!empty($this->root_meta['DEVICE']) && is_array($this->root_meta['DEVICE'])) {
            return $this->root_meta['DEVICE'];
        }

        return array();
    }


    /**
     * Get list of folders available for sync
     *
     * @param string $deviceid  Device identifier
     * @param string $type      Folder type
     *
     * @return array|bool List of mailbox folders, False on backend failure
     */
    public function folders_list($deviceid, $type)
    {
        // get all folders of specified type
        $folders = (array) kolab_storage::list_folders('', '*', $type, false, $typedata);

        // get folders activesync config
        $folderdata = $this->folder_meta();

        if (!is_array($folders) || !is_array($folderdata)) {
            return false;
        }

        $folders_list = array();

        // check if folders are "subscribed" for activesync
        foreach ($folderdata as $folder => $meta) {
            if (empty($meta['FOLDER']) || empty($meta['FOLDER'][$deviceid])
                || empty($meta['FOLDER'][$deviceid]['S'])
            ) {
                continue;
            }

            if (!empty($type) && !in_array($folder, $folders)) {
                continue;
            }

            // Activesync folder identifier (serverId)
            $folder_type = $typedata[$folder];
            $folder_id   = self::folder_id($folder, $folder_type);

            $folders_list[$folder_id] = $this->folder_data($folder, $folder_type);
        }

        return $folders_list;
    }

    /**
     * Getter for folder metadata
     *
     * @return array|bool Hash array with meta data for each folder, False on backend failure
     */
    public function folder_meta()
    {
        if (!isset($this->folder_meta)) {
            $this->folder_meta = array();
            // get folders activesync config
            $folderdata = $this->storage->get_metadata("*", self::ASYNC_KEY);

            if (!is_array($folderdata)) {
                return false;
            }

            foreach ($folderdata as $folder => $meta) {
                if ($asyncdata = $meta[self::ASYNC_KEY]) {
                    if ($metadata = $this->unserialize_metadata($asyncdata)) {
                        $this->folder_meta[$folder] = $metadata;
                    }
                }
            }
        }

        return $this->folder_meta;
    }


    /**
     * Creates folder and subscribes to the device
     *
     * @param string $name      Folder name (UTF7-IMAP)
     * @param int    $type      Folder (ActiveSync) type
     * @param string $deviceid  Device identifier
     *
     * @return bool True on success, False on failure
     */
    public function folder_create($name, $type, $deviceid)
    {
        if ($this->storage->folder_exists($name)) {
            $created = true;
        }
        else {
            $type    = self::type_activesync2kolab($type);
            $created = kolab_storage::folder_create($name, $type, true);
        }

        if ($created) {
            // Set ActiveSync subscription flag
            $this->folder_set($name, $deviceid, 1);

            return true;
        }

        return false;
    }


    /**
     * Renames a folder
     *
     * @param string $old_name  Old folder name (UTF7-IMAP)
     * @param string $new_name  New folder name (UTF7-IMAP)
     * @param int    $type      Folder (ActiveSync) type
     * @param string $deviceid  Device identifier
     *
     * @return bool True on success, False on failure
     */
    public function folder_rename($old_name, $new_name, $type, $deviceid)
    {
        $this->folder_meta = null;

        $type = self::type_activesync2kolab($type);

        // don't use kolab_storage for moving mail folders
        if (preg_match('/^mail/', $type)) {
            return $this->storage->rename_folder($old_name, $new_name);
        }
        else {
            return kolab_storage::folder_rename($old_name, $new_name);
        }
    }


    /**
     * Deletes folder
     *
     * @param string $name      Folder name (UTF7-IMAP)
     * @param string $deviceid  Device identifier
     *
     */
    public function folder_delete($name, $deviceid)
    {
        unset($this->folder_meta[$name]);

        return kolab_storage::folder_delete($name);
    }


    /**
     * Sets ActiveSync subscription flag on a folder
     *
     * @param string $name      Folder name (UTF7-IMAP)
     * @param string $deviceid  Device identifier
     * @param int    $flag      Flag value (0|1|2)
     */
    public function folder_set($name, $deviceid, $flag)
    {
        if (empty($deviceid)) {
            return false;
        }

        // get folders activesync config
        $metadata = $this->folder_meta();

        if (!is_array($metadata)) {
            return false;
        }

        $metadata = $metadata[$name];

        if ($flag)  {
            if (empty($metadata)) {
                $metadata = array();
            }

            if (empty($metadata['FOLDER'])) {
                $metadata['FOLDER'] = array();
            }

            if (empty($metadata['FOLDER'][$deviceid])) {
                $metadata['FOLDER'][$deviceid] = array();
            }

            // Z-Push uses:
            //  1 - synchronize, no alarms
            //  2 - synchronize with alarms
            $metadata['FOLDER'][$deviceid]['S'] = $flag;
        }

        if (!$flag) {
            unset($metadata['FOLDER'][$deviceid]['S']);

            if (empty($metadata['FOLDER'][$deviceid])) {
                unset($metadata['FOLDER'][$deviceid]);
            }

            if (empty($metadata['FOLDER'])) {
                unset($metadata['FOLDER']);
            }

            if (empty($metadata)) {
                $metadata = null;
            }
        }

        // Return if nothing's been changed
        if (!self::data_array_diff($this->folder_meta[$name], $metadata)) {
            return true;
        }

        $this->folder_meta[$name] = $metadata;

        return $this->storage->set_metadata($name, array(
            self::ASYNC_KEY => $this->serialize_metadata($metadata)));
    }


    public function device_get($id)
    {
        $devices_list = $this->devices_list();

        $result = $devices_list[$id];

        return $result;
    }

    /**
     * Registers new device on server
     *
     * @param array  $device  Device data
     * @param string $id      Device ID
     *
     * @return bool True on success, False on failure
     */
    public function device_create($device, $id)
    {
        // Fill local cache
        $this->devices_list();

        // Some devices create dummy devices with name "validate" (#1109)
        // This device entry is used in two initial requests, but later
        // the device registers a real name. We can remove this dummy entry
        // on new device creation
        $this->device_delete('validate');

        // Old Kolab_ZPush device parameters
        // MODE:  -1 | 0 | 1  (not set | flatmode | foldermode)
        // TYPE:  device type string
        // ALIAS: user-friendly device name

        // Syncroton (kolab_sync_backend_device) uses
        // ID:    internal identifier in syncroton database
        // TYPE:  device type string
        // ALIAS: user-friendly device name

        $metadata = $this->root_meta;
        $metadata['DEVICE'][$id] = $device;
        $metadata = array(self::ASYNC_KEY => $this->serialize_metadata($metadata));

        $result = $this->storage->set_metadata(self::ROOT_MAILBOX, $metadata);

        if ($result) {
            // Update local cache
            $this->root_meta['DEVICE'][$id] = $device;

            // subscribe default set of folders
            $this->device_init_subscriptions($id);
        }

        return $result;
    }

    /**
     * Device update.
     *
     * @param array  $device  Device data
     * @param string $id      Device ID
     *
     * @return bool True on success, False on failure
     */
    public function device_update($device, $id)
    {
        $devices_list = $this->devices_list();
        $old_device   = $devices_list[$id];

        if (!$old_device) {
            return false;
        }

        // Do nothing if nothing is changed
        if (!self::data_array_diff($old_device, $device)) {
            return true;
        }

        $device = array_merge($old_device, $device);

        $metadata = $this->root_meta;
        $metadata['DEVICE'][$id] = $device;
        $metadata = array(self::ASYNC_KEY => $this->serialize_metadata($metadata));

        $result = $this->storage->set_metadata(self::ROOT_MAILBOX, $metadata);

        if ($result) {
            // Update local cache
            $this->root_meta['DEVICE'][$id] = $device;
        }

        return $result;
    }


    /**
     * Device delete.
     *
     * @param string $id  Device ID
     *
     * @return bool True on success, False on failure
     */
    public function device_delete($id)
    {
        $device = $this->device_get($id);

        if (!$device) {
            return false;
        }

        unset($this->root_meta['DEVICE'][$id], $this->root_meta['FOLDER'][$id]);

        if (empty($this->root_meta['DEVICE'])) {
            unset($this->root_meta['DEVICE']);
        }
        if (empty($this->root_meta['FOLDER'])) {
            unset($this->root_meta['FOLDER']);
        }

        $metadata = $this->serialize_metadata($this->root_meta);
        $metadata = array(self::ASYNC_KEY => $metadata);

        // update meta data
        $result = $this->storage->set_metadata(self::ROOT_MAILBOX, $metadata);

        if ($result) {
            // remove device annotation for every folder
            foreach ($this->folder_meta() as $folder => $meta) {
                // skip root folder (already handled above)
                if ($folder == self::ROOT_MAILBOX)
                    continue;

                if (!empty($meta['FOLDER']) && isset($meta['FOLDER'][$id])) {
                    unset($meta['FOLDER'][$id]);

                    if (empty($meta['FOLDER'])) {
                        unset($this->folder_meta[$folder]['FOLDER']);
                        unset($meta['FOLDER']);
                    }
                    if (empty($meta)) {
                        unset($this->folder_meta[$folder]);
                        $meta = null;
                    }

                    $metadata = array(self::ASYNC_KEY => $this->serialize_metadata($meta));
                    $res = $this->storage->set_metadata($folder, $metadata);

                    if ($res && $meta) {
                        $this->folder_meta[$folder] = $meta;
                    }
                }
            }
        }

        return $result;
    }

    /**
     * Subscribe default set of folders on device registration
     */
    private function device_init_subscriptions($deviceid)
    {
        // INBOX always exists
        $this->folder_set('INBOX', $deviceid, 1);

        $supported_types = array(
            'mail.drafts',
            'mail.wastebasket',
            'mail.sentitems',
            'mail.outbox',
            'event.default',
            'contact.default',
            'note.default',
            'task.default',
            'event',
            'contact',
            'note',
            'task'
        );

        // This default set can be extended by adding following values:
        $modes = array(
            'SUB_PERSONAL' => 1, // all subscribed folders in personal namespace
            'ALL_PERSONAL' => 2, // all folders in personal namespace
            'SUB_OTHER'    => 4, // all subscribed folders in other users namespace
            'ALL_OTHER'    => 8, // all folders in other users namespace
            'SUB_SHARED'   => 16, // all subscribed folders in shared namespace
            'ALL_SHARED'   => 32, // all folders in shared namespace
        );

        $rcube   = rcube::get_instance();
        $config  = $rcube->config;
        $mode    = (int) $config->get('activesync_init_subscriptions');
        $folders = array();

        // Subscribe to default folders
        $foldertypes = kolab_storage::folders_typedata();

        if (!empty($foldertypes)) {
            $_foldertypes = array_intersect($foldertypes, $supported_types);

            // get default folders
            foreach ($_foldertypes as $folder => $type) {
                // only personal folders
                if ($this->storage->folder_namespace($folder) == 'personal') {
                    $flag = preg_match('/^(event|task)/', $type) ? 2 : 1;
                    $this->folder_set($folder, $deviceid, $flag);
                    $folders[] = $folder;
                }
            }
        }

        // we're in default mode, exit
        if (!$mode) {
            return;
        }

        // below we support additionally all mail folders
        $supported_types[] = 'mail';
        $supported_types[] = 'mail.junkemail';

        // get configured special folders
        $special_folders = array();
        $map             = array(
            'drafts' => 'mail.drafts',
            'junk'   => 'mail.junkemail',
            'sent'   => 'mail.sentitems',
            'trash'  => 'mail.wastebasket',
        );

        foreach ($map as $folder => $type) {
            if ($folder = $config->get($folder . '_mbox')) {
                $special_folders[$folder] = $type;
            }
        }

        // get folders list(s)
        if (($mode & $modes['ALL_PERSONAL']) || ($mode & $modes['ALL_OTHER']) || ($mode & $modes['ALL_SHARED'])) {
            $all_folders = $this->storage->list_folders();
            if (($mode & $modes['SUB_PERSONAL']) || ($mode & $modes['SUB_OTHER']) || ($mode & $modes['SUB_SHARED'])) {
                $subscribed_folders = $this->storage->list_folders_subscribed();
            }
        }
        else {
            $all_folders = $this->storage->list_folders_subscribed();
        }

        foreach ($all_folders as $folder) {
            // folder already subscribed
            if (in_array($folder, $folders)) {
                continue;
            }

            $type = $foldertypes[$folder] ?: 'mail';
            if ($type == 'mail' && isset($special_folders[$folder])) {
                $type = $special_folders[$folder];
            }

            if (!in_array($type, $supported_types)) {
                continue;
            }

            $ns = strtoupper($this->storage->folder_namespace($folder));

            // subscribe the folder according to configured mode
            // and folder namespace/subscription status
            if (($mode & $modes["ALL_$ns"])
                || (($mode & $modes["SUB_$ns"])
                    && (!isset($subscribed_folders) || in_array($folder, $subscribed_folders)))
            ) {
                $flag = preg_match('/^(event|task)/', $type) ? 2 : 1;
                $this->folder_set($folder, $deviceid, $flag);
            }
        }
    }

    /**
     * Helper method to decode saved IMAP metadata
     */
    private function unserialize_metadata($str)
    {
        if (!empty($str)) {
            // Support old Z-Push annotation format
            if ($str[0] != '{') {
                $str = base64_decode($str);
            }
            $data = json_decode($str, true);
            return $data;
        }

        return null;
    }

    /**
     * Helper method to encode IMAP metadata for saving
     */
    private function serialize_metadata($data)
    {
        if (!empty($data) && is_array($data)) {
            $data = json_encode($data);
//            $data = base64_encode($data);
            return $data;
        }

        return null;
    }

    /**
     * Returns Kolab folder type for specified ActiveSync type ID
     */
    public static function type_activesync2kolab($type)
    {
        if (!empty(self::$types[$type])) {
            return self::$types[$type];
        }

        return '';
    }

    /**
     * Returns ActiveSync folder type for specified Kolab type
     */
    public static function type_kolab2activesync($type)
    {
        if ($key = array_search($type, self::$types)) {
            return $key;
        }

        return key(self::$types);
    }

    /**
     * Returns Kolab folder type for specified ActiveSync class name
     */
    public static function class_activesync2kolab($class)
    {
        if (!empty(self::$classes[$class])) {
            return self::$classes[$class];
        }

        return '';
    }

    private function folder_data($folder, $type)
    {
        // Folder name parameters
        $delim = $this->storage->get_hierarchy_delimiter();
        $items = explode($delim, $folder);
        $name  = array_pop($items);

        // Folder UID
        $folder_id = $this->folder_id($folder, $type);

        // Folder type
        $type = self::type_kolab2activesync($type);
        // fix type, if there's no type annotation it's detected as UNKNOWN
        // we'll use 'mail' (12) or 'mail.inbox' (2)
        if ($type == 1) {
            $type = $folder == 'INBOX' ? 2 : 12;
        }

        // Syncroton folder data array
        return array(
            'serverId'    => $folder_id,
            'parentId'    => count($items) ? self::folder_id(implode($delim, $items)) : 0,
            'displayName' => rcube_charset::convert($name, 'UTF7-IMAP', kolab_sync::CHARSET),
            'type'        => $type,
        );
    }

    /**
     * Builds folder ID based on folder name
     */
    public function folder_id($name, $type = null)
    {
        // ActiveSync expects folder identifiers to be max.64 characters
        // So we can't use just folder name

        if ($name === '' || !is_string($name)) {
            return null;
        }

        if (isset($this->folder_uids[$name])) {
            return $this->folder_uids[$name];
        }

/*
        @TODO: For now uniqueid annotation doesn't work, we will create UIDs by ourselves.
               There's one inconvenience of this solution: folder name/type change
               would be handled in ActiveSync as delete + create.

        // get folders unique identifier
        $folderdata = $this->storage->get_metadata($name, self::UID_KEY);

        if ($folderdata && !empty($folderdata[$name])) {
            $uid = $folderdata[$name][self::UID_KEY];
            return $this->folder_uids[$name] = $uid;
        }
*/
        // Add type to folder UID hash, so type change can be detected by Syncroton
        $uid = $name . '!!' . ($type !== null ? $type : kolab_storage::folder_type($name));
        $uid = md5($uid);

        return $this->folder_uids[$name] = $uid;
    }

    /**
     * Returns IMAP folder name
     *
     * @param string $id        Folder identifier
     * @param string $deviceid  Device dentifier
     *
     * @return string Folder name (UTF7-IMAP)
     */
    public function folder_id2name($id, $deviceid)
    {
        // check in cache first
        if (!empty($this->folder_uids)) {
            if (($name = array_search($id, $this->folder_uids)) !== false) {
                return $name;
            }
        }

/*
        @TODO: see folder_id()

        // get folders unique identifier
        $folderdata = $this->storage->get_metadata('*', self::UID_KEY);

        foreach ((array)$folderdata as $folder => $data) {
            if (!empty($data[self::UID_KEY])) {
                $uid = $data[self::UID_KEY];
                $this->folder_uids[$folder] = $uid;
                if ($uid == $id) {
                    $name = $folder;
                }
            }
        }
*/
        // get all folders of specified type
        $folderdata = $this->folder_meta();

        if (!is_array($folderdata)) {
            return null;
        }

        // check if folders are "subscribed" for activesync
        foreach ($folderdata as $folder => $meta) {
            if (empty($meta['FOLDER']) || empty($meta['FOLDER'][$deviceid])
                || empty($meta['FOLDER'][$deviceid]['S'])
            ) {
                continue;
            }

            $uid = self::folder_id($folder);
            $this->folder_uids[$folder] = $uid;

            if ($uid == $id) {
                $name = $folder;
            }
        }

        return $name;
    }

    /**
     */
    public function modseq_set($deviceid, $folderid, $synctime, $data)
    {
        $synctime = $synctime->format('Y-m-d H:i:s');
        $rcube    = rcube::get_instance();
        $db       = $rcube->get_dbh();
        $old_data = $this->modseq[$folderid][$synctime];

        if (empty($old_data)) {
            $this->modseq[$folderid][$synctime] = $data;
            $data = json_encode($data);

            $db->set_option('ignore_key_errors', true);
            $db->query("INSERT INTO `syncroton_modseq` (`device_id`, `folder_id`, `synctime`, `data`)"
                ." VALUES (?, ?, ?, ?)",
                $deviceid, $folderid, $synctime, $data);
            $db->set_option('ignore_key_errors', false);
        }
    }

    public function modseq_get($deviceid, $folderid, $synctime)
    {
        $synctime = $synctime->format('Y-m-d H:i:s');

        if (empty($this->modseq[$folderid][$synctime])) {
            $this->modseq[$folderid] = array();

            $rcube = rcube::get_instance();
            $db    = $rcube->get_dbh();

            $db->limitquery("SELECT `data`, `synctime` FROM `syncroton_modseq`"
                ." WHERE `device_id` = ? AND `folder_id` = ? AND `synctime` <= ?"
                ." ORDER BY `synctime` DESC",
                0, 1, $deviceid, $folderid, $synctime);

            if ($row = $db->fetch_assoc()) {
                $synctime = $row['synctime'];
                // @TODO: make sure synctime from sql is in "Y-m-d H:i:s" format
                $this->modseq[$folderid][$synctime] = json_decode($row['data']);
            }

            // Cleanup: remove all records except the current one
            $db->query("DELETE FROM `syncroton_modseq`"
                ." WHERE `device_id` = ? AND `folder_id` = ? AND `synctime` <> ?",
                $deviceid, $folderid, $synctime);
        }

        return @$this->modseq[$folderid][$synctime];
    }

    /**
     * Compares two arrays
     *
     * @param array $array1
     * @param array $array2
     *
     * @return bool True if arrays differs, False otherwise
     */
    private static function data_array_diff($array1, $array2)
    {
        if (!is_array($array1) || !is_array($array2)) {
            return $array1 != $array2;
        }

        if (count($array1) != count($array2)) {
            return true;
        }

        foreach ($array1 as $key => $val) {
            if (!array_key_exists($key, $array2)) {
                return true;
            }
            if ($val !== $array2[$key]) {
                return true;
            }
        }

        return false;
    }
}