summaryrefslogtreecommitdiff
path: root/plugins/libkolab/lib/kolab_storage.php
blob: 5b8eca7c05478d5d891429644b0195c02b6a48f4 (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
<?php

/**
 * Kolab storage class providing static methods to access groupware objects on a Kolab server.
 *
 * @version @package_version@
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * Copyright (C) 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/>.
 */

class kolab_storage
{
    const CTYPE_KEY = '/shared/vendor/kolab/folder-type';
    const CTYPE_KEY_PRIVATE = '/private/vendor/kolab/folder-type';
    const COLOR_KEY_SHARED = '/shared/vendor/kolab/color';
    const COLOR_KEY_PRIVATE = '/private/vendor/kolab/color';
    const SERVERSIDE_SUBSCRIPTION = 0;
    const CLIENTSIDE_SUBSCRIPTION = 1;

    public static $version = 2.0;
    public static $last_error;

    private static $ready = false;
    private static $config;
    private static $cache;
    private static $imap;


    /**
     * Setup the environment needed by the libs
     */
    public static function setup()
    {
        if (self::$ready)
            return true;

        $rcmail = rcube::get_instance();
        self::$config = $rcmail->config;
        self::$imap = $rcmail->get_storage();
        self::$ready = class_exists('Horde_Kolab_Format') &&
            (self::$imap->get_capability('METADATA') || self::$imap->get_capability('ANNOTATEMORE') || self::$imap->get_capability('ANNOTATEMORE2'));

        if (self::$ready) {
            // set imap options
            self::$imap->set_options(array(
                'skip_deleted' => true,
                'threading' => false,
            ));
            self::$imap->set_pagesize(9999);
        }

        return self::$ready;
    }


    /**
     * Get a list of storage folders for the given data type
     *
     * @param string Data type to list folders for (contact,distribution-list,event,task,note)
     *
     * @return array List of Kolab_Folder objects (folder names in UTF7-IMAP)
     */
    public static function get_folders($type)
    {
        $folders = $folderdata = array();

        if (self::setup()) {
            foreach ((array)self::list_folders('', '*', $type, false, $folderdata) as $foldername) {
                $folders[$foldername] = new kolab_storage_folder($foldername, $folderdata[$foldername]);
            }
        }

        return $folders;
    }


    /**
     * Getter for a specific storage folder
     *
     * @param string  IMAP folder to access (UTF7-IMAP)
     * @return object kolab_storage_folder  The folder object
     */
    public static function get_folder($folder)
    {
        return self::setup() ? new kolab_storage_folder($folder) : null;
    }


    /**
     * Getter for a single Kolab object, identified by its UID.
     * This will search all folders storing objects of the given type.
     *
     * @param string Object UID
     * @param string Object type (contact,distribution-list,event,task,note)
     * @return array The Kolab object represented as hash array or false if not found
     */
    public static function get_object($uid, $type)
    {
        self::setup();
        $folder = null;
        foreach ((array)self::list_folders('', '*', $type) as $foldername) {
            if (!$folder)
                $folder = new kolab_storage_folder($foldername);
            else
                $folder->set_folder($foldername);

            if ($object = $folder->get_object($uid))
                return $object;
        }

        return false;
    }


    /**
     *
     */
    public static function get_freebusy_server()
    {
        return unslashify(self::$config->get('kolab_freebusy_server', 'https://' . $_SESSION['imap_host'] . '/freebusy'));
    }


    /**
     * Compose an URL to query the free/busy status for the given user
     */
    public static function get_freebusy_url($email)
    {
        return self::get_freebusy_server() . '/' . $email . '.ifb';
    }


    /**
     * Creates folder ID from folder name
     *
     * @param string $folder Folder name (UTF7-IMAP)
     *
     * @return string Folder ID string
     */
    public static function folder_id($folder)
    {
        return asciiwords(strtr($folder, '/.-', '___'));
    }


    /**
     * Deletes IMAP folder
     *
     * @param string $name Folder name (UTF7-IMAP)
     *
     * @return bool True on success, false on failure
     */
    public static function folder_delete($name)
    {
        // clear cached entries first
        if ($folder = self::get_folder($name))
            $folder->cache->purge();

        $success = self::$imap->delete_folder($name);
        self::$last_error = self::$imap->get_error_str();

        return $success;
    }

    /**
     * Creates IMAP folder
     *
     * @param string $name        Folder name (UTF7-IMAP)
     * @param string $type        Folder type
     * @param bool   $subscribed  Sets folder subscription
     *
     * @return bool True on success, false on failure
     */
    public static function folder_create($name, $type = null, $subscribed = false)
    {
        self::setup();

        if ($saved = self::$imap->create_folder($name, $subscribed)) {
            // set metadata for folder type
            if ($type) {
                $saved = self::set_folder_type($name, $type);

                // revert if metadata could not be set
                if (!$saved) {
                    self::$imap->delete_folder($name);
                }
            }
        }

        if ($saved) {
            return true;
        }

        self::$last_error = self::$imap->get_error_str();
        return false;
    }

    /**
     * Renames IMAP folder
     *
     * @param string $oldname Old folder name (UTF7-IMAP)
     * @param string $newname New folder name (UTF7-IMAP)
     *
     * @return bool True on success, false on failure
     */
    public static function folder_rename($oldname, $newname)
    {
        self::setup();

        $success = self::$imap->rename_folder($oldname, $newname);
        self::$last_error = self::$imap->get_error_str();

        return $success;
    }


    /**
     * Rename or Create a new IMAP folder.
     *
     * Does additional checks for permissions and folder name restrictions
     *
     * @param array Hash array with folder properties and metadata
     *  - name: Folder name
     *  - oldname: Old folder name when changed
     *  - parent: Parent folder to create the new one in
     *  - type: Folder type to create
     * @return mixed New folder name or False on failure
     */
    public static function folder_update(&$prop)
    {
        self::setup();

        $folder    = rcube_charset::convert($prop['name'], RCMAIL_CHARSET, 'UTF7-IMAP');
        $oldfolder = $prop['oldname']; // UTF7
        $parent    = $prop['parent']; // UTF7
        $delimiter = self::$imap->get_hierarchy_delimiter();

        if (strlen($oldfolder)) {
            $options = self::$imap->folder_info($oldfolder);
        }

        if (!empty($options) && ($options['norename'] || $options['protected'])) {
        }
        // sanity checks (from steps/settings/save_folder.inc)
        else if (!strlen($folder)) {
            self::$last_error = 'cannotbeempty';
            return false;
        }
        else if (strlen($folder) > 128) {
            self::$last_error = 'nametoolong';
            return false;
        }
        else {
            // these characters are problematic e.g. when used in LIST/LSUB
            foreach (array($delimiter, '%', '*') as $char) {
                if (strpos($folder, $delimiter) !== false) {
                    self::$last_error = 'forbiddencharacter';
                    return false;
                }
            }
        }

        if (!empty($options) && ($options['protected'] || $options['norename'])) {
            $folder = $oldfolder;
        }
        else if (strlen($parent)) {
            $folder = $parent . $delimiter . $folder;
        }
        else {
            // add namespace prefix (when needed)
            $folder = self::$imap->mod_folder($folder, 'in');
        }

        // Check access rights to the parent folder
        if (strlen($parent) && (!strlen($oldfolder) || $oldfolder != $folder)) {
            $parent_opts = self::$imap->folder_info($parent);
            if ($parent_opts['namespace'] != 'personal'
                && (empty($parent_opts['rights']) || !preg_match('/[ck]/', implode($parent_opts['rights'])))
            ) {
                self::$last_error = 'No permission to create folder';
                return false;
          }
        }

        // update the folder name
        if (strlen($oldfolder)) {
            if ($oldfolder != $folder) {
                $result = self::folder_rename($oldfolder, $folder);
          }
          else
              $result = true;
        }
        // create new folder
        else {
            $result = self::folder_create($folder, $prop['type'], $prop['subscribed'] === self::SERVERSIDE_SUBSCRIPTION);
        }

        // save color in METADATA
        // TODO: also save 'showalarams' and other properties here

        if ($result && $prop['color']) {
            $meta_saved = false;
            $ns = self::$imap->folder_namespace($folder);
            if ($ns == 'personal')  // save in shared namespace for personal folders
                $meta_saved = self::$imap->set_metadata($folder, array(self::COLOR_KEY_SHARED => $prop['color']));
            if (!$meta_saved)    // try in private namespace
                $meta_saved = self::$imap->set_metadata($folder, array(self::COLOR_KEY_PRIVATE => $prop['color']));
            if ($meta_saved)
                unset($prop['color']);  // unsetting will prevent fallback to local user prefs
        }

        return $result ? $folder : false;
    }


    /**
     * Getter for human-readable name of Kolab object (folder)
     * See http://wiki.kolab.org/UI-Concepts/Folder-Listing for reference
     *
     * @param string $folder    IMAP folder name (UTF7-IMAP)
     * @param string $folder_ns Will be set to namespace name of the folder
     *
     * @return string Name of the folder-object
     */
    public static function object_name($folder, &$folder_ns=null)
    {
        self::setup();

        $found     = false;
        $namespace = self::$imap->get_namespace();

        if (!empty($namespace['shared'])) {
            foreach ($namespace['shared'] as $ns) {
                if (strlen($ns[0]) && strpos($folder, $ns[0]) === 0) {
                    $prefix = '';
                    $folder = substr($folder, strlen($ns[0]));
                    $delim  = $ns[1];
                    $found  = true;
                    $folder_ns = 'shared';
                    break;
                }
            }
        }
        if (!$found && !empty($namespace['other'])) {
            foreach ($namespace['other'] as $ns) {
                if (strlen($ns[0]) && strpos($folder, $ns[0]) === 0) {
                    // remove namespace prefix
                    $folder = substr($folder, strlen($ns[0]));
                    $delim  = $ns[1];
                    // get username
                    $pos    = strpos($folder, $delim);
                    if ($pos) {
                        $prefix = '('.substr($folder, 0, $pos).') ';
                        $folder = substr($folder, $pos+1);
                    }
                    else {
                        $prefix = '('.$folder.')';
                        $folder = '';
                    }
                    $found  = true;
                    $folder_ns = 'other';
                    break;
                }
            }
        }
        if (!$found && !empty($namespace['personal'])) {
            foreach ($namespace['personal'] as $ns) {
                if (strlen($ns[0]) && strpos($folder, $ns[0]) === 0) {
                    // remove namespace prefix
                    $folder = substr($folder, strlen($ns[0]));
                    $prefix = '';
                    $delim  = $ns[1];
                    $found  = true;
                    break;
                }
            }
        }

        if (empty($delim))
            $delim = self::$imap->get_hierarchy_delimiter();

        $folder = rcube_charset::convert($folder, 'UTF7-IMAP');
        $folder = html::quote($folder);
        $folder = str_replace(html::quote($delim), ' &raquo; ', $folder);

        if ($prefix)
            $folder = html::quote($prefix) . ' ' . $folder;

        if (!$folder_ns)
            $folder_ns = 'personal';

        return $folder;
    }


    /**
     * Helper method to generate a truncated folder name to display
     */
    public static function folder_displayname($origname, &$names)
    {
        $name = $origname;

        // find folder prefix to truncate
        for ($i = count($names)-1; $i >= 0; $i--) {
            if (strpos($name, $names[$i] . ' &raquo; ') === 0) {
                $length = strlen($names[$i] . ' &raquo; ');
                $prefix = substr($name, 0, $length);
                $count  = count(explode(' &raquo; ', $prefix));
                $name   = str_repeat('&nbsp;&nbsp;', $count-1) . '&raquo; ' . substr($name, $length);
                break;
            }
        }
        $names[] = $origname;

        return $name;
    }


    /**
     * Creates a SELECT field with folders list
     *
     * @param string $type    Folder type
     * @param array  $attrs   SELECT field attributes (e.g. name)
     * @param string $current The name of current folder (to skip it)
     *
     * @return html_select SELECT object
     */
    public static function folder_selector($type, $attrs, $current = '')
    {
        // get all folders of specified type
        $folders = self::get_folders($type);

        $delim = self::$imap->get_hierarchy_delimiter();
        $names = array();
        $len   = strlen($current);

        if ($len && ($rpos = strrpos($current, $delim))) {
            $parent = substr($current, 0, $rpos);
            $p_len  = strlen($parent);
        }

        // Filter folders list
        foreach ($folders as $c_folder) {
            $name = $c_folder->name;
            // skip current folder and it's subfolders
            if ($len && ($name == $current || strpos($name, $current.$delim) === 0)) {
                continue;
            }

            // always show the parent of current folder
            if ($p_len && $name == $parent) { }
            // skip folders where user have no rights to create subfolders
            else if ($c_folder->get_owner() != $_SESSION['username']) {
                $rights = $c_folder->get_myrights();
                if (!preg_match('/[ck]/', $rights)) {
                    continue;
                }
            }

            $names[$name] = rcube_charset::convert($name, 'UTF7-IMAP');
        }

        // Make sure parent folder is listed (might be skipped e.g. if it's namespace root)
        if ($p_len && !isset($names[$parent])) {
            $names[$parent] = rcube_charset::convert($parent, 'UTF7-IMAP');
        }

        // Sort folders list
        asort($names, SORT_LOCALE_STRING);

        $folders = array_keys($names);
        $names   = array();

        // Build SELECT field of parent folder
        $attrs['is_escaped'] = true;
        $select = new html_select($attrs);
        $select->add('---', '');

        foreach ($folders as $name) {
            $imap_name = $name;
            $name      = $origname = self::object_name($name);

            // find folder prefix to truncate
            for ($i = count($names)-1; $i >= 0; $i--) {
                if (strpos($name, $names[$i].' &raquo; ') === 0) {
                    $length = strlen($names[$i].' &raquo; ');
                    $prefix = substr($name, 0, $length);
                    $count  = count(explode(' &raquo; ', $prefix));
                    $name   = str_repeat('&nbsp;&nbsp;', $count-1) . '&raquo; ' . substr($name, $length);
                    break;
                }
            }

            $names[] = $origname;
            $select->add($name, $imap_name);
        }

        return $select;
    }


    /**
     * Returns a list of folder names
     *
     * @param string  Optional root folder
     * @param string  Optional name pattern
     * @param string  Data type to list folders for (contact,distribution-list,event,task,note,mail)
     * @param string  Enable to return subscribed folders only
     * @param array   Will be filled with folder-types data
     *
     * @return array List of folders
     */
    public static function list_folders($root = '', $mbox = '*', $filter = null, $subscribed = false, &$folderdata = array())
    {
        if (!self::setup()) {
            return null;
        }

        if (!$filter) {
            // Get ALL folders list, standard way
            if ($subscribed) {
                return self::$imap->list_folders_subscribed($root, $mbox);
            }
            else {
                return self::$imap->list_folders($root, $mbox);
            }
        }

        $prefix = $root . $mbox;

        // get folders types
        $folderdata = self::$imap->get_metadata($prefix, array(self::CTYPE_KEY, self::CTYPE_KEY_PRIVATE));

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

        $folderdata = array_map(array('kolab_storage', 'folder_select_metadata'), $folderdata);
        $regexp     = '/^' . preg_quote($filter, '/') . '(\..+)?$/';

        // In some conditions we can skip LIST command (?)
        if ($subscribed == false && $filter != 'mail' && $prefix == '*') {
            foreach ($folderdata as $folder => $type) {
                if (!preg_match($regexp, $type)) {
                    unset($folderdata[$folder]);
                }
            }
            return array_keys($folderdata);
        }

        // Get folders list
        if ($subscribed) {
            $folders = self::$imap->list_folders_subscribed($root, $mbox);
        }
        else {
            $folders = self::$imap->list_folders($root, $mbox);
        }

        // In case of an error, return empty list (?)
        if (!is_array($folders)) {
            return array();
        }

        // Filter folders list
        foreach ($folders as $idx => $folder) {
            $type = $folderdata[$folder];

            if ($filter == 'mail' && empty($type)) {
                continue;
            }
            if (empty($type) || !preg_match($regexp, $type)) {
                unset($folders[$idx]);
            }
        }

        return $folders;
    }


    /**
     * Callback for array_map to select the correct annotation value
     */
    static function folder_select_metadata($types)
    {
        if (!empty($types[self::CTYPE_KEY_PRIVATE])) {
            return $types[self::CTYPE_KEY_PRIVATE];
        }
        else if (!empty($types[self::CTYPE_KEY])) {
            list($ctype, $suffix) = explode('.', $types[self::CTYPE_KEY]);
            return $ctype;
        }
        return null;
    }


    /**
     * Returns type of IMAP folder
     *
     * @param string $folder Folder name (UTF7-IMAP)
     *
     * @return string Folder type
     */
    static function folder_type($folder)
    {
        self::setup();

        $metadata = self::$imap->get_metadata($folder, array(self::CTYPE_KEY, self::CTYPE_KEY_PRIVATE));

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

        if (!empty($metadata[$folder])) {
            return self::folder_select_metadata($metadata[$folder]);
        }

        return 'mail';
    }

    /**
     * Sets folder content-type.
     *
     * @param string $folder Folder name
     * @param string $type   Content type
     *
     * @return boolean True on success
     */
    static function set_folder_type($folder, $type='mail')
    {
        self::setup();

        list($ctype, $subtype) = explode('.', $type);

        $success = self::$imap->set_metadata($folder, array(self::CTYPE_KEY => $ctype, self::CTYPE_KEY_PRIVATE => $subtype ? $type : null));

        if (!$success)  // fallback: only set private annotation
            $success |= self::$imap->set_metadata($folder, array(self::CTYPE_KEY_PRIVATE => $type));

        return $success;
    }
}