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

   1  <?php
   2  
   3  require_once 'Horde/Identity.php';
   4  
   5  /**
   6   * This class provides an IMP-specific interface to all identities a
   7   * user might have. Its methods take care of any site-specific
   8   * restrictions configured in prefs.php and conf.php.
   9   *
  10   * $Horde: imp/lib/Identity/imp.php,v 1.44.2.10 2007/01/02 13:54:58 jan Exp $
  11   *
  12   * Copyright 2001-2007 Jan Schneider <jan@horde.org>
  13   * Copyright 2001-2007 Chuck Hagenbuch <chuck@horde.org>
  14   *
  15   * See the enclosed file COPYING for license information (GPL). If you
  16   * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  17   *
  18   * @author  Jan Schneider <jan@horde.org>
  19   * @author  Chuck Hagenbuch <chuck@horde.org>
  20   * @since   IMP 2.3.7
  21   * @package Horde_Identity
  22   */
  23  class Identity_imp extends Identity {
  24  
  25      /**
  26       * Cached alias list.
  27       *
  28       * @var array
  29       */
  30      var $_aliases = array();
  31  
  32      /**
  33       * Cached from address list.
  34       *
  35       * @var array
  36       */
  37      var $_fromList = array();
  38  
  39      /**
  40       * Cached names list.
  41       *
  42       * @var array
  43       */
  44      var $_names = array();
  45  
  46      /**
  47       * Cached signature list.
  48       *
  49       * @var array
  50       */
  51      var $_signatures = array();
  52  
  53      /**
  54       * Reads all the user's identities from the prefs object or builds
  55       * a new identity from the standard values given in prefs.php.
  56       */
  57      function Identity_imp()
  58      {
  59          parent::Identity();
  60          $this->_properties = array_merge(
  61              $this->_properties,
  62              array('replyto_addr', 'alias_addr', 'tieto_addr', 'bcc_addr',
  63                    'mail_hdr', 'signature', 'sig_first', 'sig_dashes',
  64                    'save_sent_mail', 'sent_mail_folder'));
  65      }
  66  
  67      /**
  68       * Verifies and sanitizes all identity properties.
  69       *
  70       * @param integer $identity  The identity to verify.
  71       *
  72       * @return boolean|object  True if the properties are valid or a PEAR_Error
  73       *                         with an error description otherwise.
  74       */
  75      function verify($identity = null)
  76      {
  77          if (is_a($result = parent::verify($identity), 'PEAR_Error')) {
  78              return $result;
  79          }
  80  
  81          if (!isset($identity)) {
  82              $identity = $this->_default;
  83          }
  84  
  85          /* Clean up Alias, Tie-to, and BCC addresses. */
  86          require_once 'Horde/Array.php';
  87          foreach (array('alias_addr', 'tieto_addr', 'bcc_addr') as $val) {
  88              $data = $this->getValue($val, $identity);
  89              if (is_array($data)) {
  90                  $data = implode("\n", $data);
  91              }
  92              $data = trim($data);
  93              $data = (empty($data)) ? array() : Horde_Array::prepareAddressList(preg_split("/[\n\r]+/", $data));
  94              $this->setValue($val, $data, $identity);
  95          }
  96  
  97          /* Split the list of headers by new lines and sort the list of headers
  98           * to make sure there are no duplicates. */
  99          $mail_hdr = $this->getValue('mail_hdr', $identity);
 100          if (is_array($mail_hdr)) {
 101              $mail_hdr = implode("\n", $mail_hdr);
 102          }
 103          $mail_hdr = trim($mail_hdr);
 104          if (!empty($mail_hdr)) {
 105              $mail_hdr = str_replace(':', '', $mail_hdr);
 106              $mail_hdr = preg_split("/[\n\r]+/", $mail_hdr);
 107              $mail_hdr = array_map('trim', $mail_hdr);
 108              $mail_hdr = array_unique($mail_hdr);
 109              natcasesort($mail_hdr);
 110          }
 111          $this->setValue('mail_hdr', $mail_hdr, $identity);
 112      }
 113  
 114      /**
 115       * Returns a complete From: header based on all relevent factors (fullname,
 116       * from address, input fields, locks etc.)
 117       *
 118       * @param integer $ident        The identity to retrieve the values from.
 119       * @param string $from_address  A default from address to use if no
 120       *                              identity is selected and the from_addr
 121       *                              preference is locked.
 122       *
 123       * @return string  A full From: header in the format
 124       *                 'Fullname <user@example.com>'.
 125       */
 126      function getFromLine($ident = null, $from_address = '')
 127      {
 128          global $imp;
 129          static $froms = array();
 130  
 131          if (isset($froms[$ident])) {
 132              return $froms[$ident];
 133          }
 134  
 135          if (!isset($ident)) {
 136              $address = $from_address;
 137          }
 138  
 139          if (empty($address) || $this->_prefs->isLocked('from_addr')) {
 140              $address = $this->getFromAddress($ident);
 141              $name = $this->getFullname($ident);
 142          }
 143  
 144          if (!empty($address)) {
 145              $ob = imap_rfc822_parse_adrlist($address, $imp['maildomain']);
 146          }
 147  
 148          if (empty($name)) {
 149              if (!empty($ob[0]->personal)) {
 150                  $name = $ob[0]->personal;
 151              } else {
 152                  $name = $this->getFullname($ident);
 153              }
 154          }
 155  
 156          $from = MIME::trimEmailAddress(MIME::rfc822WriteAddress($ob[0]->mailbox, $ob[0]->host, $name));
 157  
 158          $froms[$ident] = $from;
 159          return $from;
 160      }
 161  
 162      /**
 163       * Returns an array with From: headers from all identities
 164       *
 165       * @return array  The From: headers from all identities
 166       */
 167      function getAllFromLines()
 168      {
 169          foreach ($this->_identities as $ident => $dummy) {
 170              $list[$ident] = $this->getFromAddress($ident);
 171          }
 172          return $list;
 173      }
 174  
 175      /**
 176       * Returns an array with the necessary values for the identity select
 177       * box in the IMP compose window.
 178       *
 179       * @return array  The array with the necessary strings
 180       */
 181      function getSelectList()
 182      {
 183          $ids = $this->getAll('id');
 184          foreach ($ids as $key => $id) {
 185              $list[$key] = $this->getFromAddress($key) . ' (' . $id . ')';
 186          }
 187          return $list;
 188      }
 189  
 190      /**
 191       * Returns true if the given address belongs to one of the identities.
 192       * This function will search aliases for an identity automatically.
 193       *
 194       * @param string $address  The address to search for in the identities.
 195       *
 196       * @return boolean  True if the address was found.
 197       */
 198      function hasAddress($address)
 199      {
 200          static $list;
 201  
 202          $address = String::lower($address);
 203          if (!isset($list)) {
 204              $list = $this->getAllFromAddresses(true);
 205          }
 206  
 207          return isset($list[$address]);
 208      }
 209  
 210      /**
 211       * Returns the from address based on the chosen identity. If no
 212       * address can be found it is built from the current user name and
 213       * the specified maildomain.
 214       *
 215       * @param integer $ident  The identity to retrieve the address from.
 216       *
 217       * @return string  A valid from address.
 218       */
 219      function getFromAddress($ident = null)
 220      {
 221          global $imp;
 222  
 223          if (!empty($this->_fromList[$ident])) {
 224              return $this->_fromList[$ident];
 225          }
 226  
 227          $val = $this->getValue('from_addr', $ident);
 228          if (empty($val)) {
 229              $val = $imp['user'];
 230          }
 231  
 232          if (!strstr($val, '@')) {
 233              $val .= '@' . $imp['maildomain'];
 234          }
 235  
 236          $this->_fromList[$ident] = $val;
 237  
 238          return $val;
 239      }
 240  
 241      /**
 242       * Returns all aliases based on the chosen identity.
 243       *
 244       * @param integer $ident  The identity to retrieve the aliases from.
 245       *
 246       * @return array  Aliases for the identity.
 247       */
 248      function getAliasAddress($ident)
 249      {
 250          if (empty($this->_aliases[$ident])) {
 251              $this->_aliases[$ident] = @array_merge($this->getValue('alias_addr', $ident),
 252                                                     array($this->getValue('replyto_addr', $ident)));
 253          }
 254  
 255          return $this->_aliases[$ident];
 256      }
 257  
 258      /**
 259       * Returns an array with all identities' from addresses.
 260       *
 261       * @param boolean $alias  Include aliases?
 262       *
 263       * @return array  The array with
 264       *                KEY - address
 265       *                VAL - identity number
 266       */
 267      function getAllFromAddresses($alias = false)
 268      {
 269          $list = array();
 270  
 271          foreach ($this->_identities as $key => $identity) {
 272              /* Get From Addresses. */
 273              $addr = String::lower($this->getFromAddress($key));
 274              if (!isset($list[$addr])) {
 275                  $list[$addr] = $key;
 276              }
 277  
 278              /* Get Aliases. */
 279              if ($alias) {
 280                  $addrs = $this->getAliasAddress($key);
 281                  if (!empty($addrs)) {
 282                      foreach ($addrs as $val) {
 283                          $val = String::lower($val);
 284                          if (!isset($list[$val])) {
 285                              $list[$val] = $key;
 286                          }
 287                      }
 288                  }
 289              }
 290          }
 291  
 292          return $list;
 293      }
 294  
 295      /**
 296       * Get all 'tie to' address/identity pairs.
 297       *
 298       * @return array  The array with
 299       *                KEY - address
 300       *                VAL - identity number
 301       */
 302      function getAllTieAddresses()
 303      {
 304          $list = array();
 305  
 306          foreach ($this->_identities as $key => $identity) {
 307              $tieaddr = $this->getValue('tieto_addr', $key);
 308              if (!empty($tieaddr)) {
 309                  foreach ($tieaddr as $val) {
 310                      if (!isset($list[$val])) {
 311                          $list[$val] = $key;
 312                      }
 313                  }
 314              }
 315          }
 316  
 317          return $list;
 318      }
 319  
 320      /**
 321       * Returns the BCC addresses for a given identity.
 322       *
 323       * @param integer $ident  The identity to retrieve the Bcc addresses from.
 324       *
 325       * @return array  The array of objects (IMAP addresses).
 326       */
 327      function getBccAddresses($ident = null)
 328      {
 329          $bcc = $this->getValue('bcc_addr', $ident);
 330          if (empty($bcc)) {
 331              return array();
 332          } else {
 333              if (!is_array($bcc)) {
 334                  $bcc = array($bcc);
 335              }
 336              $addresses = implode(', ', $bcc);
 337              return imap_rfc822_parse_adrlist($addresses, '');
 338          }
 339      }
 340  
 341      /**
 342       * Returns the identity's id that matches the passed addresses.
 343       *
 344       * @param mixed $addresses      Either an array or a single string or a
 345       *                              comma-separated list of email addresses.
 346       * @param boolean $search_ties  Search for a matching identity in tied
 347       *                              addresses too?
 348       *
 349       * @return integer  The id of the first identity that from or alias
 350       *                  addresses match (one of) the passed addresses or
 351       *                  null if none matches.
 352       */
 353      function getMatchingIdentity($addresses, $search_ties = true)
 354      {
 355          static $tie_addresses, $own_addresses;
 356  
 357          if (!isset($tie_addresses)) {
 358              $tie_addresses = $this->getAllTieAddresses();
 359              $own_addresses = $this->getAllFromAddresses(true);
 360          }
 361  
 362          /* Normalize address list. */
 363          if (!is_array($addresses)) {
 364              $addresses = array($addresses);
 365          }
 366          $addresses = implode(', ', $addresses);
 367          $addresses = imap_rfc822_parse_adrlist($addresses, '');
 368  
 369          foreach ($addresses as $address) {
 370              if (empty($address->mailbox)) {
 371                  continue;
 372              }
 373              $find_address = $address->mailbox;
 374              if (!empty($address->host)) {
 375                  $find_address .= '@' . $address->host;
 376              }
 377              $find_address = String::lower($find_address);
 378  
 379              /* Search 'tieto' addresses first. */
 380              /* Check for this address explicitly. */
 381              if ($search_ties && isset($tie_addresses[$find_address])) {
 382                  return $tie_addresses[$find_address];
 383              }
 384  
 385              /* If we didn't find the address, check for the domain. */
 386              if (!empty($address->host)) {
 387                  $host = '@' . $address->host;
 388                  if ($search_ties && $host != '@' && isset($tie_addresses[$host])) {
 389                      return $tie_addresses[$host];
 390                  }
 391              }
 392  
 393              /* Next, search all from addresses. */
 394              if (isset($own_addresses[$find_address])) {
 395                  return $own_addresses[$find_address];
 396              }
 397          }
 398  
 399          return null;
 400      }
 401  
 402      /**
 403       * Returns the user's full name.
 404       *
 405       * @param integer $ident  The identity to retrieve the name from.
 406       *
 407       * @return string  The user's full name.
 408       */
 409      function getFullname($ident = null)
 410      {
 411          if (isset($this->_names[$ident])) {
 412              return $this->_names[$ident];
 413          }
 414  
 415          $this->_names[$ident] = $this->getValue('fullname', $ident);
 416  
 417          return $this->_names[$ident];
 418      }
 419  
 420      /**
 421       * Returns the full signature based on the current settings for the
 422       * signature itself, the dashes and the position.
 423       *
 424       * @param integer $ident  The identity to retrieve the signature from.
 425       *
 426       * @return string  The full signature.
 427       */
 428      function getSignature($ident = null)
 429      {
 430          if (isset($this->_signatures[$ident])) {
 431              return $this->_signatures[$ident];
 432          }
 433  
 434          $val = $this->getValue('signature', $ident);
 435          if (!empty($val)) {
 436              $sig_first = $this->getValue('sig_first', $ident);
 437              $sig_dashes = $this->getValue('sig_dashes', $ident);
 438              $val = str_replace("\r\n", "\n", $val);
 439              if ($sig_dashes) {
 440                  $val = "-- \n$val";
 441              }
 442              if (isset($sig_first) && $sig_first) {
 443                  $val = "\n" . $val . "\n\n\n";
 444              } else {
 445                  $val = "\n" . $val;
 446              }
 447          }
 448  
 449          if (!empty($GLOBALS['conf']['hooks']['signature'])) {
 450              include_once HORDE_BASE . '/config/hooks.php';
 451              if (function_exists('_imp_hook_signature')) {
 452                  $val = call_user_func('_imp_hook_signature', $val);
 453              }
 454          }
 455  
 456          $this->_signatures[$ident] = $val;
 457  
 458          return $val;
 459      }
 460  
 461      /**
 462       * Returns an array with the signatures from all identities
 463       *
 464       * @return array  The array with all the signatures.
 465       */
 466      function getAllSignatures()
 467      {
 468          static $list;
 469  
 470          if (isset($list)) {
 471              return $list;
 472          }
 473  
 474          foreach ($this->_identities as $key => $identity) {
 475              $list[$key] = $this->getSignature($key);
 476          }
 477  
 478          return $list;
 479      }
 480  
 481      /**
 482       * @see Identity::getValue()
 483       */
 484      function getValue($key, $identity = null)
 485      {
 486          if ($key == 'sent_mail_folder') {
 487              return IMP::folderPref(parent::getValue('sent_mail_folder', $identity), true);
 488          }
 489          return parent::getValue($key, $identity);
 490      }
 491  
 492      /**
 493       * Returns an array with the sent-mail folder names from all the
 494       * identities.
 495       *
 496       * @return array  The array with the folder names.
 497       */
 498      function getAllSentmailFolders()
 499      {
 500          $list = array();
 501          foreach ($this->_identities as $key => $identity) {
 502              if ($folder = $this->getValue('sent_mail_folder', $key)) {
 503                  $list[$folder] = true;
 504              }
 505          }
 506          return array_keys($list);
 507      }
 508  
 509      /**
 510       * Returns true if the mail should be saved and the user is allowed to.
 511       *
 512       * @param integer $ident  The identity to retrieve the setting from.
 513       *
 514       * @return boolean  True if the sent mail should be saved.
 515       */
 516      function saveSentmail($ident = null)
 517      {
 518          if (!$GLOBALS['conf']['user']['allow_folders']) {
 519              return false;
 520          }
 521  
 522          return $this->getValue('save_sent_mail', $ident);
 523      }
 524  
 525  }


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