This file is indexed.

/usr/share/php/Horde/Imap/Client/Cache/Backend/Mongo.php is in php-horde-imap-client 2.25.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
<?php
/**
 * Copyright 2013-2014 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  Horde
 * @copyright 2013-2014 Horde LLC
 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
 * @package   Imap_Client
 */

/**
 * A MongoDB database implementation for caching IMAP/POP data.
 * Requires the Horde_Mongo class.
 *
 * @author    Michael Slusarz <slusarz@horde.org>
 * @category  Horde
 * @copyright 2013-2014 Horde LLC
 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
 * @package   Imap_Client
 */
class Horde_Imap_Client_Cache_Backend_Mongo extends Horde_Imap_Client_Cache_Backend implements Horde_Mongo_Collection_Index
{
    /** Mongo collection names. */
    const BASE = 'horde_imap_client_cache_data';
    const MD = 'horde_imap_client_cache_metadata';
    const MSG = 'horde_imap_client_cache_message';

    /** Mongo field names: BASE collection. */
    const BASE_HOSTSPEC = 'hostspec';
    const BASE_MAILBOX = 'mailbox';
    const BASE_MODIFIED = 'modified';
    const BASE_PORT = 'port';
    const BASE_UID = 'data';
    const BASE_USERNAME = 'username';

    /** Mongo field names: MD collection. */
    const MD_DATA = 'data';
    const MD_FIELD = 'field';
    const MD_UID = 'uid';

    /** Mongo field names: MSG collection. */
    const MSG_DATA = 'data';
    const MSG_MSGUID = 'msguid';
    const MSG_UID = 'uid';

    /**
     * The MongoDB object for the cache data.
     *
     * @var MongoDB
     */
    protected $_db;

    /**
     * The list of indices.
     *
     * @var array
     */
    protected $_indices = array(
        self::BASE => array(
            'base_index_1' => array(
                self::BASE_HOSTSPEC => 1,
                self::BASE_MAILBOX => 1,
                self::BASE_PORT => 1,
                self::BASE_USERNAME => 1,
            )
        ),
        self::MSG => array(
            'msg_index_1' => array(
                self::MSG_MSGUID => 1,
                self::MSG_UID => 1
            )
        )
    );

    /**
     * Constructor.
     *
     * @param array $params  Configuration parameters:
     * <pre>
     *   - REQUIRED parameters:
     *     - mongo_db: (Horde_Mongo_Client) A MongoDB client object.
     * </pre>
     */
    public function __construct(array $params = array())
    {
        if (!isset($params['mongo_db'])) {
            throw new InvalidArgumentException('Missing mongo_db parameter.');
        }

        parent::__construct($params);
    }

    /**
     */
    protected function _initOb()
    {
        $this->_db = $this->_params['mongo_db']->selectDB(null);
    }

    /**
     */
    public function get($mailbox, $uids, $fields, $uidvalid)
    {
        $this->getMetaData($mailbox, $uidvalid, array('uidvalid'));

        if (!($uid = $this->_getUid($mailbox))) {
            return array();
        }

        $out = array();
        $query = array(
            self::MSG_MSGUID => array('$in' => array_map('strval', $uids)),
            self::MSG_UID => $uid
        );

        try {
            $cursor = $this->_db->selectCollection(self::MSG)->find(
                $query,
                array(
                    self::MSG_DATA => true,
                    self::MSG_MSGUID => true
                )
            );
            foreach ($cursor as $val) {
                $out[$val[self::MSG_MSGUID]] = $this->_value($val[self::MSG_DATA]);
            }
        } catch (MongoException $e) {}

        return $out;
    }

    /**
     */
    public function getCachedUids($mailbox, $uidvalid)
    {
        $this->getMetaData($mailbox, $uidvalid, array('uidvalid'));

        if (!($uid = $this->_getUid($mailbox))) {
            return array();
        }

        $out = array();
        $query = array(
            self::MSG_UID => $uid
        );

        try {
            $cursor = $this->_db->selectCollection(self::MSG)->find(
                $query,
                array(
                    self::MSG_MSGUID => true
                )
            );
            foreach ($cursor as $val) {
                $out[] = $val[self::MSG_MSGUID];
            }
        } catch (MongoException $e) {}

        return $out;
    }

    /**
     */
    public function set($mailbox, $data, $uidvalid)
    {
        if ($uid = $this->_getUid($mailbox)) {
            $res = $this->get($mailbox, array_keys($data), array(), $uidvalid);
        } else {
            $res = array();
            $uid = $this->_createUid($mailbox);
        }

        $coll = $this->_db->selectCollection(self::MSG);

        foreach ($data as $key => $val) {
            try {
                if (isset($res[$key])) {
                    $coll->update(array(
                        self::MSG_MSGUID => strval($key),
                        self::MSG_UID => $uid
                    ), array(
                        self::MSG_DATA => $this->_value(array_merge($res[$key], $val)),
                        self::MSG_MSGUID => strval($key),
                        self::MSG_UID => $uid
                    ));
                } else {
                    $coll->insert(array(
                        self::MSG_DATA => $this->_value($val),
                        self::MSG_MSGUID => strval($key),
                        self::MSG_UID => $uid
                    ));
                }
            } catch (MongoException $e) {}
        }

        /* Update modified time. */
        try {
            $this->_db->selectCollection(self::BASE)->update(
                array(self::BASE_UID => $uid),
                array(self::BASE_MODIFIED => time())
            );
        } catch (MongoException $e) {}

        /* Update uidvalidity. */
        $this->setMetaData($mailbox, array('uidvalid' => $uidvalid));
    }

    /**
     */
    public function getMetaData($mailbox, $uidvalid, $entries)
    {
        if (!($uid = $this->_getUid($mailbox))) {
            return array();
        }

        $out = array();
        $query = array(
            self::MD_UID => $uid
        );

        if (!empty($entries)) {
            $entries[] = 'uidvalid';
            $query[self::MD_FIELD] = array(
                '$in' => array_unique($entries)
            );
        }

        try {
            $cursor = $this->_db->selectCollection(self::MD)->find(
                $query,
                array(
                    self::MD_DATA => true,
                    self::MD_FIELD => true
                )
            );
            foreach ($cursor as $val) {
                $out[$val[self::MD_FIELD]] = $this->_value($val[self::MD_DATA]);
            }

            if (is_null($uidvalid) ||
                !isset($out['uidvalid']) ||
                ($out['uidvalid'] == $uidvalid)) {
                return $out;
            }

            $this->deleteMailbox($mailbox);
        } catch (MongoException $e) {}

        return array();
    }

    /**
     */
    public function setMetaData($mailbox, $data)
    {
        if (!($uid = $this->_getUid($mailbox))) {
            $uid = $this->_createUid($mailbox);
        }

        $coll = $this->_db->selectCollection(self::MD);

        foreach ($data as $key => $val) {
            try {
                $coll->update(
                    array(
                        self::MD_FIELD => $key,
                        self::MD_UID => $uid
                    ),
                    array(
                        self::MD_DATA => $this->_value($val),
                        self::MD_FIELD => $key,
                        self::MD_UID => $uid
                    ),
                    array('upsert' => true)
                );
            } catch (MongoException $e) {}
        }
    }

    /**
     */
    public function deleteMsgs($mailbox, $uids)
    {
        if (!empty($uids) && ($uid = $this->_getUid($mailbox))) {
            try {
                $this->_db->selectCollection(self::MSG)->remove(array(
                    self::MSG_MSGUID => array('$in' => array_map('strval', $uids)),
                    self::MSG_UID => $uid
                ));
            } catch (MongoException $e) {}
        }
    }

    /**
     */
    public function deleteMailbox($mailbox)
    {
        if (!($uid = $this->_getUid($mailbox))) {
            return;
        }

        foreach (array(self::BASE, self::MD, self::MSG) as $val) {
            try {
                $this->_db->selectCollection($val)->remove(array(
                    'uid' => $uid
                ));
            } catch (MongoException $e) {}
        }
    }

    /**
     */
    public function clear($lifetime)
    {
        if (is_null($lifetime)) {
            foreach (array(self::BASE, self::MD, self::MSG) as $val) {
                $this->_db->selectCollection($val)->drop();
            }
            return;
        }

        $query = array(
            self::BASE_MODIFIED => array('$lt' => (time() - $lifetime))
        );
        $uids = array();

        try {
            $cursor = $this->_db->selectCollection(self::BASE)->find($query);
            foreach ($cursor as $val) {
                $uids[] = strval($val['_id']);
            }
        } catch (MongoException $e) {}

        if (empty($uids)) {
            return;
        }

        foreach (array(self::BASE, self::MD, self::MSG) as $val) {
            try {
                $this->_db->selectCollection($val)->remove(array(
                    'uid' => array('$in' => $uids)
                ));
            } catch (MongoException $e) {}
        }
    }

    /**
     * Return the UID for a mailbox/user/server combo.
     *
     * @param string $mailbox  Mailbox name.
     *
     * @return string  UID from base table.
     */
    protected function _getUid($mailbox)
    {
        $query = array(
            self::BASE_HOSTSPEC => $this->_params['hostspec'],
            self::BASE_MAILBOX => $mailbox,
            self::BASE_PORT => $this->_params['port'],
            self::BASE_USERNAME => $this->_params['username']
        );

        try {
            if ($result = $this->_db->selectCollection(self::BASE)->findOne($query)) {
                return strval($result['_id']);
            }
        } catch (MongoException $e) {}

        return null;
    }

    /**
     * Create and return the UID for a mailbox/user/server combo.
     *
     * @param string $mailbox  Mailbox name.
     *
     * @return string  UID from base table.
     */
    protected function _createUid($mailbox)
    {
        $this->_db->selectCollection(self::BASE)->insert(array(
            self::BASE_HOSTSPEC => $this->_params['hostspec'],
            self::BASE_MAILBOX => $mailbox,
            self::BASE_PORT => $this->_params['port'],
            self::BASE_USERNAME => $this->_params['username']
        ));

        return $this->_getUid($mailbox);
    }

    /**
     * Convert data from/to storage format.
     *
     * @param mixed|MongoBinData $data  The data object.
     *
     * @return mixed|MongoBinData  The converted data.
     */
    protected function _value($data)
    {
        static $compress;

        if (!isset($compress)) {
            $compress = new Horde_Compress_Fast();
        }

        return ($data instanceof MongoBinData)
            ? @unserialize($compress->decompress($data->bin))
            : new MongoBinData($compress->compress(serialize($data)), MongoBinData::BYTE_ARRAY);
    }

    /* Horde_Mongo_Collection_Index methods. */

    /**
     */
    public function checkMongoIndices()
    {
        foreach ($this->_indices as $key => $val) {
            if (!$this->_params['mongo_db']->checkIndices($key, $val)) {
                return false;
            }
        }

        return true;
    }

    /**
     */
    public function createMongoIndices()
    {
        foreach ($this->_indices as $key => $val) {
            $this->_params['mongo_db']->createIndices($key, $val);
        }
    }

}