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 | * Gallery |
||
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 | * @author Robin Appelman <[email protected]> |
||
10 | * |
||
11 | * @copyright Olivier Paroz 2014-2016 |
||
12 | * @copyright Robin Appelman 2012-2014 |
||
13 | */ |
||
14 | |||
15 | namespace OCA\Gallery\Controller; |
||
16 | |||
17 | use OCP\IURLGenerator; |
||
18 | use OCP\ILogger; |
||
19 | use OCP\Files\File; |
||
20 | |||
21 | use OCP\AppFramework\Http; |
||
22 | |||
23 | use OCA\Gallery\Service\ServiceException; |
||
24 | use OCA\Gallery\Service\NotFoundServiceException; |
||
25 | use OCA\Gallery\Service\ConfigService; |
||
26 | use OCA\Gallery\Service\ThumbnailService; |
||
27 | use OCA\Gallery\Service\PreviewService; |
||
28 | use OCA\Gallery\Service\DownloadService; |
||
29 | |||
30 | /** |
||
31 | * Class Preview |
||
32 | * |
||
33 | * @package OCA\Gallery\Controller |
||
34 | */ |
||
35 | trait Preview { |
||
36 | use HttpError; |
||
37 | |||
38 | /** @var IURLGenerator */ |
||
39 | private $urlGenerator; |
||
40 | /** @var ConfigService */ |
||
41 | private $configService; |
||
42 | /** @var ThumbnailService */ |
||
43 | private $thumbnailService; |
||
44 | /** @var PreviewService */ |
||
45 | private $previewService; |
||
46 | /** @var DownloadService */ |
||
47 | private $downloadService; |
||
48 | /** @var ILogger */ |
||
49 | private $logger; |
||
50 | /** @type bool */ |
||
51 | private $download = false; |
||
52 | |||
53 | /** |
||
54 | * Exits the controller in a live environment and throws an exception when testing |
||
55 | */ |
||
56 | protected function exitController() { |
||
57 | if (\defined('PHPUNIT_RUN')) { |
||
58 | throw new \Exception(); |
||
59 | // @codeCoverageIgnoreStart |
||
60 | } else { |
||
61 | exit(); |
||
62 | } |
||
63 | // @codeCoverageIgnoreEnd |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Retrieves the thumbnail to send back to the browser |
||
68 | * |
||
69 | * The thumbnail is either a resized preview of the file or the original file |
||
70 | * Thumbnails are base64encoded before getting sent back |
||
71 | * |
||
72 | * |
||
73 | * @param int $fileId the ID of the file of which we need a thumbnail preview of |
||
74 | * @param bool $square whether the thumbnail should be square |
||
75 | * @param double $scale whether we're allowed to scale the preview up |
||
76 | * |
||
77 | * @return array<string,array|string> |
||
78 | */ |
||
79 | private function getThumbnail($fileId, $square, $scale) { |
||
80 | list($width, $height, $aspect, $animatedPreview, $base64Encode) = |
||
81 | $this->thumbnailService->getThumbnailSpecs($square, $scale); |
||
82 | /** @type File $file */ |
||
83 | list($file, $preview, $status) = |
||
84 | $this->getData( |
||
85 | $fileId, $width, $height, $aspect, $animatedPreview, $base64Encode |
||
86 | ); |
||
87 | if ($preview === null) { |
||
88 | $preview = $this->prepareEmptyThumbnail($file, $status); |
||
89 | } |
||
90 | |||
91 | return [$preview, $status]; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Returns either a generated preview, the file as-is or an empty object |
||
96 | * |
||
97 | * @param int $fileId |
||
98 | * @param int $width |
||
99 | * @param int $height |
||
100 | * @param bool $keepAspect |
||
101 | * @param bool $animatedPreview |
||
102 | * @param bool $base64Encode |
||
103 | * |
||
104 | * @return array<string,\OC_Image|string> |
||
105 | * |
||
106 | * @throws NotFoundServiceException |
||
107 | */ |
||
108 | private function getData( |
||
109 | $fileId, $width, $height, $keepAspect = true, $animatedPreview = true, $base64Encode = false |
||
110 | ) { |
||
111 | /** @type File $file */ |
||
112 | list($file, $status) = $this->getFile($fileId); |
||
113 | try { |
||
114 | if ($file !== null) { |
||
115 | $data = $this->getPreviewData( |
||
116 | $file, $animatedPreview, $width, $height, $keepAspect, $base64Encode |
||
117 | ); |
||
118 | } else { |
||
119 | $data = $this->getErrorData($status); |
||
120 | } |
||
121 | } catch (ServiceException $exception) { |
||
122 | $data = $this->getExceptionData($exception); |
||
123 | } |
||
124 | \array_unshift($data, $file); |
||
125 | |||
126 | return $data; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Returns the file of which a preview will be generated |
||
131 | * |
||
132 | * @param int $fileId |
||
133 | * |
||
134 | * @return array<File|int|null> |
||
135 | */ |
||
136 | private function getFile($fileId) { |
||
137 | $status = Http::STATUS_OK; |
||
138 | try { |
||
139 | /** @type File $file */ |
||
140 | $file = $this->previewService->getFile($fileId); |
||
141 | $this->configService->validateMimeType($file->getMimeType()); |
||
142 | } catch (ServiceException $exception) { |
||
143 | $file = null; |
||
144 | $status = $this->getHttpStatusCode($exception); |
||
145 | } |
||
146 | |||
147 | return [$file, $status]; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param File $file |
||
152 | * @param bool $animatedPreview |
||
153 | * @param int $width |
||
154 | * @param int $height |
||
155 | * @param bool $keepAspect |
||
156 | * @param bool $base64Encode |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | private function getPreviewData( |
||
161 | $file, $animatedPreview, $width, $height, $keepAspect, $base64Encode |
||
162 | ) { |
||
163 | $status = Http::STATUS_OK; |
||
164 | if ($this->previewService->isPreviewRequired($file, $animatedPreview)) { |
||
165 | $preview = $this->previewService->createPreview( |
||
166 | $file, $width, $height, $keepAspect, $base64Encode |
||
167 | ); |
||
168 | } else { |
||
169 | $preview = $this->downloadService->downloadFile($file, $base64Encode); |
||
170 | } |
||
171 | if (!$preview) { |
||
172 | list($preview, $status) = $this->getErrorData(); |
||
173 | } |
||
174 | |||
175 | return [$preview, $status]; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Returns an error array |
||
180 | * |
||
181 | * @param $status |
||
182 | * |
||
183 | * @return array<null|int> |
||
184 | */ |
||
185 | private function getErrorData($status = Http::STATUS_INTERNAL_SERVER_ERROR) { |
||
186 | return [null, $status]; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Returns an error array |
||
191 | * |
||
192 | * @param ServiceException $exception |
||
193 | * |
||
194 | * @return array<null|int|string> |
||
0 ignored issues
–
show
|
|||
195 | */ |
||
196 | private function getExceptionData($exception) { |
||
197 | $code = $this->getHttpStatusCode($exception); |
||
198 | |||
199 | return $this->getErrorData($code); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Prepares an empty Thumbnail array to send back |
||
204 | * |
||
205 | * When we can't even get the file information, we send an empty mimeType |
||
206 | * |
||
207 | * @param File $file |
||
208 | * @param int $status |
||
209 | * |
||
210 | * @return array<string,null|string> |
||
211 | */ |
||
212 | private function prepareEmptyThumbnail($file, $status) { |
||
213 | $thumbnail = []; |
||
214 | if ($status !== Http::STATUS_NOT_FOUND) { |
||
215 | $mimeType = ''; |
||
216 | if ($file) { |
||
217 | $mimeType = $file->getMimeType(); |
||
218 | } |
||
219 | $thumbnail = ['preview' => null, 'mimetype' => $mimeType]; |
||
220 | } |
||
221 | |||
222 | return $thumbnail; |
||
223 | } |
||
224 | } |
||
225 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.