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 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
|
<?php
/* Defines used to determine what kind of field query we are dealing with. */
define('IMP_SEARCH_HEADER', 1);
define('IMP_SEARCH_BODY', 2);
define('IMP_SEARCH_DATE', 3);
define('IMP_SEARCH_TEXT', 4);
/* Defines used to identify the flag input. */
define('IMP_SEARCH_FLAG_SEEN', 1);
define('IMP_SEARCH_FLAG_ANSWERED', 2);
define('IMP_SEARCH_FLAG_FLAGGED', 3);
define('IMP_SEARCH_FLAG_DELETED', 4);
/* Defines used to identify whether to show unsubscribed folders. */
define('IMP_SEARCH_SHOW_UNSUBSCRIBED', 0);
define('IMP_SEARCH_SHOW_SUBSCRIBED_ONLY', 1);
/**
* The IMP_Search:: class contains all code related to mailbox searching
* in IMP.
*
* The class uses the $_SESSION['imp']['search'] variable to store information
* across page accesses. The format of that entry is as follows:
*
* $_SESSION['imp']['search'] = array(
* 'q' => array(
* 'id_1' => array(
* 'query' => IMAP_Search_Query object (serialized),
* 'folders' => array (List of folders to search),
* 'uiinfo' => array (Info used by search.php to render page),
* 'label' => string (Description of search),
* 'vfolder' => boolean (True if this is a Virtual Folder)
* ),
* 'id_2' => array(
* ....
* ),
* ....
* ),
* 'vtrash_id' => string (The Virtual Trash query ID),
* 'vinbox_id' => string (The Virtual Inbox query ID)
* );
*
* $Horde: imp/lib/Search.php,v 1.37.10.41 2008/03/06 04:13:26 chuck Exp $
*
* Copyright 2002-2008 The Horde Project (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
*
* @author Michael Slusarz <slusarz@horde.org>
* @package IMP
*/
class IMP_Search {
/**
* The ID of the current search query in use.
*
* @var string
*/
var $_id = null;
/**
* Save Virtual Folder information when adding entries?
*
* @var boolean
*/
var $_saveVFolder = true;
/**
* Constructor.
*
* @param array $params Available parameters:
* <pre>
* 'id' -- The ID of the search query in use.
* </pre>
*/
function IMP_Search($params = array())
{
if (!empty($params['id'])) {
$this->_id = $this->_strip($params['id']);
}
}
/**
* Set up IMP_Search variables for the current session.
*
* @param boolean $no_vf Don't readd the Virtual Folders.
*/
function sessionSetup($no_vf = false)
{
if (!isset($_SESSION['imp']['search'])) {
$_SESSION['imp']['search'] = array('q' => array());
}
if (!$no_vf) {
foreach ($this->_getVFolderList() as $key => $val) {
if (!empty($val['vfolder']) &&
!$this->isVTrashFolder($key) &&
!$this->isVINBOXFolder($key)) {
$this->_updateIMPTree('add', $key, $val['label']);
$_SESSION['imp']['search']['q'][$key] = $val;
}
}
}
$this->createVINBOXFolder();
$this->createVTrashFolder();
}
/**
* Run a search.
*
* @param IMAP_Search_Query &$ob An optional search query to add (via
* 'AND') to the active search.
* @param string $id The search query id to use (by default,
* will use the current ID set in the
* object).
*
* @return array The sorted list.
*/
function runSearch(&$ob, $id = null)
{
$id = $this->_strip($id);
$mbox = '';
$sorted = array();
if (empty($_SESSION['imp']['search']['q'][$id])) {
return $sorted;
}
$search = &$_SESSION['imp']['search']['q'][$id];
$imap_search = &$this->_getIMAPSearch();
/* Prepare the search query. */
if (!empty($ob)) {
$old_query = unserialize($search['query']);
$query = new IMP_IMAP_Search_Query();
$query->imapAnd(array($ob, $old_query));
} else {
$query = unserialize($search['query']);
}
/* How do we want to sort results? */
$sortpref = IMP::getSort();
if ($sortpref['by'] == SORTTHREAD) {
$sortpref['by'] = SORTDATE;
}
foreach ($search['folders'] as $val) {
$results = $imap_search->searchSortMailbox($query, null, $val, $sortpref['by'], $sortpref['dir']);
if (is_array($results)) {
foreach ($results as $val2) {
$sorted[] = $val2 . IMP_IDX_SEP . $val;
}
}
}
return $sorted;
}
/**
* Run a search query not stored in the current session. Allows custom
* queries with custom sorts to be used without affecting cached
* mailboxes.
*
* @since IMP 4.2
*
* @param IMAP_Search_Query $ob The search query.
*
* @return array The sorted list.
*/
function runSearchQuery($ob, $mailbox, $sortby, $sortdir)
{
$imap_search = &$this->_getIMAPSearch();
return $imap_search->searchSortMailbox($ob, null, $mailbox, $sortby, $sortdir);
}
/**
* Creates the IMAP search query in the IMP session.
*
* @param IMAP_Search_Query $query The search query object.
* @param array $folders The list of folders to search.
* @param array $search The search array used to build the
* search UI screen.
* @param string $label The label to use for the search
* results.
* @param string $id The query id to use (or else one is
* automatically generated).
*
* @return string Returns the search query id.
*/
function createSearchQuery($query, $folders, $search, $label, $id = null)
{
$id = (empty($id)) ? base_convert(microtime() . mt_rand(), 16, 36) : $this->_strip($id);
$_SESSION['imp']['search']['q'][$id] = array(
'query' => serialize($query),
'folders' => $folders,
'uiinfo' => $search,
'label' => $label,
'vfolder' => false
);
return $id;
}
/**
* Deletes an IMAP search query.
*
* @param string $id The search query id to use (by default, will use
* the current ID set in the object).
*
* @return string Returns the search query id.
*/
function deleteSearchQuery($id = null)
{
$id = $this->_strip($id);
$is_vfolder = !empty($_SESSION['imp']['search']['q'][$id]['vfolder']);
unset($_SESSION['imp']['search']['q'][$id]);
if ($is_vfolder) {
$vfolders = $this->_getVFolderList();
unset($vfolders[$id]);
$this->_saveVFolderList($vfolders);
$this->_updateIMPTree('delete', $id);
}
}
/**
* Retrieves the previously stored search UI information.
*
* @param string $id The search query id to use (by default, will use
* the current ID set in the object).
*
* @return array The array necessary to rebuild the search UI page.
*/
function retrieveUIQuery($id = null)
{
$id = $this->_strip($id);
return (isset($_SESSION['imp']['search']['q'][$id]['uiinfo']))
? $_SESSION['imp']['search']['q'][$id]['uiinfo']
: array();
}
/**
* Generates the label to use for search results.
*
* @param string $id The search query id to use (by default, will use
* the current ID set in the object).
*
* @return string The search results label.
*/
function getLabel($id = null)
{
$id = $this->_strip($id);
return (isset($_SESSION['imp']['search']['q'][$id]['label']))
? $_SESSION['imp']['search']['q'][$id]['label']
: '';
}
/**
* Obtains the list of virtual folders for the current user.
*
* @access private
*
* @return array The list of virtual folders.
*/
function _getVFolderList()
{
$vfolder = $GLOBALS['prefs']->getValue('vfolder');
if (empty($vfolder)) {
return array();
}
$vfolder = @unserialize($vfolder);
return (is_array($vfolder)) ? $vfolder : array();
}
/**
* Saves the list of virtual folders for the current user.
*
* @access private
*
* @param array The virtual folder list.
*/
function _saveVFolderList($vfolder)
{
$GLOBALS['prefs']->setValue('vfolder', serialize($vfolder));
}
/**
* Add a virtual folder for the current user.
*
* @param IMAP_Search_Query $query The search query object.
* @param array $folders The list of folders to search.
* @param array $search The search array used to build the
* search UI screen.
* @param string $label The label to use for the search
* results.
* @param string $id The virtual folder id.
*
* @return string The virtual folder ID.
*/
function addVFolder($query, $folders, $search, $label, $id = null)
{
$id = $this->createSearchQuery($query, $folders, $search, $label, $id);
$_SESSION['imp']['search']['q'][$id]['vfolder'] = true;
if ($this->_saveVFolder) {
$vfolders = $this->_getVFolderList();
$vfolders[$id] = $_SESSION['imp']['search']['q'][$id];
$this->_saveVFolderList($vfolders);
}
$this->_updateIMPTree('add', $id, $label);
return $id;
}
/**
* Add a virtual trash folder for the current user.
*/
function createVTrashFolder()
{
/* Delete the current Virtual Trash folder, if it exists. */
$vtrash_id = $GLOBALS['prefs']->getValue('vtrash_id');
if (!empty($vtrash_id)) {
$this->deleteSearchQuery($vtrash_id);
}
if (!$GLOBALS['prefs']->getValue('use_vtrash')) {
return;
}
/* Create Virtual Trash with new folder list. */
require_once IMP_BASE . '/lib/Folder.php';
$imp_folder = &IMP_Folder::singleton();
$fl = $imp_folder->flist_IMP();
$flist = array();
foreach ($fl as $mbox) {
if (!empty($mbox['val'])) {
$flist[] = $mbox['val'];
}
}
array_unshift($flist, 'INBOX');
require_once IMP_BASE . '/lib/IMAP/Search.php';
$query = new IMP_IMAP_Search_Query();
$query->deleted(true);
$label = _("Virtual Trash");
$this->_saveVFolder = false;
if (empty($vtrash_id)) {
$vtrash_id = $this->addVFolder($query, $flist, array(), $label);
$GLOBALS['prefs']->setValue('vtrash_id', $vtrash_id);
} else {
$this->addVFolder($query, $flist, array(), $label, $vtrash_id);
}
$this->_saveVFolder = true;
$_SESSION['imp']['search']['vtrash_id'] = $vtrash_id;
}
/**
* Determines whether a virtual folder ID is the Virtual Trash Folder.
*
* @param string $id The search query id to use (by default, will use
* the current ID set in the object).
*
* @return boolean True if the virutal folder ID is the Virtual Trash
* folder.
*/
function isVTrashFolder($id = null)
{
$id = $this->_strip($id);
$vtrash_id = $GLOBALS['prefs']->getValue('vtrash_id');
return (!empty($vtrash_id) && ($id == $vtrash_id));
}
/**
* Add a virtual INBOX folder for the current user.
*/
function createVINBOXFolder()
{
/* Initialize IMP_Tree. */
require_once IMP_BASE . '/lib/IMAP/Tree.php';
$imptree = &IMP_Tree::singleton();
/* Delete the current Virtual Inbox folder, if it exists. */
$vinbox_id = $GLOBALS['prefs']->getValue('vinbox_id');
if (!empty($vinbox_id)) {
$this->deleteSearchQuery($vinbox_id);
}
if (!$GLOBALS['prefs']->getValue('use_vinbox')) {
return;
}
/* Create Virtual INBOX with nav_poll list. Filter out any nav_poll
* entries that don't exist. Sort the list also. */
$flist = $imptree->getPollList(true, true);
require_once IMP_BASE . '/lib/IMAP/Search.php';
$query = new IMP_IMAP_Search_Query();
$query->seen(false);
$query->deleted(false);
$label = _("Virtual INBOX");
$this->_saveVFolder = false;
if (empty($vinbox_id)) {
$vinbox_id = $this->addVFolder($query, $flist, array(), $label);
$GLOBALS['prefs']->setValue('vinbox_id', $vinbox_id);
} else {
$this->addVFolder($query, $flist, array(), $label, $vinbox_id);
}
$this->_saveVFolder = true;
$_SESSION['imp']['search']['vinbox_id'] = $vinbox_id;
}
/**
* Determines whether a virtual folder ID is the Virtual INBOX Folder.
*
* @param string $id The search query id to use (by default, will use
* the current ID set in the object).
*
* @return boolean True if the virutal folder ID is the Virtual INBOX
* folder.
*/
function isVINBOXFolder($id = null)
{
$id = $this->_strip($id);
$vinbox_id = $GLOBALS['prefs']->getValue('vinbox_id');
return (!empty($vinbox_id) && ($id == $vinbox_id));
}
/**
* Is the current active folder an editable Virtual Folder?
*
* @param string $id The search query id to use (by default, will use
* the current ID set in the object).
*
* @return boolean True if the current folder is both a virtual folder
* and can be edited.
*/
function isEditableVFolder($id = null)
{
$id = $this->_strip($id);
return ($this->isVFolder($id) && !$this->isVTrashFolder($id) && !$this->isVINBOXFolder($id));
}
/**
* Return a list of IDs and query labels, sorted by the label.
*
* @param boolean $vfolder If true, only return Virtual Folders?
*
* @return array An array with the folder IDs as the key and the labels
* as the value.
*/
function listQueries($vfolder = false)
{
$vfolders = array();
if (empty($_SESSION['imp']['search']['q'])) {
return $vfolders;
}
foreach ($_SESSION['imp']['search']['q'] as $key => $val) {
if (!$vfolder || !empty($val['vfolder'])) {
$vfolders[$key] = $this->getLabel($key);
}
}
natcasesort($vfolders);
return $vfolders;
}
/**
* Get the list of searchable folders for the given search query.
*
* @param string $id The search query id to use (by default, will use
* the current ID set in the object).
*
* @return array The list of searchable folders.
*/
function getSearchFolders($id = null)
{
$id = $this->_strip($id);
return (isset($_SESSION['imp']['search']['q'][$id]['folders'])) ? $_SESSION['imp']['search']['q'][$id]['folders'] : array();
}
/**
* Return a list of search queries valid only for the current session
* (i.e. no virtual folders).
*
* @return array Keys are the search ids, values are a textual
* description of the search.
*/
function getSearchQueries()
{
$retarray = array();
foreach ($_SESSION['imp']['search']['q'] as $key => $val) {
if (!$this->isVFolder($key) &&
($text = $this->searchQueryText($key))) {
$retarray[$key] = $text;
}
}
return array_reverse($retarray, true);
}
/**
* Return search query text representation for a given search ID.
*
* @since IMP 4.2
*
* @param string $id The search query id to use (by default, will use
* the current ID set in the object).
*
* @return array The textual description of the search.
*/
function searchQueryText($id = null)
{
$id = $this->_strip($id);
if (empty($_SESSION['imp']['search']['q'][$id])) {
return '';
} elseif ($this->isVINBOXFolder($id) || $this->isVTrashFolder($id)) {
return $_SESSION['imp']['search']['q'][$id]['label'];
} elseif (empty($_SESSION['imp']['search']['q'][$id]['uiinfo'])) {
unset($_SESSION['imp']['search']['q'][$id]);
return '';
}
$searchfields = $this->searchFields();
$val = $_SESSION['imp']['search']['q'][$id];
$text = '';
if (!empty($val['uiinfo']['field'])) {
$text = _("Search") . ' ';
$text_array = array();
foreach ($val['uiinfo']['field'] as $key2 => $val2) {
if ($searchfields[$val2]['type'] == IMP_SEARCH_DATE) {
$text_array[] = sprintf("%s '%s'", $searchfields[$val2]['label'], strftime("%x", mktime(0, 0, 0, $val['uiinfo']['date'][$key2]['month'], $val['uiinfo']['date'][$key2]['day'], $val['uiinfo']['date'][$key2]['year'])));
} else {
$text_array[] = sprintf("%s for '%s'", $searchfields[$val2]['label'], ((!empty($val['uiinfo']['text_not'][$key2])) ? _("not") . ' ' : '') . $val['uiinfo']['text'][$key2]);
}
}
$text .= implode(' ' . (($val['uiinfo']['match'] == 'and') ? _("and") : _("or")) . ' ', $text_array);
}
return $text . ' ' . _("in") . ' ' . implode(', ', $val['uiinfo']['folders']);
}
/**
* Returns a link to edit a given search query.
*
* @param string $id The search query id to use (by default, will use
* the current ID set in the object).
*
* @rerturn string The URL to the search page.
*/
function editURL($id = null)
{
$id = $this->_strip($id);
return Util::addParameter(Horde::applicationUrl('search.php'), array('edit_query' => $id));
}
/**
* Returns a link to delete a given search query.
*
* @param string $id The search query id to use (by default, will use
* the current ID set in the object).
*
* @return string The URL to allow deletion of the search query.
*/
function deleteURL($id = null)
{
$id = $this->_strip($id);
return Util::addParameter(Horde::applicationUrl('folders.php'),
array('actionID' => 'delete_search_query',
'folders_token' => IMP::getRequestToken('imp.folders'),
'queryid' => $id,
));
}
/**
* Is the given mailbox a search mailbox?
*
* @param string $id The search query id to use (by default, will use
* the current ID set in the object).
*
* @return boolean Whether the given mailbox name is a search mailbox.
*/
function isSearchMbox($id = null)
{
return ($id === null) ? !empty($this->_id) : isset($_SESSION['imp']['search']['q'][$this->_strip($id)]);
}
/**
* Is the given mailbox a virtual folder?
*
* @param string $id The search query id to use (by default, will use
* the current ID set in the object).
*
* @return boolean Whether the given mailbox name is a virtual folder.
*/
function isVFolder($id = null)
{
$id = $this->_strip($id);
return (!empty($_SESSION['imp']['search']['q'][$id]['vfolder']));
}
/**
* Get the ID for the search mailbox, if we are currently in a search
* mailbox.
*
* @return mixed The search ID if in a mailbox, else false.
*/
function searchMboxID()
{
return ($this->_id !== null) ? $this->_id : false;
}
/**
* Strip the identifying label from a mailbox ID.
*
* @access private
*
* @param string $id The mailbox query ID.
*
* @return string The virtual folder ID, with any IMP specific identifying
* information stripped off.
*/
function _strip($id)
{
return ($id === null) ? $this->_id : ((strpos($id, IMP_SEARCH_MBOX) === 0) ? substr($id, strlen(IMP_SEARCH_MBOX)) : $id);
}
/**
* Create the canonical search ID for a given search query.
*
* @since IMP 4.1.2
*
* @access public
*
* @param string $id The mailbox query ID.
*
* @return string The canonical search query ID.
*/
function createSearchID($id)
{
return IMP_SEARCH_MBOX . $this->_strip($id);
}
/**
* Return the base search fields.
*
* @return array The base search fields.
*/
function searchFields()
{
return array(
'from' => array(
'label' => _("From"),
'type' => IMP_SEARCH_HEADER
),
'to' => array(
'label' => _("To"),
'type' => IMP_SEARCH_HEADER
),
'cc' => array(
'label' => _("Cc"),
'type' => IMP_SEARCH_HEADER
),
'bcc' => array(
'label' => _("Bcc"),
'type' => IMP_SEARCH_HEADER
),
'subject' => array(
'label' => _("Subject"),
'type' => IMP_SEARCH_HEADER
),
'body' => array(
'label' => _("Body"),
'type' => IMP_SEARCH_BODY
),
'text' => array(
'label' => _("Entire Message"),
'type' => IMP_SEARCH_TEXT
),
'date_on' => array(
'label' => _("Date ="),
'type' => IMP_SEARCH_DATE
),
'date_until' => array(
'label' => _("Date <"),
'type' => IMP_SEARCH_DATE
),
'date_since' => array(
'label' => _("Date >="),
'type' => IMP_SEARCH_DATE
)
);
}
/**
* Update IMAP_Tree object.
*
* @access private
*
* @param string $action Either 'delete' or 'add'.
* @param string $id The query ID to update.
* @param string $label If $action = 'add', the label to use for the
* query ID.
*/
function _updateIMPTree($action, $id, $label = null)
{
require_once IMP_BASE . '/lib/IMAP/Tree.php';
$imptree = &IMP_Tree::singleton();
switch ($action) {
case 'delete':
$imptree->delete($id);
break;
case 'add':
$imptree->insertVFolders(array($id => $label));
break;
}
}
/**
* Return an IMAP_Search object.
*
* @access private
*
* @return IMAP_Search The IMAP_Search object.
*/
function &_getIMAPSearch()
{
$charset = NLS::getCharset();
$search_params = array('pop3' => ($_SESSION['imp']['base_protocol'] == 'pop3'), 'charset' => $charset);
/* Check if the IMAP server supports searches in the current
* charset. */
if (empty($_SESSION['imp']['imap_server']['search_charset'][$charset])) {
$search_params['no_imap_charset'] = true;
}
require_once IMP_BASE . '/lib/IMAP/Search.php';
$imap_search = &IMP_IMAP_Search::singleton($search_params);
return $imap_search;
}
}
|