summaryrefslogtreecommitdiff
path: root/program/lib/Roundcube/rcube_imap_search.php
blob: 70a11bc1c8280fa8fc8338e5a892a17e6aaa53ea (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
<?php

/*
 +-----------------------------------------------------------------------+
 | This file is part of the Roundcube Webmail client                     |
 |                                                                       |
 | Copyright (C) 2013, The Roundcube Dev Team                            |
 | Copyright (C) 2013, Kolab Systems AG                                  |
 |                                                                       |
 | Licensed under the GNU General Public License version 3 or            |
 | any later version with exceptions for skins & plugins.                |
 | See the README file for a full license statement.                     |
 |                                                                       |
 | PURPOSE:                                                              |
 |   Execute (multi-threaded) searches in multiple IMAP folders          |
 +-----------------------------------------------------------------------+
 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
 +-----------------------------------------------------------------------+
*/

// create classes defined by the pthreads module if that isn't installed
if (!defined('PTHREADS_INHERIT_ALL')) {
    class Worker { }
    class Stackable { }
}

/**
 * Class to control search jobs on multiple IMAP folders.
 * This implement a simple threads pool using the pthreads extension.
 *
 * @package    Framework
 * @subpackage Storage
 * @author     Thomas Bruederli <roundcube@gmail.com>
 */
class rcube_imap_search
{
    public $options = array();

    private $size = 10;
    private $next = 0;
    private $workers = array();
    private $states = array();
    private $jobs = array();
    private $conn;

    /**
     * Default constructor
     */
    public function __construct($options, $conn)
    {
        $this->options = $options;
        $this->conn = $conn;
    }

    /**
     * Invoke search request to IMAP server
     *
     * @param  array   $folders    List of IMAP folders to search in
     * @param  string  $str        Search criteria
     * @param  string  $charset    Search charset
     * @param  string  $sort_field Header field to sort by
     * @param  boolean $threading  True if threaded listing is active
     */
    public function exec($folders, $str, $charset = null, $sort_field = null, $threading=null)
    {
        $pthreads = defined('PTHREADS_INHERIT_ALL');

        // start a search job for every folder to search in
        foreach ($folders as $folder) {
            $job = new rcube_imap_search_job($folder, $str, $charset, $sort_field, $threading);
            if ($pthreads && $this->submit($job)) {
                $this->jobs[] = $job;
            }
            else {
                $job->worker = $this;
                $job->run();
                $this->jobs[] = $job;
            }
        }

        // wait for all workers to be done
        $this->shutdown();

        // gather results
        $results = new rcube_result_multifolder;
        foreach ($this->jobs as $job) {
            $results->add($job->get_result());
        }

        return $results;
    }

    /**
     * Assign the given job object to one of the worker threads for execution
     */
    public function submit(Stackable $job)
    {
        if (count($this->workers) < $this->size) {
            $id = count($this->workers);
            $this->workers[$id] = new rcube_imap_search_worker($id, $this->options);
            $this->workers[$id]->start(PTHREADS_INHERIT_ALL);

            if ($this->workers[$id]->stack($job)) {
                return $job;
            }
            else {
                // trigger_error(sprintf("Failed to push Stackable onto %s", $id), E_USER_WARNING);
            }
        }
        if (($worker = $this->workers[$this->next])) {
            $this->next = ($this->next+1) % $this->size;
            if ($worker->stack($job)) {
                return $job;
            }
            else {
                // trigger_error(sprintf("Failed to stack onto selected worker %s", $worker->id), E_USER_WARNING);
            }
        }
        else {
            // trigger_error(sprintf("Failed to select a worker for Stackable"), E_USER_WARNING);
        }

        return false;
    }

    /**
     * Shutdown the pool of threads cleanly, retaining exit status locally
     */
    public function shutdown()
    {
        foreach ($this->workers as $worker) {
            $this->states[$worker->getThreadId()] = $worker->shutdown();
            $worker->close();
        }

        # console('shutdown', $this->states);
    }
    
    /**
     * Get connection to the IMAP server
     * (used for single-thread mode)
     */
    public function get_imap()
    {
        return $this->conn;
    }
}


/**
 * Stackable item to run the search on a specific IMAP folder
 */
class rcube_imap_search_job extends Stackable
{
    private $folder;
    private $search;
    private $charset;
    private $sort_field;
    private $threading;
    private $searchset;
    private $result;
    private $pagesize = 100;

    public function __construct($folder, $str, $charset = null, $sort_field = null, $threading=false)
    {
        $this->folder = $folder;
        $this->search = $str;
        $this->charset = $charset;
        $this->sort_field = $sort_field;
        $this->threading = $threading;
    }

    public function run()
    {
        // trigger_error("Start search $this->folder", E_USER_NOTICE);
        $this->result = $this->search_index();
        // trigger_error("End search $this->folder: " . $this->result->count(), E_USER_NOTICE);
    }

    /**
     * Copy of rcube_imap::search_index()
     */
    protected function search_index()
    {
        $pthreads = defined('PTHREADS_INHERIT_ALL');
        $criteria = $this->search;
        $charset = $this->charset;

        $imap = $this->worker->get_imap();

        if (!$imap->connected()) {
            if ($this->threading) {
                return new rcube_result_thread();
            }
            else {
                return new rcube_result_index();
            }
        }

        if ($this->worker->options['skip_deleted'] && !preg_match('/UNDELETED/', $criteria)) {
            $criteria = 'UNDELETED '.$criteria;
        }

        // unset CHARSET if criteria string is ASCII, this way
        // SEARCH won't be re-sent after "unsupported charset" response
        if ($charset && $charset != 'US-ASCII' && is_ascii($criteria)) {
            $charset = 'US-ASCII';
        }

        if ($this->threading) {
            $threads = $imap->thread($this->folder, $this->threading, $criteria, true, $charset);

            // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8,
            // but I've seen that Courier doesn't support UTF-8)
            if ($threads->is_error() && $charset && $charset != 'US-ASCII') {
                $threads = $imap->thread($this->folder, $this->threading,
                    rcube_imap::convert_criteria($criteria, $charset), true, 'US-ASCII');
            }

            // close IMAP connection again
            if ($pthreads)
                $imap->closeConnection();

            return $threads;
        }

        if ($this->sort_field) {
            $messages = $imap->sort($this->folder, $this->sort_field, $criteria, true, $charset);

            // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8,
            // but I've seen Courier with disabled UTF-8 support)
            if ($messages->is_error() && $charset && $charset != 'US-ASCII') {
                $messages = $imap->sort($this->folder, $this->sort_field,
                    rcube_imap::convert_criteria($criteria, $charset), true, 'US-ASCII');
            }
        }

        if (!$messages || $messages->is_error()) {
            $messages = $imap->search($this->folder,
                ($charset && $charset != 'US-ASCII' ? "CHARSET $charset " : '') . $criteria, true);

            // Error, try with US-ASCII (some servers may support only US-ASCII)
            if ($messages->is_error() && $charset && $charset != 'US-ASCII') {
                $messages = $imap->search($this->folder,
                    rcube_imap::convert_criteria($criteria, $charset), true);
            }
        }

        // close IMAP connection again
        if ($pthreads)
            $imap->closeConnection();

        return $messages;
    }

    public function get_search_set()
    {
        return array(
            $this->search,
            $this->result,
            $this->charset,
            $this->sort_field,
            $this->threading,
        );
    }

    public function get_result()
    {
        return $this->result;
    }
}


/**
 * Worker thread to run search jobs while maintaining a common context
 */
class rcube_imap_search_worker extends Worker
{
    public $id;
    public $options;

    private $conn;

    /**
     * Default constructor
     */
    public function __construct($id, $options)
    {
        $options['ident']['command'] = 'search-'.$id;

        $this->id = $id;
        $this->options = $options;
    }

    /**
     * Get a dedicated connection to the IMAP server
     */
    public function get_imap()
    {
        // TODO: make this connection persistent for several jobs
        #if ($this->conn)
        #    return $this->conn;

        $conn = new rcube_imap_generic();
        # $conn->setDebug(true, function($conn, $message){ trigger_error($message, E_USER_NOTICE); });

        if ($this->options['user'] && $this->options['password']) {
            // TODO: do this synchronized to avoid warnings like "Only one Id allowed in non-authenticated state"
            $conn->connect($this->options['host'], $this->options['user'], $this->options['password'], $this->options);
        }

        if ($conn->error)
            trigger_error($conn->error, E_USER_WARNING);

        #$this->conn = $conn;
        return $conn;
    }

    /**
     * @override
     */
    public function run()
    {
        
    }

    /**
     * Close IMAP connection
     */
    public function close()
    {
        if ($this->conn) {
            $this->conn->close();
        }
    }
}