[ Index ]
 

Code source de IMP H3 (4.1.5)

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/lib/IMAP/ -> Tree.php (source)

   1  <?php
   2  
   3  require_once 'Horde/IMAP/Tree.php';
   4  
   5  /** Identify an element as a virtual folder. */
   6  define('IMPTREE_ELT_VFOLDER', 8192);
   7  
   8  /**
   9   * The IMP_tree class provides a tree view of the folders in an IMAP/POP3
  10   * repository.  It provides access functions to iterate through this tree and
  11   * query information about individual mailboxes.
  12   *
  13   * $Horde: imp/lib/IMAP/Tree.php,v 1.25.2.45 2007/04/18 13:20:53 slusarz Exp $
  14   *
  15   * Copyright 2000-2007 Chuck Hagenbuch <chuck@horde.org>
  16   * Copyright 2000-2007 Jon Parise <jon@horde.org>
  17   * Copyright 2000-2007 Anil Madhavapeddy <avsm@horde.org>
  18   * Copyright 2003-2007 Michael Slusarz <slusarz@horde.org>
  19   *
  20   * See the enclosed file COPYING for license information (GPL). If you did not
  21   * receive this file, see http://www.fsf.org/copyleft/gpl.html.
  22   *
  23   * @author  Chuck Hagenbuch <chuck@horde.org>
  24   * @author  Jon Parise <jon@horde.org>
  25   * @author  Anil Madhavapeddy <avsm@horde.org>
  26   * @author  Michael Slusarz <slusarz@horde.org>
  27   * @since   IMP 2.3
  28   * @package IMP
  29   */
  30  class IMP_Tree extends IMAP_Tree {
  31  
  32      /**
  33       * Mapping for virtual folders to their label.
  34       *
  35       * @var array
  36       */
  37      var $_vfolders = array();
  38  
  39      /**
  40       * Are we inheriting from the new (i.e. Horde 3.1+) version of IMAP_Tree?
  41       *
  42       * @var boolean
  43       */
  44      var $_newimaptree = false;
  45  
  46      /**
  47       * Singleton method.
  48       * By default, the tree will contain only currently viewable items and
  49       * will be stored in the session under 'imp'.  However, if you would
  50       * rather pass different parameters to IMAP_Tree, you may declare a
  51       * global variable named $imp_tree_singleton with the array of parameters
  52       * you want to pass to IMAP_Tree::singleton().
  53       */
  54      function &singleton()
  55      {
  56          if (empty($GLOBALS['imp_tree_singleton'])) {
  57              return IMAP_Tree::singleton('imp', 'IMP_Tree', true);
  58          } else {
  59              $ret = call_user_func_array(array('IMAP_Tree', 'singleton'), $GLOBALS['imp_tree_singleton']);
  60              return $ret;
  61          }
  62      }
  63  
  64      /**
  65       * Constructor.
  66       *
  67       * @see IMAP_Tree::IMAP_Tree()
  68       */
  69      function IMP_Tree($init = IMAPTREE_INIT_SUB, $cachename = null)
  70      {
  71          global $imp;
  72  
  73          $this->_app = 'imp';
  74          $this->_mode = IMAPTREE_MODE_MAIL;
  75          $this->_server = IMP::serverString();
  76  
  77          if ($imp['base_protocol'] != 'pop3') {
  78              $ptr = reset($_SESSION['imp']['namespace']);
  79              $this->_delimiter = $ptr['delimiter'];
  80              if (method_exists($this, 'extendedNamespaceSupport')) {
  81                  $this->_newimaptree = true;
  82                  $this->IMAPchildrenSupport($_SESSION['imp']['imap_server']['children']);
  83                  $this->_namespaces = (empty($GLOBALS['conf']['user']['allow_folders'])) ? array() : $_SESSION['imp']['namespace'];
  84              } else {
  85                  // BC for Horde < 3.1.0
  86                  $this->_delimiter = $ptr['delimiter'];
  87                  $this->_prefix = $ptr['name'];
  88              }
  89          }
  90  
  91          parent::IMAP_Tree(null);
  92      }
  93  
  94      /**
  95       * Initalizes the list at the top level of the hierarchy.
  96       *
  97       * @see IMAP_Tree::_init()
  98       */
  99      function init($init = null)
 100      {
 101          static $already_init = null;
 102  
 103          $initmask = (($GLOBALS['imp']['base_protocol'] == 'pop3') ||
 104                       !$GLOBALS['prefs']->getValue('subscribe') ||
 105                       $GLOBALS['imp']['showunsub'])
 106              ? IMAPTREE_INIT_UNSUB : IMAPTREE_INIT_SUB;
 107          if ($GLOBALS['prefs']->getValue('show_sidebar')) {
 108              $initmask |= IMAPTREE_INIT_FETCHALL;
 109          }
 110  
 111          if ($already_init == $initmask) {
 112              return;
 113          } else {
 114              $already_init = $initmask;
 115          }
 116          parent::init($initmask);
 117  
 118          /* Convert 'INBOX' to localized name. */
 119          $this->_tree['INBOX']['l'] = _("Inbox");
 120  
 121          /* Add virtual folders to the tree. */
 122          $this->_vfolders = array();
 123          $this->insertVFolders($GLOBALS['imp_search']->listQueries(true));
 124      }
 125  
 126      /**
 127       * Inserts virtual folders into the tree.
 128       *
 129       * @param array $id_list  An array with the folder IDs to add as the key
 130       *                        and the labels as the value.
 131       */
 132      function insertVFolders($id_list)
 133      {
 134          if (empty($id_list)) {
 135              return;
 136          }
 137  
 138          $id = array();
 139          foreach ($id_list as $key => $val) {
 140              $id[$GLOBALS['imp_search']->createSearchID($key)] = $val;
 141          }
 142  
 143          if (empty($this->_vfolders)) {
 144              $this->_vfolders = $id;
 145              $id = array_merge(array(_("Virtual Folders")), array_keys($id));
 146          } else {
 147              $this->_vfolders = array_merge($this->_vfolders, $id);
 148              $id = array_keys($id);
 149          }
 150  
 151          $this->_nonimapelt = true;
 152          $this->_initmode = IMAPTREE_INIT_SUB;
 153          $this->insert($id);
 154          $this->_initmode = 0;
 155          $this->_nonimapelt = false;
 156  
 157          /* Sort the Virtual Folder list in the object, if necessary. */
 158          if ($this->_needSort($this->_tree[_("Virtual Folders")])) {
 159              $vsort = array();
 160              foreach ($this->_parent[_("Virtual Folders")] as $val) {
 161                  $vsort[$val] = $this->_tree[$val]['l'];
 162              }
 163              natcasesort($vsort);
 164              $this->_parent[_("Virtual Folders")] = array_keys($vsort);
 165              $this->_setNeedSort($this->_tree[_("Virtual Folders")], false);
 166              $this->_changed = true;
 167          }
 168      }
 169  
 170      /**
 171       * Subclass specific initialization tasks.
 172       *
 173       * @see IMAP_Tree::_init()
 174       */
 175      function _init()
 176      {
 177          $boxes = array();
 178  
 179          if (empty($GLOBALS['conf']['user']['allow_folders'])) {
 180              $boxes['INBOX'] = $this->_getMailbox('INBOX');
 181              return $boxes;
 182          }
 183  
 184          foreach ($_SESSION['imp']['namespace'] as $val) {
 185              /* We only need to provide the list of folders in the base
 186               * personal namespace.  Else, just use the base namespace entry. */
 187              if (($val['type'] == 'personal') || empty($val['name'])) {
 188                  $query = $val['name'] . '%';
 189              } else {
 190                  if (empty($val['delimiter'])) {
 191                      $query = $val['name'];
 192                  } else {
 193                      $query = rtrim($val['name'], $val['delimiter']);
 194                  }
 195                  if (isset($tmp[$query])) {
 196                      continue;
 197                  }
 198              }
 199              $tmp = $this->_getList($query);
 200              if (!empty($tmp)) {
 201                  if ($val['type'] == 'personal') {
 202                      /* IMAP servers put the INBOX in the personal namespace -
 203                       * simply rename to 'INBOX' since that is where we
 204                       * always access the mailbox. */
 205                      $inbox_str = $val['name'] . 'INBOX';
 206                      if (!empty($val['name']) && isset($tmp[$inbox_str])) {
 207                          $tmp = array('INBOX' => $tmp[$inbox_str]) + $tmp;
 208                          $tmp['INBOX']->name = 'INBOX';
 209                          unset($tmp[$inbox_str]);
 210                      }
 211                  }
 212                  $boxes = array_merge($boxes, $tmp);
 213              }
 214          }
 215  
 216          if (!isset($boxes['INBOX'])) {
 217              $boxes['INBOX'] = $this->_getMailbox('INBOX');
 218          }
 219  
 220          /* Do a sort to make sure that 'INBOX' always appears as the first
 221           * element. */
 222          require_once  HORDE_BASE . '/lib/version.php';
 223          if (version_compare(HORDE_VERSION, '3.0.4') == -1) {
 224              $this->_sortList($boxes, true);
 225          }
 226  
 227          return $boxes;
 228      }
 229  
 230      /**
 231       * Adds aliases to a tree element and returns the resulting array.
 232       *
 233       * @access protected
 234       *
 235       * @param array $elt  A tree element.
 236       *
 237       * @return array  A tree element with the aliases added.
 238       */
 239      function _addAliases($elt)
 240      {
 241          $elt = parent::_addAliases($elt);
 242          if ($elt['label'] == 'INBOX') {
 243              $elt['label'] = _("Inbox");
 244          }
 245  
 246          return $elt;
 247      }
 248  
 249      /**
 250       * Returns a reference to a currently open IMAP stream.
 251       *
 252       * @see IMAP_Tree::_getStream()
 253       */
 254      function &_getStream()
 255      {
 256          return $_SESSION['imp']['stream'];
 257      }
 258  
 259      /**
 260       * Initializes the expanded folder list.
 261       *
 262       * @see IMAP_Tree::_initExpandedList()
 263       */
 264      function _initExpandedList()
 265      {
 266          if (is_null($this->_expanded)) {
 267              $serialized = $GLOBALS['prefs']->getValue('expanded_folders');
 268              $this->_expanded = ($serialized) ? unserialize($serialized) : array();
 269          }
 270      }
 271  
 272      /**
 273       * Adds an element to the expanded list.
 274       *
 275       * @see IMAP_Tree::_addExpandedList()
 276       */
 277      function _addExpandedList($id)
 278      {
 279          $this->_initExpandedList();
 280          $this->_expanded[$id] = true;
 281          $GLOBALS['prefs']->setValue('expanded_folders', serialize($this->_expanded));
 282      }
 283  
 284      /**
 285       * Removes an element from the expanded list.
 286       *
 287       * @see IMAP_Tree::_removeExpandedList()
 288       */
 289      function _removeExpandedList($id)
 290      {
 291          $this->_initExpandedList();
 292          unset($this->_expanded[$id]);
 293          $GLOBALS['prefs']->setValue('expanded_folders', serialize($this->_expanded));
 294      }
 295  
 296      /**
 297       * Initializes and returns the list of mailboxes to poll.
 298       *
 299       * @see IMAP_Tree::getPollList()
 300       */
 301      function getPollList()
 302      {
 303          if (is_null($this->_poll)) {
 304              /* We ALWAYS poll the INBOX. */
 305              $this->_poll = array('INBOX' => 1);
 306  
 307              /* Add the list of polled mailboxes from the prefs. */
 308              $navPollList = @unserialize($GLOBALS['prefs']->getValue('nav_poll'));
 309              if ($navPollList) {
 310                  $this->_poll += $navPollList;
 311              }
 312          }
 313  
 314          return $this->_poll;
 315      }
 316  
 317      /**
 318       * Adds element(s) to the poll list.
 319       *
 320       * @see IMAP_Tree::addPollList()
 321       */
 322      function addPollList($id)
 323      {
 324          if (!is_array($id)) {
 325              $id = array($id);
 326          }
 327  
 328          if (!empty($id) && !$GLOBALS['prefs']->isLocked('nav_poll')) {
 329              require_once  IMP_BASE . '/lib/Folder.php';
 330              $imp_folder = &IMP_Folder::singleton();
 331              $this->getPollList();
 332              foreach ($id as $val) {
 333                  if (!$this->isSubscribed($this->_tree[$val])) {
 334                      $imp_folder->subscribe(array($val));
 335                  }
 336                  $this->_poll[$val] = true;
 337                  $this->_setPolled($this->_tree[$val], true);
 338              }
 339              $GLOBALS['prefs']->setValue('nav_poll', serialize($this->_poll));
 340              $this->_changed = true;
 341          }
 342      }
 343  
 344      /**
 345       * Removes element(s) from the poll list.
 346       *
 347       * @see IMAP_Tree::removePollList()
 348       */
 349      function removePollList($id)
 350      {
 351          if (!is_array($id)) {
 352              $id = array($id);
 353          }
 354  
 355          $removed = false;
 356  
 357          if (!$GLOBALS['prefs']->isLocked('nav_poll')) {
 358              $this->getPollList();
 359              foreach ($id as $val) {
 360                  if ($val != 'INBOX') {
 361                      unset($this->_poll[$val]);
 362                      if (isset($this->_tree[$val])) {
 363                          $this->_setPolled($this->_tree[$val], false);
 364                      }
 365                      $removed = true;
 366                  }
 367              }
 368              if ($removed) {
 369                  $GLOBALS['prefs']->setValue('nav_poll', serialize($this->_poll));
 370                  $this->_changed = true;
 371              }
 372          }
 373      }
 374  
 375      /**
 376       * Returns the currently selected initialization expanded mode.
 377       *
 378       * @see IMAP_Tree::_getInitExpandedMode()
 379       */
 380      function _getInitExpandedMode()
 381      {
 382          return $GLOBALS['prefs']->getValue('nav_expanded');
 383      }
 384  
 385      /**
 386       * Creates the virtual folder container.
 387       *
 388       * @access private
 389       *
 390       * @return array  A mailbox element.
 391       */
 392      function _createVFolderContainer()
 393      {
 394          $base = _("Virtual Folders");
 395  
 396          $ob = &new stdClass;
 397          $ob->delimiter = $this->_delimiter;
 398          $ob->attributes = LATT_NOSELECT | LATT_HASCHILDREN | IMAPTREE_ELT_IS_DISCOVERED | IMAPTREE_ELT_IS_SUBSCRIBED | IMPTREE_ELT_VFOLDER;
 399          $ob->fullServerPath = $ob->name = $base;
 400  
 401          $elt = $this->_makeMailboxTreeElt($ob);
 402          $elt['l'] = $elt['v'] = $base;
 403  
 404          return $elt;
 405      }
 406  
 407      /**
 408       * Creates a virtual folder element.
 409       *
 410       * @access private
 411       *
 412       * @param string $vfolder  Virtual folder ID.
 413       *
 414       * @return array  A mailbox element.
 415       */
 416      function _createVFolderElt($vfolder)
 417      {
 418          $base = _("Virtual Folders");
 419  
 420          $ob = &new stdClass;
 421          $ob->delimiter = $this->_delimiter;
 422          $ob->attributes = LATT_HASNOCHILDREN | IMAPTREE_ELT_IS_DISCOVERED | IMAPTREE_ELT_IS_SUBSCRIBED | IMPTREE_ELT_VFOLDER;
 423          $ob->name = $base . $this->_delimiter . $vfolder;
 424          $ob->fullServerPath = $ob->name;
 425  
 426          $elt = $this->_makeMailboxTreeElt($ob);
 427          $elt['l'] = $this->_vfolders[$vfolder];
 428          $elt['v'] = $vfolder;
 429  
 430          return $elt;
 431      }
 432  
 433      /**
 434       * Returns whether this element is a virtual folder.
 435       *
 436       * @param array $elt  A tree element.
 437       *
 438       * @return integer  True if the element is a virtual folder.
 439       */
 440      function isVFolder($elt)
 441      {
 442          return $elt['a'] & IMPTREE_ELT_VFOLDER;
 443      }
 444  
 445      /**
 446       * Returns a non-IMAP mailbox element given an element identifier.
 447       *
 448       * @access private
 449       *
 450       * @param string $id  The element identifier.
 451       *
 452       * @return array  A mailbox element.
 453       */
 454      function _getNonIMAPElt($id)
 455      {
 456          if ($id == _("Virtual Folders")) {
 457              return $this->_createVfolderContainer();
 458          } else {
 459              return $this->_createVfolderElt($id);
 460          }
 461      }
 462  
 463      /**
 464       * Deletes an element from the tree.
 465       *
 466       * @see IMAP_Tree::delete()
 467       */
 468      function delete($id)
 469      {
 470          if (!is_array($id)) {
 471              $vfolder_base = ($id == _("Virtual Folders"));
 472              $search_id = $GLOBALS['imp_search']->createSearchID($id);
 473  
 474              if (($vfolder_base ||
 475                   (isset($this->_tree[$search_id]) &&
 476                    $this->isVFolder($this->_tree[$search_id])))) {
 477                  if (!$vfolder_base) {
 478                      $id = $search_id;
 479                  }
 480                  $parent = $this->_tree[$id]['p'];
 481                  unset($this->_vfolders[$id]);
 482                  unset($this->_tree[$id]);
 483  
 484                  /* Delete the entry from the parent tree. */
 485                  $key = array_search($id, $this->_parent[$parent]);
 486                  unset($this->_parent[$parent][$key]);
 487  
 488                  /* Rebuild the parent tree. */
 489                  if (!$vfolder_base && empty($this->_parent[$parent])) {
 490                      $this->delete($parent);
 491                  } else {
 492                      $this->_parent[$parent] = array_values($this->_parent[$parent]);
 493                  }
 494                  $this->_changed = true;
 495  
 496                  return true;
 497              }
 498          }
 499  
 500          return parent::delete($id);
 501      }
 502  
 503      /**
 504       * Rename a current folder.
 505       *
 506       * @since IMP 4.1
 507       *
 508       * @param array $old  The old folder names.
 509       * @param array $new  The new folder names.
 510       */
 511      function rename($old, $new)
 512      {
 513          foreach ($old as $key => $val) {
 514              $polled = (isset($this->_tree[$val])) ? $this->isPolled($this->_tree[$val]) : false;
 515              if ($this->delete($val)) {
 516                  $this->insert($new[$key]);
 517                  if ($polled) {
 518                      $this->addPollList($new[$key]);
 519                  }
 520              }
 521          }
 522      }
 523  
 524      /**
 525       * Does the element have any children?
 526       *
 527       * @see IMAP_Tree::hasChildren()
 528       */
 529      function hasChildren($elt, $viewable = false)
 530      {
 531          if ($this->isVFolder($elt) && $this->isContainer($elt)) {
 532              return true;
 533          }
 534          return parent::hasChildren($elt, $viewable);
 535      }
 536  
 537      /**
 538       * Initializes the list of subscribed mailboxes.
 539       *
 540       * @deprecated since Horde 3.1
 541       *
 542       * @see IMAP_Tree::_initSubscribed()
 543       */
 544      function _initSubscribed()
 545      {
 546          $hsub = (is_null($this->_subscribed));
 547          parent::_initSubscribed();
 548  
 549          /* Add in other hierarchy subscription information. */
 550          if (!$this->_newimaptree && $hsub && $this->_namespaces) {
 551              foreach ($this->_namespaces as $val) {
 552                  $sublist = @imap_lsub($this->_getStream(), $this->_server, $val['name'] . '*');
 553                  if (!empty($sublist)) {
 554                      foreach ($sublist as $val2) {
 555                          $this->_subscribed[substr($val2, strpos($val2, '}') + 1)] = 1;
 556                      }
 557                  }
 558              }
 559          }
 560      }
 561  
 562      /**
 563       * Returns a list of all IMAP folders in the tree (i.e. not containers or
 564       * non-imap elements).
 565       *
 566       * @todo Move core code to framework.
 567       * @since IMP 4.1
 568       *
 569       * @return array  An array of IMAP mailbox names.
 570       */
 571      function folderList()
 572      {
 573          $ret_array = array();
 574  
 575          $mailbox = $this->reset();
 576          do {
 577              if (!$this->isContainer($mailbox) &&
 578                  !$this->isVFolder($mailbox)) {
 579                  $ret_array[] = $mailbox['v'];
 580              }
 581          } while (($mailbox = $this->next(IMAPTREE_NEXT_SHOWCLOSED)));
 582  
 583          return $ret_array;
 584      }
 585  
 586      /**
 587       * Is the mailbox open in the sidebar?
 588       *
 589       * @since IMP 4.1.1
 590       *
 591       * @param array $mbox  A mailbox name.
 592       *
 593       * @return integer  True if the mailbox is open in the sidebar.
 594       */
 595      function isOpenSidebar($mbox)
 596      {
 597          switch ($GLOBALS['prefs']->getValue('nav_expanded_sidebar')) {
 598          case IMAPTREE_OPEN_USER:
 599              $this->_initExpandedList();
 600              return !empty($this->_expanded[$mbox]);
 601              break;
 602  
 603          case IMAPTREE_OPEN_ALL:
 604              return true;
 605              break;
 606  
 607          case IMAPTREE_OPEN_NONE:
 608          default:
 609              return false;
 610              break;
 611          }
 612      }
 613  
 614  }


Généré le : Thu Nov 29 12:30:07 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics