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

   1  <?php
   2  /**
   3   * Functions required to start an IMP session.
   4   *
   5   * $Horde: imp/lib/Session.php,v 1.74.2.33 2007/01/02 13:54:56 jan Exp $
   6   *
   7   * Copyright 1999-2007 Chuck Hagenbuch <chuck@horde.org>
   8   * Copyright 1999-2007 Jon Parise <jon@horde.org>
   9   *
  10   * See the enclosed file COPYING for license information (GPL). If you
  11   * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  12   *
  13   * @author  Chuck Hagenbuch <chuck@horde.org>
  14   * @author  Jon Parise <jon@horde.org>
  15   * @since   IMP 4.0
  16   * @package IMP
  17   */
  18  class IMP_Session {
  19  
  20      /**
  21       * Take information posted from a login attempt and try setting up
  22       * an initial IMP session. Handle Horde authentication, if
  23       * required, and only do enough work to see if the user can log
  24       * in. This function should only be called once, when the user
  25       * first logs in.
  26       *
  27       * Creates the $imp session variable with the following entries:
  28       * '_logintasks'   -- Have the login tasks been completed?
  29       * 'acl'           -- See config/servers.php.
  30       * 'admin'         -- See config/servers.php.
  31       * 'base_protocol' -- Either 'imap' or 'pop3'.
  32       * 'cache'         -- Various IMP libraries can use this variable to cache
  33       *                    data.
  34       * 'file_upload'   -- If file uploads are allowed, the max size.
  35       * 'filteravail'   -- Can we apply filters manually?
  36       * 'imap_server'   -- IMAP server capabilities.
  37       * 'mailbox'       -- The current viewable mailbox.
  38       * 'maildomain'    -- See config/servers.php.
  39       * 'namespace'     -- See config/servers.php.
  40       * 'notepadavail'  -- Is listing of notepads available?
  41       * 'pass'          -- The encrypted password.
  42       * 'port'          -- See config/servers.php.
  43       * 'protocol'      -- See config/servers.php.
  44       * 'quota'         -- See config/servers.php.
  45       * 'search'        -- Settings used by the IMP_Search library.
  46       * 'server'        -- The name of the server entry in config/servers.php.
  47       * 'smime'         -- Settings related to the S/MIME viewer.
  48       * 'smtphost'      -- The SMTP host to use instead of the Horde default.
  49       * 'smtpport'      -- The SMTP port to use instead of the Horde default.
  50       * 'showunsub'     -- Show unsusubscribed mailboxes on the folders screen.
  51       * 'tasklistavail' -- Is listing of tasklists available?
  52       * 'thismailbox'   -- The mailbox containing the current viewable message.
  53       * 'uniquser'      -- The unique user name.
  54       * 'user'          -- The IMAP username.
  55       *
  56       * @param string $imapuser  The username of the user.
  57       * @param string $password  The password of the user.
  58       * @param string $server    The server to use (see config/servers.php).
  59       * @param array $args       The necessary server information.
  60       *
  61       * @return boolean  True on success, false on failure.
  62       */
  63      function createSession($imapuser, $password, $server, $args = array())
  64      {
  65          global $conf, $registry;
  66  
  67          /* We need both a username and password. */
  68          if (empty($imapuser) || empty($password)) {
  69              return false;
  70          }
  71  
  72          /* Make sure all necessary parameters are set. */
  73          $default_args = array(
  74              'realm'        => '',
  75              'port'         => '',
  76              'protocol'     => '',
  77              'maildomain'   => '',
  78              'imap_server'  => array('children' => false),
  79          );
  80  
  81          /* Merge with the passed-in parameters. */
  82          $args = array_merge($default_args, $args);
  83  
  84          /* Create the $imp session variable. */
  85          $_SESSION['imp'] = array();
  86          $_SESSION['imp']['cache'] = array();
  87          $_SESSION['imp']['pass'] = Secret::write(Secret::getKey('imp'), $password);
  88  
  89          /* Set the logintasks flag. */
  90          IMP::loginTasksFlag(1);
  91  
  92          /* Run the username through virtualhost expansion functions if
  93           * necessary. */
  94          $_SESSION['imp']['user'] = $imapuser;
  95          if (!empty($conf['hooks']['vinfo'])) {
  96              require_once HORDE_BASE . '/config/hooks.php';
  97              if (function_exists('_imp_hook_vinfo')) {
  98                  $_SESSION['imp']['user'] = call_user_func('_imp_hook_vinfo');
  99              }
 100          }
 101  
 102          /* We might need to override some of the defaults with
 103           * environment-wide settings. Do NOT use the global $servers
 104           * variable as it may not exist. */
 105          require IMP_BASE . '/config/servers.php';
 106  
 107          /* Determine the unique user name. */
 108          if (Auth::isAuthenticated()) {
 109              $_SESSION['imp']['uniquser'] = Auth::removeHook(Auth::getAuth());
 110          } else {
 111              $_SESSION['imp']['uniquser'] = $_SESSION['imp']['user'];
 112  
 113              if (($conf['server']['server_list'] != 'none') &&
 114                  !empty($servers[$server]['realm'])) {
 115                  $_SESSION['imp']['uniquser'] .= '@' . $servers[$server]['realm'];
 116              } elseif (!empty($args['realm'])) {
 117                  $_SESSION['imp']['uniquser'] .= '@' . $args['realm'];
 118              }
 119          }
 120  
 121          if (($conf['server']['server_list'] != 'none') &&
 122              !empty($servers[$server]) &&
 123              is_array($servers[$server])) {
 124              $fields = array('server', 'protocol', 'port', 'maildomain',
 125                              'quota', 'acl');
 126              $fields_array = array('admin');
 127              $ptr = &$servers[$server];
 128  
 129              foreach ($fields as $val) {
 130                  $_SESSION['imp'][$val] = isset($ptr[$val]) ? $ptr[$val] : null;
 131                  /* 'admin' and 'quota' have password entries - encrypt these
 132                   * entries in the session if they exist. */
 133                  if (isset($ptr[$val]['params']['password'])) {
 134                      $_SESSION['imp'][$val]['params']['password'] = Secret::write(Secret::getKey('imp'), $ptr[$val]['params']['password']);
 135                  }
 136              }
 137              foreach ($fields_array as $val) {
 138                  $_SESSION['imp'][$val] = isset($ptr[$val]) ? $ptr[$val] : array();
 139                  /* 'admin' and 'quota' have password entries - encrypt these
 140                   * entries in the session if they exist. */
 141                  if (isset($ptr[$val]['params']['password'])) {
 142                      $_SESSION['imp'][$val]['params']['password'] = Secret::write(Secret::getKey('imp'), $ptr[$val]['params']['password']);
 143                  }
 144              }
 145  
 146              if ($conf['mailer']['type'] == 'smtp') {
 147                  if (!empty($ptr['smtphost'])) {
 148                      $_SESSION['imp']['smtphost'] = $ptr['smtphost'];
 149                  }
 150                  if (!empty($ptr['smtpport'])) {
 151                      $_SESSION['imp']['smtpport'] = $ptr['smtpport'];
 152                  }
 153              }
 154          } else {
 155              $server_key = null;
 156              foreach ($servers as $key => $val) {
 157                  if (is_null($server_key) && (substr($key, 0, 1) != '_')) {
 158                      $server_key = $key;
 159                  }
 160                  if (IMP::isPreferredServer($val, $key)) {
 161                      $server_key = $key;
 162                      break;
 163                  }
 164              }
 165              $ptr = &$servers[$server_key];
 166  
 167              if (!empty($conf['server']['change_server'])) {
 168                  $_SESSION['imp']['server'] = $server;
 169              } else {
 170                  $_SESSION['imp']['server'] = $ptr['server'];
 171  
 172                  foreach (array('acl', 'admin', 'quota') as $val) {
 173                      if (isset($ptr[$val])) {
 174                          $_SESSION['imp'][$val] = $ptr[$val];
 175                          /* 'admin' and 'quota' have password entries - encrypt
 176                           * these entries in the session if they exist. */
 177                          if (isset($ptr[$val]['params']['password'])) {
 178                              $_SESSION['imp'][$val]['params']['password'] = Secret::write(Secret::getKey('imp'), $ptr[$val]['params']['password']);
 179                          }
 180                      } else {
 181                          $_SESSION['imp'][$val] = false;
 182                      }
 183                  }
 184              }
 185  
 186              foreach (array('port', 'protocol', 'smtphost', 'smtpport') as $param) {
 187                  if (!empty($conf['server']['change_' . $param])) {
 188                      $_SESSION['imp'][$param] = isset($args[$param]) ? $args[$param] : null;
 189                  } else {
 190                      $_SESSION['imp'][$param] = isset($ptr[$param]) ? $ptr[$param] : null;
 191                  }
 192              }
 193  
 194              $_SESSION['imp']['maildomain'] = $args['maildomain'];
 195          }
 196  
 197          /* Determine the base protocol. */
 198          if (($pos = strpos($_SESSION['imp']['protocol'], '/'))) {
 199              $_SESSION['imp']['base_protocol'] = strtolower(substr($_SESSION['imp']['protocol'], 0, $pos));
 200          } else {
 201              $_SESSION['imp']['base_protocol'] = strtolower($_SESSION['imp']['protocol']);
 202          }
 203  
 204          /* Set the initial mailbox to 'INBOX'. */
 205          $_SESSION['imp']['mailbox'] = $_SESSION['imp']['thismailbox'] = 'INBOX';
 206  
 207          /* Determine max login attempts. */
 208          if (empty($ptr['login_tries']) || ($ptr['login_tries'] < 1)) {
 209              $_SESSION['imp']['login_tries'] = 3;
 210          } else {
 211              $_SESSION['imp']['login_tries'] = $ptr['login_tries'];
 212          }
 213  
 214          /* Try to authenticate with the given information. */
 215          $auth_imp = &Auth::singleton(array('imp', 'imp'));
 216          $auth_imp->authenticateOptions(array('flags' => OP_HALFOPEN));
 217          if ($auth_imp->authenticate(null, null, true) !== true) {
 218              unset($_SESSION['imp']);
 219              return false;
 220          }
 221  
 222          /* Does the server allow file uploads? If yes, store the
 223           * value, in bytes, of the maximum file size. */
 224          $_SESSION['imp']['file_upload'] = Browser::allowFileUploads();
 225  
 226          /* Is the 'mail/canApplyFilters' API call available? */
 227          if ($registry->hasMethod('mail/canApplyFilters')) {
 228              $_SESSION['imp']['filteravail'] = $registry->call('mail/canApplyFilters');
 229          } else {
 230              $_SESSION['imp']['filteravail'] = false;
 231          }
 232  
 233          /* Is the 'tasks/listTasklists' call available? */
 234          $_SESSION['imp']['tasklistavail'] = ($conf['tasklist']['use_tasklist'] && $registry->hasMethod('tasks/listTasklists'));
 235  
 236          /* Is the 'notes/listNotepads' call available? */
 237          $_SESSION['imp']['notepadavail'] = ($conf['notepad']['use_notepad'] && $registry->hasMethod('notes/listNotepads'));
 238  
 239          /* IMAP specific variables. */
 240          if ($_SESSION['imp']['base_protocol'] != 'pop3') {
 241              /* Check for timeouts. */
 242              if (!empty($ptr['timeout'])) {
 243                  $_SESSION['imp']['imap_server']['timeout'] = $ptr['timeout'];
 244              }
 245  
 246              /* Initialize the 'showunsub' value. */
 247              $_SESSION['imp']['showunsub'] = false;
 248  
 249              /* Check for manual configuration. */
 250              if (!empty($ptr['imap_config'])) {
 251                  $_SESSION['imp']['imap_server']['children'] = $ptr['imap_config']['children'];
 252                  $_SESSION['imp']['namespace'] = $ptr['imap_config']['namespace'];
 253                  $_SESSION['imp']['imap_server']['search_charset'] = $ptr['imap_config']['search_charset'];
 254              } else {
 255                  require_once  IMP_BASE . '/lib/IMAP/Client.php';
 256                  $imapclient = &new IMP_IMAPClient($_SESSION['imp']['server'], $_SESSION['imp']['port'], $_SESSION['imp']['protocol']);
 257  
 258                  $_SESSION['imp']['namespace'] = array();
 259  
 260                  $use_tls = $imapclient->useTLS();
 261                  $user_namespace = (isset($ptr['namespace']) && is_array($ptr['namespace'])) ? $ptr['namespace'] : array();
 262                  if (is_a($use_tls, 'PEAR_Error')) {
 263                      if (!empty($user_namespace)) {
 264                          foreach ($ptr as $val) {
 265                              /* This is a correct TLS configuration - we only
 266                               * need to determine the delimiters for each
 267                               * namespace.  Get the default delimiter value
 268                               * (per RFC 3501 [6.3.8]). */
 269                              $box = @imap_getmailboxes($_SESSION['imp']['stream'], IMP::serverString(), $val);
 270                              if (!empty($box[0]->delimiter)) {
 271                                  $_SESSION['imp']['namespace'][$val] = array('name' => $val, 'delimiter' => $box[0]->delimiter);
 272                              }
 273                          }
 274                      } else {
 275                          $auth_imp->IMPsetAuthErrorMsg($use_tls->getMessage());
 276                          $auth_imp->clearAuth();
 277                          unset($_SESSION['imp']);
 278                          return false;
 279                      }
 280                  } else {
 281                      /* Auto-detect namespace parameters from IMAP server. */
 282                      $res = $imapclient->login($_SESSION['imp']['user'], $password);
 283                      if (is_a($res, 'PEAR_Error')) {
 284                          $auth_imp->IMPsetAuthErrorMsg($res->getMessage());
 285                          unset($_SESSION['imp']);
 286                          return false;
 287                      }
 288                      $_SESSION['imp']['namespace'] = $imapclient->namespace($user_namespace);
 289                      if (!is_array($_SESSION['imp']['namespace'])) {
 290                          $auth_imp->IMPsetAuthErrorMsg(_("Could not retrieve namespace information from IMAP server."));
 291                          unset($_SESSION['imp']);
 292                          return false;
 293                      }
 294                      $_SESSION['imp']['imap_server']['children'] = $imapclient->queryCapability('CHILDREN');
 295  
 296                      /* Determine if the search command supports the current
 297                       * browser's charset. */
 298                      $charset = NLS::getCharset();
 299                      $_SESSION['imp']['imap_server']['search_charset'] = array($charset => $imapclient->searchCharset($charset));
 300                  }
 301  
 302                  /* Initialize the IMP_Tree object.  This needs to be done
 303                   * before initializing IMP_Search::. */
 304                  require_once  IMP_BASE . '/lib/IMAP/Tree.php';
 305                  IMP_IMAP::singleton();
 306  
 307                  /* Set up search information for the session. */
 308                  $GLOBALS['imp_search']->sessionSetup();
 309              }
 310          } else {
 311              $_SESSION['imp']['namespace'] = null;
 312          }
 313  
 314          /* Store the $imp variable globally. */
 315          $GLOBALS['imp'] = &$_SESSION['imp'];
 316  
 317          return true;
 318      }
 319  
 320      /**
 321       * Perform IMP login tasks.
 322       */
 323      function loginTasks()
 324      {
 325          if (!IMP::loginTasksFlag()) {
 326              return;
 327          }
 328  
 329          IMP::loginTasksFlag(2);
 330  
 331          if (empty($_SESSION['imp']['stream'])) {
 332              IMP::checkAuthentication(OP_HALFOPEN, true);
 333          }
 334  
 335          /* Do maintenance operations. */
 336          if ($GLOBALS['prefs']->getValue('do_maintenance')) {
 337              require_once 'Horde/Maintenance.php';
 338              $maint = &Maintenance::factory('imp', array('last_maintenance' => $GLOBALS['prefs']->getValue('last_maintenance')));
 339              if (!$maint) {
 340                  $GLOBALS['notification']->push(_("Could not execute maintenance operations."), 'horde.warning');
 341              } else {
 342                  $maint->runMaintenance();
 343              }
 344          }
 345  
 346          /* If the user wants to run filters on login, make sure they get
 347             run. */
 348          if ($GLOBALS['prefs']->getValue('filter_on_login')) {
 349              require_once  IMP_BASE . '/lib/IMAP.php';
 350              $imp_imap = &IMP_IMAP::singleton();
 351  
 352              /* Open the INBOX read-write. */
 353              $imp_imap->changeMbox('INBOX');
 354  
 355              /* Run filters. */
 356              require_once  IMP_BASE . '/lib/Filter.php';
 357              $imp_filter = &IMP_Filter::singleton();
 358              $imp_filter->filter('INBOX');
 359          }
 360  
 361          IMP::loginTasksFlag(0);
 362      }
 363  
 364      /**
 365       * Returns the initial URL.
 366       *
 367       * @param string $actionID  The action ID to perform on the initial page.
 368       * @param boolean $encode   If true the argument separator gets encoded.
 369       *
 370       * @return string  The initial URL.
 371       */
 372      function getInitialUrl($actionID = null, $encode = true)
 373      {
 374          $init_url = ($_SESSION['imp']['base_protocol'] == 'pop3') ? 'INBOX' : $GLOBALS['prefs']->getValue('initial_page');
 375  
 376          require_once  IMP_BASE . '/lib/Search.php';
 377          $imp_search = new IMP_Search();
 378  
 379          if (!$GLOBALS['prefs']->getValue('use_vinbox') &&
 380              $imp_search->isVINBOXFolder($init_url)) {
 381              $init_url = 'folders.php';
 382          } elseif (($imp_search->createSearchID($init_url) == $init_url) &&
 383                    !$imp_search->isVFolder($init_url)) {
 384              $init_url = 'INBOX';
 385              if (!$GLOBALS['prefs']->isLocked('initial_page')) {
 386                  $GLOBALS['prefs']->setValue('initial_page', $init_url);
 387              }
 388          }
 389  
 390          if ($init_url == 'folders.php') {
 391              $url = Horde::applicationUrl($init_url, !$encode);
 392              $url = Util::addParameter($url, IMP::getComposeArgs(), null, $encode);
 393          } else {
 394              $url = Horde::applicationUrl('mailbox.php', !$encode);
 395              $url = Util::addParameter($url, 'mailbox', $init_url, $encode);
 396              $url = Util::addParameter($url, IMP::getComposeArgs(), null, $encode);
 397          }
 398  
 399          if (!empty($actionID)) {
 400              $url = Util::addParameter($url, 'actionID', $actionID, $encode);
 401          }
 402  
 403          return $url;
 404      }
 405  
 406  }


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