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

   1  <?php
   2  /**
   3   * The IMP_Fetchmail:: class provides an interface to download mail from
   4   * remote mail servers.
   5   *
   6   * $Horde: imp/lib/Fetchmail.php,v 1.41.8.12 2007/01/02 13:54:56 jan 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 {
  20  
  21      /**
  22       * Parameters used by the driver.
  23       *
  24       * @var array
  25       */
  26      var $_params;
  27  
  28      /**
  29       * The list of active fetchmail parameters for the current driver.
  30       * ALL DRIVERS SHOULD UNSET ANY FETCHMAIL PARAMETERS THEY DO NOT USE
  31       * OR ELSE THEY WILL APPEAR IN THE PREFERENCES PAGE.
  32       * The following parameters are available:
  33       *   'id'          --  The account name.
  34       *   'driver'      --  The driver to use.
  35       *   'protocol'    --  The protocol type.
  36       *   'username'    --  The username on the remote server.
  37       *   'password'    --  The password on the remote server.
  38       *   'server'      --  The remote server name/address.
  39       *   'rmailbox'    --  The remote mailbox name.
  40       *   'lmailbox'    --  The local mailbox to download messages to.
  41       *   'onlynew'     --  Only retrieve new messages?
  42       *   'markseen'    --  Mark messages as seen?
  43       *   'del'         --  Delete messages after fetching?
  44       *   'loginfetch'  --  Fetch mail from other accounts on login?
  45       *   'acctcolor'   --  Should these messages be colored differently
  46       *                     in mailbox view?
  47       *
  48       * @var array
  49       */
  50      var $_activeparams = array(
  51          'id', 'driver', 'type', 'protocol', 'username', 'password', 'server',
  52          'rmailbox', 'lmailbox', 'onlynew', 'markseen', 'del', 'loginfetch',
  53          'acctcolor'
  54      );
  55  
  56      /**
  57       * Attempts to return a concrete IMP_Fetchmail instance based on $driver.
  58       *
  59       * @param string $driver  The type of concrete IMP_Fetchmail subclass to
  60       *                        return, based on the driver indicated. The code
  61       *                        is dynamically included.
  62       *
  63       * @param array $params   The configuration parameter array.
  64       *
  65       * @return mixed  The newly created concrete IMP_Fetchmail instance, or
  66       *                false on error.
  67       */
  68      function &factory($driver, $params = array())
  69      {
  70          $driver = basename($driver);
  71          require_once dirname(__FILE__) . '/Fetchmail/' . $driver . '.php';
  72          $class = 'IMP_Fetchmail_' . $driver;
  73          if (class_exists($class)) {
  74              $fetchmail = &new $class($params);
  75          } else {
  76              $fetchmail = false;
  77          }
  78  
  79          return $fetchmail;
  80      }
  81  
  82      /**
  83       * Returns a list of available drivers, with a description of each.
  84       * This function can be called statically:
  85       *   $list = IMP_Fetchmail::listDrivers();
  86       *
  87       * @return array  The list of available drivers, with the driver name as
  88       *                the key and the description as the value.
  89       */
  90      function listDrivers()
  91      {
  92          $drivers = array();
  93  
  94          if (($dir = opendir(dirname(__FILE__) . '/Fetchmail'))) {
  95              while (false !== ($file = readdir($dir))) {
  96                  if (!is_dir($file)) {
  97                      $driver = basename($file, '.php');
  98                      $class = 'IMP_Fetchmail_' . $driver;
  99                      require_once dirname(__FILE__) . '/Fetchmail/' . $file;
 100                      if (is_callable(array($class, 'description')) &&
 101                          ($descrip = call_user_func(array($class, 'description')))) {
 102                          $drivers[$driver] = $descrip;
 103                      }
 104                  }
 105              }
 106              closedir($dir);
 107          }
 108  
 109          return $drivers;
 110      }
 111  
 112      /**
 113       * List the colors available for coloring fetched messages.
 114       * This function can be called statically:
 115       *   $list = IMP_Fetchmail::listColors();
 116       *
 117       * @return array  The list of available colors;
 118       */
 119      function listColors()
 120      {
 121          return array(
 122              'purple', 'lime', 'teal', 'blue', 'olive', 'fuchsia', 'navy',
 123              'aqua'
 124          );
 125      }
 126  
 127      /**
 128       * Returns a description of the driver.
 129       * This function can be called statically:
 130       *   $description = IMP_Fetchmail::description();
 131       *
 132       * @abstract
 133       *
 134       * @return string  The description of the driver.
 135       */
 136      function description()
 137      {
 138          return '';
 139      }
 140  
 141      /**
 142       * Constructor.
 143       *
 144       * @param array $params  The configuration parameter array.
 145       */
 146      function IMP_Fetchmail($params)
 147      {
 148          /* Check for missing params. */
 149          $paramlist = $this->getParameterList();
 150          if (array_diff($paramlist, array_keys($params))) {
 151              // TODO: Error message here
 152          }
 153  
 154          $this->_params = $params;
 155      }
 156  
 157      /**
 158       * Return the list of parameters valid for this driver.
 159       *
 160       * @return array  The list of active parameters.
 161       */
 162      function getParameterList()
 163      {
 164          return $this->_activeparams;
 165      }
 166  
 167      /**
 168       * Return a list of protocols supported by this driver.
 169       *
 170       * @abstract
 171       *
 172       * @return array  The list of protocols.
 173       *                KEY: protocol ID
 174       *                VAL: protocol description
 175       */
 176      function getProtocolList()
 177      {
 178          return array();
 179      }
 180  
 181      /**
 182       * Gets the mail using the data in this object.
 183       *
 184       * @abstract
 185       *
 186       * @return mixed  Returns the number of messages retrieved on success.
 187       *                Returns PEAR_Error on error.
 188       */
 189      function getMail()
 190      {
 191          return PEAR::raiseError('not implemented');
 192      }
 193  
 194      /**
 195       * Processes a single mail message by calling any user defined functions,
 196       * stripping bare newlines, and adding color information to the headers.
 197       *
 198       * @access private
 199       *
 200       * @param string $header  The message header text.
 201       * @param string $body    The message body text.
 202       *
 203       * @return string  The complete message.
 204       */
 205      function _processMailMessage($header, $body)
 206      {
 207          $msg = rtrim($header);
 208  
 209          if (empty($this->_params['acctcolor'])) {
 210              $msg .= "\nX-color: " . $this->_params['acctcolor'];
 211          }
 212          $msg .= "\n\n" . $body;
 213  
 214          /* If there is a user defined function, call it with the current
 215           * message as an argument. */
 216          if ($GLOBALS['conf']['hooks']['fetchmail_filter']) {
 217              include_once HORDE_BASE . '/config/hooks.php';
 218              if (function_exists('_imp_hook_fetchmail_filter')) {
 219                  $msg = call_user_func('_imp_hook_fetchmail_filter', $msg);
 220              }
 221          }
 222  
 223          /* Make absolutely sure there are no bare newlines. */
 224          $msg = preg_replace("|([^\r])\n|", "\\1\r\n", $msg);
 225          $msg = str_replace("\n\n", "\n\r\n", $msg);
 226  
 227          return $msg;
 228      }
 229  
 230      /**
 231       * Checks the message size to see if it exceeds the maximum value
 232       * allowable in the configuration file.
 233       *
 234       * @access private
 235       *
 236       * @param integer $size    The size of the message.
 237       * @param string $subject  The subject of the message.
 238       * @param string $from     The message sender.
 239       *
 240       * @return boolean  False if message is too large, true if OK.
 241       */
 242      function _checkMessageSize($size, $subject, $from)
 243      {
 244          if (!empty($GLOBALS['conf']['fetchmail']['size_limit']) &&
 245              ($size > $GLOBALS['conf']['fetchmail']['size_limit'])) {
 246              require_once 'Horde/MIME.php';
 247              $GLOBALS['notification']->push(sprintf(_("The message \"%s\" from \"%s\" (%d bytes) exceeds fetch size limit."), MIME::Decode($subject), MIME::Decode($from), $size), 'horde.warning');
 248              return false;
 249          } else {
 250              return true;
 251          }
 252      }
 253  
 254      /**
 255       * Add the message to the requested local mailbox.
 256       *
 257       * @access private
 258       *
 259       * @param string $msg  The message text.
 260       *
 261       * @return boolean  True on success, false on failure.
 262       */
 263      function _addMessage($msg)
 264      {
 265          return @imap_append($GLOBALS['imp']['stream'], IMP::serverString($this->_params['lmailbox']), $msg);
 266      }
 267  
 268      /**
 269       * Perform fetchmail on the list of accounts given. Outputs informaton
 270       * to the global notification driver.
 271       * This function can be called statically.
 272       *
 273       * @param array $accounts  The list of account identifiers to fetch mail
 274       *                         for.
 275       */
 276      function fetchMail($accounts)
 277      {
 278          $fm_account = &new IMP_Fetchmail_Account();
 279  
 280          foreach ($accounts as $val) {
 281              $params = $fm_account->getAllValues($val);
 282              $driver = &IMP_Fetchmail::factory($params['driver'], $params);
 283              $res = $driver->getMail();
 284  
 285              if (is_a($res, 'PEAR_Error')) {
 286                  $GLOBALS['notification']->push(_("Fetchmail: ") . $res->getMessage(), 'horde.warning');
 287              } elseif ($res == 1) {
 288                  $GLOBALS['notification']->push(_("Fetchmail: ") . sprintf(_("Fetched 1 message from %s"), $fm_account->getValue('id', $val)), 'horde.success');
 289              } elseif ($res >= 0) {
 290                  $GLOBALS['notification']->push(_("Fetchmail: ") . sprintf(_("Fetched %d messages from %s"), $res, $fm_account->getValue('id', $val)), 'horde.success');
 291              } else {
 292                  $GLOBALS['notification']->push(_("Fetchmail: no new messages."), 'horde.success');
 293              }
 294          }
 295      }
 296  
 297  }
 298  
 299  /**
 300   * The IMP_Fetchmail_Account:: class provides an interface to accessing
 301   * fetchmail preferences for all mail accounts a user might have.
 302   *
 303   * @author  Nuno Loureiro <nuno@co.sapo.pt>
 304   * @since   IMP 4.0
 305   * @package IMP
 306   */
 307  class IMP_Fetchmail_Account {
 308  
 309      /**
 310       * Array containing all the user's accounts.
 311       *
 312       * @var array
 313       */
 314      var $_accounts = array();
 315  
 316      /**
 317       * Constructor.
 318       */
 319      function IMP_Fetchmail_Account()
 320      {
 321          /* Read all the user's accounts from the prefs object or build
 322           * a new account from the standard values given in prefs.php. */
 323          $accounts = @unserialize($GLOBALS['prefs']->getValue('fm_accounts'));
 324          if (is_array($accounts)) {
 325              $this->_accounts = $accounts;
 326          }
 327      }
 328  
 329      /**
 330       * Saves all accounts in the prefs backend.
 331       *
 332       * @access private
 333       */
 334      function _save()
 335      {
 336          $GLOBALS['prefs']->setValue('fm_accounts', serialize($this->_accounts));
 337      }
 338  
 339      /**
 340       * Adds a new empty account to the array of accounts.
 341       *
 342       * @return integer  The pointer to the created account.
 343       */
 344      function add()
 345      {
 346          $this->_accounts[] = array();
 347          $this->_save();
 348          return count($this->_accounts) - 1;
 349      }
 350  
 351      /**
 352       * Remove an account from the array of accounts
 353       *
 354       * @param integer $account  The pointer to the account to be removed.
 355       *
 356       * @return array  The removed account.
 357       */
 358      function delete($account)
 359      {
 360          $deleted = $this->_accounts[$account];
 361          unset($this->_accounts[$account]);
 362          $this->_accounts = array_values($this->_accounts);
 363          $this->_save();
 364          return $deleted;
 365      }
 366  
 367      /**
 368       * Returns a property from one of the accounts.
 369       *
 370       * @param string $key       The property to retrieve.
 371       * @param integer $account  The account to retrieve the property from.
 372       *
 373       * @return mixed  The value of the property or false if the property
 374       *                doesn't exist.
 375       */
 376      function getValue($key, $account)
 377      {
 378          return (isset($this->_accounts[$account][$key])) ? $this->_accounts[$account][$key] : false;
 379      }
 380  
 381      /**
 382       * Returns all properties from the requested accounts.
 383       *
 384       * @param integer $account  The account to retrieve the properties from.
 385       *
 386       * @return array  The entire properties array, or false on error.
 387       */
 388      function getAllValues($account)
 389      {
 390          return (isset($this->_accounts[$account])) ? $this->_accounts[$account] : false;
 391      }
 392  
 393      /**
 394       * Returns an array with the specified property from all existing accounts.
 395       *
 396       * @param string $key  The property to retrieve.
 397       *
 398       * @return array  The array with the values from all accounts.
 399       */
 400      function getAll($key)
 401      {
 402          $list = array();
 403          foreach (array_keys($this->_accounts) as $account) {
 404              $list[$account] = $this->getValue($key, $account);
 405          }
 406  
 407          return $list;
 408      }
 409  
 410      /**
 411       * Sets a property with a specified value.
 412       *
 413       * @param string $key       The property to set.
 414       * @param mixed $val        The value the property should be set to.
 415       * @param integer $account  The account to set the property in.
 416       */
 417      function setValue($key, $val, $account)
 418      {
 419          /* These parameters are checkbox items - make sure they are stored
 420           * as boolean values. */
 421          $list = array('del', 'onlynew', 'markseen', 'loginfetch');
 422          if (in_array($key, $list) && !is_bool($val)) {
 423              if (($val == 'yes') || (intval($val) != 0)) {
 424                  $val = true;
 425              } else {
 426                  $val = false;
 427              }
 428          }
 429  
 430          $this->_accounts[$account][$key] = $val;
 431          $this->_save();
 432      }
 433  
 434      /**
 435       * Returns true if the pair key/value is already in the accounts array.
 436       *
 437       * @param string $key  The account key to search.
 438       * @param string $val  The value to search for in $key.
 439       *
 440       * @return boolean  True if the value was found in $key.
 441       */
 442      function hasValue($key, $val)
 443      {
 444          $list = $this->getAll($key);
 445          foreach ($list as $val2) {
 446              if (strpos(String::lower($val), String::lower($val2)) !== false) {
 447                  return true;
 448              }
 449          }
 450          return false;
 451      }
 452  
 453  }


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