Security update to Drupal 8.4.6
[yaffs-website] / vendor / doctrine / cache / lib / Doctrine / Common / Cache / LegacyMongoDBCache.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\Common\Cache;
21
22 use MongoBinData;
23 use MongoCollection;
24 use MongoCursorException;
25 use MongoDate;
26
27 /**
28  * MongoDB cache provider.
29  *
30  * @author Jeremy Mikola <jmikola@gmail.com>
31  * @internal Do not use - will be removed in 2.0. Use MongoDBCache instead
32  */
33 class LegacyMongoDBCache extends CacheProvider
34 {
35     /**
36      * @var MongoCollection
37      */
38     private $collection;
39
40     /**
41      * @var bool
42      */
43     private $expirationIndexCreated = false;
44
45     /**
46      * Constructor.
47      *
48      * This provider will default to the write concern and read preference
49      * options set on the MongoCollection instance (or inherited from MongoDB or
50      * MongoClient). Using an unacknowledged write concern (< 1) may make the
51      * return values of delete() and save() unreliable. Reading from secondaries
52      * may make contain() and fetch() unreliable.
53      *
54      * @see http://www.php.net/manual/en/mongo.readpreferences.php
55      * @see http://www.php.net/manual/en/mongo.writeconcerns.php
56      * @param MongoCollection $collection
57      */
58     public function __construct(MongoCollection $collection)
59     {
60         @trigger_error('Using the legacy MongoDB cache provider is deprecated and will be removed in 2.0', E_USER_DEPRECATED);
61         $this->collection = $collection;
62     }
63
64     /**
65      * {@inheritdoc}
66      */
67     protected function doFetch($id)
68     {
69         $document = $this->collection->findOne(['_id' => $id], [MongoDBCache::DATA_FIELD, MongoDBCache::EXPIRATION_FIELD]);
70
71         if ($document === null) {
72             return false;
73         }
74
75         if ($this->isExpired($document)) {
76             $this->createExpirationIndex();
77             $this->doDelete($id);
78             return false;
79         }
80
81         return unserialize($document[MongoDBCache::DATA_FIELD]->bin);
82     }
83
84     /**
85      * {@inheritdoc}
86      */
87     protected function doContains($id)
88     {
89         $document = $this->collection->findOne(['_id' => $id], [MongoDBCache::EXPIRATION_FIELD]);
90
91         if ($document === null) {
92             return false;
93         }
94
95         if ($this->isExpired($document)) {
96             $this->createExpirationIndex();
97             $this->doDelete($id);
98             return false;
99         }
100
101         return true;
102     }
103
104     /**
105      * {@inheritdoc}
106      */
107     protected function doSave($id, $data, $lifeTime = 0)
108     {
109         try {
110             $result = $this->collection->update(
111                 ['_id' => $id],
112                 ['$set' => [
113                     MongoDBCache::EXPIRATION_FIELD => ($lifeTime > 0 ? new MongoDate(time() + $lifeTime) : null),
114                     MongoDBCache::DATA_FIELD => new MongoBinData(serialize($data), MongoBinData::BYTE_ARRAY),
115                 ]],
116                 ['upsert' => true, 'multiple' => false]
117             );
118         } catch (MongoCursorException $e) {
119             return false;
120         }
121
122         return ($result['ok'] ?? 1) == 1;
123     }
124
125     /**
126      * {@inheritdoc}
127      */
128     protected function doDelete($id)
129     {
130         $result = $this->collection->remove(['_id' => $id]);
131
132         return ($result['ok'] ?? 1) == 1;
133     }
134
135     /**
136      * {@inheritdoc}
137      */
138     protected function doFlush()
139     {
140         // Use remove() in lieu of drop() to maintain any collection indexes
141         $result = $this->collection->remove();
142
143         return ($result['ok'] ?? 1) == 1;
144     }
145
146     /**
147      * {@inheritdoc}
148      */
149     protected function doGetStats()
150     {
151         $serverStatus = $this->collection->db->command([
152             'serverStatus' => 1,
153             'locks' => 0,
154             'metrics' => 0,
155             'recordStats' => 0,
156             'repl' => 0,
157         ]);
158
159         $collStats = $this->collection->db->command(['collStats' => 1]);
160
161         return [
162             Cache::STATS_HITS => null,
163             Cache::STATS_MISSES => null,
164             Cache::STATS_UPTIME => $serverStatus['uptime'] ?? null,
165             Cache::STATS_MEMORY_USAGE => $collStats['size'] ?? null,
166             Cache::STATS_MEMORY_AVAILABLE  => null,
167         ];
168     }
169
170     /**
171      * Check if the document is expired.
172      *
173      * @param array $document
174      *
175      * @return bool
176      */
177     private function isExpired(array $document) : bool
178     {
179         return isset($document[MongoDBCache::EXPIRATION_FIELD]) &&
180             $document[MongoDBCache::EXPIRATION_FIELD] instanceof MongoDate &&
181             $document[MongoDBCache::EXPIRATION_FIELD]->sec < time();
182     }
183
184
185     private function createExpirationIndex(): void
186     {
187         if ($this->expirationIndexCreated) {
188             return;
189         }
190
191         $this->expirationIndexCreated = true;
192         $this->collection->createIndex([MongoDBCache::EXPIRATION_FIELD => 1], ['background' => true, 'expireAfterSeconds' => 0]);
193     }
194 }