This file is indexed.

/usr/share/php/Horde/Kolab/Storage/Driver/Mock.php is in php-horde-kolab-storage 2.0.5-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
<?php
/**
 * An Kolab storage mock driver.
 *
 * PHP version 5
 *
 * @category Kolab
 * @package  Kolab_Storage
 * @author   Gunnar Wrobel <wrobel@pardus.de>
 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 * @link     http://pear.horde.org/index.php?package=Kolab_Storage
 */

/**
 * An Kolab storage mock driver.
 *
 * Copyright 2010-2013 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 *
 * @category Kolab
 * @package  Kolab_Storage
 * @author   Gunnar Wrobel <wrobel@pardus.de>
 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 * @link     http://pear.horde.org/index.php?package=Kolab_Storage
 */
class Horde_Kolab_Storage_Driver_Mock
extends Horde_Kolab_Storage_Driver_Base
{
    /**
     * The data of the folders.
     *
     * @var Horde_Kolab_Storage_Driver_Mock_Data
     */
    private $_data;

    /**
     * The regular expression for converting folder names.
     *
     * @var string
     */
    private $_conversion_pattern;

    /**
     * The data of the folder currently opened
     *
     * @var array
     */
    private $_mbox = null;

    /**
     * The name of the folder currently opened
     *
     * @var array
     */
    private $_mboxname = null;

    /**
     * A list of groups (associates users [key] with an array of group names
     * [value]).
     *
     * @var array
     */
    private $_groups = array();

    /**
     * Constructor.
     *
     * @param Horde_Kolab_Storage_Factory $factory A factory for helper objects.
     * @param array $params                        Connection parameters.
     */
    public function __construct(Horde_Kolab_Storage_Factory $factory,
                                $params = array())
    {
        if (isset($params['data'])) {
            if (is_array($params['data'])) {
                $params['data'] = new Horde_Kolab_Storage_Driver_Mock_Data(
                    $params['data']
                );
            }
            $this->_data = $params['data'];
            unset($params['data']);
        } else {
            $this->_data = new Horde_Kolab_Storage_Driver_Mock_Data(array());
        }
        parent::__construct($factory, $params);
    }

    /**
     * Convert the external folder id to an internal folder name.
     *
     * @param string $folder The external folder name.
     *
     * @return string The internal folder id.
     */
    private function _convertToInternal($folder)
    {
        if (substr($folder, 0, 5) == 'INBOX') {
            $user = explode('@', $this->getAuth());
            return 'user/' . $user[0] . substr($folder, 5);
        }
        return $folder;
    }

    /**
     * Convert the internal folder name into an external folder id.
     *
     * @param string $mbox The internal folder name.
     *
     * @return string The external folder id.
     */
    private function _convertToExternal($mbox)
    {
        if ($this->_conversion_pattern === null) {
            if ($this->getAuth() != '') {
                $user = explode('@', $this->getAuth());
                $this->_conversion_pattern = '#^user/' . $user[0] . '#';
            } else {
                /**
                 * @todo: FIXME, this is a hack for the current state of the
                 * Kolab share driver which does not yet know how to properly
                 * deal with system shares.
                 */
                if ($mbox == 'user/') {
                    return 'INBOX';
                } else {
                    return preg_replace('#^user//#', 'INBOX/', $mbox);
                }
            }
        }
        return preg_replace($this->_conversion_pattern, 'INBOX', $mbox);
    }

    /**
     * Set a group list.
     *
     * @param array $groups A list of groups. User names are the keys, an array
     *                      of group names are the values.
     *
     * @return NULL
     */
    public function setGroups($groups)
    {
        $this->_groups = $groups;
    }

    /**
     * Create the backend driver.
     *
     * @return mixed The backend driver.
     */
    public function createBackend()
    {
    }

    /**
     * Return the unique connection id.
     *
     * @return string The connection id.
     */
    public function getId()
    {
        return $this->getAuth() . '@mock:0';
    }

    /**
     * Retrieves a list of folders on the server.
     *
     * @return array The list of folders.
     */
    public function listFolders()
    {
        $result = array();
        foreach ($this->_data->arrayKeys() as $mbox) {
            if ($this->_folderVisible($mbox, $this->getAuth())) {
                $result[] = $this->_convertToExternal($mbox);
            }
        }
        return $result;
    }

    /**
     * Create the specified folder.
     *
     * @param string $folder The folder to create.
     *
     * @return NULL
     */
    public function create($folder)
    {
        $folder = $this->_convertToInternal($folder);
        if (isset($this->_data[$folder])) {
            throw new Horde_Kolab_Storage_Exception(
                sprintf("IMAP folder %s does already exist!", $folder)
            );
        }
        $this->_data[$folder] = array(
            'status' => array(
                'uidvalidity' => time(),
                'uidnext' => 1
            ),
            'mails' => array(),
            'permissions' => array($this->getAuth() => 'lrswipkxtecda'),
            'annotations' => array(),
        );
    }

    /**
     * Delete the specified folder.
     *
     * @param string $folder The folder to delete.
     *
     * @return NULL
     */
    public function delete($folder)
    {
        $folder = $this->_convertToInternal($folder);
        if (!isset($this->_data[$folder])) {
            throw new Horde_Kolab_Storage_Exception(
                sprintf("IMAP folder %s does not exist!", $folder)
            );
        }
        unset($this->_data[$folder]);
    }

    /**
     * Rename the specified folder.
     *
     * @param string $old  The folder to rename.
     * @param string $new  The new name of the folder.
     *
     * @return NULL
     */
    public function rename($old, $new)
    {
        $old = $this->_convertToInternal($old);
        $new = $this->_convertToInternal($new);
        if (!isset($this->_data[$old])) {
            throw new Horde_Kolab_Storage_Exception(
                sprintf("IMAP folder %s does not exist!", $old)
            );
        }
        if (isset($this->_data[$new])) {
            throw new Horde_Kolab_Storage_Exception(
                sprintf("IMAP folder %s does already exist!", $new)
            );
        }
        $this->_data[$new] = $this->_data[$old];
        unset($this->_data[$old]);
    }

    /**
     * Does the backend support ACL?
     *
     * @return boolean True if the backend supports ACLs.
     */
    public function hasAclSupport()
    {
        return true;
    }

    /**
     * Retrieve the access rights for a folder.
     *
     * @param string $folder The folder to retrieve the ACL for.
     *
     * @return array An array of rights.
     */
    public function getAcl($folder)
    {
        $folder = $this->_convertToInternal($folder);
        $this->_failOnMissingFolder($folder);
        $this->_failOnNoAdmin($folder);
        if ($this->_data->hasPermissions($folder)) {
            return $this->_data->getPermissions($folder);
        }
        return array();
    }

    /**
     * Retrieve the access rights the current user has on a folder.
     *
     * @param string $folder The folder to retrieve the user ACL for.
     *
     * @return string The user rights.
     */
    public function getMyAcl($folder)
    {
        $folder = $this->_convertToInternal($folder);
        $this->_failOnMissingFolder($folder);
        $myacl = array();
        $users = array($this->getAuth(), 'anyone', 'anonymous');
        if (isset($this->_groups[$this->getAuth()])) {
            foreach ($this->_groups[$this->getAuth()] as $group) {
                $users[] = 'group:' . $group;
            }
        }
        foreach ($users as $user) {
            if ($this->_data->hasUserPermissions($folder, $user)) {
                $myacl = array_merge($myacl, str_split($this->_data->getUserPermissions($folder, $user)));
            }
        }
        return join('', $myacl);
    }

    /**
     * Set the access rights for a folder.
     *
     * @param string $folder  The folder to act upon.
     * @param string $user    The user to set the ACL for.
     * @param string $acl     The ACL.
     *
     * @return NULL
     */
    public function setAcl($folder, $user, $acl)
    {
        $folder = $this->_convertToInternal($folder);
        $this->_failOnMissingFolder($folder);
        $this->_failOnNoAdmin($folder);
        $this->_data->setUserPermissions($folder, $user, $acl);
    }

    /**
     * Delete the access rights for user on a folder.
     *
     * @param string $folder  The folder to act upon.
     * @param string $user    The user to delete the ACL for
     *
     * @return NULL
     */
    public function deleteAcl($folder, $user)
    {
        $folder = $this->_convertToInternal($folder);
        $this->_failOnMissingFolder($folder);
        $this->_failOnNoAdmin($folder);
        if ($this->_data->hasUserPermissions($folder, $user)) {
            $this->_data->deleteUserPermissions($folder, $user);
        }
    }

    /**
     * Retrieves the specified annotation for the complete list of folders.
     *
     * @param string $annotation The name of the annotation to retrieve.
     *
     * @return array An associative array combining the folder names as key with
     *               the corresponding annotation value.
     */
    public function listAnnotation($annotation)
    {
        $result = array();
        foreach ($this->_data->arrayKeys() as $folder) {
            if ($this->_folderVisible($folder, $this->getAuth())) {
                if ($this->_data->hasAnnotation($folder, $annotation)) {
                    $result[$this->_convertToExternal($folder)] = $this->_data->getAnnotation($folder, $annotation);
                }
            }
        }
        return $result;
    }

    /**
     * Fetches the annotation from a folder.
     *
     * @param string $folder    The name of the folder.
     * @param string $annotation The annotation to get.
     *
     * @return string The annotation value.
     */
    public function getAnnotation($folder, $annotation)
    {
        $folder = $this->_convertToInternal($folder);
        $this->_failOnMissingFolder($folder);
        if ($this->_data->hasAnnotation($folder, $annotation)) {
            return $this->_data->getAnnotation($folder, $annotation);
        }
        return '';
    }

    /**
     * Sets the annotation on a folder.
     *
     * @param string $folder    The name of the folder.
     * @param string $annotation The annotation to set.
     * @param array  $value      The values to set
     *
     * @return NULL
     */
    public function setAnnotation($folder, $annotation, $value)
    {
        $folder = $this->_convertToInternal($folder);
        $this->_failOnMissingFolder($folder);
        $this->_data->setAnnotation($folder, $annotation, $value);
    }

    /**
     * Error out in case the provided folder is missing.
     *
     * @param string  $folder The folder.
     *
     * @return NULL
     *
     * @throws Horde_Kolab_Storage_Exception In case the folder is missing.
     */
    private function _failOnMissingFolder($folder)
    {
        if (!isset($this->_data[$folder])
            || !$this->_folderVisible($folder, $this->getAuth())) {
            $this->_folderMissing($folder);
        }
    }

    /**
     * Is the folder visible to the specified user (or a global group)?
     *
     * @param string $folder The folder.
     * @param string $user   The user.
     *
     * @return boolean True if the folder is visible.
     */
    private function _folderVisible($folder, $user)
    {
        return empty($user)
            || $this->_folderVisibleToUnique($folder, $user)
            || $this->_folderVisibleToGroup($folder, $user)
            || $this->_folderVisibleToUnique($folder, 'anyone')
            || $this->_folderVisibleToUnique($folder, 'anonymous');
    }

    /**
     * Is the folder visible to a group the user belongs to?
     *
     * @param string $folder The folder.
     * @param string $user   The user.
     *
     * @return boolean True if the folder is visible.
     */
    private function _folderVisibleToGroup($folder, $user)
    {
        if (isset($this->_groups[$user])) {
            foreach ($this->_groups[$user] as $group) {
                if ($this->_folderVisibleToUnique($folder, 'group:' . $group)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Is the folder visible to exactly the specified user?
     *
     * @param string $folder The folder.
     * @param string $user   The user.
     *
     * @return boolean True if the folder is visible.
     */
    private function _folderVisibleToUnique($folder, $user)
    {
        if ($this->_data->hasUserPermissions($folder, $user)) {
            if (strpos($this->_data->getUserPermissions($folder, $user), 'l') !== false
                || strpos($this->_data->getUserPermissions($folder, $user), 'r') !== false
                || strpos($this->_data->getUserPermissions($folder, $user), 'a') !== false) {
                return true;
            }
        }
        return false;
    }

    /**
     * Error out indicating that the user does not have the required
     * permissions.
     *
     * @param string  $folder The folder.
     *
     * @return NULL
     *
     * @throws Horde_Kolab_Storage_Exception In case the folder is missing.
     */
    private function _folderMissing($folder)
    {
        throw new Horde_Kolab_Storage_Exception(
            sprintf('The folder %s does not exist!', $folder)
        );
    }

    /**
     * Error out in case the user is no admin of the specified folder.
     *
     * @param string  $folder The folder.
     *
     * @return NULL
     *
     * @throws Horde_Kolab_Storage_Exception In case the user has no admin rights.
     */
    private function _failOnNoAdmin($folder)
    {
        if (!isset($this->_data[$folder])
            || !$this->_folderAdmin($folder, $this->getAuth())) {
            $this->_permissionDenied();
        }
    }

    /**
     * Is the user a folder admin (or one of the global groups)?
     *
     * @param string $folder The folder.
     * @param string $user   The user.
     *
     * @return boolean True if the user has admin rights on the folder.
     */
    private function _folderAdmin($folder, $user)
    {
        return empty($user)
            || $this->_folderAdminForUnique($folder, $user)
            || $this->_folderAdminForGroup($folder, $user)
            || $this->_folderAdminForUnique($folder, 'anyone')
            || $this->_folderAdminForUnique($folder, 'anonymous');
    }

    /**
     * Is the folder visible to a group the user belongs to?
     *
     * @param string $folder The folder.
     * @param string $user   The user.
     *
     * @return boolean True if the folder is visible.
     */
    private function _folderAdminForGroup($folder, $user)
    {
        if (isset($this->_groups[$user])) {
            foreach ($this->_groups[$user] as $group) {
                if ($this->_folderAdminForUnique($folder, 'group:' . $group)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Is the exact specified user an admin for the folder?
     *
     * @param string $folder The folder.
     * @param string $user   The user.
     *
     * @return boolean True if the user has admin rights on the folder.
     */
    private function _folderAdminForUnique($folder, $user)
    {
        if ($this->_data->hasUserPermissions($folder, $user)
            && strpos($this->_data->getUserPermissions($folder, $user), 'a') !== false) {
            return true;
        }
        return false;
    }

    /**
     * Error out indicating that the user does not have the required
     * permissions.
     *
     * @return NULL
     *
     * @throws Horde_Kolab_Storage_Exception In case the folder is missing.
     */
    private function _permissionDenied()
    {
        throw new Horde_Kolab_Storage_Exception('Permission denied!');
    }

    /**
     * Opens the given folder.
     *
     * @param string $folder  The folder to open
     *
     * @return NULL
     */
    public function select($folder)
    {
        $this->_data->select($this->_convertToInternal($folder));
    }

    /**
     * Returns the status of the current folder.
     *
     * @param string $folder Check the status of this folder.
     *
     * @return array  An array that contains 'uidvalidity' and 'uidnext'.
     */
    public function status($folder)
    {
        return $this->_data->status($this->_convertToInternal($folder));
    }

    /**
     * Returns the message ids of the messages in this folder.
     *
     * @param string $folder Check the status of this folder.
     *
     * @return array  The message ids.
     */
    public function getUids($folder)
    {
        return $this->_data->getUids($this->_convertToInternal($folder));
    }

    /**
     * Retrieves the messages for the given message ids.
     *
     * @param string $folder The folder to fetch the messages from.
     * @param array  $uids                The message UIDs.
     *
     * @return array An array of message structures parsed into Horde_Mime_Part
     *               instances.
     */
    public function fetchStructure($folder, $uids)
    {
        return $this->_data->fetchStructure(
            $this->_convertToInternal($folder),
            $uids
        );
    }

    /**
     * Retrieves a complete message.
     *
     * @param string $folder The folder to fetch the messages from.
     * @param array  $uid    The message UID.
     *
     * @return array The message encapsuled as an array that contains a
     *               Horde_Mime_Headers and a Horde_Mime_Part object.
     */
    public function fetchComplete($folder, $uid)
    {
        return $this->_data->fetchComplete(
            $this->_convertToInternal($folder),
            $uid
        );
    }

    /**
     * Retrieves a bodypart for the given message ID and mime part ID.
     *
     * @param string $folder The folder to fetch the messages from.
     * @param array  $uid                 The message UID.
     * @param array  $id                  The mime part ID.
     *
     * @return resource|string The body part, as a stream resource or string.
     */
    public function fetchBodypart($folder, $uid, $id)
    {
        return $this->_data->fetchBodypart(
            $this->_convertToInternal($folder),
            $uid,
            $id
        );
    }

    /**
     * Appends a message to the given folder.
     *
     * @param string   $folder  The folder to append the message(s) to.
     * @param resource $msg     The message to append.
     *
     * @return mixed True or the UID of the new message in case the backend
     *               supports UIDPLUS.
     */
    public function appendMessage($folder, $msg)
    {
        return $this->_data->appendMessage(
            $this->_convertToInternal($folder),
            $msg
        );
    }

    /**
     * Deletes messages from the specified folder.
     *
     * @param string  $folder  The folder to delete messages from.
     * @param integer $uids    IMAP message ids.
     *
     * @return NULL
     */
    public function deleteMessages($folder, $uids)
    {
        $this->_data->deleteMessages(
            $this->_convertToInternal($folder),
            $uids
        );
    }

    /**
     * Moves a message to a new folder.
     *
     * @param integer $uid         IMAP message id.
     * @param string  $old_folder  Source folder.
     * @param string  $new_folder  Target folder.
     *
     * @return NULL
     */
    public function moveMessage($uid, $old_folder, $new_folder)
    {
        $this->_data->moveMessage(
            $uid,
            $this->_convertToInternal($old_folder),
            $this->_convertToInternal($new_folder)
        );
    }

    /**
     * Expunges messages in the current folder.
     *
     * @param string $folder The folder to expunge.
     *
     * @return NULL
     */
    public function expunge($folder)
    {
        $this->_data->expunge($this->_convertToInternal($folder));
    }
}