1 | <?php |
||
33 | class ConfigService extends FilesService { |
||
34 | |||
35 | /** @var string */ |
||
36 | private $configName = 'gallery.cnf'; |
||
37 | /** @var array <string,bool> */ |
||
38 | private $completionStatus = ['design' => false, 'information' => false, 'sorting' => false]; |
||
39 | /** @var ConfigParser */ |
||
40 | private $configParser; |
||
41 | /** @var Preview */ |
||
42 | private $previewManager; |
||
43 | /** |
||
44 | * @todo This hard-coded array could be replaced by admin settings |
||
45 | * |
||
46 | * @var string[] |
||
47 | */ |
||
48 | private $baseMimeTypes = [ |
||
49 | 'image/png', |
||
50 | 'image/jpeg', |
||
51 | 'image/gif', |
||
52 | 'image/x-xbitmap', |
||
53 | 'image/bmp', |
||
54 | 'image/tiff', |
||
55 | 'image/x-dcraw', |
||
56 | 'application/x-photoshop', |
||
57 | 'application/illustrator', |
||
58 | 'application/postscript', |
||
59 | ]; |
||
60 | /** |
||
61 | * These types are useful for files preview in the files app, but |
||
62 | * not for the gallery side |
||
63 | * |
||
64 | * @var string[] |
||
65 | */ |
||
66 | private $slideshowMimeTypes = [ |
||
67 | 'application/font-sfnt', |
||
68 | 'application/x-font', |
||
69 | ]; |
||
70 | |||
71 | /** |
||
72 | * Constructor |
||
73 | * |
||
74 | * @param string $appName |
||
75 | * @param Environment $environment |
||
76 | * @param ConfigParser $configParser |
||
77 | * @param Preview $previewManager |
||
78 | * @param ILogger $logger |
||
79 | */ |
||
80 | public function __construct( |
||
81 | $appName, |
||
82 | Environment $environment, |
||
83 | ConfigParser $configParser, |
||
84 | Preview $previewManager, |
||
85 | ILogger $logger |
||
86 | ) { |
||
87 | parent::__construct($appName, $environment, $logger); |
||
88 | |||
89 | 57 | $this->configParser = $configParser; |
|
90 | $this->previewManager = $previewManager; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Returns a list of supported features |
||
95 | * |
||
96 | 57 | * @return string[] |
|
97 | */ |
||
98 | 57 | public function getFeaturesList() { |
|
99 | 57 | $featuresList = []; |
|
100 | 57 | /** @var Folder $rootFolder */ |
|
101 | $rootFolder = $this->environment->getVirtualRootFolder(); |
||
102 | if ($this->isAllowedAndAvailable($rootFolder) && $this->configExists($rootFolder)) { |
||
103 | try { |
||
104 | $featuresList = |
||
105 | $this->configParser->getFeaturesList($rootFolder, $this->configName); |
||
106 | } catch (ConfigException $exception) { |
||
107 | 6 | $featuresList = $this->buildErrorMessage($exception, $rootFolder); |
|
108 | 6 | } |
|
109 | } |
||
110 | 6 | ||
111 | 6 | return $featuresList; |
|
112 | } |
||
113 | |||
114 | 5 | /** |
|
115 | 5 | * This builds and returns a list of all supported media types |
|
116 | 1 | * |
|
117 | * @todo Native SVG could be disabled via admin settings |
||
118 | * |
||
119 | * @param bool $extraMediaTypes |
||
120 | 6 | * @param bool $nativeSvgSupport |
|
121 | * |
||
122 | * @return string[] all supported media types |
||
123 | */ |
||
124 | public function getSupportedMediaTypes($extraMediaTypes, $nativeSvgSupport) { |
||
125 | $supportedMimes = []; |
||
126 | $wantedMimes = $this->baseMimeTypes; |
||
127 | if ($extraMediaTypes) { |
||
128 | $wantedMimes = array_merge($wantedMimes, $this->slideshowMimeTypes); |
||
129 | } |
||
130 | foreach ($wantedMimes as $wantedMime) { |
||
131 | // Let's see if a preview of files of that media type can be generated |
||
132 | if ($this->isMimeSupported($wantedMime)) { |
||
133 | 32 | // We store the media type |
|
134 | 32 | $supportedMimes[] = $wantedMime; |
|
135 | 32 | } |
|
136 | 32 | } |
|
137 | 24 | $supportedMimes = $this->addSvgSupport($supportedMimes, $nativeSvgSupport); |
|
138 | 13 | ||
139 | 32 | //$this->logger->debug("Supported Mimes: {mimes}", ['mimes' => $supportedMimes]); |
|
140 | |||
141 | 32 | return $supportedMimes; |
|
142 | } |
||
143 | 31 | ||
144 | 15 | /** |
|
145 | 32 | * Returns the configuration of the currently selected folder |
|
146 | 32 | * |
|
147 | * * information (description, copyright) |
||
148 | * * sorting (date, name, inheritance) |
||
149 | * * design (colour) |
||
150 | 32 | * * if the album should be ignored |
|
151 | * |
||
152 | * @param Folder $folderNode the current folder |
||
153 | * @param array $features the list of features retrieved fro the configuration file |
||
154 | * |
||
155 | * @return array|null |
||
156 | * @throws ForbiddenServiceException |
||
157 | */ |
||
158 | public function getConfig($folderNode, $features) { |
||
159 | $this->features = $features; |
||
160 | list ($albumConfig, $ignored) = |
||
161 | $this->collectConfig($folderNode, $this->ignoreAlbum, $this->configName); |
||
162 | if ($ignored) { |
||
163 | throw new ForbiddenServiceException( |
||
164 | 'The owner has placed a restriction or the storage location is unavailable' |
||
165 | ); |
||
166 | } |
||
167 | |||
168 | 6 | return $albumConfig; |
|
169 | 6 | } |
|
170 | |||
171 | 6 | /** |
|
172 | 6 | * Throws an exception if the media type of the file is not part of what the app allows |
|
173 | 1 | * |
|
174 | * @param $mimeType |
||
175 | * |
||
176 | 5 | * @throws ForbiddenServiceException |
|
177 | 5 | */ |
|
178 | 5 | public function validateMimeType($mimeType) { |
|
183 | |||
184 | 5 | /** |
|
185 | * Determines if we have a configuration file to work with |
||
186 | * |
||
187 | * @param Folder $rootFolder the virtual root folder |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | private function configExists($rootFolder) { |
||
194 | 21 | ||
195 | 21 | /** |
|
196 | 6 | * Adds the SVG media type if it's not already there |
|
197 | * |
||
198 | 15 | * If it's enabled, but doesn't work, an exception will be raised when trying to generate a |
|
199 | * preview. If it's disabled, we support it via the browser's native support |
||
200 | * |
||
201 | * @param string[] $supportedMimes |
||
202 | * @param bool $nativeSvgSupport |
||
203 | * |
||
204 | * @return string[] |
||
205 | */ |
||
206 | private function addSvgSupport($supportedMimes, $nativeSvgSupport) { |
||
213 | |||
214 | /** |
||
215 | * Returns true if the passed mime type is supported |
||
216 | * |
||
217 | * In case of a failure, we just return that the media type is not supported |
||
218 | * |
||
219 | * @param string $mimeType |
||
220 | * |
||
221 | * @return boolean |
||
222 | 36 | */ |
|
223 | 36 | private function isMimeSupported($mimeType = '*') { |
|
232 | |||
233 | /** |
||
234 | * Returns an album configuration array |
||
235 | * |
||
236 | * Goes through all the parent folders until either we're told the album is private or we've |
||
237 | * reached the root folder |
||
238 | * |
||
239 | 32 | * @param Folder $folder the current folder |
|
240 | * @param string $ignoreAlbum name of the file which blacklists folders |
||
241 | 32 | * @param string $configName name of the configuration file |
|
242 | 1 | * @param int $level the starting level is 0 and we add 1 each time we visit a parent folder |
|
243 | 1 | * @param array $configSoFar the configuration collected so far |
|
244 | * |
||
245 | 1 | * @return array <null|array,bool> |
|
246 | */ |
||
247 | private function collectConfig( |
||
266 | 6 | ||
267 | /** |
||
268 | 1 | * Returns a parsed configuration if one was found in the current folder or generates an error |
|
269 | * message to send back |
||
270 | 5 | * |
|
271 | 5 | * @param Folder $folder the current folder |
|
272 | 4 | * @param string $configName name of the configuration file |
|
273 | * @param array $collectedConfig the configuration collected so far |
||
274 | 5 | * @param int $level the starting level is 0 and we add 1 each time we visit a parent folder |
|
275 | 1 | * |
|
276 | 1 | * @return array |
|
277 | 1 | */ |
|
278 | private function buildFolderConfig($folder, $configName, $collectedConfig, $level) { |
||
290 | |||
291 | /** |
||
292 | * Builds the error message to send back when there is an error |
||
293 | * |
||
294 | * @fixme Missing translation |
||
295 | * |
||
296 | 5 | * @param ConfigException $exception |
|
297 | * @param Folder $folder the current folder |
||
298 | 5 | * |
|
299 | 5 | * @return array<array<string,string>,bool> |
|
300 | 5 | */ |
|
301 | 4 | private function buildErrorMessage($exception, $folder) { |
|
315 | |||
316 | /** |
||
317 | * Removes links if they were collected outside of the virtual root |
||
318 | * |
||
319 | 2 | * This is for shared folders which have a virtual root |
|
320 | 2 | * |
|
321 | 2 | * @param array $albumConfig |
|
322 | 2 | * |
|
323 | 2 | * @return array |
|
324 | */ |
||
325 | 2 | private function validatesInfoConfig($albumConfig) { |
|
340 | |||
341 | /** |
||
342 | * Looks for an album configuration in the parent folder |
||
343 | 8 | * |
|
344 | 8 | * We will look up to the virtual root of a shared folder, for privacy reasons |
|
345 | 8 | * |
|
346 | 7 | * @param Folder $folder the current folder |
|
347 | 7 | * @param string $privacyChecker name of the file which blacklists folders |
|
348 | 7 | * @param string $configName name of the configuration file |
|
349 | 7 | * @param int $level the starting level is 0 and we add 1 each time we visit a parent folder |
|
350 | 1 | * @param array $collectedConfig the configuration collected so far |
|
351 | 1 | * |
|
352 | 1 | * @return array<null|array,bool> |
|
353 | 3 | */ |
|
354 | 3 | private function getParentConfig($folder, $privacyChecker, $configName, $level, $collectedConfig |
|
363 | |||
364 | } |
||
365 |