@@ -132,7 +132,7 @@ |
||
132 | 132 | if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { |
133 | 133 | |
134 | 134 | // If the timestamp is older than 300 seconds request the files new |
135 | - if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { |
|
135 | + if ((int) $jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { |
|
136 | 136 | return $jsonBlob['data']; |
137 | 137 | } |
138 | 138 |
@@ -39,162 +39,162 @@ |
||
39 | 39 | use OCP\Util; |
40 | 40 | |
41 | 41 | abstract class Fetcher { |
42 | - const INVALIDATE_AFTER_SECONDS = 300; |
|
43 | - |
|
44 | - /** @var IAppData */ |
|
45 | - protected $appData; |
|
46 | - /** @var IClientService */ |
|
47 | - protected $clientService; |
|
48 | - /** @var ITimeFactory */ |
|
49 | - protected $timeFactory; |
|
50 | - /** @var IConfig */ |
|
51 | - protected $config; |
|
52 | - /** @var Ilogger */ |
|
53 | - protected $logger; |
|
54 | - /** @var string */ |
|
55 | - protected $fileName; |
|
56 | - /** @var string */ |
|
57 | - protected $endpointUrl; |
|
58 | - /** @var string */ |
|
59 | - protected $version; |
|
60 | - |
|
61 | - /** |
|
62 | - * @param Factory $appDataFactory |
|
63 | - * @param IClientService $clientService |
|
64 | - * @param ITimeFactory $timeFactory |
|
65 | - * @param IConfig $config |
|
66 | - * @param ILogger $logger |
|
67 | - */ |
|
68 | - public function __construct(Factory $appDataFactory, |
|
69 | - IClientService $clientService, |
|
70 | - ITimeFactory $timeFactory, |
|
71 | - IConfig $config, |
|
72 | - ILogger $logger) { |
|
73 | - $this->appData = $appDataFactory->get('appstore'); |
|
74 | - $this->clientService = $clientService; |
|
75 | - $this->timeFactory = $timeFactory; |
|
76 | - $this->config = $config; |
|
77 | - $this->logger = $logger; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Fetches the response from the server |
|
82 | - * |
|
83 | - * @param string $ETag |
|
84 | - * @param string $content |
|
85 | - * |
|
86 | - * @return array |
|
87 | - */ |
|
88 | - protected function fetch($ETag, $content) { |
|
89 | - $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
90 | - |
|
91 | - if (!$appstoreenabled) { |
|
92 | - return []; |
|
93 | - } |
|
94 | - |
|
95 | - $options = [ |
|
96 | - 'timeout' => 10, |
|
97 | - ]; |
|
98 | - |
|
99 | - if ($ETag !== '') { |
|
100 | - $options['headers'] = [ |
|
101 | - 'If-None-Match' => $ETag, |
|
102 | - ]; |
|
103 | - } |
|
104 | - |
|
105 | - $client = $this->clientService->newClient(); |
|
106 | - $response = $client->get($this->endpointUrl, $options); |
|
107 | - |
|
108 | - $responseJson = []; |
|
109 | - if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { |
|
110 | - $responseJson['data'] = json_decode($content, true); |
|
111 | - } else { |
|
112 | - $responseJson['data'] = json_decode($response->getBody(), true); |
|
113 | - $ETag = $response->getHeader('ETag'); |
|
114 | - } |
|
115 | - |
|
116 | - $responseJson['timestamp'] = $this->timeFactory->getTime(); |
|
117 | - $responseJson['ncversion'] = $this->getVersion(); |
|
118 | - if ($ETag !== '') { |
|
119 | - $responseJson['ETag'] = $ETag; |
|
120 | - } |
|
121 | - |
|
122 | - return $responseJson; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Returns the array with the categories on the appstore server |
|
127 | - * |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - public function get() { |
|
131 | - $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
132 | - $internetavailable = $this->config->getSystemValue('has_internet_connection', true); |
|
133 | - |
|
134 | - if (!$appstoreenabled || !$internetavailable) { |
|
135 | - return []; |
|
136 | - } |
|
137 | - |
|
138 | - $rootFolder = $this->appData->getFolder('/'); |
|
139 | - |
|
140 | - $ETag = ''; |
|
141 | - $content = ''; |
|
142 | - |
|
143 | - try { |
|
144 | - // File does already exists |
|
145 | - $file = $rootFolder->getFile($this->fileName); |
|
146 | - $jsonBlob = json_decode($file->getContent(), true); |
|
147 | - if (is_array($jsonBlob)) { |
|
148 | - |
|
149 | - // No caching when the version has been updated |
|
150 | - if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { |
|
151 | - |
|
152 | - // If the timestamp is older than 300 seconds request the files new |
|
153 | - if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { |
|
154 | - return $jsonBlob['data']; |
|
155 | - } |
|
156 | - |
|
157 | - if (isset($jsonBlob['ETag'])) { |
|
158 | - $ETag = $jsonBlob['ETag']; |
|
159 | - $content = json_encode($jsonBlob['data']); |
|
160 | - } |
|
161 | - } |
|
162 | - } |
|
163 | - } catch (NotFoundException $e) { |
|
164 | - // File does not already exists |
|
165 | - $file = $rootFolder->newFile($this->fileName); |
|
166 | - } |
|
167 | - |
|
168 | - // Refresh the file content |
|
169 | - try { |
|
170 | - $responseJson = $this->fetch($ETag, $content); |
|
171 | - $file->putContent(json_encode($responseJson)); |
|
172 | - return json_decode($file->getContent(), true)['data']; |
|
173 | - } catch (ConnectException $e) { |
|
174 | - $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO, 'message' => 'Could not connect to appstore']); |
|
175 | - return []; |
|
176 | - } catch (\Exception $e) { |
|
177 | - $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO]); |
|
178 | - return []; |
|
179 | - } |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Get the currently Nextcloud version |
|
184 | - * @return string |
|
185 | - */ |
|
186 | - protected function getVersion() { |
|
187 | - if ($this->version === null) { |
|
188 | - $this->version = $this->config->getSystemValue('version', '0.0.0'); |
|
189 | - } |
|
190 | - return $this->version; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Set the current Nextcloud version |
|
195 | - * @param string $version |
|
196 | - */ |
|
197 | - public function setVersion(string $version) { |
|
198 | - $this->version = $version; |
|
199 | - } |
|
42 | + const INVALIDATE_AFTER_SECONDS = 300; |
|
43 | + |
|
44 | + /** @var IAppData */ |
|
45 | + protected $appData; |
|
46 | + /** @var IClientService */ |
|
47 | + protected $clientService; |
|
48 | + /** @var ITimeFactory */ |
|
49 | + protected $timeFactory; |
|
50 | + /** @var IConfig */ |
|
51 | + protected $config; |
|
52 | + /** @var Ilogger */ |
|
53 | + protected $logger; |
|
54 | + /** @var string */ |
|
55 | + protected $fileName; |
|
56 | + /** @var string */ |
|
57 | + protected $endpointUrl; |
|
58 | + /** @var string */ |
|
59 | + protected $version; |
|
60 | + |
|
61 | + /** |
|
62 | + * @param Factory $appDataFactory |
|
63 | + * @param IClientService $clientService |
|
64 | + * @param ITimeFactory $timeFactory |
|
65 | + * @param IConfig $config |
|
66 | + * @param ILogger $logger |
|
67 | + */ |
|
68 | + public function __construct(Factory $appDataFactory, |
|
69 | + IClientService $clientService, |
|
70 | + ITimeFactory $timeFactory, |
|
71 | + IConfig $config, |
|
72 | + ILogger $logger) { |
|
73 | + $this->appData = $appDataFactory->get('appstore'); |
|
74 | + $this->clientService = $clientService; |
|
75 | + $this->timeFactory = $timeFactory; |
|
76 | + $this->config = $config; |
|
77 | + $this->logger = $logger; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Fetches the response from the server |
|
82 | + * |
|
83 | + * @param string $ETag |
|
84 | + * @param string $content |
|
85 | + * |
|
86 | + * @return array |
|
87 | + */ |
|
88 | + protected function fetch($ETag, $content) { |
|
89 | + $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
90 | + |
|
91 | + if (!$appstoreenabled) { |
|
92 | + return []; |
|
93 | + } |
|
94 | + |
|
95 | + $options = [ |
|
96 | + 'timeout' => 10, |
|
97 | + ]; |
|
98 | + |
|
99 | + if ($ETag !== '') { |
|
100 | + $options['headers'] = [ |
|
101 | + 'If-None-Match' => $ETag, |
|
102 | + ]; |
|
103 | + } |
|
104 | + |
|
105 | + $client = $this->clientService->newClient(); |
|
106 | + $response = $client->get($this->endpointUrl, $options); |
|
107 | + |
|
108 | + $responseJson = []; |
|
109 | + if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { |
|
110 | + $responseJson['data'] = json_decode($content, true); |
|
111 | + } else { |
|
112 | + $responseJson['data'] = json_decode($response->getBody(), true); |
|
113 | + $ETag = $response->getHeader('ETag'); |
|
114 | + } |
|
115 | + |
|
116 | + $responseJson['timestamp'] = $this->timeFactory->getTime(); |
|
117 | + $responseJson['ncversion'] = $this->getVersion(); |
|
118 | + if ($ETag !== '') { |
|
119 | + $responseJson['ETag'] = $ETag; |
|
120 | + } |
|
121 | + |
|
122 | + return $responseJson; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Returns the array with the categories on the appstore server |
|
127 | + * |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + public function get() { |
|
131 | + $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
132 | + $internetavailable = $this->config->getSystemValue('has_internet_connection', true); |
|
133 | + |
|
134 | + if (!$appstoreenabled || !$internetavailable) { |
|
135 | + return []; |
|
136 | + } |
|
137 | + |
|
138 | + $rootFolder = $this->appData->getFolder('/'); |
|
139 | + |
|
140 | + $ETag = ''; |
|
141 | + $content = ''; |
|
142 | + |
|
143 | + try { |
|
144 | + // File does already exists |
|
145 | + $file = $rootFolder->getFile($this->fileName); |
|
146 | + $jsonBlob = json_decode($file->getContent(), true); |
|
147 | + if (is_array($jsonBlob)) { |
|
148 | + |
|
149 | + // No caching when the version has been updated |
|
150 | + if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { |
|
151 | + |
|
152 | + // If the timestamp is older than 300 seconds request the files new |
|
153 | + if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { |
|
154 | + return $jsonBlob['data']; |
|
155 | + } |
|
156 | + |
|
157 | + if (isset($jsonBlob['ETag'])) { |
|
158 | + $ETag = $jsonBlob['ETag']; |
|
159 | + $content = json_encode($jsonBlob['data']); |
|
160 | + } |
|
161 | + } |
|
162 | + } |
|
163 | + } catch (NotFoundException $e) { |
|
164 | + // File does not already exists |
|
165 | + $file = $rootFolder->newFile($this->fileName); |
|
166 | + } |
|
167 | + |
|
168 | + // Refresh the file content |
|
169 | + try { |
|
170 | + $responseJson = $this->fetch($ETag, $content); |
|
171 | + $file->putContent(json_encode($responseJson)); |
|
172 | + return json_decode($file->getContent(), true)['data']; |
|
173 | + } catch (ConnectException $e) { |
|
174 | + $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO, 'message' => 'Could not connect to appstore']); |
|
175 | + return []; |
|
176 | + } catch (\Exception $e) { |
|
177 | + $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO]); |
|
178 | + return []; |
|
179 | + } |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Get the currently Nextcloud version |
|
184 | + * @return string |
|
185 | + */ |
|
186 | + protected function getVersion() { |
|
187 | + if ($this->version === null) { |
|
188 | + $this->version = $this->config->getSystemValue('version', '0.0.0'); |
|
189 | + } |
|
190 | + return $this->version; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Set the current Nextcloud version |
|
195 | + * @param string $version |
|
196 | + */ |
|
197 | + public function setVersion(string $version) { |
|
198 | + $this->version = $version; |
|
199 | + } |
|
200 | 200 | } |
@@ -13,234 +13,234 @@ |
||
13 | 13 | |
14 | 14 | class PhotoCache { |
15 | 15 | |
16 | - /** @var IAppData $appData */ |
|
17 | - protected $appData; |
|
18 | - |
|
19 | - /** |
|
20 | - * PhotoCache constructor. |
|
21 | - * |
|
22 | - * @param IAppData $appData |
|
23 | - */ |
|
24 | - public function __construct(IAppData $appData) { |
|
25 | - $this->appData = $appData; |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * @param int $addressBookId |
|
30 | - * @param string $cardUri |
|
31 | - * @param int $size |
|
32 | - * @param Card $card |
|
33 | - * |
|
34 | - * @return ISimpleFile |
|
35 | - * @throws NotFoundException |
|
36 | - */ |
|
37 | - public function get($addressBookId, $cardUri, $size, Card $card) { |
|
38 | - $folder = $this->getFolder($addressBookId, $cardUri); |
|
39 | - |
|
40 | - if ($this->isEmpty($folder)) { |
|
41 | - $this->init($folder, $card); |
|
42 | - } |
|
43 | - |
|
44 | - if (!$this->hasPhoto($folder)) { |
|
45 | - throw new NotFoundException(); |
|
46 | - } |
|
47 | - |
|
48 | - if ($size !== -1) { |
|
49 | - $size = 2 ** ceil(log($size) / log(2)); |
|
50 | - } |
|
51 | - |
|
52 | - return $this->getFile($folder, $size); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @param ISimpleFolder $folder |
|
57 | - * @return bool |
|
58 | - */ |
|
59 | - private function isEmpty(ISimpleFolder $folder) { |
|
60 | - return $folder->getDirectoryListing() === []; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @param ISimpleFolder $folder |
|
65 | - * @param Card $card |
|
66 | - */ |
|
67 | - private function init(ISimpleFolder $folder, Card $card) { |
|
68 | - $data = $this->getPhoto($card); |
|
69 | - |
|
70 | - if ($data === false) { |
|
71 | - $folder->newFile('nophoto'); |
|
72 | - } else { |
|
73 | - switch ($data['Content-Type']) { |
|
74 | - case 'image/png': |
|
75 | - $ext = 'png'; |
|
76 | - break; |
|
77 | - case 'image/jpeg': |
|
78 | - $ext = 'jpg'; |
|
79 | - break; |
|
80 | - case 'image/gif': |
|
81 | - $ext = 'gif'; |
|
82 | - break; |
|
83 | - } |
|
84 | - $file = $folder->newFile('photo.' . $ext); |
|
85 | - $file->putContent($data['body']); |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - private function hasPhoto(ISimpleFolder $folder) { |
|
90 | - return !$folder->fileExists('nophoto'); |
|
91 | - } |
|
92 | - |
|
93 | - private function getFile(ISimpleFolder $folder, $size) { |
|
94 | - $ext = $this->getExtension($folder); |
|
95 | - |
|
96 | - if ($size === -1) { |
|
97 | - $path = 'photo.' . $ext; |
|
98 | - } else { |
|
99 | - $path = 'photo.' . $size . '.' . $ext; |
|
100 | - } |
|
101 | - |
|
102 | - try { |
|
103 | - $file = $folder->getFile($path); |
|
104 | - } catch (NotFoundException $e) { |
|
105 | - if ($size <= 0) { |
|
106 | - throw new NotFoundException; |
|
107 | - } |
|
108 | - |
|
109 | - $photo = new \OC_Image(); |
|
110 | - /** @var ISimpleFile $file */ |
|
111 | - $file = $folder->getFile('photo.' . $ext); |
|
112 | - $photo->loadFromData($file->getContent()); |
|
113 | - |
|
114 | - $ratio = $photo->width() / $photo->height(); |
|
115 | - if ($ratio < 1) { |
|
116 | - $ratio = 1/$ratio; |
|
117 | - } |
|
118 | - $size = (int)($size * $ratio); |
|
119 | - |
|
120 | - if ($size !== -1) { |
|
121 | - $photo->resize($size); |
|
122 | - } |
|
123 | - try { |
|
124 | - $file = $folder->newFile($path); |
|
125 | - $file->putContent($photo->data()); |
|
126 | - } catch (NotPermittedException $e) { |
|
127 | - |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - return $file; |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * @param int $addressBookId |
|
137 | - * @param string $cardUri |
|
138 | - * @return ISimpleFolder |
|
139 | - */ |
|
140 | - private function getFolder($addressBookId, $cardUri) { |
|
141 | - $hash = md5($addressBookId . ' ' . $cardUri); |
|
142 | - try { |
|
143 | - return $this->appData->getFolder($hash); |
|
144 | - } catch (NotFoundException $e) { |
|
145 | - return $this->appData->newFolder($hash); |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Get the extension of the avatar. If there is no avatar throw Exception |
|
151 | - * |
|
152 | - * @param ISimpleFolder $folder |
|
153 | - * @return string |
|
154 | - * @throws NotFoundException |
|
155 | - */ |
|
156 | - private function getExtension(ISimpleFolder $folder) { |
|
157 | - if ($folder->fileExists('photo.jpg')) { |
|
158 | - return 'jpg'; |
|
159 | - } elseif ($folder->fileExists('photo.png')) { |
|
160 | - return 'png'; |
|
161 | - } elseif ($folder->fileExists('photo.gif')) { |
|
162 | - return 'gif'; |
|
163 | - } |
|
164 | - throw new NotFoundException; |
|
165 | - } |
|
166 | - |
|
167 | - private function getPhoto(Card $node) { |
|
168 | - try { |
|
169 | - $vObject = $this->readCard($node->get()); |
|
170 | - if (!$vObject->PHOTO) { |
|
171 | - return false; |
|
172 | - } |
|
173 | - |
|
174 | - $photo = $vObject->PHOTO; |
|
175 | - $type = $this->getType($photo); |
|
176 | - |
|
177 | - $val = $photo->getValue(); |
|
178 | - if ($photo->getValueType() === 'URI') { |
|
179 | - $parsed = \Sabre\URI\parse($val); |
|
180 | - //only allow data:// |
|
181 | - if ($parsed['scheme'] !== 'data') { |
|
182 | - return false; |
|
183 | - } |
|
184 | - if (substr_count($parsed['path'], ';') === 1) { |
|
185 | - list($type,) = explode(';', $parsed['path']); |
|
186 | - } |
|
187 | - $val = file_get_contents($val); |
|
188 | - } |
|
189 | - |
|
190 | - $allowedContentTypes = [ |
|
191 | - 'image/png', |
|
192 | - 'image/jpeg', |
|
193 | - 'image/gif', |
|
194 | - ]; |
|
195 | - |
|
196 | - if(!in_array($type, $allowedContentTypes, true)) { |
|
197 | - $type = 'application/octet-stream'; |
|
198 | - } |
|
199 | - |
|
200 | - return [ |
|
201 | - 'Content-Type' => $type, |
|
202 | - 'body' => $val |
|
203 | - ]; |
|
204 | - } catch(\Exception $ex) { |
|
205 | - |
|
206 | - } |
|
207 | - return false; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * @param string $cardData |
|
212 | - * @return \Sabre\VObject\Document |
|
213 | - */ |
|
214 | - private function readCard($cardData) { |
|
215 | - return Reader::read($cardData); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * @param Binary $photo |
|
220 | - * @return string |
|
221 | - */ |
|
222 | - private function getType(Binary $photo) { |
|
223 | - $params = $photo->parameters(); |
|
224 | - if (isset($params['TYPE']) || isset($params['MEDIATYPE'])) { |
|
225 | - /** @var Parameter $typeParam */ |
|
226 | - $typeParam = isset($params['TYPE']) ? $params['TYPE'] : $params['MEDIATYPE']; |
|
227 | - $type = $typeParam->getValue(); |
|
228 | - |
|
229 | - if (strpos($type, 'image/') === 0) { |
|
230 | - return $type; |
|
231 | - } else { |
|
232 | - return 'image/' . strtolower($type); |
|
233 | - } |
|
234 | - } |
|
235 | - return ''; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * @param int $addressBookId |
|
240 | - * @param string $cardUri |
|
241 | - */ |
|
242 | - public function delete($addressBookId, $cardUri) { |
|
243 | - $folder = $this->getFolder($addressBookId, $cardUri); |
|
244 | - $folder->delete(); |
|
245 | - } |
|
16 | + /** @var IAppData $appData */ |
|
17 | + protected $appData; |
|
18 | + |
|
19 | + /** |
|
20 | + * PhotoCache constructor. |
|
21 | + * |
|
22 | + * @param IAppData $appData |
|
23 | + */ |
|
24 | + public function __construct(IAppData $appData) { |
|
25 | + $this->appData = $appData; |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * @param int $addressBookId |
|
30 | + * @param string $cardUri |
|
31 | + * @param int $size |
|
32 | + * @param Card $card |
|
33 | + * |
|
34 | + * @return ISimpleFile |
|
35 | + * @throws NotFoundException |
|
36 | + */ |
|
37 | + public function get($addressBookId, $cardUri, $size, Card $card) { |
|
38 | + $folder = $this->getFolder($addressBookId, $cardUri); |
|
39 | + |
|
40 | + if ($this->isEmpty($folder)) { |
|
41 | + $this->init($folder, $card); |
|
42 | + } |
|
43 | + |
|
44 | + if (!$this->hasPhoto($folder)) { |
|
45 | + throw new NotFoundException(); |
|
46 | + } |
|
47 | + |
|
48 | + if ($size !== -1) { |
|
49 | + $size = 2 ** ceil(log($size) / log(2)); |
|
50 | + } |
|
51 | + |
|
52 | + return $this->getFile($folder, $size); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @param ISimpleFolder $folder |
|
57 | + * @return bool |
|
58 | + */ |
|
59 | + private function isEmpty(ISimpleFolder $folder) { |
|
60 | + return $folder->getDirectoryListing() === []; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @param ISimpleFolder $folder |
|
65 | + * @param Card $card |
|
66 | + */ |
|
67 | + private function init(ISimpleFolder $folder, Card $card) { |
|
68 | + $data = $this->getPhoto($card); |
|
69 | + |
|
70 | + if ($data === false) { |
|
71 | + $folder->newFile('nophoto'); |
|
72 | + } else { |
|
73 | + switch ($data['Content-Type']) { |
|
74 | + case 'image/png': |
|
75 | + $ext = 'png'; |
|
76 | + break; |
|
77 | + case 'image/jpeg': |
|
78 | + $ext = 'jpg'; |
|
79 | + break; |
|
80 | + case 'image/gif': |
|
81 | + $ext = 'gif'; |
|
82 | + break; |
|
83 | + } |
|
84 | + $file = $folder->newFile('photo.' . $ext); |
|
85 | + $file->putContent($data['body']); |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + private function hasPhoto(ISimpleFolder $folder) { |
|
90 | + return !$folder->fileExists('nophoto'); |
|
91 | + } |
|
92 | + |
|
93 | + private function getFile(ISimpleFolder $folder, $size) { |
|
94 | + $ext = $this->getExtension($folder); |
|
95 | + |
|
96 | + if ($size === -1) { |
|
97 | + $path = 'photo.' . $ext; |
|
98 | + } else { |
|
99 | + $path = 'photo.' . $size . '.' . $ext; |
|
100 | + } |
|
101 | + |
|
102 | + try { |
|
103 | + $file = $folder->getFile($path); |
|
104 | + } catch (NotFoundException $e) { |
|
105 | + if ($size <= 0) { |
|
106 | + throw new NotFoundException; |
|
107 | + } |
|
108 | + |
|
109 | + $photo = new \OC_Image(); |
|
110 | + /** @var ISimpleFile $file */ |
|
111 | + $file = $folder->getFile('photo.' . $ext); |
|
112 | + $photo->loadFromData($file->getContent()); |
|
113 | + |
|
114 | + $ratio = $photo->width() / $photo->height(); |
|
115 | + if ($ratio < 1) { |
|
116 | + $ratio = 1/$ratio; |
|
117 | + } |
|
118 | + $size = (int)($size * $ratio); |
|
119 | + |
|
120 | + if ($size !== -1) { |
|
121 | + $photo->resize($size); |
|
122 | + } |
|
123 | + try { |
|
124 | + $file = $folder->newFile($path); |
|
125 | + $file->putContent($photo->data()); |
|
126 | + } catch (NotPermittedException $e) { |
|
127 | + |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + return $file; |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * @param int $addressBookId |
|
137 | + * @param string $cardUri |
|
138 | + * @return ISimpleFolder |
|
139 | + */ |
|
140 | + private function getFolder($addressBookId, $cardUri) { |
|
141 | + $hash = md5($addressBookId . ' ' . $cardUri); |
|
142 | + try { |
|
143 | + return $this->appData->getFolder($hash); |
|
144 | + } catch (NotFoundException $e) { |
|
145 | + return $this->appData->newFolder($hash); |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Get the extension of the avatar. If there is no avatar throw Exception |
|
151 | + * |
|
152 | + * @param ISimpleFolder $folder |
|
153 | + * @return string |
|
154 | + * @throws NotFoundException |
|
155 | + */ |
|
156 | + private function getExtension(ISimpleFolder $folder) { |
|
157 | + if ($folder->fileExists('photo.jpg')) { |
|
158 | + return 'jpg'; |
|
159 | + } elseif ($folder->fileExists('photo.png')) { |
|
160 | + return 'png'; |
|
161 | + } elseif ($folder->fileExists('photo.gif')) { |
|
162 | + return 'gif'; |
|
163 | + } |
|
164 | + throw new NotFoundException; |
|
165 | + } |
|
166 | + |
|
167 | + private function getPhoto(Card $node) { |
|
168 | + try { |
|
169 | + $vObject = $this->readCard($node->get()); |
|
170 | + if (!$vObject->PHOTO) { |
|
171 | + return false; |
|
172 | + } |
|
173 | + |
|
174 | + $photo = $vObject->PHOTO; |
|
175 | + $type = $this->getType($photo); |
|
176 | + |
|
177 | + $val = $photo->getValue(); |
|
178 | + if ($photo->getValueType() === 'URI') { |
|
179 | + $parsed = \Sabre\URI\parse($val); |
|
180 | + //only allow data:// |
|
181 | + if ($parsed['scheme'] !== 'data') { |
|
182 | + return false; |
|
183 | + } |
|
184 | + if (substr_count($parsed['path'], ';') === 1) { |
|
185 | + list($type,) = explode(';', $parsed['path']); |
|
186 | + } |
|
187 | + $val = file_get_contents($val); |
|
188 | + } |
|
189 | + |
|
190 | + $allowedContentTypes = [ |
|
191 | + 'image/png', |
|
192 | + 'image/jpeg', |
|
193 | + 'image/gif', |
|
194 | + ]; |
|
195 | + |
|
196 | + if(!in_array($type, $allowedContentTypes, true)) { |
|
197 | + $type = 'application/octet-stream'; |
|
198 | + } |
|
199 | + |
|
200 | + return [ |
|
201 | + 'Content-Type' => $type, |
|
202 | + 'body' => $val |
|
203 | + ]; |
|
204 | + } catch(\Exception $ex) { |
|
205 | + |
|
206 | + } |
|
207 | + return false; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * @param string $cardData |
|
212 | + * @return \Sabre\VObject\Document |
|
213 | + */ |
|
214 | + private function readCard($cardData) { |
|
215 | + return Reader::read($cardData); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * @param Binary $photo |
|
220 | + * @return string |
|
221 | + */ |
|
222 | + private function getType(Binary $photo) { |
|
223 | + $params = $photo->parameters(); |
|
224 | + if (isset($params['TYPE']) || isset($params['MEDIATYPE'])) { |
|
225 | + /** @var Parameter $typeParam */ |
|
226 | + $typeParam = isset($params['TYPE']) ? $params['TYPE'] : $params['MEDIATYPE']; |
|
227 | + $type = $typeParam->getValue(); |
|
228 | + |
|
229 | + if (strpos($type, 'image/') === 0) { |
|
230 | + return $type; |
|
231 | + } else { |
|
232 | + return 'image/' . strtolower($type); |
|
233 | + } |
|
234 | + } |
|
235 | + return ''; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * @param int $addressBookId |
|
240 | + * @param string $cardUri |
|
241 | + */ |
|
242 | + public function delete($addressBookId, $cardUri) { |
|
243 | + $folder = $this->getFolder($addressBookId, $cardUri); |
|
244 | + $folder->delete(); |
|
245 | + } |
|
246 | 246 | } |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | $ext = 'gif'; |
82 | 82 | break; |
83 | 83 | } |
84 | - $file = $folder->newFile('photo.' . $ext); |
|
84 | + $file = $folder->newFile('photo.'.$ext); |
|
85 | 85 | $file->putContent($data['body']); |
86 | 86 | } |
87 | 87 | } |
@@ -94,9 +94,9 @@ discard block |
||
94 | 94 | $ext = $this->getExtension($folder); |
95 | 95 | |
96 | 96 | if ($size === -1) { |
97 | - $path = 'photo.' . $ext; |
|
97 | + $path = 'photo.'.$ext; |
|
98 | 98 | } else { |
99 | - $path = 'photo.' . $size . '.' . $ext; |
|
99 | + $path = 'photo.'.$size.'.'.$ext; |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | try { |
@@ -108,14 +108,14 @@ discard block |
||
108 | 108 | |
109 | 109 | $photo = new \OC_Image(); |
110 | 110 | /** @var ISimpleFile $file */ |
111 | - $file = $folder->getFile('photo.' . $ext); |
|
111 | + $file = $folder->getFile('photo.'.$ext); |
|
112 | 112 | $photo->loadFromData($file->getContent()); |
113 | 113 | |
114 | 114 | $ratio = $photo->width() / $photo->height(); |
115 | 115 | if ($ratio < 1) { |
116 | - $ratio = 1/$ratio; |
|
116 | + $ratio = 1 / $ratio; |
|
117 | 117 | } |
118 | - $size = (int)($size * $ratio); |
|
118 | + $size = (int) ($size * $ratio); |
|
119 | 119 | |
120 | 120 | if ($size !== -1) { |
121 | 121 | $photo->resize($size); |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | * @return ISimpleFolder |
139 | 139 | */ |
140 | 140 | private function getFolder($addressBookId, $cardUri) { |
141 | - $hash = md5($addressBookId . ' ' . $cardUri); |
|
141 | + $hash = md5($addressBookId.' '.$cardUri); |
|
142 | 142 | try { |
143 | 143 | return $this->appData->getFolder($hash); |
144 | 144 | } catch (NotFoundException $e) { |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | 'image/gif', |
194 | 194 | ]; |
195 | 195 | |
196 | - if(!in_array($type, $allowedContentTypes, true)) { |
|
196 | + if (!in_array($type, $allowedContentTypes, true)) { |
|
197 | 197 | $type = 'application/octet-stream'; |
198 | 198 | } |
199 | 199 | |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | 'Content-Type' => $type, |
202 | 202 | 'body' => $val |
203 | 203 | ]; |
204 | - } catch(\Exception $ex) { |
|
204 | + } catch (\Exception $ex) { |
|
205 | 205 | |
206 | 206 | } |
207 | 207 | return false; |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | if (strpos($type, 'image/') === 0) { |
230 | 230 | return $type; |
231 | 231 | } else { |
232 | - return 'image/' . strtolower($type); |
|
232 | + return 'image/'.strtolower($type); |
|
233 | 233 | } |
234 | 234 | } |
235 | 235 | return ''; |
@@ -32,85 +32,85 @@ |
||
32 | 32 | |
33 | 33 | class ImageExportPlugin extends ServerPlugin { |
34 | 34 | |
35 | - /** @var Server */ |
|
36 | - protected $server; |
|
37 | - /** @var PhotoCache */ |
|
38 | - private $cache; |
|
39 | - |
|
40 | - /** |
|
41 | - * ImageExportPlugin constructor. |
|
42 | - * |
|
43 | - * @param PhotoCache $cache |
|
44 | - */ |
|
45 | - public function __construct(PhotoCache $cache) { |
|
46 | - $this->cache = $cache; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Initializes the plugin and registers event handlers |
|
51 | - * |
|
52 | - * @param Server $server |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function initialize(Server $server) { |
|
56 | - $this->server = $server; |
|
57 | - $this->server->on('method:GET', [$this, 'httpGet'], 90); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Intercepts GET requests on addressbook urls ending with ?photo. |
|
62 | - * |
|
63 | - * @param RequestInterface $request |
|
64 | - * @param ResponseInterface $response |
|
65 | - * @return bool |
|
66 | - */ |
|
67 | - public function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
68 | - |
|
69 | - $queryParams = $request->getQueryParameters(); |
|
70 | - // TODO: in addition to photo we should also add logo some point in time |
|
71 | - if (!array_key_exists('photo', $queryParams)) { |
|
72 | - return true; |
|
73 | - } |
|
74 | - |
|
75 | - $size = isset($queryParams['size']) ? (int)$queryParams['size'] : -1; |
|
76 | - |
|
77 | - $path = $request->getPath(); |
|
78 | - $node = $this->server->tree->getNodeForPath($path); |
|
79 | - |
|
80 | - if (!($node instanceof Card)) { |
|
81 | - return true; |
|
82 | - } |
|
83 | - |
|
84 | - $this->server->transactionType = 'carddav-image-export'; |
|
85 | - |
|
86 | - // Checking ACL, if available. |
|
87 | - if ($aclPlugin = $this->server->getPlugin('acl')) { |
|
88 | - /** @var \Sabre\DAVACL\Plugin $aclPlugin */ |
|
89 | - $aclPlugin->checkPrivileges($path, '{DAV:}read'); |
|
90 | - } |
|
91 | - |
|
92 | - // Fetch addressbook |
|
93 | - $addressbookpath = explode('/', $path); |
|
94 | - array_pop($addressbookpath); |
|
95 | - $addressbookpath = implode('/', $addressbookpath); |
|
96 | - /** @var AddressBook $addressbook */ |
|
97 | - $addressbook = $this->server->tree->getNodeForPath($addressbookpath); |
|
98 | - |
|
99 | - $response->setHeader('Cache-Control', 'private, max-age=3600, must-revalidate'); |
|
100 | - $response->setHeader('Etag', $node->getETag() ); |
|
101 | - $response->setHeader('Pragma', 'public'); |
|
102 | - |
|
103 | - try { |
|
104 | - $file = $this->cache->get($addressbook->getResourceId(), $node->getName(), $size, $node); |
|
105 | - $response->setHeader('Content-Type', $file->getMimeType()); |
|
106 | - $response->setHeader('Content-Disposition', 'attachment'); |
|
107 | - $response->setStatus(200); |
|
108 | - |
|
109 | - $response->setBody($file->getContent()); |
|
110 | - } catch (NotFoundException $e) { |
|
111 | - $response->setStatus(404); |
|
112 | - } |
|
113 | - |
|
114 | - return false; |
|
115 | - } |
|
35 | + /** @var Server */ |
|
36 | + protected $server; |
|
37 | + /** @var PhotoCache */ |
|
38 | + private $cache; |
|
39 | + |
|
40 | + /** |
|
41 | + * ImageExportPlugin constructor. |
|
42 | + * |
|
43 | + * @param PhotoCache $cache |
|
44 | + */ |
|
45 | + public function __construct(PhotoCache $cache) { |
|
46 | + $this->cache = $cache; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Initializes the plugin and registers event handlers |
|
51 | + * |
|
52 | + * @param Server $server |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function initialize(Server $server) { |
|
56 | + $this->server = $server; |
|
57 | + $this->server->on('method:GET', [$this, 'httpGet'], 90); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Intercepts GET requests on addressbook urls ending with ?photo. |
|
62 | + * |
|
63 | + * @param RequestInterface $request |
|
64 | + * @param ResponseInterface $response |
|
65 | + * @return bool |
|
66 | + */ |
|
67 | + public function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
68 | + |
|
69 | + $queryParams = $request->getQueryParameters(); |
|
70 | + // TODO: in addition to photo we should also add logo some point in time |
|
71 | + if (!array_key_exists('photo', $queryParams)) { |
|
72 | + return true; |
|
73 | + } |
|
74 | + |
|
75 | + $size = isset($queryParams['size']) ? (int)$queryParams['size'] : -1; |
|
76 | + |
|
77 | + $path = $request->getPath(); |
|
78 | + $node = $this->server->tree->getNodeForPath($path); |
|
79 | + |
|
80 | + if (!($node instanceof Card)) { |
|
81 | + return true; |
|
82 | + } |
|
83 | + |
|
84 | + $this->server->transactionType = 'carddav-image-export'; |
|
85 | + |
|
86 | + // Checking ACL, if available. |
|
87 | + if ($aclPlugin = $this->server->getPlugin('acl')) { |
|
88 | + /** @var \Sabre\DAVACL\Plugin $aclPlugin */ |
|
89 | + $aclPlugin->checkPrivileges($path, '{DAV:}read'); |
|
90 | + } |
|
91 | + |
|
92 | + // Fetch addressbook |
|
93 | + $addressbookpath = explode('/', $path); |
|
94 | + array_pop($addressbookpath); |
|
95 | + $addressbookpath = implode('/', $addressbookpath); |
|
96 | + /** @var AddressBook $addressbook */ |
|
97 | + $addressbook = $this->server->tree->getNodeForPath($addressbookpath); |
|
98 | + |
|
99 | + $response->setHeader('Cache-Control', 'private, max-age=3600, must-revalidate'); |
|
100 | + $response->setHeader('Etag', $node->getETag() ); |
|
101 | + $response->setHeader('Pragma', 'public'); |
|
102 | + |
|
103 | + try { |
|
104 | + $file = $this->cache->get($addressbook->getResourceId(), $node->getName(), $size, $node); |
|
105 | + $response->setHeader('Content-Type', $file->getMimeType()); |
|
106 | + $response->setHeader('Content-Disposition', 'attachment'); |
|
107 | + $response->setStatus(200); |
|
108 | + |
|
109 | + $response->setBody($file->getContent()); |
|
110 | + } catch (NotFoundException $e) { |
|
111 | + $response->setStatus(404); |
|
112 | + } |
|
113 | + |
|
114 | + return false; |
|
115 | + } |
|
116 | 116 | } |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | return true; |
73 | 73 | } |
74 | 74 | |
75 | - $size = isset($queryParams['size']) ? (int)$queryParams['size'] : -1; |
|
75 | + $size = isset($queryParams['size']) ? (int) $queryParams['size'] : -1; |
|
76 | 76 | |
77 | 77 | $path = $request->getPath(); |
78 | 78 | $node = $this->server->tree->getNodeForPath($path); |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | $addressbook = $this->server->tree->getNodeForPath($addressbookpath); |
98 | 98 | |
99 | 99 | $response->setHeader('Cache-Control', 'private, max-age=3600, must-revalidate'); |
100 | - $response->setHeader('Etag', $node->getETag() ); |
|
100 | + $response->setHeader('Etag', $node->getETag()); |
|
101 | 101 | $response->setHeader('Pragma', 'public'); |
102 | 102 | |
103 | 103 | try { |
@@ -41,7 +41,7 @@ |
||
41 | 41 | <?php p($theme->getName()); ?> |
42 | 42 | </h1> |
43 | 43 | <?php if(\OC::$server->getConfig()->getSystemValue('installed', false) |
44 | - && \OC::$server->getConfig()->getAppValue('theming', 'logoMime', false)): ?> |
|
44 | + && \OC::$server->getConfig()->getAppValue('theming', 'logoMime', false)): ?> |
|
45 | 45 | <img src="<?php p($theme->getLogo()); ?>"/> |
46 | 46 | <?php endif; ?> |
47 | 47 | </div> |
@@ -18,18 +18,18 @@ |
||
18 | 18 | <?php emit_script_loading_tags($_); ?> |
19 | 19 | <?php print_unescaped($_['headers']); ?> |
20 | 20 | </head> |
21 | - <body id="<?php p($_['bodyid']);?>"> |
|
21 | + <body id="<?php p($_['bodyid']); ?>"> |
|
22 | 22 | <?php include 'layout.noscript.warning.php'; ?> |
23 | 23 | <div class="wrapper"> |
24 | 24 | <div class="v-align"> |
25 | - <?php if ($_['bodyid'] === 'body-login' ): ?> |
|
25 | + <?php if ($_['bodyid'] === 'body-login'): ?> |
|
26 | 26 | <header role="banner"> |
27 | 27 | <div id="header"> |
28 | 28 | <div class="logo"> |
29 | 29 | <h1 class="hidden-visually"> |
30 | 30 | <?php p($theme->getName()); ?> |
31 | 31 | </h1> |
32 | - <?php if(\OC::$server->getConfig()->getSystemValue('installed', false) |
|
32 | + <?php if (\OC::$server->getConfig()->getSystemValue('installed', false) |
|
33 | 33 | && \OC::$server->getConfig()->getAppValue('theming', 'logoMime', false)): ?> |
34 | 34 | <img src="<?php p($theme->getLogo()); ?>"/> |
35 | 35 | <?php endif; ?> |
@@ -40,235 +40,235 @@ |
||
40 | 40 | */ |
41 | 41 | class Config { |
42 | 42 | |
43 | - const ENV_PREFIX = 'NC_'; |
|
43 | + const ENV_PREFIX = 'NC_'; |
|
44 | 44 | |
45 | - /** @var array Associative array ($key => $value) */ |
|
46 | - protected $cache = array(); |
|
47 | - /** @var string */ |
|
48 | - protected $configDir; |
|
49 | - /** @var string */ |
|
50 | - protected $configFilePath; |
|
51 | - /** @var string */ |
|
52 | - protected $configFileName; |
|
45 | + /** @var array Associative array ($key => $value) */ |
|
46 | + protected $cache = array(); |
|
47 | + /** @var string */ |
|
48 | + protected $configDir; |
|
49 | + /** @var string */ |
|
50 | + protected $configFilePath; |
|
51 | + /** @var string */ |
|
52 | + protected $configFileName; |
|
53 | 53 | |
54 | - /** |
|
55 | - * @param string $configDir Path to the config dir, needs to end with '/' |
|
56 | - * @param string $fileName (Optional) Name of the config file. Defaults to config.php |
|
57 | - */ |
|
58 | - public function __construct($configDir, $fileName = 'config.php') { |
|
59 | - $this->configDir = $configDir; |
|
60 | - $this->configFilePath = $this->configDir.$fileName; |
|
61 | - $this->configFileName = $fileName; |
|
62 | - $this->readData(); |
|
63 | - } |
|
54 | + /** |
|
55 | + * @param string $configDir Path to the config dir, needs to end with '/' |
|
56 | + * @param string $fileName (Optional) Name of the config file. Defaults to config.php |
|
57 | + */ |
|
58 | + public function __construct($configDir, $fileName = 'config.php') { |
|
59 | + $this->configDir = $configDir; |
|
60 | + $this->configFilePath = $this->configDir.$fileName; |
|
61 | + $this->configFileName = $fileName; |
|
62 | + $this->readData(); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Lists all available config keys |
|
67 | - * |
|
68 | - * Please note that it does not return the values. |
|
69 | - * |
|
70 | - * @return array an array of key names |
|
71 | - */ |
|
72 | - public function getKeys() { |
|
73 | - return array_keys($this->cache); |
|
74 | - } |
|
65 | + /** |
|
66 | + * Lists all available config keys |
|
67 | + * |
|
68 | + * Please note that it does not return the values. |
|
69 | + * |
|
70 | + * @return array an array of key names |
|
71 | + */ |
|
72 | + public function getKeys() { |
|
73 | + return array_keys($this->cache); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Returns a config value |
|
78 | - * |
|
79 | - * gets its value from an `NC_` prefixed environment variable |
|
80 | - * if it doesn't exist from config.php |
|
81 | - * if this doesn't exist either, it will return the given `$default` |
|
82 | - * |
|
83 | - * @param string $key key |
|
84 | - * @param mixed $default = null default value |
|
85 | - * @return mixed the value or $default |
|
86 | - */ |
|
87 | - public function getValue($key, $default = null) { |
|
88 | - $envValue = getenv(self::ENV_PREFIX . $key); |
|
89 | - if ($envValue !== false) { |
|
90 | - return $envValue; |
|
91 | - } |
|
76 | + /** |
|
77 | + * Returns a config value |
|
78 | + * |
|
79 | + * gets its value from an `NC_` prefixed environment variable |
|
80 | + * if it doesn't exist from config.php |
|
81 | + * if this doesn't exist either, it will return the given `$default` |
|
82 | + * |
|
83 | + * @param string $key key |
|
84 | + * @param mixed $default = null default value |
|
85 | + * @return mixed the value or $default |
|
86 | + */ |
|
87 | + public function getValue($key, $default = null) { |
|
88 | + $envValue = getenv(self::ENV_PREFIX . $key); |
|
89 | + if ($envValue !== false) { |
|
90 | + return $envValue; |
|
91 | + } |
|
92 | 92 | |
93 | - if (isset($this->cache[$key])) { |
|
94 | - return $this->cache[$key]; |
|
95 | - } |
|
93 | + if (isset($this->cache[$key])) { |
|
94 | + return $this->cache[$key]; |
|
95 | + } |
|
96 | 96 | |
97 | - return $default; |
|
98 | - } |
|
97 | + return $default; |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * Sets and deletes values and writes the config.php |
|
102 | - * |
|
103 | - * @param array $configs Associative array with `key => value` pairs |
|
104 | - * If value is null, the config key will be deleted |
|
105 | - */ |
|
106 | - public function setValues(array $configs) { |
|
107 | - $needsUpdate = false; |
|
108 | - foreach ($configs as $key => $value) { |
|
109 | - if ($value !== null) { |
|
110 | - $needsUpdate |= $this->set($key, $value); |
|
111 | - } else { |
|
112 | - $needsUpdate |= $this->delete($key); |
|
113 | - } |
|
114 | - } |
|
100 | + /** |
|
101 | + * Sets and deletes values and writes the config.php |
|
102 | + * |
|
103 | + * @param array $configs Associative array with `key => value` pairs |
|
104 | + * If value is null, the config key will be deleted |
|
105 | + */ |
|
106 | + public function setValues(array $configs) { |
|
107 | + $needsUpdate = false; |
|
108 | + foreach ($configs as $key => $value) { |
|
109 | + if ($value !== null) { |
|
110 | + $needsUpdate |= $this->set($key, $value); |
|
111 | + } else { |
|
112 | + $needsUpdate |= $this->delete($key); |
|
113 | + } |
|
114 | + } |
|
115 | 115 | |
116 | - if ($needsUpdate) { |
|
117 | - // Write changes |
|
118 | - $this->writeData(); |
|
119 | - } |
|
120 | - } |
|
116 | + if ($needsUpdate) { |
|
117 | + // Write changes |
|
118 | + $this->writeData(); |
|
119 | + } |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Sets the value and writes it to config.php if required |
|
124 | - * |
|
125 | - * @param string $key key |
|
126 | - * @param mixed $value value |
|
127 | - */ |
|
128 | - public function setValue($key, $value) { |
|
129 | - if ($this->set($key, $value)) { |
|
130 | - // Write changes |
|
131 | - $this->writeData(); |
|
132 | - } |
|
133 | - } |
|
122 | + /** |
|
123 | + * Sets the value and writes it to config.php if required |
|
124 | + * |
|
125 | + * @param string $key key |
|
126 | + * @param mixed $value value |
|
127 | + */ |
|
128 | + public function setValue($key, $value) { |
|
129 | + if ($this->set($key, $value)) { |
|
130 | + // Write changes |
|
131 | + $this->writeData(); |
|
132 | + } |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * This function sets the value |
|
137 | - * |
|
138 | - * @param string $key key |
|
139 | - * @param mixed $value value |
|
140 | - * @return bool True if the file needs to be updated, false otherwise |
|
141 | - */ |
|
142 | - protected function set($key, $value) { |
|
143 | - if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) { |
|
144 | - // Add change |
|
145 | - $this->cache[$key] = $value; |
|
146 | - return true; |
|
147 | - } |
|
135 | + /** |
|
136 | + * This function sets the value |
|
137 | + * |
|
138 | + * @param string $key key |
|
139 | + * @param mixed $value value |
|
140 | + * @return bool True if the file needs to be updated, false otherwise |
|
141 | + */ |
|
142 | + protected function set($key, $value) { |
|
143 | + if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) { |
|
144 | + // Add change |
|
145 | + $this->cache[$key] = $value; |
|
146 | + return true; |
|
147 | + } |
|
148 | 148 | |
149 | - return false; |
|
150 | - } |
|
149 | + return false; |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * Removes a key from the config and removes it from config.php if required |
|
154 | - * @param string $key |
|
155 | - */ |
|
156 | - public function deleteKey($key) { |
|
157 | - if ($this->delete($key)) { |
|
158 | - // Write changes |
|
159 | - $this->writeData(); |
|
160 | - } |
|
161 | - } |
|
152 | + /** |
|
153 | + * Removes a key from the config and removes it from config.php if required |
|
154 | + * @param string $key |
|
155 | + */ |
|
156 | + public function deleteKey($key) { |
|
157 | + if ($this->delete($key)) { |
|
158 | + // Write changes |
|
159 | + $this->writeData(); |
|
160 | + } |
|
161 | + } |
|
162 | 162 | |
163 | - /** |
|
164 | - * This function removes a key from the config |
|
165 | - * |
|
166 | - * @param string $key |
|
167 | - * @return bool True if the file needs to be updated, false otherwise |
|
168 | - */ |
|
169 | - protected function delete($key) { |
|
170 | - if (isset($this->cache[$key])) { |
|
171 | - // Delete key from cache |
|
172 | - unset($this->cache[$key]); |
|
173 | - return true; |
|
174 | - } |
|
175 | - return false; |
|
176 | - } |
|
163 | + /** |
|
164 | + * This function removes a key from the config |
|
165 | + * |
|
166 | + * @param string $key |
|
167 | + * @return bool True if the file needs to be updated, false otherwise |
|
168 | + */ |
|
169 | + protected function delete($key) { |
|
170 | + if (isset($this->cache[$key])) { |
|
171 | + // Delete key from cache |
|
172 | + unset($this->cache[$key]); |
|
173 | + return true; |
|
174 | + } |
|
175 | + return false; |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * Loads the config file |
|
180 | - * |
|
181 | - * Reads the config file and saves it to the cache |
|
182 | - * |
|
183 | - * @throws \Exception If no lock could be acquired or the config file has not been found |
|
184 | - */ |
|
185 | - private function readData() { |
|
186 | - // Default config should always get loaded |
|
187 | - $configFiles = array($this->configFilePath); |
|
178 | + /** |
|
179 | + * Loads the config file |
|
180 | + * |
|
181 | + * Reads the config file and saves it to the cache |
|
182 | + * |
|
183 | + * @throws \Exception If no lock could be acquired or the config file has not been found |
|
184 | + */ |
|
185 | + private function readData() { |
|
186 | + // Default config should always get loaded |
|
187 | + $configFiles = array($this->configFilePath); |
|
188 | 188 | |
189 | - // Add all files in the config dir ending with the same file name |
|
190 | - $extra = glob($this->configDir.'*.'.$this->configFileName); |
|
191 | - if (is_array($extra)) { |
|
192 | - natsort($extra); |
|
193 | - $configFiles = array_merge($configFiles, $extra); |
|
194 | - } |
|
189 | + // Add all files in the config dir ending with the same file name |
|
190 | + $extra = glob($this->configDir.'*.'.$this->configFileName); |
|
191 | + if (is_array($extra)) { |
|
192 | + natsort($extra); |
|
193 | + $configFiles = array_merge($configFiles, $extra); |
|
194 | + } |
|
195 | 195 | |
196 | - // Include file and merge config |
|
197 | - foreach ($configFiles as $file) { |
|
198 | - $fileExistsAndIsReadable = file_exists($file) && is_readable($file); |
|
199 | - $filePointer = $fileExistsAndIsReadable ? fopen($file, 'r') : false; |
|
200 | - if($file === $this->configFilePath && |
|
201 | - $filePointer === false) { |
|
202 | - // Opening the main config might not be possible, e.g. if the wrong |
|
203 | - // permissions are set (likely on a new installation) |
|
204 | - continue; |
|
205 | - } |
|
196 | + // Include file and merge config |
|
197 | + foreach ($configFiles as $file) { |
|
198 | + $fileExistsAndIsReadable = file_exists($file) && is_readable($file); |
|
199 | + $filePointer = $fileExistsAndIsReadable ? fopen($file, 'r') : false; |
|
200 | + if($file === $this->configFilePath && |
|
201 | + $filePointer === false) { |
|
202 | + // Opening the main config might not be possible, e.g. if the wrong |
|
203 | + // permissions are set (likely on a new installation) |
|
204 | + continue; |
|
205 | + } |
|
206 | 206 | |
207 | - // Try to acquire a file lock |
|
208 | - if(!flock($filePointer, LOCK_SH)) { |
|
209 | - throw new \Exception(sprintf('Could not acquire a shared lock on the config file %s', $file)); |
|
210 | - } |
|
207 | + // Try to acquire a file lock |
|
208 | + if(!flock($filePointer, LOCK_SH)) { |
|
209 | + throw new \Exception(sprintf('Could not acquire a shared lock on the config file %s', $file)); |
|
210 | + } |
|
211 | 211 | |
212 | - unset($CONFIG); |
|
213 | - include $file; |
|
214 | - if(isset($CONFIG) && is_array($CONFIG)) { |
|
215 | - $this->cache = array_merge($this->cache, $CONFIG); |
|
216 | - } |
|
212 | + unset($CONFIG); |
|
213 | + include $file; |
|
214 | + if(isset($CONFIG) && is_array($CONFIG)) { |
|
215 | + $this->cache = array_merge($this->cache, $CONFIG); |
|
216 | + } |
|
217 | 217 | |
218 | - // Close the file pointer and release the lock |
|
219 | - flock($filePointer, LOCK_UN); |
|
220 | - fclose($filePointer); |
|
221 | - } |
|
222 | - } |
|
218 | + // Close the file pointer and release the lock |
|
219 | + flock($filePointer, LOCK_UN); |
|
220 | + fclose($filePointer); |
|
221 | + } |
|
222 | + } |
|
223 | 223 | |
224 | - /** |
|
225 | - * Writes the config file |
|
226 | - * |
|
227 | - * Saves the config to the config file. |
|
228 | - * |
|
229 | - * @throws HintException If the config file cannot be written to |
|
230 | - * @throws \Exception If no file lock can be acquired |
|
231 | - */ |
|
232 | - private function writeData() { |
|
233 | - // Create a php file ... |
|
234 | - $content = "<?php\n"; |
|
235 | - $content .= '$CONFIG = '; |
|
236 | - $content .= var_export($this->cache, true); |
|
237 | - $content .= ";\n"; |
|
224 | + /** |
|
225 | + * Writes the config file |
|
226 | + * |
|
227 | + * Saves the config to the config file. |
|
228 | + * |
|
229 | + * @throws HintException If the config file cannot be written to |
|
230 | + * @throws \Exception If no file lock can be acquired |
|
231 | + */ |
|
232 | + private function writeData() { |
|
233 | + // Create a php file ... |
|
234 | + $content = "<?php\n"; |
|
235 | + $content .= '$CONFIG = '; |
|
236 | + $content .= var_export($this->cache, true); |
|
237 | + $content .= ";\n"; |
|
238 | 238 | |
239 | - touch ($this->configFilePath); |
|
240 | - $filePointer = fopen($this->configFilePath, 'r+'); |
|
239 | + touch ($this->configFilePath); |
|
240 | + $filePointer = fopen($this->configFilePath, 'r+'); |
|
241 | 241 | |
242 | - // Prevent others not to read the config |
|
243 | - chmod($this->configFilePath, 0640); |
|
242 | + // Prevent others not to read the config |
|
243 | + chmod($this->configFilePath, 0640); |
|
244 | 244 | |
245 | - // File does not exist, this can happen when doing a fresh install |
|
246 | - if(!is_resource ($filePointer)) { |
|
247 | - // TODO fix this via DI once it is very clear that this doesn't cause side effects due to initialization order |
|
248 | - // currently this breaks app routes but also could have other side effects especially during setup and exception handling |
|
249 | - $url = \OC::$server->getURLGenerator()->linkToDocs('admin-dir_permissions'); |
|
250 | - throw new HintException( |
|
251 | - "Can't write into config directory!", |
|
252 | - 'This can usually be fixed by giving the webserver write access to the config directory. See ' . $url); |
|
253 | - } |
|
245 | + // File does not exist, this can happen when doing a fresh install |
|
246 | + if(!is_resource ($filePointer)) { |
|
247 | + // TODO fix this via DI once it is very clear that this doesn't cause side effects due to initialization order |
|
248 | + // currently this breaks app routes but also could have other side effects especially during setup and exception handling |
|
249 | + $url = \OC::$server->getURLGenerator()->linkToDocs('admin-dir_permissions'); |
|
250 | + throw new HintException( |
|
251 | + "Can't write into config directory!", |
|
252 | + 'This can usually be fixed by giving the webserver write access to the config directory. See ' . $url); |
|
253 | + } |
|
254 | 254 | |
255 | - // Try to acquire a file lock |
|
256 | - if(!flock($filePointer, LOCK_EX)) { |
|
257 | - throw new \Exception(sprintf('Could not acquire an exclusive lock on the config file %s', $this->configFilePath)); |
|
258 | - } |
|
255 | + // Try to acquire a file lock |
|
256 | + if(!flock($filePointer, LOCK_EX)) { |
|
257 | + throw new \Exception(sprintf('Could not acquire an exclusive lock on the config file %s', $this->configFilePath)); |
|
258 | + } |
|
259 | 259 | |
260 | - // Write the config and release the lock |
|
261 | - ftruncate ($filePointer, 0); |
|
262 | - fwrite($filePointer, $content); |
|
263 | - fflush($filePointer); |
|
264 | - flock($filePointer, LOCK_UN); |
|
265 | - fclose($filePointer); |
|
260 | + // Write the config and release the lock |
|
261 | + ftruncate ($filePointer, 0); |
|
262 | + fwrite($filePointer, $content); |
|
263 | + fflush($filePointer); |
|
264 | + flock($filePointer, LOCK_UN); |
|
265 | + fclose($filePointer); |
|
266 | 266 | |
267 | - // Try invalidating the opcache just for the file we wrote... |
|
268 | - if (!\OC_Util::deleteFromOpcodeCache($this->configFilePath)) { |
|
269 | - // But if that doesn't work, clear the whole cache. |
|
270 | - \OC_Util::clearOpcodeCache(); |
|
271 | - } |
|
272 | - } |
|
267 | + // Try invalidating the opcache just for the file we wrote... |
|
268 | + if (!\OC_Util::deleteFromOpcodeCache($this->configFilePath)) { |
|
269 | + // But if that doesn't work, clear the whole cache. |
|
270 | + \OC_Util::clearOpcodeCache(); |
|
271 | + } |
|
272 | + } |
|
273 | 273 | } |
274 | 274 |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | * @return mixed the value or $default |
86 | 86 | */ |
87 | 87 | public function getValue($key, $default = null) { |
88 | - $envValue = getenv(self::ENV_PREFIX . $key); |
|
88 | + $envValue = getenv(self::ENV_PREFIX.$key); |
|
89 | 89 | if ($envValue !== false) { |
90 | 90 | return $envValue; |
91 | 91 | } |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | foreach ($configFiles as $file) { |
198 | 198 | $fileExistsAndIsReadable = file_exists($file) && is_readable($file); |
199 | 199 | $filePointer = $fileExistsAndIsReadable ? fopen($file, 'r') : false; |
200 | - if($file === $this->configFilePath && |
|
200 | + if ($file === $this->configFilePath && |
|
201 | 201 | $filePointer === false) { |
202 | 202 | // Opening the main config might not be possible, e.g. if the wrong |
203 | 203 | // permissions are set (likely on a new installation) |
@@ -205,13 +205,13 @@ discard block |
||
205 | 205 | } |
206 | 206 | |
207 | 207 | // Try to acquire a file lock |
208 | - if(!flock($filePointer, LOCK_SH)) { |
|
208 | + if (!flock($filePointer, LOCK_SH)) { |
|
209 | 209 | throw new \Exception(sprintf('Could not acquire a shared lock on the config file %s', $file)); |
210 | 210 | } |
211 | 211 | |
212 | 212 | unset($CONFIG); |
213 | 213 | include $file; |
214 | - if(isset($CONFIG) && is_array($CONFIG)) { |
|
214 | + if (isset($CONFIG) && is_array($CONFIG)) { |
|
215 | 215 | $this->cache = array_merge($this->cache, $CONFIG); |
216 | 216 | } |
217 | 217 | |
@@ -236,29 +236,29 @@ discard block |
||
236 | 236 | $content .= var_export($this->cache, true); |
237 | 237 | $content .= ";\n"; |
238 | 238 | |
239 | - touch ($this->configFilePath); |
|
239 | + touch($this->configFilePath); |
|
240 | 240 | $filePointer = fopen($this->configFilePath, 'r+'); |
241 | 241 | |
242 | 242 | // Prevent others not to read the config |
243 | 243 | chmod($this->configFilePath, 0640); |
244 | 244 | |
245 | 245 | // File does not exist, this can happen when doing a fresh install |
246 | - if(!is_resource ($filePointer)) { |
|
246 | + if (!is_resource($filePointer)) { |
|
247 | 247 | // TODO fix this via DI once it is very clear that this doesn't cause side effects due to initialization order |
248 | 248 | // currently this breaks app routes but also could have other side effects especially during setup and exception handling |
249 | 249 | $url = \OC::$server->getURLGenerator()->linkToDocs('admin-dir_permissions'); |
250 | 250 | throw new HintException( |
251 | 251 | "Can't write into config directory!", |
252 | - 'This can usually be fixed by giving the webserver write access to the config directory. See ' . $url); |
|
252 | + 'This can usually be fixed by giving the webserver write access to the config directory. See '.$url); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | // Try to acquire a file lock |
256 | - if(!flock($filePointer, LOCK_EX)) { |
|
256 | + if (!flock($filePointer, LOCK_EX)) { |
|
257 | 257 | throw new \Exception(sprintf('Could not acquire an exclusive lock on the config file %s', $this->configFilePath)); |
258 | 258 | } |
259 | 259 | |
260 | 260 | // Write the config and release the lock |
261 | - ftruncate ($filePointer, 0); |
|
261 | + ftruncate($filePointer, 0); |
|
262 | 262 | fwrite($filePointer, $content); |
263 | 263 | fflush($filePointer); |
264 | 264 | flock($filePointer, LOCK_UN); |
@@ -25,36 +25,36 @@ |
||
25 | 25 | |
26 | 26 | class LanguageParseChecker { |
27 | 27 | |
28 | - /** |
|
29 | - * @param string $appId |
|
30 | - * @return array |
|
31 | - */ |
|
32 | - public function analyse($appId) { |
|
33 | - $appPath = \OC_App::getAppPath($appId); |
|
34 | - if ($appPath === false) { |
|
35 | - throw new \RuntimeException("No app with given id <$appId> known."); |
|
36 | - } |
|
37 | - |
|
38 | - if (!is_dir($appPath . '/l10n/')) { |
|
39 | - return []; |
|
40 | - } |
|
41 | - |
|
42 | - $errors = []; |
|
43 | - $directory = new \DirectoryIterator($appPath . '/l10n/'); |
|
44 | - |
|
45 | - foreach ($directory as $file) { |
|
46 | - if ($file->getExtension() !== 'json') { |
|
47 | - continue; |
|
48 | - } |
|
49 | - |
|
50 | - $content = file_get_contents($file->getPathname()); |
|
51 | - json_decode($content, true); |
|
52 | - |
|
53 | - if (json_last_error() !== JSON_ERROR_NONE) { |
|
54 | - $errors[] = 'Invalid language file found: l10n/' . $file->getFilename() . ': ' . json_last_error_msg(); |
|
55 | - } |
|
56 | - } |
|
57 | - |
|
58 | - return $errors; |
|
59 | - } |
|
28 | + /** |
|
29 | + * @param string $appId |
|
30 | + * @return array |
|
31 | + */ |
|
32 | + public function analyse($appId) { |
|
33 | + $appPath = \OC_App::getAppPath($appId); |
|
34 | + if ($appPath === false) { |
|
35 | + throw new \RuntimeException("No app with given id <$appId> known."); |
|
36 | + } |
|
37 | + |
|
38 | + if (!is_dir($appPath . '/l10n/')) { |
|
39 | + return []; |
|
40 | + } |
|
41 | + |
|
42 | + $errors = []; |
|
43 | + $directory = new \DirectoryIterator($appPath . '/l10n/'); |
|
44 | + |
|
45 | + foreach ($directory as $file) { |
|
46 | + if ($file->getExtension() !== 'json') { |
|
47 | + continue; |
|
48 | + } |
|
49 | + |
|
50 | + $content = file_get_contents($file->getPathname()); |
|
51 | + json_decode($content, true); |
|
52 | + |
|
53 | + if (json_last_error() !== JSON_ERROR_NONE) { |
|
54 | + $errors[] = 'Invalid language file found: l10n/' . $file->getFilename() . ': ' . json_last_error_msg(); |
|
55 | + } |
|
56 | + } |
|
57 | + |
|
58 | + return $errors; |
|
59 | + } |
|
60 | 60 | } |
@@ -35,12 +35,12 @@ discard block |
||
35 | 35 | throw new \RuntimeException("No app with given id <$appId> known."); |
36 | 36 | } |
37 | 37 | |
38 | - if (!is_dir($appPath . '/l10n/')) { |
|
38 | + if (!is_dir($appPath.'/l10n/')) { |
|
39 | 39 | return []; |
40 | 40 | } |
41 | 41 | |
42 | 42 | $errors = []; |
43 | - $directory = new \DirectoryIterator($appPath . '/l10n/'); |
|
43 | + $directory = new \DirectoryIterator($appPath.'/l10n/'); |
|
44 | 44 | |
45 | 45 | foreach ($directory as $file) { |
46 | 46 | if ($file->getExtension() !== 'json') { |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | json_decode($content, true); |
52 | 52 | |
53 | 53 | if (json_last_error() !== JSON_ERROR_NONE) { |
54 | - $errors[] = 'Invalid language file found: l10n/' . $file->getFilename() . ': ' . json_last_error_msg(); |
|
54 | + $errors[] = 'Invalid language file found: l10n/'.$file->getFilename().': '.json_last_error_msg(); |
|
55 | 55 | } |
56 | 56 | } |
57 | 57 |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | /** |
53 | 53 | * @param array $urlParams |
54 | 54 | */ |
55 | - public function __construct(array $urlParams=[]){ |
|
55 | + public function __construct(array $urlParams = []) { |
|
56 | 56 | parent::__construct('settings', $urlParams); |
57 | 57 | |
58 | 58 | $container = $this->getContainer(); |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | $container->registerService('isSubAdmin', function(IContainer $c) { |
73 | 73 | $userObject = \OC::$server->getUserSession()->getUser(); |
74 | 74 | $isSubAdmin = false; |
75 | - if($userObject !== null) { |
|
75 | + if ($userObject !== null) { |
|
76 | 76 | $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
77 | 77 | } |
78 | 78 | return $isSubAdmin; |
@@ -80,17 +80,17 @@ discard block |
||
80 | 80 | $container->registerService('userCertificateManager', function(IContainer $c) { |
81 | 81 | return $c->query('ServerContainer')->getCertificateManager(); |
82 | 82 | }, false); |
83 | - $container->registerService('systemCertificateManager', function (IContainer $c) { |
|
83 | + $container->registerService('systemCertificateManager', function(IContainer $c) { |
|
84 | 84 | return $c->query('ServerContainer')->getCertificateManager(null); |
85 | 85 | }, false); |
86 | - $container->registerService(IProvider::class, function (IContainer $c) { |
|
86 | + $container->registerService(IProvider::class, function(IContainer $c) { |
|
87 | 87 | return $c->query('ServerContainer')->query(IProvider::class); |
88 | 88 | }); |
89 | - $container->registerService(IManager::class, function (IContainer $c) { |
|
89 | + $container->registerService(IManager::class, function(IContainer $c) { |
|
90 | 90 | return $c->query('ServerContainer')->getSettingsManager(); |
91 | 91 | }); |
92 | 92 | |
93 | - $container->registerService(NewUserMailHelper::class, function (IContainer $c) { |
|
93 | + $container->registerService(NewUserMailHelper::class, function(IContainer $c) { |
|
94 | 94 | /** @var Server $server */ |
95 | 95 | $server = $c->query('ServerContainer'); |
96 | 96 | /** @var Defaults $defaults */ |
@@ -50,122 +50,122 @@ |
||
50 | 50 | class Application extends App { |
51 | 51 | |
52 | 52 | |
53 | - /** |
|
54 | - * @param array $urlParams |
|
55 | - */ |
|
56 | - public function __construct(array $urlParams=[]){ |
|
57 | - parent::__construct('settings', $urlParams); |
|
58 | - |
|
59 | - $container = $this->getContainer(); |
|
60 | - |
|
61 | - // Register Middleware |
|
62 | - $container->registerAlias('SubadminMiddleware', SubadminMiddleware::class); |
|
63 | - $container->registerMiddleWare('SubadminMiddleware'); |
|
64 | - |
|
65 | - /** |
|
66 | - * Core class wrappers |
|
67 | - */ |
|
68 | - /** FIXME: Remove once OC_User is non-static and mockable */ |
|
69 | - $container->registerService('isAdmin', function() { |
|
70 | - return \OC_User::isAdminUser(\OC_User::getUser()); |
|
71 | - }); |
|
72 | - /** FIXME: Remove once OC_SubAdmin is non-static and mockable */ |
|
73 | - $container->registerService('isSubAdmin', function(IContainer $c) { |
|
74 | - $userObject = \OC::$server->getUserSession()->getUser(); |
|
75 | - $isSubAdmin = false; |
|
76 | - if($userObject !== null) { |
|
77 | - $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
78 | - } |
|
79 | - return $isSubAdmin; |
|
80 | - }); |
|
81 | - $container->registerService('userCertificateManager', function(IContainer $c) { |
|
82 | - return $c->query('ServerContainer')->getCertificateManager(); |
|
83 | - }, false); |
|
84 | - $container->registerService('systemCertificateManager', function (IContainer $c) { |
|
85 | - return $c->query('ServerContainer')->getCertificateManager(null); |
|
86 | - }, false); |
|
87 | - $container->registerService(IProvider::class, function (IContainer $c) { |
|
88 | - return $c->query('ServerContainer')->query(IProvider::class); |
|
89 | - }); |
|
90 | - $container->registerService(IManager::class, function (IContainer $c) { |
|
91 | - return $c->query('ServerContainer')->getSettingsManager(); |
|
92 | - }); |
|
93 | - |
|
94 | - $container->registerService(NewUserMailHelper::class, function (IContainer $c) { |
|
95 | - /** @var Server $server */ |
|
96 | - $server = $c->query('ServerContainer'); |
|
97 | - /** @var Defaults $defaults */ |
|
98 | - $defaults = $server->query(Defaults::class); |
|
99 | - |
|
100 | - return new NewUserMailHelper( |
|
101 | - $defaults, |
|
102 | - $server->getURLGenerator(), |
|
103 | - $server->getL10NFactory(), |
|
104 | - $server->getMailer(), |
|
105 | - $server->getSecureRandom(), |
|
106 | - new TimeFactory(), |
|
107 | - $server->getConfig(), |
|
108 | - $server->getCrypto(), |
|
109 | - Util::getDefaultEmailAddress('no-reply') |
|
110 | - ); |
|
111 | - }); |
|
112 | - } |
|
113 | - |
|
114 | - public function register() { |
|
115 | - $activityManager = $this->getContainer()->getServer()->getActivityManager(); |
|
116 | - $activityManager->registerSetting(Setting::class); // FIXME move to info.xml |
|
117 | - $activityManager->registerProvider(Provider::class); // FIXME move to info.xml |
|
118 | - $activityManager->registerFilter(SecurityFilter::class); // FIXME move to info.xml |
|
119 | - $activityManager->registerSetting(SecuritySetting::class); // FIXME move to info.xml |
|
120 | - $activityManager->registerProvider(SecurityProvider::class); // FIXME move to info.xml |
|
121 | - |
|
122 | - Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword'); |
|
123 | - Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo'); |
|
124 | - |
|
125 | - Util::connectHook('\OCP\Config', 'js', $this, 'extendJsConfig'); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @param array $parameters |
|
130 | - * @throws \InvalidArgumentException |
|
131 | - * @throws \BadMethodCallException |
|
132 | - * @throws \Exception |
|
133 | - * @throws \OCP\AppFramework\QueryException |
|
134 | - */ |
|
135 | - public function onChangePassword(array $parameters) { |
|
136 | - /** @var Hooks $hooks */ |
|
137 | - $hooks = $this->getContainer()->query(Hooks::class); |
|
138 | - $hooks->onChangePassword($parameters['uid']); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @param array $parameters |
|
143 | - * @throws \InvalidArgumentException |
|
144 | - * @throws \BadMethodCallException |
|
145 | - * @throws \Exception |
|
146 | - * @throws \OCP\AppFramework\QueryException |
|
147 | - */ |
|
148 | - public function onChangeInfo(array $parameters) { |
|
149 | - if ($parameters['feature'] !== 'eMailAddress') { |
|
150 | - return; |
|
151 | - } |
|
152 | - |
|
153 | - /** @var Hooks $hooks */ |
|
154 | - $hooks = $this->getContainer()->query(Hooks::class); |
|
155 | - $hooks->onChangeEmail($parameters['user'], $parameters['old_value']); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * @param array $settings |
|
160 | - */ |
|
161 | - public function extendJsConfig(array $settings) { |
|
162 | - $appConfig = json_decode($settings['array']['oc_appconfig'], true); |
|
163 | - |
|
164 | - $publicWebFinger = \OC::$server->getConfig()->getAppValue('core', 'public_webfinger', ''); |
|
165 | - if (!empty($publicWebFinger)) { |
|
166 | - $appConfig['core']['public_webfinger'] = $publicWebFinger; |
|
167 | - } |
|
168 | - |
|
169 | - $settings['array']['oc_appconfig'] = json_encode($appConfig); |
|
170 | - } |
|
53 | + /** |
|
54 | + * @param array $urlParams |
|
55 | + */ |
|
56 | + public function __construct(array $urlParams=[]){ |
|
57 | + parent::__construct('settings', $urlParams); |
|
58 | + |
|
59 | + $container = $this->getContainer(); |
|
60 | + |
|
61 | + // Register Middleware |
|
62 | + $container->registerAlias('SubadminMiddleware', SubadminMiddleware::class); |
|
63 | + $container->registerMiddleWare('SubadminMiddleware'); |
|
64 | + |
|
65 | + /** |
|
66 | + * Core class wrappers |
|
67 | + */ |
|
68 | + /** FIXME: Remove once OC_User is non-static and mockable */ |
|
69 | + $container->registerService('isAdmin', function() { |
|
70 | + return \OC_User::isAdminUser(\OC_User::getUser()); |
|
71 | + }); |
|
72 | + /** FIXME: Remove once OC_SubAdmin is non-static and mockable */ |
|
73 | + $container->registerService('isSubAdmin', function(IContainer $c) { |
|
74 | + $userObject = \OC::$server->getUserSession()->getUser(); |
|
75 | + $isSubAdmin = false; |
|
76 | + if($userObject !== null) { |
|
77 | + $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
78 | + } |
|
79 | + return $isSubAdmin; |
|
80 | + }); |
|
81 | + $container->registerService('userCertificateManager', function(IContainer $c) { |
|
82 | + return $c->query('ServerContainer')->getCertificateManager(); |
|
83 | + }, false); |
|
84 | + $container->registerService('systemCertificateManager', function (IContainer $c) { |
|
85 | + return $c->query('ServerContainer')->getCertificateManager(null); |
|
86 | + }, false); |
|
87 | + $container->registerService(IProvider::class, function (IContainer $c) { |
|
88 | + return $c->query('ServerContainer')->query(IProvider::class); |
|
89 | + }); |
|
90 | + $container->registerService(IManager::class, function (IContainer $c) { |
|
91 | + return $c->query('ServerContainer')->getSettingsManager(); |
|
92 | + }); |
|
93 | + |
|
94 | + $container->registerService(NewUserMailHelper::class, function (IContainer $c) { |
|
95 | + /** @var Server $server */ |
|
96 | + $server = $c->query('ServerContainer'); |
|
97 | + /** @var Defaults $defaults */ |
|
98 | + $defaults = $server->query(Defaults::class); |
|
99 | + |
|
100 | + return new NewUserMailHelper( |
|
101 | + $defaults, |
|
102 | + $server->getURLGenerator(), |
|
103 | + $server->getL10NFactory(), |
|
104 | + $server->getMailer(), |
|
105 | + $server->getSecureRandom(), |
|
106 | + new TimeFactory(), |
|
107 | + $server->getConfig(), |
|
108 | + $server->getCrypto(), |
|
109 | + Util::getDefaultEmailAddress('no-reply') |
|
110 | + ); |
|
111 | + }); |
|
112 | + } |
|
113 | + |
|
114 | + public function register() { |
|
115 | + $activityManager = $this->getContainer()->getServer()->getActivityManager(); |
|
116 | + $activityManager->registerSetting(Setting::class); // FIXME move to info.xml |
|
117 | + $activityManager->registerProvider(Provider::class); // FIXME move to info.xml |
|
118 | + $activityManager->registerFilter(SecurityFilter::class); // FIXME move to info.xml |
|
119 | + $activityManager->registerSetting(SecuritySetting::class); // FIXME move to info.xml |
|
120 | + $activityManager->registerProvider(SecurityProvider::class); // FIXME move to info.xml |
|
121 | + |
|
122 | + Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword'); |
|
123 | + Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo'); |
|
124 | + |
|
125 | + Util::connectHook('\OCP\Config', 'js', $this, 'extendJsConfig'); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @param array $parameters |
|
130 | + * @throws \InvalidArgumentException |
|
131 | + * @throws \BadMethodCallException |
|
132 | + * @throws \Exception |
|
133 | + * @throws \OCP\AppFramework\QueryException |
|
134 | + */ |
|
135 | + public function onChangePassword(array $parameters) { |
|
136 | + /** @var Hooks $hooks */ |
|
137 | + $hooks = $this->getContainer()->query(Hooks::class); |
|
138 | + $hooks->onChangePassword($parameters['uid']); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @param array $parameters |
|
143 | + * @throws \InvalidArgumentException |
|
144 | + * @throws \BadMethodCallException |
|
145 | + * @throws \Exception |
|
146 | + * @throws \OCP\AppFramework\QueryException |
|
147 | + */ |
|
148 | + public function onChangeInfo(array $parameters) { |
|
149 | + if ($parameters['feature'] !== 'eMailAddress') { |
|
150 | + return; |
|
151 | + } |
|
152 | + |
|
153 | + /** @var Hooks $hooks */ |
|
154 | + $hooks = $this->getContainer()->query(Hooks::class); |
|
155 | + $hooks->onChangeEmail($parameters['user'], $parameters['old_value']); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * @param array $settings |
|
160 | + */ |
|
161 | + public function extendJsConfig(array $settings) { |
|
162 | + $appConfig = json_decode($settings['array']['oc_appconfig'], true); |
|
163 | + |
|
164 | + $publicWebFinger = \OC::$server->getConfig()->getAppValue('core', 'public_webfinger', ''); |
|
165 | + if (!empty($publicWebFinger)) { |
|
166 | + $appConfig['core']['public_webfinger'] = $publicWebFinger; |
|
167 | + } |
|
168 | + |
|
169 | + $settings['array']['oc_appconfig'] = json_encode($appConfig); |
|
170 | + } |
|
171 | 171 | } |
@@ -22,10 +22,10 @@ |
||
22 | 22 | $dispatcher = \OC::$server->getEventDispatcher(); |
23 | 23 | |
24 | 24 | $dispatcher->addListener('OC\AccountManager::userUpdated', function(\Symfony\Component\EventDispatcher\GenericEvent $event) { |
25 | - /** @var \OCP\IUser $user */ |
|
26 | - $user = $event->getSubject(); |
|
25 | + /** @var \OCP\IUser $user */ |
|
26 | + $user = $event->getSubject(); |
|
27 | 27 | |
28 | - /** @var \OCA\LookupServerConnector\UpdateLookupServer $updateLookupServer */ |
|
29 | - $updateLookupServer = \OC::$server->query(\OCA\LookupServerConnector\UpdateLookupServer::class); |
|
30 | - $updateLookupServer->userUpdated($user); |
|
28 | + /** @var \OCA\LookupServerConnector\UpdateLookupServer $updateLookupServer */ |
|
29 | + $updateLookupServer = \OC::$server->query(\OCA\LookupServerConnector\UpdateLookupServer::class); |
|
30 | + $updateLookupServer->userUpdated($user); |
|
31 | 31 | }); |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | $json = file_get_contents('php://stdin'); |
113 | 113 | } else { |
114 | 114 | if (!file_exists($path)) { |
115 | - $output->writeln('<error>File not found: ' . $path . '</error>'); |
|
115 | + $output->writeln('<error>File not found: '.$path.'</error>'); |
|
116 | 116 | return 1; |
117 | 117 | } |
118 | 118 | $json = file_get_contents($path); |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | $existingMount->getApplicableUsers() === $mount->getApplicableUsers() && |
165 | 165 | $existingMount->getBackendOptions() === $mount->getBackendOptions() |
166 | 166 | ) { |
167 | - $output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>"); |
|
167 | + $output->writeln("<error>Duplicate mount (".$mount->getMountPoint().")</error>"); |
|
168 | 168 | return 1; |
169 | 169 | } |
170 | 170 | } |
@@ -39,187 +39,187 @@ |
||
39 | 39 | use Symfony\Component\Console\Output\OutputInterface; |
40 | 40 | |
41 | 41 | class Import extends Base { |
42 | - /** |
|
43 | - * @var GlobalStoragesService |
|
44 | - */ |
|
45 | - private $globalService; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var UserStoragesService |
|
49 | - */ |
|
50 | - private $userService; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var IUserSession |
|
54 | - */ |
|
55 | - private $userSession; |
|
56 | - |
|
57 | - /** |
|
58 | - * @var IUserManager |
|
59 | - */ |
|
60 | - private $userManager; |
|
61 | - |
|
62 | - /** @var ImportLegacyStoragesService */ |
|
63 | - private $importLegacyStorageService; |
|
64 | - |
|
65 | - /** @var BackendService */ |
|
66 | - private $backendService; |
|
67 | - |
|
68 | - function __construct(GlobalStoragesService $globalService, |
|
69 | - UserStoragesService $userService, |
|
70 | - IUserSession $userSession, |
|
71 | - IUserManager $userManager, |
|
72 | - ImportLegacyStoragesService $importLegacyStorageService, |
|
73 | - BackendService $backendService |
|
74 | - ) { |
|
75 | - parent::__construct(); |
|
76 | - $this->globalService = $globalService; |
|
77 | - $this->userService = $userService; |
|
78 | - $this->userSession = $userSession; |
|
79 | - $this->userManager = $userManager; |
|
80 | - $this->importLegacyStorageService = $importLegacyStorageService; |
|
81 | - $this->backendService = $backendService; |
|
82 | - } |
|
83 | - |
|
84 | - protected function configure() { |
|
85 | - $this |
|
86 | - ->setName('files_external:import') |
|
87 | - ->setDescription('Import mount configurations') |
|
88 | - ->addOption( |
|
89 | - 'user', |
|
90 | - '', |
|
91 | - InputOption::VALUE_OPTIONAL, |
|
92 | - 'user to add the mount configurations for, if not set the mount will be added as system mount' |
|
93 | - ) |
|
94 | - ->addArgument( |
|
95 | - 'path', |
|
96 | - InputArgument::REQUIRED, |
|
97 | - 'path to a json file containing the mounts to import, use "-" to read from stdin' |
|
98 | - ) |
|
99 | - ->addOption( |
|
100 | - 'dry', |
|
101 | - '', |
|
102 | - InputOption::VALUE_NONE, |
|
103 | - 'Don\'t save the imported mounts, only list the new mounts' |
|
104 | - ); |
|
105 | - parent::configure(); |
|
106 | - } |
|
107 | - |
|
108 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
109 | - $user = $input->getOption('user'); |
|
110 | - $path = $input->getArgument('path'); |
|
111 | - if ($path === '-') { |
|
112 | - $json = file_get_contents('php://stdin'); |
|
113 | - } else { |
|
114 | - if (!file_exists($path)) { |
|
115 | - $output->writeln('<error>File not found: ' . $path . '</error>'); |
|
116 | - return 1; |
|
117 | - } |
|
118 | - $json = file_get_contents($path); |
|
119 | - } |
|
120 | - if (!is_string($json) || strlen($json) < 2) { |
|
121 | - $output->writeln('<error>Error while reading json</error>'); |
|
122 | - return 1; |
|
123 | - } |
|
124 | - $data = json_decode($json, true); |
|
125 | - if (!is_array($data)) { |
|
126 | - $output->writeln('<error>Error while parsing json</error>'); |
|
127 | - return 1; |
|
128 | - } |
|
129 | - |
|
130 | - $isLegacy = isset($data['user']) || isset($data['group']); |
|
131 | - if ($isLegacy) { |
|
132 | - $this->importLegacyStorageService->setData($data); |
|
133 | - $mounts = $this->importLegacyStorageService->getAllStorages(); |
|
134 | - foreach ($mounts as $mount) { |
|
135 | - if ($mount->getBackendOption('password') === false) { |
|
136 | - $output->writeln('<error>Failed to decrypt password</error>'); |
|
137 | - return 1; |
|
138 | - } |
|
139 | - } |
|
140 | - } else { |
|
141 | - if (!isset($data[0])) { //normalize to an array of mounts |
|
142 | - $data = [$data]; |
|
143 | - } |
|
144 | - $mounts = array_map([$this, 'parseData'], $data); |
|
145 | - } |
|
146 | - |
|
147 | - if ($user) { |
|
148 | - // ensure applicables are correct for personal mounts |
|
149 | - foreach ($mounts as $mount) { |
|
150 | - $mount->setApplicableGroups([]); |
|
151 | - $mount->setApplicableUsers([$user]); |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - $storageService = $this->getStorageService($user); |
|
156 | - |
|
157 | - $existingMounts = $storageService->getAllStorages(); |
|
158 | - |
|
159 | - foreach ($mounts as $mount) { |
|
160 | - foreach ($existingMounts as $existingMount) { |
|
161 | - if ( |
|
162 | - $existingMount->getMountPoint() === $mount->getMountPoint() && |
|
163 | - $existingMount->getApplicableGroups() === $mount->getApplicableGroups() && |
|
164 | - $existingMount->getApplicableUsers() === $mount->getApplicableUsers() && |
|
165 | - $existingMount->getBackendOptions() === $mount->getBackendOptions() |
|
166 | - ) { |
|
167 | - $output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>"); |
|
168 | - return 1; |
|
169 | - } |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - if ($input->getOption('dry')) { |
|
174 | - if (count($mounts) === 0) { |
|
175 | - $output->writeln('<error>No mounts to be imported</error>'); |
|
176 | - return 1; |
|
177 | - } |
|
178 | - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
179 | - $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
180 | - $listInput->setOption('output', $input->getOption('output')); |
|
181 | - $listInput->setOption('show-password', true); |
|
182 | - $listCommand->listMounts($user, $mounts, $listInput, $output); |
|
183 | - } else { |
|
184 | - foreach ($mounts as $mount) { |
|
185 | - $storageService->addStorage($mount); |
|
186 | - } |
|
187 | - } |
|
188 | - return 0; |
|
189 | - } |
|
190 | - |
|
191 | - private function parseData(array $data) { |
|
192 | - $mount = new StorageConfig($data['mount_id']); |
|
193 | - $mount->setMountPoint($data['mount_point']); |
|
194 | - $mount->setBackend($this->getBackendByClass($data['storage'])); |
|
195 | - $authBackend = $this->backendService->getAuthMechanism($data['authentication_type']); |
|
196 | - $mount->setAuthMechanism($authBackend); |
|
197 | - $mount->setBackendOptions($data['configuration']); |
|
198 | - $mount->setMountOptions($data['options']); |
|
199 | - $mount->setApplicableUsers(isset($data['applicable_users']) ? $data['applicable_users'] : []); |
|
200 | - $mount->setApplicableGroups(isset($data['applicable_groups']) ? $data['applicable_groups'] : []); |
|
201 | - return $mount; |
|
202 | - } |
|
203 | - |
|
204 | - private function getBackendByClass($className) { |
|
205 | - $backends = $this->backendService->getBackends(); |
|
206 | - foreach ($backends as $backend) { |
|
207 | - if ($backend->getStorageClass() === $className) { |
|
208 | - return $backend; |
|
209 | - } |
|
210 | - } |
|
211 | - } |
|
212 | - |
|
213 | - protected function getStorageService($userId) { |
|
214 | - if (!empty($userId)) { |
|
215 | - $user = $this->userManager->get($userId); |
|
216 | - if (is_null($user)) { |
|
217 | - throw new NoUserException("user $userId not found"); |
|
218 | - } |
|
219 | - $this->userSession->setUser($user); |
|
220 | - return $this->userService; |
|
221 | - } else { |
|
222 | - return $this->globalService; |
|
223 | - } |
|
224 | - } |
|
42 | + /** |
|
43 | + * @var GlobalStoragesService |
|
44 | + */ |
|
45 | + private $globalService; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var UserStoragesService |
|
49 | + */ |
|
50 | + private $userService; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var IUserSession |
|
54 | + */ |
|
55 | + private $userSession; |
|
56 | + |
|
57 | + /** |
|
58 | + * @var IUserManager |
|
59 | + */ |
|
60 | + private $userManager; |
|
61 | + |
|
62 | + /** @var ImportLegacyStoragesService */ |
|
63 | + private $importLegacyStorageService; |
|
64 | + |
|
65 | + /** @var BackendService */ |
|
66 | + private $backendService; |
|
67 | + |
|
68 | + function __construct(GlobalStoragesService $globalService, |
|
69 | + UserStoragesService $userService, |
|
70 | + IUserSession $userSession, |
|
71 | + IUserManager $userManager, |
|
72 | + ImportLegacyStoragesService $importLegacyStorageService, |
|
73 | + BackendService $backendService |
|
74 | + ) { |
|
75 | + parent::__construct(); |
|
76 | + $this->globalService = $globalService; |
|
77 | + $this->userService = $userService; |
|
78 | + $this->userSession = $userSession; |
|
79 | + $this->userManager = $userManager; |
|
80 | + $this->importLegacyStorageService = $importLegacyStorageService; |
|
81 | + $this->backendService = $backendService; |
|
82 | + } |
|
83 | + |
|
84 | + protected function configure() { |
|
85 | + $this |
|
86 | + ->setName('files_external:import') |
|
87 | + ->setDescription('Import mount configurations') |
|
88 | + ->addOption( |
|
89 | + 'user', |
|
90 | + '', |
|
91 | + InputOption::VALUE_OPTIONAL, |
|
92 | + 'user to add the mount configurations for, if not set the mount will be added as system mount' |
|
93 | + ) |
|
94 | + ->addArgument( |
|
95 | + 'path', |
|
96 | + InputArgument::REQUIRED, |
|
97 | + 'path to a json file containing the mounts to import, use "-" to read from stdin' |
|
98 | + ) |
|
99 | + ->addOption( |
|
100 | + 'dry', |
|
101 | + '', |
|
102 | + InputOption::VALUE_NONE, |
|
103 | + 'Don\'t save the imported mounts, only list the new mounts' |
|
104 | + ); |
|
105 | + parent::configure(); |
|
106 | + } |
|
107 | + |
|
108 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
109 | + $user = $input->getOption('user'); |
|
110 | + $path = $input->getArgument('path'); |
|
111 | + if ($path === '-') { |
|
112 | + $json = file_get_contents('php://stdin'); |
|
113 | + } else { |
|
114 | + if (!file_exists($path)) { |
|
115 | + $output->writeln('<error>File not found: ' . $path . '</error>'); |
|
116 | + return 1; |
|
117 | + } |
|
118 | + $json = file_get_contents($path); |
|
119 | + } |
|
120 | + if (!is_string($json) || strlen($json) < 2) { |
|
121 | + $output->writeln('<error>Error while reading json</error>'); |
|
122 | + return 1; |
|
123 | + } |
|
124 | + $data = json_decode($json, true); |
|
125 | + if (!is_array($data)) { |
|
126 | + $output->writeln('<error>Error while parsing json</error>'); |
|
127 | + return 1; |
|
128 | + } |
|
129 | + |
|
130 | + $isLegacy = isset($data['user']) || isset($data['group']); |
|
131 | + if ($isLegacy) { |
|
132 | + $this->importLegacyStorageService->setData($data); |
|
133 | + $mounts = $this->importLegacyStorageService->getAllStorages(); |
|
134 | + foreach ($mounts as $mount) { |
|
135 | + if ($mount->getBackendOption('password') === false) { |
|
136 | + $output->writeln('<error>Failed to decrypt password</error>'); |
|
137 | + return 1; |
|
138 | + } |
|
139 | + } |
|
140 | + } else { |
|
141 | + if (!isset($data[0])) { //normalize to an array of mounts |
|
142 | + $data = [$data]; |
|
143 | + } |
|
144 | + $mounts = array_map([$this, 'parseData'], $data); |
|
145 | + } |
|
146 | + |
|
147 | + if ($user) { |
|
148 | + // ensure applicables are correct for personal mounts |
|
149 | + foreach ($mounts as $mount) { |
|
150 | + $mount->setApplicableGroups([]); |
|
151 | + $mount->setApplicableUsers([$user]); |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + $storageService = $this->getStorageService($user); |
|
156 | + |
|
157 | + $existingMounts = $storageService->getAllStorages(); |
|
158 | + |
|
159 | + foreach ($mounts as $mount) { |
|
160 | + foreach ($existingMounts as $existingMount) { |
|
161 | + if ( |
|
162 | + $existingMount->getMountPoint() === $mount->getMountPoint() && |
|
163 | + $existingMount->getApplicableGroups() === $mount->getApplicableGroups() && |
|
164 | + $existingMount->getApplicableUsers() === $mount->getApplicableUsers() && |
|
165 | + $existingMount->getBackendOptions() === $mount->getBackendOptions() |
|
166 | + ) { |
|
167 | + $output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>"); |
|
168 | + return 1; |
|
169 | + } |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + if ($input->getOption('dry')) { |
|
174 | + if (count($mounts) === 0) { |
|
175 | + $output->writeln('<error>No mounts to be imported</error>'); |
|
176 | + return 1; |
|
177 | + } |
|
178 | + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
179 | + $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
180 | + $listInput->setOption('output', $input->getOption('output')); |
|
181 | + $listInput->setOption('show-password', true); |
|
182 | + $listCommand->listMounts($user, $mounts, $listInput, $output); |
|
183 | + } else { |
|
184 | + foreach ($mounts as $mount) { |
|
185 | + $storageService->addStorage($mount); |
|
186 | + } |
|
187 | + } |
|
188 | + return 0; |
|
189 | + } |
|
190 | + |
|
191 | + private function parseData(array $data) { |
|
192 | + $mount = new StorageConfig($data['mount_id']); |
|
193 | + $mount->setMountPoint($data['mount_point']); |
|
194 | + $mount->setBackend($this->getBackendByClass($data['storage'])); |
|
195 | + $authBackend = $this->backendService->getAuthMechanism($data['authentication_type']); |
|
196 | + $mount->setAuthMechanism($authBackend); |
|
197 | + $mount->setBackendOptions($data['configuration']); |
|
198 | + $mount->setMountOptions($data['options']); |
|
199 | + $mount->setApplicableUsers(isset($data['applicable_users']) ? $data['applicable_users'] : []); |
|
200 | + $mount->setApplicableGroups(isset($data['applicable_groups']) ? $data['applicable_groups'] : []); |
|
201 | + return $mount; |
|
202 | + } |
|
203 | + |
|
204 | + private function getBackendByClass($className) { |
|
205 | + $backends = $this->backendService->getBackends(); |
|
206 | + foreach ($backends as $backend) { |
|
207 | + if ($backend->getStorageClass() === $className) { |
|
208 | + return $backend; |
|
209 | + } |
|
210 | + } |
|
211 | + } |
|
212 | + |
|
213 | + protected function getStorageService($userId) { |
|
214 | + if (!empty($userId)) { |
|
215 | + $user = $this->userManager->get($userId); |
|
216 | + if (is_null($user)) { |
|
217 | + throw new NoUserException("user $userId not found"); |
|
218 | + } |
|
219 | + $this->userSession->setUser($user); |
|
220 | + return $this->userService; |
|
221 | + } else { |
|
222 | + return $this->globalService; |
|
223 | + } |
|
224 | + } |
|
225 | 225 | } |