@@ -23,50 +23,50 @@ |
||
23 | 23 | $application = new \OCA\Federation\AppInfo\Application(); |
24 | 24 | |
25 | 25 | $application->registerRoutes( |
26 | - $this, |
|
27 | - [ |
|
28 | - 'routes' => [ |
|
29 | - [ |
|
30 | - 'name' => 'Settings#addServer', |
|
31 | - 'url' => '/trusted-servers', |
|
32 | - 'verb' => 'POST' |
|
33 | - ], |
|
34 | - [ |
|
35 | - 'name' => 'Settings#removeServer', |
|
36 | - 'url' => '/trusted-servers/{id}', |
|
37 | - 'verb' => 'DELETE' |
|
38 | - ], |
|
39 | - [ |
|
40 | - 'name' => 'Settings#autoAddServers', |
|
41 | - 'url' => '/auto-add-servers', |
|
42 | - 'verb' => 'POST' |
|
43 | - ], |
|
44 | - ], |
|
45 | - 'ocs' => [ |
|
46 | - // old endpoints, only used by Nextcloud and ownCloud |
|
47 | - [ |
|
48 | - 'name' => 'OCSAuthAPI#getSharedSecretLegacy', |
|
49 | - 'url' => '/api/v1/shared-secret', |
|
50 | - 'verb' => 'GET', |
|
51 | - ], |
|
52 | - [ |
|
53 | - 'name' => 'OCSAuthAPI#requestSharedSecretLegacy', |
|
54 | - 'url' => '/api/v1/request-shared-secret', |
|
55 | - 'verb' => 'POST', |
|
56 | - ], |
|
57 | - // new endpoints, published as public api |
|
58 | - [ |
|
59 | - 'name' => 'OCSAuthAPI#getSharedSecret', |
|
60 | - 'root' => '/cloud', |
|
61 | - 'url' => '/shared-secret', |
|
62 | - 'verb' => 'GET', |
|
63 | - ], |
|
64 | - [ |
|
65 | - 'name' => 'OCSAuthAPI#requestSharedSecret', |
|
66 | - 'root' => '/cloud', |
|
67 | - 'url' => '/shared-secret', |
|
68 | - 'verb' => 'POST', |
|
69 | - ], |
|
70 | - ], |
|
71 | - ] |
|
26 | + $this, |
|
27 | + [ |
|
28 | + 'routes' => [ |
|
29 | + [ |
|
30 | + 'name' => 'Settings#addServer', |
|
31 | + 'url' => '/trusted-servers', |
|
32 | + 'verb' => 'POST' |
|
33 | + ], |
|
34 | + [ |
|
35 | + 'name' => 'Settings#removeServer', |
|
36 | + 'url' => '/trusted-servers/{id}', |
|
37 | + 'verb' => 'DELETE' |
|
38 | + ], |
|
39 | + [ |
|
40 | + 'name' => 'Settings#autoAddServers', |
|
41 | + 'url' => '/auto-add-servers', |
|
42 | + 'verb' => 'POST' |
|
43 | + ], |
|
44 | + ], |
|
45 | + 'ocs' => [ |
|
46 | + // old endpoints, only used by Nextcloud and ownCloud |
|
47 | + [ |
|
48 | + 'name' => 'OCSAuthAPI#getSharedSecretLegacy', |
|
49 | + 'url' => '/api/v1/shared-secret', |
|
50 | + 'verb' => 'GET', |
|
51 | + ], |
|
52 | + [ |
|
53 | + 'name' => 'OCSAuthAPI#requestSharedSecretLegacy', |
|
54 | + 'url' => '/api/v1/request-shared-secret', |
|
55 | + 'verb' => 'POST', |
|
56 | + ], |
|
57 | + // new endpoints, published as public api |
|
58 | + [ |
|
59 | + 'name' => 'OCSAuthAPI#getSharedSecret', |
|
60 | + 'root' => '/cloud', |
|
61 | + 'url' => '/shared-secret', |
|
62 | + 'verb' => 'GET', |
|
63 | + ], |
|
64 | + [ |
|
65 | + 'name' => 'OCSAuthAPI#requestSharedSecret', |
|
66 | + 'root' => '/cloud', |
|
67 | + 'url' => '/shared-secret', |
|
68 | + 'verb' => 'POST', |
|
69 | + ], |
|
70 | + ], |
|
71 | + ] |
|
72 | 72 | ); |
@@ -410,6 +410,9 @@ discard block |
||
410 | 410 | } |
411 | 411 | } |
412 | 412 | |
413 | + /** |
|
414 | + * @param string $path |
|
415 | + */ |
|
413 | 416 | private function getParentPath($path) { |
414 | 417 | $parent = dirname($path); |
415 | 418 | if ($parent === '.') { |
@@ -629,7 +632,7 @@ discard block |
||
629 | 632 | * |
630 | 633 | * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image') |
631 | 634 | * where it will search for all mimetypes in the group ('image/*') |
632 | - * @return ICacheEntry[] an array of cache entries where the mimetype matches the search |
|
635 | + * @return CacheEntry[] an array of cache entries where the mimetype matches the search |
|
633 | 636 | */ |
634 | 637 | public function searchByMime($mimetype) { |
635 | 638 | if (strpos($mimetype, '/')) { |
@@ -56,842 +56,842 @@ |
||
56 | 56 | * - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater |
57 | 57 | */ |
58 | 58 | class Cache implements ICache { |
59 | - use MoveFromCacheTrait { |
|
60 | - MoveFromCacheTrait::moveFromCache as moveFromCacheFallback; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @var array partial data for the cache |
|
65 | - */ |
|
66 | - protected $partial = array(); |
|
67 | - |
|
68 | - /** |
|
69 | - * @var string |
|
70 | - */ |
|
71 | - protected $storageId; |
|
72 | - |
|
73 | - /** |
|
74 | - * @var Storage $storageCache |
|
75 | - */ |
|
76 | - protected $storageCache; |
|
77 | - |
|
78 | - /** @var IMimeTypeLoader */ |
|
79 | - protected $mimetypeLoader; |
|
80 | - |
|
81 | - /** |
|
82 | - * @var IDBConnection |
|
83 | - */ |
|
84 | - protected $connection; |
|
85 | - |
|
86 | - /** @var QuerySearchHelper */ |
|
87 | - protected $querySearchHelper; |
|
88 | - |
|
89 | - /** |
|
90 | - * @param \OC\Files\Storage\Storage|string $storage |
|
91 | - */ |
|
92 | - public function __construct($storage) { |
|
93 | - if ($storage instanceof \OC\Files\Storage\Storage) { |
|
94 | - $this->storageId = $storage->getId(); |
|
95 | - } else { |
|
96 | - $this->storageId = $storage; |
|
97 | - } |
|
98 | - if (strlen($this->storageId) > 64) { |
|
99 | - $this->storageId = md5($this->storageId); |
|
100 | - } |
|
101 | - |
|
102 | - $this->storageCache = new Storage($storage); |
|
103 | - $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
104 | - $this->connection = \OC::$server->getDatabaseConnection(); |
|
105 | - $this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Get the numeric storage id for this cache's storage |
|
110 | - * |
|
111 | - * @return int |
|
112 | - */ |
|
113 | - public function getNumericStorageId() { |
|
114 | - return $this->storageCache->getNumericId(); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * get the stored metadata of a file or folder |
|
119 | - * |
|
120 | - * @param string | int $file either the path of a file or folder or the file id for a file or folder |
|
121 | - * @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache |
|
122 | - */ |
|
123 | - public function get($file) { |
|
124 | - if (is_string($file) or $file == '') { |
|
125 | - // normalize file |
|
126 | - $file = $this->normalize($file); |
|
127 | - |
|
128 | - $where = 'WHERE `storage` = ? AND `path_hash` = ?'; |
|
129 | - $params = array($this->getNumericStorageId(), md5($file)); |
|
130 | - } else { //file id |
|
131 | - $where = 'WHERE `fileid` = ?'; |
|
132 | - $params = array($file); |
|
133 | - } |
|
134 | - $sql = 'SELECT `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
59 | + use MoveFromCacheTrait { |
|
60 | + MoveFromCacheTrait::moveFromCache as moveFromCacheFallback; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @var array partial data for the cache |
|
65 | + */ |
|
66 | + protected $partial = array(); |
|
67 | + |
|
68 | + /** |
|
69 | + * @var string |
|
70 | + */ |
|
71 | + protected $storageId; |
|
72 | + |
|
73 | + /** |
|
74 | + * @var Storage $storageCache |
|
75 | + */ |
|
76 | + protected $storageCache; |
|
77 | + |
|
78 | + /** @var IMimeTypeLoader */ |
|
79 | + protected $mimetypeLoader; |
|
80 | + |
|
81 | + /** |
|
82 | + * @var IDBConnection |
|
83 | + */ |
|
84 | + protected $connection; |
|
85 | + |
|
86 | + /** @var QuerySearchHelper */ |
|
87 | + protected $querySearchHelper; |
|
88 | + |
|
89 | + /** |
|
90 | + * @param \OC\Files\Storage\Storage|string $storage |
|
91 | + */ |
|
92 | + public function __construct($storage) { |
|
93 | + if ($storage instanceof \OC\Files\Storage\Storage) { |
|
94 | + $this->storageId = $storage->getId(); |
|
95 | + } else { |
|
96 | + $this->storageId = $storage; |
|
97 | + } |
|
98 | + if (strlen($this->storageId) > 64) { |
|
99 | + $this->storageId = md5($this->storageId); |
|
100 | + } |
|
101 | + |
|
102 | + $this->storageCache = new Storage($storage); |
|
103 | + $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
104 | + $this->connection = \OC::$server->getDatabaseConnection(); |
|
105 | + $this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Get the numeric storage id for this cache's storage |
|
110 | + * |
|
111 | + * @return int |
|
112 | + */ |
|
113 | + public function getNumericStorageId() { |
|
114 | + return $this->storageCache->getNumericId(); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * get the stored metadata of a file or folder |
|
119 | + * |
|
120 | + * @param string | int $file either the path of a file or folder or the file id for a file or folder |
|
121 | + * @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache |
|
122 | + */ |
|
123 | + public function get($file) { |
|
124 | + if (is_string($file) or $file == '') { |
|
125 | + // normalize file |
|
126 | + $file = $this->normalize($file); |
|
127 | + |
|
128 | + $where = 'WHERE `storage` = ? AND `path_hash` = ?'; |
|
129 | + $params = array($this->getNumericStorageId(), md5($file)); |
|
130 | + } else { //file id |
|
131 | + $where = 'WHERE `fileid` = ?'; |
|
132 | + $params = array($file); |
|
133 | + } |
|
134 | + $sql = 'SELECT `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
135 | 135 | `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
136 | 136 | FROM `*PREFIX*filecache` ' . $where; |
137 | - $result = $this->connection->executeQuery($sql, $params); |
|
138 | - $data = $result->fetch(); |
|
139 | - |
|
140 | - //FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO |
|
141 | - //PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false |
|
142 | - if ($data === null) { |
|
143 | - $data = false; |
|
144 | - } |
|
145 | - |
|
146 | - //merge partial data |
|
147 | - if (!$data and is_string($file)) { |
|
148 | - if (isset($this->partial[$file])) { |
|
149 | - $data = $this->partial[$file]; |
|
150 | - } |
|
151 | - return $data; |
|
152 | - } else { |
|
153 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Create a CacheEntry from database row |
|
159 | - * |
|
160 | - * @param array $data |
|
161 | - * @param IMimeTypeLoader $mimetypeLoader |
|
162 | - * @return CacheEntry |
|
163 | - */ |
|
164 | - public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) { |
|
165 | - //fix types |
|
166 | - $data['fileid'] = (int)$data['fileid']; |
|
167 | - $data['parent'] = (int)$data['parent']; |
|
168 | - $data['size'] = 0 + $data['size']; |
|
169 | - $data['mtime'] = (int)$data['mtime']; |
|
170 | - $data['storage_mtime'] = (int)$data['storage_mtime']; |
|
171 | - $data['encryptedVersion'] = (int)$data['encrypted']; |
|
172 | - $data['encrypted'] = (bool)$data['encrypted']; |
|
173 | - $data['storage_id'] = $data['storage']; |
|
174 | - $data['storage'] = (int)$data['storage']; |
|
175 | - $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']); |
|
176 | - $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']); |
|
177 | - if ($data['storage_mtime'] == 0) { |
|
178 | - $data['storage_mtime'] = $data['mtime']; |
|
179 | - } |
|
180 | - $data['permissions'] = (int)$data['permissions']; |
|
181 | - return new CacheEntry($data); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * get the metadata of all files stored in $folder |
|
186 | - * |
|
187 | - * @param string $folder |
|
188 | - * @return ICacheEntry[] |
|
189 | - */ |
|
190 | - public function getFolderContents($folder) { |
|
191 | - $fileId = $this->getId($folder); |
|
192 | - return $this->getFolderContentsById($fileId); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * get the metadata of all files stored in $folder |
|
197 | - * |
|
198 | - * @param int $fileId the file id of the folder |
|
199 | - * @return ICacheEntry[] |
|
200 | - */ |
|
201 | - public function getFolderContentsById($fileId) { |
|
202 | - if ($fileId > -1) { |
|
203 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
137 | + $result = $this->connection->executeQuery($sql, $params); |
|
138 | + $data = $result->fetch(); |
|
139 | + |
|
140 | + //FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO |
|
141 | + //PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false |
|
142 | + if ($data === null) { |
|
143 | + $data = false; |
|
144 | + } |
|
145 | + |
|
146 | + //merge partial data |
|
147 | + if (!$data and is_string($file)) { |
|
148 | + if (isset($this->partial[$file])) { |
|
149 | + $data = $this->partial[$file]; |
|
150 | + } |
|
151 | + return $data; |
|
152 | + } else { |
|
153 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Create a CacheEntry from database row |
|
159 | + * |
|
160 | + * @param array $data |
|
161 | + * @param IMimeTypeLoader $mimetypeLoader |
|
162 | + * @return CacheEntry |
|
163 | + */ |
|
164 | + public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) { |
|
165 | + //fix types |
|
166 | + $data['fileid'] = (int)$data['fileid']; |
|
167 | + $data['parent'] = (int)$data['parent']; |
|
168 | + $data['size'] = 0 + $data['size']; |
|
169 | + $data['mtime'] = (int)$data['mtime']; |
|
170 | + $data['storage_mtime'] = (int)$data['storage_mtime']; |
|
171 | + $data['encryptedVersion'] = (int)$data['encrypted']; |
|
172 | + $data['encrypted'] = (bool)$data['encrypted']; |
|
173 | + $data['storage_id'] = $data['storage']; |
|
174 | + $data['storage'] = (int)$data['storage']; |
|
175 | + $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']); |
|
176 | + $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']); |
|
177 | + if ($data['storage_mtime'] == 0) { |
|
178 | + $data['storage_mtime'] = $data['mtime']; |
|
179 | + } |
|
180 | + $data['permissions'] = (int)$data['permissions']; |
|
181 | + return new CacheEntry($data); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * get the metadata of all files stored in $folder |
|
186 | + * |
|
187 | + * @param string $folder |
|
188 | + * @return ICacheEntry[] |
|
189 | + */ |
|
190 | + public function getFolderContents($folder) { |
|
191 | + $fileId = $this->getId($folder); |
|
192 | + return $this->getFolderContentsById($fileId); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * get the metadata of all files stored in $folder |
|
197 | + * |
|
198 | + * @param int $fileId the file id of the folder |
|
199 | + * @return ICacheEntry[] |
|
200 | + */ |
|
201 | + public function getFolderContentsById($fileId) { |
|
202 | + if ($fileId > -1) { |
|
203 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
204 | 204 | `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
205 | 205 | FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC'; |
206 | - $result = $this->connection->executeQuery($sql, [$fileId]); |
|
207 | - $files = $result->fetchAll(); |
|
208 | - return array_map(function (array $data) { |
|
209 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
210 | - }, $files); |
|
211 | - } |
|
212 | - return []; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * insert or update meta data for a file or folder |
|
217 | - * |
|
218 | - * @param string $file |
|
219 | - * @param array $data |
|
220 | - * |
|
221 | - * @return int file id |
|
222 | - * @throws \RuntimeException |
|
223 | - */ |
|
224 | - public function put($file, array $data) { |
|
225 | - if (($id = $this->getId($file)) > -1) { |
|
226 | - $this->update($id, $data); |
|
227 | - return $id; |
|
228 | - } else { |
|
229 | - return $this->insert($file, $data); |
|
230 | - } |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * insert meta data for a new file or folder |
|
235 | - * |
|
236 | - * @param string $file |
|
237 | - * @param array $data |
|
238 | - * |
|
239 | - * @return int file id |
|
240 | - * @throws \RuntimeException |
|
241 | - */ |
|
242 | - public function insert($file, array $data) { |
|
243 | - // normalize file |
|
244 | - $file = $this->normalize($file); |
|
245 | - |
|
246 | - if (isset($this->partial[$file])) { //add any saved partial data |
|
247 | - $data = array_merge($this->partial[$file], $data); |
|
248 | - unset($this->partial[$file]); |
|
249 | - } |
|
250 | - |
|
251 | - $requiredFields = array('size', 'mtime', 'mimetype'); |
|
252 | - foreach ($requiredFields as $field) { |
|
253 | - if (!isset($data[$field])) { //data not complete save as partial and return |
|
254 | - $this->partial[$file] = $data; |
|
255 | - return -1; |
|
256 | - } |
|
257 | - } |
|
258 | - |
|
259 | - $data['path'] = $file; |
|
260 | - $data['parent'] = $this->getParentId($file); |
|
261 | - $data['name'] = basename($file); |
|
262 | - |
|
263 | - list($queryParts, $params) = $this->buildParts($data); |
|
264 | - $queryParts[] = '`storage`'; |
|
265 | - $params[] = $this->getNumericStorageId(); |
|
266 | - |
|
267 | - $queryParts = array_map(function ($item) { |
|
268 | - return trim($item, "`"); |
|
269 | - }, $queryParts); |
|
270 | - $values = array_combine($queryParts, $params); |
|
271 | - if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [ |
|
272 | - 'storage', |
|
273 | - 'path_hash', |
|
274 | - ]) |
|
275 | - ) { |
|
276 | - return (int)$this->connection->lastInsertId('*PREFIX*filecache'); |
|
277 | - } |
|
278 | - |
|
279 | - // The file was created in the mean time |
|
280 | - if (($id = $this->getId($file)) > -1) { |
|
281 | - $this->update($id, $data); |
|
282 | - return $id; |
|
283 | - } else { |
|
284 | - throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.'); |
|
285 | - } |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * update the metadata of an existing file or folder in the cache |
|
290 | - * |
|
291 | - * @param int $id the fileid of the existing file or folder |
|
292 | - * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged |
|
293 | - */ |
|
294 | - public function update($id, array $data) { |
|
295 | - |
|
296 | - if (isset($data['path'])) { |
|
297 | - // normalize path |
|
298 | - $data['path'] = $this->normalize($data['path']); |
|
299 | - } |
|
300 | - |
|
301 | - if (isset($data['name'])) { |
|
302 | - // normalize path |
|
303 | - $data['name'] = $this->normalize($data['name']); |
|
304 | - } |
|
305 | - |
|
306 | - list($queryParts, $params) = $this->buildParts($data); |
|
307 | - // duplicate $params because we need the parts twice in the SQL statement |
|
308 | - // once for the SET part, once in the WHERE clause |
|
309 | - $params = array_merge($params, $params); |
|
310 | - $params[] = $id; |
|
311 | - |
|
312 | - // don't update if the data we try to set is the same as the one in the record |
|
313 | - // some databases (Postgres) don't like superfluous updates |
|
314 | - $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' . |
|
315 | - 'WHERE (' . |
|
316 | - implode(' <> ? OR ', $queryParts) . ' <> ? OR ' . |
|
317 | - implode(' IS NULL OR ', $queryParts) . ' IS NULL' . |
|
318 | - ') AND `fileid` = ? '; |
|
319 | - $this->connection->executeQuery($sql, $params); |
|
320 | - |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * extract query parts and params array from data array |
|
325 | - * |
|
326 | - * @param array $data |
|
327 | - * @return array [$queryParts, $params] |
|
328 | - * $queryParts: string[], the (escaped) column names to be set in the query |
|
329 | - * $params: mixed[], the new values for the columns, to be passed as params to the query |
|
330 | - */ |
|
331 | - protected function buildParts(array $data) { |
|
332 | - $fields = array( |
|
333 | - 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted', |
|
334 | - 'etag', 'permissions', 'checksum', 'storage'); |
|
335 | - |
|
336 | - $doNotCopyStorageMTime = false; |
|
337 | - if (array_key_exists('mtime', $data) && $data['mtime'] === null) { |
|
338 | - // this horrific magic tells it to not copy storage_mtime to mtime |
|
339 | - unset($data['mtime']); |
|
340 | - $doNotCopyStorageMTime = true; |
|
341 | - } |
|
342 | - |
|
343 | - $params = array(); |
|
344 | - $queryParts = array(); |
|
345 | - foreach ($data as $name => $value) { |
|
346 | - if (array_search($name, $fields) !== false) { |
|
347 | - if ($name === 'path') { |
|
348 | - $params[] = md5($value); |
|
349 | - $queryParts[] = '`path_hash`'; |
|
350 | - } elseif ($name === 'mimetype') { |
|
351 | - $params[] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/'))); |
|
352 | - $queryParts[] = '`mimepart`'; |
|
353 | - $value = $this->mimetypeLoader->getId($value); |
|
354 | - } elseif ($name === 'storage_mtime') { |
|
355 | - if (!$doNotCopyStorageMTime && !isset($data['mtime'])) { |
|
356 | - $params[] = $value; |
|
357 | - $queryParts[] = '`mtime`'; |
|
358 | - } |
|
359 | - } elseif ($name === 'encrypted') { |
|
360 | - if (isset($data['encryptedVersion'])) { |
|
361 | - $value = $data['encryptedVersion']; |
|
362 | - } else { |
|
363 | - // Boolean to integer conversion |
|
364 | - $value = $value ? 1 : 0; |
|
365 | - } |
|
366 | - } |
|
367 | - $params[] = $value; |
|
368 | - $queryParts[] = '`' . $name . '`'; |
|
369 | - } |
|
370 | - } |
|
371 | - return array($queryParts, $params); |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * get the file id for a file |
|
376 | - * |
|
377 | - * A file id is a numeric id for a file or folder that's unique within an owncloud instance which stays the same for the lifetime of a file |
|
378 | - * |
|
379 | - * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing |
|
380 | - * |
|
381 | - * @param string $file |
|
382 | - * @return int |
|
383 | - */ |
|
384 | - public function getId($file) { |
|
385 | - // normalize file |
|
386 | - $file = $this->normalize($file); |
|
387 | - |
|
388 | - $pathHash = md5($file); |
|
389 | - |
|
390 | - $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
391 | - $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
392 | - if ($row = $result->fetch()) { |
|
393 | - return $row['fileid']; |
|
394 | - } else { |
|
395 | - return -1; |
|
396 | - } |
|
397 | - } |
|
398 | - |
|
399 | - /** |
|
400 | - * get the id of the parent folder of a file |
|
401 | - * |
|
402 | - * @param string $file |
|
403 | - * @return int |
|
404 | - */ |
|
405 | - public function getParentId($file) { |
|
406 | - if ($file === '') { |
|
407 | - return -1; |
|
408 | - } else { |
|
409 | - $parent = $this->getParentPath($file); |
|
410 | - return (int)$this->getId($parent); |
|
411 | - } |
|
412 | - } |
|
413 | - |
|
414 | - private function getParentPath($path) { |
|
415 | - $parent = dirname($path); |
|
416 | - if ($parent === '.') { |
|
417 | - $parent = ''; |
|
418 | - } |
|
419 | - return $parent; |
|
420 | - } |
|
421 | - |
|
422 | - /** |
|
423 | - * check if a file is available in the cache |
|
424 | - * |
|
425 | - * @param string $file |
|
426 | - * @return bool |
|
427 | - */ |
|
428 | - public function inCache($file) { |
|
429 | - return $this->getId($file) != -1; |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * remove a file or folder from the cache |
|
434 | - * |
|
435 | - * when removing a folder from the cache all files and folders inside the folder will be removed as well |
|
436 | - * |
|
437 | - * @param string $file |
|
438 | - */ |
|
439 | - public function remove($file) { |
|
440 | - $entry = $this->get($file); |
|
441 | - $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
442 | - $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
443 | - if ($entry['mimetype'] === 'httpd/unix-directory') { |
|
444 | - $this->removeChildren($entry); |
|
445 | - } |
|
446 | - } |
|
447 | - |
|
448 | - /** |
|
449 | - * Get all sub folders of a folder |
|
450 | - * |
|
451 | - * @param array $entry the cache entry of the folder to get the subfolders for |
|
452 | - * @return array[] the cache entries for the subfolders |
|
453 | - */ |
|
454 | - private function getSubFolders($entry) { |
|
455 | - $children = $this->getFolderContentsById($entry['fileid']); |
|
456 | - return array_filter($children, function ($child) { |
|
457 | - return $child['mimetype'] === 'httpd/unix-directory'; |
|
458 | - }); |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Recursively remove all children of a folder |
|
463 | - * |
|
464 | - * @param array $entry the cache entry of the folder to remove the children of |
|
465 | - * @throws \OC\DatabaseException |
|
466 | - */ |
|
467 | - private function removeChildren($entry) { |
|
468 | - $subFolders = $this->getSubFolders($entry); |
|
469 | - foreach ($subFolders as $folder) { |
|
470 | - $this->removeChildren($folder); |
|
471 | - } |
|
472 | - $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?'; |
|
473 | - $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * Move a file or folder in the cache |
|
478 | - * |
|
479 | - * @param string $source |
|
480 | - * @param string $target |
|
481 | - */ |
|
482 | - public function move($source, $target) { |
|
483 | - $this->moveFromCache($this, $source, $target); |
|
484 | - } |
|
485 | - |
|
486 | - /** |
|
487 | - * Get the storage id and path needed for a move |
|
488 | - * |
|
489 | - * @param string $path |
|
490 | - * @return array [$storageId, $internalPath] |
|
491 | - */ |
|
492 | - protected function getMoveInfo($path) { |
|
493 | - return [$this->getNumericStorageId(), $path]; |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * Move a file or folder in the cache |
|
498 | - * |
|
499 | - * @param \OCP\Files\Cache\ICache $sourceCache |
|
500 | - * @param string $sourcePath |
|
501 | - * @param string $targetPath |
|
502 | - * @throws \OC\DatabaseException |
|
503 | - * @throws \Exception if the given storages have an invalid id |
|
504 | - * @suppress SqlInjectionChecker |
|
505 | - */ |
|
506 | - public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) { |
|
507 | - if ($sourceCache instanceof Cache) { |
|
508 | - // normalize source and target |
|
509 | - $sourcePath = $this->normalize($sourcePath); |
|
510 | - $targetPath = $this->normalize($targetPath); |
|
511 | - |
|
512 | - $sourceData = $sourceCache->get($sourcePath); |
|
513 | - $sourceId = $sourceData['fileid']; |
|
514 | - $newParentId = $this->getParentId($targetPath); |
|
515 | - |
|
516 | - list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath); |
|
517 | - list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); |
|
518 | - |
|
519 | - if (is_null($sourceStorageId) || $sourceStorageId === false) { |
|
520 | - throw new \Exception('Invalid source storage id: ' . $sourceStorageId); |
|
521 | - } |
|
522 | - if (is_null($targetStorageId) || $targetStorageId === false) { |
|
523 | - throw new \Exception('Invalid target storage id: ' . $targetStorageId); |
|
524 | - } |
|
525 | - |
|
526 | - $this->connection->beginTransaction(); |
|
527 | - if ($sourceData['mimetype'] === 'httpd/unix-directory') { |
|
528 | - //update all child entries |
|
529 | - $sourceLength = mb_strlen($sourcePath); |
|
530 | - $query = $this->connection->getQueryBuilder(); |
|
531 | - |
|
532 | - $fun = $query->func(); |
|
533 | - $newPathFunction = $fun->concat( |
|
534 | - $query->createNamedParameter($targetPath), |
|
535 | - $fun->substring('path', $query->createNamedParameter($sourceLength + 1, IQueryBuilder::PARAM_INT))// +1 for the leading slash |
|
536 | - ); |
|
537 | - $query->update('filecache') |
|
538 | - ->set('storage', $query->createNamedParameter($targetStorageId, IQueryBuilder::PARAM_INT)) |
|
539 | - ->set('path_hash', $fun->md5($newPathFunction)) |
|
540 | - ->set('path', $newPathFunction) |
|
541 | - ->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT))) |
|
542 | - ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%'))); |
|
543 | - |
|
544 | - try { |
|
545 | - $query->execute(); |
|
546 | - } catch (\OC\DatabaseException $e) { |
|
547 | - $this->connection->rollBack(); |
|
548 | - throw $e; |
|
549 | - } |
|
550 | - } |
|
551 | - |
|
552 | - $sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` = ? WHERE `fileid` = ?'; |
|
553 | - $this->connection->executeQuery($sql, array($targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId)); |
|
554 | - $this->connection->commit(); |
|
555 | - } else { |
|
556 | - $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath); |
|
557 | - } |
|
558 | - } |
|
559 | - |
|
560 | - /** |
|
561 | - * remove all entries for files that are stored on the storage from the cache |
|
562 | - */ |
|
563 | - public function clear() { |
|
564 | - $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
565 | - $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
566 | - |
|
567 | - $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?'; |
|
568 | - $this->connection->executeQuery($sql, array($this->storageId)); |
|
569 | - } |
|
570 | - |
|
571 | - /** |
|
572 | - * Get the scan status of a file |
|
573 | - * |
|
574 | - * - Cache::NOT_FOUND: File is not in the cache |
|
575 | - * - Cache::PARTIAL: File is not stored in the cache but some incomplete data is known |
|
576 | - * - Cache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned |
|
577 | - * - Cache::COMPLETE: The file or folder, with all it's children) are fully scanned |
|
578 | - * |
|
579 | - * @param string $file |
|
580 | - * |
|
581 | - * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
|
582 | - */ |
|
583 | - public function getStatus($file) { |
|
584 | - // normalize file |
|
585 | - $file = $this->normalize($file); |
|
586 | - |
|
587 | - $pathHash = md5($file); |
|
588 | - $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
589 | - $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
590 | - if ($row = $result->fetch()) { |
|
591 | - if ((int)$row['size'] === -1) { |
|
592 | - return self::SHALLOW; |
|
593 | - } else { |
|
594 | - return self::COMPLETE; |
|
595 | - } |
|
596 | - } else { |
|
597 | - if (isset($this->partial[$file])) { |
|
598 | - return self::PARTIAL; |
|
599 | - } else { |
|
600 | - return self::NOT_FOUND; |
|
601 | - } |
|
602 | - } |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * search for files matching $pattern |
|
607 | - * |
|
608 | - * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%') |
|
609 | - * @return ICacheEntry[] an array of cache entries where the name matches the search pattern |
|
610 | - */ |
|
611 | - public function search($pattern) { |
|
612 | - // normalize pattern |
|
613 | - $pattern = $this->normalize($pattern); |
|
614 | - |
|
615 | - if ($pattern === '%%') { |
|
616 | - return []; |
|
617 | - } |
|
618 | - |
|
619 | - |
|
620 | - $sql = ' |
|
206 | + $result = $this->connection->executeQuery($sql, [$fileId]); |
|
207 | + $files = $result->fetchAll(); |
|
208 | + return array_map(function (array $data) { |
|
209 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
210 | + }, $files); |
|
211 | + } |
|
212 | + return []; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * insert or update meta data for a file or folder |
|
217 | + * |
|
218 | + * @param string $file |
|
219 | + * @param array $data |
|
220 | + * |
|
221 | + * @return int file id |
|
222 | + * @throws \RuntimeException |
|
223 | + */ |
|
224 | + public function put($file, array $data) { |
|
225 | + if (($id = $this->getId($file)) > -1) { |
|
226 | + $this->update($id, $data); |
|
227 | + return $id; |
|
228 | + } else { |
|
229 | + return $this->insert($file, $data); |
|
230 | + } |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * insert meta data for a new file or folder |
|
235 | + * |
|
236 | + * @param string $file |
|
237 | + * @param array $data |
|
238 | + * |
|
239 | + * @return int file id |
|
240 | + * @throws \RuntimeException |
|
241 | + */ |
|
242 | + public function insert($file, array $data) { |
|
243 | + // normalize file |
|
244 | + $file = $this->normalize($file); |
|
245 | + |
|
246 | + if (isset($this->partial[$file])) { //add any saved partial data |
|
247 | + $data = array_merge($this->partial[$file], $data); |
|
248 | + unset($this->partial[$file]); |
|
249 | + } |
|
250 | + |
|
251 | + $requiredFields = array('size', 'mtime', 'mimetype'); |
|
252 | + foreach ($requiredFields as $field) { |
|
253 | + if (!isset($data[$field])) { //data not complete save as partial and return |
|
254 | + $this->partial[$file] = $data; |
|
255 | + return -1; |
|
256 | + } |
|
257 | + } |
|
258 | + |
|
259 | + $data['path'] = $file; |
|
260 | + $data['parent'] = $this->getParentId($file); |
|
261 | + $data['name'] = basename($file); |
|
262 | + |
|
263 | + list($queryParts, $params) = $this->buildParts($data); |
|
264 | + $queryParts[] = '`storage`'; |
|
265 | + $params[] = $this->getNumericStorageId(); |
|
266 | + |
|
267 | + $queryParts = array_map(function ($item) { |
|
268 | + return trim($item, "`"); |
|
269 | + }, $queryParts); |
|
270 | + $values = array_combine($queryParts, $params); |
|
271 | + if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [ |
|
272 | + 'storage', |
|
273 | + 'path_hash', |
|
274 | + ]) |
|
275 | + ) { |
|
276 | + return (int)$this->connection->lastInsertId('*PREFIX*filecache'); |
|
277 | + } |
|
278 | + |
|
279 | + // The file was created in the mean time |
|
280 | + if (($id = $this->getId($file)) > -1) { |
|
281 | + $this->update($id, $data); |
|
282 | + return $id; |
|
283 | + } else { |
|
284 | + throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.'); |
|
285 | + } |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * update the metadata of an existing file or folder in the cache |
|
290 | + * |
|
291 | + * @param int $id the fileid of the existing file or folder |
|
292 | + * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged |
|
293 | + */ |
|
294 | + public function update($id, array $data) { |
|
295 | + |
|
296 | + if (isset($data['path'])) { |
|
297 | + // normalize path |
|
298 | + $data['path'] = $this->normalize($data['path']); |
|
299 | + } |
|
300 | + |
|
301 | + if (isset($data['name'])) { |
|
302 | + // normalize path |
|
303 | + $data['name'] = $this->normalize($data['name']); |
|
304 | + } |
|
305 | + |
|
306 | + list($queryParts, $params) = $this->buildParts($data); |
|
307 | + // duplicate $params because we need the parts twice in the SQL statement |
|
308 | + // once for the SET part, once in the WHERE clause |
|
309 | + $params = array_merge($params, $params); |
|
310 | + $params[] = $id; |
|
311 | + |
|
312 | + // don't update if the data we try to set is the same as the one in the record |
|
313 | + // some databases (Postgres) don't like superfluous updates |
|
314 | + $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' . |
|
315 | + 'WHERE (' . |
|
316 | + implode(' <> ? OR ', $queryParts) . ' <> ? OR ' . |
|
317 | + implode(' IS NULL OR ', $queryParts) . ' IS NULL' . |
|
318 | + ') AND `fileid` = ? '; |
|
319 | + $this->connection->executeQuery($sql, $params); |
|
320 | + |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * extract query parts and params array from data array |
|
325 | + * |
|
326 | + * @param array $data |
|
327 | + * @return array [$queryParts, $params] |
|
328 | + * $queryParts: string[], the (escaped) column names to be set in the query |
|
329 | + * $params: mixed[], the new values for the columns, to be passed as params to the query |
|
330 | + */ |
|
331 | + protected function buildParts(array $data) { |
|
332 | + $fields = array( |
|
333 | + 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted', |
|
334 | + 'etag', 'permissions', 'checksum', 'storage'); |
|
335 | + |
|
336 | + $doNotCopyStorageMTime = false; |
|
337 | + if (array_key_exists('mtime', $data) && $data['mtime'] === null) { |
|
338 | + // this horrific magic tells it to not copy storage_mtime to mtime |
|
339 | + unset($data['mtime']); |
|
340 | + $doNotCopyStorageMTime = true; |
|
341 | + } |
|
342 | + |
|
343 | + $params = array(); |
|
344 | + $queryParts = array(); |
|
345 | + foreach ($data as $name => $value) { |
|
346 | + if (array_search($name, $fields) !== false) { |
|
347 | + if ($name === 'path') { |
|
348 | + $params[] = md5($value); |
|
349 | + $queryParts[] = '`path_hash`'; |
|
350 | + } elseif ($name === 'mimetype') { |
|
351 | + $params[] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/'))); |
|
352 | + $queryParts[] = '`mimepart`'; |
|
353 | + $value = $this->mimetypeLoader->getId($value); |
|
354 | + } elseif ($name === 'storage_mtime') { |
|
355 | + if (!$doNotCopyStorageMTime && !isset($data['mtime'])) { |
|
356 | + $params[] = $value; |
|
357 | + $queryParts[] = '`mtime`'; |
|
358 | + } |
|
359 | + } elseif ($name === 'encrypted') { |
|
360 | + if (isset($data['encryptedVersion'])) { |
|
361 | + $value = $data['encryptedVersion']; |
|
362 | + } else { |
|
363 | + // Boolean to integer conversion |
|
364 | + $value = $value ? 1 : 0; |
|
365 | + } |
|
366 | + } |
|
367 | + $params[] = $value; |
|
368 | + $queryParts[] = '`' . $name . '`'; |
|
369 | + } |
|
370 | + } |
|
371 | + return array($queryParts, $params); |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * get the file id for a file |
|
376 | + * |
|
377 | + * A file id is a numeric id for a file or folder that's unique within an owncloud instance which stays the same for the lifetime of a file |
|
378 | + * |
|
379 | + * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing |
|
380 | + * |
|
381 | + * @param string $file |
|
382 | + * @return int |
|
383 | + */ |
|
384 | + public function getId($file) { |
|
385 | + // normalize file |
|
386 | + $file = $this->normalize($file); |
|
387 | + |
|
388 | + $pathHash = md5($file); |
|
389 | + |
|
390 | + $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
391 | + $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
392 | + if ($row = $result->fetch()) { |
|
393 | + return $row['fileid']; |
|
394 | + } else { |
|
395 | + return -1; |
|
396 | + } |
|
397 | + } |
|
398 | + |
|
399 | + /** |
|
400 | + * get the id of the parent folder of a file |
|
401 | + * |
|
402 | + * @param string $file |
|
403 | + * @return int |
|
404 | + */ |
|
405 | + public function getParentId($file) { |
|
406 | + if ($file === '') { |
|
407 | + return -1; |
|
408 | + } else { |
|
409 | + $parent = $this->getParentPath($file); |
|
410 | + return (int)$this->getId($parent); |
|
411 | + } |
|
412 | + } |
|
413 | + |
|
414 | + private function getParentPath($path) { |
|
415 | + $parent = dirname($path); |
|
416 | + if ($parent === '.') { |
|
417 | + $parent = ''; |
|
418 | + } |
|
419 | + return $parent; |
|
420 | + } |
|
421 | + |
|
422 | + /** |
|
423 | + * check if a file is available in the cache |
|
424 | + * |
|
425 | + * @param string $file |
|
426 | + * @return bool |
|
427 | + */ |
|
428 | + public function inCache($file) { |
|
429 | + return $this->getId($file) != -1; |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * remove a file or folder from the cache |
|
434 | + * |
|
435 | + * when removing a folder from the cache all files and folders inside the folder will be removed as well |
|
436 | + * |
|
437 | + * @param string $file |
|
438 | + */ |
|
439 | + public function remove($file) { |
|
440 | + $entry = $this->get($file); |
|
441 | + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
442 | + $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
443 | + if ($entry['mimetype'] === 'httpd/unix-directory') { |
|
444 | + $this->removeChildren($entry); |
|
445 | + } |
|
446 | + } |
|
447 | + |
|
448 | + /** |
|
449 | + * Get all sub folders of a folder |
|
450 | + * |
|
451 | + * @param array $entry the cache entry of the folder to get the subfolders for |
|
452 | + * @return array[] the cache entries for the subfolders |
|
453 | + */ |
|
454 | + private function getSubFolders($entry) { |
|
455 | + $children = $this->getFolderContentsById($entry['fileid']); |
|
456 | + return array_filter($children, function ($child) { |
|
457 | + return $child['mimetype'] === 'httpd/unix-directory'; |
|
458 | + }); |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Recursively remove all children of a folder |
|
463 | + * |
|
464 | + * @param array $entry the cache entry of the folder to remove the children of |
|
465 | + * @throws \OC\DatabaseException |
|
466 | + */ |
|
467 | + private function removeChildren($entry) { |
|
468 | + $subFolders = $this->getSubFolders($entry); |
|
469 | + foreach ($subFolders as $folder) { |
|
470 | + $this->removeChildren($folder); |
|
471 | + } |
|
472 | + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?'; |
|
473 | + $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * Move a file or folder in the cache |
|
478 | + * |
|
479 | + * @param string $source |
|
480 | + * @param string $target |
|
481 | + */ |
|
482 | + public function move($source, $target) { |
|
483 | + $this->moveFromCache($this, $source, $target); |
|
484 | + } |
|
485 | + |
|
486 | + /** |
|
487 | + * Get the storage id and path needed for a move |
|
488 | + * |
|
489 | + * @param string $path |
|
490 | + * @return array [$storageId, $internalPath] |
|
491 | + */ |
|
492 | + protected function getMoveInfo($path) { |
|
493 | + return [$this->getNumericStorageId(), $path]; |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * Move a file or folder in the cache |
|
498 | + * |
|
499 | + * @param \OCP\Files\Cache\ICache $sourceCache |
|
500 | + * @param string $sourcePath |
|
501 | + * @param string $targetPath |
|
502 | + * @throws \OC\DatabaseException |
|
503 | + * @throws \Exception if the given storages have an invalid id |
|
504 | + * @suppress SqlInjectionChecker |
|
505 | + */ |
|
506 | + public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) { |
|
507 | + if ($sourceCache instanceof Cache) { |
|
508 | + // normalize source and target |
|
509 | + $sourcePath = $this->normalize($sourcePath); |
|
510 | + $targetPath = $this->normalize($targetPath); |
|
511 | + |
|
512 | + $sourceData = $sourceCache->get($sourcePath); |
|
513 | + $sourceId = $sourceData['fileid']; |
|
514 | + $newParentId = $this->getParentId($targetPath); |
|
515 | + |
|
516 | + list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath); |
|
517 | + list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); |
|
518 | + |
|
519 | + if (is_null($sourceStorageId) || $sourceStorageId === false) { |
|
520 | + throw new \Exception('Invalid source storage id: ' . $sourceStorageId); |
|
521 | + } |
|
522 | + if (is_null($targetStorageId) || $targetStorageId === false) { |
|
523 | + throw new \Exception('Invalid target storage id: ' . $targetStorageId); |
|
524 | + } |
|
525 | + |
|
526 | + $this->connection->beginTransaction(); |
|
527 | + if ($sourceData['mimetype'] === 'httpd/unix-directory') { |
|
528 | + //update all child entries |
|
529 | + $sourceLength = mb_strlen($sourcePath); |
|
530 | + $query = $this->connection->getQueryBuilder(); |
|
531 | + |
|
532 | + $fun = $query->func(); |
|
533 | + $newPathFunction = $fun->concat( |
|
534 | + $query->createNamedParameter($targetPath), |
|
535 | + $fun->substring('path', $query->createNamedParameter($sourceLength + 1, IQueryBuilder::PARAM_INT))// +1 for the leading slash |
|
536 | + ); |
|
537 | + $query->update('filecache') |
|
538 | + ->set('storage', $query->createNamedParameter($targetStorageId, IQueryBuilder::PARAM_INT)) |
|
539 | + ->set('path_hash', $fun->md5($newPathFunction)) |
|
540 | + ->set('path', $newPathFunction) |
|
541 | + ->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT))) |
|
542 | + ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%'))); |
|
543 | + |
|
544 | + try { |
|
545 | + $query->execute(); |
|
546 | + } catch (\OC\DatabaseException $e) { |
|
547 | + $this->connection->rollBack(); |
|
548 | + throw $e; |
|
549 | + } |
|
550 | + } |
|
551 | + |
|
552 | + $sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` = ? WHERE `fileid` = ?'; |
|
553 | + $this->connection->executeQuery($sql, array($targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId)); |
|
554 | + $this->connection->commit(); |
|
555 | + } else { |
|
556 | + $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath); |
|
557 | + } |
|
558 | + } |
|
559 | + |
|
560 | + /** |
|
561 | + * remove all entries for files that are stored on the storage from the cache |
|
562 | + */ |
|
563 | + public function clear() { |
|
564 | + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
565 | + $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
566 | + |
|
567 | + $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?'; |
|
568 | + $this->connection->executeQuery($sql, array($this->storageId)); |
|
569 | + } |
|
570 | + |
|
571 | + /** |
|
572 | + * Get the scan status of a file |
|
573 | + * |
|
574 | + * - Cache::NOT_FOUND: File is not in the cache |
|
575 | + * - Cache::PARTIAL: File is not stored in the cache but some incomplete data is known |
|
576 | + * - Cache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned |
|
577 | + * - Cache::COMPLETE: The file or folder, with all it's children) are fully scanned |
|
578 | + * |
|
579 | + * @param string $file |
|
580 | + * |
|
581 | + * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
|
582 | + */ |
|
583 | + public function getStatus($file) { |
|
584 | + // normalize file |
|
585 | + $file = $this->normalize($file); |
|
586 | + |
|
587 | + $pathHash = md5($file); |
|
588 | + $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
589 | + $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
590 | + if ($row = $result->fetch()) { |
|
591 | + if ((int)$row['size'] === -1) { |
|
592 | + return self::SHALLOW; |
|
593 | + } else { |
|
594 | + return self::COMPLETE; |
|
595 | + } |
|
596 | + } else { |
|
597 | + if (isset($this->partial[$file])) { |
|
598 | + return self::PARTIAL; |
|
599 | + } else { |
|
600 | + return self::NOT_FOUND; |
|
601 | + } |
|
602 | + } |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * search for files matching $pattern |
|
607 | + * |
|
608 | + * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%') |
|
609 | + * @return ICacheEntry[] an array of cache entries where the name matches the search pattern |
|
610 | + */ |
|
611 | + public function search($pattern) { |
|
612 | + // normalize pattern |
|
613 | + $pattern = $this->normalize($pattern); |
|
614 | + |
|
615 | + if ($pattern === '%%') { |
|
616 | + return []; |
|
617 | + } |
|
618 | + |
|
619 | + |
|
620 | + $sql = ' |
|
621 | 621 | SELECT `fileid`, `storage`, `path`, `parent`, `name`, |
622 | 622 | `mimetype`, `storage_mtime`, `mimepart`, `size`, `mtime`, |
623 | 623 | `encrypted`, `etag`, `permissions`, `checksum` |
624 | 624 | FROM `*PREFIX*filecache` |
625 | 625 | WHERE `storage` = ? AND `name` ILIKE ?'; |
626 | - $result = $this->connection->executeQuery($sql, |
|
627 | - [$this->getNumericStorageId(), $pattern] |
|
628 | - ); |
|
629 | - |
|
630 | - return $this->searchResultToCacheEntries($result); |
|
631 | - } |
|
632 | - |
|
633 | - /** |
|
634 | - * @param Statement $result |
|
635 | - * @return CacheEntry[] |
|
636 | - */ |
|
637 | - private function searchResultToCacheEntries(Statement $result) { |
|
638 | - $files = $result->fetchAll(); |
|
639 | - |
|
640 | - return array_map(function (array $data) { |
|
641 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
642 | - }, $files); |
|
643 | - } |
|
644 | - |
|
645 | - /** |
|
646 | - * search for files by mimetype |
|
647 | - * |
|
648 | - * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image') |
|
649 | - * where it will search for all mimetypes in the group ('image/*') |
|
650 | - * @return ICacheEntry[] an array of cache entries where the mimetype matches the search |
|
651 | - */ |
|
652 | - public function searchByMime($mimetype) { |
|
653 | - if (strpos($mimetype, '/')) { |
|
654 | - $where = '`mimetype` = ?'; |
|
655 | - } else { |
|
656 | - $where = '`mimepart` = ?'; |
|
657 | - } |
|
658 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
|
626 | + $result = $this->connection->executeQuery($sql, |
|
627 | + [$this->getNumericStorageId(), $pattern] |
|
628 | + ); |
|
629 | + |
|
630 | + return $this->searchResultToCacheEntries($result); |
|
631 | + } |
|
632 | + |
|
633 | + /** |
|
634 | + * @param Statement $result |
|
635 | + * @return CacheEntry[] |
|
636 | + */ |
|
637 | + private function searchResultToCacheEntries(Statement $result) { |
|
638 | + $files = $result->fetchAll(); |
|
639 | + |
|
640 | + return array_map(function (array $data) { |
|
641 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
642 | + }, $files); |
|
643 | + } |
|
644 | + |
|
645 | + /** |
|
646 | + * search for files by mimetype |
|
647 | + * |
|
648 | + * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image') |
|
649 | + * where it will search for all mimetypes in the group ('image/*') |
|
650 | + * @return ICacheEntry[] an array of cache entries where the mimetype matches the search |
|
651 | + */ |
|
652 | + public function searchByMime($mimetype) { |
|
653 | + if (strpos($mimetype, '/')) { |
|
654 | + $where = '`mimetype` = ?'; |
|
655 | + } else { |
|
656 | + $where = '`mimepart` = ?'; |
|
657 | + } |
|
658 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
|
659 | 659 | FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?'; |
660 | - $mimetype = $this->mimetypeLoader->getId($mimetype); |
|
661 | - $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId())); |
|
662 | - |
|
663 | - return $this->searchResultToCacheEntries($result); |
|
664 | - } |
|
665 | - |
|
666 | - public function searchQuery(ISearchQuery $searchQuery) { |
|
667 | - $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
668 | - |
|
669 | - $query = $builder->select(['fileid', 'storage', 'path', 'parent', 'name', 'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum']) |
|
670 | - ->from('filecache', 'file'); |
|
671 | - |
|
672 | - $query->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->getNumericStorageId()))); |
|
673 | - |
|
674 | - if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) { |
|
675 | - $query |
|
676 | - ->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid')) |
|
677 | - ->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX( |
|
678 | - $builder->expr()->eq('tagmap.type', 'tag.type'), |
|
679 | - $builder->expr()->eq('tagmap.categoryid', 'tag.id') |
|
680 | - )) |
|
681 | - ->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files'))) |
|
682 | - ->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($searchQuery->getUser()->getUID()))); |
|
683 | - } |
|
684 | - |
|
685 | - $query->andWhere($this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation())); |
|
686 | - |
|
687 | - $this->querySearchHelper->addSearchOrdersToQuery($query, $searchQuery->getOrder()); |
|
688 | - |
|
689 | - if ($searchQuery->getLimit()) { |
|
690 | - $query->setMaxResults($searchQuery->getLimit()); |
|
691 | - } |
|
692 | - if ($searchQuery->getOffset()) { |
|
693 | - $query->setFirstResult($searchQuery->getOffset()); |
|
694 | - } |
|
695 | - |
|
696 | - $result = $query->execute(); |
|
697 | - return $this->searchResultToCacheEntries($result); |
|
698 | - } |
|
699 | - |
|
700 | - /** |
|
701 | - * Search for files by tag of a given users. |
|
702 | - * |
|
703 | - * Note that every user can tag files differently. |
|
704 | - * |
|
705 | - * @param string|int $tag name or tag id |
|
706 | - * @param string $userId owner of the tags |
|
707 | - * @return ICacheEntry[] file data |
|
708 | - */ |
|
709 | - public function searchByTag($tag, $userId) { |
|
710 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . |
|
711 | - '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' . |
|
712 | - '`encrypted`, `etag`, `permissions`, `checksum` ' . |
|
713 | - 'FROM `*PREFIX*filecache` `file`, ' . |
|
714 | - '`*PREFIX*vcategory_to_object` `tagmap`, ' . |
|
715 | - '`*PREFIX*vcategory` `tag` ' . |
|
716 | - // JOIN filecache to vcategory_to_object |
|
717 | - 'WHERE `file`.`fileid` = `tagmap`.`objid` ' . |
|
718 | - // JOIN vcategory_to_object to vcategory |
|
719 | - 'AND `tagmap`.`type` = `tag`.`type` ' . |
|
720 | - 'AND `tagmap`.`categoryid` = `tag`.`id` ' . |
|
721 | - // conditions |
|
722 | - 'AND `file`.`storage` = ? ' . |
|
723 | - 'AND `tag`.`type` = \'files\' ' . |
|
724 | - 'AND `tag`.`uid` = ? '; |
|
725 | - if (is_int($tag)) { |
|
726 | - $sql .= 'AND `tag`.`id` = ? '; |
|
727 | - } else { |
|
728 | - $sql .= 'AND `tag`.`category` = ? '; |
|
729 | - } |
|
730 | - $result = $this->connection->executeQuery( |
|
731 | - $sql, |
|
732 | - [ |
|
733 | - $this->getNumericStorageId(), |
|
734 | - $userId, |
|
735 | - $tag |
|
736 | - ] |
|
737 | - ); |
|
738 | - |
|
739 | - $files = $result->fetchAll(); |
|
740 | - |
|
741 | - return array_map(function (array $data) { |
|
742 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
743 | - }, $files); |
|
744 | - } |
|
745 | - |
|
746 | - /** |
|
747 | - * Re-calculate the folder size and the size of all parent folders |
|
748 | - * |
|
749 | - * @param string|boolean $path |
|
750 | - * @param array $data (optional) meta data of the folder |
|
751 | - */ |
|
752 | - public function correctFolderSize($path, $data = null) { |
|
753 | - $this->calculateFolderSize($path, $data); |
|
754 | - if ($path !== '') { |
|
755 | - $parent = dirname($path); |
|
756 | - if ($parent === '.' or $parent === '/') { |
|
757 | - $parent = ''; |
|
758 | - } |
|
759 | - $this->correctFolderSize($parent); |
|
760 | - } |
|
761 | - } |
|
762 | - |
|
763 | - /** |
|
764 | - * calculate the size of a folder and set it in the cache |
|
765 | - * |
|
766 | - * @param string $path |
|
767 | - * @param array $entry (optional) meta data of the folder |
|
768 | - * @return int |
|
769 | - */ |
|
770 | - public function calculateFolderSize($path, $entry = null) { |
|
771 | - $totalSize = 0; |
|
772 | - if (is_null($entry) or !isset($entry['fileid'])) { |
|
773 | - $entry = $this->get($path); |
|
774 | - } |
|
775 | - if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') { |
|
776 | - $id = $entry['fileid']; |
|
777 | - $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' . |
|
778 | - 'FROM `*PREFIX*filecache` ' . |
|
779 | - 'WHERE `parent` = ? AND `storage` = ?'; |
|
780 | - $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
781 | - if ($row = $result->fetch()) { |
|
782 | - $result->closeCursor(); |
|
783 | - list($sum, $min) = array_values($row); |
|
784 | - $sum = 0 + $sum; |
|
785 | - $min = 0 + $min; |
|
786 | - if ($min === -1) { |
|
787 | - $totalSize = $min; |
|
788 | - } else { |
|
789 | - $totalSize = $sum; |
|
790 | - } |
|
791 | - $update = array(); |
|
792 | - if ($entry['size'] !== $totalSize) { |
|
793 | - $update['size'] = $totalSize; |
|
794 | - } |
|
795 | - if (count($update) > 0) { |
|
796 | - $this->update($id, $update); |
|
797 | - } |
|
798 | - } else { |
|
799 | - $result->closeCursor(); |
|
800 | - } |
|
801 | - } |
|
802 | - return $totalSize; |
|
803 | - } |
|
804 | - |
|
805 | - /** |
|
806 | - * get all file ids on the files on the storage |
|
807 | - * |
|
808 | - * @return int[] |
|
809 | - */ |
|
810 | - public function getAll() { |
|
811 | - $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
812 | - $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
813 | - $ids = array(); |
|
814 | - while ($row = $result->fetch()) { |
|
815 | - $ids[] = $row['fileid']; |
|
816 | - } |
|
817 | - return $ids; |
|
818 | - } |
|
819 | - |
|
820 | - /** |
|
821 | - * find a folder in the cache which has not been fully scanned |
|
822 | - * |
|
823 | - * If multiple incomplete folders are in the cache, the one with the highest id will be returned, |
|
824 | - * use the one with the highest id gives the best result with the background scanner, since that is most |
|
825 | - * likely the folder where we stopped scanning previously |
|
826 | - * |
|
827 | - * @return string|bool the path of the folder or false when no folder matched |
|
828 | - */ |
|
829 | - public function getIncomplete() { |
|
830 | - $query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`' |
|
831 | - . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1); |
|
832 | - $query->execute([$this->getNumericStorageId()]); |
|
833 | - if ($row = $query->fetch()) { |
|
834 | - return $row['path']; |
|
835 | - } else { |
|
836 | - return false; |
|
837 | - } |
|
838 | - } |
|
839 | - |
|
840 | - /** |
|
841 | - * get the path of a file on this storage by it's file id |
|
842 | - * |
|
843 | - * @param int $id the file id of the file or folder to search |
|
844 | - * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache |
|
845 | - */ |
|
846 | - public function getPathById($id) { |
|
847 | - $sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?'; |
|
848 | - $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
849 | - if ($row = $result->fetch()) { |
|
850 | - // Oracle stores empty strings as null... |
|
851 | - if ($row['path'] === null) { |
|
852 | - return ''; |
|
853 | - } |
|
854 | - return $row['path']; |
|
855 | - } else { |
|
856 | - return null; |
|
857 | - } |
|
858 | - } |
|
859 | - |
|
860 | - /** |
|
861 | - * get the storage id of the storage for a file and the internal path of the file |
|
862 | - * unlike getPathById this does not limit the search to files on this storage and |
|
863 | - * instead does a global search in the cache table |
|
864 | - * |
|
865 | - * @param int $id |
|
866 | - * @deprecated use getPathById() instead |
|
867 | - * @return array first element holding the storage id, second the path |
|
868 | - */ |
|
869 | - static public function getById($id) { |
|
870 | - $connection = \OC::$server->getDatabaseConnection(); |
|
871 | - $sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
872 | - $result = $connection->executeQuery($sql, array($id)); |
|
873 | - if ($row = $result->fetch()) { |
|
874 | - $numericId = $row['storage']; |
|
875 | - $path = $row['path']; |
|
876 | - } else { |
|
877 | - return null; |
|
878 | - } |
|
879 | - |
|
880 | - if ($id = Storage::getStorageId($numericId)) { |
|
881 | - return array($id, $path); |
|
882 | - } else { |
|
883 | - return null; |
|
884 | - } |
|
885 | - } |
|
886 | - |
|
887 | - /** |
|
888 | - * normalize the given path |
|
889 | - * |
|
890 | - * @param string $path |
|
891 | - * @return string |
|
892 | - */ |
|
893 | - public function normalize($path) { |
|
894 | - |
|
895 | - return trim(\OC_Util::normalizeUnicode($path), '/'); |
|
896 | - } |
|
660 | + $mimetype = $this->mimetypeLoader->getId($mimetype); |
|
661 | + $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId())); |
|
662 | + |
|
663 | + return $this->searchResultToCacheEntries($result); |
|
664 | + } |
|
665 | + |
|
666 | + public function searchQuery(ISearchQuery $searchQuery) { |
|
667 | + $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
668 | + |
|
669 | + $query = $builder->select(['fileid', 'storage', 'path', 'parent', 'name', 'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum']) |
|
670 | + ->from('filecache', 'file'); |
|
671 | + |
|
672 | + $query->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->getNumericStorageId()))); |
|
673 | + |
|
674 | + if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) { |
|
675 | + $query |
|
676 | + ->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid')) |
|
677 | + ->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX( |
|
678 | + $builder->expr()->eq('tagmap.type', 'tag.type'), |
|
679 | + $builder->expr()->eq('tagmap.categoryid', 'tag.id') |
|
680 | + )) |
|
681 | + ->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files'))) |
|
682 | + ->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($searchQuery->getUser()->getUID()))); |
|
683 | + } |
|
684 | + |
|
685 | + $query->andWhere($this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation())); |
|
686 | + |
|
687 | + $this->querySearchHelper->addSearchOrdersToQuery($query, $searchQuery->getOrder()); |
|
688 | + |
|
689 | + if ($searchQuery->getLimit()) { |
|
690 | + $query->setMaxResults($searchQuery->getLimit()); |
|
691 | + } |
|
692 | + if ($searchQuery->getOffset()) { |
|
693 | + $query->setFirstResult($searchQuery->getOffset()); |
|
694 | + } |
|
695 | + |
|
696 | + $result = $query->execute(); |
|
697 | + return $this->searchResultToCacheEntries($result); |
|
698 | + } |
|
699 | + |
|
700 | + /** |
|
701 | + * Search for files by tag of a given users. |
|
702 | + * |
|
703 | + * Note that every user can tag files differently. |
|
704 | + * |
|
705 | + * @param string|int $tag name or tag id |
|
706 | + * @param string $userId owner of the tags |
|
707 | + * @return ICacheEntry[] file data |
|
708 | + */ |
|
709 | + public function searchByTag($tag, $userId) { |
|
710 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . |
|
711 | + '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' . |
|
712 | + '`encrypted`, `etag`, `permissions`, `checksum` ' . |
|
713 | + 'FROM `*PREFIX*filecache` `file`, ' . |
|
714 | + '`*PREFIX*vcategory_to_object` `tagmap`, ' . |
|
715 | + '`*PREFIX*vcategory` `tag` ' . |
|
716 | + // JOIN filecache to vcategory_to_object |
|
717 | + 'WHERE `file`.`fileid` = `tagmap`.`objid` ' . |
|
718 | + // JOIN vcategory_to_object to vcategory |
|
719 | + 'AND `tagmap`.`type` = `tag`.`type` ' . |
|
720 | + 'AND `tagmap`.`categoryid` = `tag`.`id` ' . |
|
721 | + // conditions |
|
722 | + 'AND `file`.`storage` = ? ' . |
|
723 | + 'AND `tag`.`type` = \'files\' ' . |
|
724 | + 'AND `tag`.`uid` = ? '; |
|
725 | + if (is_int($tag)) { |
|
726 | + $sql .= 'AND `tag`.`id` = ? '; |
|
727 | + } else { |
|
728 | + $sql .= 'AND `tag`.`category` = ? '; |
|
729 | + } |
|
730 | + $result = $this->connection->executeQuery( |
|
731 | + $sql, |
|
732 | + [ |
|
733 | + $this->getNumericStorageId(), |
|
734 | + $userId, |
|
735 | + $tag |
|
736 | + ] |
|
737 | + ); |
|
738 | + |
|
739 | + $files = $result->fetchAll(); |
|
740 | + |
|
741 | + return array_map(function (array $data) { |
|
742 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
743 | + }, $files); |
|
744 | + } |
|
745 | + |
|
746 | + /** |
|
747 | + * Re-calculate the folder size and the size of all parent folders |
|
748 | + * |
|
749 | + * @param string|boolean $path |
|
750 | + * @param array $data (optional) meta data of the folder |
|
751 | + */ |
|
752 | + public function correctFolderSize($path, $data = null) { |
|
753 | + $this->calculateFolderSize($path, $data); |
|
754 | + if ($path !== '') { |
|
755 | + $parent = dirname($path); |
|
756 | + if ($parent === '.' or $parent === '/') { |
|
757 | + $parent = ''; |
|
758 | + } |
|
759 | + $this->correctFolderSize($parent); |
|
760 | + } |
|
761 | + } |
|
762 | + |
|
763 | + /** |
|
764 | + * calculate the size of a folder and set it in the cache |
|
765 | + * |
|
766 | + * @param string $path |
|
767 | + * @param array $entry (optional) meta data of the folder |
|
768 | + * @return int |
|
769 | + */ |
|
770 | + public function calculateFolderSize($path, $entry = null) { |
|
771 | + $totalSize = 0; |
|
772 | + if (is_null($entry) or !isset($entry['fileid'])) { |
|
773 | + $entry = $this->get($path); |
|
774 | + } |
|
775 | + if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') { |
|
776 | + $id = $entry['fileid']; |
|
777 | + $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' . |
|
778 | + 'FROM `*PREFIX*filecache` ' . |
|
779 | + 'WHERE `parent` = ? AND `storage` = ?'; |
|
780 | + $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
781 | + if ($row = $result->fetch()) { |
|
782 | + $result->closeCursor(); |
|
783 | + list($sum, $min) = array_values($row); |
|
784 | + $sum = 0 + $sum; |
|
785 | + $min = 0 + $min; |
|
786 | + if ($min === -1) { |
|
787 | + $totalSize = $min; |
|
788 | + } else { |
|
789 | + $totalSize = $sum; |
|
790 | + } |
|
791 | + $update = array(); |
|
792 | + if ($entry['size'] !== $totalSize) { |
|
793 | + $update['size'] = $totalSize; |
|
794 | + } |
|
795 | + if (count($update) > 0) { |
|
796 | + $this->update($id, $update); |
|
797 | + } |
|
798 | + } else { |
|
799 | + $result->closeCursor(); |
|
800 | + } |
|
801 | + } |
|
802 | + return $totalSize; |
|
803 | + } |
|
804 | + |
|
805 | + /** |
|
806 | + * get all file ids on the files on the storage |
|
807 | + * |
|
808 | + * @return int[] |
|
809 | + */ |
|
810 | + public function getAll() { |
|
811 | + $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
812 | + $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
813 | + $ids = array(); |
|
814 | + while ($row = $result->fetch()) { |
|
815 | + $ids[] = $row['fileid']; |
|
816 | + } |
|
817 | + return $ids; |
|
818 | + } |
|
819 | + |
|
820 | + /** |
|
821 | + * find a folder in the cache which has not been fully scanned |
|
822 | + * |
|
823 | + * If multiple incomplete folders are in the cache, the one with the highest id will be returned, |
|
824 | + * use the one with the highest id gives the best result with the background scanner, since that is most |
|
825 | + * likely the folder where we stopped scanning previously |
|
826 | + * |
|
827 | + * @return string|bool the path of the folder or false when no folder matched |
|
828 | + */ |
|
829 | + public function getIncomplete() { |
|
830 | + $query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`' |
|
831 | + . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1); |
|
832 | + $query->execute([$this->getNumericStorageId()]); |
|
833 | + if ($row = $query->fetch()) { |
|
834 | + return $row['path']; |
|
835 | + } else { |
|
836 | + return false; |
|
837 | + } |
|
838 | + } |
|
839 | + |
|
840 | + /** |
|
841 | + * get the path of a file on this storage by it's file id |
|
842 | + * |
|
843 | + * @param int $id the file id of the file or folder to search |
|
844 | + * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache |
|
845 | + */ |
|
846 | + public function getPathById($id) { |
|
847 | + $sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?'; |
|
848 | + $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
849 | + if ($row = $result->fetch()) { |
|
850 | + // Oracle stores empty strings as null... |
|
851 | + if ($row['path'] === null) { |
|
852 | + return ''; |
|
853 | + } |
|
854 | + return $row['path']; |
|
855 | + } else { |
|
856 | + return null; |
|
857 | + } |
|
858 | + } |
|
859 | + |
|
860 | + /** |
|
861 | + * get the storage id of the storage for a file and the internal path of the file |
|
862 | + * unlike getPathById this does not limit the search to files on this storage and |
|
863 | + * instead does a global search in the cache table |
|
864 | + * |
|
865 | + * @param int $id |
|
866 | + * @deprecated use getPathById() instead |
|
867 | + * @return array first element holding the storage id, second the path |
|
868 | + */ |
|
869 | + static public function getById($id) { |
|
870 | + $connection = \OC::$server->getDatabaseConnection(); |
|
871 | + $sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
872 | + $result = $connection->executeQuery($sql, array($id)); |
|
873 | + if ($row = $result->fetch()) { |
|
874 | + $numericId = $row['storage']; |
|
875 | + $path = $row['path']; |
|
876 | + } else { |
|
877 | + return null; |
|
878 | + } |
|
879 | + |
|
880 | + if ($id = Storage::getStorageId($numericId)) { |
|
881 | + return array($id, $path); |
|
882 | + } else { |
|
883 | + return null; |
|
884 | + } |
|
885 | + } |
|
886 | + |
|
887 | + /** |
|
888 | + * normalize the given path |
|
889 | + * |
|
890 | + * @param string $path |
|
891 | + * @return string |
|
892 | + */ |
|
893 | + public function normalize($path) { |
|
894 | + |
|
895 | + return trim(\OC_Util::normalizeUnicode($path), '/'); |
|
896 | + } |
|
897 | 897 | } |
@@ -163,21 +163,21 @@ discard block |
||
163 | 163 | */ |
164 | 164 | public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) { |
165 | 165 | //fix types |
166 | - $data['fileid'] = (int)$data['fileid']; |
|
167 | - $data['parent'] = (int)$data['parent']; |
|
166 | + $data['fileid'] = (int) $data['fileid']; |
|
167 | + $data['parent'] = (int) $data['parent']; |
|
168 | 168 | $data['size'] = 0 + $data['size']; |
169 | - $data['mtime'] = (int)$data['mtime']; |
|
170 | - $data['storage_mtime'] = (int)$data['storage_mtime']; |
|
171 | - $data['encryptedVersion'] = (int)$data['encrypted']; |
|
172 | - $data['encrypted'] = (bool)$data['encrypted']; |
|
169 | + $data['mtime'] = (int) $data['mtime']; |
|
170 | + $data['storage_mtime'] = (int) $data['storage_mtime']; |
|
171 | + $data['encryptedVersion'] = (int) $data['encrypted']; |
|
172 | + $data['encrypted'] = (bool) $data['encrypted']; |
|
173 | 173 | $data['storage_id'] = $data['storage']; |
174 | - $data['storage'] = (int)$data['storage']; |
|
174 | + $data['storage'] = (int) $data['storage']; |
|
175 | 175 | $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']); |
176 | 176 | $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']); |
177 | 177 | if ($data['storage_mtime'] == 0) { |
178 | 178 | $data['storage_mtime'] = $data['mtime']; |
179 | 179 | } |
180 | - $data['permissions'] = (int)$data['permissions']; |
|
180 | + $data['permissions'] = (int) $data['permissions']; |
|
181 | 181 | return new CacheEntry($data); |
182 | 182 | } |
183 | 183 | |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC'; |
206 | 206 | $result = $this->connection->executeQuery($sql, [$fileId]); |
207 | 207 | $files = $result->fetchAll(); |
208 | - return array_map(function (array $data) { |
|
208 | + return array_map(function(array $data) { |
|
209 | 209 | return self::cacheEntryFromData($data, $this->mimetypeLoader); |
210 | 210 | }, $files); |
211 | 211 | } |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | $queryParts[] = '`storage`'; |
265 | 265 | $params[] = $this->getNumericStorageId(); |
266 | 266 | |
267 | - $queryParts = array_map(function ($item) { |
|
267 | + $queryParts = array_map(function($item) { |
|
268 | 268 | return trim($item, "`"); |
269 | 269 | }, $queryParts); |
270 | 270 | $values = array_combine($queryParts, $params); |
@@ -273,7 +273,7 @@ discard block |
||
273 | 273 | 'path_hash', |
274 | 274 | ]) |
275 | 275 | ) { |
276 | - return (int)$this->connection->lastInsertId('*PREFIX*filecache'); |
|
276 | + return (int) $this->connection->lastInsertId('*PREFIX*filecache'); |
|
277 | 277 | } |
278 | 278 | |
279 | 279 | // The file was created in the mean time |
@@ -311,10 +311,10 @@ discard block |
||
311 | 311 | |
312 | 312 | // don't update if the data we try to set is the same as the one in the record |
313 | 313 | // some databases (Postgres) don't like superfluous updates |
314 | - $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' . |
|
315 | - 'WHERE (' . |
|
316 | - implode(' <> ? OR ', $queryParts) . ' <> ? OR ' . |
|
317 | - implode(' IS NULL OR ', $queryParts) . ' IS NULL' . |
|
314 | + $sql = 'UPDATE `*PREFIX*filecache` SET '.implode(' = ?, ', $queryParts).'=? '. |
|
315 | + 'WHERE ('. |
|
316 | + implode(' <> ? OR ', $queryParts).' <> ? OR '. |
|
317 | + implode(' IS NULL OR ', $queryParts).' IS NULL'. |
|
318 | 318 | ') AND `fileid` = ? '; |
319 | 319 | $this->connection->executeQuery($sql, $params); |
320 | 320 | |
@@ -365,7 +365,7 @@ discard block |
||
365 | 365 | } |
366 | 366 | } |
367 | 367 | $params[] = $value; |
368 | - $queryParts[] = '`' . $name . '`'; |
|
368 | + $queryParts[] = '`'.$name.'`'; |
|
369 | 369 | } |
370 | 370 | } |
371 | 371 | return array($queryParts, $params); |
@@ -407,7 +407,7 @@ discard block |
||
407 | 407 | return -1; |
408 | 408 | } else { |
409 | 409 | $parent = $this->getParentPath($file); |
410 | - return (int)$this->getId($parent); |
|
410 | + return (int) $this->getId($parent); |
|
411 | 411 | } |
412 | 412 | } |
413 | 413 | |
@@ -453,7 +453,7 @@ discard block |
||
453 | 453 | */ |
454 | 454 | private function getSubFolders($entry) { |
455 | 455 | $children = $this->getFolderContentsById($entry['fileid']); |
456 | - return array_filter($children, function ($child) { |
|
456 | + return array_filter($children, function($child) { |
|
457 | 457 | return $child['mimetype'] === 'httpd/unix-directory'; |
458 | 458 | }); |
459 | 459 | } |
@@ -517,10 +517,10 @@ discard block |
||
517 | 517 | list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); |
518 | 518 | |
519 | 519 | if (is_null($sourceStorageId) || $sourceStorageId === false) { |
520 | - throw new \Exception('Invalid source storage id: ' . $sourceStorageId); |
|
520 | + throw new \Exception('Invalid source storage id: '.$sourceStorageId); |
|
521 | 521 | } |
522 | 522 | if (is_null($targetStorageId) || $targetStorageId === false) { |
523 | - throw new \Exception('Invalid target storage id: ' . $targetStorageId); |
|
523 | + throw new \Exception('Invalid target storage id: '.$targetStorageId); |
|
524 | 524 | } |
525 | 525 | |
526 | 526 | $this->connection->beginTransaction(); |
@@ -539,7 +539,7 @@ discard block |
||
539 | 539 | ->set('path_hash', $fun->md5($newPathFunction)) |
540 | 540 | ->set('path', $newPathFunction) |
541 | 541 | ->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT))) |
542 | - ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%'))); |
|
542 | + ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath).'/%'))); |
|
543 | 543 | |
544 | 544 | try { |
545 | 545 | $query->execute(); |
@@ -588,7 +588,7 @@ discard block |
||
588 | 588 | $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
589 | 589 | $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
590 | 590 | if ($row = $result->fetch()) { |
591 | - if ((int)$row['size'] === -1) { |
|
591 | + if ((int) $row['size'] === -1) { |
|
592 | 592 | return self::SHALLOW; |
593 | 593 | } else { |
594 | 594 | return self::COMPLETE; |
@@ -637,7 +637,7 @@ discard block |
||
637 | 637 | private function searchResultToCacheEntries(Statement $result) { |
638 | 638 | $files = $result->fetchAll(); |
639 | 639 | |
640 | - return array_map(function (array $data) { |
|
640 | + return array_map(function(array $data) { |
|
641 | 641 | return self::cacheEntryFromData($data, $this->mimetypeLoader); |
642 | 642 | }, $files); |
643 | 643 | } |
@@ -656,7 +656,7 @@ discard block |
||
656 | 656 | $where = '`mimepart` = ?'; |
657 | 657 | } |
658 | 658 | $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
659 | - FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?'; |
|
659 | + FROM `*PREFIX*filecache` WHERE ' . $where.' AND `storage` = ?'; |
|
660 | 660 | $mimetype = $this->mimetypeLoader->getId($mimetype); |
661 | 661 | $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId())); |
662 | 662 | |
@@ -707,20 +707,20 @@ discard block |
||
707 | 707 | * @return ICacheEntry[] file data |
708 | 708 | */ |
709 | 709 | public function searchByTag($tag, $userId) { |
710 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . |
|
711 | - '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' . |
|
712 | - '`encrypted`, `etag`, `permissions`, `checksum` ' . |
|
713 | - 'FROM `*PREFIX*filecache` `file`, ' . |
|
714 | - '`*PREFIX*vcategory_to_object` `tagmap`, ' . |
|
715 | - '`*PREFIX*vcategory` `tag` ' . |
|
710 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, '. |
|
711 | + '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, '. |
|
712 | + '`encrypted`, `etag`, `permissions`, `checksum` '. |
|
713 | + 'FROM `*PREFIX*filecache` `file`, '. |
|
714 | + '`*PREFIX*vcategory_to_object` `tagmap`, '. |
|
715 | + '`*PREFIX*vcategory` `tag` '. |
|
716 | 716 | // JOIN filecache to vcategory_to_object |
717 | - 'WHERE `file`.`fileid` = `tagmap`.`objid` ' . |
|
717 | + 'WHERE `file`.`fileid` = `tagmap`.`objid` '. |
|
718 | 718 | // JOIN vcategory_to_object to vcategory |
719 | - 'AND `tagmap`.`type` = `tag`.`type` ' . |
|
720 | - 'AND `tagmap`.`categoryid` = `tag`.`id` ' . |
|
719 | + 'AND `tagmap`.`type` = `tag`.`type` '. |
|
720 | + 'AND `tagmap`.`categoryid` = `tag`.`id` '. |
|
721 | 721 | // conditions |
722 | - 'AND `file`.`storage` = ? ' . |
|
723 | - 'AND `tag`.`type` = \'files\' ' . |
|
722 | + 'AND `file`.`storage` = ? '. |
|
723 | + 'AND `tag`.`type` = \'files\' '. |
|
724 | 724 | 'AND `tag`.`uid` = ? '; |
725 | 725 | if (is_int($tag)) { |
726 | 726 | $sql .= 'AND `tag`.`id` = ? '; |
@@ -738,7 +738,7 @@ discard block |
||
738 | 738 | |
739 | 739 | $files = $result->fetchAll(); |
740 | 740 | |
741 | - return array_map(function (array $data) { |
|
741 | + return array_map(function(array $data) { |
|
742 | 742 | return self::cacheEntryFromData($data, $this->mimetypeLoader); |
743 | 743 | }, $files); |
744 | 744 | } |
@@ -774,8 +774,8 @@ discard block |
||
774 | 774 | } |
775 | 775 | if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') { |
776 | 776 | $id = $entry['fileid']; |
777 | - $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' . |
|
778 | - 'FROM `*PREFIX*filecache` ' . |
|
777 | + $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 '. |
|
778 | + 'FROM `*PREFIX*filecache` '. |
|
779 | 779 | 'WHERE `parent` = ? AND `storage` = ?'; |
780 | 780 | $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
781 | 781 | if ($row = $result->fetch()) { |
@@ -119,9 +119,9 @@ |
||
119 | 119 | $elements = explode(' ', $fullName); |
120 | 120 | $result = ['', '', '', '', '']; |
121 | 121 | if (count($elements) > 2) { |
122 | - $result[0] = implode(' ', array_slice($elements, count($elements)-1)); |
|
122 | + $result[0] = implode(' ', array_slice($elements, count($elements) - 1)); |
|
123 | 123 | $result[1] = $elements[0]; |
124 | - $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2)); |
|
124 | + $result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2)); |
|
125 | 125 | } elseif (count($elements) === 2) { |
126 | 126 | $result[0] = $elements[1]; |
127 | 127 | $result[1] = $elements[0]; |
@@ -31,122 +31,122 @@ |
||
31 | 31 | |
32 | 32 | class Converter { |
33 | 33 | |
34 | - /** @var AccountManager */ |
|
35 | - private $accountManager; |
|
36 | - |
|
37 | - /** |
|
38 | - * Converter constructor. |
|
39 | - * |
|
40 | - * @param AccountManager $accountManager |
|
41 | - */ |
|
42 | - public function __construct(AccountManager $accountManager) { |
|
43 | - $this->accountManager = $accountManager; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @param IUser $user |
|
48 | - * @return VCard|null |
|
49 | - */ |
|
50 | - public function createCardFromUser(IUser $user) { |
|
51 | - |
|
52 | - $userData = $this->accountManager->getUser($user); |
|
53 | - |
|
54 | - $uid = $user->getUID(); |
|
55 | - $cloudId = $user->getCloudId(); |
|
56 | - $image = $this->getAvatarImage($user); |
|
57 | - |
|
58 | - $vCard = new VCard(); |
|
59 | - $vCard->VERSION = '3.0'; |
|
60 | - $vCard->UID = $uid; |
|
61 | - |
|
62 | - $publish = false; |
|
63 | - |
|
64 | - if ($image !== null && isset($userData[AccountManager::PROPERTY_AVATAR])) { |
|
65 | - $userData[AccountManager::PROPERTY_AVATAR]['value'] = true; |
|
66 | - } |
|
67 | - |
|
68 | - foreach ($userData as $property => $value) { |
|
69 | - |
|
70 | - $shareWithTrustedServers = |
|
71 | - $value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY || |
|
72 | - $value['scope'] === AccountManager::VISIBILITY_PUBLIC; |
|
73 | - |
|
74 | - $emptyValue = !isset($value['value']) || $value['value'] === ''; |
|
75 | - |
|
76 | - if ($shareWithTrustedServers && !$emptyValue) { |
|
77 | - $publish = true; |
|
78 | - switch ($property) { |
|
79 | - case AccountManager::PROPERTY_DISPLAYNAME: |
|
80 | - $vCard->add(new Text($vCard, 'FN', $value['value'])); |
|
81 | - $vCard->add(new Text($vCard, 'N', $this->splitFullName($value['value']))); |
|
82 | - break; |
|
83 | - case AccountManager::PROPERTY_AVATAR: |
|
84 | - if ($image !== null) { |
|
85 | - $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
86 | - } |
|
87 | - break; |
|
88 | - case AccountManager::PROPERTY_EMAIL: |
|
89 | - $vCard->add(new Text($vCard, 'EMAIL', $value['value'], ['TYPE' => 'OTHER'])); |
|
90 | - break; |
|
91 | - case AccountManager::PROPERTY_WEBSITE: |
|
92 | - $vCard->add(new Text($vCard, 'URL', $value['value'])); |
|
93 | - break; |
|
94 | - case AccountManager::PROPERTY_PHONE: |
|
95 | - $vCard->add(new Text($vCard, 'TEL', $value['value'], ['TYPE' => 'OTHER'])); |
|
96 | - break; |
|
97 | - case AccountManager::PROPERTY_ADDRESS: |
|
98 | - $vCard->add(new Text($vCard, 'ADR', $value['value'], ['TYPE' => 'OTHER'])); |
|
99 | - break; |
|
100 | - case AccountManager::PROPERTY_TWITTER: |
|
101 | - $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $value['value'], ['TYPE' => 'TWITTER'])); |
|
102 | - break; |
|
103 | - } |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - if ($publish && !empty($cloudId)) { |
|
108 | - $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
109 | - $vCard->validate(); |
|
110 | - return $vCard; |
|
111 | - } |
|
112 | - |
|
113 | - return null; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * @param string $fullName |
|
118 | - * @return string[] |
|
119 | - */ |
|
120 | - public function splitFullName($fullName) { |
|
121 | - // Very basic western style parsing. I'm not gonna implement |
|
122 | - // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
123 | - |
|
124 | - $elements = explode(' ', $fullName); |
|
125 | - $result = ['', '', '', '', '']; |
|
126 | - if (count($elements) > 2) { |
|
127 | - $result[0] = implode(' ', array_slice($elements, count($elements)-1)); |
|
128 | - $result[1] = $elements[0]; |
|
129 | - $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2)); |
|
130 | - } elseif (count($elements) === 2) { |
|
131 | - $result[0] = $elements[1]; |
|
132 | - $result[1] = $elements[0]; |
|
133 | - } else { |
|
134 | - $result[0] = $elements[0]; |
|
135 | - } |
|
136 | - |
|
137 | - return $result; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @param IUser $user |
|
142 | - * @return null|IImage |
|
143 | - */ |
|
144 | - private function getAvatarImage(IUser $user) { |
|
145 | - try { |
|
146 | - return $user->getAvatarImage(-1); |
|
147 | - } catch (\Exception $ex) { |
|
148 | - return null; |
|
149 | - } |
|
150 | - } |
|
34 | + /** @var AccountManager */ |
|
35 | + private $accountManager; |
|
36 | + |
|
37 | + /** |
|
38 | + * Converter constructor. |
|
39 | + * |
|
40 | + * @param AccountManager $accountManager |
|
41 | + */ |
|
42 | + public function __construct(AccountManager $accountManager) { |
|
43 | + $this->accountManager = $accountManager; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @param IUser $user |
|
48 | + * @return VCard|null |
|
49 | + */ |
|
50 | + public function createCardFromUser(IUser $user) { |
|
51 | + |
|
52 | + $userData = $this->accountManager->getUser($user); |
|
53 | + |
|
54 | + $uid = $user->getUID(); |
|
55 | + $cloudId = $user->getCloudId(); |
|
56 | + $image = $this->getAvatarImage($user); |
|
57 | + |
|
58 | + $vCard = new VCard(); |
|
59 | + $vCard->VERSION = '3.0'; |
|
60 | + $vCard->UID = $uid; |
|
61 | + |
|
62 | + $publish = false; |
|
63 | + |
|
64 | + if ($image !== null && isset($userData[AccountManager::PROPERTY_AVATAR])) { |
|
65 | + $userData[AccountManager::PROPERTY_AVATAR]['value'] = true; |
|
66 | + } |
|
67 | + |
|
68 | + foreach ($userData as $property => $value) { |
|
69 | + |
|
70 | + $shareWithTrustedServers = |
|
71 | + $value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY || |
|
72 | + $value['scope'] === AccountManager::VISIBILITY_PUBLIC; |
|
73 | + |
|
74 | + $emptyValue = !isset($value['value']) || $value['value'] === ''; |
|
75 | + |
|
76 | + if ($shareWithTrustedServers && !$emptyValue) { |
|
77 | + $publish = true; |
|
78 | + switch ($property) { |
|
79 | + case AccountManager::PROPERTY_DISPLAYNAME: |
|
80 | + $vCard->add(new Text($vCard, 'FN', $value['value'])); |
|
81 | + $vCard->add(new Text($vCard, 'N', $this->splitFullName($value['value']))); |
|
82 | + break; |
|
83 | + case AccountManager::PROPERTY_AVATAR: |
|
84 | + if ($image !== null) { |
|
85 | + $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
86 | + } |
|
87 | + break; |
|
88 | + case AccountManager::PROPERTY_EMAIL: |
|
89 | + $vCard->add(new Text($vCard, 'EMAIL', $value['value'], ['TYPE' => 'OTHER'])); |
|
90 | + break; |
|
91 | + case AccountManager::PROPERTY_WEBSITE: |
|
92 | + $vCard->add(new Text($vCard, 'URL', $value['value'])); |
|
93 | + break; |
|
94 | + case AccountManager::PROPERTY_PHONE: |
|
95 | + $vCard->add(new Text($vCard, 'TEL', $value['value'], ['TYPE' => 'OTHER'])); |
|
96 | + break; |
|
97 | + case AccountManager::PROPERTY_ADDRESS: |
|
98 | + $vCard->add(new Text($vCard, 'ADR', $value['value'], ['TYPE' => 'OTHER'])); |
|
99 | + break; |
|
100 | + case AccountManager::PROPERTY_TWITTER: |
|
101 | + $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $value['value'], ['TYPE' => 'TWITTER'])); |
|
102 | + break; |
|
103 | + } |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + if ($publish && !empty($cloudId)) { |
|
108 | + $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
109 | + $vCard->validate(); |
|
110 | + return $vCard; |
|
111 | + } |
|
112 | + |
|
113 | + return null; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * @param string $fullName |
|
118 | + * @return string[] |
|
119 | + */ |
|
120 | + public function splitFullName($fullName) { |
|
121 | + // Very basic western style parsing. I'm not gonna implement |
|
122 | + // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
123 | + |
|
124 | + $elements = explode(' ', $fullName); |
|
125 | + $result = ['', '', '', '', '']; |
|
126 | + if (count($elements) > 2) { |
|
127 | + $result[0] = implode(' ', array_slice($elements, count($elements)-1)); |
|
128 | + $result[1] = $elements[0]; |
|
129 | + $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2)); |
|
130 | + } elseif (count($elements) === 2) { |
|
131 | + $result[0] = $elements[1]; |
|
132 | + $result[1] = $elements[0]; |
|
133 | + } else { |
|
134 | + $result[0] = $elements[0]; |
|
135 | + } |
|
136 | + |
|
137 | + return $result; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @param IUser $user |
|
142 | + * @return null|IImage |
|
143 | + */ |
|
144 | + private function getAvatarImage(IUser $user) { |
|
145 | + try { |
|
146 | + return $user->getAvatarImage(-1); |
|
147 | + } catch (\Exception $ex) { |
|
148 | + return null; |
|
149 | + } |
|
150 | + } |
|
151 | 151 | |
152 | 152 | } |
@@ -30,64 +30,64 @@ |
||
30 | 30 | |
31 | 31 | class SyncFederationAddressBooks { |
32 | 32 | |
33 | - /** @var DbHandler */ |
|
34 | - protected $dbHandler; |
|
33 | + /** @var DbHandler */ |
|
34 | + protected $dbHandler; |
|
35 | 35 | |
36 | - /** @var SyncService */ |
|
37 | - private $syncService; |
|
36 | + /** @var SyncService */ |
|
37 | + private $syncService; |
|
38 | 38 | |
39 | - /** @var DiscoveryService */ |
|
40 | - private $ocsDiscoveryService; |
|
39 | + /** @var DiscoveryService */ |
|
40 | + private $ocsDiscoveryService; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @param DbHandler $dbHandler |
|
44 | - * @param SyncService $syncService |
|
45 | - * @param IDiscoveryService $ocsDiscoveryService |
|
46 | - */ |
|
47 | - public function __construct(DbHandler $dbHandler, |
|
48 | - SyncService $syncService, |
|
49 | - IDiscoveryService $ocsDiscoveryService |
|
50 | - ) { |
|
51 | - $this->syncService = $syncService; |
|
52 | - $this->dbHandler = $dbHandler; |
|
53 | - $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
54 | - } |
|
42 | + /** |
|
43 | + * @param DbHandler $dbHandler |
|
44 | + * @param SyncService $syncService |
|
45 | + * @param IDiscoveryService $ocsDiscoveryService |
|
46 | + */ |
|
47 | + public function __construct(DbHandler $dbHandler, |
|
48 | + SyncService $syncService, |
|
49 | + IDiscoveryService $ocsDiscoveryService |
|
50 | + ) { |
|
51 | + $this->syncService = $syncService; |
|
52 | + $this->dbHandler = $dbHandler; |
|
53 | + $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param \Closure $callback |
|
58 | - */ |
|
59 | - public function syncThemAll(\Closure $callback) { |
|
56 | + /** |
|
57 | + * @param \Closure $callback |
|
58 | + */ |
|
59 | + public function syncThemAll(\Closure $callback) { |
|
60 | 60 | |
61 | - $trustedServers = $this->dbHandler->getAllServer(); |
|
62 | - foreach ($trustedServers as $trustedServer) { |
|
63 | - $url = $trustedServer['url']; |
|
64 | - $callback($url, null); |
|
65 | - $sharedSecret = $trustedServer['shared_secret']; |
|
66 | - $syncToken = $trustedServer['sync_token']; |
|
61 | + $trustedServers = $this->dbHandler->getAllServer(); |
|
62 | + foreach ($trustedServers as $trustedServer) { |
|
63 | + $url = $trustedServer['url']; |
|
64 | + $callback($url, null); |
|
65 | + $sharedSecret = $trustedServer['shared_secret']; |
|
66 | + $syncToken = $trustedServer['sync_token']; |
|
67 | 67 | |
68 | - $endPoints = $this->ocsDiscoveryService->discover($url, 'FEDERATED_SHARING'); |
|
69 | - $cardDavUser = isset($endPoints['carddav-user']) ? $endPoints['carddav-user'] : 'system'; |
|
70 | - $addressBookUrl = isset($endPoints['system-address-book']) ? trim($endPoints['system-address-book'], '/') : 'remote.php/dav/addressbooks/system/system/system'; |
|
68 | + $endPoints = $this->ocsDiscoveryService->discover($url, 'FEDERATED_SHARING'); |
|
69 | + $cardDavUser = isset($endPoints['carddav-user']) ? $endPoints['carddav-user'] : 'system'; |
|
70 | + $addressBookUrl = isset($endPoints['system-address-book']) ? trim($endPoints['system-address-book'], '/') : 'remote.php/dav/addressbooks/system/system/system'; |
|
71 | 71 | |
72 | - if (is_null($sharedSecret)) { |
|
73 | - continue; |
|
74 | - } |
|
75 | - $targetBookId = $trustedServer['url_hash']; |
|
76 | - $targetPrincipal = "principals/system/system"; |
|
77 | - $targetBookProperties = [ |
|
78 | - '{DAV:}displayname' => $url |
|
79 | - ]; |
|
80 | - try { |
|
81 | - $newToken = $this->syncService->syncRemoteAddressBook($url, $cardDavUser, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties); |
|
82 | - if ($newToken !== $syncToken) { |
|
83 | - $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken); |
|
84 | - } |
|
85 | - } catch (\Exception $ex) { |
|
86 | - if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
87 | - $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED); |
|
88 | - } |
|
89 | - $callback($url, $ex); |
|
90 | - } |
|
91 | - } |
|
92 | - } |
|
72 | + if (is_null($sharedSecret)) { |
|
73 | + continue; |
|
74 | + } |
|
75 | + $targetBookId = $trustedServer['url_hash']; |
|
76 | + $targetPrincipal = "principals/system/system"; |
|
77 | + $targetBookProperties = [ |
|
78 | + '{DAV:}displayname' => $url |
|
79 | + ]; |
|
80 | + try { |
|
81 | + $newToken = $this->syncService->syncRemoteAddressBook($url, $cardDavUser, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties); |
|
82 | + if ($newToken !== $syncToken) { |
|
83 | + $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken); |
|
84 | + } |
|
85 | + } catch (\Exception $ex) { |
|
86 | + if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
87 | + $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED); |
|
88 | + } |
|
89 | + $callback($url, $ex); |
|
90 | + } |
|
91 | + } |
|
92 | + } |
|
93 | 93 | } |
@@ -28,35 +28,35 @@ |
||
28 | 28 | use Symfony\Component\Console\Output\OutputInterface; |
29 | 29 | |
30 | 30 | class Check extends Base { |
31 | - /** |
|
32 | - * @var SystemConfig |
|
33 | - */ |
|
34 | - private $config; |
|
31 | + /** |
|
32 | + * @var SystemConfig |
|
33 | + */ |
|
34 | + private $config; |
|
35 | 35 | |
36 | - public function __construct(SystemConfig $config) { |
|
37 | - parent::__construct(); |
|
38 | - $this->config = $config; |
|
39 | - } |
|
36 | + public function __construct(SystemConfig $config) { |
|
37 | + parent::__construct(); |
|
38 | + $this->config = $config; |
|
39 | + } |
|
40 | 40 | |
41 | - protected function configure() { |
|
42 | - parent::configure(); |
|
41 | + protected function configure() { |
|
42 | + parent::configure(); |
|
43 | 43 | |
44 | - $this |
|
45 | - ->setName('check') |
|
46 | - ->setDescription('check dependencies of the server environment') |
|
47 | - ; |
|
48 | - } |
|
44 | + $this |
|
45 | + ->setName('check') |
|
46 | + ->setDescription('check dependencies of the server environment') |
|
47 | + ; |
|
48 | + } |
|
49 | 49 | |
50 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
51 | - $errors = \OC_Util::checkServer($this->config); |
|
52 | - if (!empty($errors)) { |
|
53 | - $errors = array_map(function($item) { |
|
54 | - return (string) $item['error']; |
|
55 | - }, $errors); |
|
50 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
51 | + $errors = \OC_Util::checkServer($this->config); |
|
52 | + if (!empty($errors)) { |
|
53 | + $errors = array_map(function($item) { |
|
54 | + return (string) $item['error']; |
|
55 | + }, $errors); |
|
56 | 56 | |
57 | - $this->writeArrayInOutputFormat($input, $output, $errors); |
|
58 | - return 1; |
|
59 | - } |
|
60 | - return 0; |
|
61 | - } |
|
57 | + $this->writeArrayInOutputFormat($input, $output, $errors); |
|
58 | + return 1; |
|
59 | + } |
|
60 | + return 0; |
|
61 | + } |
|
62 | 62 | } |
@@ -33,10 +33,10 @@ |
||
33 | 33 | } |
34 | 34 | |
35 | 35 | public function setupDatabase($username) { |
36 | - $datadir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data'); |
|
36 | + $datadir = $this->config->getValue('datadirectory', \OC::$SERVERROOT.'/data'); |
|
37 | 37 | |
38 | 38 | //delete the old sqlite database first, might cause infinte loops otherwise |
39 | - if(file_exists("$datadir/owncloud.db")) { |
|
39 | + if (file_exists("$datadir/owncloud.db")) { |
|
40 | 40 | unlink("$datadir/owncloud.db"); |
41 | 41 | } |
42 | 42 | //in case of sqlite, we can always fill the database |
@@ -23,23 +23,23 @@ |
||
23 | 23 | namespace OC\Setup; |
24 | 24 | |
25 | 25 | class Sqlite extends AbstractDatabase { |
26 | - public $dbprettyname = 'Sqlite'; |
|
26 | + public $dbprettyname = 'Sqlite'; |
|
27 | 27 | |
28 | - public function validate($config) { |
|
29 | - return array(); |
|
30 | - } |
|
28 | + public function validate($config) { |
|
29 | + return array(); |
|
30 | + } |
|
31 | 31 | |
32 | - public function initialize($config) { |
|
33 | - } |
|
32 | + public function initialize($config) { |
|
33 | + } |
|
34 | 34 | |
35 | - public function setupDatabase($username) { |
|
36 | - $datadir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data'); |
|
35 | + public function setupDatabase($username) { |
|
36 | + $datadir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data'); |
|
37 | 37 | |
38 | - //delete the old sqlite database first, might cause infinte loops otherwise |
|
39 | - if(file_exists("$datadir/owncloud.db")) { |
|
40 | - unlink("$datadir/owncloud.db"); |
|
41 | - } |
|
42 | - //in case of sqlite, we can always fill the database |
|
43 | - error_log("creating sqlite db"); |
|
44 | - } |
|
38 | + //delete the old sqlite database first, might cause infinte loops otherwise |
|
39 | + if(file_exists("$datadir/owncloud.db")) { |
|
40 | + unlink("$datadir/owncloud.db"); |
|
41 | + } |
|
42 | + //in case of sqlite, we can always fill the database |
|
43 | + error_log("creating sqlite db"); |
|
44 | + } |
|
45 | 45 | } |
@@ -39,33 +39,33 @@ |
||
39 | 39 | * @package OCA\DAV\Connector\Sabre |
40 | 40 | */ |
41 | 41 | class DummyGetResponsePlugin extends \Sabre\DAV\ServerPlugin { |
42 | - /** @var \Sabre\DAV\Server */ |
|
43 | - protected $server; |
|
42 | + /** @var \Sabre\DAV\Server */ |
|
43 | + protected $server; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param \Sabre\DAV\Server $server |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - function initialize(\Sabre\DAV\Server $server) { |
|
50 | - $this->server = $server; |
|
51 | - $this->server->on('method:GET', [$this, 'httpGet'], 200); |
|
52 | - } |
|
45 | + /** |
|
46 | + * @param \Sabre\DAV\Server $server |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + function initialize(\Sabre\DAV\Server $server) { |
|
50 | + $this->server = $server; |
|
51 | + $this->server->on('method:GET', [$this, 'httpGet'], 200); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * @param RequestInterface $request |
|
56 | - * @param ResponseInterface $response |
|
57 | - * @return false |
|
58 | - */ |
|
59 | - function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
60 | - $string = 'This is the WebDAV interface. It can only be accessed by ' . |
|
61 | - 'WebDAV clients such as the Nextcloud desktop sync client.'; |
|
62 | - $stream = fopen('php://memory','r+'); |
|
63 | - fwrite($stream, $string); |
|
64 | - rewind($stream); |
|
54 | + /** |
|
55 | + * @param RequestInterface $request |
|
56 | + * @param ResponseInterface $response |
|
57 | + * @return false |
|
58 | + */ |
|
59 | + function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
60 | + $string = 'This is the WebDAV interface. It can only be accessed by ' . |
|
61 | + 'WebDAV clients such as the Nextcloud desktop sync client.'; |
|
62 | + $stream = fopen('php://memory','r+'); |
|
63 | + fwrite($stream, $string); |
|
64 | + rewind($stream); |
|
65 | 65 | |
66 | - $response->setStatus(200); |
|
67 | - $response->setBody($stream); |
|
66 | + $response->setStatus(200); |
|
67 | + $response->setBody($stream); |
|
68 | 68 | |
69 | - return false; |
|
70 | - } |
|
69 | + return false; |
|
70 | + } |
|
71 | 71 | } |
@@ -57,9 +57,9 @@ |
||
57 | 57 | * @return false |
58 | 58 | */ |
59 | 59 | function httpGet(RequestInterface $request, ResponseInterface $response) { |
60 | - $string = 'This is the WebDAV interface. It can only be accessed by ' . |
|
60 | + $string = 'This is the WebDAV interface. It can only be accessed by '. |
|
61 | 61 | 'WebDAV clients such as the Nextcloud desktop sync client.'; |
62 | - $stream = fopen('php://memory','r+'); |
|
62 | + $stream = fopen('php://memory', 'r+'); |
|
63 | 63 | fwrite($stream, $string); |
64 | 64 | rewind($stream); |
65 | 65 |
@@ -37,57 +37,57 @@ |
||
37 | 37 | */ |
38 | 38 | class ConsoleOutput implements IOutput { |
39 | 39 | |
40 | - /** @var OutputInterface */ |
|
41 | - private $output; |
|
40 | + /** @var OutputInterface */ |
|
41 | + private $output; |
|
42 | 42 | |
43 | - /** @var ProgressBar */ |
|
44 | - private $progressBar; |
|
43 | + /** @var ProgressBar */ |
|
44 | + private $progressBar; |
|
45 | 45 | |
46 | - public function __construct(OutputInterface $output) { |
|
47 | - $this->output = $output; |
|
48 | - } |
|
46 | + public function __construct(OutputInterface $output) { |
|
47 | + $this->output = $output; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @param string $message |
|
52 | - */ |
|
53 | - public function info($message) { |
|
54 | - $this->output->writeln("<info>$message</info>"); |
|
55 | - } |
|
50 | + /** |
|
51 | + * @param string $message |
|
52 | + */ |
|
53 | + public function info($message) { |
|
54 | + $this->output->writeln("<info>$message</info>"); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param string $message |
|
59 | - */ |
|
60 | - public function warning($message) { |
|
61 | - $this->output->writeln("<comment>$message</comment>"); |
|
62 | - } |
|
57 | + /** |
|
58 | + * @param string $message |
|
59 | + */ |
|
60 | + public function warning($message) { |
|
61 | + $this->output->writeln("<comment>$message</comment>"); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @param int $max |
|
66 | - */ |
|
67 | - public function startProgress($max = 0) { |
|
68 | - if (!is_null($this->progressBar)) { |
|
69 | - $this->progressBar->finish(); |
|
70 | - } |
|
71 | - $this->progressBar = new ProgressBar($this->output); |
|
72 | - $this->progressBar->start($max); |
|
73 | - } |
|
64 | + /** |
|
65 | + * @param int $max |
|
66 | + */ |
|
67 | + public function startProgress($max = 0) { |
|
68 | + if (!is_null($this->progressBar)) { |
|
69 | + $this->progressBar->finish(); |
|
70 | + } |
|
71 | + $this->progressBar = new ProgressBar($this->output); |
|
72 | + $this->progressBar->start($max); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * @param int $step |
|
77 | - * @param string $description |
|
78 | - */ |
|
79 | - public function advance($step = 1, $description = '') { |
|
80 | - if (!is_null($this->progressBar)) { |
|
81 | - $this->progressBar = new ProgressBar($this->output); |
|
82 | - $this->progressBar->start(); |
|
83 | - } |
|
84 | - $this->progressBar->advance($step); |
|
85 | - } |
|
75 | + /** |
|
76 | + * @param int $step |
|
77 | + * @param string $description |
|
78 | + */ |
|
79 | + public function advance($step = 1, $description = '') { |
|
80 | + if (!is_null($this->progressBar)) { |
|
81 | + $this->progressBar = new ProgressBar($this->output); |
|
82 | + $this->progressBar->start(); |
|
83 | + } |
|
84 | + $this->progressBar->advance($step); |
|
85 | + } |
|
86 | 86 | |
87 | - public function finishProgress() { |
|
88 | - if (is_null($this->progressBar)) { |
|
89 | - return; |
|
90 | - } |
|
91 | - $this->progressBar->finish(); |
|
92 | - } |
|
87 | + public function finishProgress() { |
|
88 | + if (is_null($this->progressBar)) { |
|
89 | + return; |
|
90 | + } |
|
91 | + $this->progressBar->finish(); |
|
92 | + } |
|
93 | 93 | } |
@@ -34,102 +34,102 @@ |
||
34 | 34 | |
35 | 35 | class EncryptAll extends Command { |
36 | 36 | |
37 | - /** @var IManager */ |
|
38 | - protected $encryptionManager; |
|
39 | - |
|
40 | - /** @var IAppManager */ |
|
41 | - protected $appManager; |
|
42 | - |
|
43 | - /** @var IConfig */ |
|
44 | - protected $config; |
|
45 | - |
|
46 | - /** @var QuestionHelper */ |
|
47 | - protected $questionHelper; |
|
48 | - |
|
49 | - /** @var bool */ |
|
50 | - protected $wasTrashbinEnabled; |
|
51 | - |
|
52 | - /** @var bool */ |
|
53 | - protected $wasMaintenanceModeEnabled; |
|
54 | - |
|
55 | - /** |
|
56 | - * @param IManager $encryptionManager |
|
57 | - * @param IAppManager $appManager |
|
58 | - * @param IConfig $config |
|
59 | - * @param QuestionHelper $questionHelper |
|
60 | - */ |
|
61 | - public function __construct( |
|
62 | - IManager $encryptionManager, |
|
63 | - IAppManager $appManager, |
|
64 | - IConfig $config, |
|
65 | - QuestionHelper $questionHelper |
|
66 | - ) { |
|
67 | - parent::__construct(); |
|
68 | - $this->appManager = $appManager; |
|
69 | - $this->encryptionManager = $encryptionManager; |
|
70 | - $this->config = $config; |
|
71 | - $this->questionHelper = $questionHelper; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Set maintenance mode and disable the trashbin app |
|
76 | - */ |
|
77 | - protected function forceMaintenanceAndTrashbin() { |
|
78 | - $this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin'); |
|
79 | - $this->wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false); |
|
80 | - $this->config->setSystemValue('maintenance', true); |
|
81 | - $this->appManager->disableApp('files_trashbin'); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Reset the maintenance mode and re-enable the trashbin app |
|
86 | - */ |
|
87 | - protected function resetMaintenanceAndTrashbin() { |
|
88 | - $this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled); |
|
89 | - if ($this->wasTrashbinEnabled) { |
|
90 | - $this->appManager->enableApp('files_trashbin'); |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - protected function configure() { |
|
95 | - parent::configure(); |
|
96 | - |
|
97 | - $this->setName('encryption:encrypt-all'); |
|
98 | - $this->setDescription('Encrypt all files for all users'); |
|
99 | - $this->setHelp( |
|
100 | - 'This will encrypt all files for all users. ' |
|
101 | - . 'Please make sure that no user access his files during this process!' |
|
102 | - ); |
|
103 | - } |
|
104 | - |
|
105 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
106 | - |
|
107 | - if ($this->encryptionManager->isEnabled() === false) { |
|
108 | - throw new \Exception('Server side encryption is not enabled'); |
|
109 | - } |
|
110 | - |
|
111 | - $output->writeln("\n"); |
|
112 | - $output->writeln('You are about to encrypt all files stored in your Nextcloud installation.'); |
|
113 | - $output->writeln('Depending on the number of available files, and their size, this may take quite some time.'); |
|
114 | - $output->writeln('Please ensure that no user accesses their files during this time!'); |
|
115 | - $output->writeln('Note: The encryption module you use determines which files get encrypted.'); |
|
116 | - $output->writeln(''); |
|
117 | - $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false); |
|
118 | - if ($this->questionHelper->ask($input, $output, $question)) { |
|
119 | - $this->forceMaintenanceAndTrashbin(); |
|
120 | - |
|
121 | - try { |
|
122 | - $defaultModule = $this->encryptionManager->getEncryptionModule(); |
|
123 | - $defaultModule->encryptAll($input, $output); |
|
124 | - } catch (\Exception $ex) { |
|
125 | - $this->resetMaintenanceAndTrashbin(); |
|
126 | - throw $ex; |
|
127 | - } |
|
128 | - |
|
129 | - $this->resetMaintenanceAndTrashbin(); |
|
130 | - } else { |
|
131 | - $output->writeln('aborted'); |
|
132 | - } |
|
133 | - } |
|
37 | + /** @var IManager */ |
|
38 | + protected $encryptionManager; |
|
39 | + |
|
40 | + /** @var IAppManager */ |
|
41 | + protected $appManager; |
|
42 | + |
|
43 | + /** @var IConfig */ |
|
44 | + protected $config; |
|
45 | + |
|
46 | + /** @var QuestionHelper */ |
|
47 | + protected $questionHelper; |
|
48 | + |
|
49 | + /** @var bool */ |
|
50 | + protected $wasTrashbinEnabled; |
|
51 | + |
|
52 | + /** @var bool */ |
|
53 | + protected $wasMaintenanceModeEnabled; |
|
54 | + |
|
55 | + /** |
|
56 | + * @param IManager $encryptionManager |
|
57 | + * @param IAppManager $appManager |
|
58 | + * @param IConfig $config |
|
59 | + * @param QuestionHelper $questionHelper |
|
60 | + */ |
|
61 | + public function __construct( |
|
62 | + IManager $encryptionManager, |
|
63 | + IAppManager $appManager, |
|
64 | + IConfig $config, |
|
65 | + QuestionHelper $questionHelper |
|
66 | + ) { |
|
67 | + parent::__construct(); |
|
68 | + $this->appManager = $appManager; |
|
69 | + $this->encryptionManager = $encryptionManager; |
|
70 | + $this->config = $config; |
|
71 | + $this->questionHelper = $questionHelper; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Set maintenance mode and disable the trashbin app |
|
76 | + */ |
|
77 | + protected function forceMaintenanceAndTrashbin() { |
|
78 | + $this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin'); |
|
79 | + $this->wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false); |
|
80 | + $this->config->setSystemValue('maintenance', true); |
|
81 | + $this->appManager->disableApp('files_trashbin'); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Reset the maintenance mode and re-enable the trashbin app |
|
86 | + */ |
|
87 | + protected function resetMaintenanceAndTrashbin() { |
|
88 | + $this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled); |
|
89 | + if ($this->wasTrashbinEnabled) { |
|
90 | + $this->appManager->enableApp('files_trashbin'); |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + protected function configure() { |
|
95 | + parent::configure(); |
|
96 | + |
|
97 | + $this->setName('encryption:encrypt-all'); |
|
98 | + $this->setDescription('Encrypt all files for all users'); |
|
99 | + $this->setHelp( |
|
100 | + 'This will encrypt all files for all users. ' |
|
101 | + . 'Please make sure that no user access his files during this process!' |
|
102 | + ); |
|
103 | + } |
|
104 | + |
|
105 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
106 | + |
|
107 | + if ($this->encryptionManager->isEnabled() === false) { |
|
108 | + throw new \Exception('Server side encryption is not enabled'); |
|
109 | + } |
|
110 | + |
|
111 | + $output->writeln("\n"); |
|
112 | + $output->writeln('You are about to encrypt all files stored in your Nextcloud installation.'); |
|
113 | + $output->writeln('Depending on the number of available files, and their size, this may take quite some time.'); |
|
114 | + $output->writeln('Please ensure that no user accesses their files during this time!'); |
|
115 | + $output->writeln('Note: The encryption module you use determines which files get encrypted.'); |
|
116 | + $output->writeln(''); |
|
117 | + $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false); |
|
118 | + if ($this->questionHelper->ask($input, $output, $question)) { |
|
119 | + $this->forceMaintenanceAndTrashbin(); |
|
120 | + |
|
121 | + try { |
|
122 | + $defaultModule = $this->encryptionManager->getEncryptionModule(); |
|
123 | + $defaultModule->encryptAll($input, $output); |
|
124 | + } catch (\Exception $ex) { |
|
125 | + $this->resetMaintenanceAndTrashbin(); |
|
126 | + throw $ex; |
|
127 | + } |
|
128 | + |
|
129 | + $this->resetMaintenanceAndTrashbin(); |
|
130 | + } else { |
|
131 | + $output->writeln('aborted'); |
|
132 | + } |
|
133 | + } |
|
134 | 134 | |
135 | 135 | } |