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

MongoDbSessionHandler.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 6.64 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
013   
014  /**
015   * MongoDB session handler.
016   *
017   * @author Markus Bachmann <markus.bachmann@bachi.biz>
018   */
019  class MongoDbSessionHandler implements \SessionHandlerInterface
020  {
021      /**
022       * @var \Mongo|\MongoClient|\MongoDB\Client
023       */
024      private $mongo;
025   
026      /**
027       * @var \MongoCollection
028       */
029      private $collection;
030   
031      /**
032       * @var array
033       */
034      private $options;
035   
036      /**
037       * Constructor.
038       *
039       * List of available options:
040       *  * database: The name of the database [required]
041       *  * collection: The name of the collection [required]
042       *  * id_field: The field name for storing the session id [default: _id]
043       *  * data_field: The field name for storing the session data [default: data]
044       *  * time_field: The field name for storing the timestamp [default: time]
045       *  * expiry_field: The field name for storing the expiry-timestamp [default: expires_at]
046       *
047       * It is strongly recommended to put an index on the `expiry_field` for
048       * garbage-collection. Alternatively it's possible to automatically expire
049       * the sessions in the database as described below:
050       *
051       * A TTL collections can be used on MongoDB 2.2+ to cleanup expired sessions
052       * automatically. Such an index can for example look like this:
053       *
054       *     db.<session-collection>.ensureIndex(
055       *         { "<expiry-field>": 1 },
056       *         { "expireAfterSeconds": 0 }
057       *     )
058       *
059       * More details on: http://docs.mongodb.org/manual/tutorial/expire-data/
060       *
061       * If you use such an index, you can drop `gc_probability` to 0 since
062       * no garbage-collection is required.
063       *
064       * @param \Mongo|\MongoClient|\MongoDB\Client $mongo   A MongoDB\Client, MongoClient or Mongo instance
065       * @param array                               $options An associative array of field options
066       *
067       * @throws \InvalidArgumentException When MongoClient or Mongo instance not provided
068       * @throws \InvalidArgumentException When "database" or "collection" not provided
069       */
070      public function __construct($mongo, array $options)
071      {
072          if (!($mongo instanceof \MongoDB\Client || $mongo instanceof \MongoClient || $mongo instanceof \Mongo)) {
073              throw new \InvalidArgumentException('MongoClient or Mongo instance required');
074          }
075   
076          if (!isset($options['database']) || !isset($options['collection'])) {
077              throw new \InvalidArgumentException('You must provide the "database" and "collection" option for MongoDBSessionHandler');
078          }
079   
080          $this->mongo = $mongo;
081   
082          $this->options = array_merge(array(
083              'id_field' => '_id',
084              'data_field' => 'data',
085              'time_field' => 'time',
086              'expiry_field' => 'expires_at',
087          ), $options);
088      }
089   
090      /**
091       * {@inheritdoc}
092       */
093      public function open($savePath, $sessionName)
094      {
095          return true;
096      }
097   
098      /**
099       * {@inheritdoc}
100       */
101      public function close()
102      {
103          return true;
104      }
105   
106      /**
107       * {@inheritdoc}
108       */
109      public function destroy($sessionId)
110      {
111          $methodName = $this->mongo instanceof \MongoDB\Client ? 'deleteOne' : 'remove';
112   
113          $this->getCollection()->$methodName(array(
114              $this->options['id_field'] => $sessionId,
115          ));
116   
117          return true;
118      }
119   
120      /**
121       * {@inheritdoc}
122       */
123      public function gc($maxlifetime)
124      {
125          $methodName = $this->mongo instanceof \MongoDB\Client ? 'deleteOne' : 'remove';
126   
127          $this->getCollection()->$methodName(array(
128              $this->options['expiry_field'] => array('$lt' => $this->createDateTime()),
129          ));
130   
131          return true;
132      }
133   
134      /**
135       * {@inheritdoc}
136       */
137      public function write($sessionId, $data)
138      {
139          $expiry = $this->createDateTime(time() + (int) ini_get('session.gc_maxlifetime'));
140   
141          $fields = array(
142              $this->options['time_field'] => $this->createDateTime(),
143              $this->options['expiry_field'] => $expiry,
144          );
145   
146          $options = array('upsert' => true);
147   
148          if ($this->mongo instanceof \MongoDB\Client) {
149              $fields[$this->options['data_field']] = new \MongoDB\BSON\Binary($data, \MongoDB\BSON\Binary::TYPE_OLD_BINARY);
150          } else {
151              $fields[$this->options['data_field']] = new \MongoBinData($data, \MongoBinData::BYTE_ARRAY);
152              $options['multiple'] = false;
153          }
154   
155          $methodName = $this->mongo instanceof \MongoDB\Client ? 'updateOne' : 'update';
156   
157          $this->getCollection()->$methodName(
158              array($this->options['id_field'] => $sessionId),
159              array('$set' => $fields),
160              $options
161          );
162   
163          return true;
164      }
165   
166      /**
167       * {@inheritdoc}
168       */
169      public function read($sessionId)
170      {
171          $dbData = $this->getCollection()->findOne(array(
172              $this->options['id_field'] => $sessionId,
173              $this->options['expiry_field'] => array('$gte' => $this->createDateTime()),
174          ));
175   
176          if (null === $dbData) {
177              return '';
178          }
179   
180          if ($dbData[$this->options['data_field']] instanceof \MongoDB\BSON\Binary) {
181              return $dbData[$this->options['data_field']]->getData();
182          }
183   
184          return $dbData[$this->options['data_field']]->bin;
185      }
186   
187      /**
188       * Return a "MongoCollection" instance.
189       *
190       * @return \MongoCollection
191       */
192      private function getCollection()
193      {
194          if (null === $this->collection) {
195              $this->collection = $this->mongo->selectCollection($this->options['database'], $this->options['collection']);
196          }
197   
198          return $this->collection;
199      }
200   
201      /**
202       * Return a Mongo instance.
203       *
204       * @return \Mongo|\MongoClient|\MongoDB\Client
205       */
206      protected function getMongo()
207      {
208          return $this->mongo;
209      }
210   
211      /**
212       * Create a date object using the class appropriate for the current mongo connection.
213       *
214       * Return an instance of a MongoDate or \MongoDB\BSON\UTCDateTime
215       *
216       * @param int $seconds An integer representing UTC seconds since Jan 1 1970.  Defaults to now.
217       *
218       * @return \MongoDate|\MongoDB\BSON\UTCDateTime
219       */
220      private function createDateTime($seconds = null)
221      {
222          if (null === $seconds) {
223              $seconds = time();
224          }
225   
226          if ($this->mongo instanceof \MongoDB\Client) {
227              return new \MongoDB\BSON\UTCDateTime($seconds * 1000);
228          }
229   
230          return new \MongoDate($seconds);
231      }
232  }
233