This file is indexed.

/usr/share/php/Horde/Vfs/Kolab.php is in php-horde-vfs 2.1.2-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
<?php
/**
 * VFS implementation for a Kolab IMAP server.
 *
 * Copyright 2002-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.
 *
 * @author  Gunnar Wrobel <wrobel@pardus.de>
 * @package Vfs
 */
class Horde_Vfs_Kolab extends Horde_Vfs_Base
{
    /**
     * Variable holding the connection to the Kolab storage system.
     *
     * @var Horde_Kolab_IMAP
     */
    protected $_imap = false;

    /**
     * Cache for the list of folders.
     *
     * @var array
     */
    protected $_folders;

    /**
     * Retrieves a file from the VFS.
     *
     * @param string $path  The pathname to the file.
     * @param string $name  The filename to retrieve.
     *
     * @return string  The file data.
     * @throws Horde_Vfs_Exception
     */
    public function read($path, $name)
    {
        list($app, $uid) = $this->_getAppUid($path);
        if ($app && $uid) {
            $handler = $this->_getAppHandler($app, $uid);
            $object = $handler->getObject($uid);

            if (isset($object['_attachments'][$name])) {
                return $handler->getAttachment($object['_attachments'][$name]['key']);
            }
        }

        //FIXME
        if ($this->isFolder(dirname($path), basename($path))) {
            $session = Horde_Kolab_Session::singleton();
            $imap = $session->getImap();

            $result = $imap->select(substr($path,1));
            if ($result instanceof PEAR_Error) {
                throw new Horde_Vfs_Exception($result->getMessage());
            }

            $file = explode('/', $name);

            return $this->_getFile($imap, $file[0], $file[1]);
        }

        return '';
    }

    /**
     * Stores a file in the VFS.
     *
     * @param string $path         The path to store the file in.
     * @param string $name         The filename to use.
     * @param string $tmpFile      The temporary file containing the data to
     *                             be stored.
     * @param boolean $autocreate  Automatically create directories?
     *
     * @throws Horde_Vfs_Exception
     */
    public function write($path, $name, $tmpFile, $autocreate = false)
    {
        list($app, $uid) = $this->_getAppUid($path);
        if ($app) {
            $handler = $this->_getAppHandler($app, $uid);
            $object = $handler->getObject($uid);
            $object['_attachments'][$name]['path'] = $tmpFile;
            if (empty($object['link-attachment'])) {
                $object['link-attachment'] = array($name);
            } else {
                $object['link-attachment'][] = $name;
            }

            return $handler->save($object, $uid);
        }

        if ($autocreate && !$this->isFolder(dirname($path), basename($path))) {
            $this->autocreatePath($path);
        }

        //FIXME
        throw new Horde_Vfs_Exception('Not supported.');
    }

    /**
     * Deletes a file from the VFS.
     *
     * @param string $path  The path to delete the file from.
     * @param string $name  The filename to delete.
     *
     * @throws Horde_Vfs_Exception
     */
    public function deleteFile($path, $name)
    {
        list($app, $uid) = $this->_getAppUid($path);
        if ($app) {
            $handler = $this->_getAppHandler($app, $uid);
            $object = $handler->getObject($uid);
            if (!isset($object['_attachments'][$name])) {
                throw new Horde_Vfs_Exception('Unable to delete VFS file.');
            }
            unset($object['_attachments'][$name]);
            $object['link-attachment'] = array_values(array_diff($object['link-attachment'], array($name)));

            $handler->save($object, $uid);
        }

        //FIXME
        throw new Horde_Vfs_Exception('Not supported.');
    }

    /**
     * Creates a folder on the VFS.
     *
     * @param string $path  The parent folder.
     * @param string $name  The name of the new folder.
     *
     * @throws Horde_Vfs_Exception
     */
    public function createFolder($path, $name)
    {
        $list = Kolab_List::singleton();
        $folder = $this->_getFolder($path, $name);

        $object = $list->getNewFolder();
        $object->setName($folder);

        $object->save(array('type' => 'h-file'));
        if ($result instanceof PEAR_Error) {
            throw new Horde_Vfs_Exception($result->getMessage());
        }

        $this->_folders = null;
    }

     /**
     * Deletes a folder from the VFS.
     *
     * @param string $path        The parent folder.
     * @param string $name        The name of the folder to delete.
     * @param boolean $recursive  Force a recursive delete?
     *
     * @throws Horde_Vfs_Exception
     */
    public function deleteFolder($path, $name, $recursive = false)
    {
        if ($recursive) {
            $this->emptyFolder($path . '/' . $name);
        } else {
            $list = $this->listFolder($path . '/' . $name, null, false);
            if (count($list)) {
                throw new Horde_Vfs_Exception(sprintf('Unable to delete %s, the directory is not empty', $path . '/' . $name));
            }
        }

        list($app,) = $this->_getAppUid($path . '/' . $name);
        if ($app) {
            /**
             * Objects provide no real folders and we don't delete them.
             */
            return;
        }

        $folders = $this->_getFolders();
        $folder = $this->_getFolder($path, $name);

        if (!empty($folders['/' . $folder])) {
            $folders['/' . $folder]->delete();
            $this->_folders = null;
            return;
        }

        throw new Horde_Vfs_Exception(sprintf('No such folder %s!', '/' . $folder));
    }

    /**
     * Recursively remove all files and subfolders from the given
     * folder.
     *
     * @param string $path  The path of the folder to empty.
     *
     * @throws Horde_Vfs_Exception
     */
    public function emptyFolder($path)
    {
        // Get and delete the subfolders.
        $list = $this->listFolder($path, null, false, true);
        foreach ($list as $folder) {
            $this->deleteFolder($path, $folder['name'], true);
        }

        // Only files are left, get and delete them.
        $list = $this->listFolder($path, null, false);
        foreach ($list as $file) {
            $this->deleteFile($path, $file['name']);
        }
    }

    /**
     * Returns an unsorted file list of the specified directory.
     *
     * @param string $path          The path of the directory.
     * @param string|array $filter  Regular expression(s) to filter
     *                              file/directory name on.
     * @param boolean $dotfiles     Show dotfiles?
     * @param boolean $dironly      Show only directories?
     *
     * @return array  File list.
     * @throws Horde_Vfs_Exception
     */
    protected function _listFolder($path = '', $filter = null, $dotfiles = true,
                                   $dironly = false)
    {
        list($app, $uid) = $this->_getAppUid($path);
        if ($app) {
            if ($dironly) {
                /**
                 * Objects dont support directories.
                 */
                return array();
            }
            if ($uid) {
                $handler = $this->_getAppHandler($app, $uid);
                $object = $handler->getObject($uid);
                if ($object instanceof PEAR_Error) {
                    throw new Horde_Vfs_Exception($object->getMessage());
                }

                $filenames = isset($object['_attachments'])
                    ? array_keys($object['_attachments'])
                    : array();
            } else {
                $filenames = $this->_getAppUids($app);
            }

            $owner = $GLOBALS['registry']->getAuth();

            $file = $files = array();
            foreach ($filenames as $filename) {
                $name = explode('.', $filename);

                if (count($name) == 1) {
                    $file['type'] = '**none';
                } else {
                    $file['type'] = Horde_String::lower($name[count($name) - 1]);
                }

                $file['size'] = '-1';
                $file['name'] = $filename;
                $file['group'] = 'none';
                $file['owner'] = $owner;
                $file['date'] = 0;
                $file['perms'] = 'rwxrwx---';

                $files[$file['name']] = $file;
            }

            return $files;
        }

        $owner = $GLOBALS['registry']->getAuth();

        $file = $files = array();

        $folderpath = $path;
        if (substr($folderpath, -1) != '/') {
            $folderpath .= '/';
        }

        $aFolder = $aFolders = array();

        if ($dotfiles && $folderpath != '/') {
            $aFolder['val'] = dirname($folderpath);
            $aFolder['abbrev'] = '..';
            $aFolder['label'] = '..';

            $aFolders[$aFolder['val']] = $aFolder;
        }

        $folders = $this->_getFolders();

        $base_len = strlen($folderpath);
        foreach (array_keys($folders) as $folder) {
            if (substr($folder, 0, $base_len) == $folderpath) {
                $name = substr($folder, $base_len);
                if (!strpos($name, '/')) {
                    $aFolder['val']	= $folder;
                    $aFolder['abbrev'] = $name;
                    $aFolder['label'] = $folder;
                    $aFolders[$aFolder['val']] = $aFolder;
                }
            }
        }

        ksort($aFolders);

        $list = $this->_getFolders();

        foreach ($aFolders as $folder) {
            $file['type'] = '**dir';
            $file['size'] = -1;
            $file['name'] = $folder['abbrev'];
            //FIXME
            $file['group'] = 'none';
            //FIXME
            $file['owner'] = $owner;
            //FIXME
            $file['date'] = 0;
            //FIXME
            $file['perms'] = 'rwxrwx---';

            $files[$file['name']] = $file;
        }

        if (!$dironly &&
            $this->isFolder(basename($path), basename($path)) &&
            !empty($list[$path])) {
            $session = Horde_Kolab_Session::singleton();
            $imap = $session->getImap();

            $result = $imap->select(substr($path, 1));
            if ($result instanceof PEAR_Error) {
                throw new Horde_Vfs_Exception($result->getMessage());
            }

            $uids = $imap->getUids();
            if ($uids instanceof PEAR_Error) {
                throw new Horde_Vfs_Exception($uids->getMessage());
            }

            foreach ($uids as $uid) {
                $result = array_merge($files, $this->_parseMessage($imap, $uid));
                $files = $result;
            }
        }

        return $files;
    }

    /**
     */
    protected function _parseMessage($imap, $uid)
    {
        $result = $imap->getMessageHeader($uid);
        if ($result instanceof PEAR_Error) {
            throw new Horde_Vfs_Exception($result->getMessage());
        }

        $raw_headers = $result;

        $body = $imap->getMessageBody($uid);
        if ($body instanceof PEAR_Error) {
            throw new Horde_Vfs_Exception($body->getMessage());
        }

        $raw_message = $raw_headers . $body;

        $mime_message = Horde_Mime_Part::parseMessage($raw_message);
        $parts = $mime_message->contentTypeMap();

        $owner = $GLOBALS['registry']->getAuth();

        $file = $files = array();

        foreach (array_keys($parts) as $part_id) {
            $part = $mime_message->getPart($part_id);

            $filename = $part->getDispositionParameter('filename');

            if ($filename) {
                $file['type'] = '**file';
                $file['size'] = $part->getSize();
                $file['name'] = $uid . '/' . $filename;
                //FIXME
                $file['group'] = 'none';
                //FIXME
                $file['owner'] = $owner;
                //FIXME
                $file['date'] = 0;
                //FIXME
                $file['perms'] = 'rwxrwx---';

                $files[$file['name']] = $file;
            }

        }

        return $files;
    }

    /**
     */
    protected function _getFile($imap, $uid, $filename)
    {
        $result = $imap->getMessageHeader($uid);
        if ($result instanceof PEAR_Error) {
            throw new Horde_Vfs_Exception($result->getMessage());
        }

        $raw_headers = $result;

        $body = $imap->getMessageBody($uid);
        if ($body instanceof PEAR_Error) {
            throw new Horde_Vfs_Exception($body->getMessage());
        }

        $raw_message = $raw_headers . $body;

        $mime_message = Horde_Mime_Part::parseMessage($raw_message);
        $parts = $mime_message->contentTypeMap();

        foreach (array_keys($parts) as $part_id) {
            $part = $mime_message->getPart($part_id);

            $f = $part->getDispositionParameter('filename');
            if ($f && $f == $filename) {
                return $part->transferDecode();
            }
        }

        return '';
    }

    /**
     */
    protected function _getFolder($path, $name)
    {
        $folder = $path . '/' . $name;

        while (substr($folder, 0, 1) == '/') {
            $folder = substr($folder, 1);
        }

        while (substr($folder, -1) == '/') {
            $folder = substr($folder, 0, -1);
        }

        return $folder;
    }

    /**
     */
    protected function _getFolders()
    {
        if (!isset($this->_folders)) {
            $vfs_folders = array();

            $list = Kolab_List::singleton();

            if (!empty($this->_params['all_folders'])) {
                $folders = $list->getFolders();
            } else {
                $folders = $list->getByType('h-file');
            }

            if ($folders instanceof PEAR_Error) {
                throw new Horde_Vfs_Exception($folders->getMessage());
            }

            foreach ($folders as $folder) {
                $vfs_folders['/' . $folder->name] = &$folder;
            }

            foreach (array_keys($vfs_folders) as $name) {
                $dir = dirname($name);
                while ($dir != '/') {
                    if (!isset($vfs_folders[$dir])) {
                        $vfs_folders[$dir] = null;
                    }
                    $dir = dirname($dir);
                }
            }
            $this->_folders = $vfs_folders;
        }

        return $this->_folders;
    }

    /**
     */
    protected function _getAppUid($path)
    {
        if (defined('TURBA_VFS_PATH') &&
            substr($path, 0, strlen(TURBA_VFS_PATH)) == TURBA_VFS_PATH) {
            return array('turba', substr($path, strlen(TURBA_VFS_PATH) + 1));
        }

        return array(false, false);
    }

    /**
     */
    protected function _getAppHandler($app, $uid)
    {
        global $registry;

        switch ($app) {
        case 'turba':
            $sources = $registry->call('contacts/sources',
                                       array('writeable' => true));
            $fields = array();
            foreach (array_keys($sources) as $source) {
                $fields[$source] = array('__uid');
            }
            $result = $registry->call('contacts/search', array($uid, array(
                'fields' => $fields,
                'sources' => array_keys($sources)
            )));
            if (!isset($result[$uid])) {
                throw new Horde_Vfs_Exception('No such contact!');
            }
            $list = Kolab_List::singleton();
            $share = $list->getByShare($result[$uid][0]['source'], 'contact');
            if ($share instanceof PEAR_Error) {
                throw new Horde_Vfs_Exception($share->getMessage());
            }
            return $share->getData();
        }
    }

    /**
     */
    protected function _getAppUids($app)
    {
        global $registry;

        switch ($app) {
        case 'turba':
            $sources = $registry->call('contacts/sources',
                                       array('writeable' => true));
            $result = $registry->call('contacts/search', array('', array(
                'fields' => array(),
                'sources' => array_keys($sources)
            )));
            $uids = array();
            foreach ($result[''] as $contact) {
                if (isset($contact['__uid'])) {
                    $uids[] = $contact['__uid'];
                }
            }
            return $uids;
        }
    }

}