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

Redis.php

Zuletzt modifiziert: 09.10.2024, 12:59 - Dateigröße: 5.11 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  use Predis\Client as Predis;
 009   
 010  /*
 011   * Stores a token in a Redis server. Requires the Predis library available at https://github.com/nrk/predis
 012   */
 013  class Redis implements TokenStorageInterface
 014  {
 015      /**
 016       * @var string
 017       */
 018      protected $key;
 019   
 020      protected $stateKey;
 021   
 022      /**
 023       * @var object|\Redis
 024       */
 025      protected $redis;
 026   
 027      /**
 028       * @var object|TokenInterface
 029       */
 030      protected $cachedTokens;
 031   
 032      /**
 033       * @var object
 034       */
 035      protected $cachedStates;
 036   
 037      /**
 038       * @param Predis $redis An instantiated and connected redis client
 039       * @param string $key The key to store the token under in redis
 040       * @param string $stateKey The key to store the state under in redis.
 041       */
 042      public function __construct(Predis $redis, $key, $stateKey)
 043      {
 044          $this->redis = $redis;
 045          $this->key = $key;
 046          $this->stateKey = $stateKey;
 047          $this->cachedTokens = array();
 048          $this->cachedStates = array();
 049      }
 050   
 051      /**
 052       * {@inheritDoc}
 053       */
 054      public function retrieveAccessToken($service)
 055      {
 056          if (!$this->hasAccessToken($service)) {
 057              throw new TokenNotFoundException('Token not found in redis');
 058          }
 059   
 060          if (isset($this->cachedTokens[$service])) {
 061              return $this->cachedTokens[$service];
 062          }
 063   
 064          $val = $this->redis->hget($this->key, $service);
 065   
 066          return $this->cachedTokens[$service] = unserialize($val);
 067      }
 068   
 069      /**
 070       * {@inheritDoc}
 071       */
 072      public function storeAccessToken($service, TokenInterface $token)
 073      {
 074          // (over)write the token
 075          $this->redis->hset($this->key, $service, serialize($token));
 076          $this->cachedTokens[$service] = $token;
 077   
 078          // allow chaining
 079          return $this;
 080      }
 081   
 082      /**
 083       * {@inheritDoc}
 084       */
 085      public function hasAccessToken($service)
 086      {
 087          if (isset($this->cachedTokens[$service])
 088              && $this->cachedTokens[$service] instanceof TokenInterface
 089          ) {
 090              return true;
 091          }
 092   
 093          return $this->redis->hexists($this->key, $service);
 094      }
 095   
 096      /**
 097       * {@inheritDoc}
 098       */
 099      public function clearToken($service)
 100      {
 101          $this->redis->hdel($this->key, $service);
 102          unset($this->cachedTokens[$service]);
 103   
 104          // allow chaining
 105          return $this;
 106      }
 107   
 108      /**
 109       * {@inheritDoc}
 110       */
 111      public function clearAllTokens()
 112      {
 113          // memory
 114          $this->cachedTokens = array();
 115   
 116          // redis
 117          $keys = $this->redis->hkeys($this->key);
 118          $me = $this; // 5.3 compat
 119   
 120          // pipeline for performance
 121          $this->redis->pipeline(
 122              function ($pipe) use ($keys, $me) {
 123                  foreach ($keys as $k) {
 124                      $pipe->hdel($me->getKey(), $k);
 125                  }
 126              }
 127          );
 128   
 129          // allow chaining
 130          return $this;
 131      }
 132   
 133      /**
 134       * {@inheritDoc}
 135       */
 136      public function retrieveAuthorizationState($service)
 137      {
 138          if (!$this->hasAuthorizationState($service)) {
 139              throw new AuthorizationStateNotFoundException('State not found in redis');
 140          }
 141   
 142          if (isset($this->cachedStates[$service])) {
 143              return $this->cachedStates[$service];
 144          }
 145   
 146          $val = $this->redis->hget($this->stateKey, $service);
 147   
 148          return $this->cachedStates[$service] = $val;
 149      }
 150   
 151      /**
 152       * {@inheritDoc}
 153       */
 154      public function storeAuthorizationState($service, $state)
 155      {
 156          // (over)write the token
 157          $this->redis->hset($this->stateKey, $service, $state);
 158          $this->cachedStates[$service] = $state;
 159   
 160          // allow chaining
 161          return $this;
 162      }
 163   
 164      /**
 165       * {@inheritDoc}
 166       */
 167      public function hasAuthorizationState($service)
 168      {
 169          if (isset($this->cachedStates[$service])
 170              && null !== $this->cachedStates[$service]
 171          ) {
 172              return true;
 173          }
 174   
 175          return $this->redis->hexists($this->stateKey, $service);
 176      }
 177   
 178      /**
 179       * {@inheritDoc}
 180       */
 181      public function clearAuthorizationState($service)
 182      {
 183          $this->redis->hdel($this->stateKey, $service);
 184          unset($this->cachedStates[$service]);
 185   
 186          // allow chaining
 187          return $this;
 188      }
 189   
 190      /**
 191       * {@inheritDoc}
 192       */
 193      public function clearAllAuthorizationStates()
 194      {
 195          // memory
 196          $this->cachedStates = array();
 197   
 198          // redis
 199          $keys = $this->redis->hkeys($this->stateKey);
 200          $me = $this; // 5.3 compat
 201   
 202          // pipeline for performance
 203          $this->redis->pipeline(
 204              function ($pipe) use ($keys, $me) {
 205                  foreach ($keys as $k) {
 206                      $pipe->hdel($me->getKey(), $k);
 207                  }
 208              }
 209          );
 210   
 211          // allow chaining
 212          return $this;
 213      }
 214   
 215      /**
 216       * @return Predis $redis
 217       */
 218      public function getRedis()
 219      {
 220          return $this->redis;
 221      }
 222   
 223      /**
 224       * @return string $key
 225       */
 226      public function getKey()
 227      {
 228          return $this->key;
 229      }
 230  }
 231