[ 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/Fetchmail/ -> imap.php (source)

   1  <?php
   2  /**
   3   * The IMP_Fetchmail_imap driver implements the IMAP_Fetchmail class for use
   4   * with IMAP/POP3 servers.
   5   *
   6   * $Horde: imp/lib/Fetchmail/imap.php,v 1.5.10.10 2007/05/12 16:32:53 chuck Exp $
   7   *
   8   * Copyright 2002-2007 Nuno Loureiro <nuno@co.sapo.pt>
   9   * Copyright 2004-2007 Michael Slusarz <slusarz@bigworm.colorado.edu>
  10   *
  11   * See the enclosed file COPYING for license information (GPL). If you
  12   * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  13   *
  14   * @author  Nuno Loureiro <nuno@co.sapo.pt>
  15   * @author  Michael Slusarz <slusarz@bigworm.colorado.edu>
  16   * @since   IMP 4.0
  17   * @package IMP
  18   */
  19  class IMP_Fetchmail_imap extends IMP_Fetchmail {
  20  
  21      /**
  22       * The internal protocols list.
  23       *
  24       * @var array
  25       */
  26      var $_protocols;
  27  
  28      /**
  29       * The current protocol being used.
  30       *
  31       * @var string
  32       */
  33      var $_currprotocol;
  34  
  35      /**
  36       * The server string to use to connect to the remote mail server.
  37       *
  38       * @var string
  39       */
  40      var $_serverstring;
  41  
  42      /**
  43       * Returns a description of the driver.
  44       *
  45       * @see IMP_Fetchmail::description()
  46       */
  47      function description()
  48      {
  49          return _("IMAP/POP3 Mail Servers");
  50      }
  51  
  52      /**
  53       * Constructor.
  54       *
  55       * @param array $params  The configuration parameter array.
  56       */
  57      function IMP_Fetchmail_imap($params)
  58      {
  59          /* Set up the protocols array. */
  60          $this->_protocols = $this->_protocolList();
  61  
  62          parent::IMP_Fetchmail($params);
  63      }
  64  
  65      /**
  66       * Return a list of protocols supported by this driver.
  67       *
  68       * @see IMP_Fetchmail::getProtocolList()
  69       */
  70      function getProtocolList()
  71      {
  72          $output = array();
  73          foreach ($this->_protocols as $key => $val) {
  74              $output[$key] = $val['name'];
  75          }
  76          return $output;
  77      }
  78  
  79      /**
  80       * Returns the list of IMAP/POP3 protocols that this driver supports, and
  81       * associated configuration options.
  82       * This needs to be in a separate function because PHP will not allow
  83       * gettext strings to appear in member variables.
  84       *
  85       * @access private
  86       *
  87       * @return array  The protocol configuration list.
  88       */
  89      function _protocolList()
  90      {
  91          return array(
  92              'pop3auto' => array(
  93                  'name' => _("POP3 (Auto Detect Protocols)"),
  94                  'auto' => array('pop3', 'pop3notls', 'pop3sslvalid', 'pop3ssl'),
  95                  'base' => 'POP3'
  96              ),
  97              'pop3' => array(
  98                  'name' => _("POP3"),
  99                  'string' => 'pop3',
 100                  'port' => 110,
 101                  'base' => 'POP3'
 102              ),
 103              'pop3notls' => array(
 104                  'name' => _("POP3, no TLS"),
 105                  'string' => 'pop3/notls',
 106                  'port' => 110,
 107                  'base' => 'POP3'
 108              ),
 109              'pop3sslvalid' => array(
 110                  'name' => _("POP3 over SSL"),
 111                  'string' => 'pop3/ssl',
 112                  'port' => 995,
 113                  'base' => 'POP3'
 114              ),
 115              'pop3ssl' => array(
 116                  'name' => _("POP3 over SSL (self-signed certificate)"),
 117                  'string' => 'pop3/ssl/novalidate-cert',
 118                  'port' => 995,
 119                  'base' => 'POP3'
 120              ),
 121              'imapauto' => array(
 122                  'name' => _("IMAP (Auto Detect Protocols)"),
 123                  'auto' => array('imap', 'imapnotls', 'imapsslvalid', 'imapssl'),
 124                  'base' => 'IMAP'
 125              ),
 126              'imap' => array(
 127                  'name' => _("IMAP"),
 128                  'string' => 'imap',
 129                  'port' => 143,
 130                  'base' => 'IMAP'
 131              ),
 132              'imapnotls' => array(
 133                  'name' => _("IMAP, no TLS"),
 134                  'string' => 'imap/notls',
 135                  'port' => 143,
 136                  'base' => 'IMAP'
 137              ),
 138              'imapsslvalid' => array(
 139                  'name' => _("IMAP over SSL"),
 140                  'string' => 'imap/ssl',
 141                  'port' => 993,
 142                  'base' => 'IMAP'
 143              ),
 144              'imapssl' => array(
 145                  'name' => _("IMAP over SSL (self-signed certificate)"),
 146                  'string' => 'imap/ssl/novalidate-cert',
 147                  'port' => 993,
 148                  'base' => 'IMAP'
 149              )
 150          );
 151      }
 152  
 153      /**
 154       * Checks if the remote mailbox exists.
 155       *
 156       * @access private
 157       *
 158       * @param resource $stream  A valid IMAP resource stream.
 159       *
 160       * @return boolean  Does the remote mailbox exist?
 161       */
 162      function _remoteMboxExists($stream)
 163      {
 164          if (empty($this->_params['rmailbox'])) {
 165              return false;
 166          }
 167  
 168          if ($this->_params['rmailbox'] == 'INBOX') {
 169              /* INBOX always exists and is a special case. */
 170              return true;
 171          }
 172  
 173          return (bool) @imap_list($stream, $this->_serverstring, $this->_params['rmailbox']);
 174      }
 175  
 176      /**
 177       * Attempts to connect to the mail server
 178       *
 179       * @access private
 180       *
 181       * @return mixed  Returns an IMAP Stream or PEAR_Error on failure.
 182       */
 183      function _connect()
 184      {
 185          /* Create the server string now. */
 186          $this->_serverstring = '{' . $this->_params['server'] . ':' . $this->_protocols[$this->_currprotocol]['port'] . '/'. $this->_protocols[$this->_currprotocol]['string'] . '}';
 187          $server_string = $this->_serverstring;
 188  
 189          if ($this->_protocols[$this->_currprotocol]['base'] == 'IMAP') {
 190              $server_string .= $this->_params['rmailbox'];
 191          }
 192  
 193          $stream = @imap_open($server_string, $this->_params['username'], $this->_params['password']);
 194  
 195          if ($stream === false) {
 196              $errstr = imap_last_error();
 197              if ($errstr) {
 198                  return PEAR::raiseError(_("Cannot connect to the remote mail server: ") . $errstr);
 199              } else {
 200                  return PEAR::raiseError(_("Cannot connect to the remote mail server."));
 201              }
 202          }
 203  
 204          return $stream;
 205      }
 206  
 207      /**
 208       * Gets the mailbody and calls the custom filter function.
 209       * Remove bare newlines and sets message color.
 210       *
 211       * @access private
 212       *
 213       * @param resource $stream  IMAP connection stream.
 214       * @param integer $uid      UID of message to fetch.
 215       *
 216       * @return string  Corrected mail content.
 217       */
 218      function _getMailMessage($stream, $uid)
 219      {
 220          /* Get the message headers. */
 221          $header = @imap_fetchheader($stream, $uid, FT_UID);
 222  
 223          /* Get the message body. */
 224          $body = @imap_body($stream, $uid, FT_UID | FT_PEEK);
 225  
 226          return parent::_processMailMessage($header, $body);
 227      }
 228  
 229      /**
 230       * Gets the mail using the data in this object.
 231       *
 232       * @see IMP_Fetchmail::getMail()
 233       */
 234      function getMail()
 235      {
 236          if (isset($this->_protocols[$this->_params['protocol']]['auto'])) {
 237              $protocols = $this->_protocols[$this->_params['protocol']]['auto'];
 238          } else {
 239              $protocols = array($this->_params['protocol']);
 240          }
 241  
 242          foreach ($protocols as $val) {
 243              $this->_currprotocol = $val;
 244              $ret = $this->_getMail();
 245              if (!is_a($ret, 'PEAR_Error')) {
 246                  break;
 247              }
 248          }
 249  
 250          return $ret;
 251      }
 252  
 253      /**
 254       * Internal function used to get mail from a single server.
 255       *
 256       * @return mixed  Returns PEAR_Error on error, the number of messages
 257       *                fetched on success.
 258       */
 259      function _getMail()
 260      {
 261          $numMsgs = 0;
 262          $protocols = array();
 263  
 264          $stream = $this->_connect();
 265          if (is_a($stream, 'PEAR_Error')) {
 266              return $stream;
 267          }
 268  
 269          /* Check to see if remote mailbox exists. */
 270          $useimap = ($this->_protocols[$this->_currprotocol]['base'] == 'IMAP');
 271          if ($useimap) {
 272              if (!$this->_remoteMboxExists($stream)) {
 273                  @imap_close($stream);
 274                  return PEAR::raiseError(_("Invalid Remote Mailbox"));
 275              }
 276          }
 277  
 278          $msg_count = @imap_num_msg($stream);
 279          if (!$msg_count) {
 280              @imap_close($stream);
 281              return 0;
 282          }
 283  
 284          $overview = @imap_fetch_overview($stream, '1:' . $msg_count);
 285          foreach ($overview as $h) {
 286              if (($this->_params['onlynew'] &&
 287                   ($h->recent || !$h->seen) &&
 288                   !$h->deleted) ||
 289                  (!$this->_params['onlynew'] && !$h->deleted)) {
 290                  /* Check message size. */
 291                  if (!$this->_checkMessageSize($h->size, isset($h->subject) ? $h->subject : '', $h->from)) {
 292                      continue;
 293                  }
 294  
 295                  /* Get the complete message. */
 296                  $mail_source = $this->_getMailMessage($stream, $h->uid);
 297  
 298                  /* Append to the server. */
 299                  if ($this->_addMessage($mail_source)) {
 300                      $flags = array();
 301                      $numMsgs++;
 302  
 303                      /* Remove the mail if 'del' is set. */
 304                      if ($this->_params['del']) {
 305                          $flags[] = "\\Deleted";
 306                      }
 307  
 308                      /* Mark message seen if 'markseen' is set. */
 309                      if ($useimap && $this->_params['markseen']) {
 310                          $flags[] = "\\Seen";
 311                      }
 312  
 313                      if (!empty($flags)) {
 314                          @imap_setflag_full($stream, $h->uid, implode(' ', $flags), ST_UID);
 315                      }
 316                  }
 317              }
 318          }
 319  
 320          /* Expunge all deleted messages now. */
 321          if ($this->_params['del']) {
 322              @imap_close($stream, CL_EXPUNGE);
 323          } else {
 324              @imap_close($stream);
 325          }
 326  
 327          return $numMsgs;
 328      }
 329  
 330  }


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