コード例 #1
0
ファイル: SessionHandler.php プロジェクト: eigentor/tommiblog
 /**
  * {@inheritdoc}
  */
 public function write($sid, $value)
 {
     // The exception handler is not active at this point, so we need to do it
     // manually.
     try {
         $request = $this->requestStack->getCurrentRequest();
         $fields = array('uid' => $request->getSession()->get('uid', 0), 'hostname' => $request->getClientIP(), 'session' => $value, 'timestamp' => REQUEST_TIME);
         $this->connection->merge('sessions')->keys(array('sid' => Crypt::hashBase64($sid)))->fields($fields)->execute();
         return TRUE;
     } catch (\Exception $exception) {
         require_once DRUPAL_ROOT . '/core/includes/errors.inc';
         // If we are displaying errors, then do so with no possibility of a
         // further uncaught exception being thrown.
         if (error_displayable()) {
             print '<h1>Uncaught exception thrown in session handler.</h1>';
             print '<p>' . Error::renderExceptionSafe($exception) . '</p><hr />';
         }
         return FALSE;
     }
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public function write($sid, $value)
 {
     global $user;
     // The exception handler is not active at this point, so we need to do it
     // manually.
     try {
         if (!$this->sessionManager->isEnabled()) {
             // We don't have anything to do if we are not allowed to save the
             // session.
             return TRUE;
         }
         // Either ssid or sid or both will be added from $key below.
         $fields = array('uid' => $user->id(), 'hostname' => $this->requestStack->getCurrentRequest()->getClientIP(), 'session' => $value, 'timestamp' => REQUEST_TIME);
         // Use the session ID as 'sid' and an empty string as 'ssid' by default.
         // read() does not allow empty strings so that's a safe default.
         $key = array('sid' => Crypt::hashBase64($sid), 'ssid' => '');
         // On HTTPS connections, use the session ID as both 'sid' and 'ssid'.
         if ($this->requestStack->getCurrentRequest()->isSecure()) {
             $key['ssid'] = $key['sid'];
             // The "secure pages" setting allows a site to simultaneously use both
             // secure and insecure session cookies. If enabled and both cookies
             // are presented then use both keys. The session ID from the cookie is
             // hashed before being stored in the database as a security measure.
             if ($this->sessionManager->isMixedMode()) {
                 $insecure_session_name = $this->sessionManager->getInsecureName();
                 $cookies = $this->requestStack->getCurrentRequest()->cookies;
                 if ($cookies->has($insecure_session_name)) {
                     $key['sid'] = Crypt::hashBase64($cookies->get($insecure_session_name));
                 }
             }
         } elseif ($this->sessionManager->isMixedMode()) {
             unset($key['ssid']);
         }
         $this->connection->merge('sessions')->keys($key)->fields($fields)->execute();
         // Remove obsolete sessions.
         $this->cleanupObsoleteSessions();
         // Likewise, do not update access time more than once per 180 seconds.
         if ($user->isAuthenticated() && REQUEST_TIME - $user->getLastAccessedTime() > Settings::get('session_write_interval', 180)) {
             /** @var \Drupal\user\UserStorageInterface $storage */
             $storage = \Drupal::entityManager()->getStorage('user');
             $storage->updateLastAccessTimestamp($user, REQUEST_TIME);
         }
         return TRUE;
     } catch (\Exception $exception) {
         require_once DRUPAL_ROOT . '/core/includes/errors.inc';
         // If we are displaying errors, then do so with no possibility of a
         // further uncaught exception being thrown.
         if (error_displayable()) {
             print '<h1>Uncaught exception thrown in session handler.</h1>';
             print '<p>' . Error::renderExceptionSafe($exception) . '</p><hr />';
         }
         return FALSE;
     }
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function write($sid, $value)
 {
     $user = \Drupal::currentUser();
     // The exception handler is not active at this point, so we need to do it
     // manually.
     try {
         $fields = array('uid' => $user->id(), 'hostname' => $this->requestStack->getCurrentRequest()->getClientIP(), 'session' => $value, 'timestamp' => REQUEST_TIME);
         $this->connection->merge('sessions')->keys(array('sid' => Crypt::hashBase64($sid)))->fields($fields)->execute();
         // Likewise, do not update access time more than once per 180 seconds.
         if ($user->isAuthenticated() && REQUEST_TIME - $user->getLastAccessedTime() > Settings::get('session_write_interval', 180)) {
             /** @var \Drupal\user\UserStorageInterface $storage */
             $storage = \Drupal::entityManager()->getStorage('user');
             $storage->updateLastAccessTimestamp($user, REQUEST_TIME);
         }
         return TRUE;
     } catch (\Exception $exception) {
         require_once DRUPAL_ROOT . '/core/includes/errors.inc';
         // If we are displaying errors, then do so with no possibility of a
         // further uncaught exception being thrown.
         if (error_displayable()) {
             print '<h1>Uncaught exception thrown in session handler.</h1>';
             print '<p>' . Error::renderExceptionSafe($exception) . '</p><hr />';
         }
         return FALSE;
     }
 }