Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

Session.php

Zuletzt modifiziert: 09.10.2024, 12:59 - Dateigröße: 5.10 KiB


 001  <?php
 002   
 003  namespace OAuth\Common\Storage;
 004   
 005  use OAuth\Common\Token\TokenInterface;
 006  use OAuth\Common\Storage\Exception\TokenNotFoundException;
 007  use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException;
 008   
 009  /**
 010   * Stores a token in a PHP session.
 011   */
 012  class Session implements TokenStorageInterface
 013  {
 014      /**
 015       * @var bool
 016       */
 017      protected $startSession;
 018   
 019      /**
 020       * @var string
 021       */
 022      protected $sessionVariableName;
 023   
 024      /**
 025       * @var string
 026       */
 027      protected $stateVariableName;
 028   
 029      /**
 030       * @param bool $startSession Whether or not to start the session upon construction.
 031       * @param string $sessionVariableName the variable name to use within the _SESSION superglobal
 032       * @param string $stateVariableName
 033       */
 034      public function __construct(
 035          $startSession = true,
 036          $sessionVariableName = 'lusitanian-oauth-token',
 037          $stateVariableName = 'lusitanian-oauth-state'
 038      ) {
 039          if ($startSession && !$this->sessionHasStarted()) {
 040              session_start();
 041          }
 042   
 043          $this->startSession = $startSession;
 044          $this->sessionVariableName = $sessionVariableName;
 045          $this->stateVariableName = $stateVariableName;
 046          if (!isset($_SESSION[$sessionVariableName])) {
 047              $_SESSION[$sessionVariableName] = array();
 048          }
 049          if (!isset($_SESSION[$stateVariableName])) {
 050              $_SESSION[$stateVariableName] = array();
 051          }
 052      }
 053   
 054      /**
 055       * {@inheritDoc}
 056       */
 057      public function retrieveAccessToken($service)
 058      {
 059          if ($this->hasAccessToken($service)) {
 060              return unserialize($_SESSION[$this->sessionVariableName][$service]);
 061          }
 062   
 063          throw new TokenNotFoundException('Token not found in session, are you sure you stored it?');
 064      }
 065   
 066      /**
 067       * {@inheritDoc}
 068       */
 069      public function storeAccessToken($service, TokenInterface $token)
 070      {
 071          $serializedToken = serialize($token);
 072   
 073          if (isset($_SESSION[$this->sessionVariableName])
 074              && is_array($_SESSION[$this->sessionVariableName])
 075          ) {
 076              $_SESSION[$this->sessionVariableName][$service] = $serializedToken;
 077          } else {
 078              $_SESSION[$this->sessionVariableName] = array(
 079                  $service => $serializedToken,
 080              );
 081          }
 082   
 083          // allow chaining
 084          return $this;
 085      }
 086   
 087      /**
 088       * {@inheritDoc}
 089       */
 090      public function hasAccessToken($service)
 091      {
 092          return isset($_SESSION[$this->sessionVariableName], $_SESSION[$this->sessionVariableName][$service]);
 093      }
 094   
 095      /**
 096       * {@inheritDoc}
 097       */
 098      public function clearToken($service)
 099      {
 100          if (array_key_exists($service, $_SESSION[$this->sessionVariableName])) {
 101              unset($_SESSION[$this->sessionVariableName][$service]);
 102          }
 103   
 104          // allow chaining
 105          return $this;
 106      }
 107   
 108      /**
 109       * {@inheritDoc}
 110       */
 111      public function clearAllTokens()
 112      {
 113          unset($_SESSION[$this->sessionVariableName]);
 114   
 115          // allow chaining
 116          return $this;
 117      }
 118   
 119      /**
 120       * {@inheritDoc}
 121       */
 122      public function storeAuthorizationState($service, $state)
 123      {
 124          if (isset($_SESSION[$this->stateVariableName])
 125              && is_array($_SESSION[$this->stateVariableName])
 126          ) {
 127              $_SESSION[$this->stateVariableName][$service] = $state;
 128          } else {
 129              $_SESSION[$this->stateVariableName] = array(
 130                  $service => $state,
 131              );
 132          }
 133   
 134          // allow chaining
 135          return $this;
 136      }
 137   
 138      /**
 139       * {@inheritDoc}
 140       */
 141      public function hasAuthorizationState($service)
 142      {
 143          return isset($_SESSION[$this->stateVariableName], $_SESSION[$this->stateVariableName][$service]);
 144      }
 145   
 146      /**
 147       * {@inheritDoc}
 148       */
 149      public function retrieveAuthorizationState($service)
 150      {
 151          if ($this->hasAuthorizationState($service)) {
 152              return $_SESSION[$this->stateVariableName][$service];
 153          }
 154   
 155          throw new AuthorizationStateNotFoundException('State not found in session, are you sure you stored it?');
 156      }
 157   
 158      /**
 159       * {@inheritDoc}
 160       */
 161      public function clearAuthorizationState($service)
 162      {
 163          if (array_key_exists($service, $_SESSION[$this->stateVariableName])) {
 164              unset($_SESSION[$this->stateVariableName][$service]);
 165          }
 166   
 167          // allow chaining
 168          return $this;
 169      }
 170   
 171      /**
 172       * {@inheritDoc}
 173       */
 174      public function clearAllAuthorizationStates()
 175      {
 176          unset($_SESSION[$this->stateVariableName]);
 177   
 178          // allow chaining
 179          return $this;
 180      }
 181   
 182      public function __destruct()
 183      {
 184          if ($this->startSession) {
 185              session_write_close();
 186          }
 187      }
 188   
 189      /**
 190       * Determine if the session has started.
 191       * @url http://stackoverflow.com/a/18542272/1470961
 192       * @return bool
 193       */
 194      protected function sessionHasStarted()
 195      {
 196          // For more modern PHP versions we use a more reliable method.
 197          if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
 198              return session_status() != PHP_SESSION_NONE;
 199          }
 200   
 201          // Below PHP 5.4 we should test for the current session ID.
 202          return session_id() !== '';
 203      }
 204  }
 205