-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.php
581 lines (511 loc) · 16.4 KB
/
Search.php
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
<?php
/**
* Copyright 2002-2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2002-2015 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
/**
* Object handling mailbox searching in IMP.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2002-2015 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Search implements ArrayAccess, IteratorAggregate, Serializable
{
/* The mailbox search prefix. */
const MBOX_PREFIX = "impsearch\0";
/* The special search mailbox names. */
const BASIC_SEARCH = 'impbsearch';
const DIMP_FILTERSEARCH = 'dimpfsearch';
const DIMP_QUICKSEARCH = 'dimpqsearch';
/* Query creation types. */
const CREATE_FILTER = 1;
const CREATE_QUERY = 2;
const CREATE_VFOLDER = 3;
/**
* Has the object data changed?
*
* @var boolean
*/
public $changed = false;
/**
* Search queries.
*
* Each subarray contains:
* Keys: mailbox IDs.
* Values: IMP_Search_Query objects.
*
* @var array
*/
protected $_search = array(
'filters' => array(),
'query' => array(),
'vfolders' => array()
);
/**
* Constructor.
*/
public function __construct()
{
$this->init();
}
/**
* Initialize session search data.
*/
public function init()
{
$this->_getFilters();
$this->_getVFolders();
}
/**
* Creates the IMAP search query in the IMP session.
*
* @param array $criteria The search criteria array.
* @param array $opts Additional options:
* - id: (string) Use as the mailbox ID.
* - label: (string) The label to use for the search results.
* - mboxes: (array) The list of mailboxes to directly search. If this
* contains the IMP_Search_Query::ALLSEARCH constant, all
* mailboxes will be searched.
* - subfolders: (array) The list of mailboxes to do subfolder searches
* on.
* - type: (integer) Query type.
*
* @return IMP_Search_Query Returns the query object.
* @throws InvalidArgumentException
*/
public function createQuery($criteria, array $opts = array())
{
global $injector;
$opts = array_merge(array(
'id' => null,
'label' => null,
'mboxes' => array(),
'subfolders' => array(),
'type' => self::CREATE_QUERY
), $opts);
/* Make sure mailbox names are not IMP_Mailbox objects. */
$opts['mboxes'] = array_map('strval', $opts['mboxes']);
$opts['subfolders'] = array_map('strval', $opts['subfolders']);
switch ($opts['type']) {
case self::CREATE_FILTER:
$cname = 'IMP_Search_Filter';
break;
case self::CREATE_QUERY:
$cname = 'IMP_Search_Query';
if (empty($opts['mboxes']) && empty($opts['subfolders'])) {
throw new InvalidArgumentException('Search query requires at least one mailbox.');
}
break;
case self::CREATE_VFOLDER:
$cname = 'IMP_Search_Vfolder';
if (empty($opts['mboxes']) && empty($opts['subfolders'])) {
throw new InvalidArgumentException('Search query requires at least one mailbox.');
}
break;
}
$ob = new $cname(array_filter(array(
'add' => $criteria,
'all' => in_array(IMP_Search_Query::ALLSEARCH, $opts['mboxes']),
'id' => $this->_strip($opts['id']),
'label' => $opts['label'],
'mboxes' => $opts['mboxes'],
'subfolders' => $opts['subfolders']
)));
switch ($opts['type']) {
case self::CREATE_FILTER:
/* This will overwrite previous value, if it exists. */
$this->_search['filters'][$ob->id] = $ob;
$this->setFilters($this->_search['filters']);
break;
case self::CREATE_QUERY:
$this->_search['query'][$ob->id] = $ob;
$ob->mbox_ob->list_ob->rebuild(true);
break;
case self::CREATE_VFOLDER:
/* This will overwrite previous value, if it exists. */
$this->_search['vfolders'][$ob->id] = $ob;
$this->setVFolders($this->_search['vfolders']);
$injector->getInstance('IMP_Mailbox_SessionCache')->expire(
array(
IMP_Mailbox_SessionCache::CACHE_DISPLAY,
IMP_Mailbox_SessionCache::CACHE_LABEL
),
$ob->mbox_ob
);
$ftree = $injector->getInstance('IMP_Ftree');
$ftree->delete($ob);
$ftree->insert($ob);
break;
}
/* Reset the sort direction for system queries. */
if ($this->isSystemQuery($ob)) {
$ob->mbox_ob->setSort(null, null, true);
}
$this->changed = true;
return $ob;
}
/**
* Saves the list of filters for the current user.
*
* @param array $filters The filter list.
*/
public function setFilters($filters)
{
$GLOBALS['prefs']->setValue('filter', serialize(array_values($filters)));
$this->_getFilters();
}
/**
* Loads the list of filters for the current user.
*/
protected function _getFilters()
{
$filters = array();
/* Build list of default filters. */
$di = new DirectoryIterator(IMP_BASE . '/lib/Search/Filter');
foreach ($di as $val) {
if ($val->isFile()) {
$cname = 'IMP_Search_Filter_' . $val->getBasename('.php');
if (($cname != 'IMP_Search_Filter_Builtin') &&
class_exists($cname)) {
$filter = new $cname();
$filters[$filter->id] = $filter;
}
}
}
if ($f_list = $GLOBALS['prefs']->getValue('filter')) {
$f_list = @unserialize($f_list);
if (is_array($f_list)) {
foreach ($f_list as $val) {
if ($val instanceof IMP_Search_Filter) {
$filters[$val->id] = $val;
}
}
}
}
$this->_search['filters'] = $filters;
$this->changed = true;
}
/**
* Is a mailbox a filter query?
*
* @param string $id The mailbox ID.
* @param boolean $editable Is this an editable (i.e. not built-in)
* filter query?
*/
public function isFilter($id, $editable = false)
{
return (isset($this->_search['filters'][$this->_strip($id)]) &&
(!$editable || $this[$id]->canEdit));
}
/**
* Converts a filter to a search query and stores it in the local
* session.
*
* @param string $id The mailbox ID of the filter.
* @param array $mboxes The list of mailboxes to apply the filter on.
* @param string $mid Use as the mailbox ID.
*
* @return IMP_Search_Query The created query object.
* @throws InvalidArgumentException
*/
public function applyFilter($id, array $mboxes, $mid = null)
{
if (!$this->isFilter($id)) {
throw new InvalidArgumentException('Invalid filter ID given.');
}
if (!is_null($mid)) {
$mid = $this->_strip($mid);
}
$q_ob = $this[$id]->toQuery($mboxes, $mid);
$this->_search['query'][$q_ob->id] = $q_ob;
$this->changed = true;
return $q_ob;
}
/**
* Saves the list of virtual folders for the current user.
*
* @param array $vfolders The virtual folder list.
*/
public function setVFolders($vfolders)
{
$GLOBALS['prefs']->setValue('vfolder', serialize(array_values($vfolders)));
}
/**
* Loads the list of virtual folders for the current user.
*/
protected function _getVFolders()
{
$vf = array();
/* Build list of default virtual folders. */
$di = new DirectoryIterator(IMP_BASE . '/lib/Search/Vfolder');
$disable = array('IMP_Search_Vfolder_Vtrash');
foreach ($di as $val) {
if ($val->isFile()) {
$cname = 'IMP_Search_Vfolder_' . $val->getBasename('.php');
if (($cname != 'IMP_Search_Vfolder_Builtin') &&
class_exists($cname)) {
$vfolder = new $cname(array(
'disable' => in_array($cname, $disable)
));
$vf[$vfolder->id] = $vfolder;
}
}
}
if ($pref_vf = $GLOBALS['prefs']->getValue('vfolder')) {
$pref_vf = @unserialize($pref_vf);
if (is_array($pref_vf)) {
foreach ($pref_vf as $val) {
if ($val instanceof IMP_Search_Vfolder) {
$vf[$val->id] = $val;
}
}
}
}
$this->_search['vfolders'] = $vf;
$this->changed = true;
}
/**
* Is a mailbox a virtual folder?
*
* @param string $id The mailbox ID.
* @param boolean $editable Is this an editable (i.e. not built-in)
* virtual folder?
*
* @return boolean Whether the mailbox ID is a virtual folder.
*/
public function isVFolder($id, $editable = false)
{
return (isset($this->_search['vfolders'][$this->_strip($id)]) &&
(!$editable || $this[$id]->canEdit));
}
/**
* Determines whether a mailbox ID is the Virtual Trash Folder.
*
* @param string $id The mailbox id.
*
* @return boolean True if the ID is the Virtual Trash folder.
*/
public function isVTrash($id)
{
return (($this->isVFolder($id)) &&
($this[$id] instanceof IMP_Search_Vfolder_Vtrash));
}
/**
* Determines whether a mailbox ID is the Virtual INBOX Folder.
*
* @param string $id The mailbox id.
*
* @return boolean True if the ID is the Virtual INBOX folder.
*/
public function isVinbox($id)
{
return (($this->isVFolder($id)) &&
($this[$id] instanceof IMP_Search_Vfolder_Vinbox));
}
/**
* Is a mailbox a search query?
*
* @param string $id The mailbox ID.
* @param boolean $editable Is this an editable (i.e. not built-in)
* search query?
*
* @return boolean True if a search query.
*/
public function isQuery($id, $editable = false)
{
return (isset($this->_search['query'][$this->_strip($id)]) &&
(!$editable || !$this->isSystemQuery($id)));
}
/**
* Is a mailbox a system (built-in) search query?
*
* @param string $id The mailbox ID.
*
* @return boolean True if a system search query.
*/
public function isSystemQuery($id)
{
return (isset($this->_search['query'][$this->_strip($id)]) &&
in_array($this[$id]->id, array(self::BASIC_SEARCH, self::DIMP_FILTERSEARCH, self::DIMP_QUICKSEARCH)));
}
/**
* Returns a link to edit a given search query.
*
* @param string $id The search query id.
*
* @return Horde_Url The URL to the search page.
*/
public function editUrl($id)
{
global $registry;
$mbox = IMP_Mailbox::get($this->createSearchId($id));
switch ($registry->getView()) {
case $registry::VIEW_BASIC:
return $mbox->url(IMP_Basic_Search::url())->add(array(
'edit_query' => 1
));
case $registry::VIEW_DYNAMIC:
return IMP_Dynamic_Mailbox::url()->setAnchor(
'search:' . json_encode(array(
'edit_query' => 1,
'mailbox' => $mbox->form_to
))
);
}
}
/**
* Is the given mailbox a search mailbox?
*
* @param string $id The mailbox name.
*
* @return boolean Whether the given mailbox name is a search mailbox.
*/
public function isSearchMbox($id)
{
return (strpos($id, self::MBOX_PREFIX) === 0);
}
/**
* Strip the identifying label from a mailbox ID.
*
* @param string $id The mailbox query ID.
*
* @return string The virtual folder ID, with any IMP specific
* identifying information stripped off.
*/
protected function _strip($id)
{
return $this->isSearchMbox($id)
? substr($id, strlen(self::MBOX_PREFIX))
: strval($id);
}
/**
* Create the canonical search ID for a given search query.
*
* @param string $id The mailbox query ID.
*
* @return string The canonical search query ID.
*/
public function createSearchId($id)
{
return self::MBOX_PREFIX . $this->_strip($id);
}
/* ArrayAccess methods. */
public function offsetExists($offset)
{
$id = $this->_strip($offset);
foreach (array_keys($this->_search) as $key) {
if (isset($this->_search[$key][$id])) {
return true;
}
}
return false;
}
public function offsetGet($offset)
{
$id = $this->_strip($offset);
foreach (array_keys($this->_search) as $key) {
if (isset($this->_search[$key][$id])) {
return $this->_search[$key][$id];
}
}
return null;
}
/**
* Alter the current IMAP search query.
*
* @param string $offset The search query id.
* @param IMP_Search_Query $value The query object.
*
* @throws InvalidArgumentException
*/
public function offsetSet($offset, $value)
{
if (!($value instanceof IMP_Search_Query)) {
throw new InvalidArgumentException('$value must be a query object.');
}
$id = $this->_strip($offset);
foreach (array_keys($this->_search) as $key) {
if (isset($this->_search[$key][$id])) {
$this->_search[$key][$id] = $value;
$this->changed = true;
if ($key == 'vfolders') {
$this->setVFolders($this->_search['vfolders']);
$ftree = $GLOBALS['injector']->getInstance('IMP_Ftree');
$ftree->delete($value);
$ftree->insert($value);
}
return;
}
}
throw new InvalidArgumentException('Creating search queries by array index is not supported. Use createQuery() instead.');
}
/**
* Deletes an IMAP search query.
*
* @param string $offset The search query id.
*/
public function offsetUnset($offset)
{
$id = $this->_strip($offset);
foreach (array_keys($this->_search) as $val) {
if (isset($this->_search[$val][$id])) {
$value = $this->_search[$val][$id];
unset($this->_search[$val][$id]);
$this->changed = true;
if ($val == 'vfolders') {
$this->setVFolders($this->_search['vfolders']);
$GLOBALS['injector']->getInstance('IMP_Ftree')->delete($value);
}
break;
}
}
}
/* IteratorAggregate method. */
/**
*/
public function getIterator()
{
$iterator = new AppendIterator();
foreach ($this->_search as $val) {
$iterator->append(new ArrayIterator($val));
}
return $iterator;
}
/* Serializable methods. */
/**
* Serialize.
*
* @return string Serialized representation of this object.
*/
public function serialize()
{
return $GLOBALS['injector']->getInstance('Horde_Pack')->pack(
$this->_search,
array(
'compression' => false,
'phpob' => true
)
);
}
/**
* Unserialize.
*
* @param string $data Serialized data.
*
* @throws Exception
*/
public function unserialize($data)
{
$this->_search = $GLOBALS['injector']->getInstance('Horde_Pack')->unpack($data);
}
}