Completed
Pull Request — master (#5585)
by Tobia
15:55
created
lib/private/Files/Config/UserMountCache.php 4 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -206,7 +206,7 @@
 block discarded – undo
206 206
 	}
207 207
 
208 208
 	/**
209
-	 * @param $fileId
209
+	 * @param integer $fileId
210 210
 	 * @return array
211 211
 	 * @throws \OCP\Files\NotFoundException
212 212
 	 */
Please login to merge, or discard this patch.
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -24,7 +24,6 @@
 block discarded – undo
24 24
 
25 25
 namespace OC\Files\Config;
26 26
 
27
-use OC\DB\QueryBuilder\Literal;
28 27
 use OCA\Files_Sharing\SharedMount;
29 28
 use OCP\DB\QueryBuilder\IQueryBuilder;
30 29
 use OCP\Files\Config\ICachedMountInfo;
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -87,11 +87,11 @@  discard block
 block discarded – undo
87 87
 
88 88
 	public function registerMounts(IUser $user, array $mounts) {
89 89
 		// filter out non-proper storages coming from unit tests
90
-		$mounts = array_filter($mounts, function (IMountPoint $mount) {
90
+		$mounts = array_filter($mounts, function(IMountPoint $mount) {
91 91
 			return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache();
92 92
 		});
93 93
 		/** @var ICachedMountInfo[] $newMounts */
94
-		$newMounts = array_map(function (IMountPoint $mount) use ($user) {
94
+		$newMounts = array_map(function(IMountPoint $mount) use ($user) {
95 95
 			// filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
96 96
 			if ($mount->getStorageRootId() === -1) {
97 97
 				return null;
@@ -102,7 +102,7 @@  discard block
 block discarded – undo
102 102
 		$newMounts = array_values(array_filter($newMounts));
103 103
 
104 104
 		$cachedMounts = $this->getMountsForUser($user);
105
-		$mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) {
105
+		$mountDiff = function(ICachedMountInfo $mount1, ICachedMountInfo $mount2) {
106 106
 			// since we are only looking for mounts for a specific user comparing on root id is enough
107 107
 			return $mount1->getRootId() - $mount2->getRootId();
108 108
 		};
@@ -163,7 +163,7 @@  discard block
 block discarded – undo
163 163
 			], ['root_id', 'user_id']);
164 164
 		} else {
165 165
 			// in some cases this is legitimate, like orphaned shares
166
-			$this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint());
166
+			$this->logger->debug('Could not get storage info for mount at '.$mount->getMountPoint());
167 167
 		}
168 168
 	}
169 169
 
@@ -194,7 +194,7 @@  discard block
 block discarded – undo
194 194
 		if (is_null($user)) {
195 195
 			return null;
196 196
 		}
197
-		return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id'], isset($row['path']) ? $row['path'] : '');
197
+		return new CachedMountInfo($user, (int) $row['storage_id'], (int) $row['root_id'], $row['mount_point'], $row['mount_id'], isset($row['path']) ? $row['path'] : '');
198 198
 	}
199 199
 
200 200
 	/**
@@ -268,12 +268,12 @@  discard block
 block discarded – undo
268 268
 			$row = $query->execute()->fetch();
269 269
 			if (is_array($row)) {
270 270
 				$this->cacheInfoCache[$fileId] = [
271
-					(int)$row['storage'],
271
+					(int) $row['storage'],
272 272
 					$row['path'],
273
-					(int)$row['mimetype']
273
+					(int) $row['mimetype']
274 274
 				];
275 275
 			} else {
276
-				throw new NotFoundException('File with id "' . $fileId . '" not found');
276
+				throw new NotFoundException('File with id "'.$fileId.'" not found');
277 277
 			}
278 278
 		}
279 279
 		return $this->cacheInfoCache[$fileId];
@@ -294,13 +294,13 @@  discard block
 block discarded – undo
294 294
 		$mountsForStorage = $this->getMountsForStorageId($storageId, $user);
295 295
 
296 296
 		// filter mounts that are from the same storage but a different directory
297
-		return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) {
297
+		return array_filter($mountsForStorage, function(ICachedMountInfo $mount) use ($internalPath, $fileId) {
298 298
 			if ($fileId === $mount->getRootId()) {
299 299
 				return true;
300 300
 			}
301 301
 			$internalMountPath = $mount->getRootInternalPath();
302 302
 
303
-			return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/';
303
+			return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath.'/';
304 304
 		});
305 305
 	}
306 306
 
@@ -344,7 +344,7 @@  discard block
 block discarded – undo
344 344
 			$slash
345 345
 		);
346 346
 
347
-		$userIds = array_map(function (IUser $user) {
347
+		$userIds = array_map(function(IUser $user) {
348 348
 			return $user->getUID();
349 349
 		}, $users);
350 350
 
Please login to merge, or discard this patch.
Indentation   +330 added lines, -330 removed lines patch added patch discarded remove patch
@@ -42,334 +42,334 @@
 block discarded – undo
42 42
  * Cache mounts points per user in the cache so we can easilly look them up
43 43
  */
44 44
 class UserMountCache implements IUserMountCache {
45
-	/**
46
-	 * @var IDBConnection
47
-	 */
48
-	private $connection;
49
-
50
-	/**
51
-	 * @var IUserManager
52
-	 */
53
-	private $userManager;
54
-
55
-	/**
56
-	 * Cached mount info.
57
-	 * Map of $userId to ICachedMountInfo.
58
-	 *
59
-	 * @var ICache
60
-	 **/
61
-	private $mountsForUsers;
62
-
63
-	/**
64
-	 * @var ILogger
65
-	 */
66
-	private $logger;
67
-
68
-	/**
69
-	 * @var ICache
70
-	 */
71
-	private $cacheInfoCache;
72
-
73
-	/**
74
-	 * UserMountCache constructor.
75
-	 *
76
-	 * @param IDBConnection $connection
77
-	 * @param IUserManager $userManager
78
-	 * @param ILogger $logger
79
-	 */
80
-	public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) {
81
-		$this->connection = $connection;
82
-		$this->userManager = $userManager;
83
-		$this->logger = $logger;
84
-		$this->cacheInfoCache = new CappedMemoryCache();
85
-		$this->mountsForUsers = new CappedMemoryCache();
86
-	}
87
-
88
-	public function registerMounts(IUser $user, array $mounts) {
89
-		// filter out non-proper storages coming from unit tests
90
-		$mounts = array_filter($mounts, function (IMountPoint $mount) {
91
-			return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache();
92
-		});
93
-		/** @var ICachedMountInfo[] $newMounts */
94
-		$newMounts = array_map(function (IMountPoint $mount) use ($user) {
95
-			// filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
96
-			if ($mount->getStorageRootId() === -1) {
97
-				return null;
98
-			} else {
99
-				return new LazyStorageMountInfo($user, $mount);
100
-			}
101
-		}, $mounts);
102
-		$newMounts = array_values(array_filter($newMounts));
103
-
104
-		$cachedMounts = $this->getMountsForUser($user);
105
-		$mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) {
106
-			// since we are only looking for mounts for a specific user comparing on root id is enough
107
-			return $mount1->getRootId() - $mount2->getRootId();
108
-		};
109
-
110
-		/** @var ICachedMountInfo[] $addedMounts */
111
-		$addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff);
112
-		/** @var ICachedMountInfo[] $removedMounts */
113
-		$removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff);
114
-
115
-		$changedMounts = $this->findChangedMounts($newMounts, $cachedMounts);
116
-
117
-		foreach ($addedMounts as $mount) {
118
-			$this->addToCache($mount);
119
-			$this->mountsForUsers[$user->getUID()][] = $mount;
120
-		}
121
-		foreach ($removedMounts as $mount) {
122
-			$this->removeFromCache($mount);
123
-			$index = array_search($mount, $this->mountsForUsers[$user->getUID()]);
124
-			unset($this->mountsForUsers[$user->getUID()][$index]);
125
-		}
126
-		foreach ($changedMounts as $mount) {
127
-			$this->updateCachedMount($mount);
128
-		}
129
-	}
130
-
131
-	/**
132
-	 * @param ICachedMountInfo[] $newMounts
133
-	 * @param ICachedMountInfo[] $cachedMounts
134
-	 * @return ICachedMountInfo[]
135
-	 */
136
-	private function findChangedMounts(array $newMounts, array $cachedMounts) {
137
-		$changed = [];
138
-		foreach ($newMounts as $newMount) {
139
-			foreach ($cachedMounts as $cachedMount) {
140
-				if (
141
-					$newMount->getRootId() === $cachedMount->getRootId() &&
142
-					(
143
-						$newMount->getMountPoint() !== $cachedMount->getMountPoint() ||
144
-						$newMount->getStorageId() !== $cachedMount->getStorageId() ||
145
-						$newMount->getMountId() !== $cachedMount->getMountId()
146
-					)
147
-				) {
148
-					$changed[] = $newMount;
149
-				}
150
-			}
151
-		}
152
-		return $changed;
153
-	}
154
-
155
-	private function addToCache(ICachedMountInfo $mount) {
156
-		if ($mount->getStorageId() !== -1) {
157
-			$this->connection->insertIfNotExist('*PREFIX*mounts', [
158
-				'storage_id' => $mount->getStorageId(),
159
-				'root_id' => $mount->getRootId(),
160
-				'user_id' => $mount->getUser()->getUID(),
161
-				'mount_point' => $mount->getMountPoint(),
162
-				'mount_id' => $mount->getMountId()
163
-			], ['root_id', 'user_id']);
164
-		} else {
165
-			// in some cases this is legitimate, like orphaned shares
166
-			$this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint());
167
-		}
168
-	}
169
-
170
-	private function updateCachedMount(ICachedMountInfo $mount) {
171
-		$builder = $this->connection->getQueryBuilder();
172
-
173
-		$query = $builder->update('mounts')
174
-			->set('storage_id', $builder->createNamedParameter($mount->getStorageId()))
175
-			->set('mount_point', $builder->createNamedParameter($mount->getMountPoint()))
176
-			->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT))
177
-			->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
178
-			->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
179
-
180
-		$query->execute();
181
-	}
182
-
183
-	private function removeFromCache(ICachedMountInfo $mount) {
184
-		$builder = $this->connection->getQueryBuilder();
185
-
186
-		$query = $builder->delete('mounts')
187
-			->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
188
-			->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
189
-		$query->execute();
190
-	}
191
-
192
-	private function dbRowToMountInfo(array $row) {
193
-		$user = $this->userManager->get($row['user_id']);
194
-		if (is_null($user)) {
195
-			return null;
196
-		}
197
-		return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id'], isset($row['path']) ? $row['path'] : '');
198
-	}
199
-
200
-	/**
201
-	 * @param IUser $user
202
-	 * @return ICachedMountInfo[]
203
-	 */
204
-	public function getMountsForUser(IUser $user) {
205
-		if (!isset($this->mountsForUsers[$user->getUID()])) {
206
-			$builder = $this->connection->getQueryBuilder();
207
-			$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
208
-				->from('mounts', 'm')
209
-				->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
210
-				->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID())));
211
-
212
-			$rows = $query->execute()->fetchAll();
213
-
214
-			$this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
215
-		}
216
-		return $this->mountsForUsers[$user->getUID()];
217
-	}
218
-
219
-	/**
220
-	 * @param int $numericStorageId
221
-	 * @param string|null $user limit the results to a single user
222
-	 * @return CachedMountInfo[]
223
-	 */
224
-	public function getMountsForStorageId($numericStorageId, $user = null) {
225
-		$builder = $this->connection->getQueryBuilder();
226
-		$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
227
-			->from('mounts', 'm')
228
-			->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
229
-			->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT)));
230
-
231
-		if ($user) {
232
-			$query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user)));
233
-		}
234
-
235
-		$rows = $query->execute()->fetchAll();
236
-
237
-		return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
238
-	}
239
-
240
-	/**
241
-	 * @param int $rootFileId
242
-	 * @return CachedMountInfo[]
243
-	 */
244
-	public function getMountsForRootId($rootFileId) {
245
-		$builder = $this->connection->getQueryBuilder();
246
-		$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
247
-			->from('mounts', 'm')
248
-			->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
249
-			->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT)));
250
-
251
-		$rows = $query->execute()->fetchAll();
252
-
253
-		return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
254
-	}
255
-
256
-	/**
257
-	 * @param $fileId
258
-	 * @return array
259
-	 * @throws \OCP\Files\NotFoundException
260
-	 */
261
-	private function getCacheInfoFromFileId($fileId) {
262
-		if (!isset($this->cacheInfoCache[$fileId])) {
263
-			$builder = $this->connection->getQueryBuilder();
264
-			$query = $builder->select('storage', 'path', 'mimetype')
265
-				->from('filecache')
266
-				->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
267
-
268
-			$row = $query->execute()->fetch();
269
-			if (is_array($row)) {
270
-				$this->cacheInfoCache[$fileId] = [
271
-					(int)$row['storage'],
272
-					$row['path'],
273
-					(int)$row['mimetype']
274
-				];
275
-			} else {
276
-				throw new NotFoundException('File with id "' . $fileId . '" not found');
277
-			}
278
-		}
279
-		return $this->cacheInfoCache[$fileId];
280
-	}
281
-
282
-	/**
283
-	 * @param int $fileId
284
-	 * @param string|null $user optionally restrict the results to a single user
285
-	 * @return ICachedMountInfo[]
286
-	 * @since 9.0.0
287
-	 */
288
-	public function getMountsForFileId($fileId, $user = null) {
289
-		try {
290
-			list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId);
291
-		} catch (NotFoundException $e) {
292
-			return [];
293
-		}
294
-		$mountsForStorage = $this->getMountsForStorageId($storageId, $user);
295
-
296
-		// filter mounts that are from the same storage but a different directory
297
-		return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) {
298
-			if ($fileId === $mount->getRootId()) {
299
-				return true;
300
-			}
301
-			$internalMountPath = $mount->getRootInternalPath();
302
-
303
-			return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/';
304
-		});
305
-	}
306
-
307
-	/**
308
-	 * Remove all cached mounts for a user
309
-	 *
310
-	 * @param IUser $user
311
-	 */
312
-	public function removeUserMounts(IUser $user) {
313
-		$builder = $this->connection->getQueryBuilder();
314
-
315
-		$query = $builder->delete('mounts')
316
-			->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID())));
317
-		$query->execute();
318
-	}
319
-
320
-	public function removeUserStorageMount($storageId, $userId) {
321
-		$builder = $this->connection->getQueryBuilder();
322
-
323
-		$query = $builder->delete('mounts')
324
-			->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId)))
325
-			->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
326
-		$query->execute();
327
-	}
328
-
329
-	public function remoteStorageMounts($storageId) {
330
-		$builder = $this->connection->getQueryBuilder();
331
-
332
-		$query = $builder->delete('mounts')
333
-			->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
334
-		$query->execute();
335
-	}
336
-
337
-	/**
338
-	 * @param array $users
339
-	 * @return array
340
-	 * @suppress SqlInjectionChecker
341
-	 */
342
-	public function getUsedSpaceForUsers(array $users) {
343
-		$builder = $this->connection->getQueryBuilder();
344
-
345
-		$slash = $builder->createNamedParameter('/');
346
-
347
-		$mountPoint = $builder->func()->concat(
348
-			$builder->func()->concat($slash, 'user_id'),
349
-			$slash
350
-		);
351
-
352
-		$userIds = array_map(function (IUser $user) {
353
-			return $user->getUID();
354
-		}, $users);
355
-
356
-		$query = $builder->select('m.user_id', 'f.size')
357
-			->from('mounts', 'm')
358
-			->innerJoin('m', 'filecache', 'f',
359
-				$builder->expr()->andX(
360
-					$builder->expr()->eq('m.storage_id', 'f.storage'),
361
-					$builder->expr()->eq('f.path', $builder->createNamedParameter('files'))
362
-				))
363
-			->where($builder->expr()->eq('m.mount_point', $mountPoint))
364
-			->andWhere($builder->expr()->in('m.user_id', $builder->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)));
365
-
366
-		$result = $query->execute();
367
-
368
-		$results = [];
369
-		while ($row = $result->fetch()) {
370
-			$results[$row['user_id']] = $row['size'];
371
-		}
372
-		$result->closeCursor();
373
-		return $results;
374
-	}
45
+    /**
46
+     * @var IDBConnection
47
+     */
48
+    private $connection;
49
+
50
+    /**
51
+     * @var IUserManager
52
+     */
53
+    private $userManager;
54
+
55
+    /**
56
+     * Cached mount info.
57
+     * Map of $userId to ICachedMountInfo.
58
+     *
59
+     * @var ICache
60
+     **/
61
+    private $mountsForUsers;
62
+
63
+    /**
64
+     * @var ILogger
65
+     */
66
+    private $logger;
67
+
68
+    /**
69
+     * @var ICache
70
+     */
71
+    private $cacheInfoCache;
72
+
73
+    /**
74
+     * UserMountCache constructor.
75
+     *
76
+     * @param IDBConnection $connection
77
+     * @param IUserManager $userManager
78
+     * @param ILogger $logger
79
+     */
80
+    public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) {
81
+        $this->connection = $connection;
82
+        $this->userManager = $userManager;
83
+        $this->logger = $logger;
84
+        $this->cacheInfoCache = new CappedMemoryCache();
85
+        $this->mountsForUsers = new CappedMemoryCache();
86
+    }
87
+
88
+    public function registerMounts(IUser $user, array $mounts) {
89
+        // filter out non-proper storages coming from unit tests
90
+        $mounts = array_filter($mounts, function (IMountPoint $mount) {
91
+            return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache();
92
+        });
93
+        /** @var ICachedMountInfo[] $newMounts */
94
+        $newMounts = array_map(function (IMountPoint $mount) use ($user) {
95
+            // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
96
+            if ($mount->getStorageRootId() === -1) {
97
+                return null;
98
+            } else {
99
+                return new LazyStorageMountInfo($user, $mount);
100
+            }
101
+        }, $mounts);
102
+        $newMounts = array_values(array_filter($newMounts));
103
+
104
+        $cachedMounts = $this->getMountsForUser($user);
105
+        $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) {
106
+            // since we are only looking for mounts for a specific user comparing on root id is enough
107
+            return $mount1->getRootId() - $mount2->getRootId();
108
+        };
109
+
110
+        /** @var ICachedMountInfo[] $addedMounts */
111
+        $addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff);
112
+        /** @var ICachedMountInfo[] $removedMounts */
113
+        $removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff);
114
+
115
+        $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts);
116
+
117
+        foreach ($addedMounts as $mount) {
118
+            $this->addToCache($mount);
119
+            $this->mountsForUsers[$user->getUID()][] = $mount;
120
+        }
121
+        foreach ($removedMounts as $mount) {
122
+            $this->removeFromCache($mount);
123
+            $index = array_search($mount, $this->mountsForUsers[$user->getUID()]);
124
+            unset($this->mountsForUsers[$user->getUID()][$index]);
125
+        }
126
+        foreach ($changedMounts as $mount) {
127
+            $this->updateCachedMount($mount);
128
+        }
129
+    }
130
+
131
+    /**
132
+     * @param ICachedMountInfo[] $newMounts
133
+     * @param ICachedMountInfo[] $cachedMounts
134
+     * @return ICachedMountInfo[]
135
+     */
136
+    private function findChangedMounts(array $newMounts, array $cachedMounts) {
137
+        $changed = [];
138
+        foreach ($newMounts as $newMount) {
139
+            foreach ($cachedMounts as $cachedMount) {
140
+                if (
141
+                    $newMount->getRootId() === $cachedMount->getRootId() &&
142
+                    (
143
+                        $newMount->getMountPoint() !== $cachedMount->getMountPoint() ||
144
+                        $newMount->getStorageId() !== $cachedMount->getStorageId() ||
145
+                        $newMount->getMountId() !== $cachedMount->getMountId()
146
+                    )
147
+                ) {
148
+                    $changed[] = $newMount;
149
+                }
150
+            }
151
+        }
152
+        return $changed;
153
+    }
154
+
155
+    private function addToCache(ICachedMountInfo $mount) {
156
+        if ($mount->getStorageId() !== -1) {
157
+            $this->connection->insertIfNotExist('*PREFIX*mounts', [
158
+                'storage_id' => $mount->getStorageId(),
159
+                'root_id' => $mount->getRootId(),
160
+                'user_id' => $mount->getUser()->getUID(),
161
+                'mount_point' => $mount->getMountPoint(),
162
+                'mount_id' => $mount->getMountId()
163
+            ], ['root_id', 'user_id']);
164
+        } else {
165
+            // in some cases this is legitimate, like orphaned shares
166
+            $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint());
167
+        }
168
+    }
169
+
170
+    private function updateCachedMount(ICachedMountInfo $mount) {
171
+        $builder = $this->connection->getQueryBuilder();
172
+
173
+        $query = $builder->update('mounts')
174
+            ->set('storage_id', $builder->createNamedParameter($mount->getStorageId()))
175
+            ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint()))
176
+            ->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT))
177
+            ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
178
+            ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
179
+
180
+        $query->execute();
181
+    }
182
+
183
+    private function removeFromCache(ICachedMountInfo $mount) {
184
+        $builder = $this->connection->getQueryBuilder();
185
+
186
+        $query = $builder->delete('mounts')
187
+            ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
188
+            ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
189
+        $query->execute();
190
+    }
191
+
192
+    private function dbRowToMountInfo(array $row) {
193
+        $user = $this->userManager->get($row['user_id']);
194
+        if (is_null($user)) {
195
+            return null;
196
+        }
197
+        return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id'], isset($row['path']) ? $row['path'] : '');
198
+    }
199
+
200
+    /**
201
+     * @param IUser $user
202
+     * @return ICachedMountInfo[]
203
+     */
204
+    public function getMountsForUser(IUser $user) {
205
+        if (!isset($this->mountsForUsers[$user->getUID()])) {
206
+            $builder = $this->connection->getQueryBuilder();
207
+            $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
208
+                ->from('mounts', 'm')
209
+                ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
210
+                ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID())));
211
+
212
+            $rows = $query->execute()->fetchAll();
213
+
214
+            $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
215
+        }
216
+        return $this->mountsForUsers[$user->getUID()];
217
+    }
218
+
219
+    /**
220
+     * @param int $numericStorageId
221
+     * @param string|null $user limit the results to a single user
222
+     * @return CachedMountInfo[]
223
+     */
224
+    public function getMountsForStorageId($numericStorageId, $user = null) {
225
+        $builder = $this->connection->getQueryBuilder();
226
+        $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
227
+            ->from('mounts', 'm')
228
+            ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
229
+            ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT)));
230
+
231
+        if ($user) {
232
+            $query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user)));
233
+        }
234
+
235
+        $rows = $query->execute()->fetchAll();
236
+
237
+        return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
238
+    }
239
+
240
+    /**
241
+     * @param int $rootFileId
242
+     * @return CachedMountInfo[]
243
+     */
244
+    public function getMountsForRootId($rootFileId) {
245
+        $builder = $this->connection->getQueryBuilder();
246
+        $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
247
+            ->from('mounts', 'm')
248
+            ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
249
+            ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT)));
250
+
251
+        $rows = $query->execute()->fetchAll();
252
+
253
+        return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
254
+    }
255
+
256
+    /**
257
+     * @param $fileId
258
+     * @return array
259
+     * @throws \OCP\Files\NotFoundException
260
+     */
261
+    private function getCacheInfoFromFileId($fileId) {
262
+        if (!isset($this->cacheInfoCache[$fileId])) {
263
+            $builder = $this->connection->getQueryBuilder();
264
+            $query = $builder->select('storage', 'path', 'mimetype')
265
+                ->from('filecache')
266
+                ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
267
+
268
+            $row = $query->execute()->fetch();
269
+            if (is_array($row)) {
270
+                $this->cacheInfoCache[$fileId] = [
271
+                    (int)$row['storage'],
272
+                    $row['path'],
273
+                    (int)$row['mimetype']
274
+                ];
275
+            } else {
276
+                throw new NotFoundException('File with id "' . $fileId . '" not found');
277
+            }
278
+        }
279
+        return $this->cacheInfoCache[$fileId];
280
+    }
281
+
282
+    /**
283
+     * @param int $fileId
284
+     * @param string|null $user optionally restrict the results to a single user
285
+     * @return ICachedMountInfo[]
286
+     * @since 9.0.0
287
+     */
288
+    public function getMountsForFileId($fileId, $user = null) {
289
+        try {
290
+            list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId);
291
+        } catch (NotFoundException $e) {
292
+            return [];
293
+        }
294
+        $mountsForStorage = $this->getMountsForStorageId($storageId, $user);
295
+
296
+        // filter mounts that are from the same storage but a different directory
297
+        return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) {
298
+            if ($fileId === $mount->getRootId()) {
299
+                return true;
300
+            }
301
+            $internalMountPath = $mount->getRootInternalPath();
302
+
303
+            return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/';
304
+        });
305
+    }
306
+
307
+    /**
308
+     * Remove all cached mounts for a user
309
+     *
310
+     * @param IUser $user
311
+     */
312
+    public function removeUserMounts(IUser $user) {
313
+        $builder = $this->connection->getQueryBuilder();
314
+
315
+        $query = $builder->delete('mounts')
316
+            ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID())));
317
+        $query->execute();
318
+    }
319
+
320
+    public function removeUserStorageMount($storageId, $userId) {
321
+        $builder = $this->connection->getQueryBuilder();
322
+
323
+        $query = $builder->delete('mounts')
324
+            ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId)))
325
+            ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
326
+        $query->execute();
327
+    }
328
+
329
+    public function remoteStorageMounts($storageId) {
330
+        $builder = $this->connection->getQueryBuilder();
331
+
332
+        $query = $builder->delete('mounts')
333
+            ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
334
+        $query->execute();
335
+    }
336
+
337
+    /**
338
+     * @param array $users
339
+     * @return array
340
+     * @suppress SqlInjectionChecker
341
+     */
342
+    public function getUsedSpaceForUsers(array $users) {
343
+        $builder = $this->connection->getQueryBuilder();
344
+
345
+        $slash = $builder->createNamedParameter('/');
346
+
347
+        $mountPoint = $builder->func()->concat(
348
+            $builder->func()->concat($slash, 'user_id'),
349
+            $slash
350
+        );
351
+
352
+        $userIds = array_map(function (IUser $user) {
353
+            return $user->getUID();
354
+        }, $users);
355
+
356
+        $query = $builder->select('m.user_id', 'f.size')
357
+            ->from('mounts', 'm')
358
+            ->innerJoin('m', 'filecache', 'f',
359
+                $builder->expr()->andX(
360
+                    $builder->expr()->eq('m.storage_id', 'f.storage'),
361
+                    $builder->expr()->eq('f.path', $builder->createNamedParameter('files'))
362
+                ))
363
+            ->where($builder->expr()->eq('m.mount_point', $mountPoint))
364
+            ->andWhere($builder->expr()->in('m.user_id', $builder->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)));
365
+
366
+        $result = $query->execute();
367
+
368
+        $results = [];
369
+        while ($row = $result->fetch()) {
370
+            $results[$row['user_id']] = $row['size'];
371
+        }
372
+        $result->closeCursor();
373
+        return $results;
374
+    }
375 375
 }
Please login to merge, or discard this patch.
lib/private/Files/Node/LazyRoot.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -52,7 +52,7 @@
 block discarded – undo
52 52
 	 * Magic method to first get the real rootFolder and then
53 53
 	 * call $method with $args on it
54 54
 	 *
55
-	 * @param $method
55
+	 * @param string $method
56 56
 	 * @param $args
57 57
 	 * @return mixed
58 58
 	 */
Please login to merge, or discard this patch.
Indentation   +443 added lines, -443 removed lines patch added patch discarded remove patch
@@ -34,447 +34,447 @@
 block discarded – undo
34 34
  * @package OC\Files\Node
35 35
  */
36 36
 class LazyRoot implements IRootFolder {
37
-	/** @var \Closure */
38
-	private $rootFolderClosure;
39
-
40
-	/** @var IRootFolder */
41
-	private $rootFolder;
42
-
43
-	/**
44
-	 * LazyRoot constructor.
45
-	 *
46
-	 * @param \Closure $rootFolderClosure
47
-	 */
48
-	public function __construct(\Closure $rootFolderClosure) {
49
-		$this->rootFolderClosure = $rootFolderClosure;
50
-	}
51
-
52
-	/**
53
-	 * Magic method to first get the real rootFolder and then
54
-	 * call $method with $args on it
55
-	 *
56
-	 * @param $method
57
-	 * @param $args
58
-	 * @return mixed
59
-	 */
60
-	public function __call($method, $args) {
61
-		if ($this->rootFolder === null) {
62
-			$this->rootFolder = call_user_func($this->rootFolderClosure);
63
-		}
64
-
65
-		return call_user_func_array([$this->rootFolder, $method], $args);
66
-	}
67
-
68
-	/**
69
-	 * @inheritDoc
70
-	 */
71
-	public function getUser() {
72
-		return $this->__call(__FUNCTION__, func_get_args());
73
-	}
74
-
75
-	/**
76
-	 * @inheritDoc
77
-	 */
78
-	public function listen($scope, $method, callable $callback) {
79
-		$this->__call(__FUNCTION__, func_get_args());
80
-	}
81
-
82
-	/**
83
-	 * @inheritDoc
84
-	 */
85
-	public function removeListener($scope = null, $method = null, callable $callback = null) {
86
-		$this->__call(__FUNCTION__, func_get_args());
87
-	}
88
-
89
-	/**
90
-	 * @inheritDoc
91
-	 */
92
-	public function emit($scope, $method, $arguments = array()) {
93
-		$this->__call(__FUNCTION__, func_get_args());
94
-	}
95
-
96
-	/**
97
-	 * @inheritDoc
98
-	 */
99
-	public function mount($storage, $mountPoint, $arguments = array()) {
100
-		$this->__call(__FUNCTION__, func_get_args());
101
-	}
102
-
103
-	/**
104
-	 * @inheritDoc
105
-	 */
106
-	public function getMount($mountPoint) {
107
-		return $this->__call(__FUNCTION__, func_get_args());
108
-	}
109
-
110
-	/**
111
-	 * @inheritDoc
112
-	 */
113
-	public function getMountsIn($mountPoint) {
114
-		return $this->__call(__FUNCTION__, func_get_args());
115
-	}
116
-
117
-	/**
118
-	 * @inheritDoc
119
-	 */
120
-	public function getMountByStorageId($storageId) {
121
-		return $this->__call(__FUNCTION__, func_get_args());
122
-	}
123
-
124
-	/**
125
-	 * @inheritDoc
126
-	 */
127
-	public function getMountByNumericStorageId($numericId) {
128
-		return $this->__call(__FUNCTION__, func_get_args());
129
-	}
130
-
131
-	/**
132
-	 * @inheritDoc
133
-	 */
134
-	public function unMount($mount) {
135
-		$this->__call(__FUNCTION__, func_get_args());
136
-	}
137
-
138
-	/**
139
-	 * @inheritDoc
140
-	 */
141
-	public function get($path) {
142
-		return $this->__call(__FUNCTION__, func_get_args());
143
-	}
144
-
145
-	/**
146
-	 * @inheritDoc
147
-	 */
148
-	public function rename($targetPath) {
149
-		return $this->__call(__FUNCTION__, func_get_args());
150
-	}
151
-
152
-	/**
153
-	 * @inheritDoc
154
-	 */
155
-	public function delete() {
156
-		return $this->__call(__FUNCTION__, func_get_args());
157
-	}
158
-
159
-	/**
160
-	 * @inheritDoc
161
-	 */
162
-	public function copy($targetPath) {
163
-		return $this->__call(__FUNCTION__, func_get_args());
164
-	}
165
-
166
-	/**
167
-	 * @inheritDoc
168
-	 */
169
-	public function touch($mtime = null) {
170
-		$this->__call(__FUNCTION__, func_get_args());
171
-	}
172
-
173
-	/**
174
-	 * @inheritDoc
175
-	 */
176
-	public function getStorage() {
177
-		return $this->__call(__FUNCTION__, func_get_args());
178
-	}
179
-
180
-	/**
181
-	 * @inheritDoc
182
-	 */
183
-	public function getPath() {
184
-		return $this->__call(__FUNCTION__, func_get_args());
185
-	}
186
-
187
-	/**
188
-	 * @inheritDoc
189
-	 */
190
-	public function getInternalPath() {
191
-		return $this->__call(__FUNCTION__, func_get_args());
192
-	}
193
-
194
-	/**
195
-	 * @inheritDoc
196
-	 */
197
-	public function getId() {
198
-		return $this->__call(__FUNCTION__, func_get_args());
199
-	}
200
-
201
-	/**
202
-	 * @inheritDoc
203
-	 */
204
-	public function stat() {
205
-		return $this->__call(__FUNCTION__, func_get_args());
206
-	}
207
-
208
-	/**
209
-	 * @inheritDoc
210
-	 */
211
-	public function getMTime() {
212
-		return $this->__call(__FUNCTION__, func_get_args());
213
-	}
214
-
215
-	/**
216
-	 * @inheritDoc
217
-	 */
218
-	public function getSize() {
219
-		return $this->__call(__FUNCTION__, func_get_args());
220
-	}
221
-
222
-	/**
223
-	 * @inheritDoc
224
-	 */
225
-	public function getEtag() {
226
-		return $this->__call(__FUNCTION__, func_get_args());
227
-	}
228
-
229
-	/**
230
-	 * @inheritDoc
231
-	 */
232
-	public function getPermissions() {
233
-		return $this->__call(__FUNCTION__, func_get_args());
234
-	}
235
-
236
-	/**
237
-	 * @inheritDoc
238
-	 */
239
-	public function isReadable() {
240
-		return $this->__call(__FUNCTION__, func_get_args());
241
-	}
242
-
243
-	/**
244
-	 * @inheritDoc
245
-	 */
246
-	public function isUpdateable() {
247
-		return $this->__call(__FUNCTION__, func_get_args());
248
-	}
249
-
250
-	/**
251
-	 * @inheritDoc
252
-	 */
253
-	public function isDeletable() {
254
-		return $this->__call(__FUNCTION__, func_get_args());
255
-	}
256
-
257
-	/**
258
-	 * @inheritDoc
259
-	 */
260
-	public function isShareable() {
261
-		return $this->__call(__FUNCTION__, func_get_args());
262
-	}
263
-
264
-	/**
265
-	 * @inheritDoc
266
-	 */
267
-	public function getParent() {
268
-		return $this->__call(__FUNCTION__, func_get_args());
269
-	}
270
-
271
-	/**
272
-	 * @inheritDoc
273
-	 */
274
-	public function getName() {
275
-		return $this->__call(__FUNCTION__, func_get_args());
276
-	}
277
-
278
-	/**
279
-	 * @inheritDoc
280
-	 */
281
-	public function getUserFolder($userId) {
282
-		return $this->__call(__FUNCTION__, func_get_args());
283
-	}
284
-
285
-	/**
286
-	 * @inheritDoc
287
-	 */
288
-	public function getMimetype() {
289
-		return $this->__call(__FUNCTION__, func_get_args());
290
-	}
291
-
292
-	/**
293
-	 * @inheritDoc
294
-	 */
295
-	public function getMimePart() {
296
-		return $this->__call(__FUNCTION__, func_get_args());
297
-	}
298
-
299
-	/**
300
-	 * @inheritDoc
301
-	 */
302
-	public function isEncrypted() {
303
-		return $this->__call(__FUNCTION__, func_get_args());
304
-	}
305
-
306
-	/**
307
-	 * @inheritDoc
308
-	 */
309
-	public function getType() {
310
-		return $this->__call(__FUNCTION__, func_get_args());
311
-	}
312
-
313
-	/**
314
-	 * @inheritDoc
315
-	 */
316
-	public function isShared() {
317
-		return $this->__call(__FUNCTION__, func_get_args());
318
-	}
319
-
320
-	/**
321
-	 * @inheritDoc
322
-	 */
323
-	public function isMounted() {
324
-		return $this->__call(__FUNCTION__, func_get_args());
325
-	}
326
-
327
-	/**
328
-	 * @inheritDoc
329
-	 */
330
-	public function getMountPoint() {
331
-		return $this->__call(__FUNCTION__, func_get_args());
332
-	}
333
-
334
-	/**
335
-	 * @inheritDoc
336
-	 */
337
-	public function getOwner() {
338
-		return $this->__call(__FUNCTION__, func_get_args());
339
-	}
340
-
341
-	/**
342
-	 * @inheritDoc
343
-	 */
344
-	public function getChecksum() {
345
-		return $this->__call(__FUNCTION__, func_get_args());
346
-	}
347
-
348
-	/**
349
-	 * @inheritDoc
350
-	 */
351
-	public function getFullPath($path) {
352
-		return $this->__call(__FUNCTION__, func_get_args());
353
-	}
354
-
355
-	/**
356
-	 * @inheritDoc
357
-	 */
358
-	public function getRelativePath($path) {
359
-		return $this->__call(__FUNCTION__, func_get_args());
360
-	}
361
-
362
-	/**
363
-	 * @inheritDoc
364
-	 */
365
-	public function isSubNode($node) {
366
-		return $this->__call(__FUNCTION__, func_get_args());
367
-	}
368
-
369
-	/**
370
-	 * @inheritDoc
371
-	 */
372
-	public function getDirectoryListing() {
373
-		return $this->__call(__FUNCTION__, func_get_args());
374
-	}
375
-
376
-	/**
377
-	 * @inheritDoc
378
-	 */
379
-	public function nodeExists($path) {
380
-		return $this->__call(__FUNCTION__, func_get_args());
381
-	}
382
-
383
-	/**
384
-	 * @inheritDoc
385
-	 */
386
-	public function newFolder($path) {
387
-		return $this->__call(__FUNCTION__, func_get_args());
388
-	}
389
-
390
-	/**
391
-	 * @inheritDoc
392
-	 */
393
-	public function newFile($path) {
394
-		return $this->__call(__FUNCTION__, func_get_args());
395
-	}
396
-
397
-	/**
398
-	 * @inheritDoc
399
-	 */
400
-	public function search($query) {
401
-		return $this->__call(__FUNCTION__, func_get_args());
402
-	}
403
-
404
-	/**
405
-	 * @inheritDoc
406
-	 */
407
-	public function searchByMime($mimetype) {
408
-		return $this->__call(__FUNCTION__, func_get_args());
409
-	}
410
-
411
-	/**
412
-	 * @inheritDoc
413
-	 */
414
-	public function searchByTag($tag, $userId) {
415
-		return $this->__call(__FUNCTION__, func_get_args());
416
-	}
417
-
418
-	/**
419
-	 * @inheritDoc
420
-	 */
421
-	public function getById($id) {
422
-		return $this->__call(__FUNCTION__, func_get_args());
423
-	}
424
-
425
-	/**
426
-	 * @inheritDoc
427
-	 */
428
-	public function getFreeSpace() {
429
-		return $this->__call(__FUNCTION__, func_get_args());
430
-	}
431
-
432
-	/**
433
-	 * @inheritDoc
434
-	 */
435
-	public function isCreatable() {
436
-		return $this->__call(__FUNCTION__, func_get_args());
437
-	}
438
-
439
-	/**
440
-	 * @inheritDoc
441
-	 */
442
-	public function getNonExistingName($name) {
443
-		return $this->__call(__FUNCTION__, func_get_args());
444
-	}
445
-
446
-	/**
447
-	 * @inheritDoc
448
-	 */
449
-	public function move($targetPath) {
450
-		return $this->__call(__FUNCTION__, func_get_args());
451
-	}
452
-
453
-	/**
454
-	 * @inheritDoc
455
-	 */
456
-	public function lock($type) {
457
-		return $this->__call(__FUNCTION__, func_get_args());
458
-	}
459
-
460
-	/**
461
-	 * @inheritDoc
462
-	 */
463
-	public function changeLock($targetType) {
464
-		return $this->__call(__FUNCTION__, func_get_args());
465
-	}
466
-
467
-	/**
468
-	 * @inheritDoc
469
-	 */
470
-	public function unlock($type) {
471
-		return $this->__call(__FUNCTION__, func_get_args());
472
-	}
473
-
474
-	/**
475
-	 * @inheritDoc
476
-	 */
477
-	public function getRecent($limit, $offset = 0) {
478
-		return $this->__call(__FUNCTION__, func_get_args());
479
-	}
37
+    /** @var \Closure */
38
+    private $rootFolderClosure;
39
+
40
+    /** @var IRootFolder */
41
+    private $rootFolder;
42
+
43
+    /**
44
+     * LazyRoot constructor.
45
+     *
46
+     * @param \Closure $rootFolderClosure
47
+     */
48
+    public function __construct(\Closure $rootFolderClosure) {
49
+        $this->rootFolderClosure = $rootFolderClosure;
50
+    }
51
+
52
+    /**
53
+     * Magic method to first get the real rootFolder and then
54
+     * call $method with $args on it
55
+     *
56
+     * @param $method
57
+     * @param $args
58
+     * @return mixed
59
+     */
60
+    public function __call($method, $args) {
61
+        if ($this->rootFolder === null) {
62
+            $this->rootFolder = call_user_func($this->rootFolderClosure);
63
+        }
64
+
65
+        return call_user_func_array([$this->rootFolder, $method], $args);
66
+    }
67
+
68
+    /**
69
+     * @inheritDoc
70
+     */
71
+    public function getUser() {
72
+        return $this->__call(__FUNCTION__, func_get_args());
73
+    }
74
+
75
+    /**
76
+     * @inheritDoc
77
+     */
78
+    public function listen($scope, $method, callable $callback) {
79
+        $this->__call(__FUNCTION__, func_get_args());
80
+    }
81
+
82
+    /**
83
+     * @inheritDoc
84
+     */
85
+    public function removeListener($scope = null, $method = null, callable $callback = null) {
86
+        $this->__call(__FUNCTION__, func_get_args());
87
+    }
88
+
89
+    /**
90
+     * @inheritDoc
91
+     */
92
+    public function emit($scope, $method, $arguments = array()) {
93
+        $this->__call(__FUNCTION__, func_get_args());
94
+    }
95
+
96
+    /**
97
+     * @inheritDoc
98
+     */
99
+    public function mount($storage, $mountPoint, $arguments = array()) {
100
+        $this->__call(__FUNCTION__, func_get_args());
101
+    }
102
+
103
+    /**
104
+     * @inheritDoc
105
+     */
106
+    public function getMount($mountPoint) {
107
+        return $this->__call(__FUNCTION__, func_get_args());
108
+    }
109
+
110
+    /**
111
+     * @inheritDoc
112
+     */
113
+    public function getMountsIn($mountPoint) {
114
+        return $this->__call(__FUNCTION__, func_get_args());
115
+    }
116
+
117
+    /**
118
+     * @inheritDoc
119
+     */
120
+    public function getMountByStorageId($storageId) {
121
+        return $this->__call(__FUNCTION__, func_get_args());
122
+    }
123
+
124
+    /**
125
+     * @inheritDoc
126
+     */
127
+    public function getMountByNumericStorageId($numericId) {
128
+        return $this->__call(__FUNCTION__, func_get_args());
129
+    }
130
+
131
+    /**
132
+     * @inheritDoc
133
+     */
134
+    public function unMount($mount) {
135
+        $this->__call(__FUNCTION__, func_get_args());
136
+    }
137
+
138
+    /**
139
+     * @inheritDoc
140
+     */
141
+    public function get($path) {
142
+        return $this->__call(__FUNCTION__, func_get_args());
143
+    }
144
+
145
+    /**
146
+     * @inheritDoc
147
+     */
148
+    public function rename($targetPath) {
149
+        return $this->__call(__FUNCTION__, func_get_args());
150
+    }
151
+
152
+    /**
153
+     * @inheritDoc
154
+     */
155
+    public function delete() {
156
+        return $this->__call(__FUNCTION__, func_get_args());
157
+    }
158
+
159
+    /**
160
+     * @inheritDoc
161
+     */
162
+    public function copy($targetPath) {
163
+        return $this->__call(__FUNCTION__, func_get_args());
164
+    }
165
+
166
+    /**
167
+     * @inheritDoc
168
+     */
169
+    public function touch($mtime = null) {
170
+        $this->__call(__FUNCTION__, func_get_args());
171
+    }
172
+
173
+    /**
174
+     * @inheritDoc
175
+     */
176
+    public function getStorage() {
177
+        return $this->__call(__FUNCTION__, func_get_args());
178
+    }
179
+
180
+    /**
181
+     * @inheritDoc
182
+     */
183
+    public function getPath() {
184
+        return $this->__call(__FUNCTION__, func_get_args());
185
+    }
186
+
187
+    /**
188
+     * @inheritDoc
189
+     */
190
+    public function getInternalPath() {
191
+        return $this->__call(__FUNCTION__, func_get_args());
192
+    }
193
+
194
+    /**
195
+     * @inheritDoc
196
+     */
197
+    public function getId() {
198
+        return $this->__call(__FUNCTION__, func_get_args());
199
+    }
200
+
201
+    /**
202
+     * @inheritDoc
203
+     */
204
+    public function stat() {
205
+        return $this->__call(__FUNCTION__, func_get_args());
206
+    }
207
+
208
+    /**
209
+     * @inheritDoc
210
+     */
211
+    public function getMTime() {
212
+        return $this->__call(__FUNCTION__, func_get_args());
213
+    }
214
+
215
+    /**
216
+     * @inheritDoc
217
+     */
218
+    public function getSize() {
219
+        return $this->__call(__FUNCTION__, func_get_args());
220
+    }
221
+
222
+    /**
223
+     * @inheritDoc
224
+     */
225
+    public function getEtag() {
226
+        return $this->__call(__FUNCTION__, func_get_args());
227
+    }
228
+
229
+    /**
230
+     * @inheritDoc
231
+     */
232
+    public function getPermissions() {
233
+        return $this->__call(__FUNCTION__, func_get_args());
234
+    }
235
+
236
+    /**
237
+     * @inheritDoc
238
+     */
239
+    public function isReadable() {
240
+        return $this->__call(__FUNCTION__, func_get_args());
241
+    }
242
+
243
+    /**
244
+     * @inheritDoc
245
+     */
246
+    public function isUpdateable() {
247
+        return $this->__call(__FUNCTION__, func_get_args());
248
+    }
249
+
250
+    /**
251
+     * @inheritDoc
252
+     */
253
+    public function isDeletable() {
254
+        return $this->__call(__FUNCTION__, func_get_args());
255
+    }
256
+
257
+    /**
258
+     * @inheritDoc
259
+     */
260
+    public function isShareable() {
261
+        return $this->__call(__FUNCTION__, func_get_args());
262
+    }
263
+
264
+    /**
265
+     * @inheritDoc
266
+     */
267
+    public function getParent() {
268
+        return $this->__call(__FUNCTION__, func_get_args());
269
+    }
270
+
271
+    /**
272
+     * @inheritDoc
273
+     */
274
+    public function getName() {
275
+        return $this->__call(__FUNCTION__, func_get_args());
276
+    }
277
+
278
+    /**
279
+     * @inheritDoc
280
+     */
281
+    public function getUserFolder($userId) {
282
+        return $this->__call(__FUNCTION__, func_get_args());
283
+    }
284
+
285
+    /**
286
+     * @inheritDoc
287
+     */
288
+    public function getMimetype() {
289
+        return $this->__call(__FUNCTION__, func_get_args());
290
+    }
291
+
292
+    /**
293
+     * @inheritDoc
294
+     */
295
+    public function getMimePart() {
296
+        return $this->__call(__FUNCTION__, func_get_args());
297
+    }
298
+
299
+    /**
300
+     * @inheritDoc
301
+     */
302
+    public function isEncrypted() {
303
+        return $this->__call(__FUNCTION__, func_get_args());
304
+    }
305
+
306
+    /**
307
+     * @inheritDoc
308
+     */
309
+    public function getType() {
310
+        return $this->__call(__FUNCTION__, func_get_args());
311
+    }
312
+
313
+    /**
314
+     * @inheritDoc
315
+     */
316
+    public function isShared() {
317
+        return $this->__call(__FUNCTION__, func_get_args());
318
+    }
319
+
320
+    /**
321
+     * @inheritDoc
322
+     */
323
+    public function isMounted() {
324
+        return $this->__call(__FUNCTION__, func_get_args());
325
+    }
326
+
327
+    /**
328
+     * @inheritDoc
329
+     */
330
+    public function getMountPoint() {
331
+        return $this->__call(__FUNCTION__, func_get_args());
332
+    }
333
+
334
+    /**
335
+     * @inheritDoc
336
+     */
337
+    public function getOwner() {
338
+        return $this->__call(__FUNCTION__, func_get_args());
339
+    }
340
+
341
+    /**
342
+     * @inheritDoc
343
+     */
344
+    public function getChecksum() {
345
+        return $this->__call(__FUNCTION__, func_get_args());
346
+    }
347
+
348
+    /**
349
+     * @inheritDoc
350
+     */
351
+    public function getFullPath($path) {
352
+        return $this->__call(__FUNCTION__, func_get_args());
353
+    }
354
+
355
+    /**
356
+     * @inheritDoc
357
+     */
358
+    public function getRelativePath($path) {
359
+        return $this->__call(__FUNCTION__, func_get_args());
360
+    }
361
+
362
+    /**
363
+     * @inheritDoc
364
+     */
365
+    public function isSubNode($node) {
366
+        return $this->__call(__FUNCTION__, func_get_args());
367
+    }
368
+
369
+    /**
370
+     * @inheritDoc
371
+     */
372
+    public function getDirectoryListing() {
373
+        return $this->__call(__FUNCTION__, func_get_args());
374
+    }
375
+
376
+    /**
377
+     * @inheritDoc
378
+     */
379
+    public function nodeExists($path) {
380
+        return $this->__call(__FUNCTION__, func_get_args());
381
+    }
382
+
383
+    /**
384
+     * @inheritDoc
385
+     */
386
+    public function newFolder($path) {
387
+        return $this->__call(__FUNCTION__, func_get_args());
388
+    }
389
+
390
+    /**
391
+     * @inheritDoc
392
+     */
393
+    public function newFile($path) {
394
+        return $this->__call(__FUNCTION__, func_get_args());
395
+    }
396
+
397
+    /**
398
+     * @inheritDoc
399
+     */
400
+    public function search($query) {
401
+        return $this->__call(__FUNCTION__, func_get_args());
402
+    }
403
+
404
+    /**
405
+     * @inheritDoc
406
+     */
407
+    public function searchByMime($mimetype) {
408
+        return $this->__call(__FUNCTION__, func_get_args());
409
+    }
410
+
411
+    /**
412
+     * @inheritDoc
413
+     */
414
+    public function searchByTag($tag, $userId) {
415
+        return $this->__call(__FUNCTION__, func_get_args());
416
+    }
417
+
418
+    /**
419
+     * @inheritDoc
420
+     */
421
+    public function getById($id) {
422
+        return $this->__call(__FUNCTION__, func_get_args());
423
+    }
424
+
425
+    /**
426
+     * @inheritDoc
427
+     */
428
+    public function getFreeSpace() {
429
+        return $this->__call(__FUNCTION__, func_get_args());
430
+    }
431
+
432
+    /**
433
+     * @inheritDoc
434
+     */
435
+    public function isCreatable() {
436
+        return $this->__call(__FUNCTION__, func_get_args());
437
+    }
438
+
439
+    /**
440
+     * @inheritDoc
441
+     */
442
+    public function getNonExistingName($name) {
443
+        return $this->__call(__FUNCTION__, func_get_args());
444
+    }
445
+
446
+    /**
447
+     * @inheritDoc
448
+     */
449
+    public function move($targetPath) {
450
+        return $this->__call(__FUNCTION__, func_get_args());
451
+    }
452
+
453
+    /**
454
+     * @inheritDoc
455
+     */
456
+    public function lock($type) {
457
+        return $this->__call(__FUNCTION__, func_get_args());
458
+    }
459
+
460
+    /**
461
+     * @inheritDoc
462
+     */
463
+    public function changeLock($targetType) {
464
+        return $this->__call(__FUNCTION__, func_get_args());
465
+    }
466
+
467
+    /**
468
+     * @inheritDoc
469
+     */
470
+    public function unlock($type) {
471
+        return $this->__call(__FUNCTION__, func_get_args());
472
+    }
473
+
474
+    /**
475
+     * @inheritDoc
476
+     */
477
+    public function getRecent($limit, $offset = 0) {
478
+        return $this->__call(__FUNCTION__, func_get_args());
479
+    }
480 480
 }
Please login to merge, or discard this patch.
lib/private/Files/Storage/Flysystem.php 3 patches
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -54,6 +54,9 @@
 block discarded – undo
54 54
 		$this->flysystem->addPlugin(new GetWithMetadata());
55 55
 	}
56 56
 
57
+	/**
58
+	 * @param string $path
59
+	 */
57 60
 	protected function buildPath($path) {
58 61
 		$fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path);
59 62
 		return ltrim($fullPath, '/');
Please login to merge, or discard this patch.
Indentation   +201 added lines, -201 removed lines patch added patch discarded remove patch
@@ -35,223 +35,223 @@
 block discarded – undo
35 35
  * To use: subclass and call $this->buildFlysystem with the flysystem adapter of choice
36 36
  */
37 37
 abstract class Flysystem extends Common {
38
-	/**
39
-	 * @var Filesystem
40
-	 */
41
-	protected $flysystem;
38
+    /**
39
+     * @var Filesystem
40
+     */
41
+    protected $flysystem;
42 42
 
43
-	/**
44
-	 * @var string
45
-	 */
46
-	protected $root = '';
43
+    /**
44
+     * @var string
45
+     */
46
+    protected $root = '';
47 47
 
48
-	/**
49
-	 * Initialize the storage backend with a flyssytem adapter
50
-	 *
51
-	 * @param \League\Flysystem\AdapterInterface $adapter
52
-	 */
53
-	protected function buildFlySystem(AdapterInterface $adapter) {
54
-		$this->flysystem = new Filesystem($adapter);
55
-		$this->flysystem->addPlugin(new GetWithMetadata());
56
-	}
48
+    /**
49
+     * Initialize the storage backend with a flyssytem adapter
50
+     *
51
+     * @param \League\Flysystem\AdapterInterface $adapter
52
+     */
53
+    protected function buildFlySystem(AdapterInterface $adapter) {
54
+        $this->flysystem = new Filesystem($adapter);
55
+        $this->flysystem->addPlugin(new GetWithMetadata());
56
+    }
57 57
 
58
-	protected function buildPath($path) {
59
-		$fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path);
60
-		return ltrim($fullPath, '/');
61
-	}
58
+    protected function buildPath($path) {
59
+        $fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path);
60
+        return ltrim($fullPath, '/');
61
+    }
62 62
 
63
-	/**
64
-	 * {@inheritdoc}
65
-	 */
66
-	public function file_get_contents($path) {
67
-		return $this->flysystem->read($this->buildPath($path));
68
-	}
63
+    /**
64
+     * {@inheritdoc}
65
+     */
66
+    public function file_get_contents($path) {
67
+        return $this->flysystem->read($this->buildPath($path));
68
+    }
69 69
 
70
-	/**
71
-	 * {@inheritdoc}
72
-	 */
73
-	public function file_put_contents($path, $data) {
74
-		return $this->flysystem->put($this->buildPath($path), $data);
75
-	}
70
+    /**
71
+     * {@inheritdoc}
72
+     */
73
+    public function file_put_contents($path, $data) {
74
+        return $this->flysystem->put($this->buildPath($path), $data);
75
+    }
76 76
 
77
-	/**
78
-	 * {@inheritdoc}
79
-	 */
80
-	public function file_exists($path) {
81
-		return $this->flysystem->has($this->buildPath($path));
82
-	}
77
+    /**
78
+     * {@inheritdoc}
79
+     */
80
+    public function file_exists($path) {
81
+        return $this->flysystem->has($this->buildPath($path));
82
+    }
83 83
 
84
-	/**
85
-	 * {@inheritdoc}
86
-	 */
87
-	public function unlink($path) {
88
-		if ($this->is_dir($path)) {
89
-			return $this->rmdir($path);
90
-		}
91
-		try {
92
-			return $this->flysystem->delete($this->buildPath($path));
93
-		} catch (FileNotFoundException $e) {
94
-			return false;
95
-		}
96
-	}
84
+    /**
85
+     * {@inheritdoc}
86
+     */
87
+    public function unlink($path) {
88
+        if ($this->is_dir($path)) {
89
+            return $this->rmdir($path);
90
+        }
91
+        try {
92
+            return $this->flysystem->delete($this->buildPath($path));
93
+        } catch (FileNotFoundException $e) {
94
+            return false;
95
+        }
96
+    }
97 97
 
98
-	/**
99
-	 * {@inheritdoc}
100
-	 */
101
-	public function rename($source, $target) {
102
-		if ($this->file_exists($target)) {
103
-			$this->unlink($target);
104
-		}
105
-		return $this->flysystem->rename($this->buildPath($source), $this->buildPath($target));
106
-	}
98
+    /**
99
+     * {@inheritdoc}
100
+     */
101
+    public function rename($source, $target) {
102
+        if ($this->file_exists($target)) {
103
+            $this->unlink($target);
104
+        }
105
+        return $this->flysystem->rename($this->buildPath($source), $this->buildPath($target));
106
+    }
107 107
 
108
-	/**
109
-	 * {@inheritdoc}
110
-	 */
111
-	public function copy($source, $target) {
112
-		if ($this->file_exists($target)) {
113
-			$this->unlink($target);
114
-		}
115
-		return $this->flysystem->copy($this->buildPath($source), $this->buildPath($target));
116
-	}
108
+    /**
109
+     * {@inheritdoc}
110
+     */
111
+    public function copy($source, $target) {
112
+        if ($this->file_exists($target)) {
113
+            $this->unlink($target);
114
+        }
115
+        return $this->flysystem->copy($this->buildPath($source), $this->buildPath($target));
116
+    }
117 117
 
118
-	/**
119
-	 * {@inheritdoc}
120
-	 */
121
-	public function filesize($path) {
122
-		if ($this->is_dir($path)) {
123
-			return 0;
124
-		} else {
125
-			return $this->flysystem->getSize($this->buildPath($path));
126
-		}
127
-	}
118
+    /**
119
+     * {@inheritdoc}
120
+     */
121
+    public function filesize($path) {
122
+        if ($this->is_dir($path)) {
123
+            return 0;
124
+        } else {
125
+            return $this->flysystem->getSize($this->buildPath($path));
126
+        }
127
+    }
128 128
 
129
-	/**
130
-	 * {@inheritdoc}
131
-	 */
132
-	public function mkdir($path) {
133
-		if ($this->file_exists($path)) {
134
-			return false;
135
-		}
136
-		return $this->flysystem->createDir($this->buildPath($path));
137
-	}
129
+    /**
130
+     * {@inheritdoc}
131
+     */
132
+    public function mkdir($path) {
133
+        if ($this->file_exists($path)) {
134
+            return false;
135
+        }
136
+        return $this->flysystem->createDir($this->buildPath($path));
137
+    }
138 138
 
139
-	/**
140
-	 * {@inheritdoc}
141
-	 */
142
-	public function filemtime($path) {
143
-		return $this->flysystem->getTimestamp($this->buildPath($path));
144
-	}
139
+    /**
140
+     * {@inheritdoc}
141
+     */
142
+    public function filemtime($path) {
143
+        return $this->flysystem->getTimestamp($this->buildPath($path));
144
+    }
145 145
 
146
-	/**
147
-	 * {@inheritdoc}
148
-	 */
149
-	public function rmdir($path) {
150
-		try {
151
-			return @$this->flysystem->deleteDir($this->buildPath($path));
152
-		} catch (FileNotFoundException $e) {
153
-			return false;
154
-		}
155
-	}
146
+    /**
147
+     * {@inheritdoc}
148
+     */
149
+    public function rmdir($path) {
150
+        try {
151
+            return @$this->flysystem->deleteDir($this->buildPath($path));
152
+        } catch (FileNotFoundException $e) {
153
+            return false;
154
+        }
155
+    }
156 156
 
157
-	/**
158
-	 * {@inheritdoc}
159
-	 */
160
-	public function opendir($path) {
161
-		try {
162
-			$content = $this->flysystem->listContents($this->buildPath($path));
163
-		} catch (FileNotFoundException $e) {
164
-			return false;
165
-		}
166
-		$names = array_map(function ($object) {
167
-			return $object['basename'];
168
-		}, $content);
169
-		return IteratorDirectory::wrap($names);
170
-	}
157
+    /**
158
+     * {@inheritdoc}
159
+     */
160
+    public function opendir($path) {
161
+        try {
162
+            $content = $this->flysystem->listContents($this->buildPath($path));
163
+        } catch (FileNotFoundException $e) {
164
+            return false;
165
+        }
166
+        $names = array_map(function ($object) {
167
+            return $object['basename'];
168
+        }, $content);
169
+        return IteratorDirectory::wrap($names);
170
+    }
171 171
 
172
-	/**
173
-	 * {@inheritdoc}
174
-	 */
175
-	public function fopen($path, $mode) {
176
-		$fullPath = $this->buildPath($path);
177
-		$useExisting = true;
178
-		switch ($mode) {
179
-			case 'r':
180
-			case 'rb':
181
-				try {
182
-					return $this->flysystem->readStream($fullPath);
183
-				} catch (FileNotFoundException $e) {
184
-					return false;
185
-				}
186
-			case 'w':
187
-			case 'w+':
188
-			case 'wb':
189
-			case 'wb+':
190
-				$useExisting = false;
191
-			case 'a':
192
-			case 'ab':
193
-			case 'r+':
194
-			case 'a+':
195
-			case 'x':
196
-			case 'x+':
197
-			case 'c':
198
-			case 'c+':
199
-				//emulate these
200
-				if ($useExisting and $this->file_exists($path)) {
201
-					if (!$this->isUpdatable($path)) {
202
-						return false;
203
-					}
204
-					$tmpFile = $this->getCachedFile($path);
205
-				} else {
206
-					if (!$this->isCreatable(dirname($path))) {
207
-						return false;
208
-					}
209
-					$tmpFile = \OCP\Files::tmpFile();
210
-				}
211
-				$source = fopen($tmpFile, $mode);
212
-				return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) {
213
-					$this->flysystem->putStream($fullPath, fopen($tmpFile, 'r'));
214
-					unlink($tmpFile);
215
-				});
216
-		}
217
-		return false;
218
-	}
172
+    /**
173
+     * {@inheritdoc}
174
+     */
175
+    public function fopen($path, $mode) {
176
+        $fullPath = $this->buildPath($path);
177
+        $useExisting = true;
178
+        switch ($mode) {
179
+            case 'r':
180
+            case 'rb':
181
+                try {
182
+                    return $this->flysystem->readStream($fullPath);
183
+                } catch (FileNotFoundException $e) {
184
+                    return false;
185
+                }
186
+            case 'w':
187
+            case 'w+':
188
+            case 'wb':
189
+            case 'wb+':
190
+                $useExisting = false;
191
+            case 'a':
192
+            case 'ab':
193
+            case 'r+':
194
+            case 'a+':
195
+            case 'x':
196
+            case 'x+':
197
+            case 'c':
198
+            case 'c+':
199
+                //emulate these
200
+                if ($useExisting and $this->file_exists($path)) {
201
+                    if (!$this->isUpdatable($path)) {
202
+                        return false;
203
+                    }
204
+                    $tmpFile = $this->getCachedFile($path);
205
+                } else {
206
+                    if (!$this->isCreatable(dirname($path))) {
207
+                        return false;
208
+                    }
209
+                    $tmpFile = \OCP\Files::tmpFile();
210
+                }
211
+                $source = fopen($tmpFile, $mode);
212
+                return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) {
213
+                    $this->flysystem->putStream($fullPath, fopen($tmpFile, 'r'));
214
+                    unlink($tmpFile);
215
+                });
216
+        }
217
+        return false;
218
+    }
219 219
 
220
-	/**
221
-	 * {@inheritdoc}
222
-	 */
223
-	public function touch($path, $mtime = null) {
224
-		if ($this->file_exists($path)) {
225
-			return false;
226
-		} else {
227
-			$this->file_put_contents($path, '');
228
-			return true;
229
-		}
230
-	}
220
+    /**
221
+     * {@inheritdoc}
222
+     */
223
+    public function touch($path, $mtime = null) {
224
+        if ($this->file_exists($path)) {
225
+            return false;
226
+        } else {
227
+            $this->file_put_contents($path, '');
228
+            return true;
229
+        }
230
+    }
231 231
 
232
-	/**
233
-	 * {@inheritdoc}
234
-	 */
235
-	public function stat($path) {
236
-		$info = $this->flysystem->getWithMetadata($this->buildPath($path), ['timestamp', 'size']);
237
-		return [
238
-			'mtime' => $info['timestamp'],
239
-			'size' => $info['size']
240
-		];
241
-	}
232
+    /**
233
+     * {@inheritdoc}
234
+     */
235
+    public function stat($path) {
236
+        $info = $this->flysystem->getWithMetadata($this->buildPath($path), ['timestamp', 'size']);
237
+        return [
238
+            'mtime' => $info['timestamp'],
239
+            'size' => $info['size']
240
+        ];
241
+    }
242 242
 
243
-	/**
244
-	 * {@inheritdoc}
245
-	 */
246
-	public function filetype($path) {
247
-		if ($path === '' or $path === '/' or $path === '.') {
248
-			return 'dir';
249
-		}
250
-		try {
251
-			$info = $this->flysystem->getMetadata($this->buildPath($path));
252
-		} catch (FileNotFoundException $e) {
253
-			return false;
254
-		}
255
-		return $info['type'];
256
-	}
243
+    /**
244
+     * {@inheritdoc}
245
+     */
246
+    public function filetype($path) {
247
+        if ($path === '' or $path === '/' or $path === '.') {
248
+            return 'dir';
249
+        }
250
+        try {
251
+            $info = $this->flysystem->getMetadata($this->buildPath($path));
252
+        } catch (FileNotFoundException $e) {
253
+            return false;
254
+        }
255
+        return $info['type'];
256
+    }
257 257
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -56,7 +56,7 @@  discard block
 block discarded – undo
56 56
 	}
57 57
 
58 58
 	protected function buildPath($path) {
59
-		$fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path);
59
+		$fullPath = \OC\Files\Filesystem::normalizePath($this->root.'/'.$path);
60 60
 		return ltrim($fullPath, '/');
61 61
 	}
62 62
 
@@ -163,7 +163,7 @@  discard block
 block discarded – undo
163 163
 		} catch (FileNotFoundException $e) {
164 164
 			return false;
165 165
 		}
166
-		$names = array_map(function ($object) {
166
+		$names = array_map(function($object) {
167 167
 			return $object['basename'];
168 168
 		}, $content);
169 169
 		return IteratorDirectory::wrap($names);
@@ -209,7 +209,7 @@  discard block
 block discarded – undo
209 209
 					$tmpFile = \OCP\Files::tmpFile();
210 210
 				}
211 211
 				$source = fopen($tmpFile, $mode);
212
-				return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) {
212
+				return CallbackWrapper::wrap($source, null, null, function() use ($tmpFile, $fullPath) {
213 213
 					$this->flysystem->putStream($fullPath, fopen($tmpFile, 'r'));
214 214
 					unlink($tmpFile);
215 215
 				});
Please login to merge, or discard this patch.
lib/private/Files/Storage/Wrapper/Quota.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -159,7 +159,7 @@
 block discarded – undo
159 159
 	 * Checks whether the given path is a part file
160 160
 	 *
161 161
 	 * @param string $path Path that may identify a .part file
162
-	 * @return string File path without .part extension
162
+	 * @return boolean File path without .part extension
163 163
 	 * @note this is needed for reusing keys
164 164
 	 */
165 165
 	private function isPartFile($path) {
Please login to merge, or discard this patch.
Indentation   +168 added lines, -168 removed lines patch added patch discarded remove patch
@@ -31,172 +31,172 @@
 block discarded – undo
31 31
 
32 32
 class Quota extends Wrapper {
33 33
 
34
-	/**
35
-	 * @var int $quota
36
-	 */
37
-	protected $quota;
38
-
39
-	/**
40
-	 * @var string $sizeRoot
41
-	 */
42
-	protected $sizeRoot;
43
-
44
-	/**
45
-	 * @param array $parameters
46
-	 */
47
-	public function __construct($parameters) {
48
-		$this->storage = $parameters['storage'];
49
-		$this->quota = $parameters['quota'];
50
-		$this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : '';
51
-	}
52
-
53
-	/**
54
-	 * @return int quota value
55
-	 */
56
-	public function getQuota() {
57
-		return $this->quota;
58
-	}
59
-
60
-	/**
61
-	 * @param string $path
62
-	 * @param \OC\Files\Storage\Storage $storage
63
-	 */
64
-	protected function getSize($path, $storage = null) {
65
-		if (is_null($storage)) {
66
-			$cache = $this->getCache();
67
-		} else {
68
-			$cache = $storage->getCache();
69
-		}
70
-		$data = $cache->get($path);
71
-		if ($data instanceof ICacheEntry and isset($data['size'])) {
72
-			return $data['size'];
73
-		} else {
74
-			return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
75
-		}
76
-	}
77
-
78
-	/**
79
-	 * Get free space as limited by the quota
80
-	 *
81
-	 * @param string $path
82
-	 * @return int
83
-	 */
84
-	public function free_space($path) {
85
-		if ($this->quota < 0) {
86
-			return $this->storage->free_space($path);
87
-		} else {
88
-			$used = $this->getSize($this->sizeRoot);
89
-			if ($used < 0) {
90
-				return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
91
-			} else {
92
-				$free = $this->storage->free_space($path);
93
-				$quotaFree = max($this->quota - $used, 0);
94
-				// if free space is known
95
-				if ($free >= 0) {
96
-					$free = min($free, $quotaFree);
97
-				} else {
98
-					$free = $quotaFree;
99
-				}
100
-				return $free;
101
-			}
102
-		}
103
-	}
104
-
105
-	/**
106
-	 * see http://php.net/manual/en/function.file_put_contents.php
107
-	 *
108
-	 * @param string $path
109
-	 * @param string $data
110
-	 * @return bool
111
-	 */
112
-	public function file_put_contents($path, $data) {
113
-		$free = $this->free_space('');
114
-		if ($free < 0 or strlen($data) < $free) {
115
-			return $this->storage->file_put_contents($path, $data);
116
-		} else {
117
-			return false;
118
-		}
119
-	}
120
-
121
-	/**
122
-	 * see http://php.net/manual/en/function.copy.php
123
-	 *
124
-	 * @param string $source
125
-	 * @param string $target
126
-	 * @return bool
127
-	 */
128
-	public function copy($source, $target) {
129
-		$free = $this->free_space('');
130
-		if ($free < 0 or $this->getSize($source) < $free) {
131
-			return $this->storage->copy($source, $target);
132
-		} else {
133
-			return false;
134
-		}
135
-	}
136
-
137
-	/**
138
-	 * see http://php.net/manual/en/function.fopen.php
139
-	 *
140
-	 * @param string $path
141
-	 * @param string $mode
142
-	 * @return resource
143
-	 */
144
-	public function fopen($path, $mode) {
145
-		$source = $this->storage->fopen($path, $mode);
146
-
147
-		// don't apply quota for part files
148
-		if (!$this->isPartFile($path)) {
149
-			$free = $this->free_space('');
150
-			if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
151
-				// only apply quota for files, not metadata, trash or others
152
-				if (strpos(ltrim($path, '/'), 'files/') === 0) {
153
-					return \OC\Files\Stream\Quota::wrap($source, $free);
154
-				}
155
-			}
156
-		}
157
-		return $source;
158
-	}
159
-
160
-	/**
161
-	 * Checks whether the given path is a part file
162
-	 *
163
-	 * @param string $path Path that may identify a .part file
164
-	 * @return string File path without .part extension
165
-	 * @note this is needed for reusing keys
166
-	 */
167
-	private function isPartFile($path) {
168
-		$extension = pathinfo($path, PATHINFO_EXTENSION);
169
-
170
-		return ($extension === 'part');
171
-	}
172
-
173
-	/**
174
-	 * @param IStorage $sourceStorage
175
-	 * @param string $sourceInternalPath
176
-	 * @param string $targetInternalPath
177
-	 * @return bool
178
-	 */
179
-	public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
180
-		$free = $this->free_space('');
181
-		if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
182
-			return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
183
-		} else {
184
-			return false;
185
-		}
186
-	}
187
-
188
-	/**
189
-	 * @param IStorage $sourceStorage
190
-	 * @param string $sourceInternalPath
191
-	 * @param string $targetInternalPath
192
-	 * @return bool
193
-	 */
194
-	public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
195
-		$free = $this->free_space('');
196
-		if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
197
-			return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
198
-		} else {
199
-			return false;
200
-		}
201
-	}
34
+    /**
35
+     * @var int $quota
36
+     */
37
+    protected $quota;
38
+
39
+    /**
40
+     * @var string $sizeRoot
41
+     */
42
+    protected $sizeRoot;
43
+
44
+    /**
45
+     * @param array $parameters
46
+     */
47
+    public function __construct($parameters) {
48
+        $this->storage = $parameters['storage'];
49
+        $this->quota = $parameters['quota'];
50
+        $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : '';
51
+    }
52
+
53
+    /**
54
+     * @return int quota value
55
+     */
56
+    public function getQuota() {
57
+        return $this->quota;
58
+    }
59
+
60
+    /**
61
+     * @param string $path
62
+     * @param \OC\Files\Storage\Storage $storage
63
+     */
64
+    protected function getSize($path, $storage = null) {
65
+        if (is_null($storage)) {
66
+            $cache = $this->getCache();
67
+        } else {
68
+            $cache = $storage->getCache();
69
+        }
70
+        $data = $cache->get($path);
71
+        if ($data instanceof ICacheEntry and isset($data['size'])) {
72
+            return $data['size'];
73
+        } else {
74
+            return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
75
+        }
76
+    }
77
+
78
+    /**
79
+     * Get free space as limited by the quota
80
+     *
81
+     * @param string $path
82
+     * @return int
83
+     */
84
+    public function free_space($path) {
85
+        if ($this->quota < 0) {
86
+            return $this->storage->free_space($path);
87
+        } else {
88
+            $used = $this->getSize($this->sizeRoot);
89
+            if ($used < 0) {
90
+                return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
91
+            } else {
92
+                $free = $this->storage->free_space($path);
93
+                $quotaFree = max($this->quota - $used, 0);
94
+                // if free space is known
95
+                if ($free >= 0) {
96
+                    $free = min($free, $quotaFree);
97
+                } else {
98
+                    $free = $quotaFree;
99
+                }
100
+                return $free;
101
+            }
102
+        }
103
+    }
104
+
105
+    /**
106
+     * see http://php.net/manual/en/function.file_put_contents.php
107
+     *
108
+     * @param string $path
109
+     * @param string $data
110
+     * @return bool
111
+     */
112
+    public function file_put_contents($path, $data) {
113
+        $free = $this->free_space('');
114
+        if ($free < 0 or strlen($data) < $free) {
115
+            return $this->storage->file_put_contents($path, $data);
116
+        } else {
117
+            return false;
118
+        }
119
+    }
120
+
121
+    /**
122
+     * see http://php.net/manual/en/function.copy.php
123
+     *
124
+     * @param string $source
125
+     * @param string $target
126
+     * @return bool
127
+     */
128
+    public function copy($source, $target) {
129
+        $free = $this->free_space('');
130
+        if ($free < 0 or $this->getSize($source) < $free) {
131
+            return $this->storage->copy($source, $target);
132
+        } else {
133
+            return false;
134
+        }
135
+    }
136
+
137
+    /**
138
+     * see http://php.net/manual/en/function.fopen.php
139
+     *
140
+     * @param string $path
141
+     * @param string $mode
142
+     * @return resource
143
+     */
144
+    public function fopen($path, $mode) {
145
+        $source = $this->storage->fopen($path, $mode);
146
+
147
+        // don't apply quota for part files
148
+        if (!$this->isPartFile($path)) {
149
+            $free = $this->free_space('');
150
+            if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
151
+                // only apply quota for files, not metadata, trash or others
152
+                if (strpos(ltrim($path, '/'), 'files/') === 0) {
153
+                    return \OC\Files\Stream\Quota::wrap($source, $free);
154
+                }
155
+            }
156
+        }
157
+        return $source;
158
+    }
159
+
160
+    /**
161
+     * Checks whether the given path is a part file
162
+     *
163
+     * @param string $path Path that may identify a .part file
164
+     * @return string File path without .part extension
165
+     * @note this is needed for reusing keys
166
+     */
167
+    private function isPartFile($path) {
168
+        $extension = pathinfo($path, PATHINFO_EXTENSION);
169
+
170
+        return ($extension === 'part');
171
+    }
172
+
173
+    /**
174
+     * @param IStorage $sourceStorage
175
+     * @param string $sourceInternalPath
176
+     * @param string $targetInternalPath
177
+     * @return bool
178
+     */
179
+    public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
180
+        $free = $this->free_space('');
181
+        if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
182
+            return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
183
+        } else {
184
+            return false;
185
+        }
186
+    }
187
+
188
+    /**
189
+     * @param IStorage $sourceStorage
190
+     * @param string $sourceInternalPath
191
+     * @param string $targetInternalPath
192
+     * @return bool
193
+     */
194
+    public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
195
+        $free = $this->free_space('');
196
+        if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
197
+            return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
198
+        } else {
199
+            return false;
200
+        }
201
+    }
202 202
 }
Please login to merge, or discard this patch.
lib/private/L10N/L10N.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -176,7 +176,7 @@
 block discarded – undo
176 176
 	 * Returns an associative array with all translations
177 177
 	 *
178 178
 	 * Called by \OC_L10N_String
179
-	 * @return array
179
+	 * @return string[]
180 180
 	 */
181 181
 	public function getTranslations() {
182 182
 		return $this->translations;
Please login to merge, or discard this patch.
Indentation   +190 added lines, -190 removed lines patch added patch discarded remove patch
@@ -29,194 +29,194 @@
 block discarded – undo
29 29
 
30 30
 class L10N implements IL10N {
31 31
 
32
-	/** @var IFactory */
33
-	protected $factory;
34
-
35
-	/** @var string App of this object */
36
-	protected $app;
37
-
38
-	/** @var string Language of this object */
39
-	protected $lang;
40
-
41
-	/** @var string Plural forms (string) */
42
-	private $pluralFormString = 'nplurals=2; plural=(n != 1);';
43
-
44
-	/** @var string Plural forms (function) */
45
-	private $pluralFormFunction = null;
46
-
47
-	/** @var string[] */
48
-	private $translations = [];
49
-
50
-	/**
51
-	 * @param IFactory $factory
52
-	 * @param string $app
53
-	 * @param string $lang
54
-	 * @param array $files
55
-	 */
56
-	public function __construct(IFactory $factory, $app, $lang, array $files) {
57
-		$this->factory = $factory;
58
-		$this->app = $app;
59
-		$this->lang = $lang;
60
-
61
-		$this->translations = [];
62
-		foreach ($files as $languageFile) {
63
-			$this->load($languageFile);
64
-		}
65
-	}
66
-
67
-	/**
68
-	 * The code (en, de, ...) of the language that is used for this instance
69
-	 *
70
-	 * @return string language
71
-	 */
72
-	public function getLanguageCode() {
73
-		return $this->lang;
74
-	}
75
-
76
-	/**
77
-	 * Translating
78
-	 * @param string $text The text we need a translation for
79
-	 * @param array $parameters default:array() Parameters for sprintf
80
-	 * @return string Translation or the same text
81
-	 *
82
-	 * Returns the translation. If no translation is found, $text will be
83
-	 * returned.
84
-	 */
85
-	public function t($text, $parameters = array()) {
86
-		return (string) new L10NString($this, $text, $parameters);
87
-	}
88
-
89
-	/**
90
-	 * Translating
91
-	 * @param string $text_singular the string to translate for exactly one object
92
-	 * @param string $text_plural the string to translate for n objects
93
-	 * @param integer $count Number of objects
94
-	 * @param array $parameters default:array() Parameters for sprintf
95
-	 * @return string Translation or the same text
96
-	 *
97
-	 * Returns the translation. If no translation is found, $text will be
98
-	 * returned. %n will be replaced with the number of objects.
99
-	 *
100
-	 * The correct plural is determined by the plural_forms-function
101
-	 * provided by the po file.
102
-	 *
103
-	 */
104
-	public function n($text_singular, $text_plural, $count, $parameters = array()) {
105
-		$identifier = "_${text_singular}_::_${text_plural}_";
106
-		if (isset($this->translations[$identifier])) {
107
-			return (string) new L10NString($this, $identifier, $parameters, $count);
108
-		} else {
109
-			if ($count === 1) {
110
-				return (string) new L10NString($this, $text_singular, $parameters, $count);
111
-			} else {
112
-				return (string) new L10NString($this, $text_plural, $parameters, $count);
113
-			}
114
-		}
115
-	}
116
-
117
-	/**
118
-	 * Localization
119
-	 * @param string $type Type of localization
120
-	 * @param \DateTime|int|string $data parameters for this localization
121
-	 * @param array $options
122
-	 * @return string|int|false
123
-	 *
124
-	 * Returns the localized data.
125
-	 *
126
-	 * Implemented types:
127
-	 *  - date
128
-	 *    - Creates a date
129
-	 *    - params: timestamp (int/string)
130
-	 *  - datetime
131
-	 *    - Creates date and time
132
-	 *    - params: timestamp (int/string)
133
-	 *  - time
134
-	 *    - Creates a time
135
-	 *    - params: timestamp (int/string)
136
-	 *  - firstday: Returns the first day of the week (0 sunday - 6 saturday)
137
-	 *  - jsdate: Returns the short JS date format
138
-	 */
139
-	public function l($type, $data = null, $options = array()) {
140
-		// Use the language of the instance
141
-		$locale = $this->getLanguageCode();
142
-		if ($locale === 'sr@latin') {
143
-			$locale = 'sr_latn';
144
-		}
145
-
146
-		if ($type === 'firstday') {
147
-			return (int) Calendar::getFirstWeekday($locale);
148
-		}
149
-		if ($type === 'jsdate') {
150
-			return (string) Calendar::getDateFormat('short', $locale);
151
-		}
152
-
153
-		$value = new \DateTime();
154
-		if ($data instanceof \DateTime) {
155
-			$value = $data;
156
-		} else if (is_string($data) && !is_numeric($data)) {
157
-			$data = strtotime($data);
158
-			$value->setTimestamp($data);
159
-		} else if ($data !== null) {
160
-			$value->setTimestamp($data);
161
-		}
162
-
163
-		$options = array_merge(array('width' => 'long'), $options);
164
-		$width = $options['width'];
165
-		switch ($type) {
166
-			case 'date':
167
-				return (string) Calendar::formatDate($value, $width, $locale);
168
-			case 'datetime':
169
-				return (string) Calendar::formatDatetime($value, $width, $locale);
170
-			case 'time':
171
-				return (string) Calendar::formatTime($value, $width, $locale);
172
-			default:
173
-				return false;
174
-		}
175
-	}
176
-
177
-	/**
178
-	 * Returns an associative array with all translations
179
-	 *
180
-	 * Called by \OC_L10N_String
181
-	 * @return array
182
-	 */
183
-	public function getTranslations() {
184
-		return $this->translations;
185
-	}
186
-
187
-	/**
188
-	 * Returnsed function accepts the argument $n
189
-	 *
190
-	 * Called by \OC_L10N_String
191
-	 * @return string the plural form function
192
-	 */
193
-	public function getPluralFormFunction() {
194
-		if (is_null($this->pluralFormFunction)) {
195
-			$lang = $this->getLanguageCode();
196
-			$this->pluralFormFunction = function($n) use ($lang) {
197
-				return PluralizationRules::get($n, $lang);
198
-			};
199
-		}
200
-
201
-		return $this->pluralFormFunction;
202
-	}
203
-
204
-	/**
205
-	 * @param $translationFile
206
-	 * @return bool
207
-	 */
208
-	protected function load($translationFile) {
209
-		$json = json_decode(file_get_contents($translationFile), true);
210
-		if (!is_array($json)) {
211
-			$jsonError = json_last_error();
212
-			\OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
213
-			return false;
214
-		}
215
-
216
-		if (!empty($json['pluralForm'])) {
217
-			$this->pluralFormString = $json['pluralForm'];
218
-		}
219
-		$this->translations = array_merge($this->translations, $json['translations']);
220
-		return true;
221
-	}
32
+    /** @var IFactory */
33
+    protected $factory;
34
+
35
+    /** @var string App of this object */
36
+    protected $app;
37
+
38
+    /** @var string Language of this object */
39
+    protected $lang;
40
+
41
+    /** @var string Plural forms (string) */
42
+    private $pluralFormString = 'nplurals=2; plural=(n != 1);';
43
+
44
+    /** @var string Plural forms (function) */
45
+    private $pluralFormFunction = null;
46
+
47
+    /** @var string[] */
48
+    private $translations = [];
49
+
50
+    /**
51
+     * @param IFactory $factory
52
+     * @param string $app
53
+     * @param string $lang
54
+     * @param array $files
55
+     */
56
+    public function __construct(IFactory $factory, $app, $lang, array $files) {
57
+        $this->factory = $factory;
58
+        $this->app = $app;
59
+        $this->lang = $lang;
60
+
61
+        $this->translations = [];
62
+        foreach ($files as $languageFile) {
63
+            $this->load($languageFile);
64
+        }
65
+    }
66
+
67
+    /**
68
+     * The code (en, de, ...) of the language that is used for this instance
69
+     *
70
+     * @return string language
71
+     */
72
+    public function getLanguageCode() {
73
+        return $this->lang;
74
+    }
75
+
76
+    /**
77
+     * Translating
78
+     * @param string $text The text we need a translation for
79
+     * @param array $parameters default:array() Parameters for sprintf
80
+     * @return string Translation or the same text
81
+     *
82
+     * Returns the translation. If no translation is found, $text will be
83
+     * returned.
84
+     */
85
+    public function t($text, $parameters = array()) {
86
+        return (string) new L10NString($this, $text, $parameters);
87
+    }
88
+
89
+    /**
90
+     * Translating
91
+     * @param string $text_singular the string to translate for exactly one object
92
+     * @param string $text_plural the string to translate for n objects
93
+     * @param integer $count Number of objects
94
+     * @param array $parameters default:array() Parameters for sprintf
95
+     * @return string Translation or the same text
96
+     *
97
+     * Returns the translation. If no translation is found, $text will be
98
+     * returned. %n will be replaced with the number of objects.
99
+     *
100
+     * The correct plural is determined by the plural_forms-function
101
+     * provided by the po file.
102
+     *
103
+     */
104
+    public function n($text_singular, $text_plural, $count, $parameters = array()) {
105
+        $identifier = "_${text_singular}_::_${text_plural}_";
106
+        if (isset($this->translations[$identifier])) {
107
+            return (string) new L10NString($this, $identifier, $parameters, $count);
108
+        } else {
109
+            if ($count === 1) {
110
+                return (string) new L10NString($this, $text_singular, $parameters, $count);
111
+            } else {
112
+                return (string) new L10NString($this, $text_plural, $parameters, $count);
113
+            }
114
+        }
115
+    }
116
+
117
+    /**
118
+     * Localization
119
+     * @param string $type Type of localization
120
+     * @param \DateTime|int|string $data parameters for this localization
121
+     * @param array $options
122
+     * @return string|int|false
123
+     *
124
+     * Returns the localized data.
125
+     *
126
+     * Implemented types:
127
+     *  - date
128
+     *    - Creates a date
129
+     *    - params: timestamp (int/string)
130
+     *  - datetime
131
+     *    - Creates date and time
132
+     *    - params: timestamp (int/string)
133
+     *  - time
134
+     *    - Creates a time
135
+     *    - params: timestamp (int/string)
136
+     *  - firstday: Returns the first day of the week (0 sunday - 6 saturday)
137
+     *  - jsdate: Returns the short JS date format
138
+     */
139
+    public function l($type, $data = null, $options = array()) {
140
+        // Use the language of the instance
141
+        $locale = $this->getLanguageCode();
142
+        if ($locale === 'sr@latin') {
143
+            $locale = 'sr_latn';
144
+        }
145
+
146
+        if ($type === 'firstday') {
147
+            return (int) Calendar::getFirstWeekday($locale);
148
+        }
149
+        if ($type === 'jsdate') {
150
+            return (string) Calendar::getDateFormat('short', $locale);
151
+        }
152
+
153
+        $value = new \DateTime();
154
+        if ($data instanceof \DateTime) {
155
+            $value = $data;
156
+        } else if (is_string($data) && !is_numeric($data)) {
157
+            $data = strtotime($data);
158
+            $value->setTimestamp($data);
159
+        } else if ($data !== null) {
160
+            $value->setTimestamp($data);
161
+        }
162
+
163
+        $options = array_merge(array('width' => 'long'), $options);
164
+        $width = $options['width'];
165
+        switch ($type) {
166
+            case 'date':
167
+                return (string) Calendar::formatDate($value, $width, $locale);
168
+            case 'datetime':
169
+                return (string) Calendar::formatDatetime($value, $width, $locale);
170
+            case 'time':
171
+                return (string) Calendar::formatTime($value, $width, $locale);
172
+            default:
173
+                return false;
174
+        }
175
+    }
176
+
177
+    /**
178
+     * Returns an associative array with all translations
179
+     *
180
+     * Called by \OC_L10N_String
181
+     * @return array
182
+     */
183
+    public function getTranslations() {
184
+        return $this->translations;
185
+    }
186
+
187
+    /**
188
+     * Returnsed function accepts the argument $n
189
+     *
190
+     * Called by \OC_L10N_String
191
+     * @return string the plural form function
192
+     */
193
+    public function getPluralFormFunction() {
194
+        if (is_null($this->pluralFormFunction)) {
195
+            $lang = $this->getLanguageCode();
196
+            $this->pluralFormFunction = function($n) use ($lang) {
197
+                return PluralizationRules::get($n, $lang);
198
+            };
199
+        }
200
+
201
+        return $this->pluralFormFunction;
202
+    }
203
+
204
+    /**
205
+     * @param $translationFile
206
+     * @return bool
207
+     */
208
+    protected function load($translationFile) {
209
+        $json = json_decode(file_get_contents($translationFile), true);
210
+        if (!is_array($json)) {
211
+            $jsonError = json_last_error();
212
+            \OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
213
+            return false;
214
+        }
215
+
216
+        if (!empty($json['pluralForm'])) {
217
+            $this->pluralFormString = $json['pluralForm'];
218
+        }
219
+        $this->translations = array_merge($this->translations, $json['translations']);
220
+        return true;
221
+    }
222 222
 }
Please login to merge, or discard this patch.
lib/private/legacy/api.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -331,7 +331,7 @@
 block discarded – undo
331 331
 
332 332
 	/**
333 333
 	 * http basic auth
334
-	 * @return string|false (username, or false on failure)
334
+	 * @return string (username, or false on failure)
335 335
 	 */
336 336
 	private static function loginUser() {
337 337
 		if(self::$isLoggedIn === true) {
Please login to merge, or discard this patch.
Indentation   +459 added lines, -459 removed lines patch added patch discarded remove patch
@@ -37,463 +37,463 @@
 block discarded – undo
37 37
 
38 38
 class OC_API {
39 39
 
40
-	/**
41
-	 * API authentication levels
42
-	 */
43
-
44
-	/** @deprecated Use \OCP\API::GUEST_AUTH instead */
45
-	const GUEST_AUTH = 0;
46
-
47
-	/** @deprecated Use \OCP\API::USER_AUTH instead */
48
-	const USER_AUTH = 1;
49
-
50
-	/** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */
51
-	const SUBADMIN_AUTH = 2;
52
-
53
-	/** @deprecated Use \OCP\API::ADMIN_AUTH instead */
54
-	const ADMIN_AUTH = 3;
55
-
56
-	/**
57
-	 * API Response Codes
58
-	 */
59
-
60
-	/** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */
61
-	const RESPOND_UNAUTHORISED = 997;
62
-
63
-	/** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */
64
-	const RESPOND_SERVER_ERROR = 996;
65
-
66
-	/** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */
67
-	const RESPOND_NOT_FOUND = 998;
68
-
69
-	/** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */
70
-	const RESPOND_UNKNOWN_ERROR = 999;
71
-
72
-	/**
73
-	 * api actions
74
-	 */
75
-	protected static $actions = array();
76
-	private static $logoutRequired = false;
77
-	private static $isLoggedIn = false;
78
-
79
-	/**
80
-	 * registers an api call
81
-	 * @param string $method the http method
82
-	 * @param string $url the url to match
83
-	 * @param callable $action the function to run
84
-	 * @param string $app the id of the app registering the call
85
-	 * @param int $authLevel the level of authentication required for the call
86
-	 * @param array $defaults
87
-	 * @param array $requirements
88
-	 */
89
-	public static function register($method, $url, $action, $app,
90
-				$authLevel = API::USER_AUTH,
91
-				$defaults = array(),
92
-				$requirements = array()) {
93
-		$name = strtolower($method).$url;
94
-		$name = str_replace(array('/', '{', '}'), '_', $name);
95
-		if(!isset(self::$actions[$name])) {
96
-			$oldCollection = OC::$server->getRouter()->getCurrentCollection();
97
-			OC::$server->getRouter()->useCollection('ocs');
98
-			OC::$server->getRouter()->create($name, $url)
99
-				->method($method)
100
-				->defaults($defaults)
101
-				->requirements($requirements)
102
-				->action('OC_API', 'call');
103
-			self::$actions[$name] = array();
104
-			OC::$server->getRouter()->useCollection($oldCollection);
105
-		}
106
-		self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel);
107
-	}
108
-
109
-	/**
110
-	 * handles an api call
111
-	 * @param array $parameters
112
-	 */
113
-	public static function call($parameters) {
114
-		$request = \OC::$server->getRequest();
115
-		$method = $request->getMethod();
116
-
117
-		// Prepare the request variables
118
-		if($method === 'PUT') {
119
-			$parameters['_put'] = $request->getParams();
120
-		} else if($method === 'DELETE') {
121
-			$parameters['_delete'] = $request->getParams();
122
-		}
123
-		$name = $parameters['_route'];
124
-		// Foreach registered action
125
-		$responses = array();
126
-		$appManager = \OC::$server->getAppManager();
127
-		foreach(self::$actions[$name] as $action) {
128
-			// Check authentication and availability
129
-			if(!self::isAuthorised($action)) {
130
-				$responses[] = array(
131
-					'app' => $action['app'],
132
-					'response' => new OC_OCS_Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'),
133
-					'shipped' => $appManager->isShipped($action['app']),
134
-					);
135
-				continue;
136
-			}
137
-			if(!is_callable($action['action'])) {
138
-				$responses[] = array(
139
-					'app' => $action['app'],
140
-					'response' => new OC_OCS_Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'),
141
-					'shipped' => $appManager->isShipped($action['app']),
142
-					);
143
-				continue;
144
-			}
145
-			// Run the action
146
-			$responses[] = array(
147
-				'app' => $action['app'],
148
-				'response' => call_user_func($action['action'], $parameters),
149
-				'shipped' => $appManager->isShipped($action['app']),
150
-				);
151
-		}
152
-		$response = self::mergeResponses($responses);
153
-		$format = self::requestedFormat();
154
-		if (self::$logoutRequired) {
155
-			\OC::$server->getUserSession()->logout();
156
-		}
157
-
158
-		self::respond($response, $format);
159
-	}
160
-
161
-	/**
162
-	 * merge the returned result objects into one response
163
-	 * @param array $responses
164
-	 * @return OC_OCS_Result
165
-	 */
166
-	public static function mergeResponses($responses) {
167
-		// Sort into shipped and third-party
168
-		$shipped = array(
169
-			'succeeded' => array(),
170
-			'failed' => array(),
171
-			);
172
-		$thirdparty = array(
173
-			'succeeded' => array(),
174
-			'failed' => array(),
175
-			);
176
-
177
-		foreach($responses as $response) {
178
-			if($response['shipped'] || ($response['app'] === 'core')) {
179
-				if($response['response']->succeeded()) {
180
-					$shipped['succeeded'][$response['app']] = $response;
181
-				} else {
182
-					$shipped['failed'][$response['app']] = $response;
183
-				}
184
-			} else {
185
-				if($response['response']->succeeded()) {
186
-					$thirdparty['succeeded'][$response['app']] = $response;
187
-				} else {
188
-					$thirdparty['failed'][$response['app']] = $response;
189
-				}
190
-			}
191
-		}
192
-
193
-		// Remove any error responses if there is one shipped response that succeeded
194
-		if(!empty($shipped['failed'])) {
195
-			// Which shipped response do we use if they all failed?
196
-			// They may have failed for different reasons (different status codes)
197
-			// Which response code should we return?
198
-			// Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR
199
-			// Merge failed responses if more than one
200
-			$data = array();
201
-			foreach($shipped['failed'] as $failure) {
202
-				$data = array_merge_recursive($data, $failure['response']->getData());
203
-			}
204
-			$picked = reset($shipped['failed']);
205
-			$code = $picked['response']->getStatusCode();
206
-			$meta = $picked['response']->getMeta();
207
-			$headers = $picked['response']->getHeaders();
208
-			$response = new OC_OCS_Result($data, $code, $meta['message'], $headers);
209
-			return $response;
210
-		} elseif(!empty($shipped['succeeded'])) {
211
-			$responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']);
212
-		} elseif(!empty($thirdparty['failed'])) {
213
-			// Merge failed responses if more than one
214
-			$data = array();
215
-			foreach($thirdparty['failed'] as $failure) {
216
-				$data = array_merge_recursive($data, $failure['response']->getData());
217
-			}
218
-			$picked = reset($thirdparty['failed']);
219
-			$code = $picked['response']->getStatusCode();
220
-			$meta = $picked['response']->getMeta();
221
-			$headers = $picked['response']->getHeaders();
222
-			$response = new OC_OCS_Result($data, $code, $meta['message'], $headers);
223
-			return $response;
224
-		} else {
225
-			$responses = $thirdparty['succeeded'];
226
-		}
227
-		// Merge the successful responses
228
-		$data = [];
229
-		$codes = [];
230
-		$header = [];
231
-
232
-		foreach($responses as $response) {
233
-			if($response['shipped']) {
234
-				$data = array_merge_recursive($response['response']->getData(), $data);
235
-			} else {
236
-				$data = array_merge_recursive($data, $response['response']->getData());
237
-			}
238
-			$header = array_merge_recursive($header, $response['response']->getHeaders());
239
-			$codes[] = ['code' => $response['response']->getStatusCode(),
240
-				'meta' => $response['response']->getMeta()];
241
-		}
242
-
243
-		// Use any non 100 status codes
244
-		$statusCode = 100;
245
-		$statusMessage = null;
246
-		foreach($codes as $code) {
247
-			if($code['code'] != 100) {
248
-				$statusCode = $code['code'];
249
-				$statusMessage = $code['meta']['message'];
250
-				break;
251
-			}
252
-		}
253
-
254
-		return new OC_OCS_Result($data, $statusCode, $statusMessage, $header);
255
-	}
256
-
257
-	/**
258
-	 * authenticate the api call
259
-	 * @param array $action the action details as supplied to OC_API::register()
260
-	 * @return bool
261
-	 */
262
-	private static function isAuthorised($action) {
263
-		$level = $action['authlevel'];
264
-		switch($level) {
265
-			case API::GUEST_AUTH:
266
-				// Anyone can access
267
-				return true;
268
-			case API::USER_AUTH:
269
-				// User required
270
-				return self::loginUser();
271
-			case API::SUBADMIN_AUTH:
272
-				// Check for subadmin
273
-				$user = self::loginUser();
274
-				if(!$user) {
275
-					return false;
276
-				} else {
277
-					$userObject = \OC::$server->getUserSession()->getUser();
278
-					if($userObject === null) {
279
-						return false;
280
-					}
281
-					$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
282
-					$admin = OC_User::isAdminUser($user);
283
-					if($isSubAdmin || $admin) {
284
-						return true;
285
-					} else {
286
-						return false;
287
-					}
288
-				}
289
-			case API::ADMIN_AUTH:
290
-				// Check for admin
291
-				$user = self::loginUser();
292
-				if(!$user) {
293
-					return false;
294
-				} else {
295
-					return OC_User::isAdminUser($user);
296
-				}
297
-			default:
298
-				// oops looks like invalid level supplied
299
-				return false;
300
-		}
301
-	}
302
-
303
-	/**
304
-	 * http basic auth
305
-	 * @return string|false (username, or false on failure)
306
-	 */
307
-	private static function loginUser() {
308
-		if(self::$isLoggedIn === true) {
309
-			return \OC_User::getUser();
310
-		}
311
-
312
-		// reuse existing login
313
-		$loggedIn = \OC::$server->getUserSession()->isLoggedIn();
314
-		if ($loggedIn === true) {
315
-			if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
316
-				// Do not allow access to OCS until the 2FA challenge was solved successfully
317
-				return false;
318
-			}
319
-			$ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false;
320
-			if ($ocsApiRequest) {
321
-
322
-				// initialize the user's filesystem
323
-				\OC_Util::setupFS(\OC_User::getUser());
324
-				self::$isLoggedIn = true;
325
-
326
-				return OC_User::getUser();
327
-			}
328
-			return false;
329
-		}
330
-
331
-		// basic auth - because OC_User::login will create a new session we shall only try to login
332
-		// if user and pass are set
333
-		$userSession = \OC::$server->getUserSession();
334
-		$request = \OC::$server->getRequest();
335
-		try {
336
-			if ($userSession->tryTokenLogin($request)
337
-				|| $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) {
338
-				self::$logoutRequired = true;
339
-			} else {
340
-				return false;
341
-			}
342
-			// initialize the user's filesystem
343
-			\OC_Util::setupFS(\OC_User::getUser());
344
-			self::$isLoggedIn = true;
345
-
346
-			return \OC_User::getUser();
347
-		} catch (\OC\User\LoginException $e) {
348
-			return false;
349
-		}
350
-	}
351
-
352
-	/**
353
-	 * respond to a call
354
-	 * @param OC_OCS_Result $result
355
-	 * @param string $format the format xml|json
356
-	 */
357
-	public static function respond($result, $format='xml') {
358
-		$request = \OC::$server->getRequest();
359
-
360
-		// Send 401 headers if unauthorised
361
-		if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
362
-			// If request comes from JS return dummy auth request
363
-			if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
364
-				header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
365
-			} else {
366
-				header('WWW-Authenticate: Basic realm="Authorisation Required"');
367
-			}
368
-			header('HTTP/1.0 401 Unauthorized');
369
-		}
370
-
371
-		foreach($result->getHeaders() as $name => $value) {
372
-			header($name . ': ' . $value);
373
-		}
374
-
375
-		$meta = $result->getMeta();
376
-		$data = $result->getData();
377
-		if (self::isV2($request)) {
378
-			$statusCode = self::mapStatusCodes($result->getStatusCode());
379
-			if (!is_null($statusCode)) {
380
-				$meta['statuscode'] = $statusCode;
381
-				OC_Response::setStatus($statusCode);
382
-			}
383
-		}
384
-
385
-		self::setContentType($format);
386
-		$body = self::renderResult($format, $meta, $data);
387
-		echo $body;
388
-	}
389
-
390
-	/**
391
-	 * @param XMLWriter $writer
392
-	 */
393
-	private static function toXML($array, $writer) {
394
-		foreach($array as $k => $v) {
395
-			if ($k[0] === '@') {
396
-				$writer->writeAttribute(substr($k, 1), $v);
397
-				continue;
398
-			} else if (is_numeric($k)) {
399
-				$k = 'element';
400
-			}
401
-			if(is_array($v)) {
402
-				$writer->startElement($k);
403
-				self::toXML($v, $writer);
404
-				$writer->endElement();
405
-			} else {
406
-				$writer->writeElement($k, $v);
407
-			}
408
-		}
409
-	}
410
-
411
-	/**
412
-	 * @return string
413
-	 */
414
-	public static function requestedFormat() {
415
-		$formats = array('json', 'xml');
416
-
417
-		$format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml';
418
-		return $format;
419
-	}
420
-
421
-	/**
422
-	 * Based on the requested format the response content type is set
423
-	 * @param string $format
424
-	 */
425
-	public static function setContentType($format = null) {
426
-		$format = is_null($format) ? self::requestedFormat() : $format;
427
-		if ($format === 'xml') {
428
-			header('Content-type: text/xml; charset=UTF-8');
429
-			return;
430
-		}
431
-
432
-		if ($format === 'json') {
433
-			header('Content-Type: application/json; charset=utf-8');
434
-			return;
435
-		}
436
-
437
-		header('Content-Type: application/octet-stream; charset=utf-8');
438
-	}
439
-
440
-	/**
441
-	 * @param \OCP\IRequest $request
442
-	 * @return bool
443
-	 */
444
-	protected static function isV2(\OCP\IRequest $request) {
445
-		$script = $request->getScriptName();
446
-
447
-		return substr($script, -11) === '/ocs/v2.php';
448
-	}
449
-
450
-	/**
451
-	 * @param integer $sc
452
-	 * @return int
453
-	 */
454
-	public static function mapStatusCodes($sc) {
455
-		switch ($sc) {
456
-			case API::RESPOND_NOT_FOUND:
457
-				return Http::STATUS_NOT_FOUND;
458
-			case API::RESPOND_SERVER_ERROR:
459
-				return Http::STATUS_INTERNAL_SERVER_ERROR;
460
-			case API::RESPOND_UNKNOWN_ERROR:
461
-				return Http::STATUS_INTERNAL_SERVER_ERROR;
462
-			case API::RESPOND_UNAUTHORISED:
463
-				// already handled for v1
464
-				return null;
465
-			case 100:
466
-				return Http::STATUS_OK;
467
-		}
468
-		// any 2xx, 4xx and 5xx will be used as is
469
-		if ($sc >= 200 && $sc < 600) {
470
-			return $sc;
471
-		}
472
-
473
-		return Http::STATUS_BAD_REQUEST;
474
-	}
475
-
476
-	/**
477
-	 * @param string $format
478
-	 * @return string
479
-	 */
480
-	public static function renderResult($format, $meta, $data) {
481
-		$response = array(
482
-			'ocs' => array(
483
-				'meta' => $meta,
484
-				'data' => $data,
485
-			),
486
-		);
487
-		if ($format == 'json') {
488
-			return OC_JSON::encode($response);
489
-		}
490
-
491
-		$writer = new XMLWriter();
492
-		$writer->openMemory();
493
-		$writer->setIndent(true);
494
-		$writer->startDocument();
495
-		self::toXML($response, $writer);
496
-		$writer->endDocument();
497
-		return $writer->outputMemory(true);
498
-	}
40
+    /**
41
+     * API authentication levels
42
+     */
43
+
44
+    /** @deprecated Use \OCP\API::GUEST_AUTH instead */
45
+    const GUEST_AUTH = 0;
46
+
47
+    /** @deprecated Use \OCP\API::USER_AUTH instead */
48
+    const USER_AUTH = 1;
49
+
50
+    /** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */
51
+    const SUBADMIN_AUTH = 2;
52
+
53
+    /** @deprecated Use \OCP\API::ADMIN_AUTH instead */
54
+    const ADMIN_AUTH = 3;
55
+
56
+    /**
57
+     * API Response Codes
58
+     */
59
+
60
+    /** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */
61
+    const RESPOND_UNAUTHORISED = 997;
62
+
63
+    /** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */
64
+    const RESPOND_SERVER_ERROR = 996;
65
+
66
+    /** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */
67
+    const RESPOND_NOT_FOUND = 998;
68
+
69
+    /** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */
70
+    const RESPOND_UNKNOWN_ERROR = 999;
71
+
72
+    /**
73
+     * api actions
74
+     */
75
+    protected static $actions = array();
76
+    private static $logoutRequired = false;
77
+    private static $isLoggedIn = false;
78
+
79
+    /**
80
+     * registers an api call
81
+     * @param string $method the http method
82
+     * @param string $url the url to match
83
+     * @param callable $action the function to run
84
+     * @param string $app the id of the app registering the call
85
+     * @param int $authLevel the level of authentication required for the call
86
+     * @param array $defaults
87
+     * @param array $requirements
88
+     */
89
+    public static function register($method, $url, $action, $app,
90
+                $authLevel = API::USER_AUTH,
91
+                $defaults = array(),
92
+                $requirements = array()) {
93
+        $name = strtolower($method).$url;
94
+        $name = str_replace(array('/', '{', '}'), '_', $name);
95
+        if(!isset(self::$actions[$name])) {
96
+            $oldCollection = OC::$server->getRouter()->getCurrentCollection();
97
+            OC::$server->getRouter()->useCollection('ocs');
98
+            OC::$server->getRouter()->create($name, $url)
99
+                ->method($method)
100
+                ->defaults($defaults)
101
+                ->requirements($requirements)
102
+                ->action('OC_API', 'call');
103
+            self::$actions[$name] = array();
104
+            OC::$server->getRouter()->useCollection($oldCollection);
105
+        }
106
+        self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel);
107
+    }
108
+
109
+    /**
110
+     * handles an api call
111
+     * @param array $parameters
112
+     */
113
+    public static function call($parameters) {
114
+        $request = \OC::$server->getRequest();
115
+        $method = $request->getMethod();
116
+
117
+        // Prepare the request variables
118
+        if($method === 'PUT') {
119
+            $parameters['_put'] = $request->getParams();
120
+        } else if($method === 'DELETE') {
121
+            $parameters['_delete'] = $request->getParams();
122
+        }
123
+        $name = $parameters['_route'];
124
+        // Foreach registered action
125
+        $responses = array();
126
+        $appManager = \OC::$server->getAppManager();
127
+        foreach(self::$actions[$name] as $action) {
128
+            // Check authentication and availability
129
+            if(!self::isAuthorised($action)) {
130
+                $responses[] = array(
131
+                    'app' => $action['app'],
132
+                    'response' => new OC_OCS_Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'),
133
+                    'shipped' => $appManager->isShipped($action['app']),
134
+                    );
135
+                continue;
136
+            }
137
+            if(!is_callable($action['action'])) {
138
+                $responses[] = array(
139
+                    'app' => $action['app'],
140
+                    'response' => new OC_OCS_Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'),
141
+                    'shipped' => $appManager->isShipped($action['app']),
142
+                    );
143
+                continue;
144
+            }
145
+            // Run the action
146
+            $responses[] = array(
147
+                'app' => $action['app'],
148
+                'response' => call_user_func($action['action'], $parameters),
149
+                'shipped' => $appManager->isShipped($action['app']),
150
+                );
151
+        }
152
+        $response = self::mergeResponses($responses);
153
+        $format = self::requestedFormat();
154
+        if (self::$logoutRequired) {
155
+            \OC::$server->getUserSession()->logout();
156
+        }
157
+
158
+        self::respond($response, $format);
159
+    }
160
+
161
+    /**
162
+     * merge the returned result objects into one response
163
+     * @param array $responses
164
+     * @return OC_OCS_Result
165
+     */
166
+    public static function mergeResponses($responses) {
167
+        // Sort into shipped and third-party
168
+        $shipped = array(
169
+            'succeeded' => array(),
170
+            'failed' => array(),
171
+            );
172
+        $thirdparty = array(
173
+            'succeeded' => array(),
174
+            'failed' => array(),
175
+            );
176
+
177
+        foreach($responses as $response) {
178
+            if($response['shipped'] || ($response['app'] === 'core')) {
179
+                if($response['response']->succeeded()) {
180
+                    $shipped['succeeded'][$response['app']] = $response;
181
+                } else {
182
+                    $shipped['failed'][$response['app']] = $response;
183
+                }
184
+            } else {
185
+                if($response['response']->succeeded()) {
186
+                    $thirdparty['succeeded'][$response['app']] = $response;
187
+                } else {
188
+                    $thirdparty['failed'][$response['app']] = $response;
189
+                }
190
+            }
191
+        }
192
+
193
+        // Remove any error responses if there is one shipped response that succeeded
194
+        if(!empty($shipped['failed'])) {
195
+            // Which shipped response do we use if they all failed?
196
+            // They may have failed for different reasons (different status codes)
197
+            // Which response code should we return?
198
+            // Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR
199
+            // Merge failed responses if more than one
200
+            $data = array();
201
+            foreach($shipped['failed'] as $failure) {
202
+                $data = array_merge_recursive($data, $failure['response']->getData());
203
+            }
204
+            $picked = reset($shipped['failed']);
205
+            $code = $picked['response']->getStatusCode();
206
+            $meta = $picked['response']->getMeta();
207
+            $headers = $picked['response']->getHeaders();
208
+            $response = new OC_OCS_Result($data, $code, $meta['message'], $headers);
209
+            return $response;
210
+        } elseif(!empty($shipped['succeeded'])) {
211
+            $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']);
212
+        } elseif(!empty($thirdparty['failed'])) {
213
+            // Merge failed responses if more than one
214
+            $data = array();
215
+            foreach($thirdparty['failed'] as $failure) {
216
+                $data = array_merge_recursive($data, $failure['response']->getData());
217
+            }
218
+            $picked = reset($thirdparty['failed']);
219
+            $code = $picked['response']->getStatusCode();
220
+            $meta = $picked['response']->getMeta();
221
+            $headers = $picked['response']->getHeaders();
222
+            $response = new OC_OCS_Result($data, $code, $meta['message'], $headers);
223
+            return $response;
224
+        } else {
225
+            $responses = $thirdparty['succeeded'];
226
+        }
227
+        // Merge the successful responses
228
+        $data = [];
229
+        $codes = [];
230
+        $header = [];
231
+
232
+        foreach($responses as $response) {
233
+            if($response['shipped']) {
234
+                $data = array_merge_recursive($response['response']->getData(), $data);
235
+            } else {
236
+                $data = array_merge_recursive($data, $response['response']->getData());
237
+            }
238
+            $header = array_merge_recursive($header, $response['response']->getHeaders());
239
+            $codes[] = ['code' => $response['response']->getStatusCode(),
240
+                'meta' => $response['response']->getMeta()];
241
+        }
242
+
243
+        // Use any non 100 status codes
244
+        $statusCode = 100;
245
+        $statusMessage = null;
246
+        foreach($codes as $code) {
247
+            if($code['code'] != 100) {
248
+                $statusCode = $code['code'];
249
+                $statusMessage = $code['meta']['message'];
250
+                break;
251
+            }
252
+        }
253
+
254
+        return new OC_OCS_Result($data, $statusCode, $statusMessage, $header);
255
+    }
256
+
257
+    /**
258
+     * authenticate the api call
259
+     * @param array $action the action details as supplied to OC_API::register()
260
+     * @return bool
261
+     */
262
+    private static function isAuthorised($action) {
263
+        $level = $action['authlevel'];
264
+        switch($level) {
265
+            case API::GUEST_AUTH:
266
+                // Anyone can access
267
+                return true;
268
+            case API::USER_AUTH:
269
+                // User required
270
+                return self::loginUser();
271
+            case API::SUBADMIN_AUTH:
272
+                // Check for subadmin
273
+                $user = self::loginUser();
274
+                if(!$user) {
275
+                    return false;
276
+                } else {
277
+                    $userObject = \OC::$server->getUserSession()->getUser();
278
+                    if($userObject === null) {
279
+                        return false;
280
+                    }
281
+                    $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
282
+                    $admin = OC_User::isAdminUser($user);
283
+                    if($isSubAdmin || $admin) {
284
+                        return true;
285
+                    } else {
286
+                        return false;
287
+                    }
288
+                }
289
+            case API::ADMIN_AUTH:
290
+                // Check for admin
291
+                $user = self::loginUser();
292
+                if(!$user) {
293
+                    return false;
294
+                } else {
295
+                    return OC_User::isAdminUser($user);
296
+                }
297
+            default:
298
+                // oops looks like invalid level supplied
299
+                return false;
300
+        }
301
+    }
302
+
303
+    /**
304
+     * http basic auth
305
+     * @return string|false (username, or false on failure)
306
+     */
307
+    private static function loginUser() {
308
+        if(self::$isLoggedIn === true) {
309
+            return \OC_User::getUser();
310
+        }
311
+
312
+        // reuse existing login
313
+        $loggedIn = \OC::$server->getUserSession()->isLoggedIn();
314
+        if ($loggedIn === true) {
315
+            if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
316
+                // Do not allow access to OCS until the 2FA challenge was solved successfully
317
+                return false;
318
+            }
319
+            $ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false;
320
+            if ($ocsApiRequest) {
321
+
322
+                // initialize the user's filesystem
323
+                \OC_Util::setupFS(\OC_User::getUser());
324
+                self::$isLoggedIn = true;
325
+
326
+                return OC_User::getUser();
327
+            }
328
+            return false;
329
+        }
330
+
331
+        // basic auth - because OC_User::login will create a new session we shall only try to login
332
+        // if user and pass are set
333
+        $userSession = \OC::$server->getUserSession();
334
+        $request = \OC::$server->getRequest();
335
+        try {
336
+            if ($userSession->tryTokenLogin($request)
337
+                || $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) {
338
+                self::$logoutRequired = true;
339
+            } else {
340
+                return false;
341
+            }
342
+            // initialize the user's filesystem
343
+            \OC_Util::setupFS(\OC_User::getUser());
344
+            self::$isLoggedIn = true;
345
+
346
+            return \OC_User::getUser();
347
+        } catch (\OC\User\LoginException $e) {
348
+            return false;
349
+        }
350
+    }
351
+
352
+    /**
353
+     * respond to a call
354
+     * @param OC_OCS_Result $result
355
+     * @param string $format the format xml|json
356
+     */
357
+    public static function respond($result, $format='xml') {
358
+        $request = \OC::$server->getRequest();
359
+
360
+        // Send 401 headers if unauthorised
361
+        if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
362
+            // If request comes from JS return dummy auth request
363
+            if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
364
+                header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
365
+            } else {
366
+                header('WWW-Authenticate: Basic realm="Authorisation Required"');
367
+            }
368
+            header('HTTP/1.0 401 Unauthorized');
369
+        }
370
+
371
+        foreach($result->getHeaders() as $name => $value) {
372
+            header($name . ': ' . $value);
373
+        }
374
+
375
+        $meta = $result->getMeta();
376
+        $data = $result->getData();
377
+        if (self::isV2($request)) {
378
+            $statusCode = self::mapStatusCodes($result->getStatusCode());
379
+            if (!is_null($statusCode)) {
380
+                $meta['statuscode'] = $statusCode;
381
+                OC_Response::setStatus($statusCode);
382
+            }
383
+        }
384
+
385
+        self::setContentType($format);
386
+        $body = self::renderResult($format, $meta, $data);
387
+        echo $body;
388
+    }
389
+
390
+    /**
391
+     * @param XMLWriter $writer
392
+     */
393
+    private static function toXML($array, $writer) {
394
+        foreach($array as $k => $v) {
395
+            if ($k[0] === '@') {
396
+                $writer->writeAttribute(substr($k, 1), $v);
397
+                continue;
398
+            } else if (is_numeric($k)) {
399
+                $k = 'element';
400
+            }
401
+            if(is_array($v)) {
402
+                $writer->startElement($k);
403
+                self::toXML($v, $writer);
404
+                $writer->endElement();
405
+            } else {
406
+                $writer->writeElement($k, $v);
407
+            }
408
+        }
409
+    }
410
+
411
+    /**
412
+     * @return string
413
+     */
414
+    public static function requestedFormat() {
415
+        $formats = array('json', 'xml');
416
+
417
+        $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml';
418
+        return $format;
419
+    }
420
+
421
+    /**
422
+     * Based on the requested format the response content type is set
423
+     * @param string $format
424
+     */
425
+    public static function setContentType($format = null) {
426
+        $format = is_null($format) ? self::requestedFormat() : $format;
427
+        if ($format === 'xml') {
428
+            header('Content-type: text/xml; charset=UTF-8');
429
+            return;
430
+        }
431
+
432
+        if ($format === 'json') {
433
+            header('Content-Type: application/json; charset=utf-8');
434
+            return;
435
+        }
436
+
437
+        header('Content-Type: application/octet-stream; charset=utf-8');
438
+    }
439
+
440
+    /**
441
+     * @param \OCP\IRequest $request
442
+     * @return bool
443
+     */
444
+    protected static function isV2(\OCP\IRequest $request) {
445
+        $script = $request->getScriptName();
446
+
447
+        return substr($script, -11) === '/ocs/v2.php';
448
+    }
449
+
450
+    /**
451
+     * @param integer $sc
452
+     * @return int
453
+     */
454
+    public static function mapStatusCodes($sc) {
455
+        switch ($sc) {
456
+            case API::RESPOND_NOT_FOUND:
457
+                return Http::STATUS_NOT_FOUND;
458
+            case API::RESPOND_SERVER_ERROR:
459
+                return Http::STATUS_INTERNAL_SERVER_ERROR;
460
+            case API::RESPOND_UNKNOWN_ERROR:
461
+                return Http::STATUS_INTERNAL_SERVER_ERROR;
462
+            case API::RESPOND_UNAUTHORISED:
463
+                // already handled for v1
464
+                return null;
465
+            case 100:
466
+                return Http::STATUS_OK;
467
+        }
468
+        // any 2xx, 4xx and 5xx will be used as is
469
+        if ($sc >= 200 && $sc < 600) {
470
+            return $sc;
471
+        }
472
+
473
+        return Http::STATUS_BAD_REQUEST;
474
+    }
475
+
476
+    /**
477
+     * @param string $format
478
+     * @return string
479
+     */
480
+    public static function renderResult($format, $meta, $data) {
481
+        $response = array(
482
+            'ocs' => array(
483
+                'meta' => $meta,
484
+                'data' => $data,
485
+            ),
486
+        );
487
+        if ($format == 'json') {
488
+            return OC_JSON::encode($response);
489
+        }
490
+
491
+        $writer = new XMLWriter();
492
+        $writer->openMemory();
493
+        $writer->setIndent(true);
494
+        $writer->startDocument();
495
+        self::toXML($response, $writer);
496
+        $writer->endDocument();
497
+        return $writer->outputMemory(true);
498
+    }
499 499
 }
Please login to merge, or discard this patch.
Spacing   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
 				$requirements = array()) {
93 93
 		$name = strtolower($method).$url;
94 94
 		$name = str_replace(array('/', '{', '}'), '_', $name);
95
-		if(!isset(self::$actions[$name])) {
95
+		if (!isset(self::$actions[$name])) {
96 96
 			$oldCollection = OC::$server->getRouter()->getCurrentCollection();
97 97
 			OC::$server->getRouter()->useCollection('ocs');
98 98
 			OC::$server->getRouter()->create($name, $url)
@@ -115,18 +115,18 @@  discard block
 block discarded – undo
115 115
 		$method = $request->getMethod();
116 116
 
117 117
 		// Prepare the request variables
118
-		if($method === 'PUT') {
118
+		if ($method === 'PUT') {
119 119
 			$parameters['_put'] = $request->getParams();
120
-		} else if($method === 'DELETE') {
120
+		} else if ($method === 'DELETE') {
121 121
 			$parameters['_delete'] = $request->getParams();
122 122
 		}
123 123
 		$name = $parameters['_route'];
124 124
 		// Foreach registered action
125 125
 		$responses = array();
126 126
 		$appManager = \OC::$server->getAppManager();
127
-		foreach(self::$actions[$name] as $action) {
127
+		foreach (self::$actions[$name] as $action) {
128 128
 			// Check authentication and availability
129
-			if(!self::isAuthorised($action)) {
129
+			if (!self::isAuthorised($action)) {
130 130
 				$responses[] = array(
131 131
 					'app' => $action['app'],
132 132
 					'response' => new OC_OCS_Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'),
@@ -134,7 +134,7 @@  discard block
 block discarded – undo
134 134
 					);
135 135
 				continue;
136 136
 			}
137
-			if(!is_callable($action['action'])) {
137
+			if (!is_callable($action['action'])) {
138 138
 				$responses[] = array(
139 139
 					'app' => $action['app'],
140 140
 					'response' => new OC_OCS_Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'),
@@ -174,15 +174,15 @@  discard block
 block discarded – undo
174 174
 			'failed' => array(),
175 175
 			);
176 176
 
177
-		foreach($responses as $response) {
178
-			if($response['shipped'] || ($response['app'] === 'core')) {
179
-				if($response['response']->succeeded()) {
177
+		foreach ($responses as $response) {
178
+			if ($response['shipped'] || ($response['app'] === 'core')) {
179
+				if ($response['response']->succeeded()) {
180 180
 					$shipped['succeeded'][$response['app']] = $response;
181 181
 				} else {
182 182
 					$shipped['failed'][$response['app']] = $response;
183 183
 				}
184 184
 			} else {
185
-				if($response['response']->succeeded()) {
185
+				if ($response['response']->succeeded()) {
186 186
 					$thirdparty['succeeded'][$response['app']] = $response;
187 187
 				} else {
188 188
 					$thirdparty['failed'][$response['app']] = $response;
@@ -191,14 +191,14 @@  discard block
 block discarded – undo
191 191
 		}
192 192
 
193 193
 		// Remove any error responses if there is one shipped response that succeeded
194
-		if(!empty($shipped['failed'])) {
194
+		if (!empty($shipped['failed'])) {
195 195
 			// Which shipped response do we use if they all failed?
196 196
 			// They may have failed for different reasons (different status codes)
197 197
 			// Which response code should we return?
198 198
 			// Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR
199 199
 			// Merge failed responses if more than one
200 200
 			$data = array();
201
-			foreach($shipped['failed'] as $failure) {
201
+			foreach ($shipped['failed'] as $failure) {
202 202
 				$data = array_merge_recursive($data, $failure['response']->getData());
203 203
 			}
204 204
 			$picked = reset($shipped['failed']);
@@ -207,12 +207,12 @@  discard block
 block discarded – undo
207 207
 			$headers = $picked['response']->getHeaders();
208 208
 			$response = new OC_OCS_Result($data, $code, $meta['message'], $headers);
209 209
 			return $response;
210
-		} elseif(!empty($shipped['succeeded'])) {
210
+		} elseif (!empty($shipped['succeeded'])) {
211 211
 			$responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']);
212
-		} elseif(!empty($thirdparty['failed'])) {
212
+		} elseif (!empty($thirdparty['failed'])) {
213 213
 			// Merge failed responses if more than one
214 214
 			$data = array();
215
-			foreach($thirdparty['failed'] as $failure) {
215
+			foreach ($thirdparty['failed'] as $failure) {
216 216
 				$data = array_merge_recursive($data, $failure['response']->getData());
217 217
 			}
218 218
 			$picked = reset($thirdparty['failed']);
@@ -229,8 +229,8 @@  discard block
 block discarded – undo
229 229
 		$codes = [];
230 230
 		$header = [];
231 231
 
232
-		foreach($responses as $response) {
233
-			if($response['shipped']) {
232
+		foreach ($responses as $response) {
233
+			if ($response['shipped']) {
234 234
 				$data = array_merge_recursive($response['response']->getData(), $data);
235 235
 			} else {
236 236
 				$data = array_merge_recursive($data, $response['response']->getData());
@@ -243,8 +243,8 @@  discard block
 block discarded – undo
243 243
 		// Use any non 100 status codes
244 244
 		$statusCode = 100;
245 245
 		$statusMessage = null;
246
-		foreach($codes as $code) {
247
-			if($code['code'] != 100) {
246
+		foreach ($codes as $code) {
247
+			if ($code['code'] != 100) {
248 248
 				$statusCode = $code['code'];
249 249
 				$statusMessage = $code['meta']['message'];
250 250
 				break;
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
 	 */
262 262
 	private static function isAuthorised($action) {
263 263
 		$level = $action['authlevel'];
264
-		switch($level) {
264
+		switch ($level) {
265 265
 			case API::GUEST_AUTH:
266 266
 				// Anyone can access
267 267
 				return true;
@@ -271,16 +271,16 @@  discard block
 block discarded – undo
271 271
 			case API::SUBADMIN_AUTH:
272 272
 				// Check for subadmin
273 273
 				$user = self::loginUser();
274
-				if(!$user) {
274
+				if (!$user) {
275 275
 					return false;
276 276
 				} else {
277 277
 					$userObject = \OC::$server->getUserSession()->getUser();
278
-					if($userObject === null) {
278
+					if ($userObject === null) {
279 279
 						return false;
280 280
 					}
281 281
 					$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
282 282
 					$admin = OC_User::isAdminUser($user);
283
-					if($isSubAdmin || $admin) {
283
+					if ($isSubAdmin || $admin) {
284 284
 						return true;
285 285
 					} else {
286 286
 						return false;
@@ -289,7 +289,7 @@  discard block
 block discarded – undo
289 289
 			case API::ADMIN_AUTH:
290 290
 				// Check for admin
291 291
 				$user = self::loginUser();
292
-				if(!$user) {
292
+				if (!$user) {
293 293
 					return false;
294 294
 				} else {
295 295
 					return OC_User::isAdminUser($user);
@@ -305,7 +305,7 @@  discard block
 block discarded – undo
305 305
 	 * @return string|false (username, or false on failure)
306 306
 	 */
307 307
 	private static function loginUser() {
308
-		if(self::$isLoggedIn === true) {
308
+		if (self::$isLoggedIn === true) {
309 309
 			return \OC_User::getUser();
310 310
 		}
311 311
 
@@ -354,13 +354,13 @@  discard block
 block discarded – undo
354 354
 	 * @param OC_OCS_Result $result
355 355
 	 * @param string $format the format xml|json
356 356
 	 */
357
-	public static function respond($result, $format='xml') {
357
+	public static function respond($result, $format = 'xml') {
358 358
 		$request = \OC::$server->getRequest();
359 359
 
360 360
 		// Send 401 headers if unauthorised
361
-		if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
361
+		if ($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
362 362
 			// If request comes from JS return dummy auth request
363
-			if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
363
+			if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
364 364
 				header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
365 365
 			} else {
366 366
 				header('WWW-Authenticate: Basic realm="Authorisation Required"');
@@ -368,8 +368,8 @@  discard block
 block discarded – undo
368 368
 			header('HTTP/1.0 401 Unauthorized');
369 369
 		}
370 370
 
371
-		foreach($result->getHeaders() as $name => $value) {
372
-			header($name . ': ' . $value);
371
+		foreach ($result->getHeaders() as $name => $value) {
372
+			header($name.': '.$value);
373 373
 		}
374 374
 
375 375
 		$meta = $result->getMeta();
@@ -391,14 +391,14 @@  discard block
 block discarded – undo
391 391
 	 * @param XMLWriter $writer
392 392
 	 */
393 393
 	private static function toXML($array, $writer) {
394
-		foreach($array as $k => $v) {
394
+		foreach ($array as $k => $v) {
395 395
 			if ($k[0] === '@') {
396 396
 				$writer->writeAttribute(substr($k, 1), $v);
397 397
 				continue;
398 398
 			} else if (is_numeric($k)) {
399 399
 				$k = 'element';
400 400
 			}
401
-			if(is_array($v)) {
401
+			if (is_array($v)) {
402 402
 				$writer->startElement($k);
403 403
 				self::toXML($v, $writer);
404 404
 				$writer->endElement();
Please login to merge, or discard this patch.
lib/private/legacy/eventsource.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -88,7 +88,7 @@
 block discarded – undo
88 88
 	 * send a message to the client
89 89
 	 *
90 90
 	 * @param string $type
91
-	 * @param mixed $data
91
+	 * @param string $data
92 92
 	 *
93 93
 	 * @throws \BadMethodCallException
94 94
 	 * if only one parameter is given, a typeless message will be send with that parameter as data
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
 		header('X-Accel-Buffering: no');
61 61
 		$this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true';
62 62
 		if ($this->fallback) {
63
-			$this->fallBackId = (int)$_GET['fallback_id'];
63
+			$this->fallBackId = (int) $_GET['fallback_id'];
64 64
 			/**
65 65
 			 * FIXME: The default content-security-policy of ownCloud forbids inline
66 66
 			 * JavaScript for security reasons. IE starting on Windows 10 will
@@ -73,11 +73,11 @@  discard block
 block discarded – undo
73 73
 			 */
74 74
 			header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'");
75 75
 			header("Content-Type: text/html");
76
-			echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy
76
+			echo str_repeat('<span></span>'.PHP_EOL, 10); //dummy data to keep IE happy
77 77
 		} else {
78 78
 			header("Content-Type: text/event-stream");
79 79
 		}
80
-		if(!\OC::$server->getRequest()->passesStrictCookieCheck()) {
80
+		if (!\OC::$server->getRequest()->passesStrictCookieCheck()) {
81 81
 			header('Location: '.\OC::$WEBROOT);
82 82
 			exit();
83 83
 		}
@@ -100,7 +100,7 @@  discard block
 block discarded – undo
100 100
 	 */
101 101
 	public function send($type, $data = null) {
102 102
 		if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) {
103
-			throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')');
103
+			throw new BadMethodCallException('Type needs to be alphanumeric ('.$type.')');
104 104
 		}
105 105
 		$this->init();
106 106
 		if (is_null($data)) {
@@ -109,13 +109,13 @@  discard block
 block discarded – undo
109 109
 		}
110 110
 		if ($this->fallback) {
111 111
 			$response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('
112
-				. $this->fallBackId . ',"' . $type . '",' . OCP\JSON::encode($data) . ')</script>' . PHP_EOL;
112
+				. $this->fallBackId.',"'.$type.'",'.OCP\JSON::encode($data).')</script>'.PHP_EOL;
113 113
 			echo $response;
114 114
 		} else {
115 115
 			if ($type) {
116
-				echo 'event: ' . $type . PHP_EOL;
116
+				echo 'event: '.$type.PHP_EOL;
117 117
 			}
118
-			echo 'data: ' . OCP\JSON::encode($data) . PHP_EOL;
118
+			echo 'data: '.OCP\JSON::encode($data).PHP_EOL;
119 119
 		}
120 120
 		echo PHP_EOL;
121 121
 		flush();
Please login to merge, or discard this patch.
Indentation   +89 added lines, -89 removed lines patch added patch discarded remove patch
@@ -33,99 +33,99 @@
 block discarded – undo
33 33
  * use server side events with caution, to many open requests can hang the server
34 34
  */
35 35
 class OC_EventSource implements \OCP\IEventSource {
36
-	/**
37
-	 * @var bool
38
-	 */
39
-	private $fallback;
36
+    /**
37
+     * @var bool
38
+     */
39
+    private $fallback;
40 40
 
41
-	/**
42
-	 * @var int
43
-	 */
44
-	private $fallBackId = 0;
41
+    /**
42
+     * @var int
43
+     */
44
+    private $fallBackId = 0;
45 45
 
46
-	/**
47
-	 * @var bool
48
-	 */
49
-	private $started = false;
46
+    /**
47
+     * @var bool
48
+     */
49
+    private $started = false;
50 50
 
51
-	protected function init() {
52
-		if ($this->started) {
53
-			return;
54
-		}
55
-		$this->started = true;
51
+    protected function init() {
52
+        if ($this->started) {
53
+            return;
54
+        }
55
+        $this->started = true;
56 56
 
57
-		// prevent php output buffering, caching and nginx buffering
58
-		OC_Util::obEnd();
59
-		header('Cache-Control: no-cache');
60
-		header('X-Accel-Buffering: no');
61
-		$this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true';
62
-		if ($this->fallback) {
63
-			$this->fallBackId = (int)$_GET['fallback_id'];
64
-			/**
65
-			 * FIXME: The default content-security-policy of ownCloud forbids inline
66
-			 * JavaScript for security reasons. IE starting on Windows 10 will
67
-			 * however also obey the CSP which will break the event source fallback.
68
-			 *
69
-			 * As a workaround thus we set a custom policy which allows the execution
70
-			 * of inline JavaScript.
71
-			 *
72
-			 * @link https://github.com/owncloud/core/issues/14286
73
-			 */
74
-			header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'");
75
-			header("Content-Type: text/html");
76
-			echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy
77
-		} else {
78
-			header("Content-Type: text/event-stream");
79
-		}
80
-		if(!\OC::$server->getRequest()->passesStrictCookieCheck()) {
81
-			header('Location: '.\OC::$WEBROOT);
82
-			exit();
83
-		}
84
-		if (!(\OC::$server->getRequest()->passesCSRFCheck())) {
85
-			$this->send('error', 'Possible CSRF attack. Connection will be closed.');
86
-			$this->close();
87
-			exit();
88
-		}
89
-		flush();
90
-	}
57
+        // prevent php output buffering, caching and nginx buffering
58
+        OC_Util::obEnd();
59
+        header('Cache-Control: no-cache');
60
+        header('X-Accel-Buffering: no');
61
+        $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true';
62
+        if ($this->fallback) {
63
+            $this->fallBackId = (int)$_GET['fallback_id'];
64
+            /**
65
+             * FIXME: The default content-security-policy of ownCloud forbids inline
66
+             * JavaScript for security reasons. IE starting on Windows 10 will
67
+             * however also obey the CSP which will break the event source fallback.
68
+             *
69
+             * As a workaround thus we set a custom policy which allows the execution
70
+             * of inline JavaScript.
71
+             *
72
+             * @link https://github.com/owncloud/core/issues/14286
73
+             */
74
+            header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'");
75
+            header("Content-Type: text/html");
76
+            echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy
77
+        } else {
78
+            header("Content-Type: text/event-stream");
79
+        }
80
+        if(!\OC::$server->getRequest()->passesStrictCookieCheck()) {
81
+            header('Location: '.\OC::$WEBROOT);
82
+            exit();
83
+        }
84
+        if (!(\OC::$server->getRequest()->passesCSRFCheck())) {
85
+            $this->send('error', 'Possible CSRF attack. Connection will be closed.');
86
+            $this->close();
87
+            exit();
88
+        }
89
+        flush();
90
+    }
91 91
 
92
-	/**
93
-	 * send a message to the client
94
-	 *
95
-	 * @param string $type
96
-	 * @param mixed $data
97
-	 *
98
-	 * @throws \BadMethodCallException
99
-	 * if only one parameter is given, a typeless message will be send with that parameter as data
100
-	 * @suppress PhanDeprecatedFunction
101
-	 */
102
-	public function send($type, $data = null) {
103
-		if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) {
104
-			throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')');
105
-		}
106
-		$this->init();
107
-		if (is_null($data)) {
108
-			$data = $type;
109
-			$type = null;
110
-		}
111
-		if ($this->fallback) {
112
-			$response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('
113
-				. $this->fallBackId . ',"' . $type . '",' . OCP\JSON::encode($data) . ')</script>' . PHP_EOL;
114
-			echo $response;
115
-		} else {
116
-			if ($type) {
117
-				echo 'event: ' . $type . PHP_EOL;
118
-			}
119
-			echo 'data: ' . OCP\JSON::encode($data) . PHP_EOL;
120
-		}
121
-		echo PHP_EOL;
122
-		flush();
123
-	}
92
+    /**
93
+     * send a message to the client
94
+     *
95
+     * @param string $type
96
+     * @param mixed $data
97
+     *
98
+     * @throws \BadMethodCallException
99
+     * if only one parameter is given, a typeless message will be send with that parameter as data
100
+     * @suppress PhanDeprecatedFunction
101
+     */
102
+    public function send($type, $data = null) {
103
+        if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) {
104
+            throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')');
105
+        }
106
+        $this->init();
107
+        if (is_null($data)) {
108
+            $data = $type;
109
+            $type = null;
110
+        }
111
+        if ($this->fallback) {
112
+            $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('
113
+                . $this->fallBackId . ',"' . $type . '",' . OCP\JSON::encode($data) . ')</script>' . PHP_EOL;
114
+            echo $response;
115
+        } else {
116
+            if ($type) {
117
+                echo 'event: ' . $type . PHP_EOL;
118
+            }
119
+            echo 'data: ' . OCP\JSON::encode($data) . PHP_EOL;
120
+        }
121
+        echo PHP_EOL;
122
+        flush();
123
+    }
124 124
 
125
-	/**
126
-	 * close the connection of the event source
127
-	 */
128
-	public function close() {
129
-		$this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it
130
-	}
125
+    /**
126
+     * close the connection of the event source
127
+     */
128
+    public function close() {
129
+        $this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it
130
+    }
131 131
 }
Please login to merge, or discard this patch.
lib/private/Memcache/Memcached.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -155,7 +155,7 @@
 block discarded – undo
155 155
 	 * Set a value in the cache if it's not already stored
156 156
 	 *
157 157
 	 * @param string $key
158
-	 * @param mixed $value
158
+	 * @param integer $value
159 159
 	 * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
160 160
 	 * @return bool
161 161
 	 * @throws \Exception
Please login to merge, or discard this patch.
Indentation   +189 added lines, -189 removed lines patch added patch discarded remove patch
@@ -33,193 +33,193 @@
 block discarded – undo
33 33
 use OCP\IMemcache;
34 34
 
35 35
 class Memcached extends Cache implements IMemcache {
36
-	use CASTrait;
37
-
38
-	/**
39
-	 * @var \Memcached $cache
40
-	 */
41
-	private static $cache = null;
42
-
43
-	use CADTrait;
44
-
45
-	public function __construct($prefix = '') {
46
-		parent::__construct($prefix);
47
-		if (is_null(self::$cache)) {
48
-			self::$cache = new \Memcached();
49
-
50
-			$defaultOptions = [
51
-				\Memcached::OPT_CONNECT_TIMEOUT => 50,
52
-				\Memcached::OPT_RETRY_TIMEOUT =>   50,
53
-				\Memcached::OPT_SEND_TIMEOUT =>    50,
54
-				\Memcached::OPT_RECV_TIMEOUT =>    50,
55
-				\Memcached::OPT_POLL_TIMEOUT =>    50,
56
-
57
-				// Enable compression
58
-				\Memcached::OPT_COMPRESSION =>          true,
59
-
60
-				// Turn on consistent hashing
61
-				\Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
62
-
63
-				// Enable Binary Protocol
64
-				//\Memcached::OPT_BINARY_PROTOCOL =>      true,
65
-			];
66
-			// by default enable igbinary serializer if available
67
-			if (\Memcached::HAVE_IGBINARY) {
68
-				$defaultOptions[\Memcached::OPT_SERIALIZER] =
69
-					\Memcached::SERIALIZER_IGBINARY;
70
-			}
71
-			$options = \OC::$server->getConfig()->getSystemValue('memcached_options', []);
72
-			if (is_array($options)) {
73
-				$options = $options + $defaultOptions;
74
-				self::$cache->setOptions($options);
75
-			} else {
76
-				throw new HintException("Expected 'memcached_options' config to be an array, got $options");
77
-			}
78
-
79
-			$servers = \OC::$server->getSystemConfig()->getValue('memcached_servers');
80
-			if (!$servers) {
81
-				$server = \OC::$server->getSystemConfig()->getValue('memcached_server');
82
-				if ($server) {
83
-					$servers = [$server];
84
-				} else {
85
-					$servers = [['localhost', 11211]];
86
-				}
87
-			}
88
-			self::$cache->addServers($servers);
89
-		}
90
-	}
91
-
92
-	/**
93
-	 * entries in XCache gets namespaced to prevent collisions between owncloud instances and users
94
-	 */
95
-	protected function getNameSpace() {
96
-		return $this->prefix;
97
-	}
98
-
99
-	public function get($key) {
100
-		$result = self::$cache->get($this->getNameSpace() . $key);
101
-		if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) {
102
-			return null;
103
-		} else {
104
-			return $result;
105
-		}
106
-	}
107
-
108
-	public function set($key, $value, $ttl = 0) {
109
-		if ($ttl > 0) {
110
-			$result =  self::$cache->set($this->getNameSpace() . $key, $value, $ttl);
111
-		} else {
112
-			$result = self::$cache->set($this->getNameSpace() . $key, $value);
113
-		}
114
-		if ($result !== true) {
115
-			$this->verifyReturnCode();
116
-		}
117
-		return $result;
118
-	}
119
-
120
-	public function hasKey($key) {
121
-		self::$cache->get($this->getNameSpace() . $key);
122
-		return self::$cache->getResultCode() === \Memcached::RES_SUCCESS;
123
-	}
124
-
125
-	public function remove($key) {
126
-		$result= self::$cache->delete($this->getNameSpace() . $key);
127
-		if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) {
128
-			$this->verifyReturnCode();
129
-		}
130
-		return $result;
131
-	}
132
-
133
-	public function clear($prefix = '') {
134
-		$prefix = $this->getNameSpace() . $prefix;
135
-		$allKeys = self::$cache->getAllKeys();
136
-		if ($allKeys === false) {
137
-			// newer Memcached doesn't like getAllKeys(), flush everything
138
-			self::$cache->flush();
139
-			return true;
140
-		}
141
-		$keys = array();
142
-		$prefixLength = strlen($prefix);
143
-		foreach ($allKeys as $key) {
144
-			if (substr($key, 0, $prefixLength) === $prefix) {
145
-				$keys[] = $key;
146
-			}
147
-		}
148
-		if (method_exists(self::$cache, 'deleteMulti')) {
149
-			self::$cache->deleteMulti($keys);
150
-		} else {
151
-			foreach ($keys as $key) {
152
-				self::$cache->delete($key);
153
-			}
154
-		}
155
-		return true;
156
-	}
157
-
158
-	/**
159
-	 * Set a value in the cache if it's not already stored
160
-	 *
161
-	 * @param string $key
162
-	 * @param mixed $value
163
-	 * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
164
-	 * @return bool
165
-	 * @throws \Exception
166
-	 */
167
-	public function add($key, $value, $ttl = 0) {
168
-		$result = self::$cache->add($this->getPrefix() . $key, $value, $ttl);
169
-		if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) {
170
-			$this->verifyReturnCode();
171
-		}
172
-		return $result;
173
-	}
174
-
175
-	/**
176
-	 * Increase a stored number
177
-	 *
178
-	 * @param string $key
179
-	 * @param int $step
180
-	 * @return int | bool
181
-	 */
182
-	public function inc($key, $step = 1) {
183
-		$this->add($key, 0);
184
-		$result = self::$cache->increment($this->getPrefix() . $key, $step);
185
-
186
-		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
187
-			return false;
188
-		}
189
-
190
-		return $result;
191
-	}
192
-
193
-	/**
194
-	 * Decrease a stored number
195
-	 *
196
-	 * @param string $key
197
-	 * @param int $step
198
-	 * @return int | bool
199
-	 */
200
-	public function dec($key, $step = 1) {
201
-		$result = self::$cache->decrement($this->getPrefix() . $key, $step);
202
-
203
-		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
204
-			return false;
205
-		}
206
-
207
-		return $result;
208
-	}
209
-
210
-	static public function isAvailable() {
211
-		return extension_loaded('memcached');
212
-	}
213
-
214
-	/**
215
-	 * @throws \Exception
216
-	 */
217
-	private function verifyReturnCode() {
218
-		$code = self::$cache->getResultCode();
219
-		if ($code === \Memcached::RES_SUCCESS) {
220
-			return;
221
-		}
222
-		$message = self::$cache->getResultMessage();
223
-		throw new \Exception("Error $code interacting with memcached : $message");
224
-	}
36
+    use CASTrait;
37
+
38
+    /**
39
+     * @var \Memcached $cache
40
+     */
41
+    private static $cache = null;
42
+
43
+    use CADTrait;
44
+
45
+    public function __construct($prefix = '') {
46
+        parent::__construct($prefix);
47
+        if (is_null(self::$cache)) {
48
+            self::$cache = new \Memcached();
49
+
50
+            $defaultOptions = [
51
+                \Memcached::OPT_CONNECT_TIMEOUT => 50,
52
+                \Memcached::OPT_RETRY_TIMEOUT =>   50,
53
+                \Memcached::OPT_SEND_TIMEOUT =>    50,
54
+                \Memcached::OPT_RECV_TIMEOUT =>    50,
55
+                \Memcached::OPT_POLL_TIMEOUT =>    50,
56
+
57
+                // Enable compression
58
+                \Memcached::OPT_COMPRESSION =>          true,
59
+
60
+                // Turn on consistent hashing
61
+                \Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
62
+
63
+                // Enable Binary Protocol
64
+                //\Memcached::OPT_BINARY_PROTOCOL =>      true,
65
+            ];
66
+            // by default enable igbinary serializer if available
67
+            if (\Memcached::HAVE_IGBINARY) {
68
+                $defaultOptions[\Memcached::OPT_SERIALIZER] =
69
+                    \Memcached::SERIALIZER_IGBINARY;
70
+            }
71
+            $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []);
72
+            if (is_array($options)) {
73
+                $options = $options + $defaultOptions;
74
+                self::$cache->setOptions($options);
75
+            } else {
76
+                throw new HintException("Expected 'memcached_options' config to be an array, got $options");
77
+            }
78
+
79
+            $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers');
80
+            if (!$servers) {
81
+                $server = \OC::$server->getSystemConfig()->getValue('memcached_server');
82
+                if ($server) {
83
+                    $servers = [$server];
84
+                } else {
85
+                    $servers = [['localhost', 11211]];
86
+                }
87
+            }
88
+            self::$cache->addServers($servers);
89
+        }
90
+    }
91
+
92
+    /**
93
+     * entries in XCache gets namespaced to prevent collisions between owncloud instances and users
94
+     */
95
+    protected function getNameSpace() {
96
+        return $this->prefix;
97
+    }
98
+
99
+    public function get($key) {
100
+        $result = self::$cache->get($this->getNameSpace() . $key);
101
+        if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) {
102
+            return null;
103
+        } else {
104
+            return $result;
105
+        }
106
+    }
107
+
108
+    public function set($key, $value, $ttl = 0) {
109
+        if ($ttl > 0) {
110
+            $result =  self::$cache->set($this->getNameSpace() . $key, $value, $ttl);
111
+        } else {
112
+            $result = self::$cache->set($this->getNameSpace() . $key, $value);
113
+        }
114
+        if ($result !== true) {
115
+            $this->verifyReturnCode();
116
+        }
117
+        return $result;
118
+    }
119
+
120
+    public function hasKey($key) {
121
+        self::$cache->get($this->getNameSpace() . $key);
122
+        return self::$cache->getResultCode() === \Memcached::RES_SUCCESS;
123
+    }
124
+
125
+    public function remove($key) {
126
+        $result= self::$cache->delete($this->getNameSpace() . $key);
127
+        if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) {
128
+            $this->verifyReturnCode();
129
+        }
130
+        return $result;
131
+    }
132
+
133
+    public function clear($prefix = '') {
134
+        $prefix = $this->getNameSpace() . $prefix;
135
+        $allKeys = self::$cache->getAllKeys();
136
+        if ($allKeys === false) {
137
+            // newer Memcached doesn't like getAllKeys(), flush everything
138
+            self::$cache->flush();
139
+            return true;
140
+        }
141
+        $keys = array();
142
+        $prefixLength = strlen($prefix);
143
+        foreach ($allKeys as $key) {
144
+            if (substr($key, 0, $prefixLength) === $prefix) {
145
+                $keys[] = $key;
146
+            }
147
+        }
148
+        if (method_exists(self::$cache, 'deleteMulti')) {
149
+            self::$cache->deleteMulti($keys);
150
+        } else {
151
+            foreach ($keys as $key) {
152
+                self::$cache->delete($key);
153
+            }
154
+        }
155
+        return true;
156
+    }
157
+
158
+    /**
159
+     * Set a value in the cache if it's not already stored
160
+     *
161
+     * @param string $key
162
+     * @param mixed $value
163
+     * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
164
+     * @return bool
165
+     * @throws \Exception
166
+     */
167
+    public function add($key, $value, $ttl = 0) {
168
+        $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl);
169
+        if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) {
170
+            $this->verifyReturnCode();
171
+        }
172
+        return $result;
173
+    }
174
+
175
+    /**
176
+     * Increase a stored number
177
+     *
178
+     * @param string $key
179
+     * @param int $step
180
+     * @return int | bool
181
+     */
182
+    public function inc($key, $step = 1) {
183
+        $this->add($key, 0);
184
+        $result = self::$cache->increment($this->getPrefix() . $key, $step);
185
+
186
+        if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
187
+            return false;
188
+        }
189
+
190
+        return $result;
191
+    }
192
+
193
+    /**
194
+     * Decrease a stored number
195
+     *
196
+     * @param string $key
197
+     * @param int $step
198
+     * @return int | bool
199
+     */
200
+    public function dec($key, $step = 1) {
201
+        $result = self::$cache->decrement($this->getPrefix() . $key, $step);
202
+
203
+        if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
204
+            return false;
205
+        }
206
+
207
+        return $result;
208
+    }
209
+
210
+    static public function isAvailable() {
211
+        return extension_loaded('memcached');
212
+    }
213
+
214
+    /**
215
+     * @throws \Exception
216
+     */
217
+    private function verifyReturnCode() {
218
+        $code = self::$cache->getResultCode();
219
+        if ($code === \Memcached::RES_SUCCESS) {
220
+            return;
221
+        }
222
+        $message = self::$cache->getResultMessage();
223
+        throw new \Exception("Error $code interacting with memcached : $message");
224
+    }
225 225
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
 	}
98 98
 
99 99
 	public function get($key) {
100
-		$result = self::$cache->get($this->getNameSpace() . $key);
100
+		$result = self::$cache->get($this->getNameSpace().$key);
101 101
 		if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) {
102 102
 			return null;
103 103
 		} else {
@@ -107,9 +107,9 @@  discard block
 block discarded – undo
107 107
 
108 108
 	public function set($key, $value, $ttl = 0) {
109 109
 		if ($ttl > 0) {
110
-			$result =  self::$cache->set($this->getNameSpace() . $key, $value, $ttl);
110
+			$result = self::$cache->set($this->getNameSpace().$key, $value, $ttl);
111 111
 		} else {
112
-			$result = self::$cache->set($this->getNameSpace() . $key, $value);
112
+			$result = self::$cache->set($this->getNameSpace().$key, $value);
113 113
 		}
114 114
 		if ($result !== true) {
115 115
 			$this->verifyReturnCode();
@@ -118,12 +118,12 @@  discard block
 block discarded – undo
118 118
 	}
119 119
 
120 120
 	public function hasKey($key) {
121
-		self::$cache->get($this->getNameSpace() . $key);
121
+		self::$cache->get($this->getNameSpace().$key);
122 122
 		return self::$cache->getResultCode() === \Memcached::RES_SUCCESS;
123 123
 	}
124 124
 
125 125
 	public function remove($key) {
126
-		$result= self::$cache->delete($this->getNameSpace() . $key);
126
+		$result = self::$cache->delete($this->getNameSpace().$key);
127 127
 		if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) {
128 128
 			$this->verifyReturnCode();
129 129
 		}
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
 	}
132 132
 
133 133
 	public function clear($prefix = '') {
134
-		$prefix = $this->getNameSpace() . $prefix;
134
+		$prefix = $this->getNameSpace().$prefix;
135 135
 		$allKeys = self::$cache->getAllKeys();
136 136
 		if ($allKeys === false) {
137 137
 			// newer Memcached doesn't like getAllKeys(), flush everything
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
 	 * @throws \Exception
166 166
 	 */
167 167
 	public function add($key, $value, $ttl = 0) {
168
-		$result = self::$cache->add($this->getPrefix() . $key, $value, $ttl);
168
+		$result = self::$cache->add($this->getPrefix().$key, $value, $ttl);
169 169
 		if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) {
170 170
 			$this->verifyReturnCode();
171 171
 		}
@@ -181,7 +181,7 @@  discard block
 block discarded – undo
181 181
 	 */
182 182
 	public function inc($key, $step = 1) {
183 183
 		$this->add($key, 0);
184
-		$result = self::$cache->increment($this->getPrefix() . $key, $step);
184
+		$result = self::$cache->increment($this->getPrefix().$key, $step);
185 185
 
186 186
 		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
187 187
 			return false;
@@ -198,7 +198,7 @@  discard block
 block discarded – undo
198 198
 	 * @return int | bool
199 199
 	 */
200 200
 	public function dec($key, $step = 1) {
201
-		$result = self::$cache->decrement($this->getPrefix() . $key, $step);
201
+		$result = self::$cache->decrement($this->getPrefix().$key, $step);
202 202
 
203 203
 		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
204 204
 			return false;
Please login to merge, or discard this patch.
lib/private/Repair.php 3 patches
Doc Comments   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
 	 * Returns expensive repair steps to be run on the
140 140
 	 * command line with a special option.
141 141
 	 *
142
-	 * @return IRepairStep[]
142
+	 * @return OldGroupMembershipShares[]
143 143
 	 */
144 144
 	public static function getExpensiveRepairSteps() {
145 145
 		return [
@@ -216,7 +216,6 @@  discard block
 block discarded – undo
216 216
 	}
217 217
 
218 218
 	/**
219
-	 * @param int $max
220 219
 	 */
221 220
 	public function finishProgress() {
222 221
 		// for now just emit as we did in the past
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -58,7 +58,7 @@
 block discarded – undo
58 58
 use Symfony\Component\EventDispatcher\EventDispatcher;
59 59
 use Symfony\Component\EventDispatcher\GenericEvent;
60 60
 
61
-class Repair implements IOutput{
61
+class Repair implements IOutput {
62 62
 	/* @var IRepairStep[] */
63 63
 	private $repairSteps;
64 64
 	/** @var EventDispatcher */
Please login to merge, or discard this patch.
Indentation   +176 added lines, -176 removed lines patch added patch discarded remove patch
@@ -54,180 +54,180 @@
 block discarded – undo
54 54
 use Symfony\Component\EventDispatcher\GenericEvent;
55 55
 
56 56
 class Repair implements IOutput{
57
-	/* @var IRepairStep[] */
58
-	private $repairSteps;
59
-	/** @var EventDispatcher */
60
-	private $dispatcher;
61
-	/** @var string */
62
-	private $currentStep;
63
-
64
-	/**
65
-	 * Creates a new repair step runner
66
-	 *
67
-	 * @param IRepairStep[] $repairSteps array of RepairStep instances
68
-	 * @param EventDispatcher $dispatcher
69
-	 */
70
-	public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
71
-		$this->repairSteps = $repairSteps;
72
-		$this->dispatcher = $dispatcher;
73
-	}
74
-
75
-	/**
76
-	 * Run a series of repair steps for common problems
77
-	 */
78
-	public function run() {
79
-		if (count($this->repairSteps) === 0) {
80
-			$this->emit('\OC\Repair', 'info', array('No repair steps available'));
81
-			return;
82
-		}
83
-		// run each repair step
84
-		foreach ($this->repairSteps as $step) {
85
-			$this->currentStep = $step->getName();
86
-			$this->emit('\OC\Repair', 'step', [$this->currentStep]);
87
-			$step->run($this);
88
-		}
89
-	}
90
-
91
-	/**
92
-	 * Add repair step
93
-	 *
94
-	 * @param IRepairStep|string $repairStep repair step
95
-	 * @throws \Exception
96
-	 */
97
-	public function addStep($repairStep) {
98
-		if (is_string($repairStep)) {
99
-			try {
100
-				$s = \OC::$server->query($repairStep);
101
-			} catch (QueryException $e) {
102
-				if (class_exists($repairStep)) {
103
-					$s = new $repairStep();
104
-				} else {
105
-					throw new \Exception("Repair step '$repairStep' is unknown");
106
-				}
107
-			}
108
-
109
-			if ($s instanceof IRepairStep) {
110
-				$this->repairSteps[] = $s;
111
-			} else {
112
-				throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
113
-			}
114
-		} else {
115
-			$this->repairSteps[] = $repairStep;
116
-		}
117
-	}
118
-
119
-	/**
120
-	 * Returns the default repair steps to be run on the
121
-	 * command line or after an upgrade.
122
-	 *
123
-	 * @return IRepairStep[]
124
-	 */
125
-	public static function getRepairSteps() {
126
-		return [
127
-			new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
128
-			new RepairMimeTypes(\OC::$server->getConfig()),
129
-			new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
130
-			new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
131
-			new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
132
-			new MoveUpdaterStepFile(\OC::$server->getConfig()),
133
-			new MoveAvatars(
134
-				\OC::$server->getJobList(),
135
-				\OC::$server->getConfig()
136
-			),
137
-			new CleanPreviews(
138
-				\OC::$server->getJobList(),
139
-				\OC::$server->getUserManager(),
140
-				\OC::$server->getConfig()
141
-			),
142
-			new FixMountStorages(\OC::$server->getDatabaseConnection()),
143
-			new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
144
-			new InstallCoreBundle(
145
-				\OC::$server->query(BundleFetcher::class),
146
-				\OC::$server->getConfig(),
147
-				\OC::$server->query(Installer::class)
148
-			),
149
-			new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig())
150
-		];
151
-	}
152
-
153
-	/**
154
-	 * Returns expensive repair steps to be run on the
155
-	 * command line with a special option.
156
-	 *
157
-	 * @return IRepairStep[]
158
-	 */
159
-	public static function getExpensiveRepairSteps() {
160
-		return [
161
-			new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager())
162
-		];
163
-	}
164
-
165
-	/**
166
-	 * Returns the repair steps to be run before an
167
-	 * upgrade.
168
-	 *
169
-	 * @return IRepairStep[]
170
-	 */
171
-	public static function getBeforeUpgradeRepairSteps() {
172
-		$connection = \OC::$server->getDatabaseConnection();
173
-		$config = \OC::$server->getConfig();
174
-		$steps = [
175
-			new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
176
-			new SqliteAutoincrement($connection),
177
-			new SaveAccountsTableData($connection, $config),
178
-			new DropAccountTermsTable($connection),
179
-		];
180
-
181
-		return $steps;
182
-	}
183
-
184
-	/**
185
-	 * @param string $scope
186
-	 * @param string $method
187
-	 * @param array $arguments
188
-	 */
189
-	public function emit($scope, $method, array $arguments = []) {
190
-		if (!is_null($this->dispatcher)) {
191
-			$this->dispatcher->dispatch("$scope::$method",
192
-				new GenericEvent("$scope::$method", $arguments));
193
-		}
194
-	}
195
-
196
-	public function info($string) {
197
-		// for now just emit as we did in the past
198
-		$this->emit('\OC\Repair', 'info', array($string));
199
-	}
200
-
201
-	/**
202
-	 * @param string $message
203
-	 */
204
-	public function warning($message) {
205
-		// for now just emit as we did in the past
206
-		$this->emit('\OC\Repair', 'warning', [$message]);
207
-	}
208
-
209
-	/**
210
-	 * @param int $max
211
-	 */
212
-	public function startProgress($max = 0) {
213
-		// for now just emit as we did in the past
214
-		$this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
215
-	}
216
-
217
-	/**
218
-	 * @param int $step
219
-	 * @param string $description
220
-	 */
221
-	public function advance($step = 1, $description = '') {
222
-		// for now just emit as we did in the past
223
-		$this->emit('\OC\Repair', 'advance', [$step, $description]);
224
-	}
225
-
226
-	/**
227
-	 * @param int $max
228
-	 */
229
-	public function finishProgress() {
230
-		// for now just emit as we did in the past
231
-		$this->emit('\OC\Repair', 'finishProgress', []);
232
-	}
57
+    /* @var IRepairStep[] */
58
+    private $repairSteps;
59
+    /** @var EventDispatcher */
60
+    private $dispatcher;
61
+    /** @var string */
62
+    private $currentStep;
63
+
64
+    /**
65
+     * Creates a new repair step runner
66
+     *
67
+     * @param IRepairStep[] $repairSteps array of RepairStep instances
68
+     * @param EventDispatcher $dispatcher
69
+     */
70
+    public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
71
+        $this->repairSteps = $repairSteps;
72
+        $this->dispatcher = $dispatcher;
73
+    }
74
+
75
+    /**
76
+     * Run a series of repair steps for common problems
77
+     */
78
+    public function run() {
79
+        if (count($this->repairSteps) === 0) {
80
+            $this->emit('\OC\Repair', 'info', array('No repair steps available'));
81
+            return;
82
+        }
83
+        // run each repair step
84
+        foreach ($this->repairSteps as $step) {
85
+            $this->currentStep = $step->getName();
86
+            $this->emit('\OC\Repair', 'step', [$this->currentStep]);
87
+            $step->run($this);
88
+        }
89
+    }
90
+
91
+    /**
92
+     * Add repair step
93
+     *
94
+     * @param IRepairStep|string $repairStep repair step
95
+     * @throws \Exception
96
+     */
97
+    public function addStep($repairStep) {
98
+        if (is_string($repairStep)) {
99
+            try {
100
+                $s = \OC::$server->query($repairStep);
101
+            } catch (QueryException $e) {
102
+                if (class_exists($repairStep)) {
103
+                    $s = new $repairStep();
104
+                } else {
105
+                    throw new \Exception("Repair step '$repairStep' is unknown");
106
+                }
107
+            }
108
+
109
+            if ($s instanceof IRepairStep) {
110
+                $this->repairSteps[] = $s;
111
+            } else {
112
+                throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
113
+            }
114
+        } else {
115
+            $this->repairSteps[] = $repairStep;
116
+        }
117
+    }
118
+
119
+    /**
120
+     * Returns the default repair steps to be run on the
121
+     * command line or after an upgrade.
122
+     *
123
+     * @return IRepairStep[]
124
+     */
125
+    public static function getRepairSteps() {
126
+        return [
127
+            new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
128
+            new RepairMimeTypes(\OC::$server->getConfig()),
129
+            new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
130
+            new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
131
+            new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
132
+            new MoveUpdaterStepFile(\OC::$server->getConfig()),
133
+            new MoveAvatars(
134
+                \OC::$server->getJobList(),
135
+                \OC::$server->getConfig()
136
+            ),
137
+            new CleanPreviews(
138
+                \OC::$server->getJobList(),
139
+                \OC::$server->getUserManager(),
140
+                \OC::$server->getConfig()
141
+            ),
142
+            new FixMountStorages(\OC::$server->getDatabaseConnection()),
143
+            new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
144
+            new InstallCoreBundle(
145
+                \OC::$server->query(BundleFetcher::class),
146
+                \OC::$server->getConfig(),
147
+                \OC::$server->query(Installer::class)
148
+            ),
149
+            new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig())
150
+        ];
151
+    }
152
+
153
+    /**
154
+     * Returns expensive repair steps to be run on the
155
+     * command line with a special option.
156
+     *
157
+     * @return IRepairStep[]
158
+     */
159
+    public static function getExpensiveRepairSteps() {
160
+        return [
161
+            new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager())
162
+        ];
163
+    }
164
+
165
+    /**
166
+     * Returns the repair steps to be run before an
167
+     * upgrade.
168
+     *
169
+     * @return IRepairStep[]
170
+     */
171
+    public static function getBeforeUpgradeRepairSteps() {
172
+        $connection = \OC::$server->getDatabaseConnection();
173
+        $config = \OC::$server->getConfig();
174
+        $steps = [
175
+            new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
176
+            new SqliteAutoincrement($connection),
177
+            new SaveAccountsTableData($connection, $config),
178
+            new DropAccountTermsTable($connection),
179
+        ];
180
+
181
+        return $steps;
182
+    }
183
+
184
+    /**
185
+     * @param string $scope
186
+     * @param string $method
187
+     * @param array $arguments
188
+     */
189
+    public function emit($scope, $method, array $arguments = []) {
190
+        if (!is_null($this->dispatcher)) {
191
+            $this->dispatcher->dispatch("$scope::$method",
192
+                new GenericEvent("$scope::$method", $arguments));
193
+        }
194
+    }
195
+
196
+    public function info($string) {
197
+        // for now just emit as we did in the past
198
+        $this->emit('\OC\Repair', 'info', array($string));
199
+    }
200
+
201
+    /**
202
+     * @param string $message
203
+     */
204
+    public function warning($message) {
205
+        // for now just emit as we did in the past
206
+        $this->emit('\OC\Repair', 'warning', [$message]);
207
+    }
208
+
209
+    /**
210
+     * @param int $max
211
+     */
212
+    public function startProgress($max = 0) {
213
+        // for now just emit as we did in the past
214
+        $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
215
+    }
216
+
217
+    /**
218
+     * @param int $step
219
+     * @param string $description
220
+     */
221
+    public function advance($step = 1, $description = '') {
222
+        // for now just emit as we did in the past
223
+        $this->emit('\OC\Repair', 'advance', [$step, $description]);
224
+    }
225
+
226
+    /**
227
+     * @param int $max
228
+     */
229
+    public function finishProgress() {
230
+        // for now just emit as we did in the past
231
+        $this->emit('\OC\Repair', 'finishProgress', []);
232
+    }
233 233
 }
Please login to merge, or discard this patch.