This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * ownCloud - galleryplus |
||
4 | * |
||
5 | * This file is licensed under the Affero General Public License version 3 or |
||
6 | * later. See the COPYING file. |
||
7 | * |
||
8 | * @author Olivier Paroz <[email protected]> |
||
9 | * |
||
10 | * @copyright Olivier Paroz 2014-2016 |
||
11 | */ |
||
12 | |||
13 | namespace OCA\GalleryPlus\Service; |
||
14 | |||
15 | use OCP\Files\File; |
||
16 | use OCP\Files\Folder; |
||
17 | use OCP\Files\Node; |
||
18 | |||
19 | /** |
||
20 | * Contains various methods to retrieve information from the filesystem |
||
21 | * |
||
22 | * @package OCA\GalleryPlus\Service |
||
23 | */ |
||
24 | abstract class FilesService extends Service { |
||
25 | |||
26 | /** @var int */ |
||
27 | protected $virtualRootLevel = null; |
||
28 | /** @var string[] */ |
||
29 | protected $features; |
||
30 | /** @var string */ |
||
31 | protected $ignoreAlbum = '.nomedia'; |
||
32 | |||
33 | /** |
||
34 | * Retrieves all files and sub-folders contained in a folder |
||
35 | * |
||
36 | * If we can't find anything in the current folder, we throw an exception as there is no point |
||
37 | * in doing any more work, but if we're looking at a sub-folder, we return an empty array so |
||
38 | * that it can be simply ignored |
||
39 | * |
||
40 | * @param Folder $folder |
||
41 | * @param int $subDepth |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | 15 | protected function getNodes($folder, $subDepth) { |
|
46 | try { |
||
47 | 15 | $nodes = $folder->getDirectoryListing(); |
|
48 | 2 | } catch (\Exception $exception) { |
|
49 | 2 | $nodes = $this->recoverFromGetNodesError($subDepth, $exception); |
|
50 | } |
||
51 | |||
52 | 14 | return $nodes; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Determines if the files are hosted locally (shared or not) and can be used by the preview |
||
57 | * system |
||
58 | * |
||
59 | * isMounted() doesn't include externally hosted shares, so we need to exclude those from the |
||
60 | * non-mounted nodes |
||
61 | * |
||
62 | * @param Node $node |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | 35 | protected function isAllowedAndAvailable($node) { |
|
67 | try { |
||
68 | 35 | return $node && $this->isAllowed($node) && $this->isAvailable($node); |
|
69 | 2 | } catch (\Exception $exception) { |
|
70 | 2 | $message = 'The folder is not available: ' . $exception->getMessage(); |
|
71 | 2 | $this->logger->error($message); |
|
72 | |||
73 | 2 | return false; |
|
74 | } |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Returns the node type, either 'dir' or 'file' |
||
79 | * |
||
80 | * If there is a problem, we return an empty string so that the node can be ignored |
||
81 | * |
||
82 | * @param Node $node |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 14 | protected function getNodeType($node) { |
|
87 | try { |
||
88 | 14 | $nodeType = $node->getType(); |
|
89 | 1 | } catch (\Exception $exception) { |
|
90 | 1 | return ''; |
|
91 | } |
||
92 | |||
93 | 13 | return $nodeType; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Returns various information about a node |
||
98 | * |
||
99 | * @param Node|File|Folder $node |
||
100 | * |
||
101 | * @return array<string,int|string|bool|array<string,int|string>> |
||
102 | */ |
||
103 | 13 | protected function getNodeData($node) { |
|
104 | 13 | $imagePath = $this->environment->getPathFromVirtualRoot($node); |
|
105 | 13 | $nodeId = $node->getId(); |
|
106 | 13 | $mTime = $node->getMTime(); |
|
107 | 13 | $etag = $node->getEtag(); |
|
108 | 13 | $size = $node->getSize(); |
|
109 | 13 | $sharedWithUser = $node->isShared(); |
|
110 | 13 | $ownerData = $this->getOwnerData($node); |
|
111 | 13 | $permissions = $node->getPermissions(); |
|
112 | |||
113 | //$this->logger->debug("Image path : {var1}", ['var1' => $imagePath]); |
||
114 | |||
115 | 13 | return $this->formatNodeData( |
|
116 | $imagePath, $nodeId, $mTime, $etag, $size, $sharedWithUser, $ownerData, $permissions |
||
117 | ); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Returns various information about a folder |
||
122 | * |
||
123 | * @param Folder $node |
||
124 | * |
||
125 | * @return array<string,int|string|bool|array<string,int|string>> |
||
126 | */ |
||
127 | 13 | protected function getFolderData($node) { |
|
128 | 13 | $folderData = $this->getNodeData($node); |
|
129 | 13 | $folderData['freespace'] = $node->getFreeSpace(); |
|
130 | |||
131 | 13 | return $folderData; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns the node if it's a folder we have access to |
||
136 | * |
||
137 | * @param Folder $node |
||
138 | * @param string $nodeType |
||
139 | * |
||
140 | * @return array|Folder |
||
141 | */ |
||
142 | 14 | protected function getAllowedSubFolder($node, $nodeType) { |
|
143 | 14 | if ($nodeType === 'dir') { |
|
144 | /** @var Folder $node */ |
||
145 | 13 | if (!$node->nodeExists($this->ignoreAlbum)) { |
|
146 | 11 | return [$node]; |
|
147 | } |
||
148 | } |
||
149 | |||
150 | 14 | return []; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Determines if we've reached the root folder |
||
155 | * |
||
156 | * @param Folder $folder |
||
157 | * @param int $level |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | 5 | protected function isRootFolder($folder, $level) { |
|
162 | 5 | $isRootFolder = false; |
|
163 | 5 | $rootFolder = $this->environment->getVirtualRootFolder(); |
|
164 | 5 | if ($folder->getPath() === $rootFolder->getPath()) { |
|
165 | 5 | $isRootFolder = true; |
|
166 | } |
||
167 | 5 | $virtualRootFolder = $this->environment->getPathFromVirtualRoot($folder); |
|
168 | 5 | if (empty($virtualRootFolder)) { |
|
169 | 5 | $this->virtualRootLevel = $level; |
|
170 | } |
||
171 | |||
172 | 5 | return $isRootFolder; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Throws an exception if this problem occurs in the current folder, otherwise just ignores the |
||
177 | * sub-folder |
||
178 | * |
||
179 | * @param int $subDepth |
||
180 | * @param \Exception $exception |
||
181 | * |
||
182 | * @return array |
||
183 | * @throws NotFoundServiceException |
||
184 | */ |
||
185 | 4 | private function recoverFromGetNodesError($subDepth, $exception) { |
|
186 | 4 | if ($subDepth === 0) { |
|
187 | 2 | throw new NotFoundServiceException($exception->getMessage()); |
|
188 | } |
||
189 | |||
190 | 2 | return []; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Determines if we can consider the node mounted locally or if it's been authorised to be |
||
195 | * scanned |
||
196 | * |
||
197 | * @param Node $node |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | 34 | private function isAllowed($node) { |
|
202 | 34 | $allowed = true; |
|
203 | 34 | if ($this->isExternalShare($node)) { |
|
204 | 6 | $allowed = $this->isExternalShareAllowed(); |
|
205 | } |
||
206 | |||
207 | 33 | if ($node->isMounted()) { |
|
208 | 3 | $mount = $node->getMountPoint(); |
|
209 | 3 | $allowed = $mount && $mount->getOption('previews', true); |
|
210 | } |
||
211 | |||
212 | 33 | return $allowed; |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * Determines if the node is available, as in readable |
||
217 | * |
||
218 | * @todo Test to see by how much using file_exists slows things down |
||
219 | * |
||
220 | * @param Node $node |
||
221 | * |
||
222 | * @return bool |
||
223 | */ |
||
224 | 31 | private function isAvailable($node) { |
|
225 | 31 | return $node->isReadable(); |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * Determines if the user has allowed the use of external shares |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | 6 | private function isExternalShareAllowed() { |
|
234 | 6 | $rootFolder = $this->environment->getVirtualRootFolder(); |
|
235 | |||
236 | 6 | return ($this->isExternalShare($rootFolder) |
|
0 ignored issues
–
show
|
|||
237 | 6 | || in_array('external_shares', $this->features)); |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * Determines if the node is a share which is hosted externally |
||
242 | * |
||
243 | * |
||
244 | * @param Node $node |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | 34 | private function isExternalShare($node) { |
|
249 | 34 | $sid = explode( |
|
250 | 34 | ':', |
|
251 | 34 | $node->getStorage() |
|
252 | 34 | ->getId() |
|
253 | ); |
||
254 | |||
255 | 34 | return ($sid[0] === 'shared' && $sid[2][0] !== '/'); |
|
256 | } |
||
257 | |||
258 | /** |
||
259 | * Returns what we known about the owner of a node |
||
260 | * |
||
261 | * @param Node $node |
||
262 | * |
||
263 | * @return null|array<string,int|string> |
||
0 ignored issues
–
show
Should the return type not be
array ? Also, consider making the array more specific, something like array<String> , or String[] .
This check compares the return type specified in the If the return type contains the type array, this check recommends the use of
a more specific type like ![]() |
|||
264 | */ |
||
265 | 13 | private function getOwnerData($node) { |
|
266 | 13 | $owner = $node->getOwner(); |
|
267 | 13 | $ownerData = []; |
|
268 | 13 | if ($owner) { |
|
269 | $ownerData = [ |
||
270 | 6 | 'uid' => $owner->getUID(), |
|
271 | 6 | 'displayname' => $owner->getDisplayName() |
|
272 | ]; |
||
273 | } |
||
274 | |||
275 | 13 | return $ownerData; |
|
276 | } |
||
277 | |||
278 | /** |
||
279 | * Returns an array containing information about a node |
||
280 | * |
||
281 | * @param string $imagePath |
||
282 | * @param int $nodeId |
||
283 | * @param int $mTime |
||
284 | * @param string $etag |
||
285 | * @param int $size |
||
286 | * @param bool $sharedWithUser |
||
287 | * @param array <string,int|string> $ownerData |
||
288 | * @param int $permissions |
||
289 | * |
||
290 | * @return array |
||
291 | */ |
||
292 | 13 | private function formatNodeData( |
|
293 | $imagePath, $nodeId, $mTime, $etag, $size, $sharedWithUser, $ownerData, $permissions |
||
294 | ) { |
||
295 | return [ |
||
296 | 13 | 'path' => $imagePath, |
|
297 | 13 | 'nodeid' => $nodeId, |
|
298 | 13 | 'mtime' => $mTime, |
|
299 | 13 | 'etag' => $etag, |
|
300 | 13 | 'size' => $size, |
|
301 | 13 | 'sharedwithuser' => $sharedWithUser, |
|
302 | 13 | 'owner' => $ownerData, |
|
303 | 13 | 'permissions' => $permissions |
|
304 | ]; |
||
305 | } |
||
306 | |||
307 | } |
||
308 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.