@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | ->from('file_locks') |
114 | 114 | ->where($query->expr()->eq('key', $query->createNamedParameter($path))); |
115 | 115 | $result = $query->executeQuery(); |
116 | - $lockValue = (int)$result->fetchOne(); |
|
116 | + $lockValue = (int) $result->fetchOne(); |
|
117 | 117 | if ($type === self::LOCK_SHARED) { |
118 | 118 | if ($this->isLocallyLocked($path)) { |
119 | 119 | // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | } |
248 | 248 | // since we keep shared locks we need to manually clean those |
249 | 249 | $lockedPaths = array_keys($this->sharedLocks); |
250 | - $lockedPaths = array_filter($lockedPaths, function ($path) { |
|
250 | + $lockedPaths = array_filter($lockedPaths, function($path) { |
|
251 | 251 | return $this->sharedLocks[$path]; |
252 | 252 | }); |
253 | 253 |
@@ -18,223 +18,223 @@ |
||
18 | 18 | * Locking provider that stores the locks in the database |
19 | 19 | */ |
20 | 20 | class DBLockingProvider extends AbstractLockingProvider { |
21 | - private array $sharedLocks = []; |
|
22 | - |
|
23 | - public function __construct( |
|
24 | - private IDBConnection $connection, |
|
25 | - private ITimeFactory $timeFactory, |
|
26 | - int $ttl = 3600, |
|
27 | - private bool $cacheSharedLocks = true, |
|
28 | - ) { |
|
29 | - parent::__construct($ttl); |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * Check if we have an open shared lock for a path |
|
34 | - */ |
|
35 | - protected function isLocallyLocked(string $path): bool { |
|
36 | - return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path]; |
|
37 | - } |
|
38 | - |
|
39 | - /** @inheritDoc */ |
|
40 | - protected function markAcquire(string $path, int $targetType): void { |
|
41 | - parent::markAcquire($path, $targetType); |
|
42 | - if ($this->cacheSharedLocks) { |
|
43 | - if ($targetType === self::LOCK_SHARED) { |
|
44 | - $this->sharedLocks[$path] = true; |
|
45 | - } |
|
46 | - } |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Change the type of an existing tracked lock |
|
51 | - */ |
|
52 | - protected function markChange(string $path, int $targetType): void { |
|
53 | - parent::markChange($path, $targetType); |
|
54 | - if ($this->cacheSharedLocks) { |
|
55 | - if ($targetType === self::LOCK_SHARED) { |
|
56 | - $this->sharedLocks[$path] = true; |
|
57 | - } elseif ($targetType === self::LOCK_EXCLUSIVE) { |
|
58 | - $this->sharedLocks[$path] = false; |
|
59 | - } |
|
60 | - } |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Insert a file locking row if it does not exists. |
|
65 | - */ |
|
66 | - protected function initLockField(string $path, int $lock = 0): int { |
|
67 | - $expire = $this->getExpireTime(); |
|
68 | - return $this->connection->insertIgnoreConflict('file_locks', [ |
|
69 | - 'key' => $path, |
|
70 | - 'lock' => $lock, |
|
71 | - 'ttl' => $expire |
|
72 | - ]); |
|
73 | - } |
|
74 | - |
|
75 | - protected function getExpireTime(): int { |
|
76 | - return $this->timeFactory->getTime() + $this->ttl; |
|
77 | - } |
|
78 | - |
|
79 | - /** @inheritDoc */ |
|
80 | - public function isLocked(string $path, int $type): bool { |
|
81 | - if ($this->hasAcquiredLock($path, $type)) { |
|
82 | - return true; |
|
83 | - } |
|
84 | - $query = $this->connection->getQueryBuilder(); |
|
85 | - $query->select('lock') |
|
86 | - ->from('file_locks') |
|
87 | - ->where($query->expr()->eq('key', $query->createNamedParameter($path))); |
|
88 | - $result = $query->executeQuery(); |
|
89 | - $lockValue = (int)$result->fetchOne(); |
|
90 | - if ($type === self::LOCK_SHARED) { |
|
91 | - if ($this->isLocallyLocked($path)) { |
|
92 | - // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
|
93 | - return $lockValue > 1; |
|
94 | - } else { |
|
95 | - return $lockValue > 0; |
|
96 | - } |
|
97 | - } elseif ($type === self::LOCK_EXCLUSIVE) { |
|
98 | - return $lockValue === -1; |
|
99 | - } else { |
|
100 | - return false; |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - /** @inheritDoc */ |
|
105 | - public function acquireLock(string $path, int $type, ?string $readablePath = null): void { |
|
106 | - $expire = $this->getExpireTime(); |
|
107 | - if ($type === self::LOCK_SHARED) { |
|
108 | - if (!$this->isLocallyLocked($path)) { |
|
109 | - $result = $this->initLockField($path, 1); |
|
110 | - if ($result <= 0) { |
|
111 | - $query = $this->connection->getQueryBuilder(); |
|
112 | - $query->update('file_locks') |
|
113 | - ->set('lock', $query->func()->add('lock', $query->createNamedParameter(1, IQueryBuilder::PARAM_INT))) |
|
114 | - ->set('ttl', $query->createNamedParameter($expire)) |
|
115 | - ->where($query->expr()->eq('key', $query->createNamedParameter($path))) |
|
116 | - ->andWhere($query->expr()->gte('lock', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT))); |
|
117 | - $result = $query->executeStatement(); |
|
118 | - } |
|
119 | - } else { |
|
120 | - $result = 1; |
|
121 | - } |
|
122 | - } else { |
|
123 | - $existing = 0; |
|
124 | - if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) { |
|
125 | - $existing = 1; |
|
126 | - } |
|
127 | - $result = $this->initLockField($path, -1); |
|
128 | - if ($result <= 0) { |
|
129 | - $query = $this->connection->getQueryBuilder(); |
|
130 | - $query->update('file_locks') |
|
131 | - ->set('lock', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)) |
|
132 | - ->set('ttl', $query->createNamedParameter($expire, IQueryBuilder::PARAM_INT)) |
|
133 | - ->where($query->expr()->eq('key', $query->createNamedParameter($path))) |
|
134 | - ->andWhere($query->expr()->eq('lock', $query->createNamedParameter($existing))); |
|
135 | - $result = $query->executeStatement(); |
|
136 | - } |
|
137 | - } |
|
138 | - if ($result !== 1) { |
|
139 | - throw new LockedException($path, null, null, $readablePath); |
|
140 | - } |
|
141 | - $this->markAcquire($path, $type); |
|
142 | - } |
|
143 | - |
|
144 | - /** @inheritDoc */ |
|
145 | - public function releaseLock(string $path, int $type): void { |
|
146 | - $this->markRelease($path, $type); |
|
147 | - |
|
148 | - // we keep shared locks till the end of the request so we can re-use them |
|
149 | - if ($type === self::LOCK_EXCLUSIVE) { |
|
150 | - $qb = $this->connection->getQueryBuilder(); |
|
151 | - $qb->update('file_locks') |
|
152 | - ->set('lock', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)) |
|
153 | - ->where($qb->expr()->eq('key', $qb->createNamedParameter($path))) |
|
154 | - ->andWhere($qb->expr()->eq('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT))); |
|
155 | - $qb->executeStatement(); |
|
156 | - } elseif (!$this->cacheSharedLocks) { |
|
157 | - $qb = $this->connection->getQueryBuilder(); |
|
158 | - $qb->update('file_locks') |
|
159 | - ->set('lock', $qb->func()->subtract('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT))) |
|
160 | - ->where($qb->expr()->eq('key', $qb->createNamedParameter($path))) |
|
161 | - ->andWhere($qb->expr()->gt('lock', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))); |
|
162 | - $qb->executeStatement(); |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - /** @inheritDoc */ |
|
167 | - public function changeLock(string $path, int $targetType): void { |
|
168 | - $expire = $this->getExpireTime(); |
|
169 | - if ($targetType === self::LOCK_SHARED) { |
|
170 | - $qb = $this->connection->getQueryBuilder(); |
|
171 | - $result = $qb->update('file_locks') |
|
172 | - ->set('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)) |
|
173 | - ->set('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT)) |
|
174 | - ->where($qb->expr()->andX( |
|
175 | - $qb->expr()->eq('key', $qb->createNamedParameter($path)), |
|
176 | - $qb->expr()->eq('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT)) |
|
177 | - ))->executeStatement(); |
|
178 | - } else { |
|
179 | - // since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually |
|
180 | - if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) { |
|
181 | - throw new LockedException($path); |
|
182 | - } |
|
183 | - $qb = $this->connection->getQueryBuilder(); |
|
184 | - $result = $qb->update('file_locks') |
|
185 | - ->set('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT)) |
|
186 | - ->set('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT)) |
|
187 | - ->where($qb->expr()->andX( |
|
188 | - $qb->expr()->eq('key', $qb->createNamedParameter($path)), |
|
189 | - $qb->expr()->eq('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)) |
|
190 | - ))->executeStatement(); |
|
191 | - } |
|
192 | - if ($result !== 1) { |
|
193 | - throw new LockedException($path); |
|
194 | - } |
|
195 | - $this->markChange($path, $targetType); |
|
196 | - } |
|
197 | - |
|
198 | - /** @inheritDoc */ |
|
199 | - public function cleanExpiredLocks(): void { |
|
200 | - $expire = $this->timeFactory->getTime(); |
|
201 | - try { |
|
202 | - $qb = $this->connection->getQueryBuilder(); |
|
203 | - $qb->delete('file_locks') |
|
204 | - ->where($qb->expr()->lt('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT))) |
|
205 | - ->executeStatement(); |
|
206 | - } catch (\Exception $e) { |
|
207 | - // If the table is missing, the clean up was successful |
|
208 | - if ($this->connection->tableExists('file_locks')) { |
|
209 | - throw $e; |
|
210 | - } |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - /** @inheritDoc */ |
|
215 | - public function releaseAll(): void { |
|
216 | - parent::releaseAll(); |
|
217 | - |
|
218 | - if (!$this->cacheSharedLocks) { |
|
219 | - return; |
|
220 | - } |
|
221 | - // since we keep shared locks we need to manually clean those |
|
222 | - $lockedPaths = array_keys($this->sharedLocks); |
|
223 | - $lockedPaths = array_filter($lockedPaths, function ($path) { |
|
224 | - return $this->sharedLocks[$path]; |
|
225 | - }); |
|
226 | - |
|
227 | - $chunkedPaths = array_chunk($lockedPaths, 100); |
|
228 | - |
|
229 | - $qb = $this->connection->getQueryBuilder(); |
|
230 | - $qb->update('file_locks') |
|
231 | - ->set('lock', $qb->func()->subtract('lock', $qb->expr()->literal(1))) |
|
232 | - ->where($qb->expr()->in('key', $qb->createParameter('chunk'))) |
|
233 | - ->andWhere($qb->expr()->gt('lock', new Literal(0))); |
|
234 | - |
|
235 | - foreach ($chunkedPaths as $chunk) { |
|
236 | - $qb->setParameter('chunk', $chunk, IQueryBuilder::PARAM_STR_ARRAY); |
|
237 | - $qb->executeStatement(); |
|
238 | - } |
|
239 | - } |
|
21 | + private array $sharedLocks = []; |
|
22 | + |
|
23 | + public function __construct( |
|
24 | + private IDBConnection $connection, |
|
25 | + private ITimeFactory $timeFactory, |
|
26 | + int $ttl = 3600, |
|
27 | + private bool $cacheSharedLocks = true, |
|
28 | + ) { |
|
29 | + parent::__construct($ttl); |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * Check if we have an open shared lock for a path |
|
34 | + */ |
|
35 | + protected function isLocallyLocked(string $path): bool { |
|
36 | + return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path]; |
|
37 | + } |
|
38 | + |
|
39 | + /** @inheritDoc */ |
|
40 | + protected function markAcquire(string $path, int $targetType): void { |
|
41 | + parent::markAcquire($path, $targetType); |
|
42 | + if ($this->cacheSharedLocks) { |
|
43 | + if ($targetType === self::LOCK_SHARED) { |
|
44 | + $this->sharedLocks[$path] = true; |
|
45 | + } |
|
46 | + } |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Change the type of an existing tracked lock |
|
51 | + */ |
|
52 | + protected function markChange(string $path, int $targetType): void { |
|
53 | + parent::markChange($path, $targetType); |
|
54 | + if ($this->cacheSharedLocks) { |
|
55 | + if ($targetType === self::LOCK_SHARED) { |
|
56 | + $this->sharedLocks[$path] = true; |
|
57 | + } elseif ($targetType === self::LOCK_EXCLUSIVE) { |
|
58 | + $this->sharedLocks[$path] = false; |
|
59 | + } |
|
60 | + } |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Insert a file locking row if it does not exists. |
|
65 | + */ |
|
66 | + protected function initLockField(string $path, int $lock = 0): int { |
|
67 | + $expire = $this->getExpireTime(); |
|
68 | + return $this->connection->insertIgnoreConflict('file_locks', [ |
|
69 | + 'key' => $path, |
|
70 | + 'lock' => $lock, |
|
71 | + 'ttl' => $expire |
|
72 | + ]); |
|
73 | + } |
|
74 | + |
|
75 | + protected function getExpireTime(): int { |
|
76 | + return $this->timeFactory->getTime() + $this->ttl; |
|
77 | + } |
|
78 | + |
|
79 | + /** @inheritDoc */ |
|
80 | + public function isLocked(string $path, int $type): bool { |
|
81 | + if ($this->hasAcquiredLock($path, $type)) { |
|
82 | + return true; |
|
83 | + } |
|
84 | + $query = $this->connection->getQueryBuilder(); |
|
85 | + $query->select('lock') |
|
86 | + ->from('file_locks') |
|
87 | + ->where($query->expr()->eq('key', $query->createNamedParameter($path))); |
|
88 | + $result = $query->executeQuery(); |
|
89 | + $lockValue = (int)$result->fetchOne(); |
|
90 | + if ($type === self::LOCK_SHARED) { |
|
91 | + if ($this->isLocallyLocked($path)) { |
|
92 | + // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
|
93 | + return $lockValue > 1; |
|
94 | + } else { |
|
95 | + return $lockValue > 0; |
|
96 | + } |
|
97 | + } elseif ($type === self::LOCK_EXCLUSIVE) { |
|
98 | + return $lockValue === -1; |
|
99 | + } else { |
|
100 | + return false; |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + /** @inheritDoc */ |
|
105 | + public function acquireLock(string $path, int $type, ?string $readablePath = null): void { |
|
106 | + $expire = $this->getExpireTime(); |
|
107 | + if ($type === self::LOCK_SHARED) { |
|
108 | + if (!$this->isLocallyLocked($path)) { |
|
109 | + $result = $this->initLockField($path, 1); |
|
110 | + if ($result <= 0) { |
|
111 | + $query = $this->connection->getQueryBuilder(); |
|
112 | + $query->update('file_locks') |
|
113 | + ->set('lock', $query->func()->add('lock', $query->createNamedParameter(1, IQueryBuilder::PARAM_INT))) |
|
114 | + ->set('ttl', $query->createNamedParameter($expire)) |
|
115 | + ->where($query->expr()->eq('key', $query->createNamedParameter($path))) |
|
116 | + ->andWhere($query->expr()->gte('lock', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT))); |
|
117 | + $result = $query->executeStatement(); |
|
118 | + } |
|
119 | + } else { |
|
120 | + $result = 1; |
|
121 | + } |
|
122 | + } else { |
|
123 | + $existing = 0; |
|
124 | + if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) { |
|
125 | + $existing = 1; |
|
126 | + } |
|
127 | + $result = $this->initLockField($path, -1); |
|
128 | + if ($result <= 0) { |
|
129 | + $query = $this->connection->getQueryBuilder(); |
|
130 | + $query->update('file_locks') |
|
131 | + ->set('lock', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)) |
|
132 | + ->set('ttl', $query->createNamedParameter($expire, IQueryBuilder::PARAM_INT)) |
|
133 | + ->where($query->expr()->eq('key', $query->createNamedParameter($path))) |
|
134 | + ->andWhere($query->expr()->eq('lock', $query->createNamedParameter($existing))); |
|
135 | + $result = $query->executeStatement(); |
|
136 | + } |
|
137 | + } |
|
138 | + if ($result !== 1) { |
|
139 | + throw new LockedException($path, null, null, $readablePath); |
|
140 | + } |
|
141 | + $this->markAcquire($path, $type); |
|
142 | + } |
|
143 | + |
|
144 | + /** @inheritDoc */ |
|
145 | + public function releaseLock(string $path, int $type): void { |
|
146 | + $this->markRelease($path, $type); |
|
147 | + |
|
148 | + // we keep shared locks till the end of the request so we can re-use them |
|
149 | + if ($type === self::LOCK_EXCLUSIVE) { |
|
150 | + $qb = $this->connection->getQueryBuilder(); |
|
151 | + $qb->update('file_locks') |
|
152 | + ->set('lock', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)) |
|
153 | + ->where($qb->expr()->eq('key', $qb->createNamedParameter($path))) |
|
154 | + ->andWhere($qb->expr()->eq('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT))); |
|
155 | + $qb->executeStatement(); |
|
156 | + } elseif (!$this->cacheSharedLocks) { |
|
157 | + $qb = $this->connection->getQueryBuilder(); |
|
158 | + $qb->update('file_locks') |
|
159 | + ->set('lock', $qb->func()->subtract('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT))) |
|
160 | + ->where($qb->expr()->eq('key', $qb->createNamedParameter($path))) |
|
161 | + ->andWhere($qb->expr()->gt('lock', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))); |
|
162 | + $qb->executeStatement(); |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + /** @inheritDoc */ |
|
167 | + public function changeLock(string $path, int $targetType): void { |
|
168 | + $expire = $this->getExpireTime(); |
|
169 | + if ($targetType === self::LOCK_SHARED) { |
|
170 | + $qb = $this->connection->getQueryBuilder(); |
|
171 | + $result = $qb->update('file_locks') |
|
172 | + ->set('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)) |
|
173 | + ->set('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT)) |
|
174 | + ->where($qb->expr()->andX( |
|
175 | + $qb->expr()->eq('key', $qb->createNamedParameter($path)), |
|
176 | + $qb->expr()->eq('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT)) |
|
177 | + ))->executeStatement(); |
|
178 | + } else { |
|
179 | + // since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually |
|
180 | + if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) { |
|
181 | + throw new LockedException($path); |
|
182 | + } |
|
183 | + $qb = $this->connection->getQueryBuilder(); |
|
184 | + $result = $qb->update('file_locks') |
|
185 | + ->set('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT)) |
|
186 | + ->set('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT)) |
|
187 | + ->where($qb->expr()->andX( |
|
188 | + $qb->expr()->eq('key', $qb->createNamedParameter($path)), |
|
189 | + $qb->expr()->eq('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)) |
|
190 | + ))->executeStatement(); |
|
191 | + } |
|
192 | + if ($result !== 1) { |
|
193 | + throw new LockedException($path); |
|
194 | + } |
|
195 | + $this->markChange($path, $targetType); |
|
196 | + } |
|
197 | + |
|
198 | + /** @inheritDoc */ |
|
199 | + public function cleanExpiredLocks(): void { |
|
200 | + $expire = $this->timeFactory->getTime(); |
|
201 | + try { |
|
202 | + $qb = $this->connection->getQueryBuilder(); |
|
203 | + $qb->delete('file_locks') |
|
204 | + ->where($qb->expr()->lt('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT))) |
|
205 | + ->executeStatement(); |
|
206 | + } catch (\Exception $e) { |
|
207 | + // If the table is missing, the clean up was successful |
|
208 | + if ($this->connection->tableExists('file_locks')) { |
|
209 | + throw $e; |
|
210 | + } |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + /** @inheritDoc */ |
|
215 | + public function releaseAll(): void { |
|
216 | + parent::releaseAll(); |
|
217 | + |
|
218 | + if (!$this->cacheSharedLocks) { |
|
219 | + return; |
|
220 | + } |
|
221 | + // since we keep shared locks we need to manually clean those |
|
222 | + $lockedPaths = array_keys($this->sharedLocks); |
|
223 | + $lockedPaths = array_filter($lockedPaths, function ($path) { |
|
224 | + return $this->sharedLocks[$path]; |
|
225 | + }); |
|
226 | + |
|
227 | + $chunkedPaths = array_chunk($lockedPaths, 100); |
|
228 | + |
|
229 | + $qb = $this->connection->getQueryBuilder(); |
|
230 | + $qb->update('file_locks') |
|
231 | + ->set('lock', $qb->func()->subtract('lock', $qb->expr()->literal(1))) |
|
232 | + ->where($qb->expr()->in('key', $qb->createParameter('chunk'))) |
|
233 | + ->andWhere($qb->expr()->gt('lock', new Literal(0))); |
|
234 | + |
|
235 | + foreach ($chunkedPaths as $chunk) { |
|
236 | + $qb->setParameter('chunk', $chunk, IQueryBuilder::PARAM_STR_ARRAY); |
|
237 | + $qb->executeStatement(); |
|
238 | + } |
|
239 | + } |
|
240 | 240 | } |
@@ -69,7 +69,7 @@ |
||
69 | 69 | public function stopQuery() { |
70 | 70 | if ($this->activated && $this->activeQuery) { |
71 | 71 | $this->activeQuery->end(microtime(true)); |
72 | - $this->queries[(string)$this->index] = $this->activeQuery; |
|
72 | + $this->queries[(string) $this->index] = $this->activeQuery; |
|
73 | 73 | $this->index++; |
74 | 74 | $this->activeQuery = null; |
75 | 75 | } |
@@ -11,64 +11,64 @@ |
||
11 | 11 | use OCP\Diagnostics\IQueryLogger; |
12 | 12 | |
13 | 13 | class QueryLogger implements IQueryLogger { |
14 | - protected int $index = 0; |
|
15 | - protected ?Query $activeQuery = null; |
|
16 | - /** @var CappedMemoryCache<Query> */ |
|
17 | - protected CappedMemoryCache $queries; |
|
14 | + protected int $index = 0; |
|
15 | + protected ?Query $activeQuery = null; |
|
16 | + /** @var CappedMemoryCache<Query> */ |
|
17 | + protected CappedMemoryCache $queries; |
|
18 | 18 | |
19 | - /** |
|
20 | - * QueryLogger constructor. |
|
21 | - */ |
|
22 | - public function __construct() { |
|
23 | - $this->queries = new CappedMemoryCache(1024); |
|
24 | - } |
|
19 | + /** |
|
20 | + * QueryLogger constructor. |
|
21 | + */ |
|
22 | + public function __construct() { |
|
23 | + $this->queries = new CappedMemoryCache(1024); |
|
24 | + } |
|
25 | 25 | |
26 | 26 | |
27 | - /** |
|
28 | - * @var bool - Module needs to be activated by some app |
|
29 | - */ |
|
30 | - private $activated = false; |
|
27 | + /** |
|
28 | + * @var bool - Module needs to be activated by some app |
|
29 | + */ |
|
30 | + private $activated = false; |
|
31 | 31 | |
32 | - /** |
|
33 | - * @inheritdoc |
|
34 | - */ |
|
35 | - public function startQuery($sql, ?array $params = null, ?array $types = null) { |
|
36 | - if ($this->activated) { |
|
37 | - $this->activeQuery = new Query($sql, $params, microtime(true), $this->getStack()); |
|
38 | - } |
|
39 | - } |
|
32 | + /** |
|
33 | + * @inheritdoc |
|
34 | + */ |
|
35 | + public function startQuery($sql, ?array $params = null, ?array $types = null) { |
|
36 | + if ($this->activated) { |
|
37 | + $this->activeQuery = new Query($sql, $params, microtime(true), $this->getStack()); |
|
38 | + } |
|
39 | + } |
|
40 | 40 | |
41 | - private function getStack() { |
|
42 | - $stack = debug_backtrace(); |
|
43 | - array_shift($stack); |
|
44 | - array_shift($stack); |
|
45 | - array_shift($stack); |
|
46 | - return $stack; |
|
47 | - } |
|
41 | + private function getStack() { |
|
42 | + $stack = debug_backtrace(); |
|
43 | + array_shift($stack); |
|
44 | + array_shift($stack); |
|
45 | + array_shift($stack); |
|
46 | + return $stack; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @inheritdoc |
|
51 | - */ |
|
52 | - public function stopQuery() { |
|
53 | - if ($this->activated && $this->activeQuery) { |
|
54 | - $this->activeQuery->end(microtime(true)); |
|
55 | - $this->queries[(string)$this->index] = $this->activeQuery; |
|
56 | - $this->index++; |
|
57 | - $this->activeQuery = null; |
|
58 | - } |
|
59 | - } |
|
49 | + /** |
|
50 | + * @inheritdoc |
|
51 | + */ |
|
52 | + public function stopQuery() { |
|
53 | + if ($this->activated && $this->activeQuery) { |
|
54 | + $this->activeQuery->end(microtime(true)); |
|
55 | + $this->queries[(string)$this->index] = $this->activeQuery; |
|
56 | + $this->index++; |
|
57 | + $this->activeQuery = null; |
|
58 | + } |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @inheritdoc |
|
63 | - */ |
|
64 | - public function getQueries() { |
|
65 | - return $this->queries->getData(); |
|
66 | - } |
|
61 | + /** |
|
62 | + * @inheritdoc |
|
63 | + */ |
|
64 | + public function getQueries() { |
|
65 | + return $this->queries->getData(); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @inheritdoc |
|
70 | - */ |
|
71 | - public function activate() { |
|
72 | - $this->activated = true; |
|
73 | - } |
|
68 | + /** |
|
69 | + * @inheritdoc |
|
70 | + */ |
|
71 | + public function activate() { |
|
72 | + $this->activated = true; |
|
73 | + } |
|
74 | 74 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | return $query->getLastInsertId(); |
81 | 81 | } |
82 | 82 | |
83 | - $message = 'Internal failure, Could not add trusted server: ' . $url; |
|
83 | + $message = 'Internal failure, Could not add trusted server: '.$url; |
|
84 | 84 | $message_t = $this->IL10N->t('Could not add server'); |
85 | 85 | throw new HintException($message, $message_t); |
86 | 86 | return -1; |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | $qResult->closeCursor(); |
115 | 115 | |
116 | 116 | if (empty($result)) { |
117 | - throw new \Exception('No Server found with ID: ' . $id); |
|
117 | + throw new \Exception('No Server found with ID: '.$id); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | return $result[0]; |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | $statement->closeCursor(); |
184 | 184 | |
185 | 185 | if (!isset($result['token'])) { |
186 | - throw new \Exception('No token found for: ' . $url); |
|
186 | + throw new \Exception('No token found for: '.$url); |
|
187 | 187 | } |
188 | 188 | |
189 | 189 | return $result['token']; |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | $statement = $query->executeQuery(); |
217 | 217 | $result = $statement->fetch(); |
218 | 218 | $statement->closeCursor(); |
219 | - return (string)$result['shared_secret']; |
|
219 | + return (string) $result['shared_secret']; |
|
220 | 220 | } |
221 | 221 | |
222 | 222 | /** |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | $statement = $query->executeQuery(); |
248 | 248 | $result = $statement->fetch(); |
249 | 249 | $statement->closeCursor(); |
250 | - return (int)$result['status']; |
|
250 | + return (int) $result['status']; |
|
251 | 251 | } |
252 | 252 | |
253 | 253 | /** |
@@ -25,244 +25,244 @@ |
||
25 | 25 | * @package OCA\Federation |
26 | 26 | */ |
27 | 27 | class DbHandler { |
28 | - private string $dbTable = 'trusted_servers'; |
|
29 | - |
|
30 | - public function __construct( |
|
31 | - private IDBConnection $connection, |
|
32 | - private IL10N $IL10N, |
|
33 | - ) { |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Add server to the list of trusted servers |
|
38 | - * |
|
39 | - * @throws HintException |
|
40 | - */ |
|
41 | - public function addServer(string $url): int { |
|
42 | - $hash = $this->hash($url); |
|
43 | - $url = rtrim($url, '/'); |
|
44 | - $query = $this->connection->getQueryBuilder(); |
|
45 | - $query->insert($this->dbTable) |
|
46 | - ->values([ |
|
47 | - 'url' => $query->createParameter('url'), |
|
48 | - 'url_hash' => $query->createParameter('url_hash'), |
|
49 | - ]) |
|
50 | - ->setParameter('url', $url) |
|
51 | - ->setParameter('url_hash', $hash); |
|
52 | - |
|
53 | - $result = $query->executeStatement(); |
|
54 | - |
|
55 | - if ($result) { |
|
56 | - return $query->getLastInsertId(); |
|
57 | - } |
|
58 | - |
|
59 | - $message = 'Internal failure, Could not add trusted server: ' . $url; |
|
60 | - $message_t = $this->IL10N->t('Could not add server'); |
|
61 | - throw new HintException($message, $message_t); |
|
62 | - return -1; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Remove server from the list of trusted servers |
|
67 | - */ |
|
68 | - public function removeServer(int $id): void { |
|
69 | - $query = $this->connection->getQueryBuilder(); |
|
70 | - $query->delete($this->dbTable) |
|
71 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
72 | - ->setParameter('id', $id); |
|
73 | - $query->executeStatement(); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Get trusted server with given ID |
|
78 | - * |
|
79 | - * @return array{id: int, url: string, url_hash: string, token: ?string, shared_secret: ?string, status: int, sync_token: ?string} |
|
80 | - * @throws \Exception |
|
81 | - */ |
|
82 | - public function getServerById(int $id): array { |
|
83 | - $query = $this->connection->getQueryBuilder(); |
|
84 | - $query->select('*')->from($this->dbTable) |
|
85 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
86 | - ->setParameter('id', $id, IQueryBuilder::PARAM_INT); |
|
87 | - |
|
88 | - $qResult = $query->executeQuery(); |
|
89 | - $result = $qResult->fetchAll(); |
|
90 | - $qResult->closeCursor(); |
|
91 | - |
|
92 | - if (empty($result)) { |
|
93 | - throw new \Exception('No Server found with ID: ' . $id); |
|
94 | - } |
|
95 | - |
|
96 | - return $result[0]; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Get all trusted servers |
|
101 | - * |
|
102 | - * @return list<array{id: int, url: string, url_hash: string, shared_secret: ?string, status: int, sync_token: ?string}> |
|
103 | - * @throws DBException |
|
104 | - */ |
|
105 | - public function getAllServer(): array { |
|
106 | - $query = $this->connection->getQueryBuilder(); |
|
107 | - $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token']) |
|
108 | - ->from($this->dbTable); |
|
109 | - $statement = $query->executeQuery(); |
|
110 | - $result = $statement->fetchAll(); |
|
111 | - $statement->closeCursor(); |
|
112 | - return $result; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Check if server already exists in the database table |
|
117 | - */ |
|
118 | - public function serverExists(string $url): bool { |
|
119 | - $hash = $this->hash($url); |
|
120 | - $query = $this->connection->getQueryBuilder(); |
|
121 | - $query->select('url') |
|
122 | - ->from($this->dbTable) |
|
123 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
124 | - ->setParameter('url_hash', $hash); |
|
125 | - $statement = $query->executeQuery(); |
|
126 | - $result = $statement->fetchAll(); |
|
127 | - $statement->closeCursor(); |
|
128 | - |
|
129 | - return !empty($result); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Write token to database. Token is used to exchange the secret |
|
134 | - */ |
|
135 | - public function addToken(string $url, string $token): void { |
|
136 | - $hash = $this->hash($url); |
|
137 | - $query = $this->connection->getQueryBuilder(); |
|
138 | - $query->update($this->dbTable) |
|
139 | - ->set('token', $query->createParameter('token')) |
|
140 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
141 | - ->setParameter('url_hash', $hash) |
|
142 | - ->setParameter('token', $token); |
|
143 | - $query->executeStatement(); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Get token stored in database |
|
148 | - * @throws \Exception |
|
149 | - */ |
|
150 | - public function getToken(string $url): string { |
|
151 | - $hash = $this->hash($url); |
|
152 | - $query = $this->connection->getQueryBuilder(); |
|
153 | - $query->select('token')->from($this->dbTable) |
|
154 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
155 | - ->setParameter('url_hash', $hash); |
|
156 | - |
|
157 | - $statement = $query->executeQuery(); |
|
158 | - $result = $statement->fetch(); |
|
159 | - $statement->closeCursor(); |
|
160 | - |
|
161 | - if (!isset($result['token'])) { |
|
162 | - throw new \Exception('No token found for: ' . $url); |
|
163 | - } |
|
164 | - |
|
165 | - return $result['token']; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Add shared Secret to database |
|
170 | - */ |
|
171 | - public function addSharedSecret(string $url, string $sharedSecret): void { |
|
172 | - $hash = $this->hash($url); |
|
173 | - $query = $this->connection->getQueryBuilder(); |
|
174 | - $query->update($this->dbTable) |
|
175 | - ->set('shared_secret', $query->createParameter('sharedSecret')) |
|
176 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
177 | - ->setParameter('url_hash', $hash) |
|
178 | - ->setParameter('sharedSecret', $sharedSecret); |
|
179 | - $query->executeStatement(); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Get shared secret from database |
|
184 | - */ |
|
185 | - public function getSharedSecret(string $url): string { |
|
186 | - $hash = $this->hash($url); |
|
187 | - $query = $this->connection->getQueryBuilder(); |
|
188 | - $query->select('shared_secret')->from($this->dbTable) |
|
189 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
190 | - ->setParameter('url_hash', $hash); |
|
191 | - |
|
192 | - $statement = $query->executeQuery(); |
|
193 | - $result = $statement->fetch(); |
|
194 | - $statement->closeCursor(); |
|
195 | - return (string)$result['shared_secret']; |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Set server status |
|
200 | - */ |
|
201 | - public function setServerStatus(string $url, int $status, ?string $token = null): void { |
|
202 | - $hash = $this->hash($url); |
|
203 | - $query = $this->connection->getQueryBuilder(); |
|
204 | - $query->update($this->dbTable) |
|
205 | - ->set('status', $query->createNamedParameter($status)) |
|
206 | - ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash))); |
|
207 | - if (!is_null($token)) { |
|
208 | - $query->set('sync_token', $query->createNamedParameter($token)); |
|
209 | - } |
|
210 | - $query->executeStatement(); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Get server status |
|
215 | - */ |
|
216 | - public function getServerStatus(string $url): int { |
|
217 | - $hash = $this->hash($url); |
|
218 | - $query = $this->connection->getQueryBuilder(); |
|
219 | - $query->select('status')->from($this->dbTable) |
|
220 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
221 | - ->setParameter('url_hash', $hash); |
|
222 | - |
|
223 | - $statement = $query->executeQuery(); |
|
224 | - $result = $statement->fetch(); |
|
225 | - $statement->closeCursor(); |
|
226 | - return (int)$result['status']; |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Create hash from URL |
|
231 | - */ |
|
232 | - protected function hash(string $url): string { |
|
233 | - $normalized = $this->normalizeUrl($url); |
|
234 | - return sha1($normalized); |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Normalize URL, used to create the sha1 hash |
|
239 | - */ |
|
240 | - protected function normalizeUrl(string $url): string { |
|
241 | - $normalized = $url; |
|
242 | - |
|
243 | - if (strpos($url, 'https://') === 0) { |
|
244 | - $normalized = substr($url, strlen('https://')); |
|
245 | - } elseif (strpos($url, 'http://') === 0) { |
|
246 | - $normalized = substr($url, strlen('http://')); |
|
247 | - } |
|
248 | - |
|
249 | - $normalized = Filesystem::normalizePath($normalized); |
|
250 | - $normalized = trim($normalized, '/'); |
|
251 | - |
|
252 | - return $normalized; |
|
253 | - } |
|
254 | - |
|
255 | - public function auth(string $username, string $password): bool { |
|
256 | - if ($username !== 'system') { |
|
257 | - return false; |
|
258 | - } |
|
259 | - $query = $this->connection->getQueryBuilder(); |
|
260 | - $query->select('url')->from($this->dbTable) |
|
261 | - ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password))); |
|
262 | - |
|
263 | - $statement = $query->executeQuery(); |
|
264 | - $result = $statement->fetch(); |
|
265 | - $statement->closeCursor(); |
|
266 | - return !empty($result); |
|
267 | - } |
|
28 | + private string $dbTable = 'trusted_servers'; |
|
29 | + |
|
30 | + public function __construct( |
|
31 | + private IDBConnection $connection, |
|
32 | + private IL10N $IL10N, |
|
33 | + ) { |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Add server to the list of trusted servers |
|
38 | + * |
|
39 | + * @throws HintException |
|
40 | + */ |
|
41 | + public function addServer(string $url): int { |
|
42 | + $hash = $this->hash($url); |
|
43 | + $url = rtrim($url, '/'); |
|
44 | + $query = $this->connection->getQueryBuilder(); |
|
45 | + $query->insert($this->dbTable) |
|
46 | + ->values([ |
|
47 | + 'url' => $query->createParameter('url'), |
|
48 | + 'url_hash' => $query->createParameter('url_hash'), |
|
49 | + ]) |
|
50 | + ->setParameter('url', $url) |
|
51 | + ->setParameter('url_hash', $hash); |
|
52 | + |
|
53 | + $result = $query->executeStatement(); |
|
54 | + |
|
55 | + if ($result) { |
|
56 | + return $query->getLastInsertId(); |
|
57 | + } |
|
58 | + |
|
59 | + $message = 'Internal failure, Could not add trusted server: ' . $url; |
|
60 | + $message_t = $this->IL10N->t('Could not add server'); |
|
61 | + throw new HintException($message, $message_t); |
|
62 | + return -1; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Remove server from the list of trusted servers |
|
67 | + */ |
|
68 | + public function removeServer(int $id): void { |
|
69 | + $query = $this->connection->getQueryBuilder(); |
|
70 | + $query->delete($this->dbTable) |
|
71 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
72 | + ->setParameter('id', $id); |
|
73 | + $query->executeStatement(); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Get trusted server with given ID |
|
78 | + * |
|
79 | + * @return array{id: int, url: string, url_hash: string, token: ?string, shared_secret: ?string, status: int, sync_token: ?string} |
|
80 | + * @throws \Exception |
|
81 | + */ |
|
82 | + public function getServerById(int $id): array { |
|
83 | + $query = $this->connection->getQueryBuilder(); |
|
84 | + $query->select('*')->from($this->dbTable) |
|
85 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
86 | + ->setParameter('id', $id, IQueryBuilder::PARAM_INT); |
|
87 | + |
|
88 | + $qResult = $query->executeQuery(); |
|
89 | + $result = $qResult->fetchAll(); |
|
90 | + $qResult->closeCursor(); |
|
91 | + |
|
92 | + if (empty($result)) { |
|
93 | + throw new \Exception('No Server found with ID: ' . $id); |
|
94 | + } |
|
95 | + |
|
96 | + return $result[0]; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Get all trusted servers |
|
101 | + * |
|
102 | + * @return list<array{id: int, url: string, url_hash: string, shared_secret: ?string, status: int, sync_token: ?string}> |
|
103 | + * @throws DBException |
|
104 | + */ |
|
105 | + public function getAllServer(): array { |
|
106 | + $query = $this->connection->getQueryBuilder(); |
|
107 | + $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token']) |
|
108 | + ->from($this->dbTable); |
|
109 | + $statement = $query->executeQuery(); |
|
110 | + $result = $statement->fetchAll(); |
|
111 | + $statement->closeCursor(); |
|
112 | + return $result; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Check if server already exists in the database table |
|
117 | + */ |
|
118 | + public function serverExists(string $url): bool { |
|
119 | + $hash = $this->hash($url); |
|
120 | + $query = $this->connection->getQueryBuilder(); |
|
121 | + $query->select('url') |
|
122 | + ->from($this->dbTable) |
|
123 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
124 | + ->setParameter('url_hash', $hash); |
|
125 | + $statement = $query->executeQuery(); |
|
126 | + $result = $statement->fetchAll(); |
|
127 | + $statement->closeCursor(); |
|
128 | + |
|
129 | + return !empty($result); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Write token to database. Token is used to exchange the secret |
|
134 | + */ |
|
135 | + public function addToken(string $url, string $token): void { |
|
136 | + $hash = $this->hash($url); |
|
137 | + $query = $this->connection->getQueryBuilder(); |
|
138 | + $query->update($this->dbTable) |
|
139 | + ->set('token', $query->createParameter('token')) |
|
140 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
141 | + ->setParameter('url_hash', $hash) |
|
142 | + ->setParameter('token', $token); |
|
143 | + $query->executeStatement(); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Get token stored in database |
|
148 | + * @throws \Exception |
|
149 | + */ |
|
150 | + public function getToken(string $url): string { |
|
151 | + $hash = $this->hash($url); |
|
152 | + $query = $this->connection->getQueryBuilder(); |
|
153 | + $query->select('token')->from($this->dbTable) |
|
154 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
155 | + ->setParameter('url_hash', $hash); |
|
156 | + |
|
157 | + $statement = $query->executeQuery(); |
|
158 | + $result = $statement->fetch(); |
|
159 | + $statement->closeCursor(); |
|
160 | + |
|
161 | + if (!isset($result['token'])) { |
|
162 | + throw new \Exception('No token found for: ' . $url); |
|
163 | + } |
|
164 | + |
|
165 | + return $result['token']; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Add shared Secret to database |
|
170 | + */ |
|
171 | + public function addSharedSecret(string $url, string $sharedSecret): void { |
|
172 | + $hash = $this->hash($url); |
|
173 | + $query = $this->connection->getQueryBuilder(); |
|
174 | + $query->update($this->dbTable) |
|
175 | + ->set('shared_secret', $query->createParameter('sharedSecret')) |
|
176 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
177 | + ->setParameter('url_hash', $hash) |
|
178 | + ->setParameter('sharedSecret', $sharedSecret); |
|
179 | + $query->executeStatement(); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Get shared secret from database |
|
184 | + */ |
|
185 | + public function getSharedSecret(string $url): string { |
|
186 | + $hash = $this->hash($url); |
|
187 | + $query = $this->connection->getQueryBuilder(); |
|
188 | + $query->select('shared_secret')->from($this->dbTable) |
|
189 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
190 | + ->setParameter('url_hash', $hash); |
|
191 | + |
|
192 | + $statement = $query->executeQuery(); |
|
193 | + $result = $statement->fetch(); |
|
194 | + $statement->closeCursor(); |
|
195 | + return (string)$result['shared_secret']; |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Set server status |
|
200 | + */ |
|
201 | + public function setServerStatus(string $url, int $status, ?string $token = null): void { |
|
202 | + $hash = $this->hash($url); |
|
203 | + $query = $this->connection->getQueryBuilder(); |
|
204 | + $query->update($this->dbTable) |
|
205 | + ->set('status', $query->createNamedParameter($status)) |
|
206 | + ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash))); |
|
207 | + if (!is_null($token)) { |
|
208 | + $query->set('sync_token', $query->createNamedParameter($token)); |
|
209 | + } |
|
210 | + $query->executeStatement(); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Get server status |
|
215 | + */ |
|
216 | + public function getServerStatus(string $url): int { |
|
217 | + $hash = $this->hash($url); |
|
218 | + $query = $this->connection->getQueryBuilder(); |
|
219 | + $query->select('status')->from($this->dbTable) |
|
220 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
221 | + ->setParameter('url_hash', $hash); |
|
222 | + |
|
223 | + $statement = $query->executeQuery(); |
|
224 | + $result = $statement->fetch(); |
|
225 | + $statement->closeCursor(); |
|
226 | + return (int)$result['status']; |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Create hash from URL |
|
231 | + */ |
|
232 | + protected function hash(string $url): string { |
|
233 | + $normalized = $this->normalizeUrl($url); |
|
234 | + return sha1($normalized); |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Normalize URL, used to create the sha1 hash |
|
239 | + */ |
|
240 | + protected function normalizeUrl(string $url): string { |
|
241 | + $normalized = $url; |
|
242 | + |
|
243 | + if (strpos($url, 'https://') === 0) { |
|
244 | + $normalized = substr($url, strlen('https://')); |
|
245 | + } elseif (strpos($url, 'http://') === 0) { |
|
246 | + $normalized = substr($url, strlen('http://')); |
|
247 | + } |
|
248 | + |
|
249 | + $normalized = Filesystem::normalizePath($normalized); |
|
250 | + $normalized = trim($normalized, '/'); |
|
251 | + |
|
252 | + return $normalized; |
|
253 | + } |
|
254 | + |
|
255 | + public function auth(string $username, string $password): bool { |
|
256 | + if ($username !== 'system') { |
|
257 | + return false; |
|
258 | + } |
|
259 | + $query = $this->connection->getQueryBuilder(); |
|
260 | + $query->select('url')->from($this->dbTable) |
|
261 | + ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password))); |
|
262 | + |
|
263 | + $statement = $query->executeQuery(); |
|
264 | + $result = $statement->fetch(); |
|
265 | + $statement->closeCursor(); |
|
266 | + return !empty($result); |
|
267 | + } |
|
268 | 268 | } |
@@ -87,13 +87,13 @@ |
||
87 | 87 | |
88 | 88 | $shares = $qb->executeQuery(); |
89 | 89 | while ($share = $shares->fetch()) { |
90 | - if ((int)$share['share_type'] === IShare::TYPE_LINK) { |
|
90 | + if ((int) $share['share_type'] === IShare::TYPE_LINK) { |
|
91 | 91 | $id = 'ocinternal'; |
92 | - } elseif ((int)$share['share_type'] === IShare::TYPE_EMAIL) { |
|
92 | + } elseif ((int) $share['share_type'] === IShare::TYPE_EMAIL) { |
|
93 | 93 | $id = 'ocMailShare'; |
94 | 94 | } |
95 | 95 | |
96 | - $id .= ':' . $share['id']; |
|
96 | + $id .= ':'.$share['id']; |
|
97 | 97 | |
98 | 98 | try { |
99 | 99 | $share = $this->shareManager->getShareById($id); |
@@ -19,60 +19,60 @@ |
||
19 | 19 | */ |
20 | 20 | class ExpireSharesJob extends TimedJob { |
21 | 21 | |
22 | - public function __construct( |
|
23 | - ITimeFactory $time, |
|
24 | - private IManager $shareManager, |
|
25 | - private IDBConnection $db, |
|
26 | - ) { |
|
27 | - parent::__construct($time); |
|
22 | + public function __construct( |
|
23 | + ITimeFactory $time, |
|
24 | + private IManager $shareManager, |
|
25 | + private IDBConnection $db, |
|
26 | + ) { |
|
27 | + parent::__construct($time); |
|
28 | 28 | |
29 | - // Run once a day |
|
30 | - $this->setInterval(24 * 60 * 60); |
|
31 | - $this->setTimeSensitivity(self::TIME_INSENSITIVE); |
|
32 | - } |
|
29 | + // Run once a day |
|
30 | + $this->setInterval(24 * 60 * 60); |
|
31 | + $this->setTimeSensitivity(self::TIME_INSENSITIVE); |
|
32 | + } |
|
33 | 33 | |
34 | 34 | |
35 | - /** |
|
36 | - * Makes the background job do its work |
|
37 | - * |
|
38 | - * @param array $argument unused argument |
|
39 | - */ |
|
40 | - public function run($argument) { |
|
41 | - //Current time |
|
42 | - $now = new \DateTime(); |
|
43 | - $now = $now->format('Y-m-d H:i:s'); |
|
35 | + /** |
|
36 | + * Makes the background job do its work |
|
37 | + * |
|
38 | + * @param array $argument unused argument |
|
39 | + */ |
|
40 | + public function run($argument) { |
|
41 | + //Current time |
|
42 | + $now = new \DateTime(); |
|
43 | + $now = $now->format('Y-m-d H:i:s'); |
|
44 | 44 | |
45 | - /* |
|
45 | + /* |
|
46 | 46 | * Expire file link shares only (for now) |
47 | 47 | */ |
48 | - $qb = $this->db->getQueryBuilder(); |
|
49 | - $qb->select('id', 'share_type') |
|
50 | - ->from('share') |
|
51 | - ->where( |
|
52 | - $qb->expr()->andX( |
|
53 | - $qb->expr()->in('share_type', $qb->createNamedParameter([IShare::TYPE_LINK, IShare::TYPE_EMAIL], IQueryBuilder::PARAM_INT_ARRAY)), |
|
54 | - $qb->expr()->lte('expiration', $qb->expr()->literal($now)), |
|
55 | - $qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)) |
|
56 | - ) |
|
57 | - ); |
|
48 | + $qb = $this->db->getQueryBuilder(); |
|
49 | + $qb->select('id', 'share_type') |
|
50 | + ->from('share') |
|
51 | + ->where( |
|
52 | + $qb->expr()->andX( |
|
53 | + $qb->expr()->in('share_type', $qb->createNamedParameter([IShare::TYPE_LINK, IShare::TYPE_EMAIL], IQueryBuilder::PARAM_INT_ARRAY)), |
|
54 | + $qb->expr()->lte('expiration', $qb->expr()->literal($now)), |
|
55 | + $qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)) |
|
56 | + ) |
|
57 | + ); |
|
58 | 58 | |
59 | - $shares = $qb->executeQuery(); |
|
60 | - while ($share = $shares->fetch()) { |
|
61 | - if ((int)$share['share_type'] === IShare::TYPE_LINK) { |
|
62 | - $id = 'ocinternal'; |
|
63 | - } elseif ((int)$share['share_type'] === IShare::TYPE_EMAIL) { |
|
64 | - $id = 'ocMailShare'; |
|
65 | - } |
|
59 | + $shares = $qb->executeQuery(); |
|
60 | + while ($share = $shares->fetch()) { |
|
61 | + if ((int)$share['share_type'] === IShare::TYPE_LINK) { |
|
62 | + $id = 'ocinternal'; |
|
63 | + } elseif ((int)$share['share_type'] === IShare::TYPE_EMAIL) { |
|
64 | + $id = 'ocMailShare'; |
|
65 | + } |
|
66 | 66 | |
67 | - $id .= ':' . $share['id']; |
|
67 | + $id .= ':' . $share['id']; |
|
68 | 68 | |
69 | - try { |
|
70 | - $share = $this->shareManager->getShareById($id); |
|
71 | - $this->shareManager->deleteShare($share); |
|
72 | - } catch (ShareNotFound $e) { |
|
73 | - // Normally the share gets automatically expired on fetching it |
|
74 | - } |
|
75 | - } |
|
76 | - $shares->closeCursor(); |
|
77 | - } |
|
69 | + try { |
|
70 | + $share = $this->shareManager->getShareById($id); |
|
71 | + $this->shareManager->deleteShare($share); |
|
72 | + } catch (ShareNotFound $e) { |
|
73 | + // Normally the share gets automatically expired on fetching it |
|
74 | + } |
|
75 | + } |
|
76 | + $shares->closeCursor(); |
|
77 | + } |
|
78 | 78 | } |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | <?php if (isset($_['dirToken'])):?> |
16 | 16 | <input type="hidden" id="publicUploadRequestToken" name="requesttoken" value="<?php p($_['requesttoken']) ?>" /> |
17 | 17 | <input type="hidden" id="dirToken" name="dirToken" value="<?php p($_['dirToken']) ?>" /> |
18 | - <?php endif;?> |
|
18 | + <?php endif; ?> |
|
19 | 19 | <input type="hidden" class="max_human_file_size" |
20 | 20 | value="(max <?php isset($_['uploadMaxHumanFilesize']) ? p($_['uploadMaxHumanFilesize']) : ''; ?>)"> |
21 | 21 | </div> |
@@ -75,6 +75,6 @@ discard block |
||
75 | 75 | </div> |
76 | 76 | <div id="uploadsize-message" title="<?php p($l->t('Upload too large'))?>"> |
77 | 77 | <p> |
78 | - <?php p($l->t('The files you are trying to upload exceed the maximum size for file uploads on this server.'));?> |
|
78 | + <?php p($l->t('The files you are trying to upload exceed the maximum size for file uploads on this server.')); ?> |
|
79 | 79 | </p> |
80 | 80 | </div> |
@@ -5,5 +5,5 @@ |
||
5 | 5 | ?> |
6 | 6 | <div class="guest-box"> |
7 | 7 | <div class="icon icon-checkmark"></div> |
8 | - <p class="message"><?php p($l->t('Your attendance was updated successfully.'));?></p> |
|
8 | + <p class="message"><?php p($l->t('Your attendance was updated successfully.')); ?></p> |
|
9 | 9 | </div> |
@@ -6,8 +6,8 @@ |
||
6 | 6 | ?> |
7 | 7 | <div class="guest-box"> |
8 | 8 | <div class="notecard error"> |
9 | - <p><?php p($l->t('There was an error updating your attendance status.'));?></p> |
|
10 | - <p><?php p($l->t('Please contact the organizer directly.'));?></p> |
|
9 | + <p><?php p($l->t('There was an error updating your attendance status.')); ?></p> |
|
10 | + <p><?php p($l->t('Please contact the organizer directly.')); ?></p> |
|
11 | 11 | <?php if (isset($_['organizer'])): ?> |
12 | 12 | <p><a href="<?php p($_['organizer']) ?>"><?php p(substr($_['organizer'], 7)) ?></a></p> |
13 | 13 | <?php endif; ?> |
@@ -17,42 +17,42 @@ |
||
17 | 17 | */ |
18 | 18 | class Version1024Date20211221144219 extends SimpleMigrationStep { |
19 | 19 | |
20 | - /** |
|
21 | - * @param IOutput $output |
|
22 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
23 | - * @param array $options |
|
24 | - */ |
|
25 | - public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * @param IOutput $output |
|
30 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
31 | - * @param array $options |
|
32 | - * @return null|ISchemaWrapper |
|
33 | - * @throws SchemaException |
|
34 | - */ |
|
35 | - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
36 | - /** @var ISchemaWrapper $schema */ |
|
37 | - $schema = $schemaClosure(); |
|
38 | - $propertiesTable = $schema->getTable('properties'); |
|
39 | - |
|
40 | - if ($propertiesTable->hasColumn('valuetype')) { |
|
41 | - return null; |
|
42 | - } |
|
43 | - $propertiesTable->addColumn('valuetype', Types::SMALLINT, [ |
|
44 | - 'notnull' => false, |
|
45 | - 'default' => CustomPropertiesBackend::PROPERTY_TYPE_STRING |
|
46 | - ]); |
|
47 | - |
|
48 | - return $schema; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @param IOutput $output |
|
53 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
54 | - * @param array $options |
|
55 | - */ |
|
56 | - public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { |
|
57 | - } |
|
20 | + /** |
|
21 | + * @param IOutput $output |
|
22 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
23 | + * @param array $options |
|
24 | + */ |
|
25 | + public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * @param IOutput $output |
|
30 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
31 | + * @param array $options |
|
32 | + * @return null|ISchemaWrapper |
|
33 | + * @throws SchemaException |
|
34 | + */ |
|
35 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
36 | + /** @var ISchemaWrapper $schema */ |
|
37 | + $schema = $schemaClosure(); |
|
38 | + $propertiesTable = $schema->getTable('properties'); |
|
39 | + |
|
40 | + if ($propertiesTable->hasColumn('valuetype')) { |
|
41 | + return null; |
|
42 | + } |
|
43 | + $propertiesTable->addColumn('valuetype', Types::SMALLINT, [ |
|
44 | + 'notnull' => false, |
|
45 | + 'default' => CustomPropertiesBackend::PROPERTY_TYPE_STRING |
|
46 | + ]); |
|
47 | + |
|
48 | + return $schema; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @param IOutput $output |
|
53 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
54 | + * @param array $options |
|
55 | + */ |
|
56 | + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { |
|
57 | + } |
|
58 | 58 | } |
@@ -28,63 +28,63 @@ |
||
28 | 28 | |
29 | 29 | class PublicCalendar extends Calendar { |
30 | 30 | |
31 | - /** |
|
32 | - * @param string $name |
|
33 | - * @throws NotFound |
|
34 | - * @return PublicCalendarObject |
|
35 | - */ |
|
36 | - public function getChild($name) { |
|
37 | - $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name); |
|
31 | + /** |
|
32 | + * @param string $name |
|
33 | + * @throws NotFound |
|
34 | + * @return PublicCalendarObject |
|
35 | + */ |
|
36 | + public function getChild($name) { |
|
37 | + $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name); |
|
38 | 38 | |
39 | - if (!$obj) { |
|
40 | - throw new NotFound('Calendar object not found'); |
|
41 | - } |
|
42 | - if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) { |
|
43 | - throw new NotFound('Calendar object not found'); |
|
44 | - } |
|
45 | - $obj['acl'] = $this->getChildACL(); |
|
39 | + if (!$obj) { |
|
40 | + throw new NotFound('Calendar object not found'); |
|
41 | + } |
|
42 | + if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) { |
|
43 | + throw new NotFound('Calendar object not found'); |
|
44 | + } |
|
45 | + $obj['acl'] = $this->getChildACL(); |
|
46 | 46 | |
47 | - return new PublicCalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj); |
|
48 | - } |
|
47 | + return new PublicCalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @return PublicCalendarObject[] |
|
52 | - */ |
|
53 | - public function getChildren() { |
|
54 | - $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']); |
|
55 | - $children = []; |
|
56 | - foreach ($objs as $obj) { |
|
57 | - if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) { |
|
58 | - continue; |
|
59 | - } |
|
60 | - $obj['acl'] = $this->getChildACL(); |
|
61 | - $children[] = new PublicCalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj); |
|
62 | - } |
|
63 | - return $children; |
|
64 | - } |
|
50 | + /** |
|
51 | + * @return PublicCalendarObject[] |
|
52 | + */ |
|
53 | + public function getChildren() { |
|
54 | + $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']); |
|
55 | + $children = []; |
|
56 | + foreach ($objs as $obj) { |
|
57 | + if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) { |
|
58 | + continue; |
|
59 | + } |
|
60 | + $obj['acl'] = $this->getChildACL(); |
|
61 | + $children[] = new PublicCalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj); |
|
62 | + } |
|
63 | + return $children; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @param string[] $paths |
|
68 | - * @return PublicCalendarObject[] |
|
69 | - */ |
|
70 | - public function getMultipleChildren(array $paths) { |
|
71 | - $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths); |
|
72 | - $children = []; |
|
73 | - foreach ($objs as $obj) { |
|
74 | - if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) { |
|
75 | - continue; |
|
76 | - } |
|
77 | - $obj['acl'] = $this->getChildACL(); |
|
78 | - $children[] = new PublicCalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj); |
|
79 | - } |
|
80 | - return $children; |
|
81 | - } |
|
66 | + /** |
|
67 | + * @param string[] $paths |
|
68 | + * @return PublicCalendarObject[] |
|
69 | + */ |
|
70 | + public function getMultipleChildren(array $paths) { |
|
71 | + $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths); |
|
72 | + $children = []; |
|
73 | + foreach ($objs as $obj) { |
|
74 | + if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) { |
|
75 | + continue; |
|
76 | + } |
|
77 | + $obj['acl'] = $this->getChildACL(); |
|
78 | + $children[] = new PublicCalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj); |
|
79 | + } |
|
80 | + return $children; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * public calendars are always shared |
|
85 | - * @return bool |
|
86 | - */ |
|
87 | - public function isShared() { |
|
88 | - return true; |
|
89 | - } |
|
83 | + /** |
|
84 | + * public calendars are always shared |
|
85 | + * @return bool |
|
86 | + */ |
|
87 | + public function isShared() { |
|
88 | + return true; |
|
89 | + } |
|
90 | 90 | } |